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

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