PluginProbe
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables / trunk
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables vtrunk
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
table-builder-block / includes / Core / Enqueue.php

Enqueue.php in TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables trunk, at includes/Core/Enqueue.php

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