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 +64 -45 1.2.5trunk 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,82 +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 - $data = json_decode( $body['data'] ); // Get contents of data.
43 39
44 - if ( $body && $data ) {
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.
45 44
46 - $blocks = ( $data && $data->blocks ) ? $data->blocks : ''; // block name.
47 - $type = ( $data && $data->type ) ? $data->type : 'enable'; // enable/disable.
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;
48 53
49 - $disabled_blocks = (array) get_option( BLOCK_MANAGER_OPTION, array() );
50 - $filtered_blocks = Gutenberg_Block_Manager::gbm_get_filtered_blocks();
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 + }
51 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 +
52 73 // Disable All.
53 - if ( $blocks && 'disable' === $type ) {
54 -
55 - // Loop blocks, add new blocks to disabled array.
74 + if ( ! empty( $blocks_array ) && $direction === 'disable' ) {
75 + // Loop blocks, add new blocks to disabled block array.
56 76 // 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;
77 + foreach ( $blocks_array as $block ) {
78 + if ( ! in_array( $block, $disabled, true ) && ! in_array( $block, $filtered, true ) ) {
79 + $disabled[] = $block;
60 80 }
61 81 }
62 82
63 - // Update option.
64 - update_option( BLOCK_MANAGER_OPTION, $disabled_blocks );
83 + $disabled = array_values( $disabled );
84 + update_option( $option, $disabled );
65 85
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 ) ),
86 + wp_send_json(
87 + [
88 + 'success' => true,
89 + 'msg' => $disabled_msg,
90 + 'disabled' => $disabled,
91 + ]
71 92 );
72 93 }
73 94
74 95 // Enable All.
75 - if ( $blocks && 'enable' === $type ) {
76 -
77 - $new_blocks = [];
96 + if ( ! empty( $blocks_array ) && $direction === 'enable' ) {
97 + $blocks = [];
78 98 // 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;
99 + foreach ( $disabled as $block ) {
100 + if ( ! in_array( $block, $blocks_array, true ) && ! in_array( $block, $filtered, true ) ) {
101 + $blocks[] = $block;
82 102 }
83 103 }
84 104
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 ) ),
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 + ]
93 113 );
94 114 }
95 115 } else {
96 - $response = array(
97 - 'success' => false,
98 - 'msg' => __( 'Error accessing API data.', 'block-manager' ),
99 - 'disabled_blocks' => count( get_option( BLOCK_MANAGER_OPTION ) ),
116 + wp_send_json(
117 + [
118 + 'success' => false,
119 + 'msg' => __( 'Error accessing API data', 'block-manager' ),
120 + 'disabled' => [],
121 + ]
100 122 );
101 123 }
102 -
103 - wp_send_json( $response ); // Send response as JSON.
104 -
105 124 }
106 125 }