class-wpseo-options.php 14 KB

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