PluginProbe
Block Manager / 1.2.4
Block Manager v1.2.4
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.4, at api/category-switch.php

94 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * API Route: Switch a category.
4 *
5 * @since 1.2
6 * @package blockmanager
7 */
8
9 add_action(
10 'rest_api_init',
11 function () {
12 $my_namespace = 'gbm';
13 $my_endpoint = '/category_switch';
14 register_rest_route(
15 $my_namespace,
16 $my_endpoint,
17 array(
18 'methods' => 'POST',
19 'callback' => 'block_manager_category_switch',
20 'permission_callback' => function () {
21 return Gutenberg_Block_Manager::has_access();
22 },
23 )
24 );
25 }
26 );
27
28 /**
29 * Switch the category of a Gutenberg block.
30 *
31 * @param WP_REST_Request $request The content of the HTTP request.
32 * @since 1.0
33 */
34 function block_manager_category_switch( WP_REST_Request $request ) {
35
36 if ( is_user_logged_in() && current_user_can( apply_filters( 'block_manager_user_role', 'activate_plugins' ) ) ) {
37
38 error_reporting( E_ALL | E_STRICT ); // @codingStandardsIgnoreLine
39
40 // Get JSON Data.
41 $body = json_decode( $request->get_body(), true ); // Get contents of request body.
42 $data = json_decode( $body['data'] ); // Get contents of data.
43
44 if ( $body && $data ) {
45
46 $block = ( $data && $data->block ) ? $data->block : '';
47 $cat = ( $data && $data->cat ) ? $data->cat : '';
48
49 // Get current options.
50 $options = (array) get_option( BLOCK_MANAGER_CATEGORIES, array() );
51
52 // Remove duplicates.
53 $duplicate = false;
54 if ( $options ) {
55 // Loop all current options.
56 foreach ( $options as $index => $item ) {
57 // Duplicate found.
58 if ( $block === $item['block'] ) {
59 $duplicate = true;
60 $options[ $index ]['cat'] = $cat;
61 }
62 }
63 }
64
65 // Create array of new data.
66 $item = array(
67 'block' => $block,
68 'cat' => $cat,
69 );
70
71 // Add $object to array.
72 if ( ! $duplicate ) {
73 $options[] = $item;
74 }
75
76 // Update WP Options table.
77 update_option( BLOCK_MANAGER_CATEGORIES, $options );
78
79 // Send Response.
80 $response = array(
81 'success' => true,
82 'msg' => $block . __( ' category updated to successfully to ', 'block-manager' ) . $cat . '.',
83 );
84 } else {
85
86 $response = array(
87 'success' => false,
88 'msg' => __( 'Error accessing API data.', 'block-manager' ),
89 );
90 }
91 wp_send_json( $response ); // Send response as JSON.
92 }
93 }
94