PluginProbe
GutSlider – All in One Slider and Carousel Blocks for Gutenberg / 2.9.7
GutSlider – All in One Slider and Carousel Blocks for Gutenberg v2.9.7
3.1.0 3.0.0 2.13.2 2.13.1 2.13.0 trunk 1.0.0 2.1.0 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.1 2.5.3 2.5.4 2.5.5 2.6.1 All 56 releases
slider-blocks / includes / api / api.php

api.php in GutSlider – All in One Slider and Carousel Blocks for Gutenberg 2.9.7, at includes/api/api.php

178 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // exit if accessed directly
4 if (!defined('ABSPATH')) exit;
5
6 if (!class_exists('GutSlider_Blocks_API')) {
7
8 /**
9 * Nexa Blocks API
10 *
11 * @since 1.0.0
12 */
13
14 class GutSlider_Blocks_API {
15
16 /**
17 * Constructor
18 *
19 * @since 1.0.0
20 */
21 public function __construct() {
22 add_action('rest_api_init', [$this, 'register_endpoints']);
23 add_action('init', [$this, 'save_default_blocks']);
24 }
25
26 /**
27 * Register Endpoints
28 *
29 * @since 1.0.0
30 */
31 public function register_endpoints() {
32 register_rest_route('gutslider/v1', '/blocks', [
33 'methods' => ['GET', 'POST'],
34 'callback' => [$this, 'handle_blocks_request'],
35 'permission_callback' => [$this, 'get_permissions']
36 ]);
37 }
38
39 /**
40 * Handle Blocks Request
41 *
42 * @since 1.0.0
43 */
44 public function handle_blocks_request($request) {
45 if ($request->get_method() === 'GET') {
46 return $this->get_blocks();
47 } elseif ($request->get_method() === 'POST') {
48 return $this->update_block_status($request);
49 }
50 }
51
52 /**
53 * Get Blocks
54 *
55 * @since 1.0.0
56 */
57 public function get_blocks() {
58 return rest_ensure_response(get_option('gutslider_blocks'));
59 }
60
61 /**
62 * Update Block Status
63 *
64 * @since 1.0.0
65 */
66 public function update_block_status( $request ) {
67 // nonce verification
68 $nonce = $request->get_param('nonce');
69
70 if (!wp_verify_nonce($nonce, 'gutslider_nonce')) {
71 return new WP_Error('invalid_request', __('Invalid request.', 'slider-blocks'), array('status' => 403));
72 }
73
74 $block_names = $request->get_param('names');
75 $single_block_name = $request->get_param('name');
76 $active_status = filter_var($request->get_param('active'), FILTER_VALIDATE_BOOLEAN);
77
78 // Fetch existing blocks
79 $blocks = get_option('gutslider_blocks');
80
81 if (!empty($single_block_name)) {
82 $block_names = [sanitize_text_field($single_block_name)];
83 } elseif (is_array($block_names)) {
84 $block_names = array_map('sanitize_text_field', $block_names);
85 } else {
86 return new WP_Error('invalid_request', __('Invalid block name(s) provided.', 'slider-blocks'), array('status' => 400));
87 }
88
89 // Update the blocks' active status
90 foreach ($blocks as &$block) {
91 if (in_array($block['name'], $block_names)) {
92 $block['active'] = $active_status;
93 }
94 }
95
96 // Update the option
97 update_option('gutslider_blocks', $blocks);
98
99 return rest_ensure_response($blocks);
100 }
101
102 /**
103 * Get Blocks Permissions
104 *
105 * @since 1.0.0
106 */
107 public function get_permissions() {
108 return current_user_can('edit_posts');
109 }
110
111 /**
112 * Save Default Blocks
113 *
114 * @since 1.0.0
115 */
116 public function save_default_blocks() {
117 $existing_blocks = get_option('gutslider_blocks', []);
118 $new_blocks = $this->get_gutslider_blocks();
119
120 // Temporary array to store the merged blocks
121 $merged_blocks = [];
122
123 // Merge existing and new blocks
124 foreach ($new_blocks as $new_block) {
125 $found = false;
126
127 foreach ($existing_blocks as $existing_block) {
128 if ($existing_block['name'] === $new_block['name']) {
129 // Merge the existing block with new data, but retain the active status
130 $merged_blocks[] = array_merge($new_block, ['active' => $existing_block['active']]);
131 $found = true;
132 break;
133 }
134 }
135
136 // If the block does not exist in the current options, add it
137 if (!$found) {
138 $merged_blocks[] = $new_block;
139 }
140 }
141
142 // Remove blocks that are no longer in the new list
143 foreach ($existing_blocks as $existing_block) {
144 $block_exists = false;
145
146 foreach ($new_blocks as $new_block) {
147 if ($new_block['name'] === $existing_block['name']) {
148 $block_exists = true;
149 break;
150 }
151 }
152
153 if (!$block_exists) {
154 // If the block exists in existing_blocks but not in new_blocks, remove it from merged_blocks
155 $key = array_search($existing_block['name'], array_column($merged_blocks, 'name'));
156 if ($key !== false) {
157 unset($merged_blocks[$key]);
158 }
159 }
160 }
161
162 // Re-index the array to ensure there are no gaps in keys
163 $merged_blocks = array_values($merged_blocks);
164
165 update_option('gutslider_blocks', $merged_blocks);
166 }
167
168 /**
169 * Get GutSlider Blocks
170 */
171 public function get_gutslider_blocks() {
172 return require GUTSLIDER_DIR_PATH . 'includes/api/blocks.php';
173 }
174
175 }
176
177 new GutSlider_Blocks_API(); // Initialize the API class.
178 }