| 1 |
<?php |
| 2 |
/** |
| 3 |
* API Route: Toggle the active state of blocks. |
| 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 = '/toggle'; |
| 14 |
register_rest_route( |
| 15 |
$my_namespace, |
| 16 |
$my_endpoint, |
| 17 |
array( |
| 18 |
'methods' => 'POST', |
| 19 |
'callback' => 'block_manager_toggle', |
| 20 |
'permission_callback' => function () { |
| 21 |
return Gutenberg_Block_Manager::has_access(); |
| 22 |
}, |
| 23 |
) |
| 24 |
); |
| 25 |
} |
| 26 |
); |
| 27 |
|
| 28 |
/** |
| 29 |
* Enable/Disable individual gutenberg blocks. |
| 30 |
* |
| 31 |
* @param WP_REST_Request $request The API request data. |
| 32 |
* @since 1.0 |
| 33 |
*/ |
| 34 |
function block_manager_toggle( 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 |
// Get JSON Data. |
| 39 |
$body = json_decode( $request->get_body(), true ); // Get contents of request body. |
| 40 |
|
| 41 |
if ( $body ) { |
| 42 |
$block = $body['block'] ? sanitize_text_field($body['block']) : ''; // block name. |
| 43 |
$title = $body['title'] ? sanitize_text_field($body['title']) : ''; // block title. |
| 44 |
$type = $body['type'] ? sanitize_text_field( $body['type']) : 'enable'; // enable/disable. |
| 45 |
$disabled_blocks = GBM_Blocks::gbm_get_disabled_blocks(); // Get disabled blocks. |
| 46 |
|
| 47 |
if ( ! $block ) { |
| 48 |
wp_send_json( |
| 49 |
[ |
| 50 |
'success' => false, |
| 51 |
'msg' => __( 'Unable to update block.', 'block-manager' ), |
| 52 |
] |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
// Disable. |
| 57 |
if ( $type === 'disable' ) { |
| 58 |
if ( ! in_array( $block, $disabled_blocks, true ) ) { |
| 59 |
$disabled_blocks[] = $block; |
| 60 |
} |
| 61 |
|
| 62 |
$disabled_blocks = array_values( $disabled_blocks ); |
| 63 |
update_option( BLOCK_MANAGER_OPTION, $disabled_blocks ); |
| 64 |
wp_send_json( |
| 65 |
[ |
| 66 |
'success' => true, |
| 67 |
// translators: %s: The block title. |
| 68 |
'msg' => sprintf( __( '%s block disabled', 'block-manager' ), '<strong>' . $title . '</strong>' ), |
| 69 |
'disabled_blocks' => $disabled_blocks, |
| 70 |
] |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
// Enable. |
| 75 |
if ( $type === 'enable' ) { |
| 76 |
$blocks = []; |
| 77 |
|
| 78 |
// Loop all blocks and remove the specific block to be enabled. |
| 79 |
foreach ( $disabled_blocks as $disabled_block ) { |
| 80 |
if ( $block !== $disabled_block ) { |
| 81 |
$blocks[] = $disabled_block; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
$blocks = array_values( $blocks ); |
| 86 |
update_option( BLOCK_MANAGER_OPTION, $blocks ); |
| 87 |
wp_send_json( |
| 88 |
[ |
| 89 |
'success' => true, |
| 90 |
// translators: %s: The block title. |
| 91 |
'msg' => sprintf( __( '%s block enabled', 'block-manager' ), '<strong>' . $title . '</strong>' ), |
| 92 |
'disabled_blocks' => $blocks, |
| 93 |
] |
| 94 |
); |
| 95 |
} |
| 96 |
} else { |
| 97 |
wp_send_json( |
| 98 |
[ |
| 99 |
'success' => false, |
| 100 |
'msg' => __( 'Error accessing API data.', 'block-manager' ), |
| 101 |
'disabled_blocks' => [], |
| 102 |
] |
| 103 |
); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|