class-wpseo-option-ms.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Internals\Options
  6. */
  7. /**
  8. * Site option for Multisite installs only
  9. *
  10. * Overloads a number of methods of the abstract class to ensure the use of the correct site_option
  11. * WP functions.
  12. */
  13. class WPSEO_Option_MS extends WPSEO_Option {
  14. /**
  15. * @var string option name
  16. */
  17. public $option_name = 'wpseo_ms';
  18. /**
  19. * @var string option group name for use in settings forms
  20. */
  21. public $group_name = 'yoast_wpseo_multisite_options';
  22. /**
  23. * @var bool whether to include the option in the return for WPSEO_Options::get_all()
  24. */
  25. public $include_in_all = false;
  26. /**
  27. * @var bool whether this option is only for when the install is multisite
  28. */
  29. public $multisite_only = true;
  30. /**
  31. * @var array Array of defaults for the option
  32. * Shouldn't be requested directly, use $this->get_defaults();
  33. */
  34. protected $defaults = array(
  35. 'access' => 'admin',
  36. 'defaultblog' => '', // Numeric blog ID or empty.
  37. );
  38. /**
  39. * @var array $allowed_access_options Available options for the 'access' setting
  40. * Used for input validation
  41. *
  42. * @static
  43. *
  44. * {@internal Important: Make sure the options added to the array here are in line
  45. * with the keys for the options set for the select box in the
  46. * admin/pages/network.php file.}}
  47. */
  48. public static $allowed_access_options = array(
  49. 'admin',
  50. 'superadmin',
  51. );
  52. /**
  53. * Get the singleton instance of this class
  54. *
  55. * @return object
  56. */
  57. public static function get_instance() {
  58. if ( ! ( self::$instance instanceof self ) ) {
  59. self::$instance = new self();
  60. }
  61. return self::$instance;
  62. }
  63. /**
  64. * Only run parent constructor in multisite context.
  65. */
  66. public function __construct() {
  67. if ( is_multisite() ) {
  68. parent::__construct();
  69. }
  70. }
  71. /**
  72. * Add filters to make sure that the option default is returned if the option is not set
  73. *
  74. * @return void
  75. */
  76. public function add_default_filters() {
  77. // Don't change, needs to check for false as could return prio 0 which would evaluate to false.
  78. if ( has_filter( 'default_site_option_' . $this->option_name, array( $this, 'get_defaults' ) ) === false ) {
  79. add_filter( 'default_site_option_' . $this->option_name, array( $this, 'get_defaults' ) );
  80. }
  81. }
  82. /**
  83. * Remove the default filters.
  84. * Called from the validate() method to prevent failure to add new options
  85. *
  86. * @return void
  87. */
  88. public function remove_default_filters() {
  89. remove_filter( 'default_site_option_' . $this->option_name, array( $this, 'get_defaults' ) );
  90. }
  91. /**
  92. * Add filters to make sure that the option is merged with its defaults before being returned
  93. *
  94. * @return void
  95. */
  96. public function add_option_filters() {
  97. // Don't change, needs to check for false as could return prio 0 which would evaluate to false.
  98. if ( has_filter( 'site_option_' . $this->option_name, array( $this, 'get_option' ) ) === false ) {
  99. add_filter( 'site_option_' . $this->option_name, array( $this, 'get_option' ) );
  100. }
  101. }
  102. /**
  103. * Remove the option filters.
  104. * Called from the clean_up methods to make sure we retrieve the original old option
  105. *
  106. * @return void
  107. */
  108. public function remove_option_filters() {
  109. remove_filter( 'site_option_' . $this->option_name, array( $this, 'get_option' ) );
  110. }
  111. /* *********** METHODS influencing add_uption(), update_option() and saving from admin pages *********** */
  112. /**
  113. * Validate the option
  114. *
  115. * @param array $dirty New value for the option.
  116. * @param array $clean Clean value for the option, normally the defaults.
  117. * @param array $old Old value of the option.
  118. *
  119. * @return array Validated clean value for the option to be saved to the database
  120. */
  121. protected function validate_option( $dirty, $clean, $old ) {
  122. foreach ( $clean as $key => $value ) {
  123. switch ( $key ) {
  124. case 'access':
  125. if ( isset( $dirty[ $key ] ) && in_array( $dirty[ $key ], self::$allowed_access_options, true ) ) {
  126. $clean[ $key ] = $dirty[ $key ];
  127. }
  128. elseif ( function_exists( 'add_settings_error' ) ) {
  129. add_settings_error(
  130. $this->group_name, // Slug title of the setting.
  131. '_' . $key, // Suffix-id for the error message box.
  132. /* translators: %1$s expands to the option name and %2$sexpands to Yoast SEO */
  133. sprintf( __( '%1$s is not a valid choice for who should be allowed access to the %2$s settings. Value reset to the default.', 'wordpress-seo' ), esc_html( sanitize_text_field( $dirty[ $key ] ) ), 'Yoast SEO' ), // The error message.
  134. 'error' // Error type, either 'error' or 'updated'.
  135. );
  136. }
  137. break;
  138. case 'defaultblog':
  139. if ( isset( $dirty[ $key ] ) && ( $dirty[ $key ] !== '' && $dirty[ $key ] !== '-' ) ) {
  140. $int = WPSEO_Utils::validate_int( $dirty[ $key ] );
  141. if ( $int !== false && $int > 0 ) {
  142. // Check if a valid blog number has been received.
  143. $exists = get_blog_details( $int, false );
  144. if ( $exists && $exists->deleted === '0' ) {
  145. $clean[ $key ] = $int;
  146. }
  147. elseif ( function_exists( 'add_settings_error' ) ) {
  148. add_settings_error(
  149. $this->group_name, // Slug title of the setting.
  150. '_' . $key, // Suffix-id for the error message box.
  151. esc_html__( 'The default blog setting must be the numeric blog id of the blog you want to use as default.', 'wordpress-seo' )
  152. . '<br>'
  153. . sprintf(
  154. /* translators: %s is the ID number of a blog. */
  155. esc_html__( 'This must be an existing blog. Blog %s does not exist or has been marked as deleted.', 'wordpress-seo' ),
  156. '<strong>' . esc_html( sanitize_text_field( $dirty[ $key ] ) ) . '</strong>'
  157. ), // The error message.
  158. 'error' // Error type, either 'error' or 'updated'.
  159. );
  160. }
  161. unset( $exists );
  162. }
  163. elseif ( function_exists( 'add_settings_error' ) ) {
  164. add_settings_error(
  165. $this->group_name, // Slug title of the setting.
  166. '_' . $key, // Suffix-id for the error message box.
  167. esc_html__( 'The default blog setting must be the numeric blog id of the blog you want to use as default.', 'wordpress-seo' ) . '<br>' . esc_html__( 'No numeric value was received.', 'wordpress-seo' ), // The error message.
  168. 'error' // Error type, either 'error' or 'updated'.
  169. );
  170. }
  171. unset( $int );
  172. }
  173. break;
  174. default:
  175. $clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : false );
  176. break;
  177. }
  178. }
  179. return $clean;
  180. }
  181. /**
  182. * Clean a given option value
  183. *
  184. * @param array $option_value Old (not merged with defaults or filtered) option value to
  185. * clean according to the rules for this option.
  186. * @param string $current_version (optional) Version from which to upgrade, if not set,
  187. * version specific upgrades will be disregarded.
  188. * @param array $all_old_option_values (optional) Only used when importing old options to have
  189. * access to the real old values, in contrast to the saved ones.
  190. *
  191. * @return array Cleaned option
  192. */
  193. /*
  194. Protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) {
  195. return $option_value;
  196. }
  197. */
  198. }