PluginProbe
Progress Bars / trunk
Progress Bars vtrunk
trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0
progress-bars / includes / font-loader.php

font-loader.php in Progress Bars trunk, at includes/font-loader.php

117 lines 3.6 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 Progressbar_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 ( null === self::$instances ) {
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'] ) ) {
52 if ( 'essential-blocks' === self::$block_name || $block['blockName'] === self::$block_name ) {
53 $fonts = self::get_fonts_family( $block['attrs'] );
54 self::$gfonts = array_unique( array_merge( self::$gfonts, $fonts ) );
55 }
56 }
57
58 return $block_content;
59 }
60
61 /**
62 * Generate Font family from Attributes
63 * @since 4.0.0
64 * @access public
65 */
66 public static function get_fonts_family( $attributes ) {
67 $keys = preg_grep( '/^(\w+)FontFamily/i', array_keys( $attributes ), 0 );
68 $googleFontFamily = [];
69 foreach ( $keys as $key ) {
70 $googleFontFamily[$attributes[$key]] = $attributes[$key];
71 }
72 return $googleFontFamily;
73 }
74
75 /**
76 * Load fonts.
77 * @since 4.0.0
78 * @access public
79 */
80 public function fonts_loader( $handle_name = 'eb-block-fonts' ) {
81 $googleFont = true;
82 if ( 'essential-blocks' === self::$block_name ) {
83 $eb_settings = get_option( 'eb_settings', [] );
84 $googleFont = ! empty( $eb_settings['googleFont'] ) ? $eb_settings['googleFont'] : 'true';
85 }
86
87 if ( 'false' !== $googleFont ) {
88 $fonts = self::$gfonts;
89
90 if ( ( $key = array_search( 'Default', $fonts ) ) !== false ) {
91 unset( $fonts[$key] );
92 }
93 if ( ! empty( $fonts ) ) {
94 $gfonts = '';
95 $gfonts_attr = ':100,100italic,200,200italic,300,300italic,400,400italic,500,500italic,600,600italic,700,700italic,800,800italic,900,900italic';
96 foreach ( $fonts as $font ) {
97 $gfonts .= str_replace( ' ', '+', trim( $font ) ) . $gfonts_attr . '|';
98 }
99 if ( ! empty( $gfonts ) ) {
100 $query_args = [
101 'family' => $gfonts
102 ];
103 wp_register_style(
104 $handle_name,
105 add_query_arg( $query_args, '//fonts.googleapis.com/css' ),
106 []
107 );
108 wp_enqueue_style( $handle_name );
109 }
110 // Reset.
111 $gfonts = '';
112 }
113 }
114 }
115 }
116 Progressbar_Font_Loader::get_instance( 'progress-bars/progress-bar-block' );
117