| @@ -1,51 +1,90 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Custom API route. | |
| 3 | + * API Route: Export block settings as PHP. | |
| 4 | 4 | * |
| 5 | 5 | * @since 1.0 |
| 6 | + * @package blockmanager | |
| 6 | 7 | */ |
| 7 | -add_action( 'rest_api_init', function () { | |
| 8 | - $my_namespace = 'gbm'; | |
| 9 | - $my_endpoint = '/export'; | |
| 10 | - register_rest_route( | |
| 11 | - $my_namespace, | |
| 12 | - $my_endpoint, | |
| 13 | - array( | |
| 14 | - 'methods' => 'GET', | |
| 15 | - 'callback' => 'block_manager_export', | |
| 16 | - 'permission_callback' => function () { | |
| 17 | - return Gutenberg_Block_Manager::has_access(); | |
| 18 | - }, | |
| 19 | - ) | |
| 20 | - ); | |
| 21 | -}); | |
| 22 | 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 | + | |
| 23 | 28 | /** |
| 24 | 29 | * Export disabled blocks as an erray. |
| 25 | 30 | * |
| 26 | - * @param $request $_POST | |
| 27 | - * @return $response json | |
| 31 | + * @param WP_REST_Request $request The API request data. | |
| 28 | 32 | * @since 1.0 |
| 29 | 33 | */ |
| 30 | 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 | |
| 31 | 37 | |
| 32 | - if ( is_user_logged_in() && current_user_can( apply_filters( 'block_manager_user_role', 'activate_plugins' ) ) ) { | |
| 38 | + switch($type) { | |
| 39 | + case 'blocks': | |
| 40 | + $filtered_blocks = GBM_Blocks::gbm_get_filtered_blocks(); | |
| 41 | + $blocks = GBM_Blocks::gbm_get_disabled_blocks(); | |
| 42 | + wp_send_json( | |
| 43 | + [ | |
| 44 | + 'success' => true, | |
| 45 | + 'code' => wp_json_encode( array_merge( $blocks, $filtered_blocks ) ), | |
| 46 | + ] | |
| 47 | + ); | |
| 48 | + break; | |
| 33 | 49 | |
| 34 | - $blocks = Gutenberg_Block_Manager::gbm_get_disabled_blocks(); | |
| 50 | + case 'patterns': | |
| 51 | + $filtered_patterns = GBM_Patterns::gbm_get_filtered_patterns(); | |
| 52 | + $patterns = GBM_Patterns::gbm_get_disabled_patterns(); | |
| 53 | + wp_send_json( | |
| 54 | + [ | |
| 55 | + 'success' => true, | |
| 56 | + 'code' => wp_json_encode( array_merge( $patterns, $filtered_patterns ) ), | |
| 57 | + ] | |
| 58 | + ); | |
| 59 | + break; | |
| 35 | 60 | |
| 36 | - $response = array( | |
| 37 | - 'success' => true, | |
| 38 | - 'blocks' => json_encode( $blocks ), | |
| 61 | + case 'categories': | |
| 62 | + $filtered_categories = GBM_Categories::gbm_get_filtered_categories(); | |
| 63 | + $categories = GBM_Categories::gbm_get_block_categories(); | |
| 64 | + wp_send_json( | |
| 65 | + [ | |
| 66 | + 'success' => true, | |
| 67 | + 'code' => wp_json_encode( array_merge( $categories, $filtered_categories ) ), | |
| 68 | + ] | |
| 69 | + ); | |
| 70 | + break; | |
| 71 | + } | |
| 72 | + | |
| 73 | + wp_send_json( | |
| 74 | + [ | |
| 75 | + 'success' => false, | |
| 76 | + ] | |
| 39 | 77 | ); |
| 40 | 78 | |
| 41 | - wp_send_json( $response ); | |
| 42 | - | |
| 43 | 79 | } else { |
| 44 | - $response = array( | |
| 45 | - 'success' => false, | |
| 46 | - 'msg' => __( 'Unatuhorized.', 'block-manager' ), | |
| 80 | + wp_send_json( | |
| 81 | + [ | |
| 82 | + 'success' => false, | |
| 83 | + 'msg' => __( | |
| 84 | + 'Unatuhorized.', | |
| 85 | + 'block-manager' | |
| 86 | + ), | |
| 87 | + ] | |
| 47 | 88 | ); |
| 48 | - | |
| 49 | - wp_send_json( $response ); | |
| 50 | 89 | } |
| 51 | 90 | } |