class-wpseo-option-ms.php 7.8 KB

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