| @@ -3,83 +3,91 @@ | ||
| 3 | 3 | // Exit if accessed directly. |
| 4 | 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | 5 | exit; |
| 6 | 6 | } |
| 7 | +if ( ! function_exists( 'bs_ends_with' ) ) { | |
| 8 | + /** | |
| 9 | + * This function is only available in PHP 8.0 and above. | |
| 10 | + * | |
| 11 | + * @param {string} $haystack | |
| 12 | + * @param {string} $needle | |
| 13 | + * @return {boolean} | |
| 14 | + */ | |
| 15 | + function bs_ends_with( $haystack, $needle ) { | |
| 16 | + $needle_len = strlen( $needle ); | |
| 17 | + return ( $needle_len === 0 || 0 === substr_compare( $haystack, $needle, - $needle_len ) ); | |
| 18 | + } | |
| 19 | +} | |
| 7 | 20 | |
| 8 | 21 | if ( ! class_exists( 'blockspare_Google_Fonts' ) ) { |
| 9 | 22 | class blockspare_Google_Fonts { |
| 10 | 23 | |
| 24 | + public static $google_fonts = []; | |
| 25 | + | |
| 11 | 26 | function __construct() { |
| 12 | - add_action( 'wp_head', array( $this, 'enqueue_frontend_block_fonts' ), 100 ); | |
| 27 | + | |
| 28 | + add_filter( 'render_block', array( $this, 'bs_gather_google_fonts' ), 10, 2 ); | |
| 13 | 29 | } |
| 14 | 30 | |
| 15 | - public function enqueue_frontend_block_fonts() { | |
| 16 | - | |
| 17 | - | |
| 18 | - | |
| 19 | - if ( ! apply_filters( 'blockspare_enqueue_fonts', true ) ) { | |
| 20 | - return; | |
| 31 | + public function bs_gather_google_fonts($block_content, $block ) { | |
| 32 | + if ( $block_content === null ) { | |
| 33 | + return $block_content; | |
| 21 | 34 | } |
| 22 | 35 | |
| 23 | - if ( is_single() || is_page() || is_404() ) { | |
| 24 | - global $post; | |
| 25 | - if ( is_object( $post ) && property_exists( $post, 'post_content' ) ) { | |
| 26 | - $this->_enqueue_frontend_block_fonts( $post->post_content ); | |
| 27 | - } | |
| 28 | - } elseif ( is_archive() || is_home() || is_search() ) { | |
| 29 | - global $wp_query; | |
| 30 | - foreach ( $wp_query as $post ) { | |
| 31 | - if ( is_object( $post ) && property_exists( $post, 'post_content' ) ) { | |
| 32 | - $this->_enqueue_frontend_block_fonts( $post->post_content ); | |
| 36 | + $block_name = isset( $block['blockName'] ) ? $block['blockName'] : ''; | |
| 37 | + if ( $this->is_blockspare_block( $block_name ) && is_array( $block['attrs'] ) ) { | |
| 38 | + if ( stripos( $block_content, 'family' ) !== false ) { | |
| 39 | + foreach ( $block['attrs'] as $attr_name => $font_name ) { | |
| 40 | + if ( bs_ends_with( strtolower( $attr_name ), 'fontfamily' ) ) { | |
| 41 | + self::register_font( $font_name ); | |
| 42 | + } | |
| 33 | 43 | } |
| 34 | 44 | } |
| 35 | 45 | } |
| 46 | + | |
| 47 | + return $block_content; | |
| 36 | 48 | } |
| 37 | 49 | |
| 38 | - public function _enqueue_frontend_block_fonts( $content ) { | |
| 39 | - $blocks = parse_blocks( $content ); | |
| 40 | - $google_fonts = $this->gather_google_fonts( $blocks ); | |
| 41 | - $this->enqueue_google_fonts( $google_fonts ); | |
| 50 | + public static function enqueue_frontend_block_fonts() { | |
| 51 | + self::enqueue_google_fonts( array_unique( self::$google_fonts ) ); | |
| 42 | 52 | } |
| 43 | 53 | |
| 44 | - public function is_web_font( $font_name ) { | |
| 54 | + public static function is_web_font( $font_name ) { | |
| 45 | 55 | |
| 46 | - return ! in_array( $font_name , [ 'Arial', 'Times New Roman', 'Helvetica', 'Georgia' ] ); | |
| 56 | + return ! in_array( strtolower($font_name) , [ 'Arial', 'Times New Roman', 'Helvetica', 'Georgia' ] ); | |
| 47 | 57 | } |
| 48 | 58 | |
| 49 | - public function is_blockspare_block( $block_name ) { | |
| 50 | - return strpos( $block_name, 'blockspare/' ) === 0; | |
| 59 | + public static function is_blockspare_block( $block_name ) { | |
| 60 | + if(!empty($block_name)){ | |
| 61 | + return strpos( $block_name, 'blockspare/' ) === 0; | |
| 62 | + | |
| 63 | + } | |
| 64 | + return false; | |
| 51 | 65 | } |
| 52 | 66 | |
| 53 | - public function gather_google_fonts( $blocks ) { | |
| 54 | - $google_fonts = array(); | |
| 55 | - foreach ( $blocks as $block ) { | |
| 67 | + public static function register_font( $font_name ) { | |
| 68 | + if ( ! self::is_web_font( $font_name ) ) { | |
| 69 | + return; | |
| 70 | + } | |
| 56 | 71 | |
| 57 | - // Gather all "fontFamily" attribute values | |
| 58 | - if ( $this->is_blockspare_block( $block['blockName'] ) ) { | |
| 59 | - foreach ( $block['attrs'] as $attr_name => $font_name ) { | |
| 60 | - if ( preg_match( '/fontFamily$/i', $attr_name ) ) { | |
| 61 | - if ( ! $this->is_web_font( $font_name ) ) { | |
| 62 | - continue; | |
| 63 | - } | |
| 64 | - if ( ! in_array( $font_name, $google_fonts ) ) { | |
| 65 | - $google_fonts[] = $font_name; | |
| 66 | - } | |
| 67 | - } | |
| 68 | - } | |
| 72 | + if ( ! in_array( $font_name, self::$google_fonts ) ) { | |
| 73 | + // Allow themes to disable enqueuing fonts, helpful for custom fonts. | |
| 74 | + if ( apply_filters( 'blockspare_enqueue_font', true, $font_name ) ) { | |
| 75 | + self::$google_fonts[] = $font_name; | |
| 76 | + | |
| 77 | + // Enqueue the fonts in the footer. | |
| 78 | + add_filter( 'wp_footer', array( 'blockspare_Google_Fonts', 'enqueue_frontend_block_fonts' ) ); | |
| 69 | 79 | } |
| 70 | - if ( ! empty( $block['innerBlocks'] ) ) { | |
| 71 | - $google_fonts = array_unique( array_merge( $google_fonts, $this->gather_google_fonts( $block['innerBlocks'] ) ) ); | |
| 72 | - } | |
| 73 | 80 | } |
| 81 | + } | |
| 74 | 82 | |
| 75 | - return $google_fonts; | |
| 76 | - } | |
| 77 | 83 | |
| 84 | + | |
| 85 | + | |
| 78 | 86 | /** |
| 79 | 87 | * Based on: https://github.com/elementor/elementor/blob/bc251b81afb626c4c47029aea8a762566524a811/includes/frontend.php#L647 |
| 80 | 88 | */ |
| 81 | - public function enqueue_google_fonts( $google_fonts ) { | |
| 89 | + public static function enqueue_google_fonts( $google_fonts,$handle ='blockspare-google-fonts' ) { | |
| 82 | 90 | if ( ! count( $google_fonts) ) { |
| 83 | 91 | return; |
| 84 | 92 | } |
| 85 | 93 | |
| @@ -105,9 +113,9 @@ | ||
| 105 | 113 | if ( isset( $subsets[ $locale ] ) ) { |
| 106 | 114 | $fonts_url .= '&subset=' . $subsets[ $locale ]; |
| 107 | 115 | } |
| 108 | 116 | |
| 109 | - wp_enqueue_style( 'blockspare-google-fonts', $fonts_url ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion | |
| 117 | + wp_enqueue_style( $handle, $fonts_url ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion | |
| 110 | 118 | } |
| 111 | 119 | |
| 112 | 120 | } |
| 113 | 121 | |