| 1 |
<?php |
| 2 |
/** |
| 3 |
* API Route: Export block settings as PHP. |
| 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 = '/export'; |
| 14 |
register_rest_route( |
| 15 |
$my_namespace, |
| 16 |
$my_endpoint, |
| 17 |
array( |
| 18 |
'methods' => 'GET', |
| 19 |
'callback' => 'block_manager_export', |
| 20 |
'permission_callback' => function () { |
| 21 |
return Gutenberg_Block_Manager::has_access(); |
| 22 |
}, |
| 23 |
) |
| 24 |
); |
| 25 |
} |
| 26 |
); |
| 27 |
|
| 28 |
/** |
| 29 |
* Export disabled blocks as an erray. |
| 30 |
* |
| 31 |
* @param WP_REST_Request $request The API request data. |
| 32 |
* @since 1.0 |
| 33 |
*/ |
| 34 |
function block_manager_export( WP_REST_Request $request ) { |
| 35 |
if ( is_user_logged_in() && current_user_can( apply_filters( 'block_manager_user_role', 'activate_plugins' ) ) ) { |
| 36 |
$type = filter_input( INPUT_GET, 'type', @FILTER_SANITIZE_STRING ); // phpcs:ignore |
| 37 |
|
| 38 |
if ( $type === 'blocks' ) { |
| 39 |
$filtered_blocks = Gutenberg_Block_Manager::gbm_get_filtered_blocks(); |
| 40 |
$blocks = Gutenberg_Block_Manager::gbm_get_disabled_blocks(); |
| 41 |
wp_send_json( |
| 42 |
[ |
| 43 |
'success' => true, |
| 44 |
'code' => wp_json_encode( array_merge( $blocks, $filtered_blocks ) ), |
| 45 |
] |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
if ( $type === 'categories' ) { |
| 50 |
$filtered_categories = Gutenberg_Block_Manager::gbm_get_filtered_categories(); |
| 51 |
$categories = Gutenberg_Block_Manager::gbm_get_block_categories(); |
| 52 |
wp_send_json( |
| 53 |
[ |
| 54 |
'success' => true, |
| 55 |
'code' => wp_json_encode( array_merge( $categories, $filtered_categories ) ), |
| 56 |
] |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
wp_send_json( |
| 61 |
[ |
| 62 |
'success' => false, |
| 63 |
] |
| 64 |
); |
| 65 |
|
| 66 |
} else { |
| 67 |
$response = array( |
| 68 |
'success' => false, |
| 69 |
'msg' => __( 'Unatuhorized.', 'block-manager' ), |
| 70 |
); |
| 71 |
|
| 72 |
wp_send_json( $response ); |
| 73 |
} |
| 74 |
} |
| 75 |
|