PluginProbe
Block Manager / 1.2.5
Block Manager v1.2.5
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.2.5, at api/bulk-process.php

107 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * API Route: Bulk updater of block statuses.
4 *
5 * @since 1.0
6 * @package blockmanager
7 */
8
9 add_action(
10 'rest_api_init',
11 function () {
12 $my_namespace = 'gbm';
13 $my_endpoint = '/bulk_process';
14 register_rest_route(
15 $my_namespace,
16 $my_endpoint,
17 array(
18 'methods' => 'POST',
19 'callback' => 'block_manager_bulk_process',
20 'permission_callback' => function () {
21 return Gutenberg_Block_Manager::has_access();
22 },
23 )
24 );
25 }
26 );
27
28 /**
29 * Bulk Enable/Disable gutenberg blocks.
30 *
31 * @param WP_REST_Request $request The content of the HTTP request.
32 * @since 1.0
33 */
34 function block_manager_bulk_process( WP_REST_Request $request ) {
35
36 if ( is_user_logged_in() && current_user_can( apply_filters( 'block_manager_user_role', 'activate_plugins' ) ) ) {
37
38 error_reporting( E_ALL | E_STRICT ); // @codingStandardsIgnoreLine
39
40 // Get JSON Data.
41 $body = json_decode( $request->get_body(), true ); // Get contents of request body.
42 $data = json_decode( $body['data'] ); // Get contents of data.
43
44 if ( $body && $data ) {
45
46 $blocks = ( $data && $data->blocks ) ? $data->blocks : ''; // block name.
47 $type = ( $data && $data->type ) ? $data->type : 'enable'; // enable/disable.
48
49 $disabled_blocks = (array) get_option( BLOCK_MANAGER_OPTION, array() );
50 $filtered_blocks = Gutenberg_Block_Manager::gbm_get_filtered_blocks();
51
52 // Disable All.
53 if ( $blocks && 'disable' === $type ) {
54
55 // Loop blocks, add new blocks to disabled array.
56 // Only add if they are not being filtered via `gbm_disabled_blocks`.
57 foreach ( $blocks as $block ) {
58 if ( ! in_array( $block, $disabled_blocks, true ) && ! in_array( $block, $filtered_blocks, true ) ) {
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 && 'enable' === $type ) {
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, true ) ) {
81 $new_blocks[] = $block;
82 }
83 }
84
85 // Update option.
86 update_option( BLOCK_MANAGER_OPTION, array_merge( $new_blocks, $filtered_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 } else {
96 $response = array(
97 'success' => false,
98 'msg' => __( 'Error accessing API data.', 'block-manager' ),
99 'disabled_blocks' => count( get_option( BLOCK_MANAGER_OPTION ) ),
100 );
101 }
102
103 wp_send_json( $response ); // Send response as JSON.
104
105 }
106 }
107