WPSEO_Options

Overal Option Management class.

Defined (1)

The class is defined in the following location(s).

/inc/options/class-wpseo-options.php  
  1. class WPSEO_Options { 
  2.  
  3. /** 
  4. * @var array Options this class uses 
  5. * Array format: (string) option_name => (string) name of concrete class for the option 
  6. * @static 
  7. */ 
  8. public static $options = array( 
  9. 'wpseo' => 'WPSEO_Option_Wpseo',  
  10. 'wpseo_permalinks' => 'WPSEO_Option_Permalinks',  
  11. 'wpseo_titles' => 'WPSEO_Option_Titles',  
  12. 'wpseo_social' => 'WPSEO_Option_Social',  
  13. 'wpseo_rss' => 'WPSEO_Option_RSS',  
  14. 'wpseo_internallinks' => 'WPSEO_Option_InternalLinks',  
  15. 'wpseo_xml' => 'WPSEO_Option_XML',  
  16. 'wpseo_ms' => 'WPSEO_Option_MS',  
  17. 'wpseo_taxonomy_meta' => 'WPSEO_Taxonomy_Meta',  
  18. ); 
  19.  
  20. /** 
  21. * @var array Array of instantiated option objects 
  22. */ 
  23. protected static $option_instances = array(); 
  24.  
  25. /** 
  26. * @var object Instance of this class 
  27. */ 
  28. protected static $instance; 
  29.  
  30.  
  31. /** 
  32. * Instantiate all the WPSEO option management classes 
  33. */ 
  34. protected function __construct() { 
  35. $is_multisite = is_multisite(); 
  36.  
  37. foreach ( self::$options as $option_name => $option_class ) { 
  38. $instance = call_user_func( array( $option_class, 'get_instance' ) ); 
  39.  
  40. if ( ! $instance->multisite_only || $is_multisite ) { 
  41. self::$option_instances[ $option_name ] = $instance; 
  42. else { 
  43. unset( self::$options[ $option_name ] ); 
  44.  
  45. /** 
  46. * Get the singleton instance of this class 
  47. * @return object 
  48. */ 
  49. public static function get_instance() { 
  50. if ( ! ( self::$instance instanceof self ) ) { 
  51. self::$instance = new self(); 
  52.  
  53. return self::$instance; 
  54.  
  55. /** 
  56. * Get the group name of an option for use in the settings form 
  57. * @param string $option_name the option for which you want to retrieve the option group name. 
  58. * @return string|bool 
  59. */ 
  60. public static function get_group_name( $option_name ) { 
  61. if ( isset( self::$option_instances[ $option_name ] ) ) { 
  62. return self::$option_instances[ $option_name ]->group_name; 
  63.  
  64. return false; 
  65.  
  66. /** 
  67. * Get a specific default value for an option 
  68. * @param string $option_name The option for which you want to retrieve a default. 
  69. * @param string $key The key within the option who's default you want. 
  70. * @return mixed 
  71. */ 
  72. public static function get_default( $option_name, $key ) { 
  73. if ( isset( self::$option_instances[ $option_name ] ) ) { 
  74. $defaults = self::$option_instances[ $option_name ]->get_defaults(); 
  75. if ( isset( $defaults[ $key ] ) ) { 
  76. return $defaults[ $key ]; 
  77.  
  78. return null; 
  79.  
  80. /** 
  81. * Update a site_option 
  82. * @param string $option_name The option name of the option to save. 
  83. * @param mixed $value The new value for the option. 
  84. * @return bool 
  85. */ 
  86. public static function update_site_option( $option_name, $value ) { 
  87. if ( is_network_admin() && isset( self::$option_instances[ $option_name ] ) ) { 
  88. return self::$option_instances[ $option_name ]->update_site_option( $value ); 
  89. else { 
  90. return false; 
  91.  
  92. /** 
  93. * Get the instantiated option instance 
  94. * @param string $option_name The option for which you want to retrieve the instance. 
  95. * @return object|bool 
  96. */ 
  97. public static function get_option_instance( $option_name ) { 
  98. if ( isset( self::$option_instances[ $option_name ] ) ) { 
  99. return self::$option_instances[ $option_name ]; 
  100.  
  101. return false; 
  102.  
  103. /** 
  104. * Retrieve an array of the options which should be included in get_all() and reset(). 
  105. * @static 
  106. * @return array Array of option names 
  107. */ 
  108. public static function get_option_names() { 
  109. static $option_names = array(); 
  110.  
  111. if ( $option_names === array() ) { 
  112. foreach ( self::$option_instances as $option_name => $option_object ) { 
  113. if ( $option_object->include_in_all === true ) { 
  114. $option_names[] = $option_name; 
  115. $option_names = apply_filters( 'wpseo_options', $option_names ); 
  116.  
  117. return $option_names; 
  118.  
  119. /** 
  120. * Retrieve all the options for the SEO plugin in one go. 
  121. * @todo [JRF] see if we can get some extra efficiency for this one, though probably not as options may 
  122. * well change between calls (enriched defaults and such) 
  123. * @static 
  124. * @return array Array combining the values of all the options 
  125. */ 
  126. public static function get_all() { 
  127. return self::get_options( self::get_option_names() ); 
  128.  
  129. /** 
  130. * Retrieve one or more options for the SEO plugin. 
  131. * @static 
  132. * @param array $option_names An array of option names of the options you want to get. 
  133. * @return array Array combining the values of the requested options 
  134. */ 
  135. public static function get_options( array $option_names ) { 
  136. $options = array(); 
  137. $option_names = array_filter( $option_names, 'is_string' ); 
  138. foreach ( $option_names as $option_name ) { 
  139. if ( isset( self::$option_instances[ $option_name ] ) ) { 
  140. $option = self::get_option( $option_name ); 
  141. $options = array_merge( $options, $option ); 
  142.  
  143. return $options; 
  144.  
  145. /** 
  146. * Retrieve a single option for the SEO plugin. 
  147. * @static 
  148. * @param string $option_name the name of the option you want to get. 
  149. * @return array Array containing the requested option 
  150. */ 
  151. public static function get_option( $option_name ) { 
  152. $option = null; 
  153. if ( is_string( $option_name ) && ! empty( $option_name ) ) { 
  154. if ( isset( self::$option_instances[ $option_name ] ) ) { 
  155. if ( self::$option_instances[ $option_name ]->multisite_only !== true ) { 
  156. $option = get_option( $option_name ); 
  157. else { 
  158. $option = get_site_option( $option_name ); 
  159.  
  160. return $option; 
  161.  
  162. /** 
  163. * Get an option only if it's been auto-loaded. 
  164. * @static 
  165. * @param string $option The option to retrieve. 
  166. * @param bool|mixed $default A default value to return. 
  167. * @return bool|mixed 
  168. */ 
  169. public static function get_autoloaded_option( $option, $default = false ) { 
  170. $value = wp_cache_get( $option, 'options' ); 
  171. if ( false === $value ) { 
  172. $passed_default = func_num_args() > 1; 
  173.  
  174. return apply_filters( "default_option_{$option}", $default, $option, $passed_default ); 
  175.  
  176. return apply_filters( "option_{$option}", maybe_unserialize( $value ), $option ); 
  177.  
  178. /** 
  179. * Run the clean up routine for one or all options 
  180. * @param array|string $option_name (optional) the option you want to clean or an array of 
  181. * option names for the options you want to clean. 
  182. * If not set, all options will be cleaned. 
  183. * @param string $current_version (optional) Version from which to upgrade, if not set,  
  184. * version specific upgrades will be disregarded. 
  185. * @return void 
  186. */ 
  187. public static function clean_up( $option_name = null, $current_version = null ) { 
  188. if ( isset( $option_name ) && is_string( $option_name ) && $option_name !== '' ) { 
  189. if ( isset( self::$option_instances[ $option_name ] ) ) { 
  190. self::$option_instances[ $option_name ]->clean( $current_version ); 
  191. elseif ( isset( $option_name ) && is_array( $option_name ) && $option_name !== array() ) { 
  192. foreach ( $option_name as $option ) { 
  193. if ( isset( self::$option_instances[ $option ] ) ) { 
  194. self::$option_instances[ $option ]->clean( $current_version ); 
  195. unset( $option ); 
  196. else { 
  197. foreach ( self::$option_instances as $instance ) { 
  198. $instance->clean( $current_version ); 
  199. unset( $instance ); 
  200.  
  201. // If we've done a full clean-up, we can safely remove this really old option. 
  202. delete_option( 'wpseo_indexation' ); 
  203.  
  204.  
  205. /** 
  206. * Check that all options exist in the database and add any which don't 
  207. * @return void 
  208. */ 
  209. public static function ensure_options_exist() { 
  210. foreach ( self::$option_instances as $instance ) { 
  211. $instance->maybe_add_option(); 
  212.  
  213. /** 
  214. * Correct the inadvertent removal of the fallback to default values from the breadcrumbs 
  215. * @since 1.5.2.3 
  216. */ 
  217. public static function bring_back_breadcrumb_defaults() { 
  218. if ( isset( self::$option_instances['wpseo_internallinks'] ) ) { 
  219. self::$option_instances['wpseo_internallinks']->bring_back_defaults(); 
  220.  
  221. /** 
  222. * Initialize some options on first install/activate/reset 
  223. * @static 
  224. * @return void 
  225. */ 
  226. public static function initialize() { 
  227. /** Force WooThemes to use Yoast SEO data. */ 
  228. if ( function_exists( 'woo_version_init' ) ) { 
  229. update_option( 'seo_woo_use_third_party_data', 'true' ); 
  230.  
  231. /** 
  232. * Reset all options to their default values and rerun some tests 
  233. * @static 
  234. * @return void 
  235. */ 
  236. public static function reset() { 
  237. if ( ! is_multisite() ) { 
  238. $option_names = self::get_option_names(); 
  239. if ( is_array( $option_names ) && $option_names !== array() ) { 
  240. foreach ( $option_names as $option_name ) { 
  241. delete_option( $option_name ); 
  242. update_option( $option_name, get_option( $option_name ) ); 
  243. unset( $option_names ); 
  244. else { 
  245. // Reset MS blog based on network default blog setting. 
  246. self::reset_ms_blog( get_current_blog_id() ); 
  247.  
  248. self::initialize(); 
  249.  
  250. /** 
  251. * Initialize default values for a new multisite blog 
  252. * @static 
  253. * @param bool $force_init Whether to always do the initialization routine (title/desc test). 
  254. * @return void 
  255. */ 
  256. public static function maybe_set_multisite_defaults( $force_init = false ) { 
  257. $option = get_option( 'wpseo' ); 
  258.  
  259. if ( is_multisite() ) { 
  260. if ( $option['ms_defaults_set'] === false ) { 
  261. self::reset_ms_blog( get_current_blog_id() ); 
  262. self::initialize(); 
  263. else if ( $force_init === true ) { 
  264. self::initialize(); 
  265.  
  266. /** 
  267. * Reset all options for a specific multisite blog to their default values based upon a 
  268. * specified default blog if one was chosen on the network page or the plugin defaults if it was not 
  269. * @static 
  270. * @param int|string $blog_id Blog id of the blog for which to reset the options. 
  271. * @return void 
  272. */ 
  273. public static function reset_ms_blog( $blog_id ) { 
  274. if ( is_multisite() ) { 
  275. $options = get_site_option( 'wpseo_ms' ); 
  276. $option_names = self::get_option_names(); 
  277.  
  278. if ( is_array( $option_names ) && $option_names !== array() ) { 
  279. $base_blog_id = $blog_id; 
  280. if ( $options['defaultblog'] !== '' && $options['defaultblog'] != 0 ) { 
  281. $base_blog_id = $options['defaultblog']; 
  282.  
  283. foreach ( $option_names as $option_name ) { 
  284. delete_blog_option( $blog_id, $option_name ); 
  285.  
  286. $new_option = get_blog_option( $base_blog_id, $option_name ); 
  287.  
  288. /** Remove sensitive, theme dependent and site dependent info */ 
  289. if ( isset( self::$option_instances[ $option_name ] ) && self::$option_instances[ $option_name ]->ms_exclude !== array() ) { 
  290. foreach ( self::$option_instances[ $option_name ]->ms_exclude as $key ) { 
  291. unset( $new_option[ $key ] ); 
  292.  
  293. if ( $option_name === 'wpseo' ) { 
  294. $new_option['ms_defaults_set'] = true; 
  295.  
  296. update_blog_option( $blog_id, $option_name, $new_option ); 
  297.  
  298. /** 
  299. * Saves the option to the database. 
  300. * @param string $wpseo_options_group_name The name for the wpseo option group in the database. 
  301. * @param string $option_name The name for the option to set. 
  302. * @param * $option_value The value for the option. 
  303. * @return boolean Returns true if the option is successfully saved in the database. 
  304. */ 
  305. public static function save_option( $wpseo_options_group_name, $option_name, $option_value ) { 
  306. $options = WPSEO_Options::get_option( $wpseo_options_group_name ); 
  307. $options[ $option_name ] = $option_value; 
  308. update_option( $wpseo_options_group_name, $options ); 
  309.  
  310. // Check if everything got saved properly. 
  311. $saved_option = self::get_option( $wpseo_options_group_name ); 
  312. return $saved_option[ $option_name ] === $options[ $option_name ]; 
  313.  
  314. /********************** DEPRECATED FUNCTIONS **********************/ 
  315.  
  316. // @codeCoverageIgnoreStart 
  317. /** 
  318. * Check whether the current user is allowed to access the configuration. 
  319. * @deprecated 1.5.6.1 
  320. * @deprecated use WPSEO_Utils::grant_access() 
  321. * @see WPSEO_Utils::grant_access() 
  322. * @return boolean 
  323. */ 
  324. public static function grant_access() { 
  325. _deprecated_function( __METHOD__, 'WPSEO 1.5.6.1', 'WPSEO_Utils::grant_access()' ); 
  326.  
  327. return WPSEO_Utils::grant_access(); 
  328.  
  329. /** 
  330. * Clears the WP or W3TC cache depending on which is used 
  331. * @deprecated 1.5.6.1 
  332. * @deprecated use WPSEO_Utils::clear_cache() 
  333. * @see WPSEO_Utils::clear_cache() 
  334. */ 
  335. public static function clear_cache() { 
  336. _deprecated_function( __METHOD__, 'WPSEO 1.5.6.1', 'WPSEO_Utils::clear_cache()' ); 
  337. WPSEO_Utils::clear_cache(); 
  338.  
  339.  
  340. /** 
  341. * Flush W3TC cache after succesfull update/add of taxonomy meta option 
  342. * @deprecated 1.5.6.1 
  343. * @deprecated use WPSEO_Utils::flush_w3tc_cache() 
  344. * @see WPSEO_Utils::flush_w3tc_cache() 
  345. */ 
  346. public static function flush_w3tc_cache() { 
  347. _deprecated_function( __METHOD__, 'WPSEO 1.5.6.1', 'WPSEO_Utils::flush_w3tc_cache()' ); 
  348. WPSEO_Utils::flush_w3tc_cache(); 
  349.  
  350.  
  351. /** 
  352. * Clear rewrite rules 
  353. * @deprecated 1.5.6.1 
  354. * @deprecated use WPSEO_Utils::clear_rewrites() 
  355. * @see WPSEO_Utils::clear_rewrites() 
  356. */ 
  357. public static function clear_rewrites() { 
  358. _deprecated_function( __METHOD__, 'WPSEO 1.5.6.1', 'WPSEO_Utils::clear_rewrites()' ); 
  359. WPSEO_Utils::clear_rewrites(); 
  360. // @codeCoverageIgnoreEnd