PluginProbe
Block Manager / trunk
Block Manager vtrunk
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
← All changes | api/bulk-process.php +63 -40 2.0.0trunk View file →
@@ -1,7 +1,7 @@
1 1 <?php
2 2 /**
3 - * API Route: Bulk updater of block statuses.
3 + * API Route: Bulk updater of blocks and patterns.
4 4 *
5 5 * @since 1.0
6 6 * @package blockmanager
7 7 */
@@ -25,78 +25,101 @@
25 25 }
26 26 );
27 27
28 28 /**
29 - * Bulk Enable/Disable gutenberg blocks.
29 + * Bulk Enable/Disable block patterns.
30 30 *
31 31 * @param WP_REST_Request $request The content of the HTTP request.
32 32 * @since 1.0
33 33 */
34 34 function block_manager_bulk_process( WP_REST_Request $request ) {
35 -
36 35 if ( is_user_logged_in() && current_user_can( apply_filters( 'block_manager_user_role', 'activate_plugins' ) ) ) {
37 -
38 36 error_reporting( E_ALL | E_STRICT ); // @codingStandardsIgnoreLine
39 37
40 - // Get JSON Data.
41 38 $body = json_decode( $request->get_body(), true ); // Get contents of request body.
42 39
43 40 if ( $body ) {
44 - $blocks_array = $body['blocks'] ? $body['blocks'] : []; // block array.
45 - $type = $body['type'] ? $body['type'] : 'enable'; // enable/disable.
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.
46 44
47 - $disabled_blocks = Gutenberg_Block_Manager::gbm_get_disabled_blocks();
48 - $filtered_blocks = Gutenberg_Block_Manager::gbm_get_filtered_blocks();
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;
49 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 +
50 73 // Disable All.
51 - if ( ! empty( $blocks_array ) && $type === 'disable' ) {
74 + if ( ! empty( $blocks_array ) && $direction === 'disable' ) {
52 75 // Loop blocks, add new blocks to disabled block array.
53 76 // Only add if they are not being filtered via `gbm_disabled_blocks`.
54 77 foreach ( $blocks_array as $block ) {
55 - if ( ! in_array( $block, $disabled_blocks, true ) && ! in_array( $block, $filtered_blocks, true ) ) {
56 - $disabled_blocks[] = $block;
78 + if ( ! in_array( $block, $disabled, true ) && ! in_array( $block, $filtered, true ) ) {
79 + $disabled[] = $block;
57 80 }
58 81 }
59 82
60 - // Update option.
61 - update_option( BLOCK_MANAGER_OPTION, $disabled_blocks );
83 + $disabled = array_values( $disabled );
84 + update_option( $option, $disabled );
62 85
63 - // Send response.
64 - $response = [
65 - 'success' => true,
66 - 'msg' => __( 'All Blocks Disabled', 'block-manager' ),
67 - 'disabled_blocks' => $disabled_blocks,
68 - ];
86 + wp_send_json(
87 + [
88 + 'success' => true,
89 + 'msg' => $disabled_msg,
90 + 'disabled' => $disabled,
91 + ]
92 + );
69 93 }
70 94
71 95 // Enable All.
72 - if ( ! empty( $blocks_array ) && $type === 'enable' ) {
96 + if ( ! empty( $blocks_array ) && $direction === 'enable' ) {
73 97 $blocks = [];
74 98 // 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 ) ) {
99 + foreach ( $disabled as $block ) {
100 + if ( ! in_array( $block, $blocks_array, true ) && ! in_array( $block, $filtered, true ) ) {
77 101 $blocks[] = $block;
78 102 }
79 103 }
80 104
81 - // Update option.
82 - update_option( BLOCK_MANAGER_OPTION, $blocks );
83 -
84 - // Send response.
85 - $response = [
86 - 'success' => true,
87 - 'msg' => __( 'All Blocks Enabled', 'block-manager' ),
88 - 'disabled_blocks' => $blocks,
89 - ];
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 + );
90 114 }
91 115 } else {
92 - $response = [
93 - 'success' => false,
94 - 'msg' => __( 'Error accessing API data.', 'block-manager' ),
95 - 'disabled_blocks' => [],
96 - ];
116 + wp_send_json(
117 + [
118 + 'success' => false,
119 + 'msg' => __( 'Error accessing API data', 'block-manager' ),
120 + 'disabled' => [],
121 + ]
122 + );
97 123 }
98 -
99 - wp_send_json( $response ); // Send response as JSON.
100 -
101 124 }
102 125 }