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 +331 -174 2.1.0 → 2.3.0 View file →
@@ -1,174 +1,331 @@
1 -<?php
2 -namespace TableBuilder\Core;
3 -
4 -defined('ABSPATH') || exit;
5 -
6 -use TableBuilder\Helpers\Utils;
7 -
8 -/**
9 - * Enqueue registrar.
10 - *
11 - * @since 1.0.0
12 - * @access public
13 - */
14 -class Enqueue {
15 - use \TableBuilder\Traits\Singleton;
16 -
17 - /**
18 - * Class constructor.
19 - * private for singleton
20 - *
21 - * @return void
22 - * @since 1.0.0
23 - */
24 - public function __construct() {
25 - add_action('admin_enqueue_scripts', array($this, 'admin_scripts'));
26 - // add_action('enqueue_block_assets', array($this, 'blocks_scripts'));
27 - add_action('enqueue_block_editor_assets', array($this, 'blocks_editor_scripts'), 5);
28 - add_action('wp_head', array($this, 'print_device_script_for_window'));
29 - }
30 -
31 - /**
32 - * Get WP filesystem
33 - *
34 - * @return void
35 - */
36 - protected function get_filesystem() {
37 - if (!function_exists('WP_Filesystem')) {
38 - require_once ABSPATH . 'wp-admin/includes/file.php';
39 - }
40 - WP_Filesystem();
41 - }
42 -
43 - /**
44 - * Enqueues necessary scripts and localizes data for the admin area.
45 - *
46 - * @param string $hook The current page.
47 - * @return void
48 - * @since 1.0.0
49 - */
50 - public function admin_scripts($hook) {
51 - wp_localize_script(
52 - 'wp-block-editor',
53 - 'tableBuilder',
54 - array(
55 - 'plugin_url' => TABLE_BUILDER_BLOCK_PLUGIN_URL,
56 - 'screen' => $hook,
57 - 'api_url' => TABLE_BUILDER_BLOCK_PLUGIN_URL . 'api/',
58 - 'root_url' => esc_url(home_url('/')),
59 - 'version' => TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
60 - 'generalSettingsUrl' => admin_url('options-general.php'),
61 - 'activeTheme' => wp_get_theme()->get('Name'),
62 - 'assetUrl' => TABLE_BUILDER_BLOCK_PLUGIN_URL . 'assets/images/',
63 - )
64 - );
65 -
66 - if($hook == 'toplevel_page_tablebuilder') {
67 - $assets = include TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'build/admin/dashboard/index.asset.php';
68 - if ( isset( $assets['version'] ) ) {
69 -
70 -
71 - // Enqueue the JavaScript
72 - wp_enqueue_script(
73 - 'tablebuilder-admin-dashboard',
74 - TABLE_BUILDER_BLOCK_PLUGIN_URL . 'build/admin/dashboard/index.js',
75 - $assets['dependencies'],
76 - $assets['version'],
77 - true
78 - );
79 -
80 - // Enqueue the stylesheet
81 - wp_enqueue_style(
82 - 'tablebuilder-admin-dashboard',
83 - TABLE_BUILDER_BLOCK_PLUGIN_URL . 'build/admin/dashboard/index.css',
84 - array(),
85 - $assets['version']
86 - );
87 -
88 - wp_localize_script(
89 - 'tablebuilder-admin-dashboard',
90 - 'tableBuilder',
91 - array(
92 - 'plugin_url' => TABLE_BUILDER_BLOCK_PLUGIN_URL,
93 - 'screen' => $hook,
94 - 'adminUrl' => esc_url(admin_url('/')),
95 - 'has_pro' => defined('TABLE_BUILDER_BLOCK_PRO_PLUGIN_VERSION'),
96 - 'version' => TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
97 - 'pro_version' => defined('TABLE_BUILDER_BLOCK_PRO_PLUGIN_VERSION') ? TABLE_BUILDER_BLOCK_PRO_PLUGIN_VERSION : '1.0.0',
98 - )
99 - );
100 - }
101 - }
102 - }
103 -
104 - /**
105 - * Enqueue block editor assets.
106 - *
107 - * @return void
108 - * @since 1.0.0
109 - */
110 - public function blocks_editor_scripts() {
111 - $assets = [
112 - 'components' => TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'build/tablebuilder/components.asset.php',
113 - 'helper' => TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'build/tablebuilder/helper.asset.php',
114 - 'global' => TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'build/tablebuilder/global.asset.php',
115 - ];
116 -
117 - foreach ($assets as $handle => $asset_path) {
118 - $this->enqueue_assets($asset_path, "table_builder-block-editor-{$handle}", "{$handle}.js");
119 - }
120 - }
121 -
122 - private function enqueue_assets($asset_file, $handle, $script_file) {
123 - $this->get_filesystem();
124 - global $wp_filesystem;
125 -
126 - if (!isset($wp_filesystem) || !$wp_filesystem->exists($asset_file)) {
127 - return;
128 - }
129 -
130 - // Check if asset file exists
131 - if ($wp_filesystem->exists($asset_file)) {
132 - $asset_data = include $asset_file;
133 -
134 - // Ensure asset data is valid
135 - if (!empty($asset_data['dependencies']) && is_array($asset_data['dependencies']) && isset($asset_data['version'])) {
136 - wp_enqueue_script(
137 - $handle,
138 - TABLE_BUILDER_BLOCK_PLUGIN_URL . "build/tablebuilder/{$script_file}",
139 - $asset_data['dependencies'],
140 - $asset_data['version'],
141 - true
142 - );
143 - }
144 - }
145 - }
146 -
147 - /**
148 - * Converts custom properties to CSS rules for global presets.
149 - *
150 - * @param array $custom_properties The array of custom CSS properties.
151 - * @return string The generated CSS rules.
152 - */
153 - public function convert_custom_properties($custom_properties) {
154 - if (empty($custom_properties)) {
155 - return '';
156 - }
157 -
158 - $css = array_map(function ($key, $value) {
159 - return "--table-builder-global-{$key}: {$value};";
160 - }, array_keys($custom_properties), $custom_properties);
161 -
162 - return "body {" . implode(' ', $css) . "}";
163 - }
164 -
165 - /**
166 - * Print device script in the header for responsive behavior.
167 - */
168 - public function print_device_script_for_window() {
169 - if (!is_admin()) {
170 - $devices = Utils::get_device_list();
171 - wp_add_inline_script('wp-block-editor', 'var breakpoints = ' . wp_json_encode($devices) . ';', 'before');
172 - }
173 - }
174 -}
1 +<?php
2 +/**
3 + * Frontend and admin asset enqueue registrar
4 + *
5 + * @package TableKit
6 + */
7 +
8 +namespace TableBuilder\Core;
9 +
10 +defined( 'ABSPATH' ) || exit;
11 +
12 +/**
13 + * Enqueue registrar.
14 + *
15 + * @since 1.0.0
16 + * @access public
17 + */
18 +class Enqueue {
19 +
20 + use \TableBuilder\Traits\Singleton;
21 +
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 + }
36 +
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 + }
48 +
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 );
59 +
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 + );
77 +
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 + }
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'] ) ) {
86 +
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 + );
95 +
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 + );
103 +
104 + wp_set_script_translations( 'tablebuilder-admin-dashboard', 'table-builder-block', TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'languages' );
105 +
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 + }
122 +
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 + }
133 +
134 + $asset_file = TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'build/admin/deactivation-popup/index.asset.php';
135 + if ( ! file_exists( $asset_file ) ) {
136 + return;
137 + }
138 +
139 + $assets = include $asset_file;
140 +
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 + );
148 +
149 + wp_set_script_translations(
150 + 'tablebuilder-deactivation-popup',
151 + 'table-builder-block',
152 + TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'languages'
153 + );
154 +
155 + wp_localize_script(
156 + 'tablebuilder-deactivation-popup',
157 + 'tableBuilderDeactivation',
158 + array(
159 + 'pluginUrl' => TABLE_BUILDER_BLOCK_PLUGIN_URL,
160 + )
161 + );
162 +
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 + }
170 +
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 + }
182 +
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 + }
192 +
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 + );
204 +
205 + $status = array();
206 +
207 + foreach ( $plugins as $slug => $plugin_file ) {
208 + $plugin_path = WP_PLUGIN_DIR . '/' . $plugin_file;
209 +
210 + if ( ! file_exists( $plugin_path ) ) {
211 + $status[ $slug ] = 'notInstalled';
212 + continue;
213 + }
214 +
215 + $status[ $slug ] = is_plugin_active( $plugin_file ) ? 'active' : 'inactive';
216 + }
217 +
218 + return $status;
219 + }
220 +
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 + );
233 +
234 + $shared_handles = array( 'gkit-components', 'gkit-helpers' );
235 +
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 + }
241 +
242 + $asset_path = TABLE_BUILDER_BLOCK_PLUGIN_DIR . "build/tablebuilder/{$name}.asset.php";
243 + $this->enqueue_assets( $asset_path, $handle, "{$name}.js" );
244 + }
245 + }
246 +
247 +
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 + }
331 +}