class-configuration-options-adapter.php 5.2 KB

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