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
table-builder-block / includes / Core / Enqueue.php

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

332 lines 10.1 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 /**
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 }
332