WPSEO_Configuration_Options_Adapter

Class WPSEO_Configuration_Options_Adapter.

Defined (1)

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

/admin/config-ui/class-configuration-options-adapter.php  
  1. class WPSEO_Configuration_Options_Adapter { 
  2.  
  3. const OPTION_TYPE_WORDPRESS = 'wordpress'; 
  4. const OPTION_TYPE_YOAST = 'yoast'; 
  5. const OPTION_TYPE_CUSTOM = 'custom'; 
  6.  
  7. /** @var array List of registered lookups */ 
  8. protected $lookup = array(); 
  9.  
  10. /** 
  11. * Add a lookup for a WordPress native option 
  12. * @param string $class_name Class to bind to an option. 
  13. * @param string $option Option name to use. 
  14. * @throws InvalidArgumentException Thrown when invalid input is provided. 
  15. */ 
  16. public function add_wordpress_lookup( $class_name, $option ) { 
  17.  
  18. if ( ! is_string( $option ) ) { 
  19. throw new InvalidArgumentException( 'WordPress option must be a string.' ); 
  20.  
  21. $this->add_lookup( $class_name, self::OPTION_TYPE_WORDPRESS, $option ); 
  22.  
  23. /** 
  24. * Add a lookup for a Yoast option 
  25. * @param string $class_name Class to bind to the lookup. 
  26. * @param string $option Option group to use. 
  27. * @param string $key Key in the option group to bind to. 
  28. * @throws InvalidArgumentException Thrown when invalid input is provided. 
  29. */ 
  30. public function add_yoast_lookup( $class_name, $option, $key ) { 
  31.  
  32. $test = WPSEO_Options::get_option( $option ); 
  33. if ( is_null( $test ) ) { 
  34. /** translators: %1$s resolves to the option name passed to the lookup registration */ 
  35. throw new InvalidArgumentException( sprintf( __( 'Yoast option %1$s not found.', 'wordpress-seo' ), $option ) ); 
  36.  
  37. $this->add_lookup( $class_name, self::OPTION_TYPE_YOAST, array( 
  38. $option,  
  39. $key,  
  40. ) ); 
  41.  
  42. /** 
  43. * Add a lookup for a custom implementation 
  44. * @param string $class_name Class to bind to the lookup. 
  45. * @param callable $callback_get Callback to retrieve data. 
  46. * @param callable $callback_set Callback to save data. 
  47. * @throws InvalidArgumentException Thrown when invalid input is provided. 
  48. */ 
  49. public function add_custom_lookup( $class_name, $callback_get, $callback_set ) { 
  50.  
  51. if ( ! is_callable( $callback_get ) || ! is_callable( $callback_set ) ) { 
  52. throw new InvalidArgumentException( 'Custom option must be callable.' ); 
  53.  
  54. $this->add_lookup( $class_name, self::OPTION_TYPE_CUSTOM, array( 
  55. $callback_get,  
  56. $callback_set,  
  57. ) ); 
  58.  
  59. /** 
  60. * Add a field lookup. 
  61. * @param string $class_name Class to add lookup for. 
  62. * @param string $type Type of lookup. 
  63. * @param string|array $option Implementation of the lookup. 
  64. * @throws Exception Thrown when invalid input is provided. 
  65. */ 
  66. protected function add_lookup( $class_name, $type, $option ) { 
  67. $this->lookup[ $class_name ] = array( 
  68. 'type' => $type,  
  69. 'option' => $option,  
  70. ); 
  71.  
  72. /** 
  73. * Get the data for the provided field 
  74. * @param WPSEO_Config_Field $field Field to get data for. 
  75. * @return mixed 
  76. */ 
  77. public function get( WPSEO_Config_Field $field ) { 
  78. $identifier = $field->get_identifier(); 
  79.  
  80. // Lookup option and retrieve value. 
  81. $type = $this->get_option_type( $identifier ); 
  82. $option = $this->get_option( $identifier ); 
  83.  
  84. switch ( $type ) { 
  85. case self::OPTION_TYPE_WORDPRESS: 
  86. return get_option( $option ); 
  87.  
  88. case self::OPTION_TYPE_YOAST: 
  89. $group = WPSEO_Options::get_option( $option[0] ); 
  90.  
  91. return $group[ $option[1] ]; 
  92.  
  93. case self::OPTION_TYPE_CUSTOM: 
  94. return call_user_func( $option[0] ); 
  95.  
  96. return null; 
  97.  
  98. /** 
  99. * Save data from a field 
  100. * @param WPSEO_Config_Field $field Field to use for lookup. 
  101. * @param mixed $value Value to save to the lookup of the field. 
  102. * @return bool 
  103. */ 
  104. public function set( WPSEO_Config_Field $field, $value ) { 
  105. $identifier = $field->get_identifier(); 
  106.  
  107. // Lookup option and retrieve value. 
  108. $type = $this->get_option_type( $identifier ); 
  109. $option = $this->get_option( $identifier ); 
  110.  
  111. switch ( $type ) { 
  112. case self::OPTION_TYPE_WORDPRESS: 
  113. return update_option( $option, $value ); 
  114.  
  115. case self::OPTION_TYPE_YOAST: 
  116. $group = WPSEO_Options::get_option( $option[0] ); 
  117.  
  118. $group[ $option[1] ] = $value; 
  119. update_option( $option[0], $group ); 
  120.  
  121. $saved = WPSEO_Options::get_option( $option[0] ); 
  122.  
  123. return $saved[ $option[1] ] === $value; 
  124.  
  125. case self::OPTION_TYPE_CUSTOM: 
  126. return call_user_func( $option[1], $value ); 
  127.  
  128. return false; 
  129.  
  130. /** 
  131. * Get the lookup type for a specific class 
  132. * @param string $class_name Class to get the type of. 
  133. * @return null|string 
  134. */ 
  135. protected function get_option_type( $class_name ) { 
  136. if ( ! isset( $this->lookup[ $class_name ] ) ) { 
  137. return null; 
  138.  
  139. return $this->lookup[ $class_name ]['type']; 
  140.  
  141. /** 
  142. * Get the option for a specific class 
  143. * @param string $class_name Class to get the option of. 
  144. * @return null|string|array 
  145. */ 
  146. protected function get_option( $class_name ) { 
  147. if ( ! isset( $this->lookup[ $class_name ] ) ) { 
  148. return null; 
  149.  
  150. return $this->lookup[ $class_name ]['option'];