class-wpseo-options.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Internals\Options
  6. */
  7. /**
  8. * Overal Option Management class.
  9. *
  10. * Instantiates all the options and offers a number of utility methods to work with the options.
  11. */
  12. class WPSEO_Options {
  13. /**
  14. * Options this class uses.
  15. *
  16. * @var array Array format: (string) option_name => (string) name of concrete class for the option.
  17. */
  18. public static $options = [
  19. 'wpseo' => 'WPSEO_Option_Wpseo',
  20. 'wpseo_titles' => 'WPSEO_Option_Titles',
  21. 'wpseo_social' => 'WPSEO_Option_Social',
  22. 'wpseo_ms' => 'WPSEO_Option_MS',
  23. 'wpseo_taxonomy_meta' => 'WPSEO_Taxonomy_Meta',
  24. ];
  25. /**
  26. * Array of instantiated option objects.
  27. *
  28. * @var array
  29. */
  30. protected static $option_instances = [];
  31. /**
  32. * Array with the option names.
  33. *
  34. * @var array
  35. */
  36. protected static $option_names = [];
  37. /**
  38. * Instance of this class.
  39. *
  40. * @var object
  41. */
  42. protected static $instance;
  43. /**
  44. * Backfill instance.
  45. *
  46. * @var WPSEO_Options_Backfill
  47. */
  48. protected static $backfill;
  49. /**
  50. * Instantiate all the WPSEO option management classes.
  51. */
  52. protected function __construct() {
  53. // Backfill option values after transferring them to another base.
  54. self::$backfill = new WPSEO_Options_Backfill();
  55. self::$backfill->register_hooks();
  56. foreach ( self::$options as $option_name => $option_class ) {
  57. self::register_option( call_user_func( [ $option_class, 'get_instance' ] ) );
  58. }
  59. }
  60. /**
  61. * Get the singleton instance of this class.
  62. *
  63. * @return object
  64. */
  65. public static function get_instance() {
  66. if ( ! ( self::$instance instanceof self ) ) {
  67. self::$instance = new self();
  68. }
  69. return self::$instance;
  70. }
  71. /**
  72. * Registers an option to the options list.
  73. *
  74. * @param WPSEO_Option $option_instance Instance of the option.
  75. */
  76. public static function register_option( WPSEO_Option $option_instance ) {
  77. $option_name = $option_instance->get_option_name();
  78. if ( $option_instance->multisite_only && ! self::is_multisite() ) {
  79. unset( self::$options[ $option_name ], self::$option_names[ $option_name ] );
  80. return;
  81. }
  82. if ( ! array_key_exists( $option_name, self::$options ) ) {
  83. self::$options[ $option_name ] = get_class( $option_instance );
  84. }
  85. if ( $option_instance->include_in_all === true ) {
  86. self::$option_names[ $option_name ] = $option_name;
  87. }
  88. self::$option_instances[ $option_name ] = $option_instance;
  89. }
  90. /**
  91. * Get the group name of an option for use in the settings form.
  92. *
  93. * @param string $option_name The option for which you want to retrieve the option group name.
  94. *
  95. * @return string|bool
  96. */
  97. public static function get_group_name( $option_name ) {
  98. if ( isset( self::$option_instances[ $option_name ] ) ) {
  99. return self::$option_instances[ $option_name ]->group_name;
  100. }
  101. return false;
  102. }
  103. /**
  104. * Get a specific default value for an option.
  105. *
  106. * @param string $option_name The option for which you want to retrieve a default.
  107. * @param string $key The key within the option who's default you want.
  108. *
  109. * @return mixed
  110. */
  111. public static function get_default( $option_name, $key ) {
  112. if ( isset( self::$option_instances[ $option_name ] ) ) {
  113. $defaults = self::$option_instances[ $option_name ]->get_defaults();
  114. if ( isset( $defaults[ $key ] ) ) {
  115. return $defaults[ $key ];
  116. }
  117. }
  118. return null;
  119. }
  120. /**
  121. * Update a site_option.
  122. *
  123. * @param string $option_name The option name of the option to save.
  124. * @param mixed $value The new value for the option.
  125. *
  126. * @return bool
  127. */
  128. public static function update_site_option( $option_name, $value ) {
  129. if ( is_multisite() && isset( self::$option_instances[ $option_name ] ) ) {
  130. return self::$option_instances[ $option_name ]->update_site_option( $value );
  131. }
  132. return false;
  133. }
  134. /**
  135. * Get the instantiated option instance.
  136. *
  137. * @param string $option_name The option for which you want to retrieve the instance.
  138. *
  139. * @return object|bool
  140. */
  141. public static function get_option_instance( $option_name ) {
  142. if ( isset( self::$option_instances[ $option_name ] ) ) {
  143. return self::$option_instances[ $option_name ];
  144. }
  145. return false;
  146. }
  147. /**
  148. * Retrieve an array of the options which should be included in get_all() and reset().
  149. *
  150. * @return array Array of option names.
  151. */
  152. public static function get_option_names() {
  153. $option_names = array_values( self::$option_names );
  154. if ( $option_names === [] ) {
  155. foreach ( self::$option_instances as $option_name => $option_object ) {
  156. if ( $option_object->include_in_all === true ) {
  157. $option_names[] = $option_name;
  158. }
  159. }
  160. }
  161. /**
  162. * Filter: wpseo_options - Allow developers to change the option name to include.
  163. *
  164. * @api array The option names to include in get_all and reset().
  165. */
  166. return apply_filters( 'wpseo_options', $option_names );
  167. }
  168. /**
  169. * Retrieve all the options for the SEO plugin in one go.
  170. *
  171. * @todo [JRF] See if we can get some extra efficiency for this one, though probably not as options may
  172. * well change between calls (enriched defaults and such).
  173. *
  174. * @return array Array combining the values of all the options.
  175. */
  176. public static function get_all() {
  177. return self::get_options( self::get_option_names() );
  178. }
  179. /**
  180. * Retrieve one or more options for the SEO plugin.
  181. *
  182. * @param array $option_names An array of option names of the options you want to get.
  183. *
  184. * @return array Array combining the values of the requested options.
  185. */
  186. public static function get_options( array $option_names ) {
  187. $options = [];
  188. $option_names = array_filter( $option_names, 'is_string' );
  189. foreach ( $option_names as $option_name ) {
  190. if ( isset( self::$option_instances[ $option_name ] ) ) {
  191. $option = self::get_option( $option_name );
  192. $options = array_merge( $options, $option );
  193. }
  194. }
  195. return $options;
  196. }
  197. /**
  198. * Retrieve a single option for the SEO plugin.
  199. *
  200. * @param string $option_name The name of the option you want to get.
  201. *
  202. * @return array Array containing the requested option.
  203. */
  204. public static function get_option( $option_name ) {
  205. $option = null;
  206. if ( is_string( $option_name ) && ! empty( $option_name ) ) {
  207. if ( isset( self::$option_instances[ $option_name ] ) ) {
  208. if ( self::$option_instances[ $option_name ]->multisite_only !== true ) {
  209. $option = get_option( $option_name );
  210. }
  211. else {
  212. $option = get_site_option( $option_name );
  213. }
  214. }
  215. }
  216. return $option;
  217. }
  218. /**
  219. * Retrieve a single field from any option for the SEO plugin. Keys are always unique.
  220. *
  221. * @param string $key The key it should return.
  222. * @param mixed $default The default value that should be returned if the key isn't set.
  223. *
  224. * @return mixed|null Returns value if found, $default if not.
  225. */
  226. public static function get( $key, $default = null ) {
  227. self::$backfill->remove_hooks();
  228. $option = self::get_all();
  229. $option = self::add_ms_option( $option );
  230. self::$backfill->register_hooks();
  231. if ( isset( $option[ $key ] ) ) {
  232. return $option[ $key ];
  233. }
  234. return $default;
  235. }
  236. /**
  237. * Retrieve a single field from an option for the SEO plugin.
  238. *
  239. * @param string $key The key to set.
  240. * @param mixed $value The value to set.
  241. *
  242. * @return mixed|null Returns value if found, $default if not.
  243. */
  244. public static function set( $key, $value ) {
  245. $lookup_table = self::get_lookup_table();
  246. if ( isset( $lookup_table[ $key ] ) ) {
  247. return self::save_option( $lookup_table[ $key ], $key, $value );
  248. }
  249. $patterns = self::get_pattern_table();
  250. foreach ( $patterns as $pattern => $option ) {
  251. if ( strpos( $key, $pattern ) === 0 ) {
  252. return self::save_option( $option, $key, $value );
  253. }
  254. }
  255. }
  256. /**
  257. * Get an option only if it's been auto-loaded.
  258. *
  259. * @param string $option The option to retrieve.
  260. * @param bool|mixed $default A default value to return.
  261. *
  262. * @return bool|mixed
  263. */
  264. public static function get_autoloaded_option( $option, $default = false ) {
  265. $value = wp_cache_get( $option, 'options' );
  266. if ( false === $value ) {
  267. $passed_default = func_num_args() > 1;
  268. return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
  269. }
  270. return apply_filters( "option_{$option}", maybe_unserialize( $value ), $option );
  271. }
  272. /**
  273. * Run the clean up routine for one or all options.
  274. *
  275. * @param array|string $option_name Optional. the option you want to clean or an array of
  276. * option names for the options you want to clean.
  277. * If not set, all options will be cleaned.
  278. * @param string $current_version Optional. Version from which to upgrade, if not set,
  279. * version specific upgrades will be disregarded.
  280. *
  281. * @return void
  282. */
  283. public static function clean_up( $option_name = null, $current_version = null ) {
  284. if ( isset( $option_name ) && is_string( $option_name ) && $option_name !== '' ) {
  285. if ( isset( self::$option_instances[ $option_name ] ) ) {
  286. self::$option_instances[ $option_name ]->clean( $current_version );
  287. }
  288. }
  289. elseif ( isset( $option_name ) && is_array( $option_name ) && $option_name !== [] ) {
  290. foreach ( $option_name as $option ) {
  291. if ( isset( self::$option_instances[ $option ] ) ) {
  292. self::$option_instances[ $option ]->clean( $current_version );
  293. }
  294. }
  295. unset( $option );
  296. }
  297. else {
  298. foreach ( self::$option_instances as $instance ) {
  299. $instance->clean( $current_version );
  300. }
  301. unset( $instance );
  302. // If we've done a full clean-up, we can safely remove this really old option.
  303. delete_option( 'wpseo_indexation' );
  304. }
  305. }
  306. /**
  307. * Check that all options exist in the database and add any which don't.
  308. *
  309. * @return void
  310. */
  311. public static function ensure_options_exist() {
  312. foreach ( self::$option_instances as $instance ) {
  313. $instance->maybe_add_option();
  314. }
  315. }
  316. /**
  317. * Initialize some options on first install/activate/reset.
  318. *
  319. * @return void
  320. */
  321. public static function initialize() {
  322. /* Force WooThemes to use Yoast SEO data. */
  323. if ( function_exists( 'woo_version_init' ) ) {
  324. update_option( 'seo_woo_use_third_party_data', 'true' );
  325. }
  326. }
  327. /**
  328. * Reset all options to their default values and rerun some tests.
  329. *
  330. * @return void
  331. */
  332. public static function reset() {
  333. if ( ! is_multisite() ) {
  334. $option_names = self::get_option_names();
  335. if ( is_array( $option_names ) && $option_names !== [] ) {
  336. foreach ( $option_names as $option_name ) {
  337. delete_option( $option_name );
  338. update_option( $option_name, get_option( $option_name ) );
  339. }
  340. }
  341. unset( $option_names );
  342. }
  343. else {
  344. // Reset MS blog based on network default blog setting.
  345. self::reset_ms_blog( get_current_blog_id() );
  346. }
  347. self::initialize();
  348. }
  349. /**
  350. * Initialize default values for a new multisite blog.
  351. *
  352. * @param bool $force_init Whether to always do the initialization routine (title/desc test).
  353. *
  354. * @return void
  355. */
  356. public static function maybe_set_multisite_defaults( $force_init = false ) {
  357. $option = get_option( 'wpseo' );
  358. if ( is_multisite() ) {
  359. if ( $option['ms_defaults_set'] === false ) {
  360. self::reset_ms_blog( get_current_blog_id() );
  361. self::initialize();
  362. }
  363. elseif ( $force_init === true ) {
  364. self::initialize();
  365. }
  366. }
  367. }
  368. /**
  369. * Reset all options for a specific multisite blog to their default values based upon a
  370. * specified default blog if one was chosen on the network page or the plugin defaults if it was not.
  371. *
  372. * @param int|string $blog_id Blog id of the blog for which to reset the options.
  373. *
  374. * @return void
  375. */
  376. public static function reset_ms_blog( $blog_id ) {
  377. if ( is_multisite() ) {
  378. $options = get_site_option( 'wpseo_ms' );
  379. $option_names = self::get_option_names();
  380. if ( is_array( $option_names ) && $option_names !== [] ) {
  381. $base_blog_id = $blog_id;
  382. if ( $options['defaultblog'] !== '' && $options['defaultblog'] !== 0 ) {
  383. $base_blog_id = $options['defaultblog'];
  384. }
  385. foreach ( $option_names as $option_name ) {
  386. delete_blog_option( $blog_id, $option_name );
  387. $new_option = get_blog_option( $base_blog_id, $option_name );
  388. /* Remove sensitive, theme dependent and site dependent info. */
  389. if ( isset( self::$option_instances[ $option_name ] ) && self::$option_instances[ $option_name ]->ms_exclude !== [] ) {
  390. foreach ( self::$option_instances[ $option_name ]->ms_exclude as $key ) {
  391. unset( $new_option[ $key ] );
  392. }
  393. }
  394. if ( $option_name === 'wpseo' ) {
  395. $new_option['ms_defaults_set'] = true;
  396. }
  397. update_blog_option( $blog_id, $option_name, $new_option );
  398. }
  399. }
  400. }
  401. }
  402. /**
  403. * Saves the option to the database.
  404. *
  405. * @param string $wpseo_options_group_name The name for the wpseo option group in the database.
  406. * @param string $option_name The name for the option to set.
  407. * @param mixed $option_value The value for the option.
  408. *
  409. * @return boolean Returns true if the option is successfully saved in the database.
  410. */
  411. public static function save_option( $wpseo_options_group_name, $option_name, $option_value ) {
  412. $options = self::get_option( $wpseo_options_group_name );
  413. $options[ $option_name ] = $option_value;
  414. if ( isset( self::$option_instances[ $wpseo_options_group_name ] ) && self::$option_instances[ $wpseo_options_group_name ]->multisite_only === true ) {
  415. self::update_site_option( $wpseo_options_group_name, $options );
  416. }
  417. else {
  418. update_option( $wpseo_options_group_name, $options );
  419. }
  420. // Check if everything got saved properly.
  421. $saved_option = self::get_option( $wpseo_options_group_name );
  422. return $saved_option[ $option_name ] === $options[ $option_name ];
  423. }
  424. /**
  425. * Adds the multisite options to the option stack if relevant.
  426. *
  427. * @param array $option The currently present options settings.
  428. *
  429. * @return array Options possibly including multisite.
  430. */
  431. protected static function add_ms_option( $option ) {
  432. if ( ! is_multisite() ) {
  433. return $option;
  434. }
  435. $ms_option = self::get_option( 'wpseo_ms' );
  436. return array_merge( $option, $ms_option );
  437. }
  438. /**
  439. * Checks if installation is multisite.
  440. *
  441. * @return bool True when is multisite.
  442. */
  443. protected static function is_multisite() {
  444. static $is_multisite;
  445. if ( $is_multisite === null ) {
  446. $is_multisite = is_multisite();
  447. }
  448. return $is_multisite;
  449. }
  450. /**
  451. * Retrieves a lookup table to find in which option_group a key is stored.
  452. *
  453. * @return array The lookup table.
  454. */
  455. private static function get_lookup_table() {
  456. $lookup_table = [];
  457. self::$backfill->remove_hooks();
  458. foreach ( array_keys( self::$options ) as $option_name ) {
  459. $full_option = self::get_option( $option_name );
  460. foreach ( $full_option as $key => $value ) {
  461. $lookup_table[ $key ] = $option_name;
  462. }
  463. }
  464. self::$backfill->register_hooks();
  465. return $lookup_table;
  466. }
  467. /**
  468. * Retrieves a lookup table to find in which option_group a key is stored.
  469. *
  470. * @return array The lookup table.
  471. */
  472. private static function get_pattern_table() {
  473. $pattern_table = [];
  474. foreach ( self::$options as $option_name => $option_class ) {
  475. $instance = call_user_func( [ $option_class, 'get_instance' ] );
  476. foreach ( $instance->get_patterns() as $key ) {
  477. $pattern_table[ $key ] = $option_name;
  478. }
  479. }
  480. return $pattern_table;
  481. }
  482. /* ********************* DEPRECATED METHODS ********************* */
  483. /**
  484. * Correct the inadvertent removal of the fallback to default values from the breadcrumbs.
  485. *
  486. * @since 1.5.2.3
  487. *
  488. * @deprecated 7.0
  489. * @codeCoverageIgnore
  490. */
  491. public static function bring_back_breadcrumb_defaults() {
  492. _deprecated_function( __METHOD__, 'WPSEO 7.0' );
  493. }
  494. }