| 1 |
<?php |
| 2 |
/** |
| 3 |
* WordPress 7.2 compatibility functions for the Gutenberg |
| 4 |
* editor plugin changes related to REST API. |
| 5 |
* |
| 6 |
* @package gutenberg |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Registers the View Config REST API routes. |
| 11 |
* |
| 12 |
* Replaces the 7.1 registration so the route is served by the 7.2 controller. |
| 13 |
*/ |
| 14 |
function gutenberg_register_view_config_controller_endpoints_7_2() { |
| 15 |
$view_config_controller = new Gutenberg_REST_View_Config_Controller_7_2(); |
| 16 |
$view_config_controller->register_routes(); |
| 17 |
} |
| 18 |
remove_action( 'rest_api_init', 'gutenberg_register_view_config_controller_endpoints', PHP_INT_MAX ); |
| 19 |
add_action( 'rest_api_init', 'gutenberg_register_view_config_controller_endpoints_7_2', PHP_INT_MAX ); |
| 20 |
|
| 21 |
/** |
| 22 |
* Registers the Templates and Template Parts REST API routes. |
| 23 |
* |
| 24 |
* Replaces the core controller class so the 7.2 controller is used. |
| 25 |
* |
| 26 |
* @see Gutenberg_REST_Templates_Controller_7_2 |
| 27 |
* |
| 28 |
* @param array $args Array of arguments for registering a post type. |
| 29 |
* @return array Modified array of arguments. |
| 30 |
*/ |
| 31 |
function gutenberg_modify_template_post_type_args_7_2( $args ) { |
| 32 |
$args['rest_controller_class'] = 'Gutenberg_REST_Templates_Controller_7_2'; |
| 33 |
return $args; |
| 34 |
} |
| 35 |
add_filter( 'register_wp_template_post_type_args', 'gutenberg_modify_template_post_type_args_7_2' ); |
| 36 |
add_filter( 'register_wp_template_part_post_type_args', 'gutenberg_modify_template_post_type_args_7_2' ); |
| 37 |
|
| 38 |
/** |
| 39 |
* Exposes the privacy policy page setting in the REST API. |
| 40 |
* |
| 41 |
* WordPress stores the page assigned in Settings > Privacy in the |
| 42 |
* `wp_page_for_privacy_policy` option, but does not register it with |
| 43 |
* `show_in_rest`, so the settings endpoint cannot report it. Registering it |
| 44 |
* lets the editor label the privacy policy page, alongside the homepage and |
| 45 |
* the posts page. |
| 46 |
* |
| 47 |
* Runs on `rest_api_init` after `register_initial_settings`, and skips |
| 48 |
* registration when WordPress Core already exposes the option. |
| 49 |
*/ |
| 50 |
function gutenberg_register_privacy_policy_page_setting() { |
| 51 |
$registered = get_registered_settings(); |
| 52 |
if ( |
| 53 |
isset( $registered['wp_page_for_privacy_policy'] ) && |
| 54 |
! empty( $registered['wp_page_for_privacy_policy']['show_in_rest'] ) |
| 55 |
) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
register_setting( |
| 60 |
'reading', |
| 61 |
'wp_page_for_privacy_policy', |
| 62 |
array( |
| 63 |
'show_in_rest' => array( |
| 64 |
'name' => 'page_for_privacy_policy', |
| 65 |
), |
| 66 |
'type' => 'integer', |
| 67 |
'description' => __( 'The ID of the page that should be displayed as the privacy policy page', 'gutenberg' ), |
| 68 |
'default' => 0, |
| 69 |
) |
| 70 |
); |
| 71 |
} |
| 72 |
add_action( 'rest_api_init', 'gutenberg_register_privacy_policy_page_setting', 11 ); |
| 73 |
|
| 74 |
/** |
| 75 |
* Prevents users without the `manage_privacy_options` capability from |
| 76 |
* changing the privacy policy page through the REST API. |
| 77 |
* |
| 78 |
* The settings endpoint only checks `manage_options`. On multisite the |
| 79 |
* `manage_privacy_options` capability maps to `manage_network`, so a site |
| 80 |
* administrator can read the setting but must not change it, matching the |
| 81 |
* Settings > Privacy screen. |
| 82 |
* |
| 83 |
* @param bool $updated Whether the setting update has already been handled. |
| 84 |
* @param string $name Setting name (as shown in REST API responses). |
| 85 |
* @return bool Whether to short-circuit the update. |
| 86 |
*/ |
| 87 |
function gutenberg_restrict_privacy_policy_page_setting_update( $updated, $name ) { |
| 88 |
if ( 'page_for_privacy_policy' === $name && ! current_user_can( 'manage_privacy_options' ) ) { |
| 89 |
return true; |
| 90 |
} |
| 91 |
return $updated; |
| 92 |
} |
| 93 |
add_filter( 'rest_pre_update_setting', 'gutenberg_restrict_privacy_policy_page_setting_update', 10, 2 ); |
| 94 |
|