class-wpseo-option-wpseo.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Internals\Options
  6. */
  7. /**
  8. * Option: wpseo.
  9. */
  10. class WPSEO_Option_Wpseo extends WPSEO_Option {
  11. /**
  12. * @var string Option name.
  13. */
  14. public $option_name = 'wpseo';
  15. /**
  16. * @var array Array of defaults for the option.
  17. * Shouldn't be requested directly, use $this->get_defaults();
  18. */
  19. protected $defaults = array(
  20. // Non-form fields, set via (ajax) function.
  21. 'ms_defaults_set' => false,
  22. // Non-form field, should only be set via validation routine.
  23. 'version' => '', // Leave default as empty to ensure activation/upgrade works.
  24. // Form fields.
  25. 'disableadvanced_meta' => true,
  26. 'onpage_indexability' => true,
  27. 'baiduverify' => '', // Text field.
  28. 'googleverify' => '', // Text field.
  29. 'msverify' => '', // Text field.
  30. 'yandexverify' => '',
  31. 'site_type' => '', // List of options.
  32. 'has_multiple_authors' => '',
  33. 'environment_type' => '',
  34. 'content_analysis_active' => true,
  35. 'keyword_analysis_active' => true,
  36. 'enable_admin_bar_menu' => true,
  37. 'enable_cornerstone_content' => true,
  38. 'enable_xml_sitemap' => true,
  39. 'enable_text_link_counter' => true,
  40. 'show_onboarding_notice' => false,
  41. 'first_activated_on' => false,
  42. );
  43. /**
  44. * @var array Sub-options which should not be overloaded with multi-site defaults.
  45. */
  46. public $ms_exclude = array(
  47. /* Privacy. */
  48. 'baiduverify',
  49. 'googleverify',
  50. 'msverify',
  51. 'yandexverify',
  52. );
  53. /** @var array Possible values for the site_type option. */
  54. protected $site_types = array(
  55. '',
  56. 'blog',
  57. 'shop',
  58. 'news',
  59. 'smallBusiness',
  60. 'corporateOther',
  61. 'personalOther',
  62. );
  63. /** @var array Possible environment types. */
  64. protected $environment_types = array(
  65. '',
  66. 'production',
  67. 'staging',
  68. 'development',
  69. );
  70. /** @var array Possible has_multiple_authors options. */
  71. protected $has_multiple_authors_options = array(
  72. '',
  73. true,
  74. false,
  75. );
  76. /**
  77. * Add the actions and filters for the option.
  78. *
  79. * @todo [JRF => testers] Check if the extra actions below would run into problems if an option
  80. * is updated early on and if so, change the call to schedule these for a later action on add/update
  81. * instead of running them straight away.
  82. *
  83. * @return \WPSEO_Option_Wpseo
  84. */
  85. protected function __construct() {
  86. parent::__construct();
  87. /* Clear the cache on update/add. */
  88. add_action( 'add_option_' . $this->option_name, array( 'WPSEO_Utils', 'clear_cache' ) );
  89. add_action( 'update_option_' . $this->option_name, array( 'WPSEO_Utils', 'clear_cache' ) );
  90. /**
  91. * Filter the `wpseo` option defaults.
  92. *
  93. * @param array $defaults Array the defaults for the `wpseo` option attributes.
  94. */
  95. $this->defaults = apply_filters( 'wpseo_option_wpseo_defaults', $this->defaults );
  96. }
  97. /**
  98. * Get the singleton instance of this class.
  99. *
  100. * @return object
  101. */
  102. public static function get_instance() {
  103. if ( ! ( self::$instance instanceof self ) ) {
  104. self::$instance = new self();
  105. }
  106. return self::$instance;
  107. }
  108. /**
  109. * Validate the option.
  110. *
  111. * @param array $dirty New value for the option.
  112. * @param array $clean Clean value for the option, normally the defaults.
  113. * @param array $old Old value of the option.
  114. *
  115. * @return array Validated clean value for the option to be saved to the database.
  116. */
  117. protected function validate_option( $dirty, $clean, $old ) {
  118. foreach ( $clean as $key => $value ) {
  119. switch ( $key ) {
  120. case 'version':
  121. $clean[ $key ] = WPSEO_VERSION;
  122. break;
  123. /* Verification strings. */
  124. case 'baiduverify':
  125. case 'googleverify':
  126. case 'msverify':
  127. case 'yandexverify':
  128. $this->validate_verification_string( $key, $dirty, $old, $clean );
  129. break;
  130. /*
  131. * Boolean dismiss warnings - not fields - may not be in form
  132. * (and don't need to be either as long as the default is false).
  133. */
  134. case 'ms_defaults_set':
  135. if ( isset( $dirty[ $key ] ) ) {
  136. $clean[ $key ] = WPSEO_Utils::validate_bool( $dirty[ $key ] );
  137. }
  138. elseif ( isset( $old[ $key ] ) ) {
  139. $clean[ $key ] = WPSEO_Utils::validate_bool( $old[ $key ] );
  140. }
  141. break;
  142. case 'site_type':
  143. $clean[ $key ] = $old[ $key ];
  144. if ( isset( $dirty[ $key ] ) && in_array( $dirty[ $key ], $this->site_types, true ) ) {
  145. $clean[ $key ] = $dirty[ $key ];
  146. }
  147. break;
  148. case 'environment_type':
  149. $clean[ $key ] = $old[ $key ];
  150. if ( isset( $dirty[ $key ] ) && in_array( $dirty[ $key ], $this->environment_types, true ) ) {
  151. $clean[ $key ] = $dirty[ $key ];
  152. }
  153. break;
  154. case 'has_multiple_authors':
  155. $clean[ $key ] = $old[ $key ];
  156. if ( isset( $dirty[ $key ] ) && in_array( $dirty[ $key ], $this->has_multiple_authors_options, true ) ) {
  157. $clean[ $key ] = $dirty[ $key ];
  158. }
  159. break;
  160. case 'first_activated_on':
  161. $clean[ $key ] = false;
  162. if ( isset( $dirty[ $key ] ) ) {
  163. if ( $dirty[ $key ] === false || WPSEO_Utils::validate_int( $dirty[ $key ] ) ) {
  164. $clean[ $key ] = $dirty[ $key ];
  165. }
  166. }
  167. break;
  168. /*
  169. * Boolean (checkbox) fields.
  170. */
  171. /*
  172. * Covers:
  173. * 'disableadvanced_meta'
  174. * 'yoast_tracking'
  175. */
  176. default:
  177. $clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : false );
  178. break;
  179. }
  180. }
  181. return $clean;
  182. }
  183. /**
  184. * Clean a given option value.
  185. *
  186. * @param array $option_value Old (not merged with defaults or filtered) option value to
  187. * clean according to the rules for this option.
  188. * @param string $current_version Optional. Version from which to upgrade, if not set,
  189. * version specific upgrades will be disregarded.
  190. * @param array $all_old_option_values Optional. Only used when importing old options to have
  191. * access to the real old values, in contrast to the saved ones.
  192. *
  193. * @return array Cleaned option.
  194. */
  195. protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) {
  196. return $option_value;
  197. }
  198. }