PluginProbe
Block Manager / 2.1.0
Block Manager v2.1.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 2.1.0, at api/bulk-process.php

102 lines 2.8 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
43 if ( $body ) {
44 $blocks_array = $body['blocks'] ? $body['blocks'] : []; // block array.
45 $type = $body['type'] ? $body['type'] : 'enable'; // enable/disable.
46
47 $disabled_blocks = Gutenberg_Block_Manager::gbm_get_disabled_blocks();
48 $filtered_blocks = Gutenberg_Block_Manager::gbm_get_filtered_blocks();
49
50 // Disable All.
51 if ( ! empty( $blocks_array ) && $type === 'disable' ) {
52 // Loop blocks, add new blocks to disabled block array.
53 // Only add if they are not being filtered via `gbm_disabled_blocks`.
54 foreach ( $blocks_array as $block ) {
55 if ( ! in_array( $block, $disabled_blocks, true ) && ! in_array( $block, $filtered_blocks, true ) ) {
56 $disabled_blocks[] = $block;
57 }
58 }
59
60 $disabled_blocks = array_values( $disabled_blocks );
61 update_option( BLOCK_MANAGER_OPTION, $disabled_blocks );
62 wp_send_json(
63 [
64 'success' => true,
65 'msg' => __( 'All blocks in category disabled', 'block-manager' ),
66 'disabled_blocks' => $disabled_blocks,
67 ]
68 );
69 }
70
71 // Enable All.
72 if ( ! empty( $blocks_array ) && $type === 'enable' ) {
73 $blocks = [];
74 // Loop blocks, create new array minus the blocks to enable.
75 foreach ( $disabled_blocks as $block ) {
76 if ( ! in_array( $block, $blocks_array, true ) && ! in_array( $block, $filtered_blocks, true ) ) {
77 $blocks[] = $block;
78 }
79 }
80
81 $blocks = array_values( $blocks );
82 update_option( BLOCK_MANAGER_OPTION, $blocks );
83 wp_send_json(
84 [
85 'success' => true,
86 'msg' => __( 'All blocks in category enabled', 'block-manager' ),
87 'disabled_blocks' => $blocks,
88 ]
89 );
90 }
91 } else {
92 wp_send_json(
93 [
94 'success' => false,
95 'msg' => __( 'Error accessing API data', 'block-manager' ),
96 'disabled_blocks' => [],
97 ]
98 );
99 }
100 }
101 }
102