PluginProbe
Block Manager / 1.0.1
Block Manager v1.0.1
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 / bulk_process.php

bulk_process.php in Block Manager 1.0.1, at api/bulk_process.php

108 lines 2.6 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 = '/bulk_process';
13 register_rest_route( $my_namespace, $my_endpoint,
14 array(
15 'methods' => 'POST',
16 'callback' => 'block_manager_bulk_process',
17 'permission_callback' => function () {
18 return Gutenberg_Block_Manager::has_access();
19 },
20 )
21 );
22 });
23
24
25
26
27 /*
28 * block_manager_toggle
29 * Enable/Disable gutenberg blocks
30 *
31 * @param $request $_POST
32 * @return $response json
33 * @since 1.0
34 */
35
36 function block_manager_bulk_process( WP_REST_Request $request ) {
37
38 if (is_user_logged_in() && current_user_can( apply_filters( 'block_manager_user_role', 'activate_plugins' ) )){
39
40 error_reporting(E_ALL|E_STRICT);
41
42 // Get JSON Data
43 $body = json_decode($request->get_body(), true); // Get contents of request body
44 $data = json_decode($body['data']); // Get contents of data
45
46 if($body && $data){
47
48 $blocks = ($data && $data->blocks) ? $data->blocks : ''; // block name
49 $type = ($data && $data->type) ? $data->type : 'enable'; // enable/disable
50
51 $disabled_blocks = (array)get_option(BLOCK_MANAGER_OPTION, array());
52
53 // Disable All
54 if($blocks && $type === 'disable'){
55
56 // Loop blocks, add new blocks to disabled array
57 foreach($blocks as $block){
58 if(!in_array($block, $disabled_blocks)){
59 $disabled_blocks[] = $block;
60 }
61 }
62
63 // Update option
64 update_option( BLOCK_MANAGER_OPTION, $disabled_blocks );
65
66 // Send response
67 $response = array(
68 'success' => true,
69 'msg' => __('All Blocks Disabled', 'block-manager'),
70 'disabled_blocks' => count(get_option( BLOCK_MANAGER_OPTION ))
71 );
72 }
73
74 // Enable All
75 if($blocks && $type === 'enable'){
76
77 $new_blocks = [];
78 // Loop blocks, create new array minus the blocks to enable
79 foreach($disabled_blocks as $block){
80 if(!in_array($block, $blocks)){
81 $new_blocks[] = $block;
82 }
83 }
84
85 // Update option
86 update_option( BLOCK_MANAGER_OPTION, $new_blocks );
87
88 // Send response
89 $response = array(
90 'success' => true,
91 'msg' => __('All Blocks Enabled', 'block-manager'),
92 'disabled_blocks' => count(get_option( BLOCK_MANAGER_OPTION ))
93 );
94 }
95
96 } else {
97 $response = array(
98 'success' => false,
99 'msg' => __('Error accessing API data.', 'block-manager'),
100 'disabled_blocks' => count(get_option( BLOCK_MANAGER_OPTION ))
101 );
102 }
103
104 wp_send_json($response); // Send response as JSON
105
106 }
107 }
108