PluginProbe
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables / 2.2.14
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables v2.2.14
2.2.14 2.2.13 2.2.12 2.2.11 2.2.10 2.2.9 2.2.8 2.2.7 2.2.6 2.2.5 2.2.4 2.2.3 trunk 1.0.0 1.0.1 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2
← All changes | includes/Core/RestApi.php +409 -267 2.2.72.2.14 View file →
@@ -1,8 +1,14 @@
1 1 <?php
2 +/**
3 + * REST API fallback for plugin dependency management
4 + *
5 + * @package TableKit
6 + */
7 +
2 8 namespace TableBuilder\Core;
3 9
4 -defined('ABSPATH') || exit;
10 +defined( 'ABSPATH' ) || exit;
5 11
6 12 /**
7 13 * REST API fallback when tablekit-essential is not active.
8 14 * Mimics tablekit-essential behavior for compatibility.
@@ -9,327 +15,463 @@
9 15 *
10 16 * @since 1.0.0
11 17 */
12 18 class RestApi {
13 - use \TableBuilder\Traits\Singleton;
19 + use \TableBuilder\Traits\Singleton;
14 20
15 - public function __construct() {
16 - add_action('rest_api_init', array($this, 'register_routes'));
17 - add_action('activated_plugin', array($this, 'clear_plugin_cache'));
18 - add_action('deactivated_plugin', array($this, 'clear_plugin_cache'));
19 - }
21 + /**
22 + * Hooks route registration and plugin-cache invalidation into WordPress.
23 + */
24 + public function __construct() {
25 + add_action( 'rest_api_init', array( $this, 'register_routes' ) );
26 + add_action( 'activated_plugin', array( $this, 'clear_plugin_cache' ) );
27 + add_action( 'deactivated_plugin', array( $this, 'clear_plugin_cache' ) );
28 + }
20 29
21 - // Clear plugin cache when plugins change.
22 - public function clear_plugin_cache() {
23 - delete_transient('tablekit_plugin_data');
24 - }
30 + /**
31 + * Clears the cached plugin-status transient when plugins are (de)activated.
32 + *
33 + * @return void
34 + */
35 + public function clear_plugin_cache() {
36 + delete_transient( 'tablekit_plugin_data' );
37 + }
25 38
26 - // Register REST routes only if tablekit-essential is not active.
27 - public function register_routes() {
28 - if ($this->is_tablekit_essential_active()) {
29 - return;
30 - }
39 + /**
40 + * Registers the tablekit/v1 fallback REST routes, only if the
41 + * tablekit-essential plugin (which normally provides them) isn't active.
42 + *
43 + * @return void
44 + */
45 + public function register_routes() {
46 + if ( $this->is_tablekit_essential_active() ) {
47 + return;
48 + }
31 49
32 - register_rest_route('tablekit/v1', '/table-manager-api/check-plugins', array(
33 - 'methods' => 'POST',
34 - 'callback' => array($this, 'check_plugins_status'),
35 - 'permission_callback' => '__return_true',
36 - ));
50 + register_rest_route(
51 + 'tablekit/v1',
52 + '/table-manager-api/check-plugins',
53 + array(
54 + 'methods' => 'POST',
55 + 'callback' => array( $this, 'check_plugins_status' ),
56 + 'permission_callback' => function () {
57 + return current_user_can( 'edit_posts' ); },
58 + )
59 + );
37 60
38 - register_rest_route('tablekit/v1', '/table-manager-api/activate-plugins', array(
39 - 'methods' => 'POST',
40 - 'callback' => array($this, 'activate_plugins'),
41 - 'permission_callback' => function() { return current_user_can('activate_plugins'); },
42 - ));
61 + register_rest_route(
62 + 'tablekit/v1',
63 + '/table-manager-api/activate-plugins',
64 + array(
65 + 'methods' => 'POST',
66 + 'callback' => array( $this, 'activate_plugins' ),
67 + 'permission_callback' => function () {
68 + return current_user_can( 'activate_plugins' ); },
69 + )
70 + );
43 71
44 - register_rest_route('tablekit/v1', '/table-manager-api/install-plugins', array(
45 - 'methods' => 'POST',
46 - 'callback' => array($this, 'install_plugins'),
47 - 'permission_callback' => function() { return current_user_can('install_plugins'); },
48 - ));
72 + register_rest_route(
73 + 'tablekit/v1',
74 + '/table-manager-api/install-plugins',
75 + array(
76 + 'methods' => 'POST',
77 + 'callback' => array( $this, 'install_plugins' ),
78 + 'permission_callback' => function () {
79 + return current_user_can( 'install_plugins' ); },
80 + )
81 + );
49 82
50 - register_rest_route('tablekit/v1', '/table-manager-api/check-pro-license', array(
51 - 'methods' => 'POST',
52 - 'callback' => array($this, 'check_pro_license'),
53 - 'permission_callback' => function() { return current_user_can('activate_plugins'); },
54 - ));
55 - }
83 + register_rest_route(
84 + 'tablekit/v1',
85 + '/table-manager-api/check-pro-license',
86 + array(
87 + 'methods' => 'POST',
88 + 'callback' => array( $this, 'check_pro_license' ),
89 + 'permission_callback' => function () {
90 + return current_user_can( 'activate_plugins' ); },
91 + )
92 + );
93 + }
56 94
57 - private function is_tablekit_essential_active() {
58 - if (!function_exists('is_plugin_active')) {
59 - require_once ABSPATH . 'wp-admin/includes/plugin.php';
60 - }
61 - return is_plugin_active('tablekit-essential/tablekit-essential.php');
62 - }
95 + /**
96 + * Checks whether the tablekit-essential plugin is active.
97 + *
98 + * @return bool True if tablekit-essential is active.
99 + */
100 + private function is_tablekit_essential_active() {
101 + if ( ! function_exists( 'is_plugin_active' ) ) {
102 + require_once ABSPATH . 'wp-admin/includes/plugin.php';
103 + }
104 + return is_plugin_active( 'tablekit-essential/tablekit-essential.php' );
105 + }
63 106
64 - public function check_plugins_status($request) {
65 - $plugins = $request->get_json_params()['plugins'] ?? array();
66 - if (empty($plugins)) {
67 - return new \WP_REST_Response(array('status' => 'error', 'message' => 'No plugins specified.'), 400);
68 - }
107 + /**
108 + * REST callback: report install/active status for a list of plugin slugs.
109 + *
110 + * @param \WP_REST_Request $request Request with a JSON "plugins" array param.
111 + * @return \WP_REST_Response Status per slug: installed/active/state/plugin_file.
112 + */
113 + public function check_plugins_status( $request ) {
114 + $plugins = $request->get_json_params()['plugins'] ?? array();
115 + if ( empty( $plugins ) ) {
116 + return new \WP_REST_Response(
117 + array(
118 + 'status' => 'error',
119 + 'message' => 'No plugins specified.',
120 + ),
121 + 400
122 + );
123 + }
69 124
70 - // Clear plugin cache to ensure fresh data (critical for detecting deactivated plugins on servers)
71 - wp_cache_delete('plugins', 'plugins');
72 -
125 + // Clear plugin cache to ensure fresh data (critical for detecting deactivated plugins on servers).
126 + wp_cache_delete( 'plugins', 'plugins' );
73 127
74 - if (!function_exists('get_plugins') || !function_exists('is_plugin_active')) {
75 - require_once ABSPATH . 'wp-admin/includes/plugin.php';
76 - }
128 + if ( ! function_exists( 'get_plugins' ) || ! function_exists( 'is_plugin_active' ) ) {
129 + require_once ABSPATH . 'wp-admin/includes/plugin.php';
130 + }
77 131
78 - $all_plugins = get_plugins();
79 - $status = array();
132 + $all_plugins = get_plugins();
133 + $status = array();
80 134
81 - foreach ($plugins as $slug) {
82 - $slug = sanitize_text_field($slug);
83 - if (empty($slug)) continue;
135 + foreach ( $plugins as $slug ) {
136 + $slug = sanitize_text_field( $slug );
137 + if ( empty( $slug ) ) {
138 + continue;
139 + }
84 140
85 - $plugin_file = null;
86 - foreach ($all_plugins as $plugin_path => $plugin_data) {
87 - if (strpos($plugin_path, $slug . '/') === 0 || $plugin_path === $slug . '.php') {
88 - $plugin_file = $plugin_path;
89 - break;
90 - }
91 - }
141 + $plugin_file = null;
142 + foreach ( $all_plugins as $plugin_path => $plugin_data ) {
143 + if ( 0 === strpos( $plugin_path, $slug . '/' ) || $slug . '.php' === $plugin_path ) {
144 + $plugin_file = $plugin_path;
145 + break;
146 + }
147 + }
92 148
93 - $is_installed = $plugin_file !== null;
94 - $is_active = $is_installed && is_plugin_active($plugin_file);
149 + $is_installed = null !== $plugin_file;
150 + $is_active = $is_installed && is_plugin_active( $plugin_file );
95 151
96 - $status[$slug] = array(
97 - 'installed' => $is_installed,
98 - 'active' => $is_active,
99 - 'state' => $is_installed ? ($is_active ? 'installed_active' : 'installed_inactive') : 'not_installed',
100 - 'plugin_file' => $plugin_file,
101 - );
102 - }
152 + $status[ $slug ] = array(
153 + 'installed' => $is_installed,
154 + 'active' => $is_active,
155 + 'state' => $is_installed ? ( $is_active ? 'installed_active' : 'installed_inactive' ) : 'not_installed',
156 + 'plugin_file' => $plugin_file,
157 + );
158 + }
103 159
104 - return new \WP_REST_Response(array('status' => 'success', 'plugins' => $status), 200);
105 - }
160 + return new \WP_REST_Response(
161 + array(
162 + 'status' => 'success',
163 + 'plugins' => $status,
164 + ),
165 + 200
166 + );
167 + }
106 168
107 - // Activate plugins.
108 - public function activate_plugins($request) {
109 - $plugins = $request->get_json_params()['plugins'] ?? array();
110 - if (empty($plugins)) {
111 - return new \WP_REST_Response(array('status' => 'error', 'message' => 'No plugins specified.'), 400);
112 - }
169 + /**
170 + * REST callback: activate a list of already-installed plugins by slug.
171 + *
172 + * @param \WP_REST_Request $request Request with a JSON "plugins" array param.
173 + * @return \WP_REST_Response Activation results: which slugs succeeded/failed.
174 + */
175 + public function activate_plugins( $request ) {
176 + $plugins = $request->get_json_params()['plugins'] ?? array();
177 + if ( empty( $plugins ) ) {
178 + return new \WP_REST_Response(
179 + array(
180 + 'status' => 'error',
181 + 'message' => 'No plugins specified.',
182 + ),
183 + 400
184 + );
185 + }
113 186
114 - if (!function_exists('activate_plugin')) {
115 - require_once ABSPATH . 'wp-admin/includes/plugin.php';
116 - }
187 + if ( ! function_exists( 'activate_plugin' ) ) {
188 + require_once ABSPATH . 'wp-admin/includes/plugin.php';
189 + }
117 190
118 - wp_cache_delete('plugins', 'plugins');
191 + wp_cache_delete( 'plugins', 'plugins' );
119 192
120 - // Set transient to prevent redirect during programmatic activation
121 - set_transient('tablekit_skip_activation_redirect', true, 60);
193 + // Set transient to prevent redirect during programmatic activation.
194 + set_transient( 'tablekit_skip_activation_redirect', true, 60 );
122 195
123 - $all_plugins = get_plugins();
124 - $activated = array();
125 - $failed = array();
196 + $all_plugins = get_plugins();
197 + $activated = array();
198 + $failed = array();
126 199
127 - foreach ($plugins as $slug) {
128 - $slug = sanitize_text_field($slug);
129 - if (empty($slug)) continue;
200 + foreach ( $plugins as $slug ) {
201 + $slug = sanitize_text_field( $slug );
202 + if ( empty( $slug ) ) {
203 + continue;
204 + }
130 205
131 - $plugin_file = null;
132 - foreach ($all_plugins as $plugin_path => $plugin_data) {
133 - if (strpos($plugin_path, $slug . '/') === 0 || $plugin_path === $slug . '.php') {
134 - $plugin_file = $plugin_path;
135 - break;
136 - }
137 - }
206 + $plugin_file = null;
207 + foreach ( $all_plugins as $plugin_path => $plugin_data ) {
208 + if ( strpos( $plugin_path, $slug . '/' ) === 0 || $plugin_path === $slug . '.php' ) {
209 + $plugin_file = $plugin_path;
210 + break;
211 + }
212 + }
138 213
139 - if (!$plugin_file) {
140 - $failed[] = array('slug' => $slug, 'error' => 'Plugin not found');
141 - continue;
142 - }
214 + if ( ! $plugin_file ) {
215 + $failed[] = array(
216 + 'slug' => $slug,
217 + 'error' => 'Plugin not found',
218 + );
219 + continue;
220 + }
143 221
144 - if (is_plugin_active($plugin_file)) {
145 - $activated[] = $slug;
146 - continue;
147 - }
222 + if ( is_plugin_active( $plugin_file ) ) {
223 + $activated[] = $slug;
224 + continue;
225 + }
148 226
149 - $result = activate_plugin($plugin_file, '', false, true); // Silent activation
150 - if (is_wp_error($result)) {
151 - $failed[] = array('slug' => $slug, 'error' => $result->get_error_message());
152 - } else {
153 - $activated[] = $slug;
154 - }
155 - }
227 + $result = activate_plugin( $plugin_file, '', false, true ); // Silent activation.
228 + if ( is_wp_error( $result ) ) {
229 + $failed[] = array(
230 + 'slug' => $slug,
231 + 'error' => $result->get_error_message(),
232 + );
233 + } else {
234 + $activated[] = $slug;
235 + }
236 + }
156 237
157 - // Clean up transient
158 - delete_transient('tablekit_skip_activation_redirect');
238 + // Clean up transient.
239 + delete_transient( 'tablekit_skip_activation_redirect' );
159 240
160 - // Clear plugin cache after activation
161 - $this->clear_plugin_cache();
241 + // Clear plugin cache after activation.
242 + $this->clear_plugin_cache();
162 243
163 - return new \WP_REST_Response(array(
164 - 'status' => empty($failed) ? 'success' : 'partial',
165 - 'activated' => $activated,
166 - 'failed' => $failed,
167 - ), 200);
168 - }
244 + return new \WP_REST_Response(
245 + array(
246 + 'status' => empty( $failed ) ? 'success' : 'partial',
247 + 'activated' => $activated,
248 + 'failed' => $failed,
249 + ),
250 + 200
251 + );
252 + }
169 253
170 - // Install plugins.
171 - public function install_plugins($request) {
172 - $plugins = $request->get_json_params()['plugins'] ?? array();
173 - if (empty($plugins)) {
174 - return new \WP_REST_Response(array('status' => 'error', 'message' => 'No plugins specified.'), 400);
175 - }
254 + /**
255 + * REST callback: install (and activate) a list of plugins by slug from
256 + * the WordPress.org plugin directory. Slugs containing "-pro" are
257 + * rejected, since pro plugins must be purchased/uploaded manually.
258 + *
259 + * @param \WP_REST_Request $request Request with a JSON "plugins" array param.
260 + * @return \WP_REST_Response Installation results: which slugs failed and why.
261 + */
262 + public function install_plugins( $request ) {
263 + $plugins = $request->get_json_params()['plugins'] ?? array();
264 + if ( empty( $plugins ) ) {
265 + return new \WP_REST_Response(
266 + array(
267 + 'status' => 'error',
268 + 'message' => 'No plugins specified.',
269 + ),
270 + 400
271 + );
272 + }
176 273
177 - if (!function_exists('plugins_api')) {
178 - require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
179 - require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
180 - require_once ABSPATH . 'wp-admin/includes/file.php';
181 - }
274 + if ( ! function_exists( 'plugins_api' ) ) {
275 + require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
276 + require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
277 + require_once ABSPATH . 'wp-admin/includes/file.php';
278 + }
182 279
183 - $failed = array();
280 + $failed = array();
184 281
185 - foreach ($plugins as $slug) {
186 - $slug = sanitize_text_field($slug);
187 - if (empty($slug)) continue;
282 + foreach ( $plugins as $slug ) {
283 + $slug = sanitize_text_field( $slug );
284 + if ( empty( $slug ) ) {
285 + continue;
286 + }
188 287
189 - // Check if pro plugin
190 - if (strpos($slug, '-pro') !== false) {
191 - $failed[] = array('slug' => $slug, 'error' => 'Pro plugins must be purchased manually');
192 - continue;
193 - }
288 + // Check if pro plugin.
289 + if ( strpos( $slug, '-pro' ) !== false ) {
290 + $failed[] = array(
291 + 'slug' => $slug,
292 + 'error' => 'Pro plugins must be purchased manually',
293 + );
294 + continue;
295 + }
194 296
195 - // Check if already installed
196 - $all_plugins = get_plugins();
197 - $plugin_file = null;
198 - foreach ($all_plugins as $plugin_path => $plugin_data) {
199 - if (strpos($plugin_path, $slug . '/') === 0) {
200 - $plugin_file = $plugin_path;
201 - break;
202 - }
203 - }
297 + // Check if already installed.
298 + $all_plugins = get_plugins();
299 + $plugin_file = null;
300 + foreach ( $all_plugins as $plugin_path => $plugin_data ) {
301 + if ( strpos( $plugin_path, $slug . '/' ) === 0 ) {
302 + $plugin_file = $plugin_path;
303 + break;
304 + }
305 + }
204 306
205 - if ($plugin_file) {
206 - // Already installed, just activate
207 - if (!is_plugin_active($plugin_file)) {
208 - set_transient('tablekit_skip_activation_redirect', true, 60); // Set transient to prevent redirect
209 - activate_plugin($plugin_file, '', false, true); // Silent activation
210 - delete_transient('tablekit_skip_activation_redirect');
211 - }
212 - continue;
213 - }
307 + if ( $plugin_file ) {
308 + // Already installed, just activate.
309 + if ( ! is_plugin_active( $plugin_file ) ) {
310 + set_transient( 'tablekit_skip_activation_redirect', true, 60 ); // Set transient to prevent redirect.
311 + activate_plugin( $plugin_file, '', false, true ); // Silent activation.
312 + delete_transient( 'tablekit_skip_activation_redirect' );
313 + }
314 + continue;
315 + }
214 316
215 - // Install plugin
216 - $api = plugins_api('plugin_information', array('slug' => $slug, 'fields' => array('sections' => false)));
217 -
218 - if (is_wp_error($api)) {
219 - $failed[] = array('slug' => $slug, 'error' => $api->get_error_message());
220 - continue;
221 - }
317 + // Install plugin.
318 + $api = plugins_api(
319 + 'plugin_information',
320 + array(
321 + 'slug' => $slug,
322 + 'fields' => array( 'sections' => false ),
323 + )
324 + );
222 325
223 - $upgrader = new \Plugin_Upgrader(new \WP_Ajax_Upgrader_Skin());
224 - $result = $upgrader->install($api->download_link);
326 + if ( is_wp_error( $api ) ) {
327 + $failed[] = array(
328 + 'slug' => $slug,
329 + 'error' => $api->get_error_message(),
330 + );
331 + continue;
332 + }
225 333
226 - if (is_wp_error($result)) {
227 - $failed[] = array('slug' => $slug, 'error' => $result->get_error_message());
228 - } elseif ($result === false) {
229 - $failed[] = array('slug' => $slug, 'error' => 'Installation failed');
230 - }
231 - }
334 + $upgrader = new \Plugin_Upgrader( new \WP_Ajax_Upgrader_Skin() );
335 + $result = $upgrader->install( $api->download_link );
232 336
233 - // Clear plugin cache after installation
234 - $this->clear_plugin_cache();
337 + if ( is_wp_error( $result ) ) {
338 + $failed[] = array(
339 + 'slug' => $slug,
340 + 'error' => $result->get_error_message(),
341 + );
342 + } elseif ( false === $result ) {
343 + $failed[] = array(
344 + 'slug' => $slug,
345 + 'error' => 'Installation failed',
346 + );
347 + }
348 + }
235 349
236 - return new \WP_REST_Response(array(
237 - 'status' => empty($failed) ? 'success' : 'partial',
238 - 'failed' => $failed,
239 - ), 200);
240 - }
350 + // Clear plugin cache after installation.
351 + $this->clear_plugin_cache();
241 352
242 - /**
243 - * Check pro plugin license.
244 - */
245 - public function check_pro_license($request) {
246 - $plugins = $request->get_json_params()['plugins'] ?? array();
247 - if (empty($plugins)) {
248 - return new \WP_REST_Response(array('status' => 'error', 'message' => 'No plugins specified.'), 400);
249 - }
353 + return new \WP_REST_Response(
354 + array(
355 + 'status' => empty( $failed ) ? 'success' : 'partial',
356 + 'failed' => $failed,
357 + ),
358 + 200
359 + );
360 + }
250 361
251 - // Check if this is gutenkit-blocks-addon-pro
252 - $is_gutenkit_pro = false;
253 - foreach ($plugins as $slug) {
254 - if (strpos($slug, 'gutenkit-blocks-addon-pro') !== false) {
255 - $is_gutenkit_pro = true;
256 - break;
257 - }
258 - }
362 + /**
363 + * REST callback: check whether GutenKit Pro's license is active
364 + * (license checks are currently only supported for that plugin).
365 + *
366 + * @param \WP_REST_Request $request Request with a JSON "plugins" array param.
367 + * @return \WP_REST_Response License status.
368 + */
369 + public function check_pro_license( $request ) {
370 + $plugins = $request->get_json_params()['plugins'] ?? array();
371 + if ( empty( $plugins ) ) {
372 + return new \WP_REST_Response(
373 + array(
374 + 'status' => 'error',
375 + 'message' => 'No plugins specified.',
376 + ),
377 + 400
378 + );
379 + }
259 380
260 - if (!$is_gutenkit_pro) {
261 - return new \WP_REST_Response(array(
262 - 'status' => 'error',
263 - 'license_valid' => false,
264 - 'message' => 'License check is only available for GutenKit Pro.',
265 - ), 400);
266 - }
381 + // Check if this is gutenkit-blocks-addon-pro.
382 + $is_gutenkit_pro = false;
383 + foreach ( $plugins as $slug ) {
384 + if ( strpos( $slug, 'gutenkit-blocks-addon-pro' ) !== false ) {
385 + $is_gutenkit_pro = true;
386 + break;
387 + }
388 + }
267 389
268 - // Check GutenKit license status
269 - // GutenKit uses these options to store license info
270 - $oppai = get_option('__gutenkit_oppai__', '');
271 - $key = get_option('__gutenkit_license_key__', '');
390 + if ( ! $is_gutenkit_pro ) {
391 + return new \WP_REST_Response(
392 + array(
393 + 'status' => 'error',
394 + 'license_valid' => false,
395 + 'message' => 'License check is only available for GutenKit Pro.',
396 + ),
397 + 400
398 + );
399 + }
272 400
273 - $license_valid = !empty($oppai) && !empty($key);
401 + // Check GutenKit license status.
402 + // GutenKit uses these options to store license info.
403 + $oppai = get_option( '__gutenkit_oppai__', '' );
404 + $key = get_option( '__gutenkit_license_key__', '' );
274 405
275 - if (!$license_valid) {
276 - return new \WP_REST_Response(array(
277 - 'status' => 'error',
278 - 'license_valid' => false,
279 - 'message' => 'Pro plugin license is not active. Please activate your license at GutenKit → Settings → License to use pro features.',
280 - ), 200);
281 - }
406 + $license_valid = ! empty( $oppai ) && ! empty( $key );
282 407
283 - return new \WP_REST_Response(array(
284 - 'status' => 'success',
285 - 'license_valid' => true,
286 - 'message' => 'License is valid.',
287 - ), 200);
288 - }
408 + if ( ! $license_valid ) {
409 + return new \WP_REST_Response(
410 + array(
411 + 'status' => 'error',
412 + 'license_valid' => false,
413 + 'message' => 'Pro plugin license is not active. Please activate your license at GutenKit → Settings → License to use pro features.',
414 + ),
415 + 200
416 + );
417 + }
289 418
290 - /**
291 - * Get localized data - mimics tablekit-essential format.
292 - * Now with caching to improve performance.
293 - */
294 - public static function get_localized_data() {
295 - // Check cache first (expires after 12 hours)
296 - $cached_data = get_transient('tablekit_plugin_data');
297 - if (false !== $cached_data && is_array($cached_data)) {
298 - // Update dynamic nonces
299 - $cached_data['restNonce'] = wp_create_nonce('wp_rest');
300 - $cached_data['ajaxNonce'] = wp_create_nonce('tablekit_activate_nonce');
301 - return $cached_data;
302 - }
419 + return new \WP_REST_Response(
420 + array(
421 + 'status' => 'success',
422 + 'license_valid' => true,
423 + 'message' => 'License is valid.',
424 + ),
425 + 200
426 + );
427 + }
303 428
304 - if (!function_exists('get_plugins') || !function_exists('is_plugin_active')) {
305 - require_once ABSPATH . 'wp-admin/includes/plugin.php';
306 - }
429 + /**
430 + * Builds the data localized to the editor as `tablekitEssential`, mirroring
431 + * the shape the tablekit-essential plugin provides, so the editor JS
432 + * doesn't need to branch on which one is active. Cached for 12 hours.
433 + *
434 + * @return array{apiUrl:string,restNonce:string,ajaxNonce:string,activePlugins:string[],installedPlugins:string[],requiredPlugins:string[]}
435 + */
436 + public static function get_localized_data() {
437 + // Check cache first (expires after 12 hours).
438 + $cached_data = get_transient( 'tablekit_plugin_data' );
439 + if ( false !== $cached_data && is_array( $cached_data ) ) {
440 + // Update dynamic nonces.
441 + $cached_data['restNonce'] = wp_create_nonce( 'wp_rest' );
442 + $cached_data['ajaxNonce'] = wp_create_nonce( 'tablekit_activate_nonce' );
443 + return $cached_data;
444 + }
307 445
308 - $all_plugins = get_plugins();
309 - $active_plugins = array();
310 - $installed_plugins = array();
446 + if ( ! function_exists( 'get_plugins' ) || ! function_exists( 'is_plugin_active' ) ) {
447 + require_once ABSPATH . 'wp-admin/includes/plugin.php';
448 + }
311 449
312 - foreach ($all_plugins as $plugin_path => $plugin_data) {
313 - $slug = explode('/', $plugin_path)[0];
314 - $installed_plugins[] = $slug;
315 -
316 - if (is_plugin_active($plugin_path)) {
317 - $active_plugins[] = $slug;
318 - }
319 - }
450 + $all_plugins = get_plugins();
451 + $active_plugins = array();
452 + $installed_plugins = array();
320 453
321 - $data = array(
322 - 'apiUrl' => rest_url('tablekit/v1/'),
323 - 'restNonce' => wp_create_nonce('wp_rest'),
324 - 'ajaxNonce' => wp_create_nonce('tablekit_activate_nonce'),
325 - 'activePlugins' => array_unique($active_plugins),
326 - 'installedPlugins' => array_unique($installed_plugins),
327 - 'requiredPlugins' => array('gutenkit-blocks-addon', 'gutenkit-blocks-addon-pro'),
328 - );
454 + foreach ( $all_plugins as $plugin_path => $plugin_data ) {
455 + $slug = explode( '/', $plugin_path )[0];
456 + $installed_plugins[] = $slug;
329 457
330 - // Cache for 12 hours
331 - set_transient('tablekit_plugin_data', $data, 12 * HOUR_IN_SECONDS);
458 + if ( is_plugin_active( $plugin_path ) ) {
459 + $active_plugins[] = $slug;
460 + }
461 + }
332 462
333 - return $data;
334 - }
463 + $data = array(
464 + 'apiUrl' => rest_url( 'tablekit/v1/' ),
465 + 'restNonce' => wp_create_nonce( 'wp_rest' ),
466 + 'ajaxNonce' => wp_create_nonce( 'tablekit_activate_nonce' ),
467 + 'activePlugins' => array_unique( $active_plugins ),
468 + 'installedPlugins' => array_unique( $installed_plugins ),
469 + 'requiredPlugins' => array( 'gutenkit-blocks-addon', 'gutenkit-blocks-addon-pro' ),
470 + );
471 +
472 + // Cache for 12 hours.
473 + set_transient( 'tablekit_plugin_data', $data, 12 * HOUR_IN_SECONDS );
474 +
475 + return $data;
476 + }
335 477 }