PluginProbe
Gutenberg / 8.5.1
Gutenberg v8.5.1
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / lib / class-wp-rest-customizer-nonces.php

class-wp-rest-customizer-nonces.php in Gutenberg 8.5.1, at lib/class-wp-rest-customizer-nonces.php

74 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @param WP_REST_Request $request Full details about the request.
47 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
48 */
49 public function permissions_check( $request ) {
50 $post_type = get_post_type_object( 'nav_menu_item' );
51 if ( ! current_user_can( $post_type->cap->edit_posts ) ) {
52 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() ) );
53 }
54 return true;
55 }
56
57 /**
58 * Returns the nonce required to request the customizer API endpoint.
59 *
60 * @access public
61 */
62 public function get_save_nonce() {
63 require_once ABSPATH . 'wp-includes/class-wp-customize-manager.php';
64 $wp_customize = new WP_Customize_Manager();
65 $nonce = wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() );
66 return array(
67 'success' => true,
68 'nonce' => $nonce,
69 'stylesheet' => $wp_customize->get_stylesheet(),
70 );
71 }
72
73 }
74