PluginProbe
Toggle Content / trunk
Toggle Content vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.8 1.3.0
toggle-content / includes / font-loader.php

font-loader.php in Toggle Content trunk, at includes/font-loader.php

127 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Load google fonts.
4 */
5
6 // Exit if accessed directly.
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit;
9 }
10
11 class Toggle_Font_Loader {
12
13 protected static $instances = null;
14
15 public static $gfonts = [];
16 private static $block_name = '';
17
18 /**
19 * Registers the plugin.
20 */
21 public static function get_instance( ...$args ) {
22 if ( self::$instances == null ) {
23 self::$instances = new static( ...$args );
24 }
25 return self::$instances;
26 }
27
28 public function __construct( $block_name ) {
29 self::$block_name = $block_name;
30 //Get font from each block loaded in page
31 add_filter( 'render_block', [$this, 'get_fonts_on_render_block'], 10, 2 );
32 // add_filter( 'wp_enqueue_scripts', [$this, 'eb_enqueue_fonts'], 15 );
33 add_action( 'wp_footer', [$this, 'eb_enqueue_fonts'], 15 );
34 }
35
36 /**
37 * Run font loader after all block render
38 * @since 4.0.2
39 * @access public
40 */
41 public function eb_enqueue_fonts() {
42 $this->fonts_loader();
43 }
44
45 /**
46 * Get Attributes on block render
47 * @since 4.0.2
48 * @access public
49 */
50 public function get_fonts_on_render_block( $block_content, $block ) {
51 if ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) {
52 $block_name = isset( $block['blockName'] ) ? $block['blockName'] : '';
53 if ( 'essential-blocks' === self::$block_name || $block_name === self::$block_name ) {
54 $fonts = self::get_fonts_family( $block['attrs'] );
55 self::$gfonts = array_unique( array_merge( self::$gfonts, $fonts ) );
56 }
57 }
58
59 return $block_content;
60 }
61
62 /**
63 * Generate Font family from Attributes
64 * @since 4.0.0
65 * @access public
66 */
67 public static function get_fonts_family( $attributes ) {
68 $googleFontFamily = [];
69 if ( ! is_array( $attributes ) || empty( $attributes ) ) {
70 return $googleFontFamily;
71 }
72 $keys = preg_grep( '/^(\w+)FontFamily/i', array_keys( $attributes ), 0 );
73 if ( empty( $keys ) ) {
74 return $googleFontFamily;
75 }
76 foreach ( $keys as $key ) {
77 // Non-scalar / empty values would produce an illegal array key on PHP 8.
78 if ( ! isset( $attributes[$key] ) || ! is_string( $attributes[$key] ) || '' === $attributes[$key] ) {
79 continue;
80 }
81 $googleFontFamily[$attributes[$key]] = $attributes[$key];
82 }
83 return $googleFontFamily;
84 }
85
86 /**
87 * Load fonts.
88 * @since 4.0.0
89 * @access public
90 */
91 public function fonts_loader( $handle_name = 'eb-block-fonts' ) {
92 $googleFont = true;
93 if ( 'essential-blocks' === self::$block_name ) {
94 $eb_settings = get_option( 'eb_settings', [] );
95 $googleFont = ! empty( $eb_settings['googleFont'] ) ? $eb_settings['googleFont'] : 'true';
96 }
97
98 if ( 'false' !== $googleFont ) {
99 $fonts = self::$gfonts;
100
101 if ( ( $key = array_search( 'Default', $fonts ) ) !== false ) {
102 unset( $fonts[$key] );
103 }
104 if ( ! empty( $fonts ) ) {
105 $gfonts = '';
106 $gfonts_attr = ':100,100italic,200,200italic,300,300italic,400,400italic,500,500italic,600,600italic,700,700italic,800,800italic,900,900italic';
107 foreach ( $fonts as $font ) {
108 $gfonts .= str_replace( ' ', '+', trim( $font ) ) . $gfonts_attr . '|';
109 }
110 if ( ! empty( $gfonts ) ) {
111 $query_args = [
112 'family' => $gfonts
113 ];
114 wp_register_style(
115 $handle_name,
116 add_query_arg( $query_args, '//fonts.googleapis.com/css' ),
117 []
118 );
119 wp_enqueue_style( $handle_name );
120 }
121 // Reset.
122 $gfonts = '';
123 }
124 }
125 }
126 }
127 Toggle_Font_Loader::get_instance( 'toggle-content/toggle-content' );