PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / trunk
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder vtrunk
2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 All 78 releases
ablocks / includes / controls / typography.php

typography.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder trunk, at includes/controls/typography.php

183 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ABlocks\Controls;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 use ABlocks\Classes\ControlBaseAbstract;
9
10 class Typography extends ControlBaseAbstract {
11 public static function get_attribute_default_value( $is_responsive = false, $default = [] ) {
12 $base_values = [
13 'fontFamily' => '',
14 'weight' => '',
15 'transform' => '',
16 'style' => '',
17 'decoration' => '',
18 'fontSize' => '',
19 'fontSizeUnit' => 'px',
20 'lineHeight' => '',
21 'lineHeightUnit' => 'px',
22 'letterSpacing' => '',
23 'letterSpacingUnit' => 'px',
24 'wordSpacing' => '',
25 'wordSpacingUnit' => 'px',
26 ];
27
28 if ( $is_responsive ) {
29 $responsive_values = [
30 'fontSizeTablet' => '',
31 'fontSizeMobile' => '',
32 'fontSizeUnitTablet' => 'px',
33 'fontSizeUnitMobile' => 'px',
34 'lineHeightTablet' => '',
35 'lineHeightMobile' => '',
36 'lineHeightUnitTablet' => 'px',
37 'lineHeightUnitMobile' => 'px',
38 'letterSpacingTablet' => '',
39 'letterSpacingMobile' => '',
40 'letterSpacingUnitTablet' => 'px',
41 'letterSpacingUnitMobile' => 'px',
42 'wordSpacingTablet' => '',
43 'wordSpacingMobile' => '',
44 'wordSpacingUnitTablet' => 'px',
45 'wordSpacingUnitMobile' => 'px',
46 ];
47 return array_merge( $base_values, $responsive_values, $default );
48 }//end if
49
50 return array_merge( $base_values, $default );
51 }
52
53
54 public static function get_attribute( $attributeName, $isResponsive = false, $default = [] ) {
55 return [
56 $attributeName . 'Global' => [
57 'type' => 'string',
58 'default' => '',
59 ],
60 $attributeName => [
61 'type' => 'object',
62 'default' => self::get_attribute_default_value( $isResponsive, $default ),
63 ]
64 ];
65 }
66
67 /**
68 * @param mixed $attribute_value Stored typography object.
69 * @param string $property Unused legacy argument.
70 * @param string $device Device suffix.
71 * @param string $attribute_global_name Global typography id, if any.
72 * @param bool $expand_font_stack Whether to turn the stored family into
73 * a full stack. The atomic compiler must
74 * pass false — see below.
75 */
76 public static function get_css( $attribute_value, $property = '', $device = '', $attribute_global_name = '', $expand_font_stack = true ) {
77 $global_value = [];
78 if ( $attribute_global_name ) {
79 $global_typography = wp_list_pluck( \Ablocks\Helper::get_settings( 'global_typography', [] ), 'value', 'id' );
80 $global_value = ( isset( $global_typography[ $attribute_global_name ] ) ? (array) $global_typography[ $attribute_global_name ] : [] );
81 }
82
83 if ( empty( $attribute_value ) && ! count( $global_value ) ) {
84 return [];
85 }
86
87 $default_attr_value = self::get_attribute_default_value( (bool) $device );
88 $value = wp_parse_args( $attribute_value, $default_attr_value );
89 $css = [];
90
91 // Helper: add CSS property with global fallback
92 $add_prop = function( $prop, $local_val, $local_unit, $global_key, $css_var_name ) use ( $attribute_global_name, $global_value, &$css ) {
93 if ( ! empty( $global_value[ $global_key ] ) ) {
94 $css[ $prop ] = "var(--ablocks-{$attribute_global_name}-{$css_var_name})";
95 } elseif ( ! empty( $local_val ) ) {
96 $css[ $prop ] = $local_unit ? $local_val . $local_unit : $local_val;
97 }
98 };
99
100 // Font family. Output the CSS value only — font *discovery* is no longer
101 // done at render time. The set of fonts a page uses is collected from
102 // saved block attributes (FontCollector) and emitted by core via
103 // theme.json (CoreFontRegistry). See docs/FONT-MANAGEMENT-PLAN.md.
104 //
105 // The stored attribute is a bare family name (that name doubles as the
106 // download key), so FontStack turns it into a real stack — quoted, with a
107 // metric-adjusted fallback face and a generic tail derived from the font's
108 // own category. There is no per-block fallback field: `fontFallback` is
109 // read only so a value set through a filter still wins.
110 if ( ! empty( $global_value['fontFamily'] ) ) {
111 $css['font-family'] = "var(--ablocks-{$attribute_global_name}-font-family)";
112 } elseif ( ! empty( $value['fontFamily'] ) ) {
113 // A stored family becomes a full stack everywhere EXCEPT the atomic
114 // compiler, whose declarations are hashed in JS at save time and
115 // again here at render time and must agree byte for byte. A stack
116 // cannot take part in that: it depends on the metrics table, the
117 // `font_metric_fallback` setting and the `ablocks/font_stack`
118 // filter, none of which the editor can see.
119 $family = $expand_font_stack
120 ? \ABlocks\Classes\FontStack::build(
121 $value['fontFamily'],
122 isset( $value['fontFallback'] ) ? $value['fontFallback'] : ''
123 )
124 : $value['fontFamily'];
125 if ( '' !== $family ) {
126 $css['font-family'] = $family;
127 }
128 }
129
130 // Desktop-only styles
131 if ( empty( $device ) ) {
132 $add_prop( 'font-weight', $value['weight'] ?? '', null, 'weight', 'weight' );
133 $add_prop( 'text-transform', $value['transform'] ?? '', null, 'transform', 'transform' );
134 $add_prop( 'font-style', $value['style'] ?? '', null, 'style', 'style' );
135 $add_prop( 'text-decoration', $value['decoration'] ?? '', null, 'decoration', 'decoration' );
136 }
137
138 // Device-specific typography
139 $map = [
140 'fontSize' => 'font-size',
141 'lineHeight' => 'line-height',
142 'letterSpacing' => 'letter-spacing',
143 'wordSpacing' => 'word-spacing',
144 ];
145
146 foreach ( $map as $prop => $css_prop ) {
147 $local_val = $value[ $prop . $device ] ?? '';
148 $local_unit = $value[ $prop . 'Unit' . $device ] ?? '';
149 $global_key = $prop . $device;
150
151 $css_var_name = $css_prop . ( $device ? '-' . strtolower( $device ) : '' );
152
153 $add_prop( $css_prop, $local_val, $local_unit, $global_key, $css_var_name );
154 }
155
156 /*
157 * Columns, direction and text stroke. Appended after the original
158 * members so anything saved before these existed compiles byte-for-byte
159 * as it did — for atomic blocks the style class is a hash of this
160 * output. Desktop-only, like weight/transform/style/decoration above,
161 * and with no global-typography equivalent to fall back to. Mirrors the
162 * tail of typographyPairs() in src/blocks/atomic-shared/styles.js.
163 */
164 if ( empty( $device ) ) {
165 if ( isset( $value['columns'] ) && '' !== $value['columns'] ) {
166 $css['column-count'] = $value['columns'];
167 }
168 if ( ! empty( $value['direction'] ) ) {
169 $css['direction'] = $value['direction'];
170 }
171 if ( isset( $value['strokeWidth'] ) && '' !== $value['strokeWidth'] ) {
172 $stroke_unit = ! empty( $value['strokeWidthUnit'] ) ? $value['strokeWidthUnit'] : 'px';
173 $css['-webkit-text-stroke-width'] = $value['strokeWidth'] . $stroke_unit;
174 }
175 if ( ! empty( $value['strokeColor'] ) ) {
176 $css['-webkit-text-stroke-color'] = $value['strokeColor'];
177 }
178 }
179
180 return $css;
181 }
182 }
183