PluginProbe ʕ •ᴥ•ʔ
Widget Options – Advanced Conditional Visibility for Gutenberg Blocks & Classic Widgets / 4.0.5
Widget Options – Advanced Conditional Visibility for Gutenberg Blocks & Classic Widgets v4.0.5
4.2.5 4.2.4 trunk 3.7.10 3.7.11 3.7.12 3.7.13 3.7.14 3.7.2 3.7.5 3.7.6 3.7.7 3.7.8 3.7.9 3.8 3.8.1 3.8.10 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.8.8 3.8.9 3.8.9.1 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.5.1 4.0.6 4.0.6.1 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.2 4.1.3 4.2.0 4.2.1 4.2.2 4.2.3
widget-options / includes / widgets / gutenberg / lib / experimental / fonts-api / class-wp-fonts-utils.php
widget-options / includes / widgets / gutenberg / lib / experimental / fonts-api Last commit date
bc-layer 2 years ago class-wp-fonts-provider-local.php 2 years ago class-wp-fonts-provider.php 2 years ago class-wp-fonts-utils.php 2 years ago class-wp-fonts.php 2 years ago fonts-api.php 2 years ago register-fonts-from-theme-json.php 2 years ago
class-wp-fonts-utils.php
123 lines
1 <?php
2 /**
3 * Font API's utility helpers.
4 *
5 * @package WordPress
6 * @subpackage Fonts API
7 * @since X.X.X
8 */
9
10 if ( class_exists( 'WP_Fonts_Utils' ) ) {
11 return;
12 }
13
14 /**
15 * Utility helpers for the Fonts API.
16 *
17 * @since X.X.X
18 */
19 class WP_Fonts_Utils {
20
21 /**
22 * Converts the given font family into a handle.
23 *
24 * @since X.X.X
25 *
26 * @param string $font_family Font family to convert into a handle.
27 * @return string|null The font family handle on success. Else, null.
28 */
29 public static function convert_font_family_into_handle( $font_family ) {
30 if ( ! self::is_defined( $font_family ) ) {
31 return null;
32 }
33
34 // If the font-family is a comma-separated list (example: "Inter, sans-serif" ), use just the first font.
35 if ( str_contains( $font_family, ',' ) ) {
36 $font_family = explode( ',', $font_family )[0];
37 }
38
39 return sanitize_title( $font_family );
40 }
41
42 /**
43 * Converts the given variation and its font-family into a handle.
44 *
45 * @since X.X.X
46 *
47 * @param string $font_family The font family's handle for this variation.
48 * @param array $variation An array of variation properties.
49 * @return string|null The variation handle.
50 */
51 public static function convert_variation_into_handle( $font_family, array $variation ) {
52 $handle = '';
53 foreach ( array( 'font-weight', 'font-style' ) as $property ) {
54 if ( ! array_key_exists( $property, $variation ) || ! static::is_defined( $variation[ $property ] ) ) {
55 continue;
56 }
57
58 $handle .= ' ' . $variation[ $property ];
59 }
60
61 if ( '' === $handle ) {
62 trigger_error( 'Variant handle could not be determined as font-weight and/or font-style are require' );
63 return null;
64 }
65
66 return sanitize_title( $font_family . $handle );
67 }
68
69 /**
70 * Gets the font family from the variation.
71 *
72 * @since X.X.X
73 *
74 * @param array $variation An array of variation properties to search.
75 * @return string|null The font family if defined. Else, null.
76 */
77 public static function get_font_family_from_variation( array $variation ) {
78 return static::search_for_font_family( $variation );
79 }
80
81 /**
82 * Checks if the given input is defined, i.e. meaning is a non-empty string.
83 *
84 * @since X.X.X
85 *
86 * @param string $input The input to check.
87 * @return bool True when non-empty string. Else false.
88 */
89 public static function is_defined( $input ) {
90 return ( is_string( $input ) && ! empty( $input ) );
91 }
92
93 /**
94 * Searches the variation array to extract the font family.
95 *
96 * @since X.X.X
97 *
98 * @param array $haystack An array of variation properties to search.
99 * @return string|null The font family when found. Else, null.
100 */
101 private static function search_for_font_family( array $haystack ) {
102 if ( array_key_exists( 'fontFamily', $haystack ) ) {
103 $key = 'fontFamily';
104 } elseif ( array_key_exists( 'font-family', $haystack ) ) {
105 $key = 'font-family';
106 } else {
107 trigger_error( 'Font family not found.' );
108 return null;
109 }
110
111 if ( static::is_defined( $haystack[ $key ] ) ) {
112 // If the font-family is a comma-separated list (example: "Inter, sans-serif" ), use just the first font.
113 if ( strpos( $haystack[ $key ], ',' ) !== false ) {
114 return explode( ',', $haystack[ $key ] )[0];
115 }
116 return $haystack[ $key ];
117 }
118
119 trigger_error( 'Font family not defined in the variation.' );
120 return null;
121 }
122 }
123