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