| @@ -7,30 +7,99 @@ | ||
| 7 | 7 | |
| 8 | 8 | use ABlocks\Classes\FileUpload; |
| 9 | 9 | use ABlocks\Classes\AssetsGenerator; |
| 10 | 10 | use ABlocks\Classes\RegisterScripts; |
| 11 | +use ABlocks\Classes\GlobalCssGenerator; | |
| 12 | +use ABlocks\Classes\GlobalClasses; | |
| 13 | +use ABlocks\Classes\FontLoadLocally; | |
| 11 | 14 | use ABlocks\Admin\Menu; |
| 12 | 15 | use ABlocks\Helper; |
| 13 | 16 | |
| 14 | 17 | class Assets { |
| 15 | 18 | |
| 19 | + /** Records which plugin version last generated the page stylesheets. */ | |
| 20 | + const BUILD_OPTION = 'ablocks_assets_build'; | |
| 21 | + | |
| 22 | + public $current_page_template_part = []; | |
| 23 | + public $current_page_blocks = []; | |
| 24 | + private $FileUpload; | |
| 25 | + private $current_page_slug = ''; | |
| 26 | + private $theme_builder_locations = []; | |
| 16 | 27 | public static function init() { |
| 17 | 28 | $self = new self(); |
| 29 | + $self->FileUpload = new FileUpload(); | |
| 30 | + $self->current_page_slug = ''; | |
| 18 | 31 | add_action( 'admin_enqueue_scripts', [ $self, 'dashboard_scripts' ], 10 ); |
| 19 | 32 | add_action( 'admin_enqueue_scripts', [ $self, 'demo_importer_scripts' ], 10 ); |
| 20 | 33 | add_action( 'enqueue_block_assets', [ $self, 'block_editor_assets' ] ); |
| 21 | 34 | add_action( 'enqueue_block_assets', [ $self, 'register_scripts' ] ); |
| 22 | - add_action( 'wp_enqueue_scripts', [ $self, 'front_end_google_fonts' ] ); | |
| 35 | + // Frontend fonts are now emitted by core via CoreFontRegistry (theme.json | |
| 36 | + // @font-face) with a Google Fonts fallback. See docs/FONT-MANAGEMENT-PLAN.md. | |
| 23 | 37 | |
| 24 | 38 | 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 | 39 | // 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 ); | |
| 40 | + add_action( 'wp_enqueue_block_assets', [ $self, 'global_css_variable' ] ); | |
| 41 | + add_action( 'wp_enqueue_scripts', [ $self, 'global_css_variable' ] ); | |
| 42 | + add_action( 'enqueue_block_editor_assets', [ $self, 'global_css_variable' ] ); | |
| 43 | + add_action( 'enqueue_block_editor_assets', [ $self, 'add_editor_inline_css' ] ); | |
| 44 | + add_action( 'enqueue_block_editor_assets', [ $self, 'editor_google_fonts' ] ); | |
| 45 | + | |
| 46 | + // Detect page | |
| 47 | + add_action( 'wp', array( $self, 'detect_page' ) ); | |
| 48 | + | |
| 49 | + if ( ! is_admin() && Helper::is_enabled_assets_generation() ) { | |
| 50 | + if ( Helper::is_fse_theme() ) { | |
| 51 | + add_filter( 'pre_render_block', [ $self, 'set_current_page_template_part' ], 5, 2 ); | |
| 52 | + add_action( 'ablocks/before_enqueue_frontend_scripts', [ $self, 'regenerate_missing_assets' ] ); | |
| 53 | + } else { | |
| 54 | + add_action( 'ablocks_theme_builder_after_dispatch', [ $self, 'set_theme_builder_locations' ] ); | |
| 55 | + add_action( 'wp', [ $self, 'regenerate_missing_assets' ], 20 ); | |
| 56 | + } | |
| 57 | + } | |
| 58 | + | |
| 31 | 59 | } |
| 32 | 60 | |
| 61 | + public function detect_page() { | |
| 62 | + if ( is_404() ) { | |
| 63 | + $this->current_page_slug = '404'; | |
| 64 | + } elseif ( is_search() ) { | |
| 65 | + $this->current_page_slug = 'search'; | |
| 66 | + } elseif ( is_home() ) { | |
| 67 | + $posts_page_id = get_option( 'page_for_posts' ); | |
| 68 | + $this->current_page_slug = $posts_page_id ? (string) $posts_page_id : 'blog'; | |
| 69 | + } elseif ( is_post_type_archive() ) { | |
| 70 | + $this->current_page_slug = 'archive-' . get_post_type(); | |
| 71 | + } elseif ( is_category() ) { | |
| 72 | + $term = get_queried_object(); | |
| 73 | + $this->current_page_slug = 'category-' . $term->term_id; // or use slug | |
| 74 | + } elseif ( is_tag() ) { | |
| 75 | + $term = get_queried_object(); | |
| 76 | + $this->current_page_slug = 'tag-' . $term->term_id; | |
| 77 | + } elseif ( is_tax() ) { | |
| 78 | + $term = get_queried_object(); | |
| 79 | + $this->current_page_slug = 'tax-' . $term->taxonomy . '-' . $term->term_id; | |
| 80 | + } elseif ( is_author() ) { | |
| 81 | + $this->current_page_slug = 'author-' . get_the_author_meta( 'ID' ); | |
| 82 | + } elseif ( is_date() ) { | |
| 83 | + $this->current_page_slug = 'date-archive'; | |
| 84 | + } elseif ( is_singular() ) { | |
| 85 | + $this->current_page_slug = (string) get_the_ID(); // just the post/page/custom ID | |
| 86 | + } else { | |
| 87 | + $this->current_page_slug = (string) get_the_ID(); // fallback to ID | |
| 88 | + }//end if | |
| 89 | + } | |
| 90 | + | |
| 91 | + public function get_current_archive_post_type() { | |
| 92 | + if ( is_post_type_archive() ) { | |
| 93 | + $post_type = get_query_var( 'post_type' ); | |
| 94 | + if ( is_array( $post_type ) ) { | |
| 95 | + $post_type = reset( $post_type ); | |
| 96 | + } | |
| 97 | + return $post_type; | |
| 98 | + } | |
| 99 | + return null; | |
| 100 | + } | |
| 101 | + | |
| 33 | 102 | public function get_localize_script_data() { |
| 34 | 103 | return [ |
| 35 | 104 | 'rest_url' => esc_url_raw( rest_url() ), |
| 36 | 105 | 'namespace' => ABLOCKS_PLUGIN_SLUG . '/v1/', |
| @@ -39,14 +108,90 @@ | ||
| 39 | 108 | 'admin_url' => admin_url(), |
| 40 | 109 | 'site_url' => site_url(), |
| 41 | 110 | 'route_path' => wp_parse_url( admin_url(), PHP_URL_PATH ), |
| 42 | 111 | 'ajax_url' => esc_url( admin_url( 'admin-ajax.php' ) ), |
| 112 | + 'is_pro' => (bool) Helper::is_active_ablocks_pro(), | |
| 113 | + 'is_archive' => (bool) is_archive(), | |
| 114 | + 'archive_post_type' => $this->get_current_archive_post_type(), | |
| 115 | + // Responsive breakpoint widths (px) shared with the JS CSS generators | |
| 116 | + // and the block editor so media queries match the frontend. | |
| 117 | + 'breakpoints' => Helper::get_breakpoints(), | |
| 118 | + // Ordered device list (Desktop + built-ins + custom breakpoints) for | |
| 119 | + // the atomic block's responsive device switcher. Widest-first, so | |
| 120 | + // the editor and the frontend agree on breakpoint precedence. | |
| 121 | + 'responsive_devices' => Helper::get_responsive_devices(), | |
| 122 | + // Whether breakpoint queries overlap (cascade) or are exclusive | |
| 123 | + // bands (strict). The editor preview builds matching queries. | |
| 124 | + 'breakpoint_mode' => Helper::get_breakpoint_mode(), | |
| 125 | + ]; | |
| 126 | + } | |
| 127 | + | |
| 128 | + public function get_localize_dashboard_data() { | |
| 129 | + // Anyone permitted to reach an aBlocks screen needs the nonce, or the | |
| 130 | + // dashboard loads and then 403s on every request it makes. | |
| 131 | + if ( ! current_user_can( Permissions::ACCESS ) ) { | |
| 132 | + return $this->get_localize_script_data(); | |
| 133 | + } | |
| 134 | + return array_merge($this->get_localize_script_data(), [ | |
| 43 | 135 | 'nonce' => wp_create_nonce( 'wp_rest' ), |
| 44 | 136 | 'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ), |
| 45 | - 'is_pro' => (bool) Helper::is_active_ablocks_pro(), | |
| 46 | - ]; | |
| 137 | + 'permissions' => Permissions::for_user(), | |
| 138 | + 'is_admin_user' => Permissions::is_real_admin(), | |
| 139 | + ]); | |
| 47 | 140 | } |
| 141 | + public function get_localize_editor_data() { | |
| 142 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 143 | + return $this->get_localize_script_data(); | |
| 144 | + } | |
| 145 | + return array_merge($this->get_localize_script_data(), [ | |
| 146 | + 'nonce' => wp_create_nonce( 'wp_rest' ), | |
| 147 | + 'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ), | |
| 148 | + ]); | |
| 149 | + } | |
| 150 | + public function get_localize_frontend_data() { | |
| 151 | + $data = array_merge($this->get_localize_script_data(), [ | |
| 152 | + 'nonce' => wp_create_nonce( 'wp_rest' ), | |
| 153 | + 'ablocks_nonce' => wp_create_nonce( 'ablocks_nonce' ), | |
| 154 | + ]); | |
| 155 | + // The absolute server path is only meaningful to admin-side code; on the | |
| 156 | + // frontend it would publish the filesystem layout in page source. No | |
| 157 | + // frontend script reads it (it is re-exported but never consumed). | |
| 158 | + unset( $data['plugin_root_path'] ); | |
| 159 | + return $data; | |
| 160 | + } | |
| 48 | 161 | |
| 162 | + /** | |
| 163 | + * Whether the frontend ABlocksGlobal payload has been attached this request. | |
| 164 | + * | |
| 165 | + * @var bool | |
| 166 | + */ | |
| 167 | + private static $globals_localized = false; | |
| 168 | + | |
| 169 | + /** | |
| 170 | + * Attach the frontend ABlocksGlobal payload to the shared data-only | |
| 171 | + * `ablocks-globals` handle, at most once per request. | |
| 172 | + * | |
| 173 | + * Previously each per-block script handle localized its own copy, so a page | |
| 174 | + * using N block types printed N identical `var ABlocksGlobal = {...}` blocks | |
| 175 | + * (and called wp_create_nonce() N times). Every aBlocks frontend script now | |
| 176 | + * depends on `ablocks-globals`, so WP prints the object once, before any | |
| 177 | + * consumer, in both asset-generation modes. | |
| 178 | + */ | |
| 179 | + public static function localize_globals_once() { | |
| 180 | + if ( self::$globals_localized ) { | |
| 181 | + return; | |
| 182 | + } | |
| 183 | + self::$globals_localized = true; | |
| 184 | + // Self-register so callers never depend on RegisterScripts having run | |
| 185 | + // first — theme-builder templates and block render callbacks fire on | |
| 186 | + // different hooks, and a missing handle would drop the payload silently. | |
| 187 | + if ( ! wp_script_is( 'ablocks-globals', 'registered' ) ) { | |
| 188 | + wp_register_script( 'ablocks-globals', false, array(), ABLOCKS_VERSION, array() ); | |
| 189 | + } | |
| 190 | + $self = new self(); | |
| 191 | + wp_localize_script( 'ablocks-globals', 'ABlocksGlobal', $self->get_localize_frontend_data() ); | |
| 192 | + } | |
| 193 | + | |
| 49 | 194 | public function get_dashboard_localize_script_data() { |
| 50 | 195 | $menu = new Menu(); |
| 51 | 196 | $args = array( |
| 52 | 197 | 'menu' => wp_json_encode( Helper::get_admin_menu_list() ), |
| @@ -56,10 +201,14 @@ | ||
| 56 | 201 | 'container_padding' => Helper::get_settings( 'container_padding', 10 ), |
| 57 | 202 | 'container_element_gap' => Helper::get_settings( 'container_element_gap', 20 ), |
| 58 | 203 | 'enabled_assets_file_generation' => (bool) Helper::get_settings( 'enabled_assets_file_generation', false ), |
| 59 | 204 | 'enabled_block_copy_paste_style' => (bool) Helper::get_settings( 'enabled_block_copy_paste_style', false ), |
| 205 | + 'enabled_load_google_font_locally' => (bool) Helper::get_settings( 'enabled_load_google_font_locally', false ), | |
| 60 | 206 | 'enabled_only_selected_fonts' => (bool) Helper::get_settings( 'enabled_only_selected_fonts', false ), |
| 61 | 207 | 'selected_fonts' => (array) Helper::get_settings( 'selected_fonts', [] ), |
| 208 | + 'font_metric_fallback' => (bool) Helper::get_settings( 'font_metric_fallback', true ), | |
| 209 | + // Global typography picks from the same list the editor offers. | |
| 210 | + 'theme_fonts' => \ABlocks\Classes\FontStack::get_theme_font_families(), | |
| 62 | 211 | ], |
| 63 | 212 | 'is_gutenberg_editor' => Helper::is_gutenberg_editor(), |
| 64 | 213 | 'is_fse_theme' => Helper::is_fse_theme(), |
| 65 | 214 | 'third_party_plugin_status' => [ |
| @@ -65,14 +214,18 @@ | ||
| 65 | 214 | 'third_party_plugin_status' => [ |
| 66 | 215 | 'academy_lms' => Helper::is_active_academy(), |
| 67 | 216 | 'storeengine' => Helper::is_active_storeengine(), |
| 68 | 217 | 'wp_map_block' => Helper::is_active_wp_map_block(), |
| 218 | + 'easy_content_manager' => Helper::is_active_easy_content_manager(), | |
| 219 | + 'zencommunity' => Helper::is_active_zencommunity(), | |
| 220 | + 'gemboards' => Helper::is_active_gemboards(), | |
| 221 | + 'quizpress' => Helper::is_active_quizpress(), | |
| 69 | 222 | ] |
| 70 | 223 | ); |
| 71 | 224 | return apply_filters( |
| 72 | 225 | 'ablocks/assets/dashboard_scripts_data', |
| 73 | 226 | array_merge( |
| 74 | - $this->get_localize_script_data(), | |
| 227 | + $this->get_localize_dashboard_data(), | |
| 75 | 228 | $args |
| 76 | 229 | ) |
| 77 | 230 | ); |
| 78 | 231 | } |
| @@ -77,8 +230,9 @@ | ||
| 77 | 230 | ); |
| 78 | 231 | } |
| 79 | 232 | public function get_editor_localize_script_data() { |
| 80 | 233 | global $ablocks_blocks; |
| 234 | + $post_types = Helper::get_public_post_type_options(); | |
| 81 | 235 | $args = array( |
| 82 | 236 | 'settings' => [ |
| 83 | 237 | 'default_container_width' => Helper::get_settings( 'default_container_width', 1280 ), |
| 84 | 238 | 'container_padding' => Helper::get_settings( 'container_padding', 10 ), |
| @@ -84,10 +238,27 @@ | ||
| 84 | 238 | 'container_padding' => Helper::get_settings( 'container_padding', 10 ), |
| 85 | 239 | 'container_element_gap' => Helper::get_settings( 'container_element_gap', 20 ), |
| 86 | 240 | 'enabled_assets_file_generation' => (bool) Helper::get_settings( 'enabled_assets_file_generation', false ), |
| 87 | 241 | 'enabled_block_copy_paste_style' => (bool) Helper::get_settings( 'enabled_block_copy_paste_style', false ), |
| 242 | + 'enabled_load_google_font_locally' => (bool) Helper::get_settings( 'enabled_load_google_font_locally', false ), | |
| 88 | 243 | 'enabled_only_selected_fonts' => (bool) Helper::get_settings( 'enabled_only_selected_fonts', false ), |
| 89 | 244 | 'selected_fonts' => (array) Helper::get_settings( 'selected_fonts', [] ), |
| 245 | + // Design system lock — the editor hides the custom pickers when these | |
| 246 | + // are on, leaving only the global presets. | |
| 247 | + 'lock_global_typography' => (bool) Helper::get_settings( 'lock_global_typography', false ), | |
| 248 | + 'lock_global_typography_strict' => (bool) Helper::get_settings( 'lock_global_typography_strict', false ), | |
| 249 | + 'lock_global_colors' => (bool) Helper::get_settings( 'lock_global_colors', false ), | |
| 250 | + 'frontend_dashboard_page' => (int) Helper::get_settings( 'frontend_dashboard_page' ), | |
| 251 | + 'global_color' => (array) Helper::get_settings( 'global_color', [] ), | |
| 252 | + 'global_typography' => (array) Helper::get_settings( 'global_typography', [] ), | |
| 253 | + 'global_typography_list' => wp_list_pluck( Helper::get_settings( 'global_typography', [] ), 'value', 'id' ), | |
| 254 | + 'global_font_family_fallback' => (string) Helper::get_settings( 'global_font_family_fallback', 'Sans-serif' ), | |
| 255 | + // Editor paste — see includes/api/paste-controller.php. | |
| 256 | + 'paste_google_docs' => (bool) Helper::get_settings( 'paste_google_docs', true ), | |
| 257 | + 'paste_convert_webp' => (bool) Helper::get_settings( 'paste_convert_webp', true ), | |
| 258 | + // Theme.json + Font Library families, so uploaded/theme fonts are | |
| 259 | + // selectable next to the Google catalog. | |
| 260 | + 'theme_fonts' => \ABlocks\Classes\FontStack::get_theme_font_families(), | |
| 90 | 261 | ], |
| 91 | 262 | 'is_gutenberg_editor' => Helper::is_gutenberg_editor(), |
| 92 | 263 | 'has_required_block_attribute_migration' => get_option( 'ablocks_has_required_block_attribute_migration' ), |
| 93 | 264 | 'third_party_plugin_status' => [ |
| @@ -93,15 +264,22 @@ | ||
| 93 | 264 | 'third_party_plugin_status' => [ |
| 94 | 265 | 'academy_lms' => Helper::is_active_academy(), |
| 95 | 266 | 'storeengine' => Helper::is_active_storeengine(), |
| 96 | 267 | 'wp_map_block' => Helper::is_active_wp_map_block(), |
| 268 | + 'quizpress' => Helper::is_active_quizpress(), | |
| 269 | + 'easy_content_manager' => Helper::is_active_easy_content_manager(), | |
| 97 | 270 | ], |
| 98 | - 'blocks_status' => $ablocks_blocks | |
| 271 | + 'blocks_status' => $ablocks_blocks, | |
| 272 | + 'post_types' => $post_types, | |
| 273 | + // What this user may do inside the editor. Read by | |
| 274 | + // src/utils/permissions.js to hide controls they cannot use. | |
| 275 | + 'permissions' => Permissions::for_user(), | |
| 276 | + 'is_admin_user' => Permissions::is_real_admin(), | |
| 99 | 277 | ); |
| 100 | 278 | return apply_filters( |
| 101 | 279 | 'ablocks/assets/editor_scripts_data', |
| 102 | 280 | array_merge( |
| 103 | - $this->get_localize_script_data(), | |
| 281 | + $this->get_localize_editor_data(), | |
| 104 | 282 | $args |
| 105 | 283 | ) |
| 106 | 284 | ); |
| 107 | 285 | } |
| @@ -184,60 +362,49 @@ | ||
| 184 | 362 | }, |
| 185 | 363 | 1 |
| 186 | 364 | ); |
| 187 | 365 | |
| 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' ); | |
| 366 | + $this->demo_template_importer_scripts(); | |
| 202 | 367 | }//end if |
| 203 | 368 | } |
| 204 | 369 | |
| 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; | |
| 370 | + public function demo_template_importer_scripts() { | |
| 371 | + 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 ); | |
| 372 | + 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' ); | |
| 373 | + 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' ); | |
| 374 | + | |
| 375 | + $dependencies = include ABLOCKS_ASSETS_PATH . 'build/demo-import.asset.php'; | |
| 376 | + wp_enqueue_script( | |
| 377 | + 'ablocks-demo-import-scripts', | |
| 378 | + ABLOCKS_ASSETS_URL . 'build/demo-import.js', | |
| 379 | + $dependencies['dependencies'], | |
| 380 | + $dependencies['version'], | |
| 381 | + true | |
| 382 | + ); | |
| 383 | + wp_localize_script( 'ablocks-demo-import-scripts', 'ABlocksGlobal', $this->get_dashboard_localize_script_data() ); | |
| 384 | + wp_set_script_translations( 'ablocks-demo-import-scripts', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' ); | |
| 211 | 385 | } |
| 212 | 386 | |
| 213 | - public function front_end_google_fonts() { | |
| 214 | - global $ablocks_fonts; | |
| 215 | - | |
| 216 | - if ( empty( $ablocks_fonts ) || ! is_array( $ablocks_fonts ) ) { | |
| 217 | - return false; | |
| 387 | + public function build_google_fonts_url( array $fonts ): string { | |
| 388 | + $family_strings = []; | |
| 389 | + foreach ( $fonts as $family => $weights ) { | |
| 390 | + $family_encoded = str_replace( ' ', '+', $family ); | |
| 391 | + $weights = array_unique( array_map( 'strval', (array) $weights ) ); | |
| 392 | + sort( $weights ); | |
| 393 | + $family_strings[] = ! empty( $weights ) ? $family_encoded . ':wght@' . implode( ';', $weights ) : $family_encoded; | |
| 218 | 394 | } |
| 395 | + return '//fonts.googleapis.com/css2?family=' . implode( '&family=', $family_strings ) . '&display=swap'; | |
| 396 | + } | |
| 219 | 397 | |
| 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; | |
| 398 | + public function web_fonts_url( $font ) { | |
| 399 | + if ( 'off' === _x( 'on', 'Google font: on or off', 'ablocks' ) ) { | |
| 400 | + return ''; | |
| 231 | 401 | } |
| 402 | + return '//fonts.googleapis.com/css2?family=' . $font; | |
| 403 | + } | |
| 232 | 404 | |
| 233 | - // Generate the URL using the web_fonts_url method | |
| 234 | - $google_fonts_url = $this->web_fonts_url( implode( '|', $font_families ) ) . '&display=swap'; | |
| 235 | 405 | |
| 236 | - wp_register_style( 'ablocks-frontend-google-fonts', esc_url( $google_fonts_url ), array(), ABLOCKS_VERSION ); | |
| 237 | - } | |
| 238 | 406 | |
| 239 | - | |
| 240 | 407 | public function block_editor_assets() { |
| 241 | 408 | if ( is_admin() ) { |
| 242 | 409 | wp_enqueue_style( 'ablocks-editor-font-awesome', ABLOCKS_ASSETS_URL . 'library/font-awesome/css/all.min.css', array(), ABLOCKS_VERSION, 'all' ); |
| 243 | 410 | 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 ); |
| @@ -260,44 +427,143 @@ | ||
| 260 | 427 | public function enqueue_frontend_assets() { |
| 261 | 428 | if ( ! Helper::is_enabled_assets_generation() ) { |
| 262 | 429 | return; |
| 263 | 430 | } |
| 264 | - if ( ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) ) { | |
| 431 | + | |
| 432 | + do_action( 'ablocks/before_enqueue_frontend_scripts' ); | |
| 433 | + | |
| 434 | + $script_loading_strategy = Helper::get_script_loading_strategy(); | |
| 435 | + | |
| 436 | + if ( ! $this->is_assets_generated() ) { | |
| 265 | 437 | return; |
| 266 | 438 | } |
| 267 | 439 | |
| 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' ); | |
| 440 | + if ( $this->current_page_slug ) { | |
| 441 | + // Enqueue google fonts | |
| 442 | + wp_enqueue_style( 'ablocks-frontend-google-fonts' ); | |
| 443 | + | |
| 444 | + $css_path = $this->FileUpload->get_file_path( $this->current_page_slug . '.min.css' ); | |
| 445 | + | |
| 446 | + // Performance Suite — inline the page CSS when it's small enough to | |
| 447 | + // avoid a render-blocking request; fall back to a normal <link> above | |
| 448 | + // the threshold. Opt-in via perf_inline_css. | |
| 449 | + $inline_css = (bool) apply_filters( | |
| 450 | + 'ablocks/perf/perf_inline_css', | |
| 451 | + (bool) Helper::get_settings( 'perf_inline_css', true ) | |
| 452 | + ); | |
| 453 | + $inline_max = (int) apply_filters( 'ablocks/perf/inline_css_max_bytes', 50 * 1024 ); | |
| 454 | + $css_size = file_exists( $css_path ) ? (int) filesize( $css_path ) : 0; | |
| 455 | + | |
| 456 | + if ( $inline_css && $css_size > 0 && $css_size <= $inline_max ) { | |
| 457 | + wp_register_style( 'ablocks-blocks-combine-style', false, array(), ABLOCKS_VERSION ); | |
| 458 | + wp_enqueue_style( 'ablocks-blocks-combine-style' ); | |
| 459 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents | |
| 460 | + wp_add_inline_style( 'ablocks-blocks-combine-style', (string) file_get_contents( $css_path ) ); | |
| 461 | + } else { | |
| 462 | + wp_enqueue_style( 'ablocks-blocks-combine-style', $this->FileUpload->get_file_url( $this->current_page_slug . '.min.css' ), array(), filemtime( $css_path ), 'all' ); | |
| 463 | + | |
| 464 | + // Performance Suite — when the page stylesheet is too large to inline, | |
| 465 | + // load it non-blocking (media="print" + onload swap) so it no longer | |
| 466 | + // delays first paint. Opt-in via perf_async_css (default off: a too- | |
| 467 | + // aggressive critical set risks FOUC, so promote after QA). Editors | |
| 468 | + // viewing the frontend are bypassed so previews render immediately. | |
| 469 | + $async_css = (bool) apply_filters( | |
| 470 | + 'ablocks/perf/perf_async_css', | |
| 471 | + (bool) Helper::get_settings( 'perf_async_css', false ) | |
| 472 | + ); | |
| 473 | + // Critical CSS — inline the above-the-fold/structural subset in | |
| 474 | + // <head> and load the full stylesheet non-blocking. This is a | |
| 475 | + // stronger form of async (it prevents the FOUC async alone can | |
| 476 | + // cause), so enabling it implies async delivery of the full file. | |
| 477 | + $critical_css = (bool) apply_filters( | |
| 478 | + 'ablocks/perf/perf_critical_css', | |
| 479 | + (bool) Helper::get_settings( 'perf_critical_css', false ) | |
| 480 | + ); | |
| 481 | + $bypass_for_editor = is_user_logged_in() && current_user_can( 'edit_posts' ) | |
| 482 | + && (bool) apply_filters( 'ablocks/perf/bypass_optimizations_for_editors', true ); | |
| 483 | + | |
| 484 | + if ( ( $async_css || $critical_css ) && ! $bypass_for_editor ) { | |
| 485 | + // Before deferring the full stylesheet, always inline the | |
| 486 | + // layout-critical CSS so the page paints with correct box | |
| 487 | + // sizes and never reflows when the deferred file loads. The | |
| 488 | + // split is property-based: the deferred remainder is cosmetic | |
| 489 | + // only (colors, shadows, hover states), so it cannot cause a | |
| 490 | + // layout shift (CLS). This makes async delivery safe. | |
| 491 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents | |
| 492 | + list( $critical ) = \ABlocks\Classes\CriticalCss::split( (string) file_get_contents( $css_path ) ); | |
| 493 | + if ( '' !== trim( (string) $critical ) ) { | |
| 494 | + wp_register_style( 'ablocks-critical-css', false, array(), ABLOCKS_VERSION ); | |
| 495 | + wp_enqueue_style( 'ablocks-critical-css' ); | |
| 496 | + wp_add_inline_style( 'ablocks-critical-css', $critical ); | |
| 497 | + } | |
| 498 | + add_filter( 'style_loader_tag', [ $this, 'async_combined_style_tag' ], 20, 2 ); | |
| 499 | + } | |
| 278 | 500 | } |
| 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 | - } | |
| 501 | + | |
| 502 | + wp_enqueue_script( 'ablocks-blocks-combine-script', $this->FileUpload->get_file_url( $this->current_page_slug . '.min.js' ), array( 'ablocks-globals' ), filemtime( $this->FileUpload->get_file_path( $this->current_page_slug . '.min.js' ) ), true, [ 'strategy' => $script_loading_strategy ] ); | |
| 503 | + self::localize_globals_once(); | |
| 504 | + wp_set_script_translations( 'ablocks-blocks-combine-script', 'ablocks', ABLOCKS_ROOT_DIR_PATH . 'languages' ); | |
| 505 | + } else { | |
| 506 | + // fallback if assets not available | |
| 507 | + add_filter( 'ablocks/is_allow_block_inline_assets', '__return_true' ); | |
| 285 | 508 | } |
| 286 | 509 | } |
| 287 | 510 | |
| 511 | + /** | |
| 512 | + * Rewrite the combined page stylesheet <link> so it loads without blocking | |
| 513 | + * render: fetch as media="print", then flip to media="all" on load, with a | |
| 514 | + * <noscript> fallback for JS-disabled clients. Only touches the aBlocks | |
| 515 | + * combined handle — third-party stylesheets are left untouched. | |
| 516 | + */ | |
| 517 | + public function async_combined_style_tag( $tag, $handle ) { | |
| 518 | + if ( 'ablocks-blocks-combine-style' !== $handle ) { | |
| 519 | + return $tag; | |
| 520 | + } | |
| 521 | + $async_tag = preg_replace( | |
| 522 | + [ "/media=(['\"])[^'\"]*\\1/", '/>$/', '/\/>$/' ], | |
| 523 | + [ 'media="print" onload="this.media=\'all\'"', '>', '/>' ], | |
| 524 | + trim( $tag ) | |
| 525 | + ); | |
| 526 | + // Guarantee the print/onload attributes even if the original tag had no media. | |
| 527 | + if ( strpos( $async_tag, 'onload=' ) === false ) { | |
| 528 | + $async_tag = str_replace( '<link ', '<link media="print" onload="this.media=\'all\'" ', $async_tag ); | |
| 529 | + } | |
| 530 | + $noscript = '<noscript>' . $tag . '</noscript>'; | |
| 531 | + return $async_tag . "\n" . $noscript; | |
| 532 | + } | |
| 533 | + | |
| 288 | 534 | public function regenerate_missing_assets() { |
| 289 | - if ( ! Helper::is_enabled_assets_generation() ) { | |
| 535 | + if ( $this->is_assets_generated() ) { | |
| 290 | 536 | return; |
| 291 | 537 | } |
| 292 | 538 | |
| 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 ); | |
| 539 | + // classic | |
| 540 | + if ( ! Helper::is_fse_theme() ) { | |
| 541 | + $is_parse_main_content = false; | |
| 542 | + if ( is_array( $this->theme_builder_locations ) ) { | |
| 543 | + foreach ( $this->theme_builder_locations as $location_name => $location_id ) { | |
| 544 | + $post = get_post( $location_id ); | |
| 545 | + if ( is_object( $post ) ) { | |
| 546 | + $this->set_current_page_template_part( $post->post_content, parse_blocks( $post->post_content ) ); | |
| 547 | + } | |
| 548 | + if ( $location_name === 'header' ) { | |
| 549 | + $post = get_post( get_the_ID() ); | |
| 550 | + if ( is_object( $post ) ) { | |
| 551 | + $this->set_current_page_template_part( $post->post_content, parse_blocks( $post->post_content ) ); | |
| 552 | + } | |
| 553 | + $is_parse_main_content = true; | |
| 554 | + } | |
| 555 | + } | |
| 299 | 556 | } |
| 557 | + if ( false === $is_parse_main_content ) { | |
| 558 | + $post = get_post( get_the_ID() ); | |
| 559 | + $this->set_current_page_template_part( $post->post_content, parse_blocks( $post->post_content ) ); | |
| 560 | + } | |
| 561 | + }//end if | |
| 562 | + | |
| 563 | + if ( count( $this->current_page_blocks ) ) { | |
| 564 | + $file_name = $this->current_page_slug; | |
| 565 | + AssetsGenerator::write_frontend_css_in_uploads_folder( $file_name, $this->current_page_blocks ); | |
| 300 | 566 | } |
| 301 | 567 | } |
| 302 | 568 | public function register_scripts() { |
| 303 | 569 | $register_styles = RegisterScripts::get_register_styles(); |
| @@ -307,9 +573,20 @@ | ||
| 307 | 573 | $register_scripts = RegisterScripts::get_register_scripts(); |
| 308 | 574 | foreach ( $register_scripts as $register_handler => $register_script ) { |
| 309 | 575 | if ( isset( $register_script['dependencies'] ) ) { |
| 310 | 576 | $dependencies = include $register_script['dependencies']; |
| 311 | - $register_script['deps'] = $dependencies['dependencies']; | |
| 577 | + // Merge rather than overwrite: the generated asset.php only knows | |
| 578 | + // about build-time (@wordpress/*) dependencies, so replacing the | |
| 579 | + // declared list would silently drop hand-declared handles such as | |
| 580 | + // ablocks-globals. | |
| 581 | + $register_script['deps'] = array_values( | |
| 582 | + array_unique( | |
| 583 | + array_merge( | |
| 584 | + isset( $register_script['deps'] ) ? (array) $register_script['deps'] : array(), | |
| 585 | + $dependencies['dependencies'] | |
| 586 | + ) | |
| 587 | + ) | |
| 588 | + ); | |
| 312 | 589 | $register_script['ver'] = $dependencies['version']; |
| 313 | 590 | } |
| 314 | 591 | wp_register_script( |
| 315 | 592 | $register_handler, |
| @@ -317,26 +594,352 @@ | ||
| 317 | 594 | $register_script['deps'], |
| 318 | 595 | $register_script['ver'], |
| 319 | 596 | $register_script['args'] |
| 320 | 597 | ); |
| 321 | - } | |
| 598 | + }//end foreach | |
| 322 | 599 | } |
| 323 | 600 | public function global_css_variable() { |
| 324 | 601 | wp_register_style( 'ablocks-editor-global-styles', false, array(), ABLOCKS_VERSION ); |
| 325 | 602 | wp_enqueue_style( 'ablocks-editor-global-styles' ); |
| 326 | 603 | |
| 604 | + // The :root variables only change when global settings change, so cache | |
| 605 | + // the generated string and rebuild it on settings save (see Blocks | |
| 606 | + // ::clear_global_css_cache) instead of looping settings on every request. | |
| 607 | + $css = get_transient( 'ablocks_global_css_vars' ); | |
| 608 | + if ( false === $css ) { | |
| 327 | 609 | $container_padding = Helper::get_settings( 'container_padding', 10 ) . 'px'; |
| 328 | - $css = ":root, body .editor-styles-wrapper { | |
| 329 | - --ablocks-container-padding: $container_padding; | |
| 330 | - }"; | |
| 610 | + $css = ":root, body .editor-styles-wrapper {\n"; | |
| 611 | + $css .= " --ablocks-container-padding: $container_padding;\n"; | |
| 612 | + | |
| 613 | + // Colors | |
| 614 | + $global_color = (array) Helper::get_settings( 'global_color', [] ); | |
| 615 | + foreach ( $global_color as $color ) { | |
| 616 | + if ( isset( $color->id, $color->value ) ) { | |
| 617 | + $css .= " --ablocks-{$color->id}: {$color->value};\n"; | |
| 618 | + } | |
| 619 | + } | |
| 620 | + | |
| 621 | + // Typography | |
| 622 | + $global_typography = (array) Helper::get_settings( 'global_typography', [] ); | |
| 623 | + foreach ( $global_typography as $typography ) { | |
| 624 | + if ( isset( $typography->id, $typography->value ) ) { | |
| 625 | + $id = $typography->id; | |
| 626 | + $value = $typography->value; | |
| 627 | + | |
| 628 | + // Desktop values | |
| 629 | + if ( ! empty( $value->fontFamily ) ) { | |
| 630 | + // Every consumer of this variable drops it straight into | |
| 631 | + // font-family, so it has to carry the whole stack. | |
| 632 | + $font_stack = \ABlocks\Classes\FontStack::build( | |
| 633 | + $value->fontFamily, | |
| 634 | + isset( $value->fontFallback ) ? $value->fontFallback : '' | |
| 635 | + ); | |
| 636 | + if ( '' !== $font_stack ) { | |
| 637 | + $css .= " --ablocks-{$id}-font-family: {$font_stack};\n"; | |
| 638 | + } | |
| 639 | + } | |
| 640 | + if ( ! empty( $value->weight ) ) { | |
| 641 | + $css .= " --ablocks-{$id}-weight: {$value->weight};\n"; | |
| 642 | + } | |
| 643 | + if ( ! empty( $value->transform ) ) { | |
| 644 | + $css .= " --ablocks-{$id}-transform: {$value->transform};\n"; | |
| 645 | + } | |
| 646 | + if ( ! empty( $value->style ) ) { | |
| 647 | + $css .= " --ablocks-{$id}-style: {$value->style};\n"; | |
| 648 | + } | |
| 649 | + if ( ! empty( $value->decoration ) ) { | |
| 650 | + $css .= " --ablocks-{$id}-decoration: {$value->decoration};\n"; | |
| 651 | + } | |
| 652 | + if ( ! empty( $value->fontSize ) ) { | |
| 653 | + $unit = ! empty( $value->fontSizeUnit ) ? $value->fontSizeUnit : 'px'; | |
| 654 | + $css .= " --ablocks-{$id}-font-size: {$value->fontSize}{$unit};\n"; | |
| 655 | + } | |
| 656 | + if ( ! empty( $value->lineHeight ) ) { | |
| 657 | + $unit = ! empty( $value->lineHeightUnit ) ? $value->lineHeightUnit : 'px'; | |
| 658 | + $css .= " --ablocks-{$id}-line-height: {$value->lineHeight}{$unit};\n"; | |
| 659 | + } | |
| 660 | + if ( ! empty( $value->letterSpacing ) ) { | |
| 661 | + $unit = ! empty( $value->letterSpacingUnit ) ? $value->letterSpacingUnit : 'px'; | |
| 662 | + $css .= " --ablocks-{$id}-letter-spacing: {$value->letterSpacing}{$unit};\n"; | |
| 663 | + } | |
| 664 | + if ( ! empty( $value->wordSpacing ) ) { | |
| 665 | + $unit = ! empty( $value->wordSpacingUnit ) ? $value->wordSpacingUnit : 'px'; | |
| 666 | + $css .= " --ablocks-{$id}-word-spacing: {$value->wordSpacing}{$unit};\n"; | |
| 667 | + } | |
| 668 | + | |
| 669 | + // Tablet values | |
| 670 | + if ( ! empty( $value->fontSizeTablet ) ) { | |
| 671 | + $unit = ! empty( $value->fontSizeUnitTablet ) ? $value->fontSizeUnitTablet : 'px'; | |
| 672 | + $css .= " --ablocks-{$id}-font-size-tablet: {$value->fontSizeTablet}{$unit};\n"; | |
| 673 | + } | |
| 674 | + if ( ! empty( $value->lineHeightTablet ) ) { | |
| 675 | + $unit = ! empty( $value->lineHeightUnitTablet ) ? $value->lineHeightUnitTablet : 'px'; | |
| 676 | + $css .= " --ablocks-{$id}-line-height-tablet: {$value->lineHeightTablet}{$unit};\n"; | |
| 677 | + } | |
| 678 | + if ( ! empty( $value->letterSpacingTablet ) ) { | |
| 679 | + $unit = ! empty( $value->letterSpacingUnitTablet ) ? $value->letterSpacingUnitTablet : 'px'; | |
| 680 | + $css .= " --ablocks-{$id}-letter-spacing-tablet: {$value->letterSpacingTablet}{$unit};\n"; | |
| 681 | + } | |
| 682 | + if ( ! empty( $value->wordSpacingTablet ) ) { | |
| 683 | + $unit = ! empty( $value->wordSpacingUnitTablet ) ? $value->wordSpacingUnitTablet : 'px'; | |
| 684 | + $css .= " --ablocks-{$id}-word-spacing-tablet: {$value->wordSpacingTablet}{$unit};\n"; | |
| 685 | + } | |
| 686 | + | |
| 687 | + // Mobile values | |
| 688 | + if ( ! empty( $value->fontSizeMobile ) ) { | |
| 689 | + $unit = ! empty( $value->fontSizeUnitMobile ) ? $value->fontSizeUnitMobile : 'px'; | |
| 690 | + $css .= " --ablocks-{$id}-font-size-mobile: {$value->fontSizeMobile}{$unit};\n"; | |
| 691 | + } | |
| 692 | + if ( ! empty( $value->lineHeightMobile ) ) { | |
| 693 | + $unit = ! empty( $value->lineHeightUnitMobile ) ? $value->lineHeightUnitMobile : 'px'; | |
| 694 | + $css .= " --ablocks-{$id}-line-height-mobile: {$value->lineHeightMobile}{$unit};\n"; | |
| 695 | + } | |
| 696 | + if ( ! empty( $value->letterSpacingMobile ) ) { | |
| 697 | + $unit = ! empty( $value->letterSpacingUnitMobile ) ? $value->letterSpacingUnitMobile : 'px'; | |
| 698 | + $css .= " --ablocks-{$id}-letter-spacing-mobile: {$value->letterSpacingMobile}{$unit};\n"; | |
| 699 | + } | |
| 700 | + if ( ! empty( $value->wordSpacingMobile ) ) { | |
| 701 | + $unit = ! empty( $value->wordSpacingUnitMobile ) ? $value->wordSpacingUnitMobile : 'px'; | |
| 702 | + $css .= " --ablocks-{$id}-word-spacing-mobile: {$value->wordSpacingMobile}{$unit};\n"; | |
| 703 | + } | |
| 704 | + }//end if | |
| 705 | + }//end foreach | |
| 706 | + | |
| 707 | + $css .= "}\n"; | |
| 708 | + set_transient( 'ablocks_global_css_vars', $css, DAY_IN_SECONDS ); | |
| 709 | + } | |
| 710 | + | |
| 711 | + if ( ! is_admin() && ! Helper::is_gutenberg_editor() ) { | |
| 712 | + $css .= $this->get_common_css(); | |
| 713 | + } | |
| 331 | 714 | wp_add_inline_style( 'ablocks-editor-global-styles', $css ); |
| 715 | + wp_add_inline_style( 'ablocks-common-style', $css ); | |
| 332 | 716 | } |
| 333 | 717 | |
| 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' ); | |
| 718 | + public function editor_google_fonts() { | |
| 719 | + // Load the fonts already used by the post being edited (plus globals) so | |
| 720 | + // existing content previews correctly on editor load. Live selections are | |
| 721 | + // handled client-side by the typography control. | |
| 722 | + $fonts = \ABlocks\Classes\FontCollector::get_global_fonts(); | |
| 337 | 723 | |
| 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' ); | |
| 724 | + $post_id = 0; | |
| 725 | + if ( function_exists( 'get_the_ID' ) && get_the_ID() ) { | |
| 726 | + $post_id = (int) get_the_ID(); | |
| 727 | + } elseif ( isset( $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 728 | + $post_id = absint( $_GET['post'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 729 | + } | |
| 730 | + if ( $post_id ) { | |
| 731 | + $fonts = \ABlocks\Classes\FontCollector::merge( $fonts, \ABlocks\Classes\FontCollector::get_post_fonts( $post_id ) ); | |
| 732 | + } | |
| 733 | + | |
| 734 | + if ( empty( $fonts ) ) { | |
| 735 | + return; | |
| 736 | + } | |
| 737 | + $url = $this->build_google_fonts_url( $fonts ); | |
| 738 | + wp_enqueue_style( 'ablocks-editor-google-fonts', $url, array(), null ); | |
| 340 | 739 | } |
| 341 | 740 | |
| 741 | + public function add_editor_inline_css() { | |
| 742 | + $custom_css = $this->get_common_css( '.editor-styles-wrapper ' ); | |
| 743 | + add_theme_support( 'editor-styles' ); | |
| 744 | + add_editor_style(); | |
| 745 | + wp_add_inline_style( 'wp-block-library', $custom_css ); | |
| 746 | + } | |
| 747 | + | |
| 748 | + public function get_common_css( $parent_selector = '' ) { | |
| 749 | + global $ablocks_settings; | |
| 750 | + $attributes = [ | |
| 751 | + 'global_body_text_color' => $ablocks_settings->global_body_text_color, | |
| 752 | + 'global_body_typography' => $ablocks_settings->global_body_typography, | |
| 753 | + 'global_body_paragraph_space' => $ablocks_settings->global_body_paragraph_space, | |
| 754 | + 'global_link_color' => $ablocks_settings->global_link_color, | |
| 755 | + 'global_link_hover_color' => $ablocks_settings->global_link_hover_color, | |
| 756 | + 'global_link_typography' => $ablocks_settings->global_link_typography, | |
| 757 | + 'global_link_hover_typography' => $ablocks_settings->global_link_hover_typography, | |
| 758 | + 'global_h1_color' => $ablocks_settings->global_h1_color, | |
| 759 | + 'global_h1_typography' => $ablocks_settings->global_h1_typography, | |
| 760 | + 'global_h2_color' => $ablocks_settings->global_h2_color, | |
| 761 | + 'global_h2_typography' => $ablocks_settings->global_h2_typography, | |
| 762 | + 'global_h3_color' => $ablocks_settings->global_h3_color, | |
| 763 | + 'global_h3_typography' => $ablocks_settings->global_h3_typography, | |
| 764 | + 'global_h4_color' => $ablocks_settings->global_h4_color, | |
| 765 | + 'global_h4_typography' => $ablocks_settings->global_h4_typography, | |
| 766 | + 'global_h5_color' => $ablocks_settings->global_h5_color, | |
| 767 | + 'global_h5_typography' => $ablocks_settings->global_h5_typography, | |
| 768 | + 'global_h6_color' => $ablocks_settings->global_h6_color, | |
| 769 | + 'global_h6_typography' => $ablocks_settings->global_h6_typography, | |
| 770 | + ]; | |
| 771 | + $css_generator = new GlobalCssGenerator( $attributes ); | |
| 772 | + | |
| 773 | + $css_generator->add_class_styles( | |
| 774 | + $parent_selector . 'body', | |
| 775 | + $css_generator->get_body_css( $attributes ), | |
| 776 | + $css_generator->get_body_css( $attributes, 'Tablet' ), | |
| 777 | + $css_generator->get_body_css( $attributes, 'Mobile' ) | |
| 778 | + ); | |
| 779 | + $css_generator->add_class_styles( | |
| 780 | + $parent_selector . 'p', | |
| 781 | + $css_generator->get_body_paragraph_css( $attributes ), | |
| 782 | + $css_generator->get_body_paragraph_css( $attributes, 'Tablet' ), | |
| 783 | + $css_generator->get_body_paragraph_css( $attributes, 'Mobile' ) | |
| 784 | + ); | |
| 785 | + $css_generator->add_class_styles( | |
| 786 | + $parent_selector . 'a', | |
| 787 | + $css_generator->get_body_anchor_css( $attributes ), | |
| 788 | + $css_generator->get_body_anchor_css( $attributes, 'Tablet' ), | |
| 789 | + $css_generator->get_body_anchor_css( $attributes, 'Mobile' ) | |
| 790 | + ); | |
| 791 | + $css_generator->add_class_styles( | |
| 792 | + $parent_selector . 'a:hover', | |
| 793 | + $css_generator->get_body_anchor_hover_css( $attributes ), | |
| 794 | + $css_generator->get_body_anchor_hover_css( $attributes, 'Tablet' ), | |
| 795 | + $css_generator->get_body_anchor_hover_css( $attributes, 'Mobile' ) | |
| 796 | + ); | |
| 797 | + $css_generator->add_class_styles( | |
| 798 | + $parent_selector . 'h1', | |
| 799 | + $css_generator->get_global_h1_css( $attributes ), | |
| 800 | + $css_generator->get_global_h1_css( $attributes, 'Tablet' ), | |
| 801 | + $css_generator->get_global_h1_css( $attributes, 'Mobile' ) | |
| 802 | + ); | |
| 803 | + $css_generator->add_class_styles( | |
| 804 | + $parent_selector . 'h2', | |
| 805 | + $css_generator->get_global_h2_css( $attributes ), | |
| 806 | + $css_generator->get_global_h2_css( $attributes, 'Tablet' ), | |
| 807 | + $css_generator->get_global_h2_css( $attributes, 'Mobile' ) | |
| 808 | + ); | |
| 809 | + $css_generator->add_class_styles( | |
| 810 | + $parent_selector . 'h3', | |
| 811 | + $css_generator->get_global_h3_css( $attributes ), | |
| 812 | + $css_generator->get_global_h3_css( $attributes, 'Tablet' ), | |
| 813 | + $css_generator->get_global_h3_css( $attributes, 'Mobile' ) | |
| 814 | + ); | |
| 815 | + $css_generator->add_class_styles( | |
| 816 | + $parent_selector . 'h4', | |
| 817 | + $css_generator->get_global_h4_css( $attributes ), | |
| 818 | + $css_generator->get_global_h4_css( $attributes, 'Tablet' ), | |
| 819 | + $css_generator->get_global_h4_css( $attributes, 'Mobile' ) | |
| 820 | + ); | |
| 821 | + $css_generator->add_class_styles( | |
| 822 | + $parent_selector . 'h5', | |
| 823 | + $css_generator->get_global_h5_css( $attributes ), | |
| 824 | + $css_generator->get_global_h5_css( $attributes, 'Tablet' ), | |
| 825 | + $css_generator->get_global_h5_css( $attributes, 'Mobile' ) | |
| 826 | + ); | |
| 827 | + $css_generator->add_class_styles( | |
| 828 | + $parent_selector . 'h6', | |
| 829 | + $css_generator->get_global_h6_css( $attributes ), | |
| 830 | + $css_generator->get_global_h6_css( $attributes, 'Tablet' ), | |
| 831 | + $css_generator->get_global_h6_css( $attributes, 'Mobile' ) | |
| 832 | + ); | |
| 833 | + return $css_generator->generate_css(); | |
| 834 | + } | |
| 835 | + | |
| 836 | + | |
| 837 | + public function is_assets_generated() { | |
| 838 | + $file_name = $this->current_page_slug; | |
| 839 | + $css_file_path = $this->FileUpload->get_file_path( $file_name . '.min.css' ); | |
| 840 | + | |
| 841 | + if ( ! file_exists( $css_file_path ) ) { | |
| 842 | + return false; | |
| 843 | + } | |
| 844 | + | |
| 845 | + // The file embeds the global-class CSS this page needs, so editing a | |
| 846 | + // class has to make every generated stylesheet stale. Comparing the | |
| 847 | + // file's mtime against the library's change stamp rebuilds them lazily, | |
| 848 | + // one page at a time on next visit, without stamping a revision into | |
| 849 | + // every file name (which the per-post delete in Blocks relies on). | |
| 850 | + // | |
| 851 | + // Strictly greater, not >=: mtime has one-second resolution, so a file | |
| 852 | + // written in the same second as a class edit is indistinguishable from | |
| 853 | + // one written just after it. Treating that tie as stale costs one extra | |
| 854 | + // regeneration; treating it as fresh would serve the old CSS until the | |
| 855 | + // next edit. | |
| 856 | + return filemtime( $css_file_path ) > max( | |
| 857 | + GlobalClasses::get_revision(), | |
| 858 | + self::build_revision() | |
| 859 | + ); | |
| 860 | + } | |
| 861 | + | |
| 862 | + /** | |
| 863 | + * When this plugin's own CSS last changed, as a timestamp. | |
| 864 | + * | |
| 865 | + * The generated file bakes in each block's static stylesheet (see | |
| 866 | + * AssetsGenerator, which concatenates get_static_css() into it), so a CSS | |
| 867 | + * fix shipped in an update reached nobody whose pages were generated before | |
| 868 | + * it: the file still existed, still parsed, and nothing about it looked | |
| 869 | + * stale, so it was served unchanged forever. Verified by planting a | |
| 870 | + * stylesheet carrying the previous release's rules — it survived every | |
| 871 | + * subsequent page load untouched. That is why a fix could land in the | |
| 872 | + * editor, which loads each block's style.css directly, and never appear on | |
| 873 | + * the front end. | |
| 874 | + * | |
| 875 | + * Keyed on the version rather than a file scan: it is one option read on | |
| 876 | + * the common path, and every shipped CSS change comes with a version bump. | |
| 877 | + * Each page then rebuilds once, on its next visit, exactly the way a | |
| 878 | + * global-class edit already makes them rebuild. | |
| 879 | + * | |
| 880 | + * @return int Timestamp of the running version's first request. | |
| 881 | + */ | |
| 882 | + public static function build_revision() { | |
| 883 | + $stamp = get_option( self::BUILD_OPTION ); | |
| 884 | + | |
| 885 | + if ( | |
| 886 | + is_array( $stamp ) && | |
| 887 | + isset( $stamp['version'], $stamp['time'] ) && | |
| 888 | + ABLOCKS_VERSION === $stamp['version'] | |
| 889 | + ) { | |
| 890 | + return (int) $stamp['time']; | |
| 891 | + } | |
| 892 | + | |
| 893 | + $now = time(); | |
| 894 | + update_option( | |
| 895 | + self::BUILD_OPTION, | |
| 896 | + [ | |
| 897 | + 'version' => ABLOCKS_VERSION, | |
| 898 | + 'time' => $now, | |
| 899 | + ], | |
| 900 | + true | |
| 901 | + ); | |
| 902 | + | |
| 903 | + return $now; | |
| 904 | + } | |
| 905 | + | |
| 906 | + public function set_current_page_template_part( $content, $block ) { | |
| 907 | + if ( ! isset( $block['blockName'] ) && is_array( $block ) ) { | |
| 908 | + foreach ( $block as $block_item ) { | |
| 909 | + if ( $this->is_page_asset_block( $block_item ) ) { | |
| 910 | + $this->current_page_blocks[] = $block_item; | |
| 911 | + } | |
| 912 | + } | |
| 913 | + } | |
| 914 | + if ( $this->is_page_asset_block( $block ) ) { | |
| 915 | + $this->current_page_blocks[] = $block; | |
| 916 | + } | |
| 917 | + return $content; | |
| 918 | + } | |
| 919 | + | |
| 920 | + /** | |
| 921 | + * Whether a top-level block contributes to the page's generated assets. | |
| 922 | + * | |
| 923 | + * A synced pattern counts too: AssetsGenerator::recursive_block_parser() | |
| 924 | + * already expands its reference. Collecting only `ablocks/*` names left a | |
| 925 | + * page whose content is just a pattern with no combined CSS/JS at all — so a | |
| 926 | + * Loop Filter placed from a pattern rendered unstyled and did nothing when | |
| 927 | + * clicked. | |
| 928 | + * | |
| 929 | + * @param mixed $block Parsed block. | |
| 930 | + * @return bool | |
| 931 | + */ | |
| 932 | + private function is_page_asset_block( $block ) { | |
| 933 | + if ( ! is_array( $block ) || empty( $block['blockName'] ) ) { | |
| 934 | + return false; | |
| 935 | + } | |
| 936 | + if ( 'core/block' === $block['blockName'] ) { | |
| 937 | + return ! empty( $block['attrs']['ref'] ); | |
| 938 | + } | |
| 939 | + return strpos( $block['blockName'], 'ablocks/' ) !== false; | |
| 940 | + } | |
| 941 | + | |
| 942 | + public function set_theme_builder_locations( $args ) { | |
| 943 | + $this->theme_builder_locations = $args; | |
| 944 | + } | |
| 342 | 945 | } |