PluginProbe
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables / 2.3.0
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables v2.3.0
2.3.0 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/Enqueue.php +287 -216 2.2.4 → 2.3.0 View file →
@@ -1,12 +1,15 @@
1 1 <?php
2 +/**
3 + * Frontend and admin asset enqueue registrar
4 + *
5 + * @package TableKit
6 + */
2 7
3 8 namespace TableBuilder\Core;
4 9
5 -defined('ABSPATH') || exit;
10 +defined( 'ABSPATH' ) || exit;
6 11
7 -use TableBuilder\Helpers\Utils;
8 -
9 12 /**
10 13 * Enqueue registrar.
11 14 *
12 15 * @since 1.0.0
@@ -11,250 +14,318 @@
11 14 *
12 15 * @since 1.0.0
13 16 * @access public
14 17 */
15 -class Enqueue
16 -{
17 - use \TableBuilder\Traits\Singleton;
18 +class Enqueue {
18 19
19 - /**
20 - * Class constructor.
21 - * private for singleton
22 - *
23 - * @return void
24 - * @since 1.0.0
25 - */
26 - public function __construct()
27 - {
28 - add_action('admin_enqueue_scripts', array($this, 'admin_scripts'));
29 - add_action('enqueue_block_editor_assets', array($this, 'blocks_editor_scripts'));
30 - add_action('wp_head', array($this, 'print_device_script_for_window'));
31 - add_filter('body_class', array($this, 'add_body_class'));
32 - add_filter('admin_body_class', array($this, 'add_body_class'));
33 - }
20 + use \TableBuilder\Traits\Singleton;
34 21
35 - /**
36 - * Get WP filesystem
37 - *
38 - * @return void
39 - */
40 - protected function get_filesystem()
41 - {
42 - if (!function_exists('WP_Filesystem')) {
43 - require_once ABSPATH . 'wp-admin/includes/file.php';
44 - }
45 - WP_Filesystem();
46 - }
22 + /**
23 + * Class constructor.
24 + * private for singleton
25 + *
26 + * @return void
27 + * @since 1.0.0
28 + */
29 + public function __construct() {
30 + add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
31 + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_deactivation_popup_scripts' ) );
32 + add_action( 'enqueue_block_editor_assets', array( $this, 'blocks_editor_scripts' ) );
33 + add_filter( 'body_class', array( $this, 'add_body_class' ) );
34 + add_filter( 'admin_body_class', array( $this, 'add_body_class' ) );
35 + }
47 36
48 - /**
49 - * Enqueues necessary scripts and localizes data for the admin area.
50 - *
51 - * @param string $hook The current page.
52 - * @return void
53 - * @since 1.0.0
54 - */
55 - public function admin_scripts($hook)
56 - {
57 - // Only load heavy data on editor screens to improve performance
58 - $is_editor_screen = in_array($hook, array('post.php', 'post-new.php', 'site-editor.php', 'widgets.php'));
37 + /**
38 + * Gets the WP filesystem instance, loading the API if needed.
39 + *
40 + * @return void
41 + */
42 + protected function get_filesystem() {
43 + if ( ! function_exists( 'WP_Filesystem' ) ) {
44 + require_once ABSPATH . 'wp-admin/includes/file.php';
45 + }
46 + WP_Filesystem();
47 + }
59 48
60 - wp_localize_script(
61 - 'wp-block-editor',
62 - 'tableBuilder',
63 - array(
64 - 'plugin_url' => TABLE_BUILDER_BLOCK_PLUGIN_URL,
65 - 'screen' => $hook,
66 - 'api_url' => TABLE_BUILDER_BLOCK_PLUGIN_URL . 'api/',
67 - 'root_url' => esc_url(home_url('/')),
68 - 'rest_url' => esc_url_raw(rest_url()),
69 - 'rest_nonce' => wp_create_nonce('wp_rest'),
70 - 'version' => TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
71 - 'generalSettingsUrl' => admin_url('options-general.php'),
72 - 'activeTheme' => wp_get_theme()->get('Name'),
73 - 'assetUrl' => TABLE_BUILDER_BLOCK_PLUGIN_URL . 'assets/images/',
74 - 'has_pro' => defined('TABLE_BUILDER_BLOCK_PRO_PLUGIN_VERSION'),
75 - )
76 - );
49 + /**
50 + * Enqueues necessary scripts and localizes data for the admin area.
51 + *
52 + * @param string $hook The current page.
53 + * @return void
54 + * @since 1.0.0
55 + */
56 + public function admin_scripts( $hook ) {
57 + // Only load heavy data on editor screens to improve performance.
58 + $is_editor_screen = in_array( $hook, array( 'post.php', 'post-new.php', 'site-editor.php', 'widgets.php' ), true );
77 59
78 - // Localize tablekitEssential only on editor screens and if tablekit-essential plugin is not active
79 - if ($is_editor_screen && !$this->is_tablekit_essential_active()) {
80 - wp_localize_script('wp-block-editor', 'tablekitEssential', RestApi::get_localized_data());
81 - }
60 + wp_localize_script(
61 + 'wp-block-editor',
62 + 'tableBuilder',
63 + array(
64 + 'plugin_url' => TABLE_BUILDER_BLOCK_PLUGIN_URL,
65 + 'screen' => $hook,
66 + 'api_url' => TABLE_BUILDER_BLOCK_PLUGIN_URL . 'api/',
67 + 'root_url' => esc_url( home_url( '/' ) ),
68 + 'rest_url' => esc_url_raw( rest_url() ),
69 + 'rest_nonce' => wp_create_nonce( 'wp_rest' ),
70 + 'version' => TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
71 + 'generalSettingsUrl' => admin_url( 'options-general.php' ),
72 + 'activeTheme' => wp_get_theme()->get( 'Name' ),
73 + 'assetUrl' => TABLE_BUILDER_BLOCK_PLUGIN_URL . 'assets/images/',
74 + 'has_pro' => defined( 'TABLE_BUILDER_BLOCK_PRO_PLUGIN_VERSION' ),
75 + )
76 + );
82 77
83 - if ($hook == 'toplevel_page_tablebuilder') {
84 - $assets = include TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'build/admin/dashboard/index.asset.php';
85 - if (isset($assets['version'])) {
78 + // Localize tablekitEssential only on editor screens and if tablekit-essential plugin is not active.
79 + if ( $is_editor_screen && ! $this->is_tablekit_essential_active() ) {
80 + wp_localize_script( 'wp-block-editor', 'tablekitEssential', RestApi::get_localized_data() );
81 + }
86 82
83 + if ( 'toplevel_page_tablebuilder' === $hook ) {
84 + $assets = include TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'build/admin/dashboard/index.asset.php';
85 + if ( isset( $assets['version'] ) ) {
87 86
88 - // Enqueue the JavaScript
89 - wp_enqueue_script(
90 - 'tablebuilder-admin-dashboard',
91 - TABLE_BUILDER_BLOCK_PLUGIN_URL . 'build/admin/dashboard/index.js',
92 - $assets['dependencies'],
93 - $assets['version'],
94 - true
95 - );
87 + // Enqueue the JavaScript.
88 + wp_enqueue_script(
89 + 'tablebuilder-admin-dashboard',
90 + TABLE_BUILDER_BLOCK_PLUGIN_URL . 'build/admin/dashboard/index.js',
91 + $assets['dependencies'],
92 + $assets['version'],
93 + true
94 + );
96 95
97 - // Enqueue the stylesheet
98 - wp_enqueue_style(
99 - 'tablebuilder-admin-dashboard',
100 - TABLE_BUILDER_BLOCK_PLUGIN_URL . 'build/admin/dashboard/index.css',
101 - array(),
102 - $assets['version']
103 - );
96 + // Enqueue the stylesheet.
97 + wp_enqueue_style(
98 + 'tablebuilder-admin-dashboard',
99 + TABLE_BUILDER_BLOCK_PLUGIN_URL . 'build/admin/dashboard/index.css',
100 + array(),
101 + $assets['version']
102 + );
104 103
105 - wp_localize_script(
106 - 'tablebuilder-admin-dashboard',
107 - 'tableBuilder',
108 - array(
109 - 'plugin_url' => TABLE_BUILDER_BLOCK_PLUGIN_URL,
110 - 'screen' => $hook,
111 - 'adminUrl' => esc_url(admin_url('/')),
112 - 'pluginStatus' => $this->get_onboard_plugin_status(),
113 - 'has_pro' => defined('TABLE_BUILDER_BLOCK_PRO_PLUGIN_VERSION'),
114 - 'version' => TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
115 - 'pro_version' => defined('TABLE_BUILDER_BLOCK_PRO_PLUGIN_VERSION') ? TABLE_BUILDER_BLOCK_PRO_PLUGIN_VERSION : '1.0.0',
116 - )
117 - );
118 - }
119 - }
120 - }
104 + wp_set_script_translations( 'tablebuilder-admin-dashboard', 'table-builder-block', TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'languages' );
121 105
122 - /**
123 - * Check if tablekit-essential is active.
124 - */
125 - private function is_tablekit_essential_active()
126 - {
127 - if (!function_exists('is_plugin_active')) {
128 - require_once ABSPATH . 'wp-admin/includes/plugin.php';
129 - }
130 - return is_plugin_active('tablekit-essential/tablekit-essential.php');
131 - }
106 + wp_localize_script(
107 + 'tablebuilder-admin-dashboard',
108 + 'tableBuilder',
109 + array(
110 + 'plugin_url' => TABLE_BUILDER_BLOCK_PLUGIN_URL,
111 + 'screen' => $hook,
112 + 'adminUrl' => esc_url( admin_url( '/' ) ),
113 + 'pluginStatus' => $this->get_onboard_plugin_status(),
114 + 'has_pro' => defined( 'TABLE_BUILDER_BLOCK_PRO_PLUGIN_VERSION' ),
115 + 'version' => TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
116 + 'pro_version' => defined( 'TABLE_BUILDER_BLOCK_PRO_PLUGIN_VERSION' ) ? TABLE_BUILDER_BLOCK_PRO_PLUGIN_VERSION : '1.0.0',
117 + )
118 + );
119 + }
120 + }
121 + }
132 122
133 - /**
134 - * Build plugin status map for PopupKit-style onboarding steps.
135 - */
136 - private function get_onboard_plugin_status()
137 - {
138 - if (!function_exists('is_plugin_active')) {
139 - require_once ABSPATH . 'wp-admin/includes/plugin.php';
140 - }
123 + /**
124 + * Enqueues the deactivation feedback popup on the plugins.php screen.
125 + *
126 + * @param string $hook The current admin page.
127 + * @return void
128 + */
129 + public function enqueue_deactivation_popup_scripts( $hook ) {
130 + if ( 'plugins.php' !== $hook ) {
131 + return;
132 + }
141 133
142 - $plugins = array(
143 - 'getgenie' => 'getgenie/getgenie.php',
144 - 'gutenkit-blocks-addon' => 'gutenkit-blocks-addon/gutenkit-blocks-addon.php',
145 - 'elementskit-lite' => 'elementskit-lite/elementskit-lite.php',
146 - 'metform' => 'metform/metform.php',
147 - 'shopengine' => 'shopengine/shopengine.php',
148 - 'blocks-for-shopengine' => 'blocks-for-shopengine/shopengine-gutenberg-addon.php',
149 - 'popup-builder-block' => 'popup-builder-block/popup-builder-block.php',
150 - 'wp-social' => 'wp-social/wp-social.php',
151 - 'wp-ultimate-review' => 'wp-ultimate-review/wp-ultimate-review.php',
152 - );
134 + $asset_file = TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'build/admin/deactivation-popup/index.asset.php';
135 + if ( ! file_exists( $asset_file ) ) {
136 + return;
137 + }
153 138
154 - $status = array();
139 + $assets = include $asset_file;
155 140
156 - foreach ($plugins as $slug => $plugin_file) {
157 - $plugin_path = WP_PLUGIN_DIR . '/' . $plugin_file;
141 + wp_enqueue_script(
142 + 'tablebuilder-deactivation-popup',
143 + TABLE_BUILDER_BLOCK_PLUGIN_URL . 'build/admin/deactivation-popup/index.js',
144 + $assets['dependencies'],
145 + $assets['version'],
146 + true
147 + );
158 148
159 - if (!file_exists($plugin_path)) {
160 - $status[$slug] = 'notInstalled';
161 - continue;
162 - }
149 + wp_set_script_translations(
150 + 'tablebuilder-deactivation-popup',
151 + 'table-builder-block',
152 + TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'languages'
153 + );
163 154
164 - $status[$slug] = is_plugin_active($plugin_file) ? 'active' : 'inactive';
165 - }
155 + wp_localize_script(
156 + 'tablebuilder-deactivation-popup',
157 + 'tableBuilderDeactivation',
158 + array(
159 + 'pluginUrl' => TABLE_BUILDER_BLOCK_PLUGIN_URL,
160 + )
161 + );
166 162
167 - return $status;
168 - }
163 + wp_enqueue_style(
164 + 'tablebuilder-deactivation-popup',
165 + TABLE_BUILDER_BLOCK_PLUGIN_URL . 'build/admin/deactivation-popup/index.css',
166 + array(),
167 + $assets['version']
168 + );
169 + }
169 170
170 - // Enqueue block editor assets.
171 - public function blocks_editor_scripts()
172 - {
173 - $scripts = [
174 - 'components' => 'gkit-components',
175 - 'helper' => 'gkit-helpers',
176 - 'global' => 'tablekit-blocks-editor-global',
177 - ];
171 + /**
172 + * Checks if tablekit-essential is active.
173 + *
174 + * @return bool
175 + */
176 + private function is_tablekit_essential_active() {
177 + if ( ! function_exists( 'is_plugin_active' ) ) {
178 + require_once ABSPATH . 'wp-admin/includes/plugin.php';
179 + }
180 + return is_plugin_active( 'tablekit-essential/tablekit-essential.php' );
181 + }
178 182
179 - $shared_handles = ['gkit-components', 'gkit-helpers'];
183 + /**
184 + * Builds a plugin status map for PopupKit-style onboarding steps.
185 + *
186 + * @return array<string,string> Status ('notInstalled'/'inactive'/'active') keyed by plugin slug.
187 + */
188 + private function get_onboard_plugin_status() {
189 + if ( ! function_exists( 'is_plugin_active' ) ) {
190 + require_once ABSPATH . 'wp-admin/includes/plugin.php';
191 + }
180 192
181 - foreach ($scripts as $name => $handle) {
182 - if (in_array($handle, $shared_handles) && wp_script_is($handle, 'registered')) {
183 - continue;
184 - }
193 + $plugins = array(
194 + 'getgenie' => 'getgenie/getgenie.php',
195 + 'gutenkit-blocks-addon' => 'gutenkit-blocks-addon/gutenkit-blocks-addon.php',
196 + 'elementskit-lite' => 'elementskit-lite/elementskit-lite.php',
197 + 'metform' => 'metform/metform.php',
198 + 'shopengine' => 'shopengine/shopengine.php',
199 + 'blocks-for-shopengine' => 'blocks-for-shopengine/shopengine-gutenberg-addon.php',
200 + 'popup-builder-block' => 'popup-builder-block/popup-builder-block.php',
201 + 'wp-social' => 'wp-social/wp-social.php',
202 + 'wp-ultimate-review' => 'wp-ultimate-review/wp-ultimate-review.php',
203 + );
185 204
186 - $asset_path = TABLE_BUILDER_BLOCK_PLUGIN_DIR . "build/tablebuilder/{$name}.asset.php";
187 - $this->enqueue_assets($asset_path, $handle, "{$name}.js");
188 - }
189 - }
205 + $status = array();
190 206
207 + foreach ( $plugins as $slug => $plugin_file ) {
208 + $plugin_path = WP_PLUGIN_DIR . '/' . $plugin_file;
191 209
192 - private function enqueue_assets($asset_file, $handle, $script_file)
193 - {
194 - $this->get_filesystem();
195 - global $wp_filesystem;
210 + if ( ! file_exists( $plugin_path ) ) {
211 + $status[ $slug ] = 'notInstalled';
212 + continue;
213 + }
196 214
197 - if (!isset($wp_filesystem) || !$wp_filesystem->exists($asset_file)) {
198 - return;
199 - }
215 + $status[ $slug ] = is_plugin_active( $plugin_file ) ? 'active' : 'inactive';
216 + }
200 217
201 - // Check if asset file exists
202 - if ($wp_filesystem->exists($asset_file)) {
203 - $asset_data = include $asset_file;
218 + return $status;
219 + }
204 220
205 - // Ensure asset data is valid
206 - if (!empty($asset_data['dependencies']) && is_array($asset_data['dependencies']) && isset($asset_data['version'])) {
207 - wp_enqueue_script(
208 - $handle,
209 - TABLE_BUILDER_BLOCK_PLUGIN_URL . "build/tablebuilder/{$script_file}",
210 - $asset_data['dependencies'],
211 - $asset_data['version'],
212 - true
213 - );
214 - }
215 - }
216 - }
221 + /**
222 + * Enqueues block editor assets: shared component/helper scripts (with
223 + * translations attached if already registered) and the global editor script.
224 + *
225 + * @return void
226 + */
227 + public function blocks_editor_scripts() {
228 + $scripts = array(
229 + 'components' => 'gkit-components',
230 + 'helper' => 'gkit-helpers',
231 + 'global' => 'tablekit-blocks-editor-global',
232 + );
217 233
218 - /**
219 - * Converts custom properties to CSS rules for global presets.
220 - *
221 - * @param array $custom_properties The array of custom CSS properties.
222 - * @return string The generated CSS rules.
223 - */
224 - public function convert_custom_properties($custom_properties)
225 - {
226 - if (empty($custom_properties)) {
227 - return '';
228 - }
234 + $shared_handles = array( 'gkit-components', 'gkit-helpers' );
229 235
230 - $css = array_map(function ($key, $value) {
231 - return "--table-builder-global-{$key}: {$value};";
232 - }, array_keys($custom_properties), $custom_properties);
236 + foreach ( $scripts as $name => $handle ) {
237 + if ( in_array( $handle, $shared_handles, true ) && wp_script_is( $handle, 'registered' ) ) {
238 + wp_set_script_translations( $handle, 'table-builder-block', TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'languages' );
239 + continue;
240 + }
233 241
234 - return "body {" . implode(' ', $css) . "}";
235 - }
242 + $asset_path = TABLE_BUILDER_BLOCK_PLUGIN_DIR . "build/tablebuilder/{$name}.asset.php";
243 + $this->enqueue_assets( $asset_path, $handle, "{$name}.js" );
244 + }
245 + }
236 246
237 - /**
238 - * Print device script in the header for responsive behavior.
239 - */
240 - public function print_device_script_for_window()
241 - {
242 - if (!is_admin()) {
243 - $devices = Utils::get_device_list();
244 - wp_add_inline_script('wp-block-editor', 'var breakpoints = ' . wp_json_encode($devices) . ';', 'before');
245 - }
246 - }
247 247
248 - /**
249 - * Add tableBuilder class to admin/editor body.
250 - */
251 - public function add_body_class($classes)
252 - {
253 - if (is_array($classes)) {
254 - $classes[] = 'tableBuilder-frontend';
255 - } else {
256 - $classes .= ' tableBuilder';
257 - }
258 - return $classes;
259 - }
248 + /**
249 + * Enqueues a build script from its `.asset.php` dependency manifest, and
250 + * attach script translations.
251 + *
252 + * @param string $asset_file Absolute path to the `*.asset.php` manifest.
253 + * @param string $handle Script handle to register.
254 + * @param string $script_file Script filename relative to `build/tablebuilder/`.
255 + * @return void
256 + */
257 + private function enqueue_assets( $asset_file, $handle, $script_file ) {
258 + $this->get_filesystem();
259 + global $wp_filesystem;
260 +
261 + if ( ! isset( $wp_filesystem ) || ! $wp_filesystem->exists( $asset_file ) ) {
262 + return;
263 + }
264 +
265 + // Check if asset file exists.
266 + if ( $wp_filesystem->exists( $asset_file ) ) {
267 + $asset_data = include $asset_file;
268 +
269 + // Ensure asset data is valid.
270 + if ( ! empty( $asset_data['dependencies'] ) && is_array( $asset_data['dependencies'] ) && isset( $asset_data['version'] ) ) {
271 + wp_enqueue_script(
272 + $handle,
273 + TABLE_BUILDER_BLOCK_PLUGIN_URL . "build/tablebuilder/{$script_file}",
274 + $asset_data['dependencies'],
275 + $asset_data['version'],
276 + true
277 + );
278 +
279 + wp_set_script_translations( $handle, 'table-builder-block', TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'languages' );
280 + }
281 + }
282 + }
283 +
284 + /**
285 + * Converts custom properties to CSS rules for global presets.
286 + *
287 + * @param array $custom_properties The array of custom CSS properties.
288 + * @return string The generated CSS rules.
289 + */
290 + public function convert_custom_properties( $custom_properties ) {
291 + if ( empty( $custom_properties ) ) {
292 + return '';
293 + }
294 +
295 + $css = array_map(
296 + function ( $key, $value ) {
297 + return "--table-builder-global-{$key}: {$value};";
298 + },
299 + array_keys( $custom_properties ),
300 + $custom_properties
301 + );
302 +
303 + return 'body {' . implode( ' ', $css ) . '}';
304 + }
305 +
306 + /**
307 + * Deprecated no-op, kept only so external callers don't fatal.
308 + *
309 + * It used to attach a "breakpoints" global to the "wp-block-editor" script
310 + * on the frontend, which no frontend script reads.
311 + *
312 + * @deprecated 2.2.15
313 + * @return void
314 + */
315 + public function print_device_script_for_window() {}
316 +
317 + /**
318 + * Adds a tableBuilder class to the admin/editor body.
319 + *
320 + * @param string[]|string $classes Existing body classes (array for "body_class", string for "admin_body_class").
321 + * @return string[]|string Modified classes, same type as received.
322 + */
323 + public function add_body_class( $classes ) {
324 + if ( is_array( $classes ) ) {
325 + $classes[] = 'tableBuilder-frontend';
326 + } else {
327 + $classes .= ' tableBuilder';
328 + }
329 + return $classes;
330 + }
260 331 }