| 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\Classes\GlobalCssGenerator; |
| 12 |
use ABlocks\Classes\FontLoadLocally; |
| 13 |
use ABlocks\Admin\Menu; |
| 14 |
use ABlocks\Helper; |
| 15 |
|
| 16 |
class Assets { |
| 17 |
public $current_page_template_part = []; |
| 18 |
public $current_page_blocks = []; |
| 19 |
private $FileUpload; |
| 20 |
private $current_page_slug = ''; |
| 21 |
private $theme_builder_locations = []; |
| 22 |
public static function init() { |
| 23 |
$self = new self(); |
| 24 |
$self->FileUpload = new FileUpload(); |
| 25 |
$self->current_page_slug = ''; |
| 26 |
add_action( 'admin_enqueue_scripts', [ $self, 'dashboard_scripts' ], 10 ); |
| 27 |
add_action( 'admin_enqueue_scripts', [ $self, 'demo_importer_scripts' ], 10 ); |
| 28 |
add_action( 'enqueue_block_assets', [ $self, 'block_editor_assets' ] ); |
| 29 |
add_action( 'enqueue_block_assets', [ $self, 'register_scripts' ] ); |
| 30 |
add_action( 'wp_enqueue_scripts', [ $self, 'front_end_google_fonts' ], 999 ); |
| 31 |
|
| 32 |
add_action( 'wp_enqueue_scripts', [ $self, 'enqueue_frontend_assets' ], 99 ); |
| 33 |
// Global CSS |
| 34 |
add_action( 'wp_enqueue_block_assets', [ $self, 'global_css_variable' ] ); |
| 35 |
add_action( 'wp_enqueue_scripts', [ $self, 'global_css_variable' ] ); |
| 36 |
add_action( 'enqueue_block_editor_assets', [ $self, 'global_css_variable' ] ); |
| 37 |
add_action( 'enqueue_block_editor_assets', [ $self, 'add_editor_inline_css' ] ); |
| 38 |
|
| 39 |
// Detect page |
| 40 |
add_action( 'wp', array( $self, 'detect_page' ) ); |
| 41 |
|
| 42 |
if ( ! is_admin() && Helper::is_enabled_assets_generation() ) { |
| 43 |
if ( Helper::is_fse_theme() ) { |
| 44 |
add_filter( 'pre_render_block', [ $self, 'set_current_page_template_part' ], 5, 2 ); |
| 45 |
add_action( 'ablocks/before_enqueue_frontend_scripts', [ $self, 'regenerate_missing_assets' ] ); |
| 46 |
} else { |
| 47 |
add_action( 'ablocks_theme_builder_after_dispatch', [ $self, 'set_theme_builder_locations' ] ); |
| 48 |
add_action( 'wp', [ $self, 'regenerate_missing_assets' ], 20 ); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
public function detect_page() { |
| 55 |
if ( is_404() ) { |
| 56 |
$this->current_page_slug = '404'; |
| 57 |
} elseif ( is_search() ) { |
| 58 |
$this->current_page_slug = 'search'; |
| 59 |
} elseif ( is_home() ) { |
| 60 |
$posts_page_id = get_option( 'page_for_posts' ); |
| 61 |
$this->current_page_slug = $posts_page_id ? (string) $posts_page_id : 'blog'; |
| 62 |
} elseif ( is_post_type_archive() ) { |
| 63 |
$this->current_page_slug = 'archive-' . get_post_type(); |
| 64 |
} elseif ( is_category() ) { |
| 65 |
$term = get_queried_object(); |
| 66 |
$this->current_page_slug = 'category-' . $term->term_id; // or use slug |
| 67 |
} elseif ( is_tag() ) { |
| 68 |
$term = get_queried_object(); |
| 69 |
$this->current_page_slug = 'tag-' . $term->term_id; |
| 70 |
} elseif ( is_tax() ) { |
| 71 |
$term = get_queried_object(); |
| 72 |
$this->current_page_slug = 'tax-' . $term->taxonomy . '-' . $term->term_id; |
| 73 |
} elseif ( is_author() ) { |
| 74 |
$this->current_page_slug = 'author-' . get_the_author_meta( 'ID' ); |
| 75 |
} elseif ( is_date() ) { |
| 76 |
$this->current_page_slug = 'date-archive'; |
| 77 |
} elseif ( is_singular() ) { |
| 78 |
$this->current_page_slug = (string) get_the_ID(); // just the post/page/custom ID |
| 79 |
} else { |
| 80 |
$this->current_page_slug = (string) get_the_ID(); // fallback to ID |
| 81 |
}//end if |
| 82 |
} |
| 83 |
|
| 84 |
public function get_current_archive_post_type() { |
| 85 |
if ( is_post_type_archive() ) { |
| 86 |
$post_type = get_query_var( 'post_type' ); |
| 87 |
if ( is_array( $post_type ) ) { |
| 88 |
$post_type = reset( $post_type ); |
| 89 |
} |
| 90 |
return $post_type; |
| 91 |
} |
| 92 |
return null; |
| 93 |
} |
| 94 |
|
| 95 |
public function get_localize_script_data() { |
| 96 |
return [ |
| 97 |
'rest_url' => esc_url_raw( rest_url() ), |
| 98 |
'namespace' => ABLOCKS_PLUGIN_SLUG . '/v1/', |
| 99 |
'plugin_root_url' => ABLOCKS_ROOT_URL, |
| 100 |
'plugin_root_path' => ABLOCKS_ROOT_DIR_PATH, |
| 101 |
'admin_url' => admin_url(), |
| 102 |
'site_url' => site_url(), |
| 103 |
'route_path' => wp_parse_url( admin_url(), PHP_URL_PATH ), |
| 104 |
'ajax_url' => esc_url( admin_url( 'admin-ajax.php' ) ), |
| 105 |
'is_pro' => (bool) Helper::is_active_ablocks_pro(), |
| 106 |
'is_archive' => (bool) is_archive(), |
| 107 |
'archive_post_type' => $this->get_current_archive_post_type(), |
| 108 |
]; |
| 109 |
} |
| 110 |
|
| 111 |
public function get_localize_dashboard_data() { |
| 112 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 113 |
return $this->get_localize_script_data(); |
| 114 |
} |
| 115 |
return array_merge($this->get_localize_script_data(), [ |
| 116 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 117 |
'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ), |
| 118 |
]); |
| 119 |
} |
| 120 |
public function get_localize_editor_data() { |
| 121 |
if ( ! current_user_can( 'edit_posts' ) ) { |
| 122 |
return $this->get_localize_script_data(); |
| 123 |
} |
| 124 |
return array_merge($this->get_localize_script_data(), [ |
| 125 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 126 |
'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ), |
| 127 |
]); |
| 128 |
} |
| 129 |
public function get_localize_frontend_data() { |
| 130 |
return array_merge($this->get_localize_script_data(), [ |
| 131 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 132 |
'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ), |
| 133 |
]); |
| 134 |
} |
| 135 |
|
| 136 |
public function get_dashboard_localize_script_data() { |
| 137 |
$menu = new Menu(); |
| 138 |
$args = array( |
| 139 |
'menu' => wp_json_encode( Helper::get_admin_menu_list() ), |
| 140 |
'toplevel_menu_icon_url' => $menu->get_toplevel_menu_icon_url(), |
| 141 |
'settings' => [ |
| 142 |
'default_container_width' => Helper::get_settings( 'default_container_width', 1280 ), |
| 143 |
'container_padding' => Helper::get_settings( 'container_padding', 10 ), |
| 144 |
'container_element_gap' => Helper::get_settings( 'container_element_gap', 20 ), |
| 145 |
'enabled_assets_file_generation' => (bool) Helper::get_settings( 'enabled_assets_file_generation', false ), |
| 146 |
'enabled_block_copy_paste_style' => (bool) Helper::get_settings( 'enabled_block_copy_paste_style', false ), |
| 147 |
'enabled_load_google_font_locally' => (bool) Helper::get_settings( 'enabled_load_google_font_locally', false ), |
| 148 |
'enabled_only_selected_fonts' => (bool) Helper::get_settings( 'enabled_only_selected_fonts', false ), |
| 149 |
'selected_fonts' => (array) Helper::get_settings( 'selected_fonts', [] ), |
| 150 |
], |
| 151 |
'is_gutenberg_editor' => Helper::is_gutenberg_editor(), |
| 152 |
'is_fse_theme' => Helper::is_fse_theme(), |
| 153 |
'third_party_plugin_status' => [ |
| 154 |
'academy_lms' => Helper::is_active_academy(), |
| 155 |
'storeengine' => Helper::is_active_storeengine(), |
| 156 |
'wp_map_block' => Helper::is_active_wp_map_block(), |
| 157 |
'easy_content_manager' => Helper::is_active_easy_content_manager(), |
| 158 |
] |
| 159 |
); |
| 160 |
return apply_filters( |
| 161 |
'ablocks/assets/dashboard_scripts_data', |
| 162 |
array_merge( |
| 163 |
$this->get_localize_dashboard_data(), |
| 164 |
$args |
| 165 |
) |
| 166 |
); |
| 167 |
} |
| 168 |
public function get_editor_localize_script_data() { |
| 169 |
global $ablocks_blocks; |
| 170 |
$post_types = Helper::get_public_post_type_options(); |
| 171 |
$args = array( |
| 172 |
'settings' => [ |
| 173 |
'default_container_width' => Helper::get_settings( 'default_container_width', 1280 ), |
| 174 |
'container_padding' => Helper::get_settings( 'container_padding', 10 ), |
| 175 |
'container_element_gap' => Helper::get_settings( 'container_element_gap', 20 ), |
| 176 |
'enabled_assets_file_generation' => (bool) Helper::get_settings( 'enabled_assets_file_generation', false ), |
| 177 |
'enabled_block_copy_paste_style' => (bool) Helper::get_settings( 'enabled_block_copy_paste_style', false ), |
| 178 |
'enabled_load_google_font_locally' => (bool) Helper::get_settings( 'enabled_load_google_font_locally', false ), |
| 179 |
'enabled_only_selected_fonts' => (bool) Helper::get_settings( 'enabled_only_selected_fonts', false ), |
| 180 |
'selected_fonts' => (array) Helper::get_settings( 'selected_fonts', [] ), |
| 181 |
'frontend_dashboard_page' => (int) Helper::get_settings( 'frontend_dashboard_page' ), |
| 182 |
'global_color' => (array) Helper::get_settings( 'global_color', [] ), |
| 183 |
'global_typography' => (array) Helper::get_settings( 'global_typography', [] ), |
| 184 |
'global_typography_list' => wp_list_pluck( Helper::get_settings( 'global_typography', [] ), 'value', 'id' ), |
| 185 |
'global_font_family_fallback' => (string) Helper::get_settings( 'global_font_family_fallback', 'Sans-serif' ), |
| 186 |
], |
| 187 |
'is_gutenberg_editor' => Helper::is_gutenberg_editor(), |
| 188 |
'has_required_block_attribute_migration' => get_option( 'ablocks_has_required_block_attribute_migration' ), |
| 189 |
'third_party_plugin_status' => [ |
| 190 |
'academy_lms' => Helper::is_active_academy(), |
| 191 |
'storeengine' => Helper::is_active_storeengine(), |
| 192 |
'wp_map_block' => Helper::is_active_wp_map_block(), |
| 193 |
'quizpress' => Helper::is_active_quizpress(), |
| 194 |
], |
| 195 |
'blocks_status' => $ablocks_blocks, |
| 196 |
'post_types' => $post_types |
| 197 |
); |
| 198 |
return apply_filters( |
| 199 |
'ablocks/assets/editor_scripts_data', |
| 200 |
array_merge( |
| 201 |
$this->get_localize_editor_data(), |
| 202 |
$args |
| 203 |
) |
| 204 |
); |
| 205 |
} |
| 206 |
|
| 207 |
public function dashboard_scripts( $hook ) { |
| 208 |
$demo_config = Helper::get_theme_demo_config(); |
| 209 |
if ( strpos( $hook, '_page_' . ABLOCKS_PLUGIN_SLUG ) !== false && strpos( $hook, $demo_config['menu_slug'] ) === false ) { |
| 210 |
// Remove Notices |
| 211 |
remove_all_actions( 'admin_notices' ); |
| 212 |
// dequeue third party plugin assets |
| 213 |
add_action( |
| 214 |
'wp_print_scripts', |
| 215 |
function () { |
| 216 |
$isSkip = apply_filters( 'ablocks/skip_no_conflict_backend_scripts', Helper::is_dev_mode_enable() ); |
| 217 |
|
| 218 |
if ( $isSkip ) { |
| 219 |
return; |
| 220 |
} |
| 221 |
|
| 222 |
global $wp_scripts; |
| 223 |
if ( ! $wp_scripts ) { |
| 224 |
return; |
| 225 |
} |
| 226 |
|
| 227 |
$pluginUrl = plugins_url(); |
| 228 |
foreach ( $wp_scripts->queue as $script ) { |
| 229 |
$src = $wp_scripts->registered[ $script ]->src; |
| 230 |
if ( strpos( $src, $pluginUrl ) !== false && ! strpos( $src, ABLOCKS_PLUGIN_SLUG ) !== false ) { |
| 231 |
wp_dequeue_script( $wp_scripts->registered[ $script ]->handle ); |
| 232 |
} |
| 233 |
} |
| 234 |
}, |
| 235 |
1 |
| 236 |
); |
| 237 |
|
| 238 |
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 ); |
| 239 |
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' ); |
| 240 |
wp_enqueue_style( 'ablocks-dashboard-style', ABLOCKS_ASSETS_URL . 'build/dashboard.css', array( 'wp-components' ), filemtime( ABLOCKS_ASSETS_PATH . 'build/dashboard.css' ), 'all' ); |
| 241 |
|
| 242 |
$dependencies = include ABLOCKS_ASSETS_PATH . 'build/dashboard.asset.php'; |
| 243 |
wp_enqueue_script( |
| 244 |
'ablocks-dashboard-scripts', |
| 245 |
ABLOCKS_ASSETS_URL . 'build/dashboard.js', |
| 246 |
$dependencies['dependencies'], |
| 247 |
$dependencies['version'], |
| 248 |
true |
| 249 |
); |
| 250 |
wp_localize_script( 'ablocks-dashboard-scripts', 'ABlocksGlobal', $this->get_dashboard_localize_script_data() ); |
| 251 |
wp_set_script_translations( 'ablocks-dashboard-scripts', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' ); |
| 252 |
}//end if |
| 253 |
} |
| 254 |
|
| 255 |
public function demo_importer_scripts( $hook ) { |
| 256 |
$demo_config = Helper::get_theme_demo_config(); |
| 257 |
if ( strpos( $hook, $demo_config['menu_slug'] ) !== false ) { |
| 258 |
// Remove Notices |
| 259 |
remove_all_actions( 'admin_notices' ); |
| 260 |
// dequeue third party plugin assets |
| 261 |
add_action( |
| 262 |
'wp_print_scripts', |
| 263 |
function () { |
| 264 |
$isSkip = apply_filters( 'ablocks/skip_no_conflict_demo_importer_scripts', Helper::is_dev_mode_enable() ); |
| 265 |
|
| 266 |
if ( $isSkip ) { |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
global $wp_scripts; |
| 271 |
if ( ! $wp_scripts ) { |
| 272 |
return; |
| 273 |
} |
| 274 |
|
| 275 |
$pluginUrl = plugins_url(); |
| 276 |
foreach ( $wp_scripts->queue as $script ) { |
| 277 |
$src = $wp_scripts->registered[ $script ]->src; |
| 278 |
if ( strpos( $src, $pluginUrl ) !== false && ! strpos( $src, ABLOCKS_PLUGIN_SLUG ) !== false ) { |
| 279 |
wp_dequeue_script( $wp_scripts->registered[ $script ]->handle ); |
| 280 |
} |
| 281 |
} |
| 282 |
}, |
| 283 |
1 |
| 284 |
); |
| 285 |
|
| 286 |
$this->demo_template_importer_scripts(); |
| 287 |
}//end if |
| 288 |
} |
| 289 |
|
| 290 |
public function demo_template_importer_scripts() { |
| 291 |
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 ); |
| 292 |
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' ); |
| 293 |
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' ); |
| 294 |
|
| 295 |
$dependencies = include ABLOCKS_ASSETS_PATH . 'build/demo-import.asset.php'; |
| 296 |
wp_enqueue_script( |
| 297 |
'ablocks-demo-import-scripts', |
| 298 |
ABLOCKS_ASSETS_URL . 'build/demo-import.js', |
| 299 |
$dependencies['dependencies'], |
| 300 |
$dependencies['version'], |
| 301 |
true |
| 302 |
); |
| 303 |
wp_localize_script( 'ablocks-demo-import-scripts', 'ABlocksGlobal', $this->get_dashboard_localize_script_data() ); |
| 304 |
wp_set_script_translations( 'ablocks-demo-import-scripts', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' ); |
| 305 |
} |
| 306 |
|
| 307 |
public function web_fonts_url( $font ) { |
| 308 |
$font_url = ''; |
| 309 |
if ( 'off' !== _x( 'on', 'Google font: on or off', 'ablocks' ) ) { |
| 310 |
$font_url = add_query_arg( 'family', rawurlencode( $font ), '//fonts.googleapis.com/css' ); |
| 311 |
} |
| 312 |
return $font_url; |
| 313 |
} |
| 314 |
|
| 315 |
public function front_end_google_fonts() { |
| 316 |
global $ablocks_fonts; |
| 317 |
if ( empty( $ablocks_fonts ) || ! is_array( $ablocks_fonts ) ) { |
| 318 |
return false; |
| 319 |
} |
| 320 |
|
| 321 |
if ( Helper::get_settings( 'enabled_load_google_font_locally', false ) ) { |
| 322 |
$FontLoadLocally = new FontLoadLocally(); |
| 323 |
$FontLoadLocally->enqueue_fonts( $ablocks_fonts ); // Attempt to load local fonts (fallback inside method) |
| 324 |
} else { |
| 325 |
$font_families = []; |
| 326 |
foreach ( $ablocks_fonts as $family => $weights ) { |
| 327 |
$font_family_string = $family; |
| 328 |
$total_weights = count( $weights ); |
| 329 |
|
| 330 |
if ( $total_weights > 0 ) { |
| 331 |
$font_family_string .= ':wght@' . implode( ';', $weights ); |
| 332 |
} |
| 333 |
|
| 334 |
$font_families[] = $font_family_string; |
| 335 |
} |
| 336 |
// Generate the URL using the web_fonts_url method |
| 337 |
$google_fonts_url = $this->web_fonts_url( implode( '|', $font_families ) ) . '&display=swap'; |
| 338 |
wp_register_style( 'ablocks-frontend-google-fonts', esc_url( $google_fonts_url ), array(), ABLOCKS_VERSION ); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
public function block_editor_assets() { |
| 344 |
if ( is_admin() ) { |
| 345 |
wp_enqueue_style( 'ablocks-editor-font-awesome', ABLOCKS_ASSETS_URL . 'library/font-awesome/css/all.min.css', array(), ABLOCKS_VERSION, 'all' ); |
| 346 |
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 ); |
| 347 |
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' ); |
| 348 |
wp_enqueue_style( 'ablocks-editor-style', ABLOCKS_ASSETS_URL . 'build/blocks.css', array( 'wp-components' ), filemtime( ABLOCKS_ASSETS_PATH . 'build/blocks.css' ), 'all' ); |
| 349 |
|
| 350 |
// js |
| 351 |
$dependencies = include ABLOCKS_ASSETS_PATH . 'build/blocks.asset.php'; |
| 352 |
wp_enqueue_script( |
| 353 |
'ablocks-editor-scripts', |
| 354 |
ABLOCKS_ASSETS_URL . 'build/blocks.js', |
| 355 |
$dependencies['dependencies'], |
| 356 |
$dependencies['version'], |
| 357 |
true |
| 358 |
); |
| 359 |
wp_localize_script( 'ablocks-editor-scripts', 'ABlocksGlobal', $this->get_editor_localize_script_data() ); |
| 360 |
wp_set_script_translations( 'ablocks-editor-scripts', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' ); |
| 361 |
} |
| 362 |
} |
| 363 |
public function enqueue_frontend_assets() { |
| 364 |
if ( ! Helper::is_enabled_assets_generation() ) { |
| 365 |
return; |
| 366 |
} |
| 367 |
|
| 368 |
do_action( 'ablocks/before_enqueue_frontend_scripts' ); |
| 369 |
|
| 370 |
$script_loading_strategy = Helper::get_script_loading_strategy(); |
| 371 |
|
| 372 |
if ( ! $this->is_assets_generated() ) { |
| 373 |
return; |
| 374 |
} |
| 375 |
|
| 376 |
if ( $this->current_page_slug ) { |
| 377 |
// Enqueue google fonts |
| 378 |
wp_enqueue_style( 'ablocks-frontend-google-fonts' ); |
| 379 |
wp_enqueue_style( 'ablocks-blocks-combine-style', $this->FileUpload->get_file_url( $this->current_page_slug . '.min.css' ), array(), filemtime( $this->FileUpload->get_file_path( $this->current_page_slug . '.min.css' ) ), 'all' ); |
| 380 |
|
| 381 |
wp_enqueue_script( 'ablocks-blocks-combine-script', $this->FileUpload->get_file_url( $this->current_page_slug . '.min.js' ), array(), filemtime( $this->FileUpload->get_file_path( $this->current_page_slug . '.min.js' ) ), true, [ 'strategy' => $script_loading_strategy ] ); |
| 382 |
wp_localize_script( 'ablocks-blocks-combine-script', 'ABlocksGlobal', $this->get_localize_frontend_data() ); |
| 383 |
wp_set_script_translations( 'ablocks-blocks-combine-script', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' ); |
| 384 |
} else { |
| 385 |
// fallback if assets not available |
| 386 |
add_filter( 'ablocks/is_allow_block_inline_assets', '__return_true' ); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
public function regenerate_missing_assets() { |
| 391 |
if ( $this->is_assets_generated() ) { |
| 392 |
return; |
| 393 |
} |
| 394 |
|
| 395 |
// classic |
| 396 |
if ( ! Helper::is_fse_theme() ) { |
| 397 |
$is_parse_main_content = false; |
| 398 |
if ( is_array( $this->theme_builder_locations ) ) { |
| 399 |
foreach ( $this->theme_builder_locations as $location_name => $location_id ) { |
| 400 |
$post = get_post( $location_id ); |
| 401 |
if ( is_object( $post ) ) { |
| 402 |
$this->set_current_page_template_part( $post->post_content, parse_blocks( $post->post_content ) ); |
| 403 |
} |
| 404 |
if ( $location_name === 'header' ) { |
| 405 |
$post = get_post( get_the_ID() ); |
| 406 |
if ( is_object( $post ) ) { |
| 407 |
$this->set_current_page_template_part( $post->post_content, parse_blocks( $post->post_content ) ); |
| 408 |
} |
| 409 |
$is_parse_main_content = true; |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
if ( false === $is_parse_main_content ) { |
| 414 |
$post = get_post( get_the_ID() ); |
| 415 |
$this->set_current_page_template_part( $post->post_content, parse_blocks( $post->post_content ) ); |
| 416 |
} |
| 417 |
}//end if |
| 418 |
|
| 419 |
if ( count( $this->current_page_blocks ) ) { |
| 420 |
$file_name = $this->current_page_slug; |
| 421 |
AssetsGenerator::write_frontend_css_in_uploads_folder( $file_name, $this->current_page_blocks ); |
| 422 |
} |
| 423 |
} |
| 424 |
public function register_scripts() { |
| 425 |
$register_styles = RegisterScripts::get_register_styles(); |
| 426 |
foreach ( $register_styles as $register_handler => $register_style ) { |
| 427 |
wp_register_style( $register_handler, $register_style['url'], $register_style['deps'], ABLOCKS_VERSION, $register_style['media'] ); |
| 428 |
} |
| 429 |
$register_scripts = RegisterScripts::get_register_scripts(); |
| 430 |
foreach ( $register_scripts as $register_handler => $register_script ) { |
| 431 |
if ( isset( $register_script['dependencies'] ) ) { |
| 432 |
$dependencies = include $register_script['dependencies']; |
| 433 |
$register_script['deps'] = $dependencies['dependencies']; |
| 434 |
$register_script['ver'] = $dependencies['version']; |
| 435 |
} |
| 436 |
wp_register_script( |
| 437 |
$register_handler, |
| 438 |
$register_script['url'], |
| 439 |
$register_script['deps'], |
| 440 |
$register_script['ver'], |
| 441 |
$register_script['args'] |
| 442 |
); |
| 443 |
} |
| 444 |
} |
| 445 |
public function global_css_variable() { |
| 446 |
wp_register_style( 'ablocks-editor-global-styles', false, array(), ABLOCKS_VERSION ); |
| 447 |
wp_enqueue_style( 'ablocks-editor-global-styles' ); |
| 448 |
|
| 449 |
$container_padding = Helper::get_settings( 'container_padding', 10 ) . 'px'; |
| 450 |
$css = ":root, body .editor-styles-wrapper {\n"; |
| 451 |
$css .= " --ablocks-container-padding: $container_padding;\n"; |
| 452 |
|
| 453 |
// Colors |
| 454 |
$global_color = (array) Helper::get_settings( 'global_color', [] ); |
| 455 |
foreach ( $global_color as $color ) { |
| 456 |
if ( isset( $color->id, $color->value ) ) { |
| 457 |
$css .= " --ablocks-{$color->id}: {$color->value};\n"; |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
// Typography |
| 462 |
$global_typography = (array) Helper::get_settings( 'global_typography', [] ); |
| 463 |
foreach ( $global_typography as $typography ) { |
| 464 |
if ( isset( $typography->id, $typography->value ) ) { |
| 465 |
$id = $typography->id; |
| 466 |
$value = $typography->value; |
| 467 |
|
| 468 |
// Desktop values |
| 469 |
if ( ! empty( $value->fontFamily ) ) { |
| 470 |
$css .= " --ablocks-{$id}-font-family: {$value->fontFamily};\n"; |
| 471 |
} |
| 472 |
if ( ! empty( $value->weight ) ) { |
| 473 |
$css .= " --ablocks-{$id}-weight: {$value->weight};\n"; |
| 474 |
} |
| 475 |
if ( ! empty( $value->transform ) ) { |
| 476 |
$css .= " --ablocks-{$id}-transform: {$value->transform};\n"; |
| 477 |
} |
| 478 |
if ( ! empty( $value->style ) ) { |
| 479 |
$css .= " --ablocks-{$id}-style: {$value->style};\n"; |
| 480 |
} |
| 481 |
if ( ! empty( $value->decoration ) ) { |
| 482 |
$css .= " --ablocks-{$id}-decoration: {$value->decoration};\n"; |
| 483 |
} |
| 484 |
if ( ! empty( $value->fontSize ) ) { |
| 485 |
$unit = ! empty( $value->fontSizeUnit ) ? $value->fontSizeUnit : 'px'; |
| 486 |
$css .= " --ablocks-{$id}-font-size: {$value->fontSize}{$unit};\n"; |
| 487 |
} |
| 488 |
if ( ! empty( $value->lineHeight ) ) { |
| 489 |
$unit = ! empty( $value->lineHeightUnit ) ? $value->lineHeightUnit : 'px'; |
| 490 |
$css .= " --ablocks-{$id}-line-height: {$value->lineHeight}{$unit};\n"; |
| 491 |
} |
| 492 |
if ( ! empty( $value->letterSpacing ) ) { |
| 493 |
$unit = ! empty( $value->letterSpacingUnit ) ? $value->letterSpacingUnit : 'px'; |
| 494 |
$css .= " --ablocks-{$id}-letter-spacing: {$value->letterSpacing}{$unit};\n"; |
| 495 |
} |
| 496 |
if ( ! empty( $value->wordSpacing ) ) { |
| 497 |
$unit = ! empty( $value->wordSpacingUnit ) ? $value->wordSpacingUnit : 'px'; |
| 498 |
$css .= " --ablocks-{$id}-word-spacing: {$value->wordSpacing}{$unit};\n"; |
| 499 |
} |
| 500 |
|
| 501 |
// Tablet values |
| 502 |
if ( ! empty( $value->fontSizeTablet ) ) { |
| 503 |
$unit = ! empty( $value->fontSizeUnitTablet ) ? $value->fontSizeUnitTablet : 'px'; |
| 504 |
$css .= " --ablocks-{$id}-font-size-tablet: {$value->fontSizeTablet}{$unit};\n"; |
| 505 |
} |
| 506 |
if ( ! empty( $value->lineHeightTablet ) ) { |
| 507 |
$unit = ! empty( $value->lineHeightUnitTablet ) ? $value->lineHeightUnitTablet : 'px'; |
| 508 |
$css .= " --ablocks-{$id}-line-height-tablet: {$value->lineHeightTablet}{$unit};\n"; |
| 509 |
} |
| 510 |
if ( ! empty( $value->letterSpacingTablet ) ) { |
| 511 |
$unit = ! empty( $value->letterSpacingUnitTablet ) ? $value->letterSpacingUnitTablet : 'px'; |
| 512 |
$css .= " --ablocks-{$id}-letter-spacing-tablet: {$value->letterSpacingTablet}{$unit};\n"; |
| 513 |
} |
| 514 |
if ( ! empty( $value->wordSpacingTablet ) ) { |
| 515 |
$unit = ! empty( $value->wordSpacingUnitTablet ) ? $value->wordSpacingUnitTablet : 'px'; |
| 516 |
$css .= " --ablocks-{$id}-word-spacing-tablet: {$value->wordSpacingTablet}{$unit};\n"; |
| 517 |
} |
| 518 |
|
| 519 |
// Mobile values |
| 520 |
if ( ! empty( $value->fontSizeMobile ) ) { |
| 521 |
$unit = ! empty( $value->fontSizeUnitMobile ) ? $value->fontSizeUnitMobile : 'px'; |
| 522 |
$css .= " --ablocks-{$id}-font-size-mobile: {$value->fontSizeMobile}{$unit};\n"; |
| 523 |
} |
| 524 |
if ( ! empty( $value->lineHeightMobile ) ) { |
| 525 |
$unit = ! empty( $value->lineHeightUnitMobile ) ? $value->lineHeightUnitMobile : 'px'; |
| 526 |
$css .= " --ablocks-{$id}-line-height-mobile: {$value->lineHeightMobile}{$unit};\n"; |
| 527 |
} |
| 528 |
if ( ! empty( $value->letterSpacingMobile ) ) { |
| 529 |
$unit = ! empty( $value->letterSpacingUnitMobile ) ? $value->letterSpacingUnitMobile : 'px'; |
| 530 |
$css .= " --ablocks-{$id}-letter-spacing-mobile: {$value->letterSpacingMobile}{$unit};\n"; |
| 531 |
} |
| 532 |
if ( ! empty( $value->wordSpacingMobile ) ) { |
| 533 |
$unit = ! empty( $value->wordSpacingUnitMobile ) ? $value->wordSpacingUnitMobile : 'px'; |
| 534 |
$css .= " --ablocks-{$id}-word-spacing-mobile: {$value->wordSpacingMobile}{$unit};\n"; |
| 535 |
} |
| 536 |
}//end if |
| 537 |
}//end foreach |
| 538 |
|
| 539 |
$css .= "}\n"; |
| 540 |
|
| 541 |
if ( ! is_admin() && ! Helper::is_gutenberg_editor() ) { |
| 542 |
$css .= $this->get_common_css(); |
| 543 |
} |
| 544 |
wp_add_inline_style( 'ablocks-editor-global-styles', $css ); |
| 545 |
wp_add_inline_style( 'ablocks-common-style', $css ); |
| 546 |
} |
| 547 |
|
| 548 |
public function add_editor_inline_css() { |
| 549 |
$custom_css = $this->get_common_css( '.editor-styles-wrapper ' ); |
| 550 |
add_theme_support( 'editor-styles' ); |
| 551 |
add_editor_style(); |
| 552 |
wp_add_inline_style( 'wp-block-library', $custom_css ); |
| 553 |
} |
| 554 |
|
| 555 |
public function get_common_css( $parent_selector = '' ) { |
| 556 |
global $ablocks_settings; |
| 557 |
$attributes = [ |
| 558 |
'global_body_text_color' => $ablocks_settings->global_body_text_color, |
| 559 |
'global_body_typography' => $ablocks_settings->global_body_typography, |
| 560 |
'global_body_paragraph_space' => $ablocks_settings->global_body_paragraph_space, |
| 561 |
'global_link_color' => $ablocks_settings->global_link_color, |
| 562 |
'global_link_hover_color' => $ablocks_settings->global_link_hover_color, |
| 563 |
'global_link_typography' => $ablocks_settings->global_link_typography, |
| 564 |
'global_link_hover_typography' => $ablocks_settings->global_link_hover_typography, |
| 565 |
'global_h1_color' => $ablocks_settings->global_h1_color, |
| 566 |
'global_h1_typography' => $ablocks_settings->global_h1_typography, |
| 567 |
'global_h2_color' => $ablocks_settings->global_h2_color, |
| 568 |
'global_h2_typography' => $ablocks_settings->global_h2_typography, |
| 569 |
'global_h3_color' => $ablocks_settings->global_h3_color, |
| 570 |
'global_h3_typography' => $ablocks_settings->global_h3_typography, |
| 571 |
'global_h4_color' => $ablocks_settings->global_h4_color, |
| 572 |
'global_h4_typography' => $ablocks_settings->global_h4_typography, |
| 573 |
'global_h5_color' => $ablocks_settings->global_h5_color, |
| 574 |
'global_h5_typography' => $ablocks_settings->global_h5_typography, |
| 575 |
'global_h6_color' => $ablocks_settings->global_h6_color, |
| 576 |
'global_h6_typography' => $ablocks_settings->global_h6_typography, |
| 577 |
]; |
| 578 |
$css_generator = new GlobalCssGenerator( $attributes ); |
| 579 |
|
| 580 |
$css_generator->add_class_styles( |
| 581 |
$parent_selector . 'body', |
| 582 |
$css_generator->get_body_css( $attributes ), |
| 583 |
$css_generator->get_body_css( $attributes, 'Tablet' ), |
| 584 |
$css_generator->get_body_css( $attributes, 'Mobile' ) |
| 585 |
); |
| 586 |
$css_generator->add_class_styles( |
| 587 |
$parent_selector . 'p', |
| 588 |
$css_generator->get_body_paragraph_css( $attributes ), |
| 589 |
$css_generator->get_body_paragraph_css( $attributes, 'Tablet' ), |
| 590 |
$css_generator->get_body_paragraph_css( $attributes, 'Mobile' ) |
| 591 |
); |
| 592 |
$css_generator->add_class_styles( |
| 593 |
$parent_selector . 'a', |
| 594 |
$css_generator->get_body_anchor_css( $attributes ), |
| 595 |
$css_generator->get_body_anchor_css( $attributes, 'Tablet' ), |
| 596 |
$css_generator->get_body_anchor_css( $attributes, 'Mobile' ) |
| 597 |
); |
| 598 |
$css_generator->add_class_styles( |
| 599 |
$parent_selector . 'a:hover', |
| 600 |
$css_generator->get_body_anchor_hover_css( $attributes ), |
| 601 |
$css_generator->get_body_anchor_hover_css( $attributes, 'Tablet' ), |
| 602 |
$css_generator->get_body_anchor_hover_css( $attributes, 'Mobile' ) |
| 603 |
); |
| 604 |
$css_generator->add_class_styles( |
| 605 |
$parent_selector . 'h1', |
| 606 |
$css_generator->get_global_h1_css( $attributes ), |
| 607 |
$css_generator->get_global_h1_css( $attributes, 'Tablet' ), |
| 608 |
$css_generator->get_global_h1_css( $attributes, 'Mobile' ) |
| 609 |
); |
| 610 |
$css_generator->add_class_styles( |
| 611 |
$parent_selector . 'h2', |
| 612 |
$css_generator->get_global_h2_css( $attributes ), |
| 613 |
$css_generator->get_global_h2_css( $attributes, 'Tablet' ), |
| 614 |
$css_generator->get_global_h2_css( $attributes, 'Mobile' ) |
| 615 |
); |
| 616 |
$css_generator->add_class_styles( |
| 617 |
$parent_selector . 'h3', |
| 618 |
$css_generator->get_global_h3_css( $attributes ), |
| 619 |
$css_generator->get_global_h3_css( $attributes, 'Tablet' ), |
| 620 |
$css_generator->get_global_h3_css( $attributes, 'Mobile' ) |
| 621 |
); |
| 622 |
$css_generator->add_class_styles( |
| 623 |
$parent_selector . 'h4', |
| 624 |
$css_generator->get_global_h4_css( $attributes ), |
| 625 |
$css_generator->get_global_h4_css( $attributes, 'Tablet' ), |
| 626 |
$css_generator->get_global_h4_css( $attributes, 'Mobile' ) |
| 627 |
); |
| 628 |
$css_generator->add_class_styles( |
| 629 |
$parent_selector . 'h5', |
| 630 |
$css_generator->get_global_h5_css( $attributes ), |
| 631 |
$css_generator->get_global_h5_css( $attributes, 'Tablet' ), |
| 632 |
$css_generator->get_global_h5_css( $attributes, 'Mobile' ) |
| 633 |
); |
| 634 |
$css_generator->add_class_styles( |
| 635 |
$parent_selector . 'h6', |
| 636 |
$css_generator->get_global_h6_css( $attributes ), |
| 637 |
$css_generator->get_global_h6_css( $attributes, 'Tablet' ), |
| 638 |
$css_generator->get_global_h6_css( $attributes, 'Mobile' ) |
| 639 |
); |
| 640 |
return $css_generator->generate_css(); |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
public function is_assets_generated() { |
| 645 |
$file_name = $this->current_page_slug; |
| 646 |
$css_file_path = $this->FileUpload->get_file_path( $file_name . '.min.css' ); |
| 647 |
return file_exists( $css_file_path ); |
| 648 |
} |
| 649 |
|
| 650 |
public function set_current_page_template_part( $content, $block ) { |
| 651 |
if ( ! isset( $block['blockName'] ) && is_array( $block ) ) { |
| 652 |
foreach ( $block as $block_item ) { |
| 653 |
if ( ! empty( $block_item['blockName'] ) && strpos( $block_item['blockName'], 'ablocks/' ) !== false ) { |
| 654 |
$this->current_page_blocks[] = $block_item; |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
if ( ! empty( $block['blockName'] ) && strpos( $block['blockName'], 'ablocks/' ) !== false ) { |
| 659 |
$this->current_page_blocks[] = $block; |
| 660 |
} |
| 661 |
return $content; |
| 662 |
} |
| 663 |
|
| 664 |
public function set_theme_builder_locations( $args ) { |
| 665 |
$this->theme_builder_locations = $args; |
| 666 |
} |
| 667 |
} |
| 668 |
|