PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 3.6.1
Block Animations, Motion & Scroll Effects – Ghost Kit v3.6.1
3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / classes / class-typography.php

class-typography.php in Block Animations, Motion & Scroll Effects – Ghost Kit 3.6.1, at classes/class-typography.php

409 lines 13.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Typography
4 *
5 * @package ghostkit
6 */
7
8 /**
9 * GhostKit_Typography
10 */
11 class GhostKit_Typography {
12 /**
13 * GhostKit_Typography constructor.
14 */
15 public function __construct() {
16 add_filter( 'gkt_custom_typography', array( $this, 'add_default_typography' ), 9 );
17 add_action( 'enqueue_block_assets', array( $this, 'enqueue_typography_assets' ), 100 );
18 }
19
20 /**
21 * Enqueue Typography assets to editor and front end.
22 */
23 public function enqueue_typography_assets() {
24 $typography_css = $this->generate_typography_styles();
25 $css = ' ';
26
27 if ( isset( $typography_css ) && ! empty( $typography_css ) && is_array( $typography_css ) ) {
28 if ( ! is_admin() && isset( $typography_css['front'] ) && ! empty( $typography_css['front'] ) ) {
29 $css = $typography_css['front'];
30 }
31 if ( is_admin() && isset( $typography_css['editor'] ) && ! empty( $typography_css['editor'] ) ) {
32 $css = $typography_css['editor'];
33 }
34 }
35 wp_register_style( 'ghostkit-typography', false, array(), GHOSTKIT_VERSION );
36 wp_enqueue_style( 'ghostkit-typography' );
37 wp_add_inline_style( 'ghostkit-typography', $css );
38 }
39
40 /**
41 * Generate Typography Styles.
42 *
43 * @return array - Typography Css.
44 */
45 public static function generate_typography_styles() {
46 $typography_prepeare_styles = array();
47 $typography_css = array(
48 'editor' => '',
49 'front' => '',
50 );
51 $default_typography = apply_filters( 'gkt_custom_typography', array() );
52 $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false;
53 $global_typography = get_option( 'ghostkit_typography', array() );
54 $is_admin_editor = is_admin() && $screen && $screen->is_block_editor;
55
56 if ( is_singular() ) {
57 $post_id = get_the_ID();
58 } elseif ( $is_admin_editor ) {
59 global $post;
60 $post_id = isset( $post->ID ) ? $post->ID : null;
61 }
62
63 $is_single = is_singular() && $post_id;
64 $is_admin_editor = $is_admin_editor && $post_id;
65
66 if ( self::is_exist( $default_typography ) ) {
67
68 foreach ( $default_typography as $key => $typography ) {
69 if ( self::is_exist( $typography['output'] ) ) {
70 $typography_prepeare_styles[ $key ] = array(
71 'style-properties' => $typography['defaults'],
72 'output' => $typography['output'],
73 );
74 }
75 }
76
77 // Global custom Typography.
78 if ( self::is_exist( $global_typography ) && self::is_exist( $global_typography['ghostkit_typography'] ) ) {
79 $typography_prepeare_styles = self::get_typography_values( $global_typography['ghostkit_typography'], $typography_prepeare_styles );
80 }
81
82 // Local custom Typography.
83 if ( $is_single || $is_admin_editor ) {
84 $meta_typography = get_post_meta( $post_id, 'ghostkit_typography', true );
85 $typography_prepeare_styles = self::get_typography_values( $meta_typography, $typography_prepeare_styles );
86 }
87
88 // Collect all the styles for further transfer to the inline file on the edit or front page.
89 foreach ( $typography_prepeare_styles as $typography_prepeare_style ) {
90 if (
91 self::is_exist( $typography_prepeare_style['output'] ) &&
92 is_array( $typography_prepeare_style['output'] ) &&
93 ( self::is_exist( $typography_prepeare_style['style-properties'], 'font-family' ) ||
94 self::is_exist( $typography_prepeare_style['style-properties'], 'font-size' ) ||
95 self::is_exist( $typography_prepeare_style['style-properties'], 'font-weight' ) ||
96 self::is_exist( $typography_prepeare_style['style-properties'], 'line-height' ) ||
97 self::is_exist( $typography_prepeare_style['style-properties'], 'letter-spacing' )
98 )
99 ) {
100 foreach ( $typography_prepeare_style['output'] as $output ) {
101 if ( self::is_exist( $output['selectors'] ) ) {
102 $typography_styles = '';
103 $typography_styles .= $output['selectors'] . '{';
104
105 if ( self::is_exist( $typography_prepeare_style['style-properties'], 'font-family' ) ) {
106 // Add double quotes to the font name, because some fonts have a space in the name.
107 // And if the font name is not in quotes, then the browser will not be able to find it.
108 // For example font-family: Source Serif 4; - will not work.
109 $typography_styles .= 'font-family: "' . esc_attr( $typography_prepeare_style['style-properties']['font-family'] ) . '", sans-serif;';
110 }
111
112 if ( self::is_exist( $typography_prepeare_style['style-properties'], 'font-size' ) ) {
113 $typography_styles .= 'font-size: ' . esc_attr( $typography_prepeare_style['style-properties']['font-size'] ) . ';';
114 }
115
116 if ( self::is_exist( $typography_prepeare_style['style-properties'], 'font-weight' ) ) {
117 $font_weight = $typography_prepeare_style['style-properties']['font-weight'];
118
119 if ( false !== strpos( $font_weight, 'i' ) ) {
120 $font_weight = str_replace( 'i', '', $font_weight );
121 $typography_styles .= 'font-style: italic;';
122 } else {
123 $typography_styles .= 'font-style: normal;';
124 }
125
126 $typography_styles .= 'font-weight: ' . esc_attr( $font_weight ) . ';';
127 }
128
129 if ( self::is_exist( $typography_prepeare_style['style-properties'], 'line-height' ) ) {
130 $typography_styles .= 'line-height: ' . esc_attr( $typography_prepeare_style['style-properties']['line-height'] ) . ';';
131 }
132
133 if ( self::is_exist( $typography_prepeare_style['style-properties'], 'letter-spacing' ) ) {
134 $typography_styles .= 'letter-spacing: ' . esc_attr( $typography_prepeare_style['style-properties']['letter-spacing'] ) . ';';
135 }
136
137 $typography_styles .= '}';
138
139 if ( isset( $output['editor'] ) && true === $output['editor'] ) {
140 $typography_css['editor'] .= $typography_styles;
141 } else {
142 $typography_css['front'] .= $typography_styles;
143 }
144 }
145 }
146 }
147 }
148 }
149
150 return $typography_css;
151 }
152
153 /**
154 * Check value on the existence and emptiness.
155 *
156 * @param void $value - Checking value.
157 * @param bool $attribute - Checking attribute of Array Value.
158 * @param string $mode - Full or isset for partial check.
159 * @return bool $value - True or false.
160 */
161 public static function is_exist( $value, $attribute = false, $mode = 'full' ) {
162 $check = false;
163
164 if ( $attribute ) {
165 if ( 'full' === $mode && isset( $value[ $attribute ] ) && ! empty( $value[ $attribute ] ) && 'null' !== $value ) {
166 $check = true;
167 }
168 if ( 'isset' === $mode && isset( $value[ $attribute ] ) ) {
169 $check = true;
170 }
171 } else {
172 if ( 'full' === $mode ) {
173 $check = ( isset( $value ) && ! empty( $value ) && 'null' !== $value ) ? true : false;
174 }
175 if ( 'isset' === $mode ) {
176 $check = ( isset( $value ) ) ? true : false;
177 }
178 }
179
180 return $check;
181 }
182
183 /**
184 * Function for get Typography Values.
185 *
186 * @param object $typography_object - Current typography.
187 * @param array $typography_prepeare_styles - Previous Array With Current Styles Properties.
188 * @return mixed - Next Array With Current Styles Properties.
189 */
190 public static function get_typography_values( $typography_object, $typography_prepeare_styles ) {
191 $conformity_attributes = array(
192 'fontFamily' => 'font-family',
193 'fontFamilyCategory' => 'font-family-category',
194 'fontSize' => 'font-size',
195 'fontWeight' => 'font-weight',
196 'lineHeight' => 'line-height',
197 'letterSpacing' => 'letter-spacing',
198 'label' => 'label',
199 'childOf' => 'child-of',
200 );
201
202 if ( self::is_exist( $typography_object ) ) {
203 foreach ( json_decode( $typography_object ) as $meta_typography_key => $meta_typography_value ) {
204 if ( array_key_exists( $meta_typography_key, $typography_prepeare_styles ) ) {
205 foreach ( $meta_typography_value as $typography_attribute_key => $typography_attribute ) {
206 if ( self::is_exist( $conformity_attributes[ $typography_attribute_key ] ) &&
207 self::is_exist( $typography_prepeare_styles[ $meta_typography_key ]['style-properties'], $conformity_attributes[ $typography_attribute_key ], 'isset' ) ) {
208 if ( '' !== $typography_attribute ) {
209 $typography_prepeare_styles[ $meta_typography_key ]['style-properties'][ $conformity_attributes[ $typography_attribute_key ] ] = $typography_attribute;
210 }
211 }
212 }
213 }
214 }
215 }
216
217 return $typography_prepeare_styles;
218 }
219
220 /**
221 * Add Default Typography.
222 *
223 * @param array $custom_typography - Current typography.
224 * @return array - New Typography.
225 */
226 public function add_default_typography( $custom_typography ) {
227 $custom_typography = array(
228 'body' => array(
229 'label' => esc_html__( 'Body', 'ghostkit' ),
230 'defaults' => array(
231 'font-family-category' => 'default',
232 'font-family' => '',
233 'font-size' => '',
234 'font-weight' => '',
235 'line-height' => '',
236 'letter-spacing' => '',
237 ),
238 'output' => array(
239 array(
240 'selectors' => 'body',
241 ),
242 array(
243 'selectors' => '.editor-styles-wrapper, .editor-styles-wrapper p',
244 'editor' => true,
245 ),
246 ),
247 ),
248 'buttons' => array(
249 'label' => esc_html__( 'Buttons', 'ghostkit' ),
250 'defaults' => array(
251 'font-family-category' => 'default',
252 'font-family' => '',
253 'font-size' => '',
254 'font-weight' => '',
255 'line-height' => '',
256 'letter-spacing' => '',
257 ),
258 'output' => array(
259 array(
260 'selectors' => '.wp-block-button, .ghostkit-button, .entry .entry-content .wp-block-button .wp-block-button__link',
261 ),
262 array(
263 'selectors' => '.editor-styles-wrapper .wp-block-button .wp-block-button__link, .editor-styles-wrapper .ghostkit-button',
264 'editor' => true,
265 ),
266 ),
267 ),
268 'headings' => array(
269 'label' => esc_html__( 'Headings', 'ghostkit' ),
270 'defaults' => array(
271 'font-family-category' => 'default',
272 'font-family' => '',
273 'font-weight' => '',
274 'line-height' => '',
275 'letter-spacing' => '',
276 ),
277 'output' => array(
278 array(
279 'selectors' => 'h1, h1.entry-title, h2, h3, h4, h5, h6, .wp-block-heading, .wp-block-post-title, h2.wp-block-heading, h3.wp-block-heading, h4.wp-block-heading, h5.wp-block-heading, h6.wp-block-heading, .wp-block-heading',
280 ),
281 array(
282 'selectors' => '.editor-styles-wrapper h1, .editor-styles-wrapper h2, .editor-styles-wrapper h3, .editor-styles-wrapper h4, .editor-styles-wrapper h5, .editor-styles-wrapper h6, .editor-styles-wrapper .editor-post-title__block .editor-post-title__input',
283 'editor' => true,
284 ),
285 ),
286 ),
287 'h1' => array(
288 'label' => esc_html__( 'H1', 'ghostkit' ),
289 'defaults' => array(
290 'font-size' => '',
291 'line-height' => '',
292 'letter-spacing' => '',
293 ),
294 'child-of' => 'headings',
295 'output' => array(
296 array(
297 'selectors' => 'h1, h1.entry-title, h1.wp-block-heading, h1.wp-block-post-title',
298 ),
299 array(
300 'selectors' => '.editor-styles-wrapper h1, .editor-styles-wrapper .editor-post-title__block .editor-post-title__input',
301 'editor' => true,
302 ),
303 ),
304 ),
305 'h2' => array(
306 'label' => esc_html__( 'H2', 'ghostkit' ),
307 'defaults' => array(
308 'font-size' => '',
309 'line-height' => '',
310 'letter-spacing' => '',
311 ),
312 'child-of' => 'headings',
313 'output' => array(
314 array(
315 'selectors' => 'h2, h2.wp-block-heading',
316 ),
317 array(
318 'selectors' => '.editor-styles-wrapper h2',
319 'editor' => true,
320 ),
321 ),
322 ),
323 'h3' => array(
324 'label' => esc_html__( 'H3', 'ghostkit' ),
325 'defaults' => array(
326 'font-size' => '',
327 'line-height' => '',
328 'letter-spacing' => '',
329 ),
330 'child-of' => 'headings',
331 'output' => array(
332 array(
333 'selectors' => 'h3, h3.wp-block-heading',
334 ),
335 array(
336 'selectors' => '.editor-styles-wrapper h3',
337 'editor' => true,
338 ),
339 ),
340 ),
341 'h4' => array(
342 'label' => esc_html__( 'H4', 'ghostkit' ),
343 'defaults' => array(
344 'font-size' => '',
345 'line-height' => '',
346 'letter-spacing' => '',
347 ),
348 'child-of' => 'headings',
349 'output' => array(
350 array(
351 'selectors' => 'h4, h4.wp-block-heading',
352 ),
353 array(
354 'selectors' => '.editor-styles-wrapper h4',
355 'editor' => true,
356 ),
357 ),
358 ),
359 'h5' => array(
360 'label' => esc_html__( 'H5', 'ghostkit' ),
361 'defaults' => array(
362 'font-size' => '',
363 'line-height' => '',
364 'letter-spacing' => '',
365 ),
366 'child-of' => 'headings',
367 'output' => array(
368 array(
369 'selectors' => 'h5, h5.wp-block-heading',
370 ),
371 array(
372 'selectors' => '.editor-styles-wrapper h5',
373 'editor' => true,
374 ),
375 ),
376 ),
377 'h6' => array(
378 'label' => esc_html__( 'H6', 'ghostkit' ),
379 'defaults' => array(
380 'font-size' => '',
381 'line-height' => '',
382 'letter-spacing' => '',
383 ),
384 'child-of' => 'headings',
385 'output' => array(
386 array(
387 'selectors' => 'h6, h6.wp-block-heading',
388 ),
389 array(
390 'selectors' => '.editor-styles-wrapper h6',
391 'editor' => true,
392 ),
393 ),
394 ),
395 );
396 return $custom_typography;
397 }
398
399 /**
400 * Fallback for removed method, which was used in the Pro version priori to v2.2.0.
401 *
402 * @return bool
403 */
404 public static function typography_exist() {
405 return false;
406 }
407 }
408 new GhostKit_Typography();
409