PluginProbe
Block Manager / 1.2.2
Block Manager v1.2.2
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
block-manager / api / category_switch.php

category_switch.php in Block Manager 1.2.2, at api/category_switch.php

90 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 = '/category_switch';
10 register_rest_route(
11 $my_namespace,
12 $my_endpoint,
13 array(
14 'methods' => 'POST',
15 'callback' => 'block_manager_category_switch',
16 'permission_callback' => function () {
17 return Gutenberg_Block_Manager::has_access();
18 },
19 )
20 );
21 });
22
23 /**
24 * Switch the category of a Gutenberg block.
25 *
26 * @param WP_REST_Request $request The content of the HTTP request.
27 * @return json
28 * @since 1.0
29 */
30 function block_manager_category_switch( 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 $block = ( $data && $data->block ) ? $data->block : '';
43 $cat = ( $data && $data->cat ) ? $data->cat : '';
44
45 // Get current options.
46 $options = (array) get_option( BLOCK_MANAGER_CATEGORIES, array() );
47
48 // Remove duplicates.
49 $duplicate = false;
50 if ( $options ) {
51 // Loop all current options.
52 foreach ( $options as $index => $item ) {
53 // Duplicate found.
54 if ( $block === $item['block'] ) {
55 $duplicate = true;
56 $options[ $index ][ 'cat' ] = $cat;
57 }
58 }
59 }
60
61 // Create array of new data.
62 $item = array(
63 'block' => $block,
64 'cat' => $cat
65 );
66
67 // Add $object to array.
68 if ( ! $duplicate ) {
69 $options[] = $item;
70 }
71
72 // Update WP Options table.
73 update_option( BLOCK_MANAGER_CATEGORIES, $options );
74
75 // Send Response.
76 $response = array(
77 'success' => true,
78 'msg' => $block . __(' category updated to successfully to ', 'block-manager') . $cat . '.'
79 );
80 } else {
81
82 $response = array(
83 'success' => false,
84 'msg' => __( 'Error accessing API data.', 'block-manager' ),
85 );
86 }
87 wp_send_json( $response ); // Send response as JSON.
88 }
89 }
90