| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The file register assets for the plugin |
| 5 |
* |
| 6 |
* @package Tableberg |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Tableberg; |
| 10 |
|
| 11 |
/** |
| 12 |
* Handle plugin assets |
| 13 |
*/ |
| 14 |
class Assets |
| 15 |
{ |
| 16 |
/** |
| 17 |
* We will store dynamically generated styles here & render them |
| 18 |
* in wp_footer call |
| 19 |
*/ |
| 20 |
public static $dynamicStyles = ''; |
| 21 |
/** |
| 22 |
* Register block assets for frontend. |
| 23 |
* I.e. dynamic responsiveness |
| 24 |
*/ |
| 25 |
public function register_frontend_assets() |
| 26 |
{ |
| 27 |
wp_enqueue_script( |
| 28 |
'tableberg-frontend-script', |
| 29 |
TABLEBERG_URL . 'includes/assets/js/frontend.js', |
| 30 |
[], |
| 31 |
Constants::plugin_version(), |
| 32 |
true |
| 33 |
); |
| 34 |
add_action('wp_footer', function () { |
| 35 |
if (self::$dynamicStyles) { |
| 36 |
echo '<style>' . self::$dynamicStyles . '</style>'; |
| 37 |
self::$dynamicStyles = ''; |
| 38 |
} |
| 39 |
}); |
| 40 |
} |
| 41 |
/** |
| 42 |
* Register blocks assets |
| 43 |
*/ |
| 44 |
public function register_blocks_assets() |
| 45 |
{ |
| 46 |
wp_register_script( |
| 47 |
'tableberg-script', |
| 48 |
TABLEBERG_URL . 'build/tableberg.build.js', |
| 49 |
array( |
| 50 |
'lodash', |
| 51 |
'react', |
| 52 |
'wp-block-editor', |
| 53 |
'wp-blocks', |
| 54 |
'wp-components', |
| 55 |
'wp-data', |
| 56 |
'wp-element', |
| 57 |
'wp-i18n', |
| 58 |
'wp-primitives', |
| 59 |
), |
| 60 |
Constants::plugin_version(), |
| 61 |
false |
| 62 |
); |
| 63 |
wp_register_style( |
| 64 |
'tableberg-editor-style', |
| 65 |
TABLEBERG_URL . 'build/tableberg-editor-style.css', |
| 66 |
array(), |
| 67 |
Constants::plugin_version(), |
| 68 |
false |
| 69 |
); |
| 70 |
wp_register_style( |
| 71 |
'tableberg-style', |
| 72 |
TABLEBERG_URL . 'build/tableberg-frontend-style.css', |
| 73 |
array(), |
| 74 |
Constants::plugin_version(), |
| 75 |
false |
| 76 |
); |
| 77 |
|
| 78 |
self::pass_data_to_js('tableberg-script'); |
| 79 |
} |
| 80 |
/** |
| 81 |
* Enqueue Admin assets |
| 82 |
*/ |
| 83 |
public function register_admin_assets() |
| 84 |
{ |
| 85 |
wp_enqueue_script( |
| 86 |
'tableberg-admin-script', |
| 87 |
TABLEBERG_URL . 'includes/Admin/assets/tableberg-admin.build.js', |
| 88 |
array( |
| 89 |
'lodash', |
| 90 |
'react', |
| 91 |
'wp-block-editor', |
| 92 |
'wp-blocks', |
| 93 |
'wp-components', |
| 94 |
'wp-data', |
| 95 |
'wp-element', |
| 96 |
'wp-i18n', |
| 97 |
'wp-primitives', |
| 98 |
), |
| 99 |
Constants::plugin_version(), |
| 100 |
true |
| 101 |
); |
| 102 |
|
| 103 |
self::pass_data_to_js('tableberg-admin-script'); |
| 104 |
|
| 105 |
wp_enqueue_script( |
| 106 |
'tableberg-preview-device-change-observer', |
| 107 |
TABLEBERG_URL . 'includes/assets/js/PreviewDeviceChangeObserver.js', |
| 108 |
[], |
| 109 |
Constants::plugin_version(), |
| 110 |
true |
| 111 |
); |
| 112 |
$frontend_script_data = apply_filters('tableberg/filter/admin_settings_menu_data', array()); |
| 113 |
wp_localize_script('tableberg-admin-script', 'tablebergAdminMenuData', $frontend_script_data); |
| 114 |
wp_enqueue_style( |
| 115 |
'tableberg-admin-style', |
| 116 |
TABLEBERG_URL . 'includes/Admin/assets/tableberg-admin-style.css', |
| 117 |
array(), |
| 118 |
Constants::plugin_version(), |
| 119 |
'all' |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
private static function pass_data_to_js(string $handle) |
| 125 |
{ |
| 126 |
$data = [ |
| 127 |
'plugin_url' => TABLEBERG_URL |
| 128 |
]; |
| 129 |
global $tp_fs; |
| 130 |
if (isset($tp_fs)) { |
| 131 |
$data['IS_PRO'] = $tp_fs->is__premium_only() |
| 132 |
&& $tp_fs->can_use_premium_code(); |
| 133 |
} else { |
| 134 |
$data['IS_PRO'] = false; |
| 135 |
} |
| 136 |
wp_localize_script($handle, 'TABLEBERG_CFG', $data); |
| 137 |
} |
| 138 |
} |
| 139 |
|