PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 2.25.0
Block Animations, Motion & Scroll Effects – Ghost Kit v2.25.0
3.7.2 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 2.25.0, at classes/class-typography.php

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