PageRenderTime 77ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/wp-content/plugins/wordpress-seo/inc/class-wpseo-options.php

https://github.com/Fishgate/privatecollectionswp
PHP | 4056 lines | 1984 code | 591 blank | 1481 comment | 450 complexity | 8a74451f2d3dd70f3c333871271cefda MD5 | raw file
  1. <?php
  2. /**
  3. * @package Internals
  4. */
  5. // Avoid direct calls to this file
  6. if ( ! defined( 'WPSEO_VERSION' ) ) {
  7. header( 'Status: 403 Forbidden' );
  8. header( 'HTTP/1.1 403 Forbidden' );
  9. exit();
  10. }
  11. if ( ! class_exists( 'WPSEO_Option' ) ) {
  12. /**
  13. * @package WordPress\Plugins\WPSeo
  14. * @subpackage Internals
  15. * @since 1.5.0
  16. * @version 1.5.0
  17. *
  18. * This abstract class and it's concrete classes implement defaults and value validation for
  19. * all WPSEO options and subkeys within options.
  20. *
  21. * Some guidelines:
  22. * [Retrieving options]
  23. * - Use the normal get_option() to retrieve an option. You will receive a complete array for the option.
  24. * Any subkeys which were not set, will have their default values in place.
  25. * - In other words, you will normally not have to check whether a subkey isset() as they will *always* be set.
  26. * They will also *always* be of the correct variable type.
  27. * The only exception to this are the options with variable option names based on post_type or taxonomy
  28. * as those will not always be available before the taxonomy/post_type is registered.
  29. * (they will be available if a value was set, they won't be if it wasn't as the class won't know
  30. * that a default needs to be injected).
  31. * Oh and the very few options where the default value is null, i.e. wpseo->'theme_has_description'
  32. *
  33. * [Updating/Adding options]
  34. * - For multisite site_options, please use the WPSEO_Options::update_site_option() method.
  35. * - For normal options, use the normal add/update_option() functions. As long a the classes here
  36. * are instantiated, validation for all options and their subkeys will be automatic.
  37. * - On (succesfull) update of a couple of options, certain related actions will be run automatically.
  38. * Some examples:
  39. * - on change of wpseo[yoast_tracking], the cron schedule will be adjusted accordingly
  40. * - on change of wpseo_permalinks and wpseo_xml, the rewrite rules will be flushed
  41. * - on change of wpseo and wpseo_title, some caches will be cleared
  42. *
  43. *
  44. * [Important information about add/updating/changing these classes]
  45. * - Make sure that option array key names are unique across options. The WPSEO_Options::get_all()
  46. * method merges most options together. If any of them have non-unique names, even if they
  47. * are in a different option, they *will* overwrite each other.
  48. * - When you add a new array key in an option: make sure you add proper defaults and add the key
  49. * to the validation routine in the proper place or add a new validation case.
  50. * You don't need to do any upgrading as any option returned will always be merged with the
  51. * defaults, so new options will automatically be available.
  52. * If the default value is a string which need translating, add this to the concrete class
  53. * translate_defaults() method.
  54. * - When you remove an array key from an option: if it's important that the option is really removed,
  55. * add the WPSEO_Option::clean_up( $option_name ) method to the upgrade run.
  56. * This will re-save the option and automatically remove the array key no longer in existance.
  57. * - When you rename a sub-option: add it to the clean_option() routine and run that in the upgrade run.
  58. * - When you change the default for an option sub-key, make sure you verify that the validation routine will
  59. * still work the way it should.
  60. * Example: changing a default from '' (empty string) to 'text' with a validation routine with tests
  61. * for an empty string will prevent a user from saving an empty string as the real value. So the
  62. * test for '' with the validation routine would have to be removed in that case.
  63. * - If an option needs specific actions different from defined in this abstract class, you can just overrule
  64. * a method by defining it in the concrete class.
  65. *
  66. *
  67. * @todo - [JRF => testers] double check that validation will not cause errors when called
  68. * from upgrade routine (some of the WP functions may not yet be available)
  69. */
  70. abstract class WPSEO_Option {
  71. /**
  72. * @var string Option name - MUST be set in concrete class and set to public.
  73. */
  74. protected $option_name;
  75. /**
  76. * @var string Option group name for use in settings forms
  77. * - will be set automagically if not set in concrete class
  78. * (i.e. if it confirm to the normal pattern 'yoast' . $option_name . 'options',
  79. * only set in conrete class if it doesn't)
  80. */
  81. public $group_name;
  82. /**
  83. * @var bool Whether to include the option in the return for WPSEO_Options::get_all().
  84. * Also determines which options are copied over for ms_(re)set_blog().
  85. */
  86. public $include_in_all = true;
  87. /**
  88. * @var bool Whether this option is only for when the install is multisite.
  89. */
  90. public $multisite_only = false;
  91. /**
  92. * @var array Array of defaults for the option - MUST be set in concrete class.
  93. * Shouldn't be requested directly, use $this->get_defaults();
  94. */
  95. protected $defaults;
  96. /**
  97. * @var array Array of variable option name patterns for the option - if any -
  98. * Set this when the option contains array keys which vary based on post_type
  99. * or taxonomy
  100. */
  101. protected $variable_array_key_patterns;
  102. /**
  103. * @var array Array of sub-options which should not be overloaded with multi-site defaults
  104. */
  105. public $ms_exclude = array();
  106. /**
  107. * @var object Instance of this class
  108. */
  109. protected static $instance;
  110. /**
  111. *
  112. * @var bool Whether the filter extension is loaded
  113. */
  114. public static $has_filters = true;
  115. /* *********** INSTANTIATION METHODS *********** */
  116. /**
  117. * Add all the actions and filters for the option
  118. *
  119. * @return \WPSEO_Option
  120. */
  121. protected function __construct() {
  122. self::$has_filters = extension_loaded( 'filter' );
  123. /* Add filters which get applied to the get_options() results */
  124. $this->add_default_filters(); // return defaults if option not set
  125. $this->add_option_filters(); // merge with defaults if option *is* set
  126. if ( $this->multisite_only !== true ) {
  127. /* The option validation routines remove the default filters to prevent failing
  128. to insert an option if it's new. Let's add them back afterwards. */
  129. add_action( 'add_option', array( $this, 'add_default_filters' ) ); // adding back after INSERT
  130. if ( version_compare( $GLOBALS['wp_version'], '3.7', '!=' ) ) { // adding back after non-WP 3.7 UPDATE
  131. add_action( 'update_option', array( $this, 'add_default_filters' ) );
  132. } else { // adding back after WP 3.7 UPDATE
  133. add_filter( 'pre_update_option_' . $this->option_name, array( $this, 'wp37_add_default_filters' ) );
  134. }
  135. }
  136. else if ( is_multisite() ) {
  137. /* The option validation routines remove the default filters to prevent failing
  138. to insert an option if it's new. Let's add them back afterwards.
  139. For site_options, this method is not foolproof as these actions are not fired
  140. on an insert/update failure. Please use the WPSEO_Options::update_site_option() method
  141. for updating site options to make sure the filters are in place. */
  142. add_action( 'add_site_option_' . $this->option_name, array( $this, 'add_default_filters' ) );
  143. add_action( 'update_site_option_' . $this->option_name, array( $this, 'add_default_filters' ) );
  144. }
  145. /* Make sure the option will always get validated, independently of register_setting()
  146. (only available on back-end) */
  147. add_filter( 'sanitize_option_' . $this->option_name, array( $this, 'validate' ) );
  148. /* Register our option for the admin pages */
  149. add_action( 'admin_init', array( $this, 'register_setting' ) );
  150. /* Set option group name if not given */
  151. if ( ! isset( $this->group_name ) || $this->group_name === '' ) {
  152. $this->group_name = 'yoast_' . $this->option_name . '_options';
  153. }
  154. /* Translate some defaults as early as possible - textdomain is loaded in init on priority 1 */
  155. if ( method_exists( $this, 'translate_defaults' ) ) {
  156. add_action( 'init', array( $this, 'translate_defaults' ), 2 );
  157. }
  158. /**
  159. * Enrich defaults once custom post types and taxonomies have been registered
  160. * which is normally done on the init action.
  161. *
  162. * @todo - [JRF/testers] verify that none of the options which are only available after
  163. * enrichment are used before the enriching
  164. */
  165. if ( method_exists( $this, 'enrich_defaults' ) ) {
  166. add_action( 'init', array( $this, 'enrich_defaults' ), 99 );
  167. }
  168. }
  169. /**
  170. * All concrete classes *must* contain the get_instance method
  171. * @internal Unfortunately I can't define it as an abstract as it also *has* to be static....
  172. */
  173. //abstract protected static function get_instance();
  174. /**
  175. * Concrete classes *may* contain a translate_defaults method
  176. */
  177. //abstract public function translate_defaults();
  178. /**
  179. * Concrete classes *may* contain a enrich_defaults method to add additional defaults once
  180. * all post_types and taxonomies have been registered
  181. */
  182. //abstract public function enrich_defaults();
  183. /* *********** METHODS INFLUENCING get_option() *********** */
  184. /**
  185. * Add filters to make sure that the option default is returned if the option is not set
  186. *
  187. * @return void
  188. */
  189. public function add_default_filters() {
  190. // Don't change, needs to check for false as could return prio 0 which would evaluate to false
  191. if ( has_filter( 'default_option_' . $this->option_name, array( $this, 'get_defaults' ) ) === false ) {
  192. add_filter( 'default_option_' . $this->option_name, array( $this, 'get_defaults' ) );
  193. }
  194. }
  195. /**
  196. * Abusing a filter to re-add our default filters
  197. * WP 3.7 specific as update_option action hook was in the wrong place temporarily
  198. * @see http://core.trac.wordpress.org/ticket/25705
  199. *
  200. * @param mixed $new_value
  201. *
  202. * @return mixed unchanged value
  203. */
  204. public function wp37_add_default_filters( $new_value ) {
  205. $this->add_default_filters();
  206. return $new_value;
  207. }
  208. /**
  209. * Remove the default filters.
  210. * Called from the validate() method to prevent failure to add new options
  211. *
  212. * @return void
  213. */
  214. public function remove_default_filters() {
  215. remove_filter( 'default_option_' . $this->option_name, array( $this, 'get_defaults' ) );
  216. }
  217. /**
  218. * Get the enriched default value for an option
  219. *
  220. * Checks if the concrete class contains an enrich_defaults() method and if so, runs it.
  221. *
  222. * @internal the enrich_defaults method is used to set defaults for variable array keys in an option,
  223. * such as array keys depending on post_types and/or taxonomies
  224. *
  225. * @return array
  226. */
  227. public function get_defaults() {
  228. if ( method_exists( $this, 'translate_defaults' ) ) {
  229. $this->translate_defaults();
  230. }
  231. if ( method_exists( $this, 'enrich_defaults' ) ) {
  232. $this->enrich_defaults();
  233. }
  234. return apply_filters( 'wpseo_defaults', $this->defaults, $this->option_name );
  235. }
  236. /**
  237. * Add filters to make sure that the option is merged with its defaults before being returned
  238. *
  239. * @return void
  240. */
  241. public function add_option_filters() {
  242. // Don't change, needs to check for false as could return prio 0 which would evaluate to false
  243. if ( has_filter( 'option_' . $this->option_name, array( $this, 'get_option' ) ) === false ) {
  244. add_filter( 'option_' . $this->option_name, array( $this, 'get_option' ) );
  245. }
  246. }
  247. /**
  248. * Remove the option filters.
  249. * Called from the clean_up methods to make sure we retrieve the original old option
  250. *
  251. * @return void
  252. */
  253. public function remove_option_filters() {
  254. remove_filter( 'option_' . $this->option_name, array( $this, 'get_option' ) );
  255. }
  256. /**
  257. * Merge an option with its default values
  258. *
  259. * This method should *not* be called directly!!! It is only meant to filter the get_option() results
  260. *
  261. * @param mixed $options Option value
  262. *
  263. * @return mixed Option merged with the defaults for that option
  264. */
  265. public function get_option( $options = null ) {
  266. $filtered = $this->array_filter_merge( $options );
  267. /* If the option contains variable option keys, make sure we don't remove those settings
  268. - even if the defaults are not complete yet.
  269. Unfortunately this means we also won't be removing the settings for post types or taxonomies
  270. which are no longer in the WP install, but rather that than the other way around */
  271. if ( isset( $this->variable_array_key_patterns ) ) {
  272. $filtered = $this->retain_variable_keys( $options, $filtered );
  273. }
  274. return $filtered;
  275. }
  276. /* *********** METHODS influencing add_uption(), update_option() and saving from admin pages *********** */
  277. /**
  278. * Register (whitelist) the option for the configuration pages.
  279. * The validation callback is already registered separately on the sanitize_option hook,
  280. * so no need to double register.
  281. *
  282. * @return void
  283. */
  284. public function register_setting() {
  285. if ( WPSEO_Options::grant_access() ) {
  286. register_setting( $this->group_name, $this->option_name );
  287. }
  288. }
  289. /**
  290. * Validate the option
  291. *
  292. * @param mixed $option_value The unvalidated new value for the option
  293. *
  294. * @return array Validated new value for the option
  295. */
  296. public function validate( $option_value ) {
  297. $clean = $this->get_defaults();
  298. /* Return the defaults if the new value is empty */
  299. if ( ! is_array( $option_value ) || $option_value === array() ) {
  300. return $clean;
  301. }
  302. $option_value = array_map( array( __CLASS__, 'trim_recursive' ), $option_value );
  303. if ( $this->multisite_only !== true ) {
  304. $old = get_option( $this->option_name );
  305. }
  306. else {
  307. $old = get_site_option( $this->option_name );
  308. }
  309. $clean = $this->validate_option( $option_value, $clean, $old );
  310. /* Retain the values for variable array keys even when the post type/taxonomy is not yet registered */
  311. if ( isset( $this->variable_array_key_patterns ) ) {
  312. $clean = $this->retain_variable_keys( $option_value, $clean );
  313. }
  314. $this->remove_default_filters();
  315. return $clean;
  316. }
  317. /**
  318. * All concrete classes must contain a validate_option() method which validates all
  319. * values within the option
  320. */
  321. abstract protected function validate_option( $dirty, $clean, $old );
  322. /* *********** METHODS for ADDING/UPDATING/UPGRADING the option *********** */
  323. /**
  324. * Retrieve the real old value (unmerged with defaults)
  325. *
  326. * @return array|bool the original option value (which can be false if the option doesn't exist)
  327. */
  328. protected function get_original_option() {
  329. $this->remove_default_filters();
  330. $this->remove_option_filters();
  331. // Get (unvalidated) array, NOT merged with defaults
  332. if ( $this->multisite_only !== true ) {
  333. $option_value = get_option( $this->option_name );
  334. }
  335. else {
  336. $option_value = get_site_option( $this->option_name );
  337. }
  338. $this->add_option_filters();
  339. $this->add_default_filters();
  340. return $option_value;
  341. }
  342. /**
  343. * Add the option if it doesn't exist for some strange reason
  344. *
  345. * @uses WPSEO_Option::get_original_option()
  346. *
  347. * @return void
  348. */
  349. public function maybe_add_option() {
  350. if ( $this->get_original_option() === false ) {
  351. if ( $this->multisite_only !== true ) {
  352. update_option( $this->option_name, $this->get_defaults() );
  353. }
  354. else {
  355. $this->update_site_option( $this->get_defaults() );
  356. }
  357. }
  358. }
  359. /**
  360. * Update a site_option
  361. *
  362. * @internal This special method is only needed for multisite options, but very needed indeed there.
  363. * The order in which certain functions and hooks are run is different between get_option() and
  364. * get_site_option() which means in practice that the removing of the default filters would be
  365. * done too late and the re-adding of the default filters might not be done at all.
  366. * Aka: use the WPSEO_Options::update_site_option() method (which calls this method) for
  367. * safely adding/updating multisite options.
  368. *
  369. * @return bool whether the update was succesfull
  370. */
  371. public function update_site_option( $value ) {
  372. if ( $this->multisite_only === true && is_multisite() ) {
  373. $this->remove_default_filters();
  374. $result = update_site_option( $this->option_name, $value );
  375. $this->add_default_filters();
  376. return $result;
  377. }
  378. else {
  379. return false;
  380. }
  381. }
  382. /**
  383. * Retrieve the real old value (unmerged with defaults), clean and re-save the option
  384. *
  385. * @uses WPSEO_Option::get_original_option()
  386. * @uses WPSEO_Option::import()
  387. *
  388. * @param string $current_version (optional) Version from which to upgrade, if not set,
  389. * version specific upgrades will be disregarded
  390. *
  391. * @return void
  392. */
  393. public function clean( $current_version = null ) {
  394. $option_value = $this->get_original_option();
  395. $this->import( $option_value, $current_version );
  396. }
  397. /**
  398. * Clean and re-save the option
  399. * @uses clean_option() method from concrete class if it exists
  400. *
  401. * @todo [JRF/whomever] Figure out a way to show settings error during/after the upgrade - maybe
  402. * something along the lines of:
  403. * -> add them to a property in this class
  404. * -> if that property isset at the end of the routine and add_settings_error function does not exist,
  405. * save as transient (or update the transient if one already exists)
  406. * -> next time an admin is in the WP back-end, show the errors and delete the transient or only delete it
  407. * once the admin has dismissed the message (add ajax function)
  408. * Important: all validation routines which add_settings_errors would need to be changed for this to work
  409. *
  410. * @param array $option_value Option value to be imported
  411. * @param string $current_version (optional) Version from which to upgrade, if not set,
  412. * version specific upgrades will be disregarded
  413. * @param array $all_old_option_values (optional) Only used when importing old options to have
  414. * access to the real old values, in contrast to the saved ones
  415. *
  416. * @return void
  417. */
  418. public function import( $option_value, $current_version = null, $all_old_option_values = null ) {
  419. if ( $option_value === false ) {
  420. $option_value = $this->get_defaults();
  421. } elseif ( is_array( $option_value ) && method_exists( $this, 'clean_option' ) ) {
  422. $option_value = $this->clean_option( $option_value, $current_version, $all_old_option_values );
  423. }
  424. /* Save the cleaned value - validation will take care of cleaning out array keys which
  425. should no longer be there */
  426. if ( $this->multisite_only !== true ) {
  427. update_option( $this->option_name, $option_value );
  428. }
  429. else {
  430. $this->update_site_option( $this->option_name, $option_value );
  431. }
  432. }
  433. /**
  434. * Concrete classes *may* contain a clean_option method which will clean out old/renamed
  435. * values within the option
  436. */
  437. //abstract public function clean_option( $option_value, $current_version = null, $all_old_option_values = null );
  438. /* *********** HELPER METHODS for internal use *********** */
  439. /**
  440. * Helper method - Combines a fixed array of default values with an options array
  441. * while filtering out any keys which are not in the defaults array.
  442. *
  443. * @todo [JRF] - shouldn't this be a straight array merge ? at the end of the day, the validation
  444. * removes any invalid keys on save
  445. *
  446. * @param array $options (Optional) Current options
  447. * - if not set, the option defaults for the $option_key will be returned.
  448. *
  449. * @return array Combined and filtered options array.
  450. */
  451. protected function array_filter_merge( $options = null ) {
  452. $defaults = $this->get_defaults();
  453. if ( ! isset( $options ) || $options === false || $options === array() ) {
  454. return $defaults;
  455. }
  456. $options = (array) $options;
  457. /*$filtered = array();
  458. if ( $defaults !== array() ) {
  459. foreach ( $defaults as $key => $default_value ) {
  460. // @todo should this walk through array subkeys ?
  461. $filtered[ $key ] = ( isset( $options[ $key ] ) ? $options[ $key ] : $default_value );
  462. }
  463. }*/
  464. $filtered = array_merge( $defaults, $options );
  465. return $filtered;
  466. }
  467. /**
  468. * Make sure that any set option values relating to post_types and/or taxonomies are retained,
  469. * even when that post_type or taxonomy may not yet have been registered.
  470. *
  471. * @internal The wpseo_titles concrete class overrules this method. Make sure that any changes
  472. * applied here, also get ported to that version.
  473. *
  474. * @param array $dirty Original option as retrieved from the database
  475. * @param array $clean Filtered option where any options which shouldn't be in our option
  476. * have already been removed and any options which weren't set
  477. * have been set to their defaults
  478. *
  479. * @return array
  480. */
  481. protected function retain_variable_keys( $dirty, $clean ) {
  482. if ( ( is_array( $this->variable_array_key_patterns ) && $this->variable_array_key_patterns !== array() ) && ( is_array( $dirty ) && $dirty !== array() ) ) {
  483. foreach ( $dirty as $key => $value ) {
  484. foreach ( $this->variable_array_key_patterns as $pattern ) {
  485. if ( strpos( $key, $pattern ) === 0 && ! isset( $clean[ $key ] ) ) {
  486. $clean[ $key ] = $value;
  487. break;
  488. }
  489. }
  490. }
  491. }
  492. return $clean;
  493. }
  494. /**
  495. * Check whether a given array key conforms to one of the variable array key patterns for this option
  496. *
  497. * @usedby validate_option() methods for options with variable array keys
  498. *
  499. * @param string $key Array key to check
  500. *
  501. * @return string Pattern if it conforms, original array key if it doesn't or if the option
  502. * does not have variable array keys
  503. */
  504. protected function get_switch_key( $key ) {
  505. if ( ! isset( $this->variable_array_key_patterns ) || ( ! is_array( $this->variable_array_key_patterns ) || $this->variable_array_key_patterns === array() ) ) {
  506. return $key;
  507. }
  508. foreach ( $this->variable_array_key_patterns as $pattern ) {
  509. if ( strpos( $key, $pattern ) === 0 ) {
  510. return $pattern;
  511. }
  512. }
  513. return $key;
  514. }
  515. /* *********** GENERIC HELPER METHODS *********** */
  516. /**
  517. * Emulate the WP native sanitize_text_field function in a %%variable%% safe way
  518. * @see https://core.trac.wordpress.org/browser/trunk/src/wp-includes/formatting.php for the original
  519. *
  520. * Sanitize a string from user input or from the db
  521. *
  522. * check for invalid UTF-8,
  523. * Convert single < characters to entity,
  524. * strip all tags,
  525. * remove line breaks, tabs and extra white space,
  526. * strip octets - BUT DO NOT REMOVE (part of) VARIABLES WHICH WILL BE REPLACED.
  527. *
  528. * @param string $value
  529. *
  530. * @return string
  531. */
  532. public static function sanitize_text_field( $value ) {
  533. $filtered = wp_check_invalid_utf8( $value );
  534. if ( strpos( $filtered, '<' ) !== false ) {
  535. $filtered = wp_pre_kses_less_than( $filtered );
  536. // This will strip extra whitespace for us.
  537. $filtered = wp_strip_all_tags( $filtered, true );
  538. } else {
  539. $filtered = trim( preg_replace( '`[\r\n\t ]+`', ' ', $filtered ) );
  540. }
  541. $found = false;
  542. while ( preg_match( '`[^%](%[a-f0-9]{2})`i', $filtered, $match ) ) {
  543. $filtered = str_replace( $match[1], '', $filtered );
  544. $found = true;
  545. }
  546. if ( $found ) {
  547. // Strip out the whitespace that may now exist after removing the octets.
  548. $filtered = trim( preg_replace( '` +`', ' ', $filtered ) );
  549. }
  550. /**
  551. * Filter a sanitized text field string.
  552. *
  553. * @since WP 2.9.0
  554. *
  555. * @param string $filtered The sanitized string.
  556. * @param string $str The string prior to being sanitized.
  557. */
  558. return apply_filters( 'sanitize_text_field', $filtered, $value );
  559. }
  560. /**
  561. * Sanitize a url for saving to the database
  562. * Not to be confused with the old native WP function
  563. *
  564. * @todo [JRF => whomever] check/improve url verification
  565. *
  566. * @todo [JRF => whomever] when someone would reorganize the classes, this should maybe
  567. * be moved to a general WPSEO_Utils class. Obviously all calls to this method should be
  568. * adjusted in that case.
  569. *
  570. * @param string $value
  571. * @param array $allowed_protocols
  572. *
  573. * @return string
  574. */
  575. public static function sanitize_url( $value, $allowed_protocols = array( 'http', 'https' ) ) {
  576. return esc_url_raw( sanitize_text_field( rawurldecode( $value ) ), $allowed_protocols );
  577. }
  578. /**
  579. * Validate a value as boolean
  580. *
  581. * @todo [JRF => whomever] when someone would reorganize the classes, this (and the emulate method
  582. * below) should maybe be moved to a general WPSEO_Utils class. Obviously all calls to this method
  583. * should be adjusted in that case.
  584. *
  585. * @static
  586. *
  587. * @param mixed $value
  588. *
  589. * @return bool
  590. */
  591. public static function validate_bool( $value ) {
  592. if ( self::$has_filters ) {
  593. return filter_var( $value, FILTER_VALIDATE_BOOLEAN );
  594. } else {
  595. return self::emulate_filter_bool( $value );
  596. }
  597. }
  598. /**
  599. * Cast a value to bool
  600. *
  601. * @static
  602. *
  603. * @param mixed $value Value to cast
  604. *
  605. * @return bool
  606. */
  607. public static function emulate_filter_bool( $value ) {
  608. $true = array(
  609. '1',
  610. 'true',
  611. 'True',
  612. 'TRUE',
  613. 'y',
  614. 'Y',
  615. 'yes',
  616. 'Yes',
  617. 'YES',
  618. 'on',
  619. 'On',
  620. 'On',
  621. );
  622. $false = array(
  623. '0',
  624. 'false',
  625. 'False',
  626. 'FALSE',
  627. 'n',
  628. 'N',
  629. 'no',
  630. 'No',
  631. 'NO',
  632. 'off',
  633. 'Off',
  634. 'OFF',
  635. );
  636. if ( is_bool( $value ) ) {
  637. return $value;
  638. } else if ( is_int( $value ) && ( $value === 0 || $value === 1 ) ) {
  639. return (bool) $value;
  640. } else if ( ( is_float( $value ) && ! is_nan( $value ) ) && ( $value === (float) 0 || $value === (float) 1 ) ) {
  641. return (bool) $value;
  642. } else if ( is_string( $value ) ) {
  643. $value = trim( $value );
  644. if ( in_array( $value, $true, true ) ) {
  645. return true;
  646. } else if ( in_array( $value, $false, true ) ) {
  647. return false;
  648. } else {
  649. return false;
  650. }
  651. }
  652. return false;
  653. }
  654. /**
  655. * Validate a value as integer
  656. *
  657. * @todo [JRF => whomever] when someone would reorganize the classes, this (and the emulate method
  658. * below) should maybe be moved to a general WPSEO_Utils class. Obviously all calls to this method
  659. * should be adjusted in that case.
  660. *
  661. * @static
  662. *
  663. * @param mixed $value
  664. *
  665. * @return mixed int or false in case of failure to convert to int
  666. */
  667. public static function validate_int( $value ) {
  668. if ( self::$has_filters ) {
  669. return filter_var( $value, FILTER_VALIDATE_INT );
  670. } else {
  671. return self::emulate_filter_int( $value );
  672. }
  673. }
  674. /**
  675. * Cast a value to integer
  676. *
  677. * @static
  678. *
  679. * @param mixed $value Value to cast
  680. *
  681. * @return int|bool
  682. */
  683. public static function emulate_filter_int( $value ) {
  684. if ( is_int( $value ) ) {
  685. return $value;
  686. } else if ( is_float( $value ) ) {
  687. if ( (int) $value == $value && ! is_nan( $value ) ) {
  688. return (int) $value;
  689. } else {
  690. return false;
  691. }
  692. } else if ( is_string( $value ) ) {
  693. $value = trim( $value );
  694. if ( $value === '' ) {
  695. return false;
  696. } else if ( ctype_digit( $value ) ) {
  697. return (int) $value;
  698. } else if ( strpos( $value, '-' ) === 0 && ctype_digit( substr( $value, 1 ) ) ) {
  699. return (int) $value;
  700. } else {
  701. return false;
  702. }
  703. }
  704. return false;
  705. }
  706. /**
  707. * Recursively trim whitespace round a string value or of string values within an array
  708. * Only trims strings to avoid typecasting a variable (to string)
  709. *
  710. * @todo [JRF => whomever] when someone would reorganize the classes, this should maybe
  711. * be moved to a general WPSEO_Utils class. Obviously all calls to this method should be
  712. * adjusted in that case.
  713. *
  714. * @static
  715. *
  716. * @param mixed $value Value to trim or array of values to trim
  717. *
  718. * @return mixed Trimmed value or array of trimmed values
  719. */
  720. public static function trim_recursive( $value ) {
  721. if ( is_string( $value ) ) {
  722. $value = trim( $value );
  723. } elseif ( is_array( $value ) ) {
  724. $value = array_map( array( __CLASS__, 'trim_recursive' ), $value );
  725. }
  726. return $value;
  727. }
  728. } /* End of class WPSEO_Option */
  729. } /* End of class-exists wrapper */
  730. /*******************************************************************
  731. * Option: wpseo
  732. *******************************************************************/
  733. if ( ! class_exists( 'WPSEO_Option_Wpseo' ) ) {
  734. class WPSEO_Option_Wpseo extends WPSEO_Option {
  735. /**
  736. * @var string option name
  737. */
  738. public $option_name = 'wpseo';
  739. /**
  740. * @var array Array of defaults for the option
  741. * Shouldn't be requested directly, use $this->get_defaults();
  742. */
  743. protected $defaults = array(
  744. // Non-form fields, set via (ajax) function
  745. 'blocking_files' => array(),
  746. 'ignore_blog_public_warning' => false,
  747. 'ignore_meta_description_warning' => null, // overwrite in __construct()
  748. 'ignore_page_comments' => false,
  749. 'ignore_permalink' => false,
  750. 'ignore_tour' => false,
  751. 'ms_defaults_set' => false,
  752. 'theme_description_found' => null, // overwrite in __construct()
  753. 'theme_has_description' => null, // overwrite in __construct()
  754. 'tracking_popup_done' => false,
  755. // Non-form field, should only be set via validation routine
  756. 'version' => '', // leave default as empty to ensure activation/upgrade works
  757. // Form fields:
  758. 'alexaverify' => '', // text field
  759. 'disableadvanced_meta' => true,
  760. 'googleverify' => '', // text field
  761. 'msverify' => '', // text field
  762. 'pinterestverify' => '',
  763. 'yandexverify' => '',
  764. 'yoast_tracking' => false,
  765. );
  766. public static $desc_defaults = array(
  767. 'ignore_meta_description_warning' => false,
  768. 'theme_description_found' => '', // text string description
  769. 'theme_has_description' => null,
  770. );
  771. /**
  772. * @var array Array of sub-options which should not be overloaded with multi-site defaults
  773. */
  774. public $ms_exclude = array(
  775. 'ignore_blog_public_warning',
  776. 'ignore_meta_description_warning',
  777. 'ignore_page_comments',
  778. 'ignore_permalink',
  779. 'ignore_tour',
  780. /* theme dependent */
  781. 'theme_description_found',
  782. 'theme_has_description',
  783. /* privacy */
  784. 'alexaverify',
  785. 'googleverify',
  786. 'msverify',
  787. 'pinterestverify',
  788. 'yandexverify',
  789. );
  790. /**
  791. * Add the actions and filters for the option
  792. *
  793. * @todo [JRF => testers] Check if the extra actions below would run into problems if an option
  794. * is updated early on and if so, change the call to schedule these for a later action on add/update
  795. * instead of running them straight away
  796. *
  797. * @return \WPSEO_Option_Wpseo
  798. */
  799. protected function __construct() {
  800. /* Dirty fix for making certain defaults available during activation while still only
  801. defining them once */
  802. foreach ( self::$desc_defaults as $key => $value ) {
  803. $this->defaults[ $key ] = $value;
  804. }
  805. parent::__construct();
  806. /* Clear the cache on update/add */
  807. add_action( 'add_option_' . $this->option_name, array( 'WPSEO_Options', 'clear_cache' ) );
  808. add_action( 'update_option_' . $this->option_name, array( 'WPSEO_Options', 'clear_cache' ) );
  809. /* Check if the yoast tracking cron job needs adding/removing on successfull option add/update */
  810. add_action( 'add_option_' . $this->option_name, array(
  811. 'WPSEO_Options',
  812. 'schedule_yoast_tracking',
  813. ), 15, 2 );
  814. add_action( 'update_option_' . $this->option_name, array(
  815. 'WPSEO_Options',
  816. 'schedule_yoast_tracking',
  817. ), 15, 2 );
  818. }
  819. /**
  820. * Get the singleton instance of this class
  821. *
  822. * @return object
  823. */
  824. public static function get_instance() {
  825. if ( ! ( self::$instance instanceof self ) ) {
  826. self::$instance = new self();
  827. }
  828. return self::$instance;
  829. }
  830. /**
  831. * Validate the option
  832. *
  833. * @param array $dirty New value for the option
  834. * @param array $clean Clean value for the option, normally the defaults
  835. * @param array $old Old value of the option
  836. *
  837. * @return array Validated clean value for the option to be saved to the database
  838. */
  839. protected function validate_option( $dirty, $clean, $old ) {
  840. foreach ( $clean as $key => $value ) {
  841. switch ( $key ) {
  842. case 'version':
  843. $clean[ $key ] = WPSEO_VERSION;
  844. break;
  845. case 'blocking_files':
  846. /* @internal [JRF] to really validate this we should also do a file_exists()
  847. * on each array entry and remove files which no longer exist, but that might be overkill */
  848. if ( isset( $dirty[ $key ] ) && is_array( $dirty[ $key ] ) ) {
  849. $clean[ $key ] = array_unique( $dirty[ $key ] );
  850. } elseif ( isset( $old[ $key ] ) && is_array( $old[ $key ] ) ) {
  851. $clean[ $key ] = array_unique( $old[ $key ] );
  852. }
  853. break;
  854. case 'theme_description_found':
  855. if ( isset( $dirty[ $key ] ) && is_string( $dirty[ $key ] ) ) {
  856. $clean[ $key ] = $dirty[ $key ]; // @todo [JRF/whomever] maybe do wp_kses ?
  857. } elseif ( isset( $old[ $key ] ) && is_string( $old[ $key ] ) ) {
  858. $clean[ $key ] = $old[ $key ];
  859. }
  860. break;
  861. /* text fields */
  862. case 'alexaverify':
  863. case 'googleverify':
  864. case 'msverify':
  865. case 'pinterestverify':
  866. case 'yandexverify':
  867. if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
  868. $meta = $dirty[ $key ];
  869. if ( strpos( $meta, 'content=' ) ) {
  870. // Make sure we only have the real key, not a complete meta tag
  871. preg_match( '`content=([\'"])?([^\'"> ]+)(?:\1|[ />])`', $meta, $match );
  872. if ( isset( $match[2] ) ) {
  873. $meta = $match[2];
  874. }
  875. unset( $match );
  876. }
  877. $meta = sanitize_text_field( $meta );
  878. if ( $meta !== '' ) {
  879. $regex = '`^[A-Fa-f0-9_-]+$`';
  880. $service = '';
  881. switch ( $key ) {
  882. case 'googleverify':
  883. $regex = '`^[A-Za-z0-9_-]+$`';
  884. $service = 'Google Webmaster tools';
  885. break;
  886. case 'msverify':
  887. $service = 'Bing Webmaster tools';
  888. break;
  889. case 'pinterestverify':
  890. $service = 'Pinterest';
  891. break;
  892. case 'yandexverify':
  893. $service = 'Yandex Webmaster tools';
  894. break;
  895. case 'alexaverify':
  896. $regex = '`^[A-Za-z0-9_-]{20,}$`';
  897. $service = 'Alexa ID';
  898. }
  899. if ( preg_match( $regex, $meta ) ) {
  900. $clean[ $key ] = $meta;
  901. } else {
  902. if ( isset( $old[ $key ] ) && preg_match( $regex, $old[ $key ] ) ) {
  903. $clean[ $key ] = $old[ $key ];
  904. }
  905. if ( function_exists( 'add_settings_error' ) ) {
  906. add_settings_error(
  907. $this->group_name, // slug title of the setting
  908. '_' . $key, // suffix-id for the error message box
  909. sprintf( __( '%s does not seem to be a valid %s verification string. Please correct.', 'wordpress-seo' ), '<strong>' . esc_html( $meta ) . '</strong>', $service ), // the error message
  910. 'error' // error type, either 'error' or 'updated'
  911. );
  912. }
  913. }
  914. }
  915. unset( $meta, $regex, $service );
  916. }
  917. break;
  918. /* boolean|null fields - if set a check was done, if null, it hasn't */
  919. case 'theme_has_description':
  920. if ( isset( $dirty[ $key ] ) ) {
  921. $clean[ $key ] = self::validate_bool( $dirty[ $key ] );
  922. } elseif ( isset( $old[ $key ] ) ) {
  923. $clean[ $key ] = self::validate_bool( $old[ $key ] );
  924. }
  925. break;
  926. /* boolean dismiss warnings - not fields - may not be in form
  927. (and don't need to be either as long as the default is false) */
  928. case 'ignore_blog_public_warning':
  929. case 'ignore_meta_description_warning':
  930. case 'ignore_page_comments':
  931. case 'ignore_permalink':
  932. case 'ignore_tour':
  933. case 'ms_defaults_set':
  934. case 'tracking_popup_done':
  935. if ( isset( $dirty[ $key ] ) ) {
  936. $clean[ $key ] = self::validate_bool( $dirty[ $key ] );
  937. } elseif ( isset( $old[ $key ] ) ) {
  938. $clean[ $key ] = self::validate_bool( $old[ $key ] );
  939. }
  940. break;
  941. /* boolean (checkbox) fields */
  942. case 'disableadvanced_meta':
  943. case 'yoast_tracking':
  944. default:
  945. $clean[ $key ] = ( isset( $dirty[ $key ] ) ? self::validate_bool( $dirty[ $key ] ) : false );
  946. break;
  947. }
  948. }
  949. return $clean;
  950. }
  951. /**
  952. * Clean a given option value
  953. *
  954. * @param array $option_value Old (not merged with defaults or filtered) option value to
  955. * clean according to the rules for this option
  956. * @param string $current_version (optional) Version from which to upgrade, if not set,
  957. * version specific upgrades will be disregarded
  958. * @param array $all_old_option_values (optional) Only used when importing old options to have
  959. * access to the real old values, in contrast to the saved ones
  960. *
  961. * @return array Cleaned option
  962. */
  963. protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) {
  964. // Rename some options *and* change their value
  965. $rename = array(
  966. 'presstrends' => array(
  967. 'new_name' => 'yoast_tracking',
  968. 'new_value' => true,
  969. ),
  970. 'presstrends_popup' => array(
  971. 'new_name' => 'tracking_popup_done',
  972. 'new_value' => true,
  973. ),
  974. );
  975. foreach ( $rename as $old => $new ) {
  976. if ( isset( $option_value[ $old ] ) && ! isset( $option_value[ $new['new_name'] ] ) ) {
  977. $option_value[ $new['new_name'] ] = $new['new_value'];
  978. unset( $option_value[ $old ] );
  979. }
  980. }
  981. unset( $rename, $old, $new );
  982. // Deal with renaming of some options without losing the settings
  983. $rename = array(
  984. 'tracking_popup' => 'tracking_popup_done',
  985. 'meta_description_warning' => 'ignore_meta_description_warning',
  986. );
  987. foreach ( $rename as $old => $new ) {
  988. if ( isset( $option_value[ $old ] ) && ! isset( $option_value[ $new ] ) ) {
  989. $option_value[ $new ] = $option_value[ $old ];
  990. unset( $option_value[ $old ] );
  991. }
  992. }
  993. unset( $rename, $old, $new );
  994. // Change a array sub-option to two straight sub-options
  995. if ( isset( $option_value['theme_check']['description'] ) && ! isset( $option_value['theme_has_description'] ) ) {
  996. // @internal the negate is by design!
  997. $option_value['theme_has_description'] = ! $option_value['theme_check']['description'];
  998. }
  999. if ( isset( $option_values['theme_check']['description_found'] ) && ! isset( $option_value['theme_description_found'] ) ) {
  1000. $option_value['theme_description_found'] = $option_value['theme_check']['description_found'];
  1001. }
  1002. // Deal with value change from text string to boolean
  1003. $value_change = array(
  1004. 'ignore_blog_public_warning',
  1005. 'ignore_meta_description_warning',
  1006. 'ignore_page_comments',
  1007. 'ignore_permalink',
  1008. 'ignore_tour',
  1009. //'disableadvanced_meta', => not needed as is 'on' which will auto-convert to true
  1010. 'tracking_popup_done',
  1011. );
  1012. foreach ( $value_change as $key ) {
  1013. if ( isset( $option_value[ $key ] ) && in_array( $option_value[ $key ], array(
  1014. 'ignore',
  1015. 'done',
  1016. ), true )
  1017. ) {
  1018. $option_value[ $key ] = true;
  1019. }
  1020. }
  1021. return $option_value;
  1022. }
  1023. } /* End of class WPSEO_Option_Wpseo */
  1024. } /* End of class-exists wrapper */
  1025. /*******************************************************************
  1026. * Option: wpseo_permalinks
  1027. *******************************************************************/
  1028. if ( ! class_exists( 'WPSEO_Option_Permalinks' ) ) {
  1029. /**
  1030. * @internal Clean routine for 1.5 not needed as values used to be saved as string 'on' and those will convert
  1031. * automatically
  1032. */
  1033. class WPSEO_Option_Permalinks extends WPSEO_Option {
  1034. /**
  1035. * @var string option name
  1036. */
  1037. public $option_name = 'wpseo_permalinks';
  1038. /**
  1039. * @var array Array of defaults for the option
  1040. * Shouldn't be requested directly, use $this->get_defaults();
  1041. */
  1042. protected $defaults = array(
  1043. 'cleanpermalinks' => false,
  1044. 'cleanpermalink-extravars' => '', // text field
  1045. 'cleanpermalink-googlecampaign' => false,
  1046. 'cleanpermalink-googlesitesearch' => false,
  1047. 'cleanreplytocom' => false,
  1048. 'cleanslugs' => true,
  1049. 'force_transport' => 'default',
  1050. 'redirectattachment' => false,
  1051. 'stripcategorybase' => false,
  1052. 'trailingslash' => false,
  1053. );
  1054. /**
  1055. * @static
  1056. * @var array $force_transport_options Available options for the force_transport setting
  1057. * Used for input validation
  1058. *
  1059. * @internal Important: Make sure the options added to the array here are in line with the keys
  1060. * for the options set for the select box in the admin/pages/permalinks.php file
  1061. */
  1062. public static $force_transport_options = array(
  1063. 'default', // = leave as-is
  1064. 'http',
  1065. 'https',
  1066. );
  1067. /**
  1068. * Add the actions and filters for the option
  1069. *
  1070. * @todo [JRF => testers] Check if the extra actions below would run into problems if an option
  1071. * is updated early on and if so, change the call to schedule these for a later action on add/update
  1072. * instead of running them straight away
  1073. *
  1074. * @return \WPSEO_Option_Permalinks
  1075. */
  1076. protected function __construct() {
  1077. parent::__construct();
  1078. add_action( 'update_option_' . $this->option_name, array( 'WPSEO_Options', 'clear_rewrites' ) );
  1079. }
  1080. /**
  1081. * Get the singleton instance of this class
  1082. *
  1083. * @return object
  1084. */
  1085. public static function get_instance() {
  1086. if ( ! ( self::$instance instanceof self ) ) {
  1087. self::$instance = new self();
  1088. }
  1089. return self::$instance;
  1090. }
  1091. /**
  1092. * Validate the option
  1093. *
  1094. * @param array $dirty New value for the option
  1095. * @param array $clean Clean value for the option, normally the defaults
  1096. * @param array $old Old value of the option (not used here as all fields will always be in the form)
  1097. *
  1098. * @return array Validated clean value for the option to be saved to the database
  1099. */
  1100. protected function validate_option( $dirty, $clean, $old ) {
  1101. foreach ( $clean as $key => $value ) {
  1102. switch ( $key ) {
  1103. case 'force_transport':
  1104. if ( isset( $dirty[ $key ] ) && in_array( $dirty[ $key ], self::$force_transport_options, true ) ) {
  1105. $clean[ $key ] = $dirty[ $key ];
  1106. } else {
  1107. if ( isset( $old[ $key ] ) && in_array( $old[ $key ], self::$force_transport_options, true ) ) {
  1108. $clean[ $key ] = $old[ $key ];
  1109. }
  1110. if ( function_exists( 'add_settings_error' ) ) {
  1111. add_settings_error(
  1112. $this->group_name, // slug title of the setting
  1113. '_' . $key, // suffix-id for the error message box
  1114. __( 'Invalid transport mode set for the canonical settings. Value reset to default.', 'wordpress-seo' ), // the error message
  1115. 'error' // error type, either 'error' or 'updated'
  1116. );
  1117. }
  1118. }
  1119. break;
  1120. /* text fields */
  1121. case 'cleanpermalink-extravars':
  1122. if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
  1123. $clean[ $key ] = sanitize_text_field( $dirty[ $key ] );
  1124. }
  1125. break;
  1126. /* boolean (checkbox) fields */
  1127. case 'cleanpermalinks':
  1128. case 'cleanpermalink-googlesitesearch':
  1129. case 'cleanpermalink-googlecampaign':
  1130. case 'cleanreplytocom':
  1131. case 'cleanslugs':
  1132. case 'redirectattachment':
  1133. case 'stripcategorybase':
  1134. case 'trailingslash':
  1135. default:
  1136. $clean[ $key ] = ( isset( $dirty[ $key ] ) ? self::validate_bool( $dirty[ $key ] ) : false );
  1137. break;
  1138. }
  1139. }
  1140. return $clean;
  1141. }
  1142. /**
  1143. * Clean a given option value
  1144. *
  1145. * @param array $option_value Old (not merged with defaults or filtered) option value to
  1146. * clean according to the rules for this option
  1147. * @param string $current_version (optional) Version from which to upgrade, if not set,
  1148. * version specific upgrades will be disregarded
  1149. * @param array $all_old_option_values (optional) Only used when importing old options to have
  1150. * access to the real old values, in contrast to the saved ones
  1151. *
  1152. * @return array Cleaned option
  1153. */
  1154. /*protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) {
  1155. return $option_value;
  1156. }*/
  1157. } /* End of class WPSEO_Option_Permalinks */
  1158. } /* End of class-exists wrapper */
  1159. /*******************************************************************
  1160. * Option: wpseo_titles
  1161. *******************************************************************/
  1162. if ( ! class_exists( 'WPSEO_Option_Titles' ) ) {
  1163. class WPSEO_Option_Titles extends WPSEO_Option {
  1164. /**
  1165. * @var string option name
  1166. */
  1167. public $option_name = 'wpseo_titles';
  1168. /**
  1169. * @var array Array of defaults for the option
  1170. * Shouldn't be requested directly, use $this->get_defaults();
  1171. * @internal Note: Some of the default values are added via the translate_defaults() method
  1172. */
  1173. protected $defaults = array(
  1174. // Non-form fields, set via (ajax) function
  1175. 'title_test' => 0,
  1176. // Form fields
  1177. 'forcerewritetitle' => false,
  1178. 'separator' => 'sc-dash',
  1179. 'hide-feedlinks' => false,
  1180. 'hide-rsdlink' => false,
  1181. 'hide-shortlink' => false,
  1182. 'hide-wlwmanifest' => false,
  1183. 'noodp' => false,
  1184. 'noydir' => false,
  1185. 'usemetakeywords' => false,
  1186. 'title-home-wpseo' => '%%sitename%% %%page%% %%sep%% %%sitedesc%%', // text field
  1187. 'title-author-wpseo' => '', // text field
  1188. 'title-archive-wpseo' => '%%date%% %%page%% %%sep%% %%sitename%%', // text field
  1189. 'title-search-wpseo' => '', // text field
  1190. 'title-404-wpseo' => '', // text field
  1191. 'metadesc-home-wpseo' => '', // text area
  1192. 'metadesc-author-wpseo' => '', // text area
  1193. 'metadesc-archive-wpseo' => '', // text area
  1194. 'metakey-home-wpseo' => '', // text field
  1195. 'metakey-author-wpseo' => '', // text field
  1196. 'noindex-subpages-wpseo' => false,
  1197. 'noindex-author-wpseo' => false,
  1198. 'noindex-archive-wpseo' => true,
  1199. 'disable-author' => false,
  1200. 'disable-date' => false,
  1201. /**
  1202. * Uses enrich_defaults to add more along the lines of:
  1203. * - 'title-' . $pt->name => ''; // text field
  1204. * - 'metadesc-' . $pt->name => ''; // text field
  1205. * - 'metakey-' . $pt->name => ''; // text field
  1206. * - 'noindex-' . $pt->name => false;
  1207. * - 'noauthorship-' . $pt->name => false;
  1208. * - 'showdate-' . $pt->name => false;
  1209. * - 'hideeditbox-' . $pt->name => false;
  1210. *
  1211. * - 'title-ptarchive-' . $pt->name => ''; // text field
  1212. * - 'metadesc-ptarchive-' . $pt->name => ''; // text field
  1213. * - 'metakey-ptarchive-' . $pt->name => ''; // text field
  1214. * - 'bctitle-ptarchive-' . $pt->name => ''; // text field
  1215. * - 'noindex-ptarchive-' . $pt->name => false;
  1216. *
  1217. * - 'title-tax-' . $tax->name => '''; // text field
  1218. * - 'metadesc-tax-' . $tax->name => ''; // text field
  1219. * - 'metakey-tax-' . $tax->name => ''; // text field
  1220. * - 'noindex-tax-' . $tax->name => false;
  1221. * - 'hideeditbox-tax-' . $tax->name => false;
  1222. */
  1223. );
  1224. /**
  1225. * @var array Array of variable option name patterns for the option
  1226. */
  1227. protected $variable_array_key_patterns = array(
  1228. 'title-',
  1229. 'metadesc-',
  1230. 'metakey-',
  1231. 'noindex-',
  1232. 'noauthorship-',
  1233. 'showdate-',
  1234. 'hideeditbox-',
  1235. 'bctitle-ptarchive-',
  1236. );
  1237. /**
  1238. * @var array Array of sub-options which should not be overloaded with multi-site defaults
  1239. */
  1240. public $ms_exclude = array(
  1241. /* theme dependent */
  1242. 'title_test',
  1243. 'forcerewritetitle',
  1244. );
  1245. /**
  1246. * @var array Array of the separator options. To get these options use WPSEO_Option_Titles::get_instance()->get_separator_options()
  1247. */
  1248. private $separator_options = array(
  1249. 'sc-dash' => '-',
  1250. 'sc-ndash' => '&ndash;',
  1251. 'sc-mdash' => '&mdash;',
  1252. 'sc-middot' => '&middot;',
  1253. 'sc-bull' => '&bull;',
  1254. 'sc-star' => '*',
  1255. 'sc-smstar' => '&#8902;',
  1256. 'sc-pipe' => '|',
  1257. 'sc-tilde' => '~',
  1258. 'sc-laquo' => '&laquo;',
  1259. 'sc-raquo' => '&raquo;',
  1260. 'sc-lt' => '&lt;',
  1261. 'sc-gt' => '&gt;',
  1262. );
  1263. /**
  1264. * Add the actions and filters for the option
  1265. *
  1266. * @todo [JRF => testers] Check if the extra actions below would run into problems if an option
  1267. * is updated early on and if so, change the call to schedule these for a later action on add/update
  1268. * instead of running them straight away
  1269. *
  1270. * @return \WPSEO_Option_Titles
  1271. */
  1272. protected function __construct() {
  1273. parent::__construct();
  1274. add_action( 'update_option_' . $this->option_name, array( 'WPSEO_Options', 'clear_cache' ) );
  1275. add_action( 'init', array( $this, 'end_of_init' ), 999 );
  1276. }
  1277. /**
  1278. * Make sure we can recognize the right action for the double cleaning
  1279. */
  1280. public function end_of_init() {
  1281. do_action( 'wpseo_double_clean_titles' );
  1282. }
  1283. /**
  1284. * Get the singleton instance of this class
  1285. *
  1286. * @return object
  1287. */
  1288. public static function get_instance() {
  1289. if ( ! ( self::$instance instanceof self ) ) {
  1290. self::$instance = new self();
  1291. }
  1292. return self::$instance;
  1293. }
  1294. /**
  1295. * Get the available separator options
  1296. *
  1297. * @return array
  1298. */
  1299. public function get_separator_options() {
  1300. $separators = $this->separator_options;
  1301. /**
  1302. * Allow altering the array with separator options
  1303. * @api array $separator_options Array with the separator options
  1304. */
  1305. $filtered_separators = apply_filters( 'wpseo_separator_options', $separators );
  1306. if ( is_array( $filtered_separators ) && $filtered_separators !== array() ) {
  1307. $separators = array_merge( $separators, $filtered_separators );
  1308. }
  1309. return $separators;
  1310. }
  1311. /**
  1312. * Translate strings used in the option defaults
  1313. *
  1314. * @return void
  1315. */
  1316. public function translate_defaults() {
  1317. $this->defaults['title-author-wpseo'] = sprintf( __( '%s, Author at %s', 'wordpress-seo' ), '%%name%%', '%%sitename%%' ) . ' %%page%% ';
  1318. $this->defaults['title-search-wpseo'] = sprintf( __( 'You searched for %s', 'wordpress-seo' ), '%%searchphrase%%' ) . ' %%page%% %%sep%% %%sitename%%';
  1319. $this->defaults['title-404-wpseo'] = __( 'Page Not Found', 'wordpress-seo' ) . ' %%sep%% %%sitename%%';
  1320. }
  1321. /**
  1322. * Add dynamically created default options based on available post types and taxonomies
  1323. *
  1324. * @return void
  1325. */
  1326. public function enrich_defaults() {
  1327. // Retrieve all the relevant post type and taxonomy arrays
  1328. $post_type_names = get_post_types( array( 'public' => true ), 'names' );
  1329. $post_type_objects_custom = get_post_types( array( 'public' => true, '_builtin' => false ), 'objects' );
  1330. $taxonomy_names = get_taxonomies( array( 'public' => true ), 'names' );
  1331. if ( $post_type_names !== array() ) {
  1332. foreach ( $post_type_names as $pt ) {
  1333. $this->defaults[ 'title-' . $pt ] = '%%title%% %%page%% %%sep%% %%sitename%%'; // text field
  1334. $this->defaults[ 'metadesc-' . $pt ] = ''; // text area
  1335. $this->defaults[ 'metakey-' . $pt ] = ''; // text field
  1336. $this->defaults[ 'noindex-' . $pt ] = false;
  1337. if ( 'post' == $pt ) {
  1338. $this->defaults[ 'noauthorship-' . $pt ] = false;
  1339. } else {
  1340. $this->defaults[ 'noauthorship-' . $pt ] = true;
  1341. }
  1342. $this->defaults[ 'showdate-' . $pt ] = false;
  1343. $this->defaults[ 'hideeditbox-' . $pt ] = false;
  1344. }
  1345. unset( $pt );
  1346. }
  1347. if ( $post_type_objects_custom !== array() ) {
  1348. foreach ( $post_type_objects_custom as $pt ) {
  1349. if ( ! $pt->has_archive ) {
  1350. continue;
  1351. }
  1352. $this->defaults[ 'title-ptarchive-' . $pt->name ] = sprintf( __( '%s Archive', 'wordpress-seo' ), '%%pt_plural%%' ) . ' %%page%% %%sep%% %%sitename%%'; // text field
  1353. $this->defaults[ 'metadesc-ptarchive-' . $pt->name ] = ''; // text area
  1354. $this->defaults[ 'metakey-ptarchive-' . $pt->name ] = ''; // text field
  1355. $this->defaults[ 'bctitle-ptarchive-' . $pt->name ] = ''; // text field
  1356. $this->defaults[ 'noindex-ptarchive-' . $pt->name ] = false;
  1357. }
  1358. unset( $pt );
  1359. }
  1360. if ( $taxonomy_names !== array() ) {
  1361. foreach ( $taxonomy_names as $tax ) {
  1362. $this->defaults[ 'title-tax-' . $tax ] = sprintf( __( '%s Archives', 'wordpress-seo' ), '%%term_title%%' ) . ' %%page%% %%sep%% %%sitename%%'; // text field
  1363. $this->defaults[ 'metadesc-tax-' . $tax ] = ''; // text area
  1364. $this->defaults[ 'metakey-tax-' . $tax ] = ''; // text field
  1365. $this->defaults[ 'hideeditbox-tax-' . $tax ] = false;
  1366. if ( $tax !== 'post_format' ) {
  1367. $this->defaults[ 'noindex-tax-' . $tax ] = false;
  1368. } else {
  1369. $this->defaults[ 'noindex-tax-' . $tax ] = true;
  1370. }
  1371. }
  1372. unset( $tax );
  1373. }
  1374. }
  1375. /**
  1376. * Validate the option
  1377. *
  1378. * @param array $dirty New value for the option
  1379. * @param array $clean Clean value for the option, normally the defaults
  1380. * @param array $old Old value of the option
  1381. *
  1382. * @return array Validated clean value for the option to be saved to the database
  1383. */
  1384. protected function validate_option( $dirty, $clean, $old ) {
  1385. foreach ( $clean as $key => $value ) {
  1386. $switch_key = $this->get_switch_key( $key );
  1387. switch ( $switch_key ) {
  1388. /* text fields */
  1389. /* Covers:
  1390. 'title-home-wpseo', 'title-author-wpseo', 'title-archive-wpseo',
  1391. 'title-search-wpseo', 'title-404-wpseo'
  1392. 'title-' . $pt->name
  1393. 'title-ptarchive-' . $pt->name
  1394. 'title-tax-' . $tax->name */
  1395. case 'title-':
  1396. if ( isset( $dirty[ $key ] ) ) {
  1397. $clean[ $key ] = self::sanitize_text_field( $dirty[ $key ] );
  1398. }
  1399. break;
  1400. /* Covers:
  1401. 'metadesc-home-wpseo', 'metadesc-author-wpseo', 'metadesc-archive-wpseo'
  1402. 'metadesc-' . $pt->name
  1403. 'metadesc-ptarchive-' . $pt->name
  1404. 'metadesc-tax-' . $tax->name */
  1405. case 'metadesc-':
  1406. /* Covers:
  1407. 'metakey-home-wpseo', 'metakey-author-wpseo'
  1408. 'metakey-' . $pt->name
  1409. 'metakey-ptarchive-' . $pt->name
  1410. 'metakey-tax-' . $tax->name */
  1411. case 'metakey-':
  1412. /* Covers:
  1413. ''bctitle-ptarchive-' . $pt->name */
  1414. case 'bctitle-ptarchive-':
  1415. if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
  1416. $clean[ $key ] = self::sanitize_text_field( $dirty[ $key ] );
  1417. }
  1418. break;
  1419. /* integer field - not in form*/
  1420. case 'title_test':
  1421. if ( isset( $dirty[ $key ] ) ) {
  1422. $int = self::validate_int( $dirty[ $key ] );
  1423. if ( $int !== false && $int >= 0 ) {
  1424. $clean[ $key ] = $int;
  1425. }
  1426. } elseif ( isset( $old[ $key ] ) ) {
  1427. $int = self::validate_int( $old[ $key ] );
  1428. if ( $int !== false && $int >= 0 ) {
  1429. $clean[ $key ] = $int;
  1430. }
  1431. }
  1432. break;
  1433. /* Separator field - Radio */
  1434. case 'separator':
  1435. if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
  1436. // Get separator fields
  1437. $separator_fields = $this->get_separator_options();
  1438. // Check if the given separator is exists
  1439. if ( isset( $separator_fields[ $dirty[ $key ] ] ) ) {
  1440. $clean[ $key ] = $dirty[ $key ];
  1441. }
  1442. }
  1443. break;
  1444. /* boolean fields */
  1445. case 'forcerewritetitle':
  1446. case 'usemetakeywords':
  1447. case 'noodp':
  1448. case 'noydir':
  1449. case 'hide-rsdlink':
  1450. case 'hide-wlwmanifest':
  1451. case 'hide-shortlink':
  1452. case 'hide-feedlinks':
  1453. case 'disable-author':
  1454. case 'disable-date':
  1455. /* Covers:
  1456. 'noindex-subpages-wpseo', 'noindex-author-wpseo', 'noindex-archive-wpseo'
  1457. 'noindex-' . $pt->name
  1458. 'noindex-ptarchive-' . $pt->name
  1459. 'noindex-tax-' . $tax->name */
  1460. case 'noindex-':
  1461. case 'noauthorship-': /* 'noauthorship-' . $pt->name */
  1462. case 'showdate-': /* 'showdate-'. $pt->name */
  1463. /* Covers:
  1464. 'hideeditbox-'. $pt->name
  1465. 'hideeditbox-tax-' . $tax->name */
  1466. case 'hideeditbox-':
  1467. default:
  1468. $clean[ $key ] = ( isset( $dirty[ $key ] ) ? self::validate_bool( $dirty[ $key ] ) : false );
  1469. break;
  1470. }
  1471. }
  1472. return $clean;
  1473. }
  1474. /**
  1475. * Clean a given option value
  1476. *
  1477. * @param array $option_value Old (not merged with defaults or filtered) option value to
  1478. * clean according to the rules for this option
  1479. * @param string $current_version (optional) Version from which to upgrade, if not set,
  1480. * version specific upgrades will be disregarded
  1481. * @param array $all_old_option_values (optional) Only used when importing old options to have
  1482. * access to the real old values, in contrast to the saved ones
  1483. *
  1484. * @return array Cleaned option
  1485. */
  1486. protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) {
  1487. static $original = null;
  1488. // Double-run this function to ensure renaming of the taxonomy options will work
  1489. if ( ! isset( $original ) && has_action( 'wpseo_double_clean_titles', array(
  1490. $this,
  1491. 'clean',
  1492. ) ) === false
  1493. ) {
  1494. add_action( 'wpseo_double_clean_titles', array( $this, 'clean' ) );
  1495. $original = $option_value;
  1496. }
  1497. /* Move options from very old option to this one
  1498. @internal Don't rename to the 'current' names straight away as that would prevent
  1499. the rename/unset combi below from working
  1500. @todo [JRF] maybe figure out a smarter way to deal with this */
  1501. $old_option = null;
  1502. if ( isset( $all_old_option_values ) ) {
  1503. // Ok, we have an import
  1504. if ( isset( $all_old_option_values['wpseo_indexation'] ) && is_array( $all_old_option_values['wpseo_indexation'] ) && $all_old_option_values['wpseo_indexation'] !== array() ) {
  1505. $old_option = $all_old_option_values['wpseo_indexation'];
  1506. }
  1507. } else {
  1508. $old_option = get_option( 'wpseo_indexation' );
  1509. }
  1510. if ( is_array( $old_option ) && $old_option !== array() ) {
  1511. $move = array(
  1512. 'noindexauthor' => 'noindex-author',
  1513. 'disableauthor' => 'disable-author',
  1514. 'noindexdate' => 'noindex-archive',
  1515. 'noindexcat' => 'noindex-category',
  1516. 'noindextag' => 'noindex-post_tag',
  1517. 'noindexpostformat' => 'noindex-post_format',
  1518. 'noindexsubpages' => 'noindex-subpages',
  1519. 'hidersdlink' => 'hide-rsdlink',
  1520. 'hidefeedlinks' => 'hide-feedlinks',
  1521. 'hidewlwmanifest' => 'hide-wlwmanifest',
  1522. 'hideshortlink' => 'hide-shortlink',
  1523. );
  1524. foreach ( $move as $old => $new ) {
  1525. if ( isset( $old_option[ $old ] ) && ! isset( $option_value[ $new ] ) ) {
  1526. $option_value[ $new ] = $old_option[ $old ];
  1527. }
  1528. }
  1529. unset( $move, $old, $new );
  1530. }
  1531. unset( $old_option );
  1532. // Fix wrongness created by buggy version 1.2.2
  1533. if ( isset( $option_value['title-home'] ) && $option_value['title-home'] === '%%sitename%% - %%sitedesc%% - 12345' ) {
  1534. $option_value['title-home-wpseo'] = '%%sitename%% - %%sitedesc%%';
  1535. }
  1536. /* Renaming these options to avoid ever overwritting these if a (bloody stupid) user /
  1537. programmer would use any of the following as a custom post type or custom taxonomy:
  1538. 'home', 'author', 'archive', 'search', '404', 'subpages'
  1539. Similarly, renaming the tax options to avoid a custom post type and a taxonomy
  1540. with the same name occupying the same option */
  1541. $rename = array(
  1542. 'title-home' => 'title-home-wpseo',
  1543. 'title-author' => 'title-author-wpseo',
  1544. 'title-archive' => 'title-archive-wpseo',
  1545. 'title-search' => 'title-search-wpseo',
  1546. 'title-404' => 'title-404-wpseo',
  1547. 'metadesc-home' => 'metadesc-home-wpseo',
  1548. 'metadesc-author' => 'metadesc-author-wpseo',
  1549. 'metadesc-archive' => 'metadesc-archive-wpseo',
  1550. 'metakey-home' => 'metakey-home-wpseo',
  1551. 'metakey-author' => 'metakey-author-wpseo',
  1552. 'noindex-subpages' => 'noindex-subpages-wpseo',
  1553. 'noindex-author' => 'noindex-author-wpseo',
  1554. 'noindex-archive' => 'noindex-archive-wpseo',
  1555. );
  1556. foreach ( $rename as $old => $new ) {
  1557. if ( isset( $option_value[ $old ] ) && ! isset( $option_value[ $new ] ) ) {
  1558. $option_value[ $new ] = $option_value[ $old ];
  1559. unset( $option_value[ $old ] );
  1560. }
  1561. }
  1562. unset( $rename, $old, $new );
  1563. /* @internal This clean-up action can only be done effectively once the taxonomies and post_types
  1564. * have been registered, i.e. at the end of the init action. */
  1565. if ( isset( $original ) && current_filter() === 'wpseo_double_clean_titles' || did_action( 'wpseo_double_clean_titles' ) > 0 ) {
  1566. $rename = array(
  1567. 'title-' => 'title-tax-',
  1568. 'metadesc-' => 'metadesc-tax-',
  1569. 'metakey-' => 'metakey-tax-',
  1570. 'noindex-' => 'noindex-tax-',
  1571. 'tax-hideeditbox-' => 'hideeditbox-tax-',
  1572. );
  1573. $taxonomy_names = get_taxonomies( array( 'public' => true ), 'names' );
  1574. $post_type_names = get_post_types( array( 'public' => true ), 'names' );
  1575. $defaults = $this->get_defaults();
  1576. if ( $taxonomy_names !== array() ) {
  1577. foreach ( $taxonomy_names as $tax ) {
  1578. foreach ( $rename as $old_prefix => $new_prefix ) {
  1579. if (
  1580. ( isset( $original[ $old_prefix . $tax ] ) && ! isset( $original[ $new_prefix . $tax ] ) )
  1581. && ( ! isset( $option_value[ $new_prefix . $tax ] )
  1582. || ( isset( $option_value[ $new_prefix . $tax ] )
  1583. && $option_value[ $new_prefix . $tax ] === $defaults[ $new_prefix . $tax ] ) )
  1584. ) {
  1585. $option_value[ $new_prefix . $tax ] = $original[ $old_prefix . $tax ];
  1586. /* Check if there is a cpt with the same name as the tax,
  1587. if so, we should make sure that the old setting hasn't been removed */
  1588. if ( ! isset( $post_type_names[ $tax ] ) && isset( $option_value[ $old_prefix . $tax ] ) ) {
  1589. unset( $option_value[ $old_prefix . $tax ] );
  1590. } else {
  1591. if ( isset( $post_type_names[ $tax ] ) && ! isset( $option_value[ $old_prefix . $tax ] ) ) {
  1592. $option_value[ $old_prefix . $tax ] = $original[ $old_prefix . $tax ];
  1593. }
  1594. }
  1595. if ( $old_prefix === 'tax-hideeditbox-' ) {
  1596. unset( $option_value[ $old_prefix . $tax ] );
  1597. }
  1598. }
  1599. }
  1600. }
  1601. }
  1602. unset( $rename, $taxonomy_names, $post_type_names, $tax, $old_prefix, $new_prefix );
  1603. }
  1604. /* Make sure the values of the variable option key options are cleaned as they
  1605. may be retained and would not be cleaned/validated then */
  1606. if ( is_array( $option_value ) && $option_value !== array() ) {
  1607. foreach ( $option_value as $key => $value ) {
  1608. $switch_key = $this->get_switch_key( $key );
  1609. // Similar to validation routine - any changes made there should be made here too
  1610. switch ( $switch_key ) {
  1611. /* text fields */
  1612. case 'title-':
  1613. case 'metadesc-':
  1614. case 'metakey-':
  1615. case 'bctitle-ptarchive-':
  1616. $option_value[ $key ] = self::sanitize_text_field( $value );
  1617. break;
  1618. /* boolean fields */
  1619. case 'noindex-':
  1620. case 'noauthorship-':
  1621. case 'showdate-':
  1622. case 'hideeditbox-':
  1623. default:
  1624. $option_value[ $key ] = self::validate_bool( $value );
  1625. break;
  1626. }
  1627. }
  1628. }
  1629. return $option_value;
  1630. }
  1631. /**
  1632. * Make sure that any set option values relating to post_types and/or taxonomies are retained,
  1633. * even when that post_type or taxonomy may not yet have been registered.
  1634. *
  1635. * @internal Overrule the abstract class version of this to make sure one extra renamed variable key
  1636. * does not get removed. IMPORTANT: keep this method in line with the parent on which it is based!
  1637. *
  1638. * @param array $dirty Original option as retrieved from the database
  1639. * @param array $clean Filtered option where any options which shouldn't be in our option
  1640. * have already been removed and any options which weren't set
  1641. * have been set to their defaults
  1642. *
  1643. * @return array
  1644. */
  1645. protected function retain_variable_keys( $dirty, $clean ) {
  1646. if ( ( is_array( $this->variable_array_key_patterns ) && $this->variable_array_key_patterns !== array() ) && ( is_array( $dirty ) && $dirty !== array() ) ) {
  1647. // Add the extra pattern
  1648. $patterns = $this->variable_array_key_patterns;
  1649. $patterns[] = 'tax-hideeditbox-';
  1650. /**
  1651. * Allow altering the array with variable array key patterns
  1652. * @api array $patterns Array with the variable array key patterns
  1653. */
  1654. $patterns = apply_filters( 'wpseo_option_titles_variable_array_key_patterns', $patterns );
  1655. foreach ( $dirty as $key => $value ) {
  1656. foreach ( $patterns as $pattern ) {
  1657. if ( strpos( $key, $pattern ) === 0 && ! isset( $clean[ $key ] ) ) {
  1658. $clean[ $key ] = $value;
  1659. break;
  1660. }
  1661. }
  1662. }
  1663. }
  1664. return $clean;
  1665. }
  1666. } /* End of class WPSEO_Option_Titles */
  1667. } /* End of class-exists wrapper */
  1668. /*******************************************************************
  1669. * Option: wpseo_rss
  1670. *******************************************************************/
  1671. if ( ! class_exists( 'WPSEO_Option_RSS' ) ) {
  1672. class WPSEO_Option_RSS extends WPSEO_Option {
  1673. /**
  1674. * @var string option name
  1675. */
  1676. public $option_name = 'wpseo_rss';
  1677. /**
  1678. * @var array Array of defaults for the option
  1679. * Shouldn't be requested directly, use $this->get_defaults();
  1680. * @internal Note: Some of the default values are added via the translate_defaults() method
  1681. */
  1682. protected $defaults = array(
  1683. 'rssbefore' => '', // text area
  1684. 'rssafter' => '', // text area
  1685. );
  1686. /**
  1687. * Get the singleton instance of this class
  1688. *
  1689. * @return object
  1690. */
  1691. public static function get_instance() {
  1692. if ( ! ( self::$instance instanceof self ) ) {
  1693. self::$instance = new self();
  1694. }
  1695. return self::$instance;
  1696. }
  1697. /**
  1698. * Translate strings used in the option defaults
  1699. *
  1700. * @return void
  1701. */
  1702. public function translate_defaults() {
  1703. $this->defaults['rssafter'] = sprintf( __( 'The post %s appeared first on %s.', 'wordpress-seo' ), '%%POSTLINK%%', '%%BLOGLINK%%' );
  1704. }
  1705. /**
  1706. * Validate the option
  1707. *
  1708. * @param array $dirty New value for the option
  1709. * @param array $clean Clean value for the option, normally the defaults
  1710. * @param array $old Old value of the option
  1711. *
  1712. * @return array Validated clean value for the option to be saved to the database
  1713. */
  1714. protected function validate_option( $dirty, $clean, $old ) {
  1715. foreach ( $clean as $key => $value ) {
  1716. if ( isset( $dirty[ $key ] ) ) {
  1717. $clean[ $key ] = wp_kses_post( $dirty[ $key ] );
  1718. }
  1719. }
  1720. return $clean;
  1721. }
  1722. } /* End of class WPSEO_Option_RSS */
  1723. } /* End of class-exists wrapper */
  1724. /*******************************************************************
  1725. * Option: wpseo_internallinks
  1726. *******************************************************************/
  1727. if ( ! class_exists( 'WPSEO_Option_InternalLinks' ) ) {
  1728. class WPSEO_Option_InternalLinks extends WPSEO_Option {
  1729. /**
  1730. * @var string option name
  1731. */
  1732. public $option_name = 'wpseo_internallinks';
  1733. /**
  1734. * @var array Array of defaults for the option
  1735. * Shouldn't be requested directly, use $this->get_defaults();
  1736. * @internal Note: Some of the default values are added via the translate_defaults() method
  1737. */
  1738. protected $defaults = array(
  1739. 'breadcrumbs-404crumb' => '', // text field
  1740. 'breadcrumbs-blog-remove' => false,
  1741. 'breadcrumbs-boldlast' => false,
  1742. 'breadcrumbs-archiveprefix' => '', // text field
  1743. 'breadcrumbs-enable' => false,
  1744. 'breadcrumbs-home' => '', // text field
  1745. 'breadcrumbs-prefix' => '', // text field
  1746. 'breadcrumbs-searchprefix' => '', // text field
  1747. 'breadcrumbs-sep' => '&raquo;', // text field
  1748. /**
  1749. * Uses enrich_defaults() to add more along the lines of:
  1750. * - 'post_types-' . $pt->name . '-maintax' => 0 / string
  1751. * - 'taxonomy-' . $tax->name . '-ptparent' => 0 / string
  1752. */
  1753. );
  1754. /**
  1755. * @var array Array of variable option name patterns for the option
  1756. */
  1757. protected $variable_array_key_patterns = array(
  1758. 'post_types-',
  1759. 'taxonomy-',
  1760. );
  1761. /**
  1762. * Get the singleton instance of this class
  1763. *
  1764. * @return object
  1765. */
  1766. public static function get_instance() {
  1767. if ( ! ( self::$instance instanceof self ) ) {
  1768. self::$instance = new self();
  1769. }
  1770. return self::$instance;
  1771. }
  1772. /**
  1773. * Translate strings used in the option defaults
  1774. *
  1775. * @return void
  1776. */
  1777. public function translate_defaults() {
  1778. $this->defaults['breadcrumbs-404crumb'] = __( 'Error 404: Page not found', 'wordpress-seo' );
  1779. $this->defaults['breadcrumbs-archiveprefix'] = __( 'Archives for', 'wordpress-seo' );
  1780. $this->defaults['breadcrumbs-home'] = __( 'Home', 'wordpress-seo' );
  1781. $this->defaults['breadcrumbs-searchprefix'] = __( 'You searched for', 'wordpress-seo' );
  1782. }
  1783. /**
  1784. * Add dynamically created default options based on available post types and taxonomies
  1785. *
  1786. * @return void
  1787. */
  1788. public function enrich_defaults() {
  1789. // Retrieve all the relevant post type and taxonomy arrays
  1790. $post_type_names = get_post_types( array( 'public' => true ), 'names' );
  1791. $taxonomy_names_custom = get_taxonomies( array( 'public' => true, '_builtin' => false ), 'names' );
  1792. if ( $post_type_names !== array() ) {
  1793. foreach ( $post_type_names as $pt ) {
  1794. $pto_taxonomies = get_object_taxonomies( $pt, 'names' );
  1795. if ( $pto_taxonomies !== array() ) {
  1796. $this->defaults[ 'post_types-' . $pt . '-maintax' ] = 0; // select box
  1797. }
  1798. unset( $pto_taxonomies );
  1799. }
  1800. unset( $pt );
  1801. }
  1802. if ( $taxonomy_names_custom !== array() ) {
  1803. foreach ( $taxonomy_names_custom as $tax ) {
  1804. $this->defaults[ 'taxonomy-' . $tax . '-ptparent' ] = 0; // select box;
  1805. }
  1806. unset( $tax );
  1807. }
  1808. }
  1809. /**
  1810. * Validate the option
  1811. *
  1812. * @param array $dirty New value for the option
  1813. * @param array $clean Clean value for the option, normally the defaults
  1814. * @param array $old Old value of the option
  1815. *
  1816. * @return array Validated clean value for the option to be saved to the database
  1817. */
  1818. protected function validate_option( $dirty, $clean, $old ) {
  1819. $allowed_post_types = $this->get_allowed_post_types();
  1820. foreach ( $clean as $key => $value ) {
  1821. $switch_key = $this->get_switch_key( $key );
  1822. switch ( $switch_key ) {
  1823. /* text fields */
  1824. case 'breadcrumbs-404crumb':
  1825. case 'breadcrumbs-archiveprefix':
  1826. case 'breadcrumbs-home':
  1827. case 'breadcrumbs-prefix':
  1828. case 'breadcrumbs-searchprefix':
  1829. case 'breadcrumbs-sep':
  1830. if ( isset( $dirty[ $key ] ) ) {
  1831. $clean[ $key ] = wp_kses_post( $dirty[ $key ] );
  1832. }
  1833. break;
  1834. /* 'post_types-' . $pt->name . '-maintax' fields */
  1835. case 'post_types-':
  1836. $post_type = str_replace( array( 'post_types-', '-maintax' ), '', $key );
  1837. $taxonomies = get_object_taxonomies( $post_type, 'names' );
  1838. if ( isset( $dirty[ $key ] ) ) {
  1839. if ( $taxonomies !== array() && in_array( $dirty[ $key ], $taxonomies, true ) ) {
  1840. $clean[ $key ] = $dirty[ $key ];
  1841. } elseif ( (string) $dirty[ $key ] === '0' || (string) $dirty[ $key ] === '' ) {
  1842. $clean[ $key ] = 0;
  1843. } elseif ( sanitize_title_with_dashes( $dirty[ $key ] ) === $dirty[ $key ] ) {
  1844. // Allow taxonomies which may not be registered yet
  1845. $clean[ $key ] = $dirty[ $key ];
  1846. } else {
  1847. if ( isset( $old[ $key ] ) ) {
  1848. $clean[ $key ] = sanitize_title_with_dashes( $old[ $key ] );
  1849. }
  1850. if ( function_exists( 'add_settings_error' ) ) {
  1851. /* @todo [JRF => whomever] maybe change the untranslated $pt name in the
  1852. * error message to the nicely translated label ? */
  1853. add_settings_error(
  1854. $this->group_name, // slug title of the setting
  1855. '_' . $key, // suffix-id for the error message box
  1856. sprintf( __( 'Please select a valid taxonomy for post type "%s"', 'wordpress-seo' ), $post_type ), // the error message
  1857. 'error' // error type, either 'error' or 'updated'
  1858. );
  1859. }
  1860. }
  1861. } elseif ( isset( $old[ $key ] ) ) {
  1862. $clean[ $key ] = sanitize_title_with_dashes( $old[ $key ] );
  1863. }
  1864. unset( $taxonomies, $post_type );
  1865. break;
  1866. /* 'taxonomy-' . $tax->name . '-ptparent' fields */
  1867. case 'taxonomy-':
  1868. if ( isset( $dirty[ $key ] ) ) {
  1869. if ( $allowed_post_types !== array() && in_array( $dirty[ $key ], $allowed_post_types, true ) ) {
  1870. $clean[ $key ] = $dirty[ $key ];
  1871. } elseif ( (string) $dirty[ $key ] === '0' || (string) $dirty[ $key ] === '' ) {
  1872. $clean[ $key ] = 0;
  1873. } elseif ( sanitize_key( $dirty[ $key ] ) === $dirty[ $key ] ) {
  1874. // Allow taxonomies which may not be registered yet
  1875. $clean[ $key ] = $dirty[ $key ];
  1876. } else {
  1877. if ( isset( $old[ $key ] ) ) {
  1878. $clean[ $key ] = sanitize_key( $old[ $key ] );
  1879. }
  1880. if ( function_exists( 'add_settings_error' ) ) {
  1881. /* @todo [JRF =? whomever] maybe change the untranslated $tax name in the
  1882. * error message to the nicely translated label ? */
  1883. $tax = str_replace( array( 'taxonomy-', '-ptparent' ), '', $key );
  1884. add_settings_error(
  1885. $this->group_name, // slug title of the setting
  1886. '_' . $tax, // suffix-id for the error message box
  1887. sprintf( __( 'Please select a valid post type for taxonomy "%s"', 'wordpress-seo' ), $tax ), // the error message
  1888. 'error' // error type, either 'error' or 'updated'
  1889. );
  1890. unset( $tax );
  1891. }
  1892. }
  1893. } elseif ( isset( $old[ $key ] ) ) {
  1894. $clean[ $key ] = sanitize_key( $old[ $key ] );
  1895. }
  1896. break;
  1897. /* boolean fields */
  1898. case 'breadcrumbs-blog-remove':
  1899. case 'breadcrumbs-boldlast':
  1900. case 'breadcrumbs-enable':
  1901. default:
  1902. $clean[ $key ] = ( isset( $dirty[ $key ] ) ? self::validate_bool( $dirty[ $key ] ) : false );
  1903. break;
  1904. }
  1905. }
  1906. return $clean;
  1907. }
  1908. /**
  1909. * Retrieve a list of the allowed post types as breadcrumb parent for a taxonomy
  1910. * Helper method for validation
  1911. * @internal don't make static as new types may still be registered
  1912. */
  1913. protected function get_allowed_post_types() {
  1914. $allowed_post_types = array();
  1915. $post_types = get_post_types( array( 'public' => true ), 'objects' );
  1916. if ( get_option( 'show_on_front' ) == 'page' && get_option( 'page_for_posts' ) > 0 ) {
  1917. $allowed_post_types[] = 'post';
  1918. }
  1919. if ( is_array( $post_types ) && $post_types !== array() ) {
  1920. foreach ( $post_types as $type ) {
  1921. if ( $type->has_archive ) {
  1922. $allowed_post_types[] = $type->name;
  1923. }
  1924. }
  1925. }
  1926. return $allowed_post_types;
  1927. }
  1928. /**
  1929. * Clean a given option value
  1930. *
  1931. * @param array $option_value Old (not merged with defaults or filtered) option value to
  1932. * clean according to the rules for this option
  1933. * @param string $current_version (optional) Version from which to upgrade, if not set,
  1934. * version specific upgrades will be disregarded
  1935. * @param array $all_old_option_values (optional) Only used when importing old options to have
  1936. * access to the real old values, in contrast to the saved ones
  1937. *
  1938. * @return array Cleaned option
  1939. */
  1940. protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) {
  1941. /* Make sure the old fall-back defaults for empty option keys are now added to the option */
  1942. if ( isset( $current_version ) && version_compare( $current_version, '1.5.2.3', '<' ) ) {
  1943. if ( has_action( 'init', array( 'WPSEO_Options', 'bring_back_breadcrumb_defaults' ) ) === false ) {
  1944. add_action( 'init', array( 'WPSEO_Options', 'bring_back_breadcrumb_defaults' ), 3 );
  1945. }
  1946. }
  1947. /* Make sure the values of the variable option key options are cleaned as they
  1948. may be retained and would not be cleaned/validated then */
  1949. if ( is_array( $option_value ) && $option_value !== array() ) {
  1950. $allowed_post_types = $this->get_allowed_post_types();
  1951. foreach ( $option_value as $key => $value ) {
  1952. $switch_key = $this->get_switch_key( $key );
  1953. // Similar to validation routine - any changes made there should be made here too
  1954. switch ( $switch_key ) {
  1955. /* 'post_types-' . $pt->name . '-maintax' fields */
  1956. case 'post_types-':
  1957. $post_type = str_replace( array( 'post_types-', '-maintax' ), '', $key );
  1958. $taxonomies = get_object_taxonomies( $post_type, 'names' );
  1959. if ( $taxonomies !== array() && in_array( $value, $taxonomies, true ) ) {
  1960. $option_value[ $key ] = $value;
  1961. } elseif ( (string) $value === '0' || (string) $value === '' ) {
  1962. $option_value[ $key ] = 0;
  1963. } elseif ( sanitize_title_with_dashes( $value ) === $value ) {
  1964. // Allow taxonomies which may not be registered yet
  1965. $option_value[ $key ] = $value;
  1966. }
  1967. unset( $taxonomies, $post_type );
  1968. break;
  1969. /* 'taxonomy-' . $tax->name . '-ptparent' fields */
  1970. case 'taxonomy-':
  1971. if ( $allowed_post_types !== array() && in_array( $value, $allowed_post_types, true ) ) {
  1972. $option_value[ $key ] = $value;
  1973. } elseif ( (string) $value === '0' || (string) $value === '' ) {
  1974. $option_value[ $key ] = 0;
  1975. } elseif ( sanitize_key( $option_value[ $key ] ) === $option_value[ $key ] ) {
  1976. // Allow post types which may not be registered yet
  1977. $option_value[ $key ] = $value;
  1978. }
  1979. break;
  1980. }
  1981. }
  1982. }
  1983. return $option_value;
  1984. }
  1985. /**
  1986. * With the changes to v1.5, the defaults for some of the textual breadcrumb settings are added
  1987. * dynamically, but empty strings are allowed.
  1988. * This caused issues for people who left the fields empty on purpose relying on the defaults.
  1989. * This little routine fixes that.
  1990. * Needs to be run on 'init' hook at prio 3 to make sure the defaults are translated.
  1991. */
  1992. public function bring_back_defaults() {
  1993. $option = get_option( $this->option_name );
  1994. $values_to_bring_back = array(
  1995. 'breadcrumbs-404crumb',
  1996. 'breadcrumbs-archiveprefix',
  1997. 'breadcrumbs-home',
  1998. 'breadcrumbs-searchprefix',
  1999. 'breadcrumbs-sep',
  2000. );
  2001. foreach ( $values_to_bring_back as $key ) {
  2002. if ( $option[ $key ] === '' && $this->defaults[ $key ] !== '' ) {
  2003. $option[ $key ] = $this->defaults[ $key ];
  2004. }
  2005. }
  2006. update_option( $this->option_name, $option );
  2007. }
  2008. } /* End of class WPSEO_Option_InternalLinks */
  2009. } /* End of class-exists wrapper */
  2010. /*******************************************************************
  2011. * Option: wpseo_xml
  2012. *******************************************************************/
  2013. if ( ! class_exists( 'WPSEO_Option_XML' ) ) {
  2014. class WPSEO_Option_XML extends WPSEO_Option {
  2015. /**
  2016. * @var string option name
  2017. */
  2018. public $option_name = 'wpseo_xml';
  2019. /**
  2020. * @var string option group name for use in settings forms
  2021. */
  2022. public $group_name = 'yoast_wpseo_xml_sitemap_options';
  2023. /**
  2024. * @var array Array of defaults for the option
  2025. * Shouldn't be requested directly, use $this->get_defaults();
  2026. */
  2027. protected $defaults = array(
  2028. 'disable_author_sitemap' => true,
  2029. 'disable_author_noposts' => true,
  2030. 'enablexmlsitemap' => true,
  2031. 'entries-per-page' => 1000,
  2032. 'xml_ping_yahoo' => false,
  2033. 'xml_ping_ask' => false,
  2034. /**
  2035. * Uses enrich_defaults to add more along the lines of:
  2036. * - 'user_role-' . $role_name . '-not_in_sitemap' => bool
  2037. * - 'post_types-' . $pt->name . '-not_in_sitemap' => bool
  2038. * - 'taxonomies-' . $tax->name . '-not_in_sitemap' => bool
  2039. */
  2040. );
  2041. /**
  2042. * @var array Array of variable option name patterns for the option
  2043. */
  2044. protected $variable_array_key_patterns = array(
  2045. 'user_role-',
  2046. 'post_types-',
  2047. 'taxonomies-',
  2048. );
  2049. /**
  2050. * Add the actions and filters for the option
  2051. *
  2052. * @todo [JRF => testers] Check if the extra actions below would run into problems if an option
  2053. * is updated early on and if so, change the call to schedule these for a later action on add/update
  2054. * instead of running them straight away
  2055. *
  2056. * @return \WPSEO_Option_XML
  2057. */
  2058. protected function __construct() {
  2059. parent::__construct();
  2060. add_action( 'update_option_' . $this->option_name, array( 'WPSEO_Options', 'clear_rewrites' ) );
  2061. }
  2062. /**
  2063. * Get the singleton instance of this class
  2064. *
  2065. * @return object
  2066. */
  2067. public static function get_instance() {
  2068. if ( ! ( self::$instance instanceof self ) ) {
  2069. self::$instance = new self();
  2070. }
  2071. return self::$instance;
  2072. }
  2073. /**
  2074. * Add dynamically created default options based on available post types and taxonomies
  2075. *
  2076. * @return void
  2077. */
  2078. public function enrich_defaults() {
  2079. $user_roles = wpseo_get_roles();
  2080. $filtered_user_roles = apply_filters( 'wpseo_sitemaps_supported_user_roles', $user_roles );
  2081. if ( is_array( $filtered_user_roles ) && $filtered_user_roles !== array() ) {
  2082. foreach ( $filtered_user_roles as $role_name => $role_value ) {
  2083. $this->defaults['user_role-' . $role_name . '-not_in_sitemap'] = false;
  2084. unset( $user_role );
  2085. }
  2086. }
  2087. unset( $filtered_user_roles );
  2088. $post_type_names = get_post_types( array( 'public' => true ), 'names' );
  2089. $filtered_post_types = apply_filters( 'wpseo_sitemaps_supported_post_types', $post_type_names );
  2090. if ( is_array( $filtered_post_types ) && $filtered_post_types !== array() ) {
  2091. foreach ( $filtered_post_types as $pt ) {
  2092. if ( $pt !== 'attachment' ) {
  2093. $this->defaults[ 'post_types-' . $pt . '-not_in_sitemap' ] = false;
  2094. } else {
  2095. $this->defaults[ 'post_types-' . $pt . '-not_in_sitemap' ] = true;
  2096. }
  2097. }
  2098. unset( $pt );
  2099. }
  2100. unset( $filtered_post_types );
  2101. $taxonomy_objects = get_taxonomies( array( 'public' => true ), 'objects' );
  2102. $filtered_taxonomies = apply_filters( 'wpseo_sitemaps_supported_taxonomies', $taxonomy_objects );
  2103. if ( is_array( $filtered_taxonomies ) && $filtered_taxonomies !== array() ) {
  2104. foreach ( $filtered_taxonomies as $tax ) {
  2105. if ( isset( $tax->labels->name ) && trim( $tax->labels->name ) != '' ) {
  2106. $this->defaults[ 'taxonomies-' . $tax->name . '-not_in_sitemap' ] = false;
  2107. }
  2108. }
  2109. unset( $tax );
  2110. }
  2111. unset( $filtered_taxonomies );
  2112. }
  2113. /**
  2114. * Validate the option
  2115. *
  2116. * @param array $dirty New value for the option
  2117. * @param array $clean Clean value for the option, normally the defaults
  2118. * @param array $old Old value of the option
  2119. *
  2120. * @return array Validated clean value for the option to be saved to the database
  2121. */
  2122. protected function validate_option( $dirty, $clean, $old ) {
  2123. foreach ( $clean as $key => $value ) {
  2124. $switch_key = $this->get_switch_key( $key );
  2125. switch ( $switch_key ) {
  2126. /* integer fields */
  2127. case 'entries-per-page':
  2128. /* @todo [JRF/JRF => Yoast] add some more rules (minimum 50 or something
  2129. * - what should be the guideline?) and adjust error message */
  2130. if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
  2131. $int = self::validate_int( $dirty[ $key ] );
  2132. if ( $int !== false && $int > 0 ) {
  2133. $clean[ $key ] = $int;
  2134. } else {
  2135. if ( isset( $old[ $key ] ) && $old[ $key ] !== '' ) {
  2136. $int = self::validate_int( $old[ $key ] );
  2137. if ( $int !== false && $int > 0 ) {
  2138. $clean[ $key ] = $int;
  2139. }
  2140. }
  2141. if ( function_exists( 'add_settings_error' ) ) {
  2142. add_settings_error(
  2143. $this->group_name, // slug title of the setting
  2144. '_' . $key, // suffix-id for the error message box
  2145. sprintf( __( '"Max entries per sitemap page" should be a positive number, which %s is not. Please correct.', 'wordpress-seo' ), '<strong>' . esc_html( sanitize_text_field( $dirty[ $key ] ) ) . '</strong>' ), // the error message
  2146. 'error' // error type, either 'error' or 'updated'
  2147. );
  2148. }
  2149. }
  2150. unset( $int );
  2151. }
  2152. break;
  2153. /* boolean fields */
  2154. case 'disable_author_sitemap':
  2155. case 'disable_author_noposts':
  2156. case 'enablexmlsitemap':
  2157. case 'user_role-': /* 'user_role' . $role_name . '-not_in_sitemap' fields */
  2158. case 'post_types-': /* 'post_types-' . $pt->name . '-not_in_sitemap' fields */
  2159. case 'taxonomies-': /* 'taxonomies-' . $tax->name . '-not_in_sitemap' fields */
  2160. case 'xml_ping_yahoo':
  2161. case 'xml_ping_ask':
  2162. default:
  2163. $clean[ $key ] = ( isset( $dirty[ $key ] ) ? self::validate_bool( $dirty[ $key ] ) : false );
  2164. break;
  2165. }
  2166. }
  2167. return $clean;
  2168. }
  2169. /**
  2170. * Clean a given option value
  2171. *
  2172. * @param array $option_value Old (not merged with defaults or filtered) option value to
  2173. * clean according to the rules for this option
  2174. * @param string $current_version (optional) Version from which to upgrade, if not set,
  2175. * version specific upgrades will be disregarded
  2176. * @param array $all_old_option_values (optional) Only used when importing old options to have
  2177. * access to the real old values, in contrast to the saved ones
  2178. *
  2179. * @return array Cleaned option
  2180. */
  2181. protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) {
  2182. /* Make sure the values of the variable option key options are cleaned as they
  2183. may be retained and would not be cleaned/validated then */
  2184. if ( is_array( $option_value ) && $option_value !== array() ) {
  2185. foreach ( $option_value as $key => $value ) {
  2186. $switch_key = $this->get_switch_key( $key );
  2187. // Similar to validation routine - any changes made there should be made here too
  2188. switch ( $switch_key ) {
  2189. case 'user_role-': /* 'user_role-' . $role_name. '-not_in_sitemap' fields */
  2190. case 'post_types-': /* 'post_types-' . $pt->name . '-not_in_sitemap' fields */
  2191. case 'taxonomies-': /* 'taxonomies-' . $tax->name . '-not_in_sitemap' fields */
  2192. $option_value[ $key ] = self::validate_bool( $value );
  2193. break;
  2194. }
  2195. }
  2196. }
  2197. return $option_value;
  2198. }
  2199. } /* End of class WPSEO_Option_XML */
  2200. } /* End of class-exists wrapper */
  2201. /*******************************************************************
  2202. * Option: wpseo_social
  2203. *******************************************************************/
  2204. if ( ! class_exists( 'WPSEO_Option_Social' ) ) {
  2205. class WPSEO_Option_Social extends WPSEO_Option {
  2206. /**
  2207. * @var string option name
  2208. */
  2209. public $option_name = 'wpseo_social';
  2210. /**
  2211. * @var array Array of defaults for the option
  2212. * Shouldn't be requested directly, use $this->get_defaults();
  2213. */
  2214. protected $defaults = array(
  2215. // Non-form fields, set via procedural code in admin/pages/social.php
  2216. 'fb_admins' => array(), // array of user id's => array( name => '', link => '' )
  2217. 'fbapps' => array(), // array of linked fb apps id's => fb app display names
  2218. // Non-form field, set via translate_defaults() and validate_option() methods
  2219. 'fbconnectkey' => '',
  2220. // Form fields:
  2221. 'facebook_site' => '', // text field
  2222. 'og_default_image' => '', // text field
  2223. 'og_frontpage_title' => '', // text field
  2224. 'og_frontpage_desc' => '', // text field
  2225. 'og_frontpage_image' => '', // text field
  2226. 'opengraph' => true,
  2227. 'googleplus' => false,
  2228. 'plus-publisher' => '', // text field
  2229. 'twitter' => false,
  2230. 'twitter_site' => '', // text field
  2231. 'twitter_card_type' => 'summary',
  2232. // Form field, but not always available:
  2233. 'fbadminapp' => 0, // app id from fbapps list
  2234. );
  2235. /**
  2236. * @var array Array of sub-options which should not be overloaded with multi-site defaults
  2237. */
  2238. public $ms_exclude = array(
  2239. /* privacy */
  2240. 'fb_admins',
  2241. 'fbapps',
  2242. 'fbconnectkey',
  2243. 'fbadminapp',
  2244. );
  2245. /**
  2246. * @var array Array of allowed twitter card types
  2247. * While we only have the options summary and summary_large_image in the
  2248. * interface now, we might change that at some point.
  2249. *
  2250. * @internal Uncomment any of these to allow them in validation *and* automatically add them as a choice
  2251. * in the options page
  2252. */
  2253. public static $twitter_card_types = array(
  2254. 'summary' => '',
  2255. 'summary_large_image' => '',
  2256. //'photo' => '',
  2257. //'gallery' => '',
  2258. //'app' => '',
  2259. //'player' => '',
  2260. //'product' => '',
  2261. );
  2262. /**
  2263. * Get the singleton instance of this class
  2264. *
  2265. * @return object
  2266. */
  2267. public static function get_instance() {
  2268. if ( ! ( self::$instance instanceof self ) ) {
  2269. self::$instance = new self();
  2270. }
  2271. return self::$instance;
  2272. }
  2273. /**
  2274. * Translate/set strings used in the option defaults
  2275. *
  2276. * @return void
  2277. */
  2278. public function translate_defaults() {
  2279. /* Auto-magically set the fb connect key */
  2280. $this->defaults['fbconnectkey'] = self::get_fbconnectkey();
  2281. self::$twitter_card_types['summary'] = __( 'Summary', 'wordpress-seo' );
  2282. self::$twitter_card_types['summary_large_image'] = __( 'Summary with large image', 'wordpress-seo' );
  2283. }
  2284. /**
  2285. * Get a Facebook connect key for the blog
  2286. *
  2287. * @static
  2288. * @return string
  2289. */
  2290. public static function get_fbconnectkey() {
  2291. return md5( get_bloginfo( 'url' ) . rand() );
  2292. }
  2293. /**
  2294. * Validate the option
  2295. *
  2296. * @param array $dirty New value for the option
  2297. * @param array $clean Clean value for the option, normally the defaults
  2298. * @param array $old Old value of the option
  2299. *
  2300. * @return array Validated clean value for the option to be saved to the database
  2301. */
  2302. protected function validate_option( $dirty, $clean, $old ) {
  2303. foreach ( $clean as $key => $value ) {
  2304. switch ( $key ) {
  2305. /* Automagic Facebook connect key */
  2306. case 'fbconnectkey':
  2307. if ( ( isset( $old[ $key ] ) && $old[ $key ] !== '' ) && preg_match( '`^[a-f0-9]{32}$`', $old[ $key ] ) > 0 ) {
  2308. $clean[ $key ] = $old[ $key ];
  2309. } else {
  2310. $clean[ $key ] = self::get_fbconnectkey();
  2311. }
  2312. break;
  2313. /* Will not always exist in form */
  2314. case 'fb_admins':
  2315. if ( isset( $dirty[ $key ] ) && is_array( $dirty[ $key ] ) ) {
  2316. if ( $dirty[ $key ] === array() ) {
  2317. $clean[ $key ] = array();
  2318. } else {
  2319. foreach ( $dirty[ $key ] as $user_id => $fb_array ) {
  2320. /* @todo [JRF/JRF => Yoast/whomever] add user_id validation -
  2321. * are these WP user-ids or FB user-ids ? Probably FB user-ids,
  2322. * if so, find out the rules for FB user-ids
  2323. */
  2324. if ( is_array( $fb_array ) && $fb_array !== array() ) {
  2325. foreach ( $fb_array as $fb_key => $fb_value ) {
  2326. switch ( $fb_key ) {
  2327. case 'name':
  2328. /* @todo [JRF => whomever] add validation for name based
  2329. * on rules if there are any
  2330. * Input comes from: $_GET['userrealname'] */
  2331. $clean[ $key ][ $user_id ][ $fb_key ] = sanitize_text_field( $fb_value );
  2332. break;
  2333. case 'link':
  2334. $clean[ $key ][ $user_id ][ $fb_key ] = self::sanitize_url( $fb_value );
  2335. break;
  2336. }
  2337. }
  2338. }
  2339. }
  2340. unset( $user_id, $fb_array, $fb_key, $fb_value );
  2341. }
  2342. } elseif ( isset( $old[ $key ] ) && is_array( $old[ $key ] ) ) {
  2343. $clean[ $key ] = $old[ $key ];
  2344. }
  2345. break;
  2346. /* Will not always exist in form */
  2347. case 'fbapps':
  2348. if ( isset( $dirty[ $key ] ) && is_array( $dirty[ $key ] ) ) {
  2349. if ( $dirty[ $key ] === array() ) {
  2350. $clean[ $key ] = array();
  2351. } else {
  2352. $clean[ $key ] = array();
  2353. foreach ( $dirty[ $key ] as $app_id => $display_name ) {
  2354. if ( ctype_digit( (string) $app_id ) !== false ) {
  2355. $clean[ $key ][ $app_id ] = sanitize_text_field( $display_name );
  2356. }
  2357. }
  2358. unset( $app_id, $display_name );
  2359. }
  2360. } elseif ( isset( $old[ $key ] ) && is_array( $old[ $key ] ) ) {
  2361. $clean[ $key ] = $old[ $key ];
  2362. }
  2363. break;
  2364. /* text fields */
  2365. case 'og_frontpage_desc':
  2366. case 'og_frontpage_title':
  2367. if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
  2368. $clean[ $key ] = self::sanitize_text_field( $dirty[ $key ] );
  2369. }
  2370. break;
  2371. /* url text fields - no ftp allowed */
  2372. case 'facebook_site':
  2373. case 'plus-publisher':
  2374. case 'og_default_image':
  2375. case 'og_frontpage_image':
  2376. if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
  2377. $url = self::sanitize_url( $dirty[ $key ] );
  2378. if ( $url !== '' ) {
  2379. $clean[ $key ] = $url;
  2380. } else {
  2381. if ( isset( $old[ $key ] ) && $old[ $key ] !== '' ) {
  2382. $url = self::sanitize_url( $old[ $key ] );
  2383. if ( $url !== '' ) {
  2384. $clean[ $key ] = $url;
  2385. }
  2386. }
  2387. if ( function_exists( 'add_settings_error' ) ) {
  2388. $url = self::sanitize_url( $dirty[ $key ] );
  2389. add_settings_error(
  2390. $this->group_name, // slug title of the setting
  2391. '_' . $key, // suffix-id for the error message box
  2392. sprintf( __( '%s does not seem to be a valid url. Please correct.', 'wordpress-seo' ), '<strong>' . esc_html( $url ) . '</strong>' ), // the error message
  2393. 'error' // error type, either 'error' or 'updated'
  2394. );
  2395. }
  2396. }
  2397. unset( $url );
  2398. }
  2399. break;
  2400. /* twitter user name */
  2401. case 'twitter_site':
  2402. if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
  2403. $twitter_id = sanitize_text_field( ltrim( $dirty[ $key ], '@' ) );
  2404. /**
  2405. * From the Twitter documentation about twitter screen names:
  2406. * Typically a maximum of 15 characters long, but some historical accounts
  2407. * may exist with longer names.
  2408. * A username can only contain alphanumeric characters (letters A-Z, numbers 0-9)
  2409. * with the exception of underscores
  2410. * @link https://support.twitter.com/articles/101299-why-can-t-i-register-certain-usernames
  2411. * @link https://dev.twitter.com/docs/platform-objects/users
  2412. */
  2413. if ( preg_match( '`^[A-Za-z0-9_]{1,25}$`', $twitter_id ) ) {
  2414. $clean[ $key ] = $twitter_id;
  2415. } else {
  2416. if ( isset( $old[ $key ] ) && $old[ $key ] !== '' ) {
  2417. $twitter_id = sanitize_text_field( ltrim( $old[ $key ], '@' ) );
  2418. if ( preg_match( '`^[A-Za-z0-9_]{1,25}$`', $twitter_id ) ) {
  2419. $clean[ $key ] = $twitter_id;
  2420. }
  2421. }
  2422. if ( function_exists( 'add_settings_error' ) ) {
  2423. add_settings_error(
  2424. $this->group_name, // slug title of the setting
  2425. '_' . $key, // suffix-id for the error message box
  2426. sprintf( __( '%s does not seem to be a valid Twitter user-id. Please correct.', 'wordpress-seo' ), '<strong>' . esc_html( sanitize_text_field( $dirty[ $key ] ) ) . '</strong>' ), // the error message
  2427. 'error' // error type, either 'error' or 'updated'
  2428. );
  2429. }
  2430. }
  2431. unset( $twitter_id );
  2432. }
  2433. break;
  2434. case 'twitter_card_type':
  2435. if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' && isset( self::$twitter_card_types[ $dirty[ $key ] ] ) ) {
  2436. $clean[ $key ] = $dirty[ $key ];
  2437. }
  2438. break;
  2439. /* boolean fields */
  2440. case 'googleplus':
  2441. case 'opengraph':
  2442. case 'twitter':
  2443. $clean[ $key ] = ( isset( $dirty[ $key ] ) ? self::validate_bool( $dirty[ $key ] ) : false );
  2444. break;
  2445. }
  2446. }
  2447. /**
  2448. * Only validate 'fbadminapp' once we are sure that 'fbapps' has been validated already.
  2449. * Will not always exist in form - if not available it means that fbapps is empty,
  2450. * so leave the clean default.
  2451. */
  2452. if ( ( isset( $dirty['fbadminapp'] ) && $dirty['fbadminapp'] != 0 ) && isset( $clean['fbapps'][ $dirty['fbadminapp'] ] ) ) {
  2453. $clean['fbadminapp'] = $dirty['fbadminapp'];
  2454. }
  2455. return $clean;
  2456. }
  2457. /**
  2458. * Clean a given option value
  2459. *
  2460. * @param array $option_value Old (not merged with defaults or filtered) option value to
  2461. * clean according to the rules for this option
  2462. * @param string $current_version (optional) Version from which to upgrade, if not set,
  2463. * version specific upgrades will be disregarded
  2464. * @param array $all_old_option_values (optional) Only used when importing old options to have
  2465. * access to the real old values, in contrast to the saved ones
  2466. *
  2467. * @return array Cleaned option
  2468. */
  2469. protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) {
  2470. /* Move options from very old option to this one */
  2471. $old_option = null;
  2472. if ( isset( $all_old_option_values ) ) {
  2473. // Ok, we have an import
  2474. if ( isset( $all_old_option_values['wpseo_indexation'] ) && is_array( $all_old_option_values['wpseo_indexation'] ) && $all_old_option_values['wpseo_indexation'] !== array() ) {
  2475. $old_option = $all_old_option_values['wpseo_indexation'];
  2476. }
  2477. } else {
  2478. $old_option = get_option( 'wpseo_indexation' );
  2479. }
  2480. if ( is_array( $old_option ) && $old_option !== array() ) {
  2481. $move = array(
  2482. 'opengraph',
  2483. 'fb_adminid',
  2484. 'fb_appid',
  2485. );
  2486. foreach ( $move as $key ) {
  2487. if ( isset( $old_option[ $key ] ) && ! isset( $option_value[ $key ] ) ) {
  2488. $option_value[ $key ] = $old_option[ $key ];
  2489. }
  2490. }
  2491. unset( $move, $key );
  2492. }
  2493. unset( $old_option );
  2494. /* Clean some values which may not always be in form and may otherwise not be cleaned/validated */
  2495. if ( isset( $option_value['fbapps'] ) && ( is_array( $option_value['fbapps'] ) && $option_value['fbapps'] !== array() ) ) {
  2496. $fbapps = array();
  2497. foreach ( $option_value['fbapps'] as $app_id => $display_name ) {
  2498. if ( ctype_digit( (string) $app_id ) !== false ) {
  2499. $fbapps[ $app_id ] = sanitize_text_field( $display_name );
  2500. }
  2501. }
  2502. unset( $app_id, $display_name );
  2503. $option_value['fbapps'] = $fbapps;
  2504. }
  2505. return $option_value;
  2506. }
  2507. } /* End of class WPSEO_Option_Social */
  2508. } /* End of class-exists wrapper */
  2509. /*******************************************************************
  2510. * Option: wpseo_ms
  2511. *******************************************************************/
  2512. if ( is_multisite() && ! class_exists( 'WPSEO_Option_MS' ) ) {
  2513. /**
  2514. * Site option for Multisite installs only
  2515. *
  2516. * This class will not even be available/loaded if not on multisite, so none of the actions will
  2517. * register if not on multisite.
  2518. *
  2519. * Overloads a number of methods of the abstract class to ensure the use of the correct site_option
  2520. * WP functions.
  2521. */
  2522. class WPSEO_Option_MS extends WPSEO_Option {
  2523. /**
  2524. * @var string option name
  2525. */
  2526. public $option_name = 'wpseo_ms';
  2527. /**
  2528. * @var string option group name for use in settings forms
  2529. */
  2530. public $group_name = 'yoast_wpseo_multisite_options';
  2531. /**
  2532. * @var bool whether to include the option in the return for WPSEO_Options::get_all()
  2533. */
  2534. public $include_in_all = false;
  2535. /**
  2536. * @var bool whether this option is only for when the install is multisite
  2537. */
  2538. public $multisite_only = true;
  2539. /**
  2540. * @var array Array of defaults for the option
  2541. * Shouldn't be requested directly, use $this->get_defaults();
  2542. */
  2543. protected $defaults = array(
  2544. 'access' => 'admin',
  2545. 'defaultblog' => '', //numeric blog id or empty
  2546. );
  2547. /**
  2548. * @static
  2549. * @var array $allowed_access_options Available options for the 'access' setting
  2550. * Used for input validation
  2551. *
  2552. * @internal Important: Make sure the options added to the array here are in line with the keys
  2553. * for the options set for the select box in the admin/pages/network.php file
  2554. */
  2555. public static $allowed_access_options = array(
  2556. 'admin',
  2557. 'superadmin',
  2558. );
  2559. /**
  2560. * Get the singleton instance of this class
  2561. *
  2562. * @return object
  2563. */
  2564. public static function get_instance() {
  2565. if ( ! ( self::$instance instanceof self ) ) {
  2566. self::$instance = new self();
  2567. }
  2568. return self::$instance;
  2569. }
  2570. /**
  2571. * Add filters to make sure that the option default is returned if the option is not set
  2572. *
  2573. * @return void
  2574. */
  2575. public function add_default_filters() {
  2576. // Don't change, needs to check for false as could return prio 0 which would evaluate to false
  2577. if ( has_filter( 'default_site_option_' . $this->option_name, array( $this, 'get_defaults' ) ) === false ) {
  2578. add_filter( 'default_site_option_' . $this->option_name, array( $this, 'get_defaults' ) );
  2579. }
  2580. }
  2581. /**
  2582. * Remove the default filters.
  2583. * Called from the validate() method to prevent failure to add new options
  2584. *
  2585. * @return void
  2586. */
  2587. public function remove_default_filters() {
  2588. remove_filter( 'default_site_option_' . $this->option_name, array( $this, 'get_defaults' ) );
  2589. }
  2590. /**
  2591. * Add filters to make sure that the option is merged with its defaults before being returned
  2592. *
  2593. * @return void
  2594. */
  2595. public function add_option_filters() {
  2596. // Don't change, needs to check for false as could return prio 0 which would evaluate to false
  2597. if ( has_filter( 'site_option_' . $this->option_name, array( $this, 'get_option' ) ) === false ) {
  2598. add_filter( 'site_option_' . $this->option_name, array( $this, 'get_option' ) );
  2599. }
  2600. }
  2601. /**
  2602. * Remove the option filters.
  2603. * Called from the clean_up methods to make sure we retrieve the original old option
  2604. *
  2605. * @return void
  2606. */
  2607. public function remove_option_filters() {
  2608. remove_filter( 'site_option_' . $this->option_name, array( $this, 'get_option' ) );
  2609. }
  2610. /* *********** METHODS influencing add_uption(), update_option() and saving from admin pages *********** */
  2611. /**
  2612. * Validate the option
  2613. *
  2614. * @param array $dirty New value for the option
  2615. * @param array $clean Clean value for the option, normally the defaults
  2616. * @param array $old Old value of the option
  2617. *
  2618. * @return array Validated clean value for the option to be saved to the database
  2619. */
  2620. protected function validate_option( $dirty, $clean, $old ) {
  2621. foreach ( $clean as $key => $value ) {
  2622. switch ( $key ) {
  2623. case 'access':
  2624. if ( isset( $dirty[ $key ] ) && in_array( $dirty[ $key ], self::$allowed_access_options, true ) ) {
  2625. $clean[ $key ] = $dirty[ $key ];
  2626. } elseif ( function_exists( 'add_settings_error' ) ) {
  2627. add_settings_error(
  2628. $this->group_name, // slug title of the setting
  2629. '_' . $key, // suffix-id for the error message box
  2630. sprintf( __( '%s is not a valid choice for who should be allowed access to the WP SEO settings. Value reset to the default.', 'wordpress-seo' ), esc_html( sanitize_text_field( $dirty[ $key ] ) ) ), // the error message
  2631. 'error' // error type, either 'error' or 'updated'
  2632. );
  2633. }
  2634. break;
  2635. case 'defaultblog':
  2636. if ( isset( $dirty[ $key ] ) && ( $dirty[ $key ] !== '' && $dirty[ $key ] !== '-' ) ) {
  2637. $int = self::validate_int( $dirty[ $key ] );
  2638. if ( $int !== false && $int > 0 ) {
  2639. // Check if a valid blog number has been received
  2640. $exists = get_blog_details( $int, false );
  2641. if ( $exists && $exists->deleted == 0 ) {
  2642. $clean[ $key ] = $int;
  2643. } elseif ( function_exists( 'add_settings_error' ) ) {
  2644. add_settings_error(
  2645. $this->group_name, // slug title of the setting
  2646. '_' . $key, // suffix-id for the error message box
  2647. esc_html__( 'The default blog setting must be the numeric blog id of the blog you want to use as default.', 'wordpress-seo' ) . '<br>' . sprintf( esc_html__( 'This must be an existing blog. Blog %s does not exist or has been marked as deleted.', 'wordpress-seo' ), '<strong>' . esc_html( sanitize_text_field( $dirty[ $key ] ) ) . '</strong>' ), // the error message
  2648. 'error' // error type, either 'error' or 'updated'
  2649. );
  2650. }
  2651. unset( $exists );
  2652. } elseif ( function_exists( 'add_settings_error' ) ) {
  2653. add_settings_error(
  2654. $this->group_name, // slug title of the setting
  2655. '_' . $key, // suffix-id for the error message box
  2656. esc_html__( 'The default blog setting must be the numeric blog id of the blog you want to use as default.', 'wordpress-seo' ) . '<br>' . esc_html__( 'No numeric value was received.', 'wordpress-seo' ), // the error message
  2657. 'error' // error type, either 'error' or 'updated'
  2658. );
  2659. }
  2660. unset( $int );
  2661. }
  2662. break;
  2663. default:
  2664. $clean[ $key ] = ( isset( $dirty[ $key ] ) ? self::validate_bool( $dirty[ $key ] ) : false );
  2665. break;
  2666. }
  2667. }
  2668. return $clean;
  2669. }
  2670. /**
  2671. * Clean a given option value
  2672. *
  2673. * @param array $option_value Old (not merged with defaults or filtered) option value to
  2674. * clean according to the rules for this option
  2675. * @param string $current_version (optional) Version from which to upgrade, if not set,
  2676. * version specific upgrades will be disregarded
  2677. * @param array $all_old_option_values (optional) Only used when importing old options to have
  2678. * access to the real old values, in contrast to the saved ones
  2679. *
  2680. * @return array Cleaned option
  2681. */
  2682. /*protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) {
  2683. return $option_value;
  2684. }*/
  2685. } /* End of class WPSEO_Option_MS */
  2686. } /* End of class-exists wrapper */
  2687. /*******************************************************************
  2688. * Option: wpseo_taxonomy_meta
  2689. *******************************************************************/
  2690. if ( ! class_exists( 'WPSEO_Taxonomy_Meta' ) ) {
  2691. class WPSEO_Taxonomy_Meta extends WPSEO_Option {
  2692. /**
  2693. * @var string option name
  2694. */
  2695. public $option_name = 'wpseo_taxonomy_meta';
  2696. /**
  2697. * @var bool whether to include the option in the return for WPSEO_Options::get_all()
  2698. */
  2699. public $include_in_all = false;
  2700. /**
  2701. * @var array Array of defaults for the option
  2702. * Shouldn't be requested directly, use $this->get_defaults();
  2703. * @internal Important: in contrast to most defaults, the below array format is
  2704. * very bare. The real option is in the format [taxonomy_name][term_id][...]
  2705. * where [...] is any of the $defaults_per_term options shown below.
  2706. * This is of course taken into account in the below methods.
  2707. */
  2708. protected $defaults = array();
  2709. /**
  2710. * @static
  2711. * @var string Option name - same as $option_name property, but now also available to static
  2712. * methods
  2713. */
  2714. public static $name;
  2715. /**
  2716. * @static
  2717. * @var array Array of defaults for individual taxonomy meta entries
  2718. */
  2719. public static $defaults_per_term = array(
  2720. 'wpseo_title' => '',
  2721. 'wpseo_desc' => '',
  2722. 'wpseo_metakey' => '',
  2723. 'wpseo_canonical' => '',
  2724. 'wpseo_bctitle' => '',
  2725. 'wpseo_noindex' => 'default',
  2726. 'wpseo_sitemap_include' => '-',
  2727. );
  2728. /**
  2729. * @static
  2730. * @var array Available index options
  2731. * Used for form generation and input validation
  2732. * @internal Labels (translation) added on admin_init via WPSEO_Taxonomy::translate_meta_options()
  2733. */
  2734. public static $no_index_options = array(
  2735. 'default' => '',
  2736. 'index' => '',
  2737. 'noindex' => '',
  2738. );
  2739. /**
  2740. * @static
  2741. * @var array Available sitemap include options
  2742. * Used for form generation and input validation
  2743. * @internal Labels (translation) added on admin_init via WPSEO_Taxonomy::translate_meta_options()
  2744. */
  2745. public static $sitemap_include_options = array(
  2746. '-' => '',
  2747. 'always' => '',
  2748. 'never' => '',
  2749. );
  2750. /**
  2751. * Add the actions and filters for the option
  2752. *
  2753. * @todo [JRF => testers] Check if the extra actions below would run into problems if an option
  2754. * is updated early on and if so, change the call to schedule these for a later action on add/update
  2755. * instead of running them straight away
  2756. *
  2757. * @return \WPSEO_Taxonomy_Meta
  2758. */
  2759. protected function __construct() {
  2760. parent::__construct();
  2761. /* On succesfull update/add of the option, flush the W3TC cache */
  2762. add_action( 'add_option_' . $this->option_name, array( 'WPSEO_Options', 'flush_w3tc_cache' ) );
  2763. add_action( 'update_option_' . $this->option_name, array( 'WPSEO_Options', 'flush_w3tc_cache' ) );
  2764. }
  2765. /**
  2766. * Get the singleton instance of this class
  2767. *
  2768. * @return object
  2769. */
  2770. public static function get_instance() {
  2771. if ( ! ( self::$instance instanceof self ) ) {
  2772. self::$instance = new self();
  2773. self::$name = self::$instance->option_name;
  2774. }
  2775. return self::$instance;
  2776. }
  2777. public function enrich_defaults() {
  2778. $extra_defaults_per_term = apply_filters( 'wpseo_add_extra_taxmeta_term_defaults', array() );
  2779. if ( is_array( $extra_defaults_per_term ) ) {
  2780. self::$defaults_per_term = array_merge( $extra_defaults_per_term, self::$defaults_per_term );
  2781. }
  2782. }
  2783. /**
  2784. * Helper method - Combines a fixed array of default values with an options array
  2785. * while filtering out any keys which are not in the defaults array.
  2786. *
  2787. * @static
  2788. *
  2789. * @param string $option_key Option name of the option we're doing the merge for
  2790. * @param array $options (Optional) Current options
  2791. * - if not set, the option defaults for the $option_key will be returned.
  2792. *
  2793. * @return array Combined and filtered options array.
  2794. */
  2795. /*public function array_filter_merge( $option_key, $options = null ) {
  2796. $defaults = $this->get_defaults( $option_key );
  2797. if ( ! isset( $options ) || $options === false ) {
  2798. return $defaults;
  2799. }
  2800. / *
  2801. @internal Adding the defaults to all taxonomy terms each time the option is retrieved
  2802. will be quite inefficient if there are a lot of taxonomy terms
  2803. As long as taxonomy_meta is only retrieved via methods in this class, we shouldn't need this
  2804. $options = (array) $options;
  2805. $filtered = array();
  2806. if ( $options !== array() ) {
  2807. foreach ( $options as $taxonomy => $terms ) {
  2808. if ( is_array( $terms ) && $terms !== array() ) {
  2809. foreach ( $terms as $id => $term_meta ) {
  2810. foreach ( self::$defaults_per_term as $name => $default ) {
  2811. if ( isset( $options[ $taxonomy ][ $id ][ $name ] ) ) {
  2812. $filtered[ $taxonomy ][ $id ][ $name ] = $options[ $taxonomy ][ $id ][ $name ];
  2813. }
  2814. else {
  2815. $filtered[ $name ] = $default;
  2816. }
  2817. }
  2818. }
  2819. }
  2820. }
  2821. unset( $taxonomy, $terms, $id, $term_meta, $name, $default );
  2822. }
  2823. // end of may be remove
  2824. return $filtered;
  2825. * /
  2826. return (array) $options;
  2827. }*/
  2828. /**
  2829. * Validate the option
  2830. *
  2831. * @param array $dirty New value for the option
  2832. * @param array $clean Clean value for the option, normally the defaults
  2833. * @param array $old Old value of the option
  2834. *
  2835. * @return array Validated clean value for the option to be saved to the database
  2836. */
  2837. protected function validate_option( $dirty, $clean, $old ) {
  2838. /* Prevent complete validation (which can be expensive when there are lots of terms)
  2839. if only one item has changed and has already been validated */
  2840. if ( isset( $dirty['wpseo_already_validated'] ) && $dirty['wpseo_already_validated'] === true ) {
  2841. unset( $dirty['wpseo_already_validated'] );
  2842. return $dirty;
  2843. }
  2844. foreach ( $dirty as $taxonomy => $terms ) {
  2845. /* Don't validate taxonomy - may not be registered yet and we don't want to remove valid ones */
  2846. if ( is_array( $terms ) && $terms !== array() ) {
  2847. foreach ( $terms as $term_id => $meta_data ) {
  2848. /* Only validate term if the taxonomy exists */
  2849. if ( taxonomy_exists( $taxonomy ) && get_term_by( 'id', $term_id, $taxonomy ) === false ) {
  2850. /* Is this term id a special case ? */
  2851. if ( has_filter( 'wpseo_tax_meta_special_term_id_validation_' . $term_id ) !== false ) {
  2852. $clean[ $taxonomy ][ $term_id ] = apply_filters( 'wpseo_tax_meta_special_term_id_validation_' . $term_id, $meta_data, $taxonomy, $term_id );
  2853. }
  2854. continue;
  2855. }
  2856. if ( is_array( $meta_data ) && $meta_data !== array() ) {
  2857. /* Validate meta data */
  2858. $old_meta = self::get_term_meta( $term_id, $taxonomy );
  2859. $meta_data = self::validate_term_meta_data( $meta_data, $old_meta );
  2860. if ( $meta_data !== array() ) {
  2861. $clean[ $taxonomy ][ $term_id ] = $meta_data;
  2862. }
  2863. }
  2864. // Deal with special cases (for when taxonomy doesn't exist yet)
  2865. if ( ! isset( $clean[ $taxonomy ][ $term_id ] ) && has_filter( 'wpseo_tax_meta_special_term_id_validation_' . $term_id ) !== false ) {
  2866. $clean[ $taxonomy ][ $term_id ] = apply_filters( 'wpseo_tax_meta_special_term_id_validation_' . $term_id, $meta_data, $taxonomy, $term_id );
  2867. }
  2868. }
  2869. }
  2870. }
  2871. return $clean;
  2872. }
  2873. /**
  2874. * Validate the meta data for one individual term and removes default values (no need to save those)
  2875. *
  2876. * @static
  2877. *
  2878. * @param array $meta_data New values
  2879. * @param array $old_meta The original values
  2880. *
  2881. * @return array Validated and filtered value
  2882. */
  2883. public static function validate_term_meta_data( $meta_data, $old_meta ) {
  2884. $clean = self::$defaults_per_term;
  2885. $meta_data = array_map( array( __CLASS__, 'trim_recursive' ), $meta_data );
  2886. if ( ! is_array( $meta_data ) || $meta_data === array() ) {
  2887. return $clean;
  2888. }
  2889. foreach ( $clean as $key => $value ) {
  2890. switch ( $key ) {
  2891. case 'wpseo_noindex':
  2892. if ( isset( $meta_data[ $key ] ) ) {
  2893. if ( isset( self::$no_index_options[ $meta_data[ $key ] ] ) ) {
  2894. $clean[ $key ] = $meta_data[ $key ];
  2895. }
  2896. } elseif ( isset( $old_meta[ $key ] ) ) {
  2897. // Retain old value if field currently not in use
  2898. $clean[ $key ] = $old_meta[ $key ];
  2899. }
  2900. break;
  2901. case 'wpseo_sitemap_include':
  2902. if ( isset( $meta_data[ $key ] ) && isset( self::$sitemap_include_options[ $meta_data[ $key ] ] ) ) {
  2903. $clean[ $key ] = $meta_data[ $key ];
  2904. }
  2905. break;
  2906. case 'wpseo_canonical':
  2907. if ( isset( $meta_data[ $key ] ) && $meta_data[ $key ] !== '' ) {
  2908. $url = self::sanitize_url( $meta_data[ $key ] );
  2909. if ( $url !== '' ) {
  2910. $clean[ $key ] = $url;
  2911. }
  2912. }
  2913. break;
  2914. case 'wpseo_metakey':
  2915. case 'wpseo_bctitle':
  2916. if ( isset( $meta_data[ $key ] ) ) {
  2917. $clean[ $key ] = self::sanitize_text_field( stripslashes( $meta_data[ $key ] ) );
  2918. } elseif ( isset( $old_meta[ $key ] ) ) {
  2919. // Retain old value if field currently not in use
  2920. $clean[ $key ] = $old_meta[ $key ];
  2921. }
  2922. break;
  2923. case 'wpseo_title':
  2924. case 'wpseo_desc':
  2925. default:
  2926. if ( isset( $meta_data[ $key ] ) && is_string( $meta_data[ $key ] ) ) {
  2927. $clean[ $key ] = self::sanitize_text_field( stripslashes( $meta_data[ $key ] ) );
  2928. }
  2929. break;
  2930. }
  2931. $clean[ $key ] = apply_filters( 'wpseo_sanitize_tax_meta_' . $key, $clean[ $key ], ( isset( $meta_data[ $key ] ) ? $meta_data[ $key ] : null ), ( isset( $old_meta[ $key ] ) ? $old_meta[ $key ] : null ) );
  2932. }
  2933. // Only save the non-default values
  2934. return array_diff_assoc( $clean, self::$defaults_per_term );
  2935. }
  2936. /**
  2937. * Clean a given option value
  2938. * - Convert old option values to new
  2939. * - Fixes strings which were escaped (should have been sanitized - escaping is for output)
  2940. *
  2941. * @param array $option_value Old (not merged with defaults or filtered) option value to
  2942. * clean according to the rules for this option
  2943. * @param string $current_version (optional) Version from which to upgrade, if not set,
  2944. * version specific upgrades will be disregarded
  2945. * @param array $all_old_option_values (optional) Only used when importing old options to have
  2946. * access to the real old values, in contrast to the saved ones
  2947. *
  2948. * @return array Cleaned option
  2949. */
  2950. protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) {
  2951. /* Clean up old values and remove empty arrays */
  2952. if ( is_array( $option_value ) && $option_value !== array() ) {
  2953. foreach ( $option_value as $taxonomy => $terms ) {
  2954. if ( is_array( $terms ) && $terms !== array() ) {
  2955. foreach ( $terms as $term_id => $meta_data ) {
  2956. if ( ! is_array( $meta_data ) || $meta_data === array() ) {
  2957. // Remove empty term arrays
  2958. unset( $option_value[ $taxonomy ][ $term_id ] );
  2959. } else {
  2960. foreach ( $meta_data as $key => $value ) {
  2961. switch ( $key ) {
  2962. case 'noindex':
  2963. if ( $value === 'on' ) {
  2964. // Convert 'on' to 'noindex'
  2965. $option_value[ $taxonomy ][ $term_id ][ $key ] = 'noindex';
  2966. }
  2967. break;
  2968. case 'canonical':
  2969. case 'wpseo_metakey':
  2970. case 'wpseo_bctitle':
  2971. case 'wpseo_title':
  2972. case 'wpseo_desc':
  2973. // @todo [JRF => whomever] needs checking, I don't have example data [JRF]
  2974. if ( $value !== '' ) {
  2975. // Fix incorrectly saved (encoded) canonical urls and texts
  2976. $option_value[ $taxonomy ][ $term_id ][ $key ] = wp_specialchars_decode( stripslashes( $value ), ENT_QUOTES );
  2977. }
  2978. break;
  2979. default:
  2980. // @todo [JRF => whomever] needs checking, I don't have example data [JRF]
  2981. if ( $value !== '' ) {
  2982. // Fix incorrectly saved (escaped) text strings
  2983. $option_value[ $taxonomy ][ $term_id ][ $key ] = wp_specialchars_decode( $value, ENT_QUOTES );
  2984. }
  2985. break;
  2986. }
  2987. }
  2988. }
  2989. }
  2990. } else {
  2991. // Remove empty taxonomy arrays
  2992. unset( $option_value[ $taxonomy ] );
  2993. }
  2994. }
  2995. }
  2996. return $option_value;
  2997. }
  2998. /**
  2999. * Retrieve a taxonomy term's meta value(s).
  3000. *
  3001. * @static
  3002. *
  3003. * @param mixed $term Term to get the meta value for
  3004. * either (string) term name, (int) term id or (object) term
  3005. * @param string $taxonomy Name of the taxonomy to which the term is attached
  3006. * @param string $meta (optional) Meta value to get (without prefix)
  3007. *
  3008. * @return mixed|bool Value for the $meta if one is given, might be the default
  3009. * If no meta is given, an array of all the meta data for the term
  3010. * False if the term does not exist or the $meta provided is invalid
  3011. */
  3012. public static function get_term_meta( $term, $taxonomy, $meta = null ) {
  3013. /* Figure out the term id */
  3014. if ( is_int( $term ) ) {
  3015. $term = get_term_by( 'id', $term, $taxonomy );
  3016. } elseif ( is_string( $term ) ) {
  3017. $term = get_term_by( 'slug', $term, $taxonomy );
  3018. }
  3019. if ( is_object( $term ) && isset( $term->term_id ) ) {
  3020. $term_id = $term->term_id;
  3021. } else {
  3022. return false;
  3023. }
  3024. $tax_meta = get_option( self::$name );
  3025. /* If we have data for the term, merge with defaults for complete array, otherwise set defaults */
  3026. if ( isset( $tax_meta[ $taxonomy ][ $term_id ] ) ) {
  3027. $tax_meta = array_merge( self::$defaults_per_term, $tax_meta[ $taxonomy ][ $term_id ] );
  3028. } else {
  3029. $tax_meta = self::$defaults_per_term;
  3030. }
  3031. /* Either return the complete array or a single value from it or false if the value does not exist
  3032. (shouldn't happen after merge with defaults, indicates typo in request) */
  3033. if ( ! isset( $meta ) ) {
  3034. return $tax_meta;
  3035. } else {
  3036. if ( isset( $tax_meta[ 'wpseo_' . $meta ] ) ) {
  3037. return $tax_meta[ 'wpseo_' . $meta ];
  3038. } else {
  3039. return false;
  3040. }
  3041. }
  3042. }
  3043. } /* End of class WPSEO_Taxonomy_Meta */
  3044. } /* End of class-exists wrapper */
  3045. /*******************************************************************
  3046. * Overall Option Management
  3047. *******************************************************************/
  3048. if ( ! class_exists( 'WPSEO_Options' ) ) {
  3049. /**
  3050. * Overal Option Management class
  3051. *
  3052. * Instantiates all the options and offers a number of utility methods to work with the options
  3053. */
  3054. class WPSEO_Options {
  3055. /**
  3056. * @static
  3057. * @var array Options this class uses
  3058. * Array format: (string) option_name => (string) name of concrete class for the option
  3059. */
  3060. public static $options = array(
  3061. 'wpseo' => 'WPSEO_Option_Wpseo',
  3062. 'wpseo_permalinks' => 'WPSEO_Option_Permalinks',
  3063. 'wpseo_titles' => 'WPSEO_Option_Titles',
  3064. 'wpseo_social' => 'WPSEO_Option_Social',
  3065. 'wpseo_rss' => 'WPSEO_Option_RSS',
  3066. 'wpseo_internallinks' => 'WPSEO_Option_InternalLinks',
  3067. 'wpseo_xml' => 'WPSEO_Option_XML',
  3068. 'wpseo_ms' => 'WPSEO_Option_MS',
  3069. 'wpseo_taxonomy_meta' => 'WPSEO_Taxonomy_Meta',
  3070. );
  3071. protected static $option_instances;
  3072. /**
  3073. * @var object Instance of this class
  3074. */
  3075. protected static $instance;
  3076. /**
  3077. * Instantiate all the WPSEO option management classes
  3078. */
  3079. protected function __construct() {
  3080. foreach ( self::$options as $option_name => $option_class ) {
  3081. if ( class_exists( $option_class ) ) {
  3082. self::$option_instances[ $option_name ] = call_user_func( array( $option_class, 'get_instance' ) );
  3083. }
  3084. else {
  3085. unset( self::$options[ $option_name ] );
  3086. }
  3087. }
  3088. }
  3089. /**
  3090. * Get the singleton instance of this class
  3091. *
  3092. * @return object
  3093. */
  3094. public static function get_instance() {
  3095. if ( ! ( self::$instance instanceof self ) ) {
  3096. self::$instance = new self();
  3097. }
  3098. return self::$instance;
  3099. }
  3100. /**
  3101. * Check whether the current user is allowed to access the configuration.
  3102. *
  3103. * @todo [JRF => whomever] when someone would reorganize the classes, this should maybe
  3104. * be moved to a general WPSEO_Utils class. Obviously all calls to this method should be
  3105. * adjusted in that case.
  3106. *
  3107. * @return boolean
  3108. */
  3109. public static function grant_access() {
  3110. if ( ! is_multisite() ) {
  3111. return true;
  3112. }
  3113. $options = get_site_option( 'wpseo_ms' );
  3114. if ( $options['access'] === 'admin' && current_user_can( 'manage_options' ) ) {
  3115. return true;
  3116. }
  3117. if ( $options['access'] === 'superadmin' && ! is_super_admin() ) {
  3118. return false;
  3119. }
  3120. return true;
  3121. }
  3122. /**
  3123. * Get the group name of an option for use in the settings form
  3124. *
  3125. * @param string $option_name the option for which you want to retrieve the option group name
  3126. *
  3127. * @return string|bool
  3128. */
  3129. public static function get_group_name( $option_name ) {
  3130. if ( isset( self::$option_instances[ $option_name ] ) ) {
  3131. return self::$option_instances[ $option_name ]->group_name;
  3132. }
  3133. return false;
  3134. }
  3135. /**
  3136. * Get a specific default value for an option
  3137. *
  3138. * @param string $option_name The option for which you want to retrieve a default
  3139. * @param string $key The key within the option who's default you want
  3140. *
  3141. * @return mixed
  3142. */
  3143. public static function get_default( $option_name, $key ) {
  3144. if ( isset( self::$option_instances[ $option_name ] ) ) {
  3145. $defaults = self::$option_instances[ $option_name ]->get_defaults();
  3146. if ( isset( $defaults[ $key ] ) ) {
  3147. return $defaults[ $key ];
  3148. }
  3149. }
  3150. return null;
  3151. }
  3152. /**
  3153. * Update a site_option
  3154. *
  3155. * @param string $option_name The option name of the option to save
  3156. * @param mized $value The new value for the option
  3157. *
  3158. * @return bool
  3159. */
  3160. public static function update_site_option( $option_name, $value ) {
  3161. if ( is_network_admin() && isset( self::$option_instances[ $option_name ] ) ) {
  3162. return self::$option_instances[ $option_name ]->update_site_option( $value );
  3163. }
  3164. else {
  3165. return false;
  3166. }
  3167. }
  3168. /**
  3169. * Get the instantiated option instance
  3170. *
  3171. * @param string $option_name The option for which you want to retrieve the instance
  3172. *
  3173. * @return object|bool
  3174. */
  3175. public static function get_option_instance( $option_name ) {
  3176. if ( isset( self::$option_instances[ $option_name ] ) ) {
  3177. return self::$option_instances[ $option_name ];
  3178. }
  3179. return false;
  3180. }
  3181. /**
  3182. * Retrieve an array of the options which should be included in get_all() and reset().
  3183. *
  3184. * @static
  3185. * @return array Array of option names
  3186. */
  3187. public static function get_option_names() {
  3188. static $option_names = array();
  3189. if ( $option_names === array() ) {
  3190. foreach ( self::$option_instances as $option_name => $option_object ) {
  3191. if ( $option_object->include_in_all === true ) {
  3192. $option_names[] = $option_name;
  3193. }
  3194. }
  3195. $option_names = apply_filters( 'wpseo_options', $option_names );
  3196. }
  3197. return $option_names;
  3198. }
  3199. /**
  3200. * Retrieve all the options for the SEO plugin in one go.
  3201. *
  3202. * @todo [JRF] see if we can get some extra efficiency for this one, though probably not as options may
  3203. * well change between calls (enriched defaults and such)
  3204. *
  3205. * @static
  3206. * @return array Array combining the values of (nearly) all the options
  3207. */
  3208. public static function get_all() {
  3209. $all_options = array();
  3210. $option_names = self::get_option_names();
  3211. if ( is_array( $option_names ) && $option_names !== array() ) {
  3212. foreach ( $option_names as $option_name ) {
  3213. if ( self::$option_instances[ $option_name ]->multisite_only !== true ) {
  3214. $option = get_option( $option_name );
  3215. }
  3216. else {
  3217. $option = get_site_option( $option_name );
  3218. }
  3219. if ( is_array( $option ) && $option !== array() ) {
  3220. $all_options = array_merge( $all_options, $option );
  3221. }
  3222. }
  3223. }
  3224. return $all_options;
  3225. }
  3226. /**
  3227. * Run the clean up routine for one or all options
  3228. *
  3229. * @param array|string $option_name (optional) the option you want to clean or an array of
  3230. * option names for the options you want to clean.
  3231. * If not set, all options will be cleaned
  3232. * @param string $current_version (optional) Version from which to upgrade, if not set,
  3233. * version specific upgrades will be disregarded
  3234. *
  3235. * @return void
  3236. */
  3237. public static function clean_up( $option_name = null, $current_version = null ) {
  3238. if ( isset( $option_name ) && is_string( $option_name ) && $option_name !== '' ) {
  3239. if ( isset( self::$option_instances[ $option_name ] ) ) {
  3240. self::$option_instances[ $option_name ]->clean( $current_version );
  3241. }
  3242. } elseif ( isset( $option_name ) && is_array( $option_name ) && $option_name !== array() ) {
  3243. foreach ( $option_name as $option ) {
  3244. if ( isset( self::$option_instances[ $option ] ) ) {
  3245. self::$option_instances[ $option ]->clean( $current_version );
  3246. }
  3247. }
  3248. } else {
  3249. foreach ( self::$option_instances as $instance ) {
  3250. $instance->clean( $current_version );
  3251. }
  3252. // If we've done a full clean-up, we can safely remove this really old option
  3253. delete_option( 'wpseo_indexation' );
  3254. }
  3255. }
  3256. /**
  3257. * Check that all options exist in the database and add any which don't
  3258. *
  3259. * @return void
  3260. */
  3261. public static function ensure_options_exist() {
  3262. foreach ( self::$option_instances as $instance ) {
  3263. $instance->maybe_add_option();
  3264. }
  3265. }
  3266. /**
  3267. * Correct the inadvertent removal of the fallback to default values from the breadcrumbs
  3268. *
  3269. * @since 1.5.2.3
  3270. */
  3271. public static function bring_back_breadcrumb_defaults() {
  3272. if ( isset( self::$option_instances['wpseo_internallinks'] ) ) {
  3273. self::$option_instances['wpseo_internallinks']->bring_back_defaults();
  3274. }
  3275. }
  3276. /**
  3277. * Initialize some options on first install/activate/reset
  3278. *
  3279. * @static
  3280. * @return void
  3281. */
  3282. public static function initialize() {
  3283. /* Make sure title_test and description_test function are available even when called
  3284. from the isolated activation */
  3285. require_once( WPSEO_PATH . 'inc/wpseo-non-ajax-functions.php' );
  3286. // wpseo_title_test();
  3287. wpseo_description_test();
  3288. /* Force WooThemes to use WordPress SEO data. */
  3289. if ( function_exists( 'woo_version_init' ) ) {
  3290. update_option( 'seo_woo_use_third_party_data', 'true' );
  3291. }
  3292. }
  3293. /**
  3294. * Reset all options to their default values and rerun some tests
  3295. *
  3296. * @static
  3297. * @return void
  3298. */
  3299. public static function reset() {
  3300. if ( ! is_multisite() ) {
  3301. $option_names = self::get_option_names();
  3302. if ( is_array( $option_names ) && $option_names !== array() ) {
  3303. foreach ( $option_names as $option_name ) {
  3304. delete_option( $option_name );
  3305. update_option( $option_name, get_option( $option_name ) );
  3306. }
  3307. }
  3308. }
  3309. else {
  3310. // Reset MS blog based on network default blog setting
  3311. self::reset_ms_blog( get_current_blog_id() );
  3312. }
  3313. self::initialize();
  3314. }
  3315. /**
  3316. * Initialize default values for a new multisite blog
  3317. *
  3318. * @static
  3319. *
  3320. * @param bool $force_init Whether to always do the initialization routine (title/desc test)
  3321. * @return void
  3322. */
  3323. public static function maybe_set_multisite_defaults( $force_init = false ) {
  3324. $option = get_option( 'wpseo' );
  3325. if ( is_multisite() ) {
  3326. if ( $option['ms_defaults_set'] === false ) {
  3327. self::reset_ms_blog( get_current_blog_id() );
  3328. self::initialize();
  3329. }
  3330. else if ( $force_init === true ) {
  3331. self::initialize();
  3332. }
  3333. }
  3334. }
  3335. /**
  3336. * Reset all options for a specific multisite blog to their default values based upon a
  3337. * specified default blog if one was chosen on the network page or the plugin defaults if it was not
  3338. *
  3339. * @static
  3340. *
  3341. * @param int|string $blog_id Blog id of the blog for which to reset the options
  3342. *
  3343. * @return void
  3344. */
  3345. public static function reset_ms_blog( $blog_id ) {
  3346. if ( is_multisite() ) {
  3347. $options = get_site_option( 'wpseo_ms' );
  3348. $option_names = self::get_option_names();
  3349. if ( is_array( $option_names ) && $option_names !== array() ) {
  3350. $base_blog_id = $blog_id;
  3351. if ( $options['defaultblog'] !== '' && $options['defaultblog'] != 0 ) {
  3352. $base_blog_id = $options['defaultblog'];
  3353. }
  3354. foreach ( $option_names as $option_name ) {
  3355. delete_blog_option( $blog_id, $option_name );
  3356. $new_option = get_blog_option( $base_blog_id, $option_name );
  3357. /* Remove sensitive, theme dependent and site dependent info */
  3358. if ( isset( self::$option_instances[ $option_name ] ) && self::$option_instances[ $option_name ]->ms_exclude !== array() ) {
  3359. foreach ( self::$option_instances[ $option_name ]->ms_exclude as $key ) {
  3360. unset( $new_option[ $key ] );
  3361. }
  3362. }
  3363. if ( $option_name === 'wpseo' ) {
  3364. $new_option['ms_defaults_set'] = true;
  3365. }
  3366. update_blog_option( $blog_id, $option_name, $new_option );
  3367. }
  3368. }
  3369. }
  3370. }
  3371. /* ************** METHODS FOR ACTIONS TO TAKE ON CERTAIN OPTION UPDATES ****************/
  3372. /**
  3373. * (Un-)schedule the yoast tracking cronjob if the tracking option has changed
  3374. *
  3375. * @internal Better to be done here, rather than in the Yoast_Tracking class as
  3376. * class-tracking.php may not be loaded and might not need to be (lean loading).
  3377. *
  3378. * @todo [JRF => whomever] when someone would reorganize the classes, this should maybe
  3379. * be moved to a general WPSEO_Utils class. Obviously all calls to this method should be
  3380. * adjusted in that case.
  3381. *
  3382. * @todo - [JRF => Yoast] check if this has any impact on other Yoast plugins which may
  3383. * use the same tracking schedule hook. If so, maybe get any other yoast plugin options,
  3384. * check for the tracking status and unschedule based on the combined status.
  3385. *
  3386. * @static
  3387. *
  3388. * @param mixed $disregard Not needed - passed by add/update_option action call
  3389. * Option name if option was added, old value if option was updated
  3390. * @param array $value The (new/current) value of the wpseo option
  3391. * @param bool $force_unschedule Whether to force an unschedule (i.e. on deactivate)
  3392. *
  3393. * @return void
  3394. */
  3395. public static function schedule_yoast_tracking( $disregard, $value, $force_unschedule = false ) {
  3396. $current_schedule = wp_next_scheduled( 'yoast_tracking' );
  3397. if ( $force_unschedule !== true && ( $value['yoast_tracking'] === true && $current_schedule === false ) ) {
  3398. // The tracking checks daily, but only sends new data every 7 days.
  3399. wp_schedule_event( time(), 'daily', 'yoast_tracking' );
  3400. } elseif ( $force_unschedule === true || ( $value['yoast_tracking'] === false && $current_schedule !== false ) ) {
  3401. wp_clear_scheduled_hook( 'yoast_tracking' );
  3402. }
  3403. }
  3404. /**
  3405. * Clears the WP or W3TC cache depending on which is used
  3406. *
  3407. * @todo [JRF => whomever] when someone would reorganize the classes, this should maybe
  3408. * be moved to a general WPSEO_Utils class. Obviously all calls to this method should be
  3409. * adjusted in that case.
  3410. *
  3411. * @static
  3412. * @return void
  3413. */
  3414. public static function clear_cache() {
  3415. if ( function_exists( 'w3tc_pgcache_flush' ) ) {
  3416. w3tc_pgcache_flush();
  3417. } elseif ( function_exists( 'wp_cache_clear_cache' ) ) {
  3418. wp_cache_clear_cache();
  3419. }
  3420. }
  3421. /**
  3422. * Flush W3TC cache after succesfull update/add of taxonomy meta option
  3423. *
  3424. * @todo [JRF => whomever] when someone would reorganize the classes, this should maybe
  3425. * be moved to a general WPSEO_Utils class. Obviously all calls to this method should be
  3426. * adjusted in that case.
  3427. *
  3428. * @todo [JRF => whomever] check the above and this function to see if they should be combined or really
  3429. * do something significantly different
  3430. *
  3431. * @static
  3432. * @return void
  3433. */
  3434. public static function flush_w3tc_cache() {
  3435. if ( defined( 'W3TC_DIR' ) && function_exists( 'w3tc_objectcache_flush' ) ) {
  3436. w3tc_objectcache_flush();
  3437. }
  3438. }
  3439. /**
  3440. * Clear rewrite rules
  3441. *
  3442. * @todo [JRF => whomever] when someone would reorganize the classes, this should maybe
  3443. * be moved to a general WPSEO_Utils class. Obviously all calls to this method should be
  3444. * adjusted in that case.
  3445. *
  3446. * @static
  3447. * @return void
  3448. */
  3449. public static function clear_rewrites() {
  3450. delete_option( 'rewrite_rules' );
  3451. }
  3452. } /* End of class WPSEO_Options */
  3453. } /* End of class-exists wrapper */