| 1 |
<?php |
| 2 |
/** |
| 3 |
* Custom API route. |
| 4 |
* |
| 5 |
* @since 1.0 |
| 6 |
*/ |
| 7 |
add_action( 'rest_api_init', function () { |
| 8 |
$my_namespace = 'gbm'; |
| 9 |
$my_endpoint = '/bulk_process'; |
| 10 |
register_rest_route( |
| 11 |
$my_namespace, |
| 12 |
$my_endpoint, |
| 13 |
array( |
| 14 |
'methods' => 'POST', |
| 15 |
'callback' => 'block_manager_bulk_process', |
| 16 |
'permission_callback' => function () { |
| 17 |
return Gutenberg_Block_Manager::has_access(); |
| 18 |
}, |
| 19 |
) |
| 20 |
); |
| 21 |
}); |
| 22 |
|
| 23 |
/** |
| 24 |
* Bulk Enable/Disable gutenberg blocks. |
| 25 |
* |
| 26 |
* @param $request $_POST |
| 27 |
* @return $response json |
| 28 |
* @since 1.0 |
| 29 |
*/ |
| 30 |
function block_manager_bulk_process( WP_REST_Request $request ) { |
| 31 |
|
| 32 |
if ( is_user_logged_in() && current_user_can( apply_filters( 'block_manager_user_role', 'activate_plugins' ) ) ) { |
| 33 |
|
| 34 |
error_reporting( E_ALL | E_STRICT ); |
| 35 |
|
| 36 |
// Get JSON Data. |
| 37 |
$body = json_decode( $request->get_body(), true ); // Get contents of request body. |
| 38 |
$data = json_decode( $body['data'] ); // Get contents of data. |
| 39 |
|
| 40 |
if ( $body && $data ) { |
| 41 |
|
| 42 |
$blocks = ( $data && $data->blocks ) ? $data->blocks : ''; // block name. |
| 43 |
$type = ( $data && $data->type ) ? $data->type : 'enable'; // enable/disable. |
| 44 |
|
| 45 |
$disabled_blocks = (array) get_option( BLOCK_MANAGER_OPTION, array() ); |
| 46 |
$filtered_blocks = Gutenberg_Block_Manager::gbm_get_filtered_blocks(); |
| 47 |
|
| 48 |
// Disable All. |
| 49 |
if ( $blocks && 'disable' === $type ) { |
| 50 |
|
| 51 |
// Loop blocks, add new blocks to disabled array. |
| 52 |
// Only add if they are not being filtered via `gbm_disabled_blocks`. |
| 53 |
foreach ( $blocks as $block ) { |
| 54 |
if ( ! in_array( $block, $disabled_blocks ) && ! in_array( $block, $filtered_blocks ) ) { |
| 55 |
$disabled_blocks[] = $block; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// Update option. |
| 60 |
update_option( BLOCK_MANAGER_OPTION, $disabled_blocks ); |
| 61 |
|
| 62 |
// Send response. |
| 63 |
$response = array( |
| 64 |
'success' => true, |
| 65 |
'msg' => __('All Blocks Disabled', 'block-manager'), |
| 66 |
'disabled_blocks' => count( get_option( BLOCK_MANAGER_OPTION ) ), |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
// Enable All. |
| 71 |
if ( $blocks && 'enable' === $type ) { |
| 72 |
|
| 73 |
$new_blocks = []; |
| 74 |
// Loop blocks, create new array minus the blocks to enable. |
| 75 |
foreach ( $disabled_blocks as $block ) { |
| 76 |
if ( ! in_array( $block, $blocks ) ) { |
| 77 |
$new_blocks[] = $block; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// Update option. |
| 82 |
update_option( BLOCK_MANAGER_OPTION, array_merge( $new_blocks, $filtered_blocks ) ); |
| 83 |
|
| 84 |
// Send response. |
| 85 |
$response = array( |
| 86 |
'success' => true, |
| 87 |
'msg' => __( 'All Blocks Enabled', 'block-manager' ), |
| 88 |
'disabled_blocks' => count( get_option( BLOCK_MANAGER_OPTION ) ), |
| 89 |
); |
| 90 |
} |
| 91 |
} else { |
| 92 |
$response = array( |
| 93 |
'success' => false, |
| 94 |
'msg' => __( 'Error accessing API data.', 'block-manager' ), |
| 95 |
'disabled_blocks' => count( get_option( BLOCK_MANAGER_OPTION ) ), |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
wp_send_json( $response ); // Send response as JSON. |
| 100 |
|
| 101 |
} |
| 102 |
} |
| 103 |
|