PluginProbe
Gutenberg / 13.7.2
Gutenberg v13.7.2
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 7.4.0 All 402 releases
gutenberg / lib / experimental / class-wp-rest-customizer-nonces.php

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

73 lines 2.0 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 * @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