| 1 |
<?php |
| 2 |
/** |
| 3 |
* WP_Rest_Customizer_Nonces class. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Class that returns the customizer "save" nonce that's required for the |
| 10 |
* batch save operation using the customizer API endpoint. |
| 11 |
*/ |
| 12 |
class WP_Rest_Customizer_Nonces extends WP_REST_Controller { |
| 13 |
|
| 14 |
/** |
| 15 |
* Constructor. |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
$this->namespace = '__experimental'; |
| 19 |
$this->rest_base = 'customizer-nonces'; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Registers the necessary REST API routes. |
| 24 |
* |
| 25 |
* @access public |
| 26 |
*/ |
| 27 |
public function register_routes() { |
| 28 |
register_rest_route( |
| 29 |
$this->namespace, |
| 30 |
'/' . $this->rest_base . '/get-save-nonce', |
| 31 |
array( |
| 32 |
array( |
| 33 |
'methods' => WP_REST_Server::READABLE, |
| 34 |
'callback' => array( $this, 'get_save_nonce' ), |
| 35 |
'permission_callback' => array( $this, 'permissions_check' ), |
| 36 |
'args' => $this->get_collection_params(), |
| 37 |
), |
| 38 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 39 |
) |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Checks if a given request has access to read menu items if they have access to edit them. |
| 45 |
* |
| 46 |
* @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
| 47 |
*/ |
| 48 |
public function permissions_check() { |
| 49 |
$post_type = get_post_type_object( 'nav_menu_item' ); |
| 50 |
if ( ! current_user_can( $post_type->cap->edit_posts ) ) { |
| 51 |
return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit posts in this post type.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) ); |
| 52 |
} |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns the nonce required to request the customizer API endpoint. |
| 58 |
* |
| 59 |
* @access public |
| 60 |
*/ |
| 61 |
public function get_save_nonce() { |
| 62 |
require_once ABSPATH . 'wp-includes/class-wp-customize-manager.php'; |
| 63 |
$wp_customize = new WP_Customize_Manager(); |
| 64 |
$nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() ); |
| 65 |
return array( |
| 66 |
'success' => true, |
| 67 |
'nonce' => $nonce, |
| 68 |
'stylesheet' => $wp_customize->get_stylesheet(), |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
} |
| 73 |
|