PluginProbe
Gutenberg / 17.2.1
Gutenberg v17.2.1
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / lib / experimental / fonts-api / class-wp-fonts-provider.php

class-wp-fonts-provider.php in Gutenberg 17.2.1, at lib/experimental/fonts-api/class-wp-fonts-provider.php

130 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Fonts API: Provider abstract class.
4 *
5 * Individual fonts providers should extend this class and implement.
6 *
7 * @package WordPress
8 * @subpackage Fonts API
9 * @since X.X.X
10 */
11
12 if ( class_exists( 'WP_Fonts_Provider' ) ) {
13 return;
14 }
15
16 /**
17 * Abstract class for Fonts API providers.
18 *
19 * The starting point to building a font service provider.
20 *
21 * What is a Provider?
22 *
23 * A provider contains the know-how (business logic) for how to
24 * process its specific font service (i.e. local or remote)
25 * and how to generate the `@font-face` styles for its service.
26 *
27 * It receives a collection of fonts from the Controller
28 * {@see WP_Fonts_Provider::set_fonts()}, and when requested
29 * {@see WP_Fonts_Provider::get_css()}, it transforms them
30 * into styles (in a performant way for the provider service
31 * it manages).
32 *
33 * @since X.X.X
34 */
35 abstract class WP_Fonts_Provider {
36
37 /**
38 * The provider's unique ID.
39 *
40 * @since X.X.X
41 *
42 * @var string
43 */
44 protected $id = '';
45
46 /**
47 * Fonts to be processed.
48 *
49 * @since X.X.X
50 *
51 * @var array[]
52 */
53 protected $fonts = array();
54
55 /**
56 * Array of Font-face style tag's attribute(s)
57 * where the key is the attribute name and the
58 * value is its value.
59 *
60 * @since X.X.X
61 *
62 * @var string[]
63 */
64 protected $style_tag_atts = array();
65
66 /**
67 * Sets this provider's fonts property.
68 *
69 * @since X.X.X
70 *
71 * @param array[] $fonts Registered fonts.
72 */
73 public function set_fonts( array $fonts ) {
74 $this->fonts = $fonts;
75 }
76
77 /**
78 * Prints the generated styles.
79 *
80 * @since X.X.X
81 */
82 public function print_styles() {
83 printf(
84 $this->get_style_element(),
85 $this->get_css()
86 );
87 }
88
89 /**
90 * Gets the `@font-face` CSS for the provider's fonts.
91 *
92 * This method is where the provider does it processing to build the
93 * needed `@font-face` CSS for all of its fonts. Specifics of how
94 * this processing is done is contained in each provider.
95 *
96 * @since X.X.X
97 *
98 * @return string The `@font-face` CSS.
99 */
100 abstract public function get_css();
101
102 /**
103 * Gets the `<style>` element for wrapping the `@font-face` CSS.
104 *
105 * @since X.X.X
106 *
107 * @return string The style element.
108 */
109 protected function get_style_element() {
110 $attributes = $this->generate_style_element_attributes();
111
112 return "<style id='wp-fonts-{$this->id}'{$attributes}>\n%s\n</style>\n";
113 }
114
115 /**
116 * Gets the defined <style> element's attributes.
117 *
118 * @since X.X.X
119 *
120 * @return string A string of attribute=value when defined, else, empty string.
121 */
122 private function generate_style_element_attributes() {
123 $attributes = '';
124 foreach ( $this->style_tag_atts as $name => $value ) {
125 $attributes .= " {$name}='{$value}'";
126 }
127 return $attributes;
128 }
129 }
130