PluginProbe
Block Manager / 1.0
Block Manager v1.0
trunk 1.0 1.0.1 1.1 1.2 1.2.1 1.2.2 1.2.3c 1.2.4 1.2.5 2.0.0 2.1.0 2.1.0.1 2.1.1 3.0.0 3.1.0 3.1.1 3.1.2 3.2.0 3.2.1
block-manager / api / toggle.php

toggle.php in Block Manager 1.0, at api/toggle.php

88 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * rest_api_init
5 * Custom /resize route
6 *
7 * @since 1.0
8 */
9
10 add_action( 'rest_api_init', function () {
11 $my_namespace = 'gbm';
12 $my_endpoint = '/toggle';
13 register_rest_route( $my_namespace, $my_endpoint,
14 array(
15 'methods' => 'POST',
16 'callback' => 'block_manager_toggle',
17 )
18 );
19 });
20
21
22
23
24 /*
25 * block_manager_toggle
26 * Enable/Disable gutenberg blocks
27 *
28 * @param $request $_POST
29 * @return $response json
30 * @since 1.0
31 */
32
33 function block_manager_toggle( WP_REST_Request $request ) {
34
35 if (is_user_logged_in() && current_user_can( apply_filters( 'block_manager_user_role', 'activate_plugins' ) )){
36
37 error_reporting(E_ALL|E_STRICT);
38
39 // Get JSON Data
40 $body = json_decode($request->get_body(), true); // Get contents of request body
41 $data = json_decode($body['data']); // Get contents of data
42
43 if($body && $data){
44
45 $block = ($data && $data->block) ? $data->block : ''; // block name
46 $type = ($data && $data->type) ? $data->type : 'enable'; // enable/disable
47 $blocks = (array)get_option(BLOCK_MANAGER_OPTION, array()); // all currently disabled blocks
48
49 // Disable
50 if($block && $type === 'disable'){
51 if ( ! in_array( $block, $blocks, true ) ) {
52 $blocks[] = $block;
53 }
54 update_option( BLOCK_MANAGER_OPTION, $blocks );
55 $response = array(
56 'success' => true,
57 'msg' => __('Block Disabled', 'block-manager'),
58 'disabled_blocks' => count(get_option( BLOCK_MANAGER_OPTION ))
59 );
60 }
61
62 // Enable
63 if($block && $type === 'enable'){
64 $new_blocks = array();
65 if ( in_array( $block, $blocks, true ) ) {
66 $new_blocks = array_diff( $blocks, array( $block ) );
67 }
68 update_option( BLOCK_MANAGER_OPTION, $new_blocks );
69 $response = array(
70 'success' => true,
71 'msg' => __('Block enabled', 'block-manager'),
72 'disabled_blocks' => count(get_option( BLOCK_MANAGER_OPTION ))
73 );
74 }
75
76 } else {
77 $response = array(
78 'success' => false,
79 'msg' => __('Error accessing API data.', 'block-manager'),
80 'disabled_blocks' => count(get_option( BLOCK_MANAGER_OPTION ))
81 );
82 }
83
84 wp_send_json($response); // Send response as JSON
85
86 }
87 }
88