class-configuration-options-adapter.php 5.5 KB

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