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