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 / bulk_process.php

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

105 lines 2.7 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 )
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_bulk_process( 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 $blocks = ($data && $data->blocks) ? $data->blocks : ''; // block name
46 $type = ($data && $data->type) ? $data->type : 'enable'; // enable/disable
47
48 $disabled_blocks = (array)get_option(BLOCK_MANAGER_OPTION, array());
49
50 // Disable All
51 if($blocks && $type === 'disable'){
52
53 // Loop blocks, add new blocks to disabled array
54 foreach($blocks as $block){
55 if(!in_array($block, $disabled_blocks)){
56 $disabled_blocks[] = $block;
57 }
58 }
59
60 // Update option
61 update_option( BLOCK_MANAGER_OPTION, $disabled_blocks );
62
63 // Send response
64 $response = array(
65 'success' => true,
66 'msg' => __('All Blocks Disabled', 'block-manager'),
67 'disabled_blocks' => count(get_option( BLOCK_MANAGER_OPTION ))
68 );
69 }
70
71 // Enable All
72 if($blocks && $type === 'enable'){
73
74 $new_blocks = [];
75 // Loop blocks, create new array minus the blocks to enable
76 foreach($disabled_blocks as $block){
77 if(!in_array($block, $blocks)){
78 $new_blocks[] = $block;
79 }
80 }
81
82 // Update option
83 update_option( BLOCK_MANAGER_OPTION, $new_blocks );
84
85 // Send response
86 $response = array(
87 'success' => true,
88 'msg' => __('All Blocks Enabled', 'block-manager'),
89 'disabled_blocks' => count(get_option( BLOCK_MANAGER_OPTION ))
90 );
91 }
92
93 } else {
94 $response = array(
95 'success' => false,
96 'msg' => __('Error accessing API data.', 'block-manager'),
97 'disabled_blocks' => count(get_option( BLOCK_MANAGER_OPTION ))
98 );
99 }
100
101 wp_send_json($response); // Send response as JSON
102
103 }
104 }
105