class-yoast-network-admin.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Internals
  6. */
  7. /**
  8. * Multisite utility class for network admin functionality.
  9. */
  10. class Yoast_Network_Admin implements WPSEO_WordPress_Integration, WPSEO_WordPress_AJAX_Integration {
  11. /**
  12. * Action identifier for updating plugin network options.
  13. */
  14. const UPDATE_OPTIONS_ACTION = 'yoast_handle_network_options';
  15. /**
  16. * Action identifier for restoring a site.
  17. */
  18. const RESTORE_SITE_ACTION = 'yoast_restore_site';
  19. /**
  20. * Gets the available sites as choices, e.g. for a dropdown.
  21. *
  22. * @param bool $include_empty Optional. Whether to include an initial placeholder choice.
  23. * Default false.
  24. * @param bool $show_title Optional. Whether to show the title for each site. This requires
  25. * switching through the sites, so has performance implications for
  26. * sites that do not use a persistent cache.
  27. * Default false.
  28. *
  29. * @return array Choices as $site_id => $site_label pairs.
  30. */
  31. public function get_site_choices( $include_empty = false, $show_title = false ) {
  32. $choices = array();
  33. if ( $include_empty ) {
  34. $choices['-'] = __( 'None', 'wordpress-seo' );
  35. }
  36. $sites = get_sites( array(
  37. 'deleted' => 0,
  38. 'network_id' => get_current_network_id(),
  39. ) );
  40. foreach ( $sites as $site ) {
  41. $site_name = $site->domain . $site->path;
  42. if ( $show_title ) {
  43. $site_name = $site->blogname . ' (' . $site->domain . $site->path . ')';
  44. }
  45. $choices[ $site->blog_id ] = $site->blog_id . ': ' . $site_name;
  46. $site_states = $this->get_site_states( $site );
  47. if ( ! empty( $site_states ) ) {
  48. $choices[ $site->blog_id ] .= ' [' . implode( ', ', $site_states ) . ']';
  49. }
  50. }
  51. return $choices;
  52. }
  53. /**
  54. * Gets the states of a site.
  55. *
  56. * @param WP_Site $site Site object.
  57. *
  58. * @return array Array of $state_slug => $state_label pairs.
  59. */
  60. public function get_site_states( $site ) {
  61. $available_states = array(
  62. 'public' => __( 'public', 'wordpress-seo' ),
  63. 'archived' => __( 'archived', 'wordpress-seo' ),
  64. 'mature' => __( 'mature', 'wordpress-seo' ),
  65. 'spam' => __( 'spam', 'wordpress-seo' ),
  66. 'deleted' => __( 'deleted', 'wordpress-seo' ),
  67. );
  68. $site_states = array();
  69. foreach ( $available_states as $state_slug => $state_label ) {
  70. if ( $site->$state_slug === '1' ) {
  71. $site_states[ $state_slug ] = $state_label;
  72. }
  73. }
  74. return $site_states;
  75. }
  76. /**
  77. * Handles a request to update plugin network options.
  78. *
  79. * This method works similar to how option updates are handled in `wp-admin/options.php` and
  80. * `wp-admin/network/settings.php`.
  81. *
  82. * @return void
  83. */
  84. public function handle_update_options_request() {
  85. $option_group = filter_input( INPUT_POST, 'network_option_group', FILTER_SANITIZE_STRING );
  86. $this->verify_request( "{$option_group}-network-options" );
  87. $whitelist_options = Yoast_Network_Settings_API::get()->get_whitelist_options( $option_group );
  88. if ( empty( $whitelist_options ) ) {
  89. add_settings_error( $option_group, 'settings_updated', __( 'You are not allowed to modify unregistered network settings.', 'wordpress-seo' ), 'error' );
  90. $this->terminate_request();
  91. return;
  92. }
  93. foreach ( $whitelist_options as $option_name ) {
  94. $value = null;
  95. if ( isset( $_POST[ $option_name ] ) ) { // WPCS: CSRF ok.
  96. $value = wp_unslash( $_POST[ $option_name ] ); // WPCS: CSRF ok.
  97. }
  98. WPSEO_Options::update_site_option( $option_name, $value );
  99. }
  100. $settings_errors = get_settings_errors();
  101. if ( empty( $settings_errors ) ) {
  102. add_settings_error( $option_group, 'settings_updated', __( 'Settings Updated.', 'wordpress-seo' ), 'updated' );
  103. }
  104. $this->terminate_request();
  105. }
  106. /**
  107. * Handles a request to restore a site's default settings.
  108. *
  109. * @return void
  110. */
  111. public function handle_restore_site_request() {
  112. $this->verify_request( 'wpseo-network-restore', 'restore_site_nonce' );
  113. $option_group = 'wpseo_ms';
  114. $site_id = ! empty( $_POST[ $option_group ]['site_id'] ) ? (int) $_POST[ $option_group ]['site_id'] : 0; // WPCS: CSRF ok.
  115. if ( ! $site_id ) {
  116. add_settings_error( $option_group, 'settings_updated', __( 'No site has been selected to restore.', 'wordpress-seo' ), 'error' );
  117. $this->terminate_request();
  118. return;
  119. }
  120. $site = get_site( $site_id );
  121. if ( ! $site ) {
  122. /* translators: %s expands to the ID of a site within a multisite network. */
  123. add_settings_error( $option_group, 'settings_updated', sprintf( __( 'Site with ID %d not found.', 'wordpress-seo' ), $site_id ), 'error' );
  124. }
  125. else {
  126. WPSEO_Options::reset_ms_blog( $site_id );
  127. /* translators: %s expands to the name of a site within a multisite network. */
  128. add_settings_error( $option_group, 'settings_updated', sprintf( __( '%s restored to default SEO settings.', 'wordpress-seo' ), esc_html( $site->blogname ) ), 'updated' );
  129. }
  130. $this->terminate_request();
  131. }
  132. /**
  133. * Outputs nonce, action and option group fields for a network settings page in the plugin.
  134. *
  135. * @param string $option_group Option group name for the current page.
  136. *
  137. * @return void
  138. */
  139. public function settings_fields( $option_group ) {
  140. ?>
  141. <input type="hidden" name="network_option_group" value="<?php echo esc_attr( $option_group ); ?>" />
  142. <input type="hidden" name="action" value="<?php echo esc_attr( self::UPDATE_OPTIONS_ACTION ); ?>" />
  143. <?php
  144. wp_nonce_field( "$option_group-network-options" );
  145. }
  146. /**
  147. * Enqueues network admin assets.
  148. *
  149. * @return void
  150. */
  151. public function enqueue_assets() {
  152. $asset_manager = new WPSEO_Admin_Asset_Manager();
  153. $asset_manager->enqueue_script( 'network-admin-script' );
  154. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'network-admin-script', 'wpseoNetworkAdminGlobalL10n', array(
  155. /* translators: %s: success message */
  156. 'success_prefix' => __( 'Success: %s', 'wordpress-seo' ),
  157. /* translators: %s: error message */
  158. 'error_prefix' => __( 'Error: %s', 'wordpress-seo' ),
  159. ) );
  160. }
  161. /**
  162. * Hooks in the necessary actions and filters.
  163. *
  164. * @return void
  165. */
  166. public function register_hooks() {
  167. if ( ! $this->meets_requirements() ) {
  168. return;
  169. }
  170. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
  171. add_action( 'admin_action_' . self::UPDATE_OPTIONS_ACTION, array( $this, 'handle_update_options_request' ) );
  172. add_action( 'admin_action_' . self::RESTORE_SITE_ACTION, array( $this, 'handle_restore_site_request' ) );
  173. }
  174. /**
  175. * Hooks in the necessary AJAX actions.
  176. *
  177. * @return void
  178. */
  179. public function register_ajax_hooks() {
  180. add_action( 'wp_ajax_' . self::UPDATE_OPTIONS_ACTION, array( $this, 'handle_update_options_request' ) );
  181. add_action( 'wp_ajax_' . self::RESTORE_SITE_ACTION, array( $this, 'handle_restore_site_request' ) );
  182. }
  183. /**
  184. * Checks whether the requirements to use this class are met.
  185. *
  186. * @return bool True if requirements are met, false otherwise.
  187. */
  188. public function meets_requirements() {
  189. return is_multisite() && is_network_admin();
  190. }
  191. /**
  192. * Verifies that the current request is valid.
  193. *
  194. * @param string $action Nonce action.
  195. * @param string $query_arg Optional. Nonce query argument. Default '_wpnonce'.
  196. *
  197. * @return void
  198. */
  199. public function verify_request( $action, $query_arg = '_wpnonce' ) {
  200. $has_access = current_user_can( 'wpseo_manage_network_options' );
  201. if ( wp_doing_ajax() ) {
  202. check_ajax_referer( $action, $query_arg );
  203. if ( ! $has_access ) {
  204. wp_die( -1, 403 );
  205. }
  206. return;
  207. }
  208. check_admin_referer( $action, $query_arg );
  209. if ( ! $has_access ) {
  210. wp_die( __( 'You are not allowed to perform this action.', 'wordpress-seo' ) );
  211. }
  212. }
  213. /**
  214. * Terminates the current request by either redirecting back or sending an AJAX response.
  215. *
  216. * @return void
  217. */
  218. public function terminate_request() {
  219. if ( wp_doing_ajax() ) {
  220. $settings_errors = get_settings_errors();
  221. if ( ! empty( $settings_errors ) && $settings_errors[0]['type'] === 'updated' ) {
  222. wp_send_json_success( $settings_errors, 200 );
  223. }
  224. wp_send_json_error( $settings_errors, 400 );
  225. }
  226. $this->persist_settings_errors();
  227. $this->redirect_back( array( 'settings-updated' => 'true' ) );
  228. }
  229. /**
  230. * Persists settings errors.
  231. *
  232. * Settings errors are stored in a transient for 30 seconds so that this transient
  233. * can be retrieved on the next page load.
  234. *
  235. * @return void
  236. */
  237. protected function persist_settings_errors() {
  238. /*
  239. * A regular transient is used here, since it is automatically cleared right after the redirect.
  240. * A network transient would be cleaner, but would require a lot of copied code from core for
  241. * just a minor adjustment when displaying settings errors.
  242. */
  243. set_transient( 'settings_errors', get_settings_errors(), 30 );
  244. }
  245. /**
  246. * Redirects back to the referer URL, with optional query arguments.
  247. *
  248. * @param array $query_args Optional. Query arguments to add to the redirect URL. Default none.
  249. *
  250. * @return void
  251. */
  252. protected function redirect_back( $query_args = array() ) {
  253. $sendback = wp_get_referer();
  254. if ( ! empty( $query_args ) ) {
  255. $sendback = add_query_arg( $query_args, $sendback );
  256. }
  257. wp_safe_redirect( $sendback );
  258. exit;
  259. }
  260. }