PluginProbe
Getwid – Gutenberg Blocks / 2.0.8
Getwid – Gutenberg Blocks v2.0.8
3.0.1 3.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.2.0 trunk 1.8.7 1.9.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
getwid / includes / functions.php

functions.php in Getwid – Gutenberg Blocks 2.0.8, at includes/functions.php

535 lines 18.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Gets plugin's absolute directory path.
5 *
6 * @param string $path Relative path
7 *
8 * @return string
9 */
10 function getwid_get_plugin_path( $path = '' ) {
11 return GETWID_PLUGIN_DIR . trim( $path, '/' );
12 }
13
14
15 /**
16 * Gets plugin's URL.
17 *
18 * @param string $path
19 *
20 * @return string
21 */
22 function getwid_get_plugin_url( $path = '' ) {
23 return plugins_url( $path, GETWID_PLUGIN_FILE );
24 }
25
26
27 /**
28 * Get template part.
29 *
30 * @param string $slug
31 * @param string $name Optional. Default ''.
32 */
33 function getwid_get_template_part( $slug, $attributes = array(), $extract = false, $extra_attr = array() ){
34
35 $template = getwid_locate_template( $slug );
36
37 // Allow 3rd party plugins to filter template file from their plugin.
38 $template = apply_filters( 'getwid/core/get_template_part', $template, $slug, $attributes );
39
40 if ( !empty( $template ) ) {
41 if ( $attributes && is_array( $attributes ) && $extract ) {
42 extract( $attributes, EXTR_SKIP );
43 }
44
45 require $template;
46 }
47
48 return $template;
49 }
50
51 /**
52 * Retrieve the name of the highest priority template file that exists.
53 *
54 * @param string $slug
55 * @param string $name Optional. Default ''.
56 */
57 function getwid_locate_template( $slug ){
58
59 $template = '';
60
61 // Look in %theme_dir%/%template_path%/slug.php
62 $template = locate_template( "getwid/{$slug}.php" );
63
64 // Get default template from plugin
65 if ( empty( $template ) && file_exists( getwid_get_plugin_path( "/includes/templates/{$slug}.php" ) ) ) {
66 $template = getwid_get_plugin_path( "/includes/templates/{$slug}.php" );
67 }
68
69 return $template;
70 }
71
72 /*
73 * Add the option if it doesn't exist
74 *
75 * @param string $option_name
76 * @param mixed $option_value
77 * @param bool $autoload
78 *
79 * @return bool True if the option was added, false otherwise.
80 */
81 function getwid_maybe_add_option( $option_name, $option_value, $autoload ) {
82
83 $result = false;
84
85 if ( get_option( $option_name ) === false ) {
86
87 /*
88 * If the option doesn't already exist in DB it was added to `notoptions` array on `get_option` call.
89 * If so, we can check this and call `add_option` to create an option with `autoload` property.
90 */
91 $notoptions = wp_cache_get( 'notoptions', 'options' );
92 if ( is_array( $notoptions ) && isset( $notoptions[ $option_name ] ) ) {
93 $result = add_option( $option_name, $option_value, '', $autoload );
94 }
95 }
96
97 return $result;
98 }
99
100
101 /**
102 * Generate section content width css
103 *
104 * @return string
105 */
106 function getwid_generate_section_content_width_css(){
107
108 global $content_width;
109
110 // Existent empty option value "" = non-existent option value
111 $sectionContentWidth = get_option( 'getwid_section_content_width', '' );
112
113 getwid_maybe_add_option( 'getwid_section_content_width', '', true );
114
115 // We need to know exactly when the value "does not exist" and when to set the global value
116 $sectionContentWidth = is_numeric($sectionContentWidth) ? floatval( $sectionContentWidth ) : $content_width;
117
118 $section_css = '';
119 if ( $sectionContentWidth ) {
120 $section_css .= '.wp-block-getwid-section .wp-block-getwid-section__wrapper .wp-block-getwid-section__inner-wrapper{max-width: '
121 . $sectionContentWidth . 'px;}';
122 }
123
124 return $section_css;
125 }
126
127 /**
128 * Generate smooth animations css
129 *
130 * @return string
131 */
132 function getwid_generate_smooth_animation_css(){
133
134 $smoothAnimationsEnabled = get_option( 'getwid_smooth_animation', false );
135
136 getwid_maybe_add_option( 'getwid_smooth_animation', false, true );
137
138 $animation_css = '';
139 if ( $smoothAnimationsEnabled ) {
140 $animation_css .= 'body{overflow-x:hidden;}';
141 $animation_css .= '.getwid-anim{visibility:hidden;}';
142 }
143
144 return $animation_css;
145 }
146
147 /**
148 * Generate text color/background color style & class
149 *
150 * @return string
151 */
152 function getwid_custom_color_style_and_class(&$style, &$class, $attributes, $process = 'color', $is_back_end = false, $custom_color = false){
153
154 if ($custom_color == false){
155 if ($process == 'color'){
156 //Color
157 $Color = isset($attributes['textColor']) ? $attributes['textColor'] : null;
158 $customColor = isset($attributes['customTextColor']) ? $attributes['customTextColor'] : null;
159 } elseif ($process == 'background'){
160 //Background
161 $Color = isset($attributes['backgroundColor']) ? $attributes['backgroundColor'] : null;
162 $customColor = isset($attributes['customBackgroundColor']) ? $attributes['customBackgroundColor'] : null;
163 }
164 } elseif (is_array($custom_color)){
165 $Color = isset($custom_color['color']) && isset($attributes[$custom_color['color']]) ? $attributes[$custom_color['color']] : null;
166 $customColor = isset($custom_color['custom']) && isset($attributes[$custom_color['custom']]) ? $attributes[$custom_color['custom']] : null;
167 }
168
169 if (isset( $Color) || isset( $customColor )){
170 if (isset( $Color)){
171 preg_match('/^#/', $Color, $matches);
172 //HEX
173 $ColorHEX = '';
174 if (isset($matches[0])){
175 $ColorHEX = $Color;
176 }
177 //String
178 else {
179 $editorColorPalette = get_theme_support('editor-color-palette');
180 if ( $editorColorPalette && isset($editorColorPalette[0]) ) {
181 $get_colors = $editorColorPalette[0];
182 if ( !empty($get_colors) ) {
183 foreach ($get_colors as $key => $value) {
184 if ($value['slug'] == $Color){
185 $ColorHEX = $value['color'];
186 }
187 }
188 }
189 }
190 }
191 }
192
193 $class .= ($process == 'color') ? ' has-text-color' : ' has-background';
194
195 if ($is_back_end){
196 $style .= (($process == 'color') ? 'color:' : 'background-color:').(isset( $customColor ) ? $customColor : $ColorHEX).';';
197 } else {
198 if (isset($customColor)){
199 $style .= (($process == 'color') ? 'color:' : 'background-color:'). esc_attr($customColor) .';';
200 } else {
201 $class .= ' has-'. esc_attr($Color) .(($process == 'color') ? '-color' : '-background-color');
202 }
203 }
204 }
205
206 $style = trim($style);
207 $class = trim($class);
208 }
209
210 /**
211 * Generate custom paddings style & class
212 *
213 * @return string
214 */
215 function getwid_custom_paddings_style_and_class(&$style, &$class, $attributes){
216
217 $class .= (isset($attributes['paddingTop']) && $attributes['paddingTop'] !='' && $attributes['paddingTop'] != 'custom') ? " getwid-padding-top-".esc_attr($attributes['paddingTop']) : '';
218 $class .= (isset($attributes['paddingBottom']) && $attributes['paddingBottom'] !='' && $attributes['paddingBottom'] != 'custom') ? " getwid-padding-bottom-".esc_attr($attributes['paddingBottom']) : '';
219 $class .= (isset($attributes['paddingLeft']) && $attributes['paddingLeft'] !='' && $attributes['paddingLeft'] != 'custom') ? " getwid-padding-left-".esc_attr($attributes['paddingLeft']) : '';
220 $class .= (isset($attributes['paddingRight']) && $attributes['paddingRight'] !='' && $attributes['paddingRight'] != 'custom') ? " getwid-padding-right-".esc_attr($attributes['paddingRight']) : '';
221
222 $class .= (isset($attributes['paddingTopTablet']) && $attributes['paddingTopTablet'] !='') ? " getwid-padding-tablet-top-".esc_attr($attributes['paddingTopTablet']) : '';
223 $class .= (isset($attributes['paddingBottomTablet']) && $attributes['paddingBottomTablet'] !='') ? " getwid-padding-tablet-bottom-".esc_attr($attributes['paddingBottomTablet']) : '';
224 $class .= (isset($attributes['paddingLeftTablet']) && $attributes['paddingLeftTablet'] !='') ? " getwid-padding-tablet-left-".esc_attr($attributes['paddingLeftTablet']) : '';
225 $class .= (isset($attributes['paddingRightTablet']) && $attributes['paddingRightTablet'] !='') ? " getwid-padding-tablet-right-".esc_attr($attributes['paddingRightTablet']) : '';
226
227 $class .= (isset($attributes['paddingTopMobile']) && $attributes['paddingTopMobile'] !='') ? " getwid-padding-mobile-top-".esc_attr($attributes['paddingTopMobile']) : '';
228 $class .= (isset($attributes['paddingBottomMobile']) && $attributes['paddingBottomMobile'] !='') ? " getwid-padding-mobile-bottom-".esc_attr($attributes['paddingBottomMobile']) : '';
229 $class .= (isset($attributes['paddingLeftMobile']) && $attributes['paddingLeftMobile'] !='') ? " getwid-padding-mobile-left-".esc_attr($attributes['paddingLeftMobile']) : '';
230 $class .= (isset($attributes['paddingRightMobile']) && $attributes['paddingRightMobile'] !='') ? " getwid-padding-mobile-right-".esc_attr($attributes['paddingRightMobile']) : '';
231
232 $style .= (isset($attributes['paddingTop']) && $attributes['paddingTop'] !='' && $attributes['paddingTop'] == 'custom') ? "padding-top:".esc_attr($attributes['paddingTopValue']).";" : '';
233 $style .= (isset($attributes['paddingBottom']) && $attributes['paddingBottom'] !='' && $attributes['paddingBottom'] == 'custom') ? "padding-bottom:".esc_attr($attributes['paddingBottomValue']).";" : '';
234 $style .= (isset($attributes['paddingLeft']) && $attributes['paddingLeft'] !='' && $attributes['paddingLeft'] == 'custom') ? "padding-left:".esc_attr($attributes['paddingLeftValue']).";" : '';
235 $style .= (isset($attributes['paddingRight']) && $attributes['paddingRight'] !='' && $attributes['paddingRight'] == 'custom') ? "padding-right:".esc_attr($attributes['paddingRightValue']).";" : '';
236 }
237
238 /**
239 * Generate custom alignment classes
240 *
241 * @return string
242 */
243 function getwid_custom_alignment_classes(&$class, $attributes){
244 $class .= (isset($attributes['verticalAlign']) && $attributes['verticalAlign'] !='center') ? " getwid-align-items-".esc_attr($attributes['verticalAlign']) : '';
245 $class .= (isset($attributes['verticalAlignTablet']) && $attributes['verticalAlignTablet'] !='') ? " getwid-align-items-tablet".esc_attr($attributes['verticalAlignTablet']) : '';
246 $class .= (isset($attributes['verticalAlignMobile']) && $attributes['verticalAlignMobile'] !='') ? " getwid-align-items-mobile-".esc_attr($attributes['verticalAlignMobile']) : '';
247
248 $class .= (isset($attributes['horizontalAlign']) && $attributes['horizontalAlign'] !='center') ? " getwid-justify-content-".esc_attr($attributes['horizontalAlign']) : '';
249 $class .= (isset($attributes['horizontalAlignTablet']) && $attributes['horizontalAlignTablet'] !='') ? " getwid-justify-content-tablet-".esc_attr($attributes['horizontalAlignTablet']) : '';
250 $class .= (isset($attributes['horizontalAlignMobile']) && $attributes['horizontalAlignMobile'] !='') ? " getwid-justify-content-mobile-".esc_attr($attributes['horizontalAlignMobile']) : '';
251 }
252
253 /**
254 * Generate custom gradient styles
255 *
256 * @return string
257 */
258 function getwid_custom_gradient_styles($prefix, &$style, $attributes){
259 $type = isset($attributes[$prefix.'GradientType']) ? esc_attr($attributes[$prefix.'GradientType']) : 'linear';
260 $angle = isset($attributes[$prefix.'GradientAngle']) ? esc_attr($attributes[$prefix.'GradientAngle']).'deg' : '180deg';
261 $firstColor = isset($attributes[$prefix.'GradientFirstColor']) ? esc_attr($attributes[$prefix.'GradientFirstColor']) : 'rgba(0,0,0,0)';
262 $secondColor = isset($attributes[$prefix.'GradientSecondColor']) ? esc_attr($attributes[$prefix.'GradientSecondColor']) : 'rgba(0,0,0,0)';
263 $firstLocation = isset($attributes[$prefix.'GradientFirstColorLocation']) ? esc_attr($attributes[$prefix.'GradientFirstColorLocation']).'%' : '0%';
264 $secondLocation = isset($attributes[$prefix.'GradientSecondColorLocation']) ? esc_attr($attributes[$prefix.'GradientSecondColorLocation']).'%' : '100%';
265
266 if ($type == 'linear'){
267 $style .= 'background-image: linear-gradient('.esc_attr($angle).', '.esc_attr($firstColor).' '.esc_attr($firstLocation).', '.esc_attr($secondColor).' '.esc_attr($secondLocation).');';
268 } elseif ($type == 'radial'){
269 $style .= 'background-image: radial-gradient('.esc_attr($firstColor).' '.esc_attr($firstLocation).', '.esc_attr($secondColor).' '.esc_attr($secondLocation).');';
270 }
271 }
272
273 /**
274 * Build WP Query
275 *
276 * @return array
277 */
278 function getwid_build_custom_post_type_query( $attributes ) {
279
280 $query_args = array();
281
282 if ( (isset($attributes['filterById']) && $attributes['filterById'] != '') ||
283 (isset($attributes['parentPageId']) && $attributes['parentPageId'] != '' ) ||
284 isset($attributes['postType']) ) {
285
286 $query_args = array(
287 'posts_per_page' => $attributes['postsToShow'],
288 'ignore_sticky_posts' => 1,
289 'post_status' => 'publish',
290 'order' => $attributes['order'],
291 'orderby' => $attributes['orderBy'],
292 );
293
294 if ( isset($attributes['ignoreSticky']) ){
295 $query_args['ignore_sticky_posts'] = $attributes['ignoreSticky'];
296 }
297
298
299 $paged = is_front_page() ? get_query_var( 'page', 1 ) : get_query_var( 'paged', 1 );
300
301 if ( isset($attributes['pagination']) && $attributes['pagination'] ){
302 $query_args['paged'] = $paged;
303 }
304
305 if ($attributes['offset'] != 0){
306 $offset = ( $paged - 1 ) * $attributes['postsToShow'] + $attributes['offset'];
307 $query_args['offset'] = $offset;
308 }
309 }
310
311 //Exclude by IDs && Current Post ID
312 if ( (isset($attributes['excludeById']) && $attributes['excludeById'] != '') || $attributes['excludeCurrentPost'] ) {
313
314 $ids_arr = [];
315 if ((isset($attributes['excludeById']) && $attributes['excludeById'] != '')){
316 $ids_arr = array_map( 'intval', explode(',', $attributes['excludeById']) );
317 }
318
319 if ($attributes['excludeCurrentPost']){
320 $ids_arr[] = get_the_ID();
321 }
322
323 $query_args['post__not_in'] = $ids_arr;
324 }
325
326 //Filter by IDs
327 if ( isset($attributes['filterById']) && $attributes['filterById'] != '' ) {
328
329 $ids_arr = array_map( 'intval', explode(',', $attributes['filterById']) );
330 $query_args['post__in'] = $ids_arr;
331
332 } else if ( (isset($attributes['parentPageId']) && $attributes['parentPageId'] !='') || $attributes['childPagesCurrentPage'] ) {
333
334 $query_args['post_type'] = 'page';
335 if ($attributes['postType'] == 'page'){
336 $query_args['post_parent'] = $attributes['childPagesCurrentPage'] ? get_the_ID() : intval($attributes['parentPageId']);
337 }
338
339 }
340
341 //Set postType
342 if ( isset( $attributes['postType'] )) {
343
344 $query_args['post_type'] = $attributes['postType'];
345
346 if ( isset($attributes['taxonomy']) && isset($attributes['terms']) ){
347
348 $query_args['tax_query'] = array(
349 'relation' => $attributes['relation'],
350 );
351
352 $taxonomy_arr = [];
353 //Get terms from taxonomy (Make arr)
354 foreach ($attributes['terms'] as $key => $value) {
355 preg_match('/(^.*)\[(\d*)\]/', $value, $find_arr);
356
357 if (isset($find_arr[1]) && isset($find_arr[2])){
358 $taxonomy = $find_arr[1];
359 $term = $find_arr[2];
360
361 $taxonomy_arr[$taxonomy][] = $term;
362 }
363 }
364
365 //Add array to query
366 if (!empty($taxonomy_arr)){
367 foreach ($taxonomy_arr as $taxonomy_name => $terms_arr) {
368
369 foreach ($terms_arr as $term_index => $term_id) {
370
371 $query_args['tax_query'][] = array(
372 'taxonomy' => $taxonomy_name,
373 'field' => 'term_id',
374 'terms' => $term_id
375 );
376 }
377 }
378 }
379 }
380 }
381
382 if ( ! empty( $attributes[ 'metaQuery' ] ) ) {
383
384 $query_args[ 'meta_query' ] = getwid_build_meta_query( $attributes[ 'metaQuery' ] );
385 }
386
387 return $query_args;
388 }
389
390 /**
391 * @param array $metaQuery
392 * @return array
393 *
394 * https://developer.wordpress.org/reference/classes/wp_meta_query/
395 */
396 function getwid_build_meta_query( $meta_query ) {
397
398 for ( $i = 0; $i < count( $meta_query ); $i++ ) {
399
400 $query = $meta_query[$i];
401
402 if ( is_array( $query ) && array_key_exists( 'children', $query ) && count( $query[ 'children' ] ) ) {
403
404 $children = &$query[ 'children' ];
405
406 for ( $j = 0; $j < count( $children ); $j++ ) {
407
408 $object = &$children[$j];
409
410 // Remove empty `type`
411 if ( array_key_exists( 'type', $object ) && empty ( $object['type'] ) ) {
412 unset ( $object['type'] );
413 }
414
415 if ( array_key_exists( 'compare', $object ) ) {
416
417 // Remove empty `compare`
418 if ( empty ( $object['compare'] ) ) {
419
420 unset ( $object['compare'] );
421
422 /*
423 * Default `compare` is `=`, so normalize `value`
424 */
425 if ( array_key_exists( 'value', $object ) ) {
426 if ( is_array( $object['value'] ) && ! empty( $object['value'] ) ) {
427 $object['value'] = array_shift( $object['value'] );
428 }
429 }
430
431 } else {
432
433 // Normalize `value`
434 switch ( $object['compare'] ) {
435
436 case 'IN':
437 case 'NOT IN':
438 case 'BETWEEN':
439 case 'NOT BETWEEN':
440 /*
441 * It can be an array only when compare is 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN'
442 */
443 break;
444
445 case 'EXISTS':
446 case 'NOTEXISTS':
447
448 /*
449 * You don't have to specify a value when using the 'EXISTS' or 'NOT EXISTS' comparisons in WordPress 3.9 and up.
450 */
451 unset( $object['value'], $object['type'] );
452 break;
453
454 default :
455
456 if ( is_array( $object['value'] ) && ! empty( $object['value'] ) ) {
457 $object['value'] = array_shift( $object['value'] );
458 }
459 break;
460 }
461 }
462 }
463
464 // Remove empty `value`
465 if ( array_key_exists( 'value', $object ) ) {
466 if ( is_array( $object['value'] ) && empty( implode( '', $object['value'] ) ) ) {
467 unset ( $object['value'] );
468 }
469 }
470 }
471
472 // Recursion
473 $query = array_merge( $query, getwid_build_meta_query( $query[ 'children' ] ) );
474
475 unset( $query[ 'children' ] );
476 $children = null;
477
478 $meta_query[$i] = $query;
479 }
480 }
481
482 return $meta_query;
483 }
484
485 /**
486 * Determine whether a post or content string has Getwid "nested" blocks.
487 * @since 1.5.3
488 */
489 function has_getwid_nested_blocks() {
490 return getwid()->blocksManager()->hasGetwidNestedBlocks();
491 }
492
493 /*
494 * Check if the ACF plugin active.
495 */
496 function getwid_acf_is_active() {
497 $acf = class_exists( 'ACF' );
498 return $acf;
499 }
500
501 /**
502 * Determines whether the block editor is loaded.
503 * @since 1.7.6
504 */
505 function getwid_is_block_editor() {
506
507 return \defined( 'REST_REQUEST' )
508 && REST_REQUEST
509 && ! empty( $_REQUEST[ 'context' ] )
510 && 'edit' === sanitize_text_field( wp_unslash( $_REQUEST[ 'context' ] ) );
511 }
512
513 /**
514 * Recursive sanitation for an array
515 *
516 * @since 1.7.7
517 *
518 * @param $array
519 *
520 * @return mixed
521 */
522 function getwid_recursive_sanitize_array( $array ) {
523
524 foreach ( $array as $key => &$value ) {
525 if ( is_array( $value ) ) {
526 $value = getwid_recursive_sanitize_array( $value );
527 }
528 else {
529 $value = sanitize_text_field( $value );
530 }
531 }
532
533 return $array;
534 }
535