PluginProbe
Block Manager / 3.1.2
Block Manager v3.1.2
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 3.1.2, at api/bulk-process.php

126 lines 3.4 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 blocks and patterns.
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 block patterns.
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 if ( is_user_logged_in() && current_user_can( apply_filters( 'block_manager_user_role', 'activate_plugins' ) ) ) {
36 error_reporting( E_ALL | E_STRICT ); // @codingStandardsIgnoreLine
37
38 $body = json_decode( $request->get_body(), true ); // Get contents of request body.
39
40 if ( $body ) {
41 $blocks_array = $body['blocks'] ? $body['blocks'] : []; // Item array.
42 $type = $body['type'] ? sanitize_text_field( $body['type'] ) : 'blocks';
43 $direction = $body['direction'] ? sanitize_text_field( $body['direction'] ) : 'enable'; // enable/disable.
44
45 switch( $type ) {
46 case 'blocks' :
47 $disabled = GBM_Blocks::gbm_get_disabled_blocks();
48 $filtered = GBM_Blocks::gbm_get_filtered_blocks();
49 $disabled_msg = __( 'All blocks in category disabled', 'block-manager' );
50 $enabled_msg = __( 'All blocks in category enabled', 'block-manager' );
51 $option = BLOCK_MANAGER_OPTION;
52 break;
53
54 case 'patterns' :
55 $disabled = GBM_Patterns::gbm_get_disabled_patterns();
56 $filtered = GBM_Patterns::gbm_get_filtered_patterns();
57 $disabled_msg = __( 'All patterns in category disabled', 'block-manager' );
58 $enabled_msg = __( 'All patterns in category enabled', 'block-manager' );
59 $option = BLOCK_MANAGER_PATTERNS;
60 break;
61 }
62
63 if ( ! $option ) {
64 wp_send_json(
65 [
66 'success' => false,
67 'msg' => __( 'Error accessing data', 'block-manager' ),
68 'disabled' => [],
69 ]
70 );
71 }
72
73 // Disable All.
74 if ( ! empty( $blocks_array ) && $direction === 'disable' ) {
75 // Loop blocks, add new blocks to disabled block array.
76 // Only add if they are not being filtered via `gbm_disabled_blocks`.
77 foreach ( $blocks_array as $block ) {
78 if ( ! in_array( $block, $disabled, true ) && ! in_array( $block, $filtered, true ) ) {
79 $disabled[] = $block;
80 }
81 }
82
83 $disabled = array_values( $disabled );
84 update_option( $option, $disabled );
85
86 wp_send_json(
87 [
88 'success' => true,
89 'msg' => $disabled_msg,
90 'disabled' => $disabled,
91 ]
92 );
93 }
94
95 // Enable All.
96 if ( ! empty( $blocks_array ) && $direction === 'enable' ) {
97 $blocks = [];
98 // Loop blocks, create new array minus the blocks to enable.
99 foreach ( $disabled as $block ) {
100 if ( ! in_array( $block, $blocks_array, true ) && ! in_array( $block, $filtered, true ) ) {
101 $blocks[] = $block;
102 }
103 }
104
105 $blocks = array_values( $blocks );
106 update_option( $option, $blocks );
107 wp_send_json(
108 [
109 'success' => true,
110 'msg' => $enabled_msg,
111 'disabled' => $blocks,
112 ]
113 );
114 }
115 } else {
116 wp_send_json(
117 [
118 'success' => false,
119 'msg' => __( 'Error accessing API data', 'block-manager' ),
120 'disabled' => [],
121 ]
122 );
123 }
124 }
125 }
126