| @@ -1,115 +1,130 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | // Exit if accessed directly. |
| 4 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | - exit; | |
| 4 | +if (! defined('ABSPATH')) { | |
| 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 | + { | |
| 17 | + $needle_len = strlen($needle); | |
| 18 | + return ($needle_len === 0 || 0 === substr_compare($haystack, $needle, -$needle_len)); | |
| 19 | + } | |
| 20 | +} | |
| 7 | 21 | |
| 8 | -if ( ! class_exists( 'blockspare_Google_Fonts' ) ) { | |
| 9 | - class blockspare_Google_Fonts { | |
| 22 | +if (! class_exists('blockspare_Google_Fonts')) { | |
| 23 | + class blockspare_Google_Fonts | |
| 24 | + { | |
| 10 | 25 | |
| 11 | - function __construct() { | |
| 12 | - add_action( 'wp_head', array( $this, 'enqueue_frontend_block_fonts' ), 100 ); | |
| 13 | - } | |
| 26 | + public static $google_fonts = []; | |
| 14 | 27 | |
| 15 | - public function enqueue_frontend_block_fonts() { | |
| 16 | - | |
| 17 | - | |
| 18 | - | |
| 19 | - if ( ! apply_filters( 'blockspare_enqueue_fonts', true ) ) { | |
| 20 | - return; | |
| 21 | - } | |
| 28 | + function __construct() | |
| 29 | + { | |
| 22 | 30 | |
| 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 ); | |
| 33 | - } | |
| 34 | - } | |
| 35 | - } | |
| 36 | - } | |
| 31 | + add_filter('render_block', array($this, 'bs_gather_google_fonts'), 10, 2); | |
| 32 | + } | |
| 37 | 33 | |
| 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 ); | |
| 42 | - } | |
| 34 | + public function bs_gather_google_fonts($block_content, $block) | |
| 35 | + { | |
| 36 | + if ($block_content === null) { | |
| 37 | + return $block_content; | |
| 38 | + } | |
| 43 | 39 | |
| 44 | - public function is_web_font( $font_name ) { | |
| 45 | - | |
| 46 | - return ! in_array( $font_name , [ 'Arial', 'Times New Roman', 'Helvetica', 'Georgia' ] ); | |
| 47 | - } | |
| 40 | + $block_name = isset($block['blockName']) ? $block['blockName'] : ''; | |
| 41 | + if ($this->is_blockspare_block($block_name) && is_array($block['attrs'])) { | |
| 42 | + // if (stripos($block_content, 'family') !== false) { | |
| 43 | + foreach ($block['attrs'] as $attr_name => $font_name) { | |
| 44 | + if (bs_ends_with(strtolower($attr_name), 'fontfamily')) { | |
| 45 | + self::register_font($font_name); | |
| 46 | + } | |
| 47 | + } | |
| 48 | + //} | |
| 49 | + } | |
| 48 | 50 | |
| 49 | - public function is_blockspare_block( $block_name ) { | |
| 50 | - return strpos( $block_name, 'blockspare/' ) === 0; | |
| 51 | - } | |
| 51 | + return $block_content; | |
| 52 | + } | |
| 52 | 53 | |
| 53 | - public function gather_google_fonts( $blocks ) { | |
| 54 | - $google_fonts = array(); | |
| 55 | - foreach ( $blocks as $block ) { | |
| 54 | + public static function enqueue_frontend_block_fonts() | |
| 55 | + { | |
| 56 | + self::enqueue_google_fonts(array_unique(self::$google_fonts)); | |
| 57 | + } | |
| 56 | 58 | |
| 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 | - } | |
| 69 | - } | |
| 70 | - if ( ! empty( $block['innerBlocks'] ) ) { | |
| 71 | - $google_fonts = array_unique( array_merge( $google_fonts, $this->gather_google_fonts( $block['innerBlocks'] ) ) ); | |
| 72 | - } | |
| 73 | - } | |
| 59 | + public static function is_web_font($font_name) | |
| 60 | + { | |
| 74 | 61 | |
| 75 | - return $google_fonts; | |
| 76 | - } | |
| 62 | + return ! in_array(strtolower($font_name), ['Arial', 'Times New Roman', 'Helvetica', 'Georgia']); | |
| 63 | + } | |
| 77 | 64 | |
| 78 | - /** | |
| 79 | - * Based on: https://github.com/elementor/elementor/blob/bc251b81afb626c4c47029aea8a762566524a811/includes/frontend.php#L647 | |
| 80 | - */ | |
| 81 | - public function enqueue_google_fonts( $google_fonts ) { | |
| 82 | - if ( ! count( $google_fonts) ) { | |
| 83 | - return; | |
| 84 | - } | |
| 65 | + public static function is_blockspare_block($block_name) | |
| 66 | + { | |
| 67 | + if (!empty($block_name)) { | |
| 68 | + return strpos($block_name, 'blockspare/') === 0; | |
| 69 | + } | |
| 70 | + return false; | |
| 71 | + } | |
| 85 | 72 | |
| 86 | - foreach ( $google_fonts as &$font ) { | |
| 87 | - $font = str_replace( ' ', '+', $font ) . ':100,100italic,200,200italic,300,300italic,400,400italic,500,500italic,600,600italic,700,700italic,800,800italic,900,900italic'; | |
| 88 | - } | |
| 73 | + public static function register_font($font_name) | |
| 74 | + { | |
| 75 | + if (! self::is_web_font($font_name)) { | |
| 76 | + return; | |
| 77 | + } | |
| 89 | 78 | |
| 90 | - $fonts_url = sprintf( 'https://fonts.googleapis.com/css?family=%s', implode( rawurlencode( '|' ), $google_fonts ) ); | |
| 79 | + if (! in_array($font_name, self::$google_fonts)) { | |
| 80 | + // Allow themes to disable enqueuing fonts, helpful for custom fonts. | |
| 81 | + if (apply_filters('blockspare_enqueue_font', true, $font_name)) { | |
| 82 | + self::$google_fonts[] = $font_name; | |
| 91 | 83 | |
| 92 | - $subsets = [ | |
| 93 | - 'ru_RU' => 'cyrillic', | |
| 94 | - 'bg_BG' => 'cyrillic', | |
| 95 | - 'he_IL' => 'hebrew', | |
| 96 | - 'el' => 'greek', | |
| 97 | - 'vi' => 'vietnamese', | |
| 98 | - 'uk' => 'cyrillic', | |
| 99 | - 'cs_CZ' => 'latin-ext', | |
| 100 | - 'ro_RO' => 'latin-ext', | |
| 101 | - 'pl_PL' => 'latin-ext', | |
| 102 | - ]; | |
| 84 | + // Enqueue the fonts in the footer. | |
| 85 | + add_filter('wp_footer', array('blockspare_Google_Fonts', 'enqueue_frontend_block_fonts')); | |
| 86 | + } | |
| 87 | + } | |
| 88 | + } | |
| 103 | 89 | |
| 104 | - $locale = get_locale(); | |
| 105 | - if ( isset( $subsets[ $locale ] ) ) { | |
| 106 | - $fonts_url .= '&subset=' . $subsets[ $locale ]; | |
| 107 | - } | |
| 108 | 90 | |
| 109 | - wp_enqueue_style( 'blockspare-google-fonts', $fonts_url ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion | |
| 110 | - } | |
| 111 | 91 | |
| 112 | - } | |
| 113 | 92 | |
| 114 | - new blockspare_Google_Fonts(); | |
| 93 | + /** | |
| 94 | + * Based on: https://github.com/elementor/elementor/blob/bc251b81afb626c4c47029aea8a762566524a811/includes/frontend.php#L647 | |
| 95 | + */ | |
| 96 | + public static function enqueue_google_fonts($google_fonts, $handle = 'blockspare-google-fonts') | |
| 97 | + { | |
| 98 | + if (! count($google_fonts)) { | |
| 99 | + return; | |
| 100 | + } | |
| 101 | + | |
| 102 | + foreach ($google_fonts as &$font) { | |
| 103 | + $font = str_replace(' ', '+', $font) . ':100,100italic,200,200italic,300,300italic,400,400italic,500,500italic,600,600italic,700,700italic,800,800italic,900,900italic'; | |
| 104 | + } | |
| 105 | + | |
| 106 | + $fonts_url = sprintf('https://fonts.googleapis.com/css?family=%s', implode(rawurlencode('|'), $google_fonts)); | |
| 107 | + | |
| 108 | + $subsets = [ | |
| 109 | + 'ru_RU' => 'cyrillic', | |
| 110 | + 'bg_BG' => 'cyrillic', | |
| 111 | + 'he_IL' => 'hebrew', | |
| 112 | + 'el' => 'greek', | |
| 113 | + 'vi' => 'vietnamese', | |
| 114 | + 'uk' => 'cyrillic', | |
| 115 | + 'cs_CZ' => 'latin-ext', | |
| 116 | + 'ro_RO' => 'latin-ext', | |
| 117 | + 'pl_PL' => 'latin-ext', | |
| 118 | + ]; | |
| 119 | + | |
| 120 | + $locale = get_locale(); | |
| 121 | + if (isset($subsets[$locale])) { | |
| 122 | + $fonts_url .= '&subset=' . $subsets[$locale]; | |
| 123 | + } | |
| 124 | + | |
| 125 | + wp_enqueue_style($handle, $fonts_url); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion | |
| 126 | + } | |
| 127 | + } | |
| 128 | + | |
| 129 | + new blockspare_Google_Fonts(); | |
| 115 | 130 | } |