PluginProbe
Qi Blocks / 1.0.7
Qi Blocks v1.0.7
1.5.3 1.5.2 1.5.1 trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 All 45 releases
qi-blocks / inc / rest / class-qi-blocks-rest-api.php

class-qi-blocks-rest-api.php in Qi Blocks 1.0.7, at inc/rest/class-qi-blocks-rest-api.php

72 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! class_exists( 'Qi_Blocks_Rest_API' ) ) {
4 /**
5 * Rest API class with configuration
6 */
7 class Qi_Blocks_Rest_API {
8 private static $instance;
9 private $namespace;
10
11 public function __construct() {
12 // Init variables
13 $this->set_namespace( 'qi-blocks/v1' );
14
15 // Localize main editor js script with additional variables
16 add_filter( 'qi_blocks_filter_localize_main_editor_js', array( $this, 'localize_script' ) );
17
18 // Function that register Rest API routes
19 add_action( 'rest_api_init', array( $this, 'register_rest_api_route' ) );
20 }
21
22 /**
23 * @return Qi_Blocks_Rest_API
24 */
25 public static function get_instance() {
26 if ( is_null( self::$instance ) ) {
27 self::$instance = new self();
28 }
29
30 return self::$instance;
31 }
32
33 public function get_namespace() {
34 return $this->namespace;
35 }
36
37 public function set_namespace( $namespace ) {
38 $this->namespace = $namespace;
39 }
40
41 function localize_script( $global ) {
42 $global['restUrl'] = esc_url_raw( rest_url() );
43 $global['restNonce'] = wp_create_nonce( 'wp_rest' );
44
45 return apply_filters( 'qi_blocks_filter_localize_main_editor_js_rest_api', $global, $this->get_namespace() );
46 }
47
48 function register_rest_api_route() {
49 $routes = apply_filters( 'qi_blocks_filter_rest_api_routes', array() );
50
51 if ( ! empty( $routes ) ) {
52 foreach ( $routes as $route ) {
53 $permission_callback = isset( $route['permission_callback'] ) && ! empty( $route['permission_callback'] ) ? $route['permission_callback'] : '__return_true';
54
55 register_rest_route(
56 $this->get_namespace(),
57 esc_attr( $route['route'] ),
58 array(
59 'methods' => $route['methods'],
60 'callback' => $route['callback'],
61 'permission_callback' => $permission_callback,
62 'args' => $route['args'] ?? array(),
63 )
64 );
65 }
66 }
67 }
68 }
69
70 Qi_Blocks_Rest_API::get_instance();
71 }
72