PluginProbe
GutSlider – All in One Slider and Carousel Blocks for Gutenberg / 2.13.0
GutSlider – All in One Slider and Carousel Blocks for Gutenberg v2.13.0
3.1.0 3.0.0 2.13.2 2.13.1 2.13.0 trunk 1.0.0 2.1.0 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.1 2.5.3 2.5.4 2.5.5 2.6.1 All 56 releases
slider-blocks / includes / Assets / LoadFonts.php

LoadFonts.php in GutSlider – All in One Slider and Carousel Blocks for Gutenberg 2.13.0, at includes/Assets/LoadFonts.php

131 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 declare( strict_types=1 );
3
4 namespace GutSlider\Assets;
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit;
8 }
9
10 /**
11 * Handles loading of Google Fonts used in slider blocks.
12 *
13 * Collects font family attributes from rendered blocks and enqueues
14 * the corresponding Google Fonts stylesheets.
15 *
16 * @package GutSlider\Assets
17 * @since 3.0.0
18 */
19 final class LoadFonts {
20
21 /**
22 * System fonts that do not need to be loaded from Google Fonts.
23 *
24 * @since 3.0.0
25 * @var array<int, string>
26 */
27 private const SYSTEM_FONTS = array(
28 'Arial',
29 'Tahoma',
30 'Verdana',
31 'Helvetica',
32 'Times New Roman',
33 'Trebuchet MS',
34 'Georgia',
35 );
36
37 /**
38 * Collected font families from rendered blocks.
39 *
40 * @since 3.0.0
41 * @var array<int, string>
42 */
43 private static array $all_fonts = array();
44
45 /**
46 * Constructor.
47 *
48 * Registers WordPress hooks for font loading.
49 *
50 * @since 3.0.0
51 */
52 public function __construct() {
53 add_action( 'wp_enqueue_scripts', array( $this, 'fonts_loader' ), 10 );
54 add_action( 'admin_enqueue_scripts', array( $this, 'fonts_loader' ), 10 );
55 add_action( 'gutsliders_render_block', array( $this, 'font_generator' ) );
56 }
57
58 /**
59 * Extract font families from a rendered block's attributes.
60 *
61 * Scans block attributes for font family values and triggers
62 * the font loader to enqueue any new fonts found.
63 *
64 * @since 3.0.0
65 *
66 * @param array<string, mixed> $block The block data array.
67 * @return void
68 */
69 public function font_generator( array $block ): void {
70 if ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) {
71 $attributes = $block['attrs'];
72
73 foreach ( $attributes as $key => $value ) {
74 if ( ! empty( $value )
75 && is_string( $value )
76 && strpos( $key, 'gutsliders_' ) === 0
77 && strpos( $key, 'FontFamily' ) !== false
78 ) {
79 self::$all_fonts[] = sanitize_text_field( $value );
80 }
81 }
82
83 $this->fonts_loader();
84 }
85 }
86
87 /**
88 * Enqueue Google Fonts stylesheets for collected font families.
89 *
90 * Filters out system fonts and duplicates, then registers and
91 * enqueues individual Google Fonts CSS files.
92 *
93 * @since 3.0.0
94 *
95 * @return void
96 */
97 public function fonts_loader(): void {
98 if ( ! is_array( self::$all_fonts ) || count( self::$all_fonts ) === 0 ) {
99 return;
100 }
101
102 $fonts = array_filter( array_unique( self::$all_fonts ) );
103
104 if ( empty( $fonts ) ) {
105 return;
106 }
107
108 $gfonts_attr = ':100,200,300,400,500,600,700,800,900';
109
110 foreach ( $fonts as $font ) {
111 if ( in_array( $font, self::SYSTEM_FONTS, true ) || empty( $font ) ) {
112 continue;
113 }
114
115 $font_family = str_replace( ' ', '+', trim( $font ) ) . $gfonts_attr;
116 $query_args = array( 'family' => $font_family );
117 $font_handle = 'gutsliders-font-' . sanitize_title( $font );
118
119 wp_register_style(
120 $font_handle,
121 add_query_arg( $query_args, '//fonts.googleapis.com/css' ),
122 array(),
123 GUTSLIDER_VERSION,
124 'all'
125 );
126
127 wp_enqueue_style( $font_handle );
128 }
129 }
130 }
131