| 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 |
|
| 67 |
/** |
| 68 |
* Enqueue block editor assets. |
| 69 |
* |
| 70 |
* @return void |
| 71 |
* @since 1.0.0 |
| 72 |
*/ |
| 73 |
public function blocks_editor_scripts() { |
| 74 |
$assets = [ |
| 75 |
'components' => TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'build/tablebuilder/components.asset.php', |
| 76 |
'helper' => TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'build/tablebuilder/helper.asset.php', |
| 77 |
'global' => TABLE_BUILDER_BLOCK_PLUGIN_DIR . 'build/tablebuilder/global.asset.php', |
| 78 |
]; |
| 79 |
|
| 80 |
foreach ($assets as $handle => $asset_path) { |
| 81 |
$this->enqueue_assets($asset_path, "table_builder-block-editor-{$handle}", "{$handle}.js"); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
private function enqueue_assets($asset_file, $handle, $script_file) { |
| 86 |
$this->get_filesystem(); |
| 87 |
global $wp_filesystem; |
| 88 |
|
| 89 |
if (!isset($wp_filesystem) || !$wp_filesystem->exists($asset_file)) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
// Check if asset file exists |
| 94 |
if ($wp_filesystem->exists($asset_file)) { |
| 95 |
$asset_data = include $asset_file; |
| 96 |
|
| 97 |
// Ensure asset data is valid |
| 98 |
if (!empty($asset_data['dependencies']) && is_array($asset_data['dependencies']) && isset($asset_data['version'])) { |
| 99 |
wp_enqueue_script( |
| 100 |
$handle, |
| 101 |
TABLE_BUILDER_BLOCK_PLUGIN_URL . "build/tablebuilder/{$script_file}", |
| 102 |
$asset_data['dependencies'], |
| 103 |
$asset_data['version'], |
| 104 |
true |
| 105 |
); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Converts custom properties to CSS rules for global presets. |
| 112 |
* |
| 113 |
* @param array $custom_properties The array of custom CSS properties. |
| 114 |
* @return string The generated CSS rules. |
| 115 |
*/ |
| 116 |
public function convert_custom_properties($custom_properties) { |
| 117 |
if (empty($custom_properties)) { |
| 118 |
return ''; |
| 119 |
} |
| 120 |
|
| 121 |
$css = array_map(function ($key, $value) { |
| 122 |
return "--table-builder-global-{$key}: {$value};"; |
| 123 |
}, array_keys($custom_properties), $custom_properties); |
| 124 |
|
| 125 |
return "body {" . implode(' ', $css) . "}"; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Print device script in the header for responsive behavior. |
| 130 |
*/ |
| 131 |
public function print_device_script_for_window() { |
| 132 |
if (!is_admin()) { |
| 133 |
$devices = Utils::get_device_list(); |
| 134 |
wp_add_inline_script('wp-block-editor', 'var breakpoints = ' . wp_json_encode($devices) . ';', 'before'); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|