| @@ -1,9 +1,14 @@ | ||
| 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 | 12 | use TableBuilder\Helpers\Utils; |
| 8 | 13 | |
| 9 | 14 | /** |
| @@ -11,212 +16,320 @@ | ||
| 11 | 16 | * |
| 12 | 17 | * @since 1.0.0 |
| 13 | 18 | * @access public |
| 14 | 19 | */ |
| 15 | -class Enqueue | |
| 16 | -{ | |
| 17 | - use \TableBuilder\Traits\Singleton; | |
| 20 | +class Enqueue { | |
| 18 | 21 | |
| 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 | - } | |
| 22 | + use \TableBuilder\Traits\Singleton; | |
| 34 | 23 | |
| 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 | - } | |
| 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 | + } | |
| 47 | 39 | |
| 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')); | |
| 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 | + } | |
| 59 | 51 | |
| 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 | - ); | |
| 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 ); | |
| 77 | 62 | |
| 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 | - } | |
| 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 | + ); | |
| 82 | 80 | |
| 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'])) { | |
| 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 | + } | |
| 86 | 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'] ) ) { | |
| 87 | 89 | |
| 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 | - ); | |
| 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 | + ); | |
| 96 | 98 | |
| 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 | - ); | |
| 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 | + ); | |
| 104 | 106 | |
| 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 | - 'has_pro' => defined('TABLE_BUILDER_BLOCK_PRO_PLUGIN_VERSION'), | |
| 113 | - 'version' => TABLE_BUILDER_BLOCK_PLUGIN_VERSION, | |
| 114 | - 'pro_version' => defined('TABLE_BUILDER_BLOCK_PRO_PLUGIN_VERSION') ? TABLE_BUILDER_BLOCK_PRO_PLUGIN_VERSION : '1.0.0', | |
| 115 | - ) | |
| 116 | - ); | |
| 117 | - } | |
| 118 | - } | |
| 119 | - } | |
| 107 | + wp_set_script_translations( 'tablebuilder-admin-dashboard', 'table-builder-block', TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'languages' ); | |
| 120 | 108 | |
| 121 | - /** | |
| 122 | - * Check if tablekit-essential is active. | |
| 123 | - */ | |
| 124 | - private function is_tablekit_essential_active() | |
| 125 | - { | |
| 126 | - if (!function_exists('is_plugin_active')) { | |
| 127 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 128 | - } | |
| 129 | - return is_plugin_active('tablekit-essential/tablekit-essential.php'); | |
| 130 | - } | |
| 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 | + } | |
| 131 | 125 | |
| 132 | - // Enqueue block editor assets. | |
| 133 | - public function blocks_editor_scripts() | |
| 134 | - { | |
| 135 | - $scripts = [ | |
| 136 | - 'components' => 'gkit-components', | |
| 137 | - 'helper' => 'gkit-helpers', | |
| 138 | - 'global' => 'tablekit-blocks-editor-global', | |
| 139 | - ]; | |
| 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 | + } | |
| 140 | 136 | |
| 141 | - $shared_handles = ['gkit-components', 'gkit-helpers']; | |
| 137 | + $asset_file = TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'build/admin/deactivation-popup/index.asset.php'; | |
| 138 | + if ( ! file_exists( $asset_file ) ) { | |
| 139 | + return; | |
| 140 | + } | |
| 142 | 141 | |
| 143 | - foreach ($scripts as $name => $handle) { | |
| 144 | - if (in_array($handle, $shared_handles) && wp_script_is($handle, 'registered')) { | |
| 145 | - continue; | |
| 146 | - } | |
| 142 | + $assets = include $asset_file; | |
| 147 | 143 | |
| 148 | - $asset_path = TABLE_BUILDER_BLOCK_PLUGIN_DIR . "build/tablebuilder/{$name}.asset.php"; | |
| 149 | - $this->enqueue_assets($asset_path, $handle, "{$name}.js"); | |
| 150 | - } | |
| 151 | - } | |
| 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 | + ); | |
| 152 | 151 | |
| 152 | + wp_set_script_translations( | |
| 153 | + 'tablebuilder-deactivation-popup', | |
| 154 | + 'table-builder-block', | |
| 155 | + TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'languages' | |
| 156 | + ); | |
| 153 | 157 | |
| 154 | - private function enqueue_assets($asset_file, $handle, $script_file) | |
| 155 | - { | |
| 156 | - $this->get_filesystem(); | |
| 157 | - global $wp_filesystem; | |
| 158 | + wp_localize_script( | |
| 159 | + 'tablebuilder-deactivation-popup', | |
| 160 | + 'tableBuilderDeactivation', | |
| 161 | + array( | |
| 162 | + 'pluginUrl' => TABLE_BUILDER_BLOCK_PLUGIN_URL, | |
| 163 | + ) | |
| 164 | + ); | |
| 158 | 165 | |
| 159 | - if (!isset($wp_filesystem) || !$wp_filesystem->exists($asset_file)) { | |
| 160 | - return; | |
| 161 | - } | |
| 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 | + } | |
| 162 | 173 | |
| 163 | - // Check if asset file exists | |
| 164 | - if ($wp_filesystem->exists($asset_file)) { | |
| 165 | - $asset_data = include $asset_file; | |
| 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 | + } | |
| 166 | 185 | |
| 167 | - // Ensure asset data is valid | |
| 168 | - if (!empty($asset_data['dependencies']) && is_array($asset_data['dependencies']) && isset($asset_data['version'])) { | |
| 169 | - wp_enqueue_script( | |
| 170 | - $handle, | |
| 171 | - TABLE_BUILDER_BLOCK_PLUGIN_URL . "build/tablebuilder/{$script_file}", | |
| 172 | - $asset_data['dependencies'], | |
| 173 | - $asset_data['version'], | |
| 174 | - true | |
| 175 | - ); | |
| 176 | - } | |
| 177 | - } | |
| 178 | - } | |
| 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 | + } | |
| 179 | 195 | |
| 180 | - /** | |
| 181 | - * Converts custom properties to CSS rules for global presets. | |
| 182 | - * | |
| 183 | - * @param array $custom_properties The array of custom CSS properties. | |
| 184 | - * @return string The generated CSS rules. | |
| 185 | - */ | |
| 186 | - public function convert_custom_properties($custom_properties) | |
| 187 | - { | |
| 188 | - if (empty($custom_properties)) { | |
| 189 | - return ''; | |
| 190 | - } | |
| 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 | + ); | |
| 191 | 207 | |
| 192 | - $css = array_map(function ($key, $value) { | |
| 193 | - return "--table-builder-global-{$key}: {$value};"; | |
| 194 | - }, array_keys($custom_properties), $custom_properties); | |
| 208 | + $status = array(); | |
| 195 | 209 | |
| 196 | - return "body {" . implode(' ', $css) . "}"; | |
| 197 | - } | |
| 210 | + foreach ( $plugins as $slug => $plugin_file ) { | |
| 211 | + $plugin_path = WP_PLUGIN_DIR . '/' . $plugin_file; | |
| 198 | 212 | |
| 199 | - /** | |
| 200 | - * Print device script in the header for responsive behavior. | |
| 201 | - */ | |
| 202 | - public function print_device_script_for_window() | |
| 203 | - { | |
| 204 | - if (!is_admin()) { | |
| 205 | - $devices = Utils::get_device_list(); | |
| 206 | - wp_add_inline_script('wp-block-editor', 'var breakpoints = ' . wp_json_encode($devices) . ';', 'before'); | |
| 207 | - } | |
| 208 | - } | |
| 213 | + if ( ! file_exists( $plugin_path ) ) { | |
| 214 | + $status[ $slug ] = 'notInstalled'; | |
| 215 | + continue; | |
| 216 | + } | |
| 209 | 217 | |
| 210 | - /** | |
| 211 | - * Add tableBuilder class to admin/editor body. | |
| 212 | - */ | |
| 213 | - public function add_body_class($classes) | |
| 214 | - { | |
| 215 | - if (is_array($classes)) { | |
| 216 | - $classes[] = 'tableBuilder-frontend'; | |
| 217 | - } else { | |
| 218 | - $classes .= ' tableBuilder'; | |
| 219 | - } | |
| 220 | - return $classes; | |
| 221 | - } | |
| 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 | + } | |
| 222 | 335 | } |