| 1 |
<?php |
| 2 |
namespace ABlocks; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
use ABlocks\Classes\FileUpload; |
| 9 |
use ABlocks\Classes\AssetsGenerator; |
| 10 |
use ABlocks\Classes\RegisterScripts; |
| 11 |
use ABlocks\Admin\Menu; |
| 12 |
use ABlocks\Helper; |
| 13 |
|
| 14 |
class Assets { |
| 15 |
|
| 16 |
public static function init() { |
| 17 |
$self = new self(); |
| 18 |
add_action( 'admin_enqueue_scripts', [ $self, 'dashboard_scripts' ], 10 ); |
| 19 |
add_action( 'admin_enqueue_scripts', [ $self, 'demo_importer_scripts' ], 10 ); |
| 20 |
add_action( 'enqueue_block_assets', [ $self, 'block_editor_assets' ] ); |
| 21 |
add_action( 'enqueue_block_assets', [ $self, 'register_scripts' ] ); |
| 22 |
add_action( 'wp_enqueue_scripts', [ $self, 'front_end_google_fonts' ] ); |
| 23 |
|
| 24 |
add_action( 'wp_enqueue_scripts', [ $self, 'enqueue_frontend_assets' ], 99 ); |
| 25 |
add_action( 'wp_enqueue_scripts', [ $self, 'enqueue_frontend_localize_script' ] ); |
| 26 |
add_action( 'wp', [ $self, 'regenerate_missing_assets' ] ); |
| 27 |
// Global CSS |
| 28 |
add_action( 'wp_enqueue_block_assets', [ $self, 'global_css_variable' ], 999 ); |
| 29 |
add_action( 'wp_enqueue_scripts', [ $self, 'global_css_variable' ], 999 ); |
| 30 |
add_action( 'enqueue_block_editor_assets', [ $self, 'global_css_variable' ], 999 ); |
| 31 |
} |
| 32 |
|
| 33 |
public function get_localize_script_data() { |
| 34 |
return [ |
| 35 |
'rest_url' => esc_url_raw( rest_url() ), |
| 36 |
'namespace' => ABLOCKS_PLUGIN_SLUG . '/v1/', |
| 37 |
'plugin_root_url' => ABLOCKS_ROOT_URL, |
| 38 |
'plugin_root_path' => ABLOCKS_ROOT_DIR_PATH, |
| 39 |
'admin_url' => admin_url(), |
| 40 |
'site_url' => site_url(), |
| 41 |
'route_path' => wp_parse_url( admin_url(), PHP_URL_PATH ), |
| 42 |
'ajax_url' => esc_url( admin_url( 'admin-ajax.php' ) ), |
| 43 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 44 |
'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ), |
| 45 |
'is_pro' => (bool) Helper::is_active_ablocks_pro(), |
| 46 |
]; |
| 47 |
} |
| 48 |
|
| 49 |
public function get_dashboard_localize_script_data() { |
| 50 |
$menu = new Menu(); |
| 51 |
$args = array( |
| 52 |
'menu' => wp_json_encode( Helper::get_admin_menu_list() ), |
| 53 |
'toplevel_menu_icon_url' => $menu->get_toplevel_menu_icon_url(), |
| 54 |
'settings' => [ |
| 55 |
'default_container_width' => Helper::get_settings( 'default_container_width', 1280 ), |
| 56 |
'container_padding' => Helper::get_settings( 'container_padding', 10 ), |
| 57 |
'container_element_gap' => Helper::get_settings( 'container_element_gap', 20 ), |
| 58 |
'enabled_assets_file_generation' => (bool) Helper::get_settings( 'enabled_assets_file_generation', false ), |
| 59 |
'enabled_block_copy_paste_style' => (bool) Helper::get_settings( 'enabled_block_copy_paste_style', false ), |
| 60 |
'enabled_only_selected_fonts' => (bool) Helper::get_settings( 'enabled_only_selected_fonts', false ), |
| 61 |
'selected_fonts' => (array) Helper::get_settings( 'selected_fonts', [] ), |
| 62 |
], |
| 63 |
'is_gutenberg_editor' => Helper::is_gutenberg_editor(), |
| 64 |
'is_fse_theme' => Helper::is_fse_theme(), |
| 65 |
'third_party_plugin_status' => [ |
| 66 |
'academy_lms' => Helper::is_active_academy(), |
| 67 |
'storeengine' => Helper::is_active_storeengine(), |
| 68 |
'wp_map_block' => Helper::is_active_wp_map_block(), |
| 69 |
] |
| 70 |
); |
| 71 |
return apply_filters( |
| 72 |
'ablocks/assets/dashboard_scripts_data', |
| 73 |
array_merge( |
| 74 |
$this->get_localize_script_data(), |
| 75 |
$args |
| 76 |
) |
| 77 |
); |
| 78 |
} |
| 79 |
public function get_editor_localize_script_data() { |
| 80 |
global $ablocks_blocks; |
| 81 |
$args = array( |
| 82 |
'settings' => [ |
| 83 |
'default_container_width' => Helper::get_settings( 'default_container_width', 1280 ), |
| 84 |
'container_padding' => Helper::get_settings( 'container_padding', 10 ), |
| 85 |
'container_element_gap' => Helper::get_settings( 'container_element_gap', 20 ), |
| 86 |
'enabled_assets_file_generation' => (bool) Helper::get_settings( 'enabled_assets_file_generation', false ), |
| 87 |
'enabled_block_copy_paste_style' => (bool) Helper::get_settings( 'enabled_block_copy_paste_style', false ), |
| 88 |
'enabled_only_selected_fonts' => (bool) Helper::get_settings( 'enabled_only_selected_fonts', false ), |
| 89 |
'selected_fonts' => (array) Helper::get_settings( 'selected_fonts', [] ), |
| 90 |
], |
| 91 |
'is_gutenberg_editor' => Helper::is_gutenberg_editor(), |
| 92 |
'has_required_block_attribute_migration' => get_option( 'ablocks_has_required_block_attribute_migration' ), |
| 93 |
'third_party_plugin_status' => [ |
| 94 |
'academy_lms' => Helper::is_active_academy(), |
| 95 |
'storeengine' => Helper::is_active_storeengine(), |
| 96 |
'wp_map_block' => Helper::is_active_wp_map_block(), |
| 97 |
], |
| 98 |
'blocks_status' => $ablocks_blocks |
| 99 |
); |
| 100 |
return apply_filters( |
| 101 |
'ablocks/assets/editor_scripts_data', |
| 102 |
array_merge( |
| 103 |
$this->get_localize_script_data(), |
| 104 |
$args |
| 105 |
) |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
public function dashboard_scripts( $hook ) { |
| 110 |
$demo_config = Helper::get_theme_demo_config(); |
| 111 |
if ( strpos( $hook, '_page_' . ABLOCKS_PLUGIN_SLUG ) !== false && strpos( $hook, $demo_config['menu_slug'] ) === false ) { |
| 112 |
// Remove Notices |
| 113 |
remove_all_actions( 'admin_notices' ); |
| 114 |
// dequeue third party plugin assets |
| 115 |
add_action( |
| 116 |
'wp_print_scripts', |
| 117 |
function () { |
| 118 |
$isSkip = apply_filters( 'ablocks/skip_no_conflict_backend_scripts', Helper::is_dev_mode_enable() ); |
| 119 |
|
| 120 |
if ( $isSkip ) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
global $wp_scripts; |
| 125 |
if ( ! $wp_scripts ) { |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
$pluginUrl = plugins_url(); |
| 130 |
foreach ( $wp_scripts->queue as $script ) { |
| 131 |
$src = $wp_scripts->registered[ $script ]->src; |
| 132 |
if ( strpos( $src, $pluginUrl ) !== false && ! strpos( $src, ABLOCKS_PLUGIN_SLUG ) !== false ) { |
| 133 |
wp_dequeue_script( $wp_scripts->registered[ $script ]->handle ); |
| 134 |
} |
| 135 |
} |
| 136 |
}, |
| 137 |
1 |
| 138 |
); |
| 139 |
|
| 140 |
wp_enqueue_style( 'ablocks-fonts', $this->web_fonts_url( 'Roboto:ital,wght@0,300;0,400;0,500;0,700;1,300&display=swap' ), array(), ABLOCKS_VERSION ); |
| 141 |
wp_enqueue_style( 'ablocks-icon', ABLOCKS_ASSETS_URL . 'library/css/ablocks-icon/style.css', array( 'wp-components' ), filemtime( ABLOCKS_ASSETS_PATH . 'library/css/ablocks-icon/style.css' ), 'all' ); |
| 142 |
wp_enqueue_style( 'ablocks-dashboard-style', ABLOCKS_ASSETS_URL . 'build/dashboard.css', array( 'wp-components' ), filemtime( ABLOCKS_ASSETS_PATH . 'build/dashboard.css' ), 'all' ); |
| 143 |
|
| 144 |
$dependencies = include ABLOCKS_ASSETS_PATH . 'build/dashboard.asset.php'; |
| 145 |
wp_enqueue_script( |
| 146 |
'ablocks-dashboard-scripts', |
| 147 |
ABLOCKS_ASSETS_URL . 'build/dashboard.js', |
| 148 |
$dependencies['dependencies'], |
| 149 |
$dependencies['version'], |
| 150 |
true |
| 151 |
); |
| 152 |
wp_localize_script( 'ablocks-dashboard-scripts', 'ABlocksGlobal', $this->get_dashboard_localize_script_data() ); |
| 153 |
wp_set_script_translations( 'ablocks-dashboard-scripts', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' ); |
| 154 |
}//end if |
| 155 |
} |
| 156 |
|
| 157 |
public function demo_importer_scripts( $hook ) { |
| 158 |
$demo_config = Helper::get_theme_demo_config(); |
| 159 |
if ( strpos( $hook, $demo_config['menu_slug'] ) !== false ) { |
| 160 |
// Remove Notices |
| 161 |
remove_all_actions( 'admin_notices' ); |
| 162 |
// dequeue third party plugin assets |
| 163 |
add_action( |
| 164 |
'wp_print_scripts', |
| 165 |
function () { |
| 166 |
$isSkip = apply_filters( 'ablocks/skip_no_conflict_demo_importer_scripts', Helper::is_dev_mode_enable() ); |
| 167 |
|
| 168 |
if ( $isSkip ) { |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
global $wp_scripts; |
| 173 |
if ( ! $wp_scripts ) { |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
$pluginUrl = plugins_url(); |
| 178 |
foreach ( $wp_scripts->queue as $script ) { |
| 179 |
$src = $wp_scripts->registered[ $script ]->src; |
| 180 |
if ( strpos( $src, $pluginUrl ) !== false && ! strpos( $src, ABLOCKS_PLUGIN_SLUG ) !== false ) { |
| 181 |
wp_dequeue_script( $wp_scripts->registered[ $script ]->handle ); |
| 182 |
} |
| 183 |
} |
| 184 |
}, |
| 185 |
1 |
| 186 |
); |
| 187 |
|
| 188 |
wp_enqueue_style( 'ablocks-fonts', $this->web_fonts_url( 'Roboto:ital,wght@0,300;0,400;0,500;0,700;1,300&display=swap' ), array(), ABLOCKS_VERSION ); |
| 189 |
wp_enqueue_style( 'ablocks-icon', ABLOCKS_ASSETS_URL . 'library/css/ablocks-icon/style.css', array( 'wp-components' ), filemtime( ABLOCKS_ASSETS_PATH . 'library/css/ablocks-icon/style.css' ), 'all' ); |
| 190 |
wp_enqueue_style( 'ablocks-dashboard-style', ABLOCKS_ASSETS_URL . 'build/demo-import.css', array( 'wp-components' ), filemtime( ABLOCKS_ASSETS_PATH . 'build/demo-import.css' ), 'all' ); |
| 191 |
|
| 192 |
$dependencies = include ABLOCKS_ASSETS_PATH . 'build/demo-import.asset.php'; |
| 193 |
wp_enqueue_script( |
| 194 |
'ablocks-demo-import-scripts', |
| 195 |
ABLOCKS_ASSETS_URL . 'build/demo-import.js', |
| 196 |
$dependencies['dependencies'], |
| 197 |
$dependencies['version'], |
| 198 |
true |
| 199 |
); |
| 200 |
wp_localize_script( 'ablocks-demo-import-scripts', 'ABlocksGlobal', $this->get_dashboard_localize_script_data() ); |
| 201 |
wp_set_script_translations( 'ablocks-demo-import-scripts', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' ); |
| 202 |
}//end if |
| 203 |
} |
| 204 |
|
| 205 |
public function web_fonts_url( $font ) { |
| 206 |
$font_url = ''; |
| 207 |
if ( 'off' !== _x( 'on', 'Google font: on or off', 'ablocks' ) ) { |
| 208 |
$font_url = add_query_arg( 'family', rawurlencode( $font ), '//fonts.googleapis.com/css' ); |
| 209 |
} |
| 210 |
return $font_url; |
| 211 |
} |
| 212 |
|
| 213 |
public function front_end_google_fonts() { |
| 214 |
global $ablocks_fonts; |
| 215 |
|
| 216 |
if ( empty( $ablocks_fonts ) || ! is_array( $ablocks_fonts ) ) { |
| 217 |
return false; |
| 218 |
} |
| 219 |
|
| 220 |
$font_families = []; |
| 221 |
|
| 222 |
foreach ( $ablocks_fonts as $family => $weights ) { |
| 223 |
$font_family_string = $family; |
| 224 |
$total_weights = count( $weights ); |
| 225 |
|
| 226 |
if ( $total_weights > 0 ) { |
| 227 |
$font_family_string .= ':wght@' . implode( ';', $weights ); |
| 228 |
} |
| 229 |
|
| 230 |
$font_families[] = $font_family_string; |
| 231 |
} |
| 232 |
|
| 233 |
// Generate the URL using the web_fonts_url method |
| 234 |
$google_fonts_url = $this->web_fonts_url( implode( '|', $font_families ) ) . '&display=swap'; |
| 235 |
|
| 236 |
wp_register_style( 'ablocks-frontend-google-fonts', esc_url( $google_fonts_url ), array(), ABLOCKS_VERSION ); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
public function block_editor_assets() { |
| 241 |
if ( is_admin() ) { |
| 242 |
wp_enqueue_style( 'ablocks-editor-font-awesome', ABLOCKS_ASSETS_URL . 'library/font-awesome/css/all.min.css', array(), ABLOCKS_VERSION, 'all' ); |
| 243 |
wp_enqueue_style( 'ablocks-editor-fonts', $this->web_fonts_url( 'Roboto:ital,wght@0,300;0,400;0,500;0,700;1,300&display=swap' ), array(), ABLOCKS_VERSION ); |
| 244 |
wp_enqueue_style( 'ablocks-editor-icon', ABLOCKS_ASSETS_URL . 'library/css/ablocks-icon/style.css', array( 'wp-components' ), filemtime( ABLOCKS_ASSETS_PATH . 'library/css/ablocks-icon/style.css' ), 'all' ); |
| 245 |
wp_enqueue_style( 'ablocks-editor-style', ABLOCKS_ASSETS_URL . 'build/blocks.css', array( 'wp-components' ), filemtime( ABLOCKS_ASSETS_PATH . 'build/blocks.css' ), 'all' ); |
| 246 |
|
| 247 |
// js |
| 248 |
$dependencies = include ABLOCKS_ASSETS_PATH . 'build/blocks.asset.php'; |
| 249 |
wp_enqueue_script( |
| 250 |
'ablocks-editor-scripts', |
| 251 |
ABLOCKS_ASSETS_URL . 'build/blocks.js', |
| 252 |
$dependencies['dependencies'], |
| 253 |
$dependencies['version'], |
| 254 |
true |
| 255 |
); |
| 256 |
wp_localize_script( 'ablocks-editor-scripts', 'ABlocksGlobal', $this->get_editor_localize_script_data() ); |
| 257 |
wp_set_script_translations( 'ablocks-editor-scripts', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' ); |
| 258 |
} |
| 259 |
} |
| 260 |
public function enqueue_frontend_assets() { |
| 261 |
if ( ! Helper::is_enabled_assets_generation() ) { |
| 262 |
return; |
| 263 |
} |
| 264 |
if ( ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) ) { |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
$post_id = get_the_ID(); |
| 269 |
if ( $post_id ) { |
| 270 |
$FileUpload = new FileUpload(); |
| 271 |
$css_file_path = $FileUpload->get_file_path( get_the_ID() . '.min.css' ); |
| 272 |
if ( file_exists( $css_file_path ) ) { |
| 273 |
$css_file_url = $FileUpload->get_file_url( get_the_ID() . '.min.css' ); |
| 274 |
// Enqueue google fonts |
| 275 |
wp_enqueue_style( 'ablocks-frontend-google-fonts' ); |
| 276 |
// Combine CSS |
| 277 |
wp_enqueue_style( 'ablocks-blocks-combine-style', $css_file_url, array(), filemtime( $css_file_path ), 'all' ); |
| 278 |
} |
| 279 |
$js_file_path = $FileUpload->get_file_path( get_the_ID() . '.min.js' ); |
| 280 |
if ( file_exists( $js_file_path ) ) { |
| 281 |
$js_file_url = $FileUpload->get_file_url( get_the_ID() . '.min.js' ); |
| 282 |
wp_enqueue_script( 'ablocks-blocks-combine-script', $js_file_url, array(), filemtime( $js_file_path ), true ); |
| 283 |
wp_localize_script( 'ablocks-blocks-combine-script', 'ABlocksGlobal', $this->get_localize_script_data() ); |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
public function regenerate_missing_assets() { |
| 289 |
if ( ! Helper::is_enabled_assets_generation() ) { |
| 290 |
return; |
| 291 |
} |
| 292 |
|
| 293 |
$post_id = (int) get_the_ID(); |
| 294 |
if ( $post_id ) { |
| 295 |
$FileUpload = new FileUpload(); |
| 296 |
$has_upload_file = $FileUpload->has_upload_file( get_the_ID() . '.min.css' ); |
| 297 |
if ( ! $has_upload_file ) { |
| 298 |
AssetsGenerator::write_frontend_css_in_uploads_folder( $post_id ); |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
public function register_scripts() { |
| 303 |
$register_styles = RegisterScripts::get_register_styles(); |
| 304 |
foreach ( $register_styles as $register_handler => $register_style ) { |
| 305 |
wp_register_style( $register_handler, $register_style['url'], $register_style['deps'], ABLOCKS_VERSION, $register_style['media'] ); |
| 306 |
} |
| 307 |
$register_scripts = RegisterScripts::get_register_scripts(); |
| 308 |
foreach ( $register_scripts as $register_handler => $register_script ) { |
| 309 |
if ( isset( $register_script['dependencies'] ) ) { |
| 310 |
$dependencies = include $register_script['dependencies']; |
| 311 |
$register_script['deps'] = $dependencies['dependencies']; |
| 312 |
$register_script['ver'] = $dependencies['version']; |
| 313 |
} |
| 314 |
wp_register_script( |
| 315 |
$register_handler, |
| 316 |
$register_script['url'], |
| 317 |
$register_script['deps'], |
| 318 |
$register_script['ver'], |
| 319 |
$register_script['args'] |
| 320 |
); |
| 321 |
} |
| 322 |
} |
| 323 |
public function global_css_variable() { |
| 324 |
wp_register_style( 'ablocks-editor-global-styles', false, array(), ABLOCKS_VERSION ); |
| 325 |
wp_enqueue_style( 'ablocks-editor-global-styles' ); |
| 326 |
|
| 327 |
$container_padding = Helper::get_settings( 'container_padding', 10 ) . 'px'; |
| 328 |
$css = ":root, body .editor-styles-wrapper { |
| 329 |
--ablocks-container-padding: $container_padding; |
| 330 |
}"; |
| 331 |
wp_add_inline_style( 'ablocks-editor-global-styles', $css ); |
| 332 |
} |
| 333 |
|
| 334 |
public function enqueue_frontend_localize_script() { |
| 335 |
wp_localize_script( 'ablocks-blocks-combine-script', 'ABlocksGlobal', $this->get_localize_script_data() ); |
| 336 |
wp_set_script_translations( 'ablocks-blocks-combine-script', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' ); |
| 337 |
|
| 338 |
wp_localize_script( 'ablocks-common-script', 'ABlocksGlobal', $this->get_localize_script_data() ); |
| 339 |
wp_set_script_translations( 'ablocks-common-script', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' ); |
| 340 |
} |
| 341 |
|
| 342 |
} |
| 343 |
|