class-yoast-network-settings-api.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Network
  6. */
  7. /**
  8. * Implements a network settings API for the plugin's multisite settings.
  9. */
  10. class Yoast_Network_Settings_API {
  11. /**
  12. * Registered network settings.
  13. *
  14. * @var array
  15. */
  16. private $registered_settings = [];
  17. /**
  18. * Options whitelist, keyed by option group.
  19. *
  20. * @var array
  21. */
  22. private $whitelist_options = [];
  23. /**
  24. * The singleton instance of this class.
  25. *
  26. * @var Yoast_Network_Settings_API
  27. */
  28. private static $instance = null;
  29. /**
  30. * Registers a network setting and its data.
  31. *
  32. * @param string $option_group The group the network option is part of.
  33. * @param string $option_name The name of the network option to sanitize and save.
  34. * @param array $args {
  35. * Optional. Data used to describe the network setting when registered.
  36. *
  37. * @type callable $sanitize_callback A callback function that sanitizes the network option's value.
  38. * @type mixed $default Default value when calling `get_network_option()`.
  39. * }
  40. *
  41. * @return void
  42. */
  43. public function register_setting( $option_group, $option_name, $args = [] ) {
  44. $defaults = [
  45. 'group' => $option_group,
  46. 'sanitize_callback' => null,
  47. ];
  48. $args = wp_parse_args( $args, $defaults );
  49. if ( ! isset( $this->whitelist_options[ $option_group ] ) ) {
  50. $this->whitelist_options[ $option_group ] = [];
  51. }
  52. $this->whitelist_options[ $option_group ][] = $option_name;
  53. if ( ! empty( $args['sanitize_callback'] ) ) {
  54. add_filter( "sanitize_option_{$option_name}", [ $this, 'filter_sanitize_option' ], 10, 2 );
  55. }
  56. if ( array_key_exists( 'default', $args ) ) {
  57. add_filter( "default_site_option_{$option_name}", [ $this, 'filter_default_option' ], 10, 2 );
  58. }
  59. $this->registered_settings[ $option_name ] = $args;
  60. }
  61. /**
  62. * Gets the registered settings and their data.
  63. *
  64. * @return array Array of $option_name => $data pairs.
  65. */
  66. public function get_registered_settings() {
  67. return $this->registered_settings;
  68. }
  69. /**
  70. * Gets the whitelisted options for a given option group.
  71. *
  72. * @param string $option_group Option group.
  73. *
  74. * @return array List of option names, or empty array if unknown option group.
  75. */
  76. public function get_whitelist_options( $option_group ) {
  77. if ( ! isset( $this->whitelist_options[ $option_group ] ) ) {
  78. return [];
  79. }
  80. return $this->whitelist_options[ $option_group ];
  81. }
  82. /**
  83. * Filters sanitization for a network option value.
  84. *
  85. * This method is added as a filter to `sanitize_option_{$option}` for network options that are
  86. * registered with a sanitize callback.
  87. *
  88. * @param string $value The sanitized option value.
  89. * @param string $option The option name.
  90. *
  91. * @return string The filtered sanitized option value.
  92. */
  93. public function filter_sanitize_option( $value, $option ) {
  94. if ( empty( $this->registered_settings[ $option ] ) ) {
  95. return $value;
  96. }
  97. return call_user_func( $this->registered_settings[ $option ]['sanitize_callback'], $value );
  98. }
  99. /**
  100. * Filters the default value for a network option.
  101. *
  102. * This function is added as a filter to `default_site_option_{$option}` for network options that
  103. * are registered with a default.
  104. *
  105. * @param mixed $default Existing default value to return.
  106. * @param string $option The option name.
  107. *
  108. * @return mixed The filtered default value.
  109. */
  110. public function filter_default_option( $default, $option ) {
  111. // If a default value was manually passed to the function, allow it to override.
  112. if ( $default !== false ) {
  113. return $default;
  114. }
  115. if ( empty( $this->registered_settings[ $option ] ) ) {
  116. return $default;
  117. }
  118. return $this->registered_settings[ $option ]['default'];
  119. }
  120. /**
  121. * Checks whether the requirements to use this class are met.
  122. *
  123. * @return bool True if requirements are met, false otherwise.
  124. */
  125. public function meets_requirements() {
  126. return is_multisite();
  127. }
  128. /**
  129. * Gets the singleton instance of this class.
  130. *
  131. * @return Yoast_Network_Settings_API The singleton instance.
  132. */
  133. public static function get() {
  134. if ( self::$instance === null ) {
  135. self::$instance = new self();
  136. }
  137. return self::$instance;
  138. }
  139. }