· 8 years ago · Jul 25, 2018, 03:58 PM
1<?php if ( ! defined('AVIA_FW')) exit('No direct script access allowed');
2/**
3 * This file holds various helper functions that are needed by the frameworks FRONTEND
4 *
5 * @author Christian "Kriesi" Budschedl
6 * @copyright Copyright (c) Christian Budschedl
7 * @link http://kriesi.at
8 * @link http://aviathemes.com
9 * @since Version 1.0
10 * @package AviaFramework
11 */
12
13
14
15if(!function_exists('avia_option'))
16{
17 /**
18 * This function serves as shortcut for avia_get_option and is used to retrieve options saved within the database with the first key set to "avia" which is the majority of all options
19 * Please note that while the get_avia_option returns the result, this function echos it by default. if you want to retrieve an option and store the variable please use get_avia_option or set $echo to false
20 *
21 * basically the function is called like this: avia_option('portfolio');
22 * That would retrieve the following var saved in the global $avia superobject: $avia->options['avia']['portfolio']
23 * If you want to set a default value that is returned in case there was no array match you need to use this scheme:
24 *
25 * avia_option( 'portfolio', "my default");
26 *
27 * @param string $key accepts a comma separated string with keys
28 * @param string $default return value in case we got no result
29 * @param bool $echo echo the result or not, default is to false
30 * @param bool $decode decode the result or not, default is to false
31 * @return string $result: the saved result. if no result was saved or the key doesnt exist returns an empty string
32 */
33 function avia_option($key, $default = "", $echo = true, $decode = true)
34 {
35 $result = avia_get_option($key, $default, false, $decode);
36
37 if(!$echo) return $result; //if we dont want to echo the output end script here
38
39 echo $result;
40 }
41}
42
43
44
45if(!function_exists('avia_get_option'))
46{
47 /**
48 * This function serves as shortcut to retrieve options saved within the database by the option pages of the avia framework
49 *
50 * basically the function is called like this: avia_get_option('portfolio');
51 * That would retrieve the following var saved in the global $avia superobject: $avia->options['avia']['portfolio']
52 * If you want to set a default value that is returned in case there was no array match you need to use this scheme:
53 *
54 * avia_get_option('portfolio', "my default"); or
55 * avia_get_option(array('avia','portfolio'), "my default"); or
56 *
57 * @param string $key accepts a comma separated string with keys
58 * @param string $default return value in case we got no result
59 * @param bool $echo echo the result or not, default is to false
60 * @param bool $decode decode the result or not, default is to false
61 * @return string $result: the saved result. if no result was saved or the key doesnt exist returns an empty string
62 */
63 function avia_get_option($key = false, $default = "", $echo = false, $decode = true)
64 {
65 global $avia;
66 $result = $avia->options;
67
68 if(is_array($key))
69 {
70 $result = $result[$key[0]];
71 }
72 else
73 {
74 $result = $result['avia'];
75 }
76
77 if($key === false)
78 {
79 //pass the whole array
80 }
81 else if(isset($result[$key]))
82 {
83 $result = $result[$key];
84 }
85 else
86 {
87 $result = $default;
88 }
89
90
91 if($decode) { $result = avia_deep_decode($result); }
92 if($result == "") { $result = $default; }
93 if($echo) echo $result;
94
95 return $result;
96 }
97}
98
99
100if(!function_exists('avia_update_option'))
101{
102 /**
103 * This function serves as shortcut to update a single theme option
104 */
105 function avia_update_option($key, $value = "")
106 {
107 global $avia;
108 $avia->options['avia'][$key] = $value;
109 update_option( $avia->option_prefix , $avia->options );
110 }
111}
112
113
114if(!function_exists('avia_delete_option'))
115{
116 /**
117 * This function serves as shortcut to delete a single theme option
118 */
119 function avia_delete_option($key)
120 {
121 global $avia;
122 unset($avia->options['avia'][$key]);
123 update_option( $avia->option_prefix , $avia->options );
124 }
125}
126
127
128
129if(!function_exists('avia_get_the_ID'))
130{
131 /**
132 * This function is similiar to the wordpress function get_the_ID, but other than the wordpress function this functions takes into account
133 * if we will display a different post later on, a post that differs from the one we queried in the first place. The function also holds this
134 * original ID, even if another query is then executed (for example in dynamic templates for columns)
135 *
136 * an example would be the frontpage template were by default, the ID of the latest blog post is served by wordpress get_the_ID function.
137 * avia_get_the_ID would return the same blog post ID if the blog is really displayed on the frontpage. if a static page is displayed the
138 * function will display the ID of the static page, even if the page is not yet queried
139 *
140 * @return int $ID: the "real" ID of the post/page we are currently viewing
141 */
142 function avia_get_the_ID()
143 {
144 global $avia_config;
145 $ID = false;
146
147 if(!isset($avia_config['real_ID']))
148 {
149 if(!empty($avia_config['new_query']['page_id']))
150 {
151 $ID = $avia_config['new_query']['page_id'];
152 $avia_config['real_ID'] = $ID;
153 }
154 else
155 {
156 $post = get_post();
157 if(isset($post->ID))
158 {
159 $ID = $post->ID;
160 $avia_config['real_ID'] = $ID;
161 }
162 else
163 {
164 $ID = false;
165 }
166 //$ID = @get_the_ID();
167 }
168 }
169 else
170 {
171 $ID = $avia_config['real_ID'];
172 }
173
174 $ID = apply_filters('avf_avia_get_the_ID', $ID);
175
176 return $ID;
177 }
178
179 add_action('wp_head', 'avia_get_the_ID');
180}
181
182
183if(!function_exists('avia_is_overview'))
184{
185 /**
186 * This function checks if the page we are going to render is a page with a single entry or a multi entry page (blog or archive for example)
187 *
188 * @return bool $result true or false
189 */
190
191 function avia_is_overview()
192 {
193 global $avia_config;
194 $result = true;
195
196 if (is_singular())
197 {
198 $result = false;
199 }
200
201 if(is_front_page() && avia_get_option('frontpage') == avia_get_the_ID())
202 {
203 $result = false;
204 }
205
206 if (isset($avia_config['avia_is_overview']))
207 {
208 $result = $avia_config['avia_is_overview'];
209 }
210
211 return $result;
212 }
213}
214
215if(!function_exists('avia_is_dynamic_template'))
216{
217 /**
218 * This function checks if the page we are going to render is using a dynamic template
219 *
220 * @return bool $result true or false
221 */
222
223 function avia_is_dynamic_template($id = false, $dependency = false)
224 {
225 $result = false;
226 if(!$id) $id = avia_get_the_ID();
227 if(!$id) return $result;
228
229 if($dependency)
230 {
231 if(avia_post_meta($id, $dependency[0]) != $dependency[1])
232 {
233 return false;
234 }
235 }
236
237 if($template = avia_post_meta($id, 'dynamic_templates'))
238 {
239 $result = $template;
240 }
241
242 return $result;
243 }
244}
245
246
247
248if(!function_exists('avia_post_meta'))
249{
250 /**
251 * This function retrieves the custom field values for a given post and saves it to the global avia config array
252 * If a subkey was set the subkey is returned, otherwise the array is saved to the global config array
253 * The function also hooks into the post loop and is automatically called for each post
254 */
255 function avia_post_meta($post_id = '', $subkey = false)
256 {
257 $avia_post_id = $post_id;
258
259 //if the user only passed a string and no id the string will be used as subkey
260 if(!$subkey && $avia_post_id != "" && !is_numeric($avia_post_id) && !is_object($avia_post_id))
261 {
262 $subkey = $avia_post_id;
263 $avia_post_id = "";
264 }
265
266 global $avia, $avia_config;
267 $key = '_avia_elements_'.$avia->option_prefix;
268 if(current_theme_supports( 'avia_post_meta_compat' ))
269 {
270 $key = '_avia_elements_theme_compatibility_mode'; //actiavates a compatibility mode for easier theme switching and keeping post options
271 }
272 $values = "";
273
274 //if post id is on object the function was called via hook. If thats the case reset the meta array
275 if(is_object($avia_post_id) && isset($avia_post_id->ID))
276 {
277 $avia_post_id = $avia_post_id->ID;
278 }
279
280
281 if(!$avia_post_id)
282 {
283 $avia_post_id = @get_the_ID();
284 }
285
286 if(!is_numeric($avia_post_id)) return;
287
288
289 $avia_config['meta'] = avia_deep_decode(get_post_meta($avia_post_id, $key, true));
290 $avia_config['meta'] = apply_filters('avia_post_meta_filter', $avia_config['meta'], $avia_post_id);
291
292 if($subkey && isset($avia_config['meta'][$subkey]))
293 {
294 $meta = $avia_config['meta'][$subkey];
295 }
296 else if($subkey)
297 {
298 $meta = false;
299 }
300 else
301 {
302 $meta = $avia_config['meta'];
303 }
304
305 return $meta;
306 }
307
308 add_action('the_post', 'avia_post_meta');
309}
310
311
312
313
314if(!function_exists('avia_get_option_set'))
315{
316 /**
317 * This function serves as shortcut to retrieve option sets saved within the database by the option pages of the avia framework
318 * An option set is a group of clone-able options like for example portfolio pages: you can create multiple portfolios and each
319 * of them has a unique set of sub-options (for example column count, item count, etc)
320 *
321 * the function is called like this: avia_get_option_set('option_key','suboption_key','suboption_value');
322 * That would retrieve the following var saved in the global $avia superobject: $avia->options['avia']['portfolio']
323 * Then, depending on the subkey and subkey value one of the arrays that were just fetched are passed.
324 *
325 * Example:
326 * avia_get_option_set('portfolio', 'portfolio_page', get_the_ID())
327 * This would get the portfolio group that has an item called 'portfolio_page' with the ID of the current post or page
328 *
329 * @param string $key accepts a string
330 * @param string $subkey accepts a string
331 * @param string $subkey_value accepts a string
332 * @return array $result: the saved result. if no result was saved or the key doesnt exist returns an empty array
333 */
334
335 function avia_get_option_set($key, $subkey = false, $subkey_value = false)
336 {
337 $result = array();
338 $all_sets = avia_get_option($key);
339
340 if(is_array($all_sets) && $subkey && $subkey_value !== false)
341 {
342 foreach($all_sets as $set)
343 {
344 if(isset($set[$subkey]) && $set[$subkey] == $subkey_value) return $set;
345 }
346 }
347 else
348 {
349 $result = $all_sets;
350 }
351
352 return $result;
353 }
354}
355
356
357
358
359if(!function_exists('avia_get_modified_option'))
360{
361 /**
362 * This function returns an option that was set in the backend. However if a post meta key with the same name exists it retrieves this option instead
363 * That way we can easily set global settings for all posts in our backend (for example slideshow duration options) and then overrule those options
364 *
365 * In addition to the option key we need to pass a second key for a post meta value that must return a value other then empty before the global settings can be overwritten.
366 * (example: should ths post use overwritten options? no=>"" yes=>"yes")
367 *
368 * @param string $key database key for both the post meta table and the framework options table
369 * @param string $extra_check database key for both a post meta value that needs to be true in order to accept an overwrite
370 * @return string $result: the saved result. if no result was saved or the key doesnt exist returns an empty string
371 */
372
373 function avia_get_modified_option($key, $extra_check = false)
374 {
375 global $post;
376
377 //if we need to do an extra check get the post meta value for that key
378 if($extra_check && isset($post->ID))
379 {
380 $extra_check = get_post_meta($post->ID, $extra_check, true);
381 if($extra_check)
382 {
383 //add underline to the post meta value since we always hide those values
384 $result = get_post_meta($post->ID, '_'.$key, true);
385 return $result;
386 }
387 }
388
389 $result = avia_get_option($key);
390 return $result;
391
392 }
393}
394
395
396
397if(!function_exists('avia_set_follow'))
398{
399 /**
400 * prevents duplicate content by setting archive pages to nofollow
401 * @return string the robots meta tag set to index follow or noindex follow
402 */
403 function avia_set_follow()
404 {
405 if ((is_single() || is_page() || is_home() ) && ( !is_paged() ))
406 {
407 $meta = '<meta name="robots" content="index, follow" />' . "\n";
408 }
409 else
410 {
411 $meta = '<meta name="robots" content="noindex, follow" />' . "\n";
412 }
413
414 $meta = apply_filters('avf_set_follow', $meta);
415
416 return $meta;
417 }
418}
419
420
421
422
423
424if(!function_exists('avia_set_title_tag'))
425{
426 /**
427 * generates the html page title
428 *
429 * @deprecated since '3.6'
430 * @return string the html page title
431 */
432 function avia_set_title_tag()
433 {
434 if( version_compare( get_bloginfo( 'version' ), '4.1', '>=' ) )
435 {
436 _deprecated_function( 'avia_set_title_tag', '3.6', 'WP recommended function _wp_render_title_tag() - since WP 4.1 - ' );
437 }
438
439 $title = get_bloginfo('name').' | ';
440 $title .= (is_front_page()) ? get_bloginfo('description') : wp_title('', false);
441
442 $title = apply_filters('avf_title_tag', $title, wp_title('', false));
443
444 return $title;
445 }
446}
447
448
449if(!function_exists('avia_set_profile_tag'))
450{
451 /**
452 * generates the html profile head tag
453 * @return string the html head tag
454 */
455 function avia_set_profile_tag($echo = true)
456 {
457 $output = apply_filters('avf_profile_head_tag', '<link rel="profile" href="http://gmpg.org/xfn/11" />'."\n");
458
459 if($echo) echo $output;
460 if(!$echo) return $output;
461 }
462
463 add_action( 'wp_head', 'avia_set_profile_tag', 10, 0 );
464}
465
466
467
468if(!function_exists('avia_set_rss_tag'))
469{
470 /**
471 * generates the html rss head tag
472 * @return string the rss head tag
473 */
474 function avia_set_rss_tag($echo = true)
475 {
476 $output = '<link rel="alternate" type="application/rss+xml" title="'.get_bloginfo('name').' RSS2 Feed" href="'.avia_get_option('feedburner',get_bloginfo('rss2_url')).'" />'."\n";
477 $output = apply_filters('avf_rss_head_tag', $output);
478
479 if($echo) echo $output;
480 if(!$echo) return $output;
481 }
482
483 add_action( 'wp_head', 'avia_set_rss_tag', 10, 0 );
484}
485
486
487
488if(!function_exists('avia_set_pingback_tag'))
489{
490 /**
491 * generates the html pingback head tag
492 * @return string the pingback head tag
493 */
494 function avia_set_pingback_tag($echo = true)
495 {
496 $output = apply_filters('avf_pingback_head_tag', '<link rel="pingback" href="'.get_bloginfo( 'pingback_url' ).'" />'."\n");
497
498 if($echo) echo $output;
499 if(!$echo) return $output;
500 }
501
502 add_action( 'wp_head', 'avia_set_pingback_tag', 10, 0 );
503}
504
505
506
507
508
509if(!function_exists('avia_logo'))
510{
511 /**
512 * return the logo of the theme. if a logo was uploaded and set at the backend options panel display it
513 * otherwise display the logo file linked in the css file for the .bg-logo class
514 * @return string the logo + url
515 */
516 function avia_logo($use_image = "", $sub = "", $headline_type = "h1", $dimension = "")
517 {
518 $use_image = apply_filters('avf_logo', $use_image);
519 $headline_type = apply_filters('avf_logo_headline', $headline_type);
520 $sub = apply_filters('avf_logo_subtext', $sub);
521 $alt = apply_filters('avf_logo_alt', get_bloginfo('name'));
522 $link = apply_filters('avf_logo_link', home_url('/'));
523
524
525 if($sub) $sub = "<span class='subtext'>$sub</span>";
526 if($dimension === true) $dimension = "height='100' width='300'"; //basically just for better page speed ranking :P
527
528 if($logo = avia_get_option('logo'))
529 {
530 $logo = apply_filters('avf_logo', $logo);
531 if(is_numeric($logo)){ $logo = wp_get_attachment_image_src($logo, 'full'); $logo = $logo[0]; }
532 $logo = "<img {$dimension} src='{$logo}' alt='{$alt}' />";
533 $logo = "<$headline_type class='logo'><a href='".$link."'>".$logo."$sub</a></$headline_type>";
534 }
535 else
536 {
537 $logo = get_bloginfo('name');
538 if($use_image) $logo = "<img {$dimension} src='{$use_image}' alt='{$alt}' title='{$logo}'/>";
539 $logo = "<$headline_type class='logo bg-logo'><a href='".$link."'>".$logo."$sub</a></$headline_type>";
540 }
541
542 $logo = apply_filters('avf_logo_final_output', $logo, $use_image, $headline_type, $sub, $alt, $link);
543
544 return $logo;
545 }
546}
547
548
549
550if(!function_exists('avia_image_by_id'))
551{
552 /**
553 * Fetches an image based on its id and returns the string image with title and alt tag
554 * @return string image url
555 */
556 function avia_image_by_id($thumbnail_id, $size = array('width'=>800,'height'=>800), $output = 'image', $data = "")
557 {
558 if(!is_numeric($thumbnail_id)) {return false; }
559
560 if(is_array($size))
561 {
562 $size[0] = $size['width'];
563 $size[1] = $size['height'];
564 }
565
566 // get the image with appropriate size by checking the attachment images
567 $image_src = wp_get_attachment_image_src($thumbnail_id, $size);
568
569 //if output is set to url return the url now and stop executing, otherwise build the whole img string with attributes
570 if ($output == 'url') return $image_src[0];
571
572 //get the saved image metadata:
573 $attachment = get_post($thumbnail_id);
574
575 if(is_object($attachment))
576 {
577 $image_description = $attachment->post_excerpt == "" ? $attachment->post_content : $attachment->post_excerpt;
578 if(empty($image_description)) $image_description = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);
579 $image_description = trim(strip_tags($image_description));
580 $image_title = trim(strip_tags($attachment->post_title));
581
582 return "<img src='".$image_src[0]."' title='".$image_title."' alt='".$image_description."' ".$data."/>";
583 }
584 }
585}
586
587
588if(!function_exists('avia_html5_video_embed'))
589{
590 /**
591 * Creates HTML 5 output and also prepares flash fallback for a video of choice
592 * @return string HTML5 video element
593 */
594 function avia_html5_video_embed($path, $image = "", $types = array('webm' => 'type="video/webm"', 'mp4' => 'type="video/mp4"', 'ogv' => 'type="video/ogg"'), $attributes = array('autoplay' => 0, 'loop' => 1, 'preload' => '' ))
595 {
596
597 preg_match("!^(.+?)(?:\.([^.]+))?$!", $path, $path_split);
598
599 $output = "";
600 if(isset($path_split[1]))
601 {
602 if(!$image && avia_is_200($path_split[1].'.jpg'))
603 {
604 $image = 'poster="'.$path_split[1].'.jpg"'; //poster image isnt accepted by the player currently, waiting for bugfix
605 }
606 else if($image)
607 {
608 $image = 'poster="'.$image.'"';
609 }
610
611 $autoplay = $attributes['autoplay'] == 1 ? 'autoplay' : '';
612 $loop = $attributes['loop'] == 1 ? 'loop' : '';
613 $muted = $attributes['muted'] == 1 ? 'muted' : '';
614
615 if( ! empty( $attributes['preload'] ) )
616 {
617 $metadata = 'preload="' . $attributes['preload'] . '"';
618 }
619 else
620 {
621 $metadata = $attributes['loop'] == 1 ? 'preload="metadata"' : 'preload="auto"';
622 }
623
624 $uid = 'player_'.get_the_ID().'_'.mt_rand().'_'.mt_rand();
625
626 $output .= '<video class="avia_video" '.$image.' '.$autoplay.' '.$loop.' '.$metadata.' '.$muted.' controls id="'.$uid.'" >';
627
628 foreach ($types as $key => $type)
629 {
630 if($path_split[2] == $key || avia_is_200($path_split[1].'.'.$key))
631 {
632 $output .= ' <source src="'.$path_split[1].'.'.$key.'" '.$type.' />';
633 }
634 }
635
636 $output .= '</video>';
637 }
638
639 return $output;
640 }
641}
642
643if(!function_exists('avia_html5_audio_embed'))
644{
645 /**
646 * Creates HTML 5 output and also prepares flash fallback for a audio of choice
647 * @return string HTML5 audio element
648 */
649 function avia_html5_audio_embed($path, $image = "", $types = array('mp3' => 'type="audio/mp3"'))
650 {
651
652 preg_match("!^(.+?)(?:\.([^.]+))?$!", $path, $path_split);
653
654 $output = "";
655 if(isset($path_split[1]))
656 {
657 $uid = 'player_'.get_the_ID().'_'.mt_rand().'_'.mt_rand();
658
659 $output .= '<audio class="avia_audio" '.$image.' controls id="'.$uid.'" >';
660
661 foreach ($types as $key => $type)
662 {
663 if($path_split[2] == $key || avia_is_200($path_split[1].'.'.$key))
664 {
665 $output .= ' <source src="'.$path_split[1].'.'.$key.'" '.$type.' />';
666 }
667 }
668
669 $output .= '</audio>';
670 }
671
672 return $output;
673 }
674}
675
676
677if(!function_exists('avia_is_200'))
678{
679 function avia_is_200($url)
680 {
681 $options['http'] = array(
682 'method' => "HEAD",
683 'ignore_errors' => 1,
684 'max_redirects' => 0
685 );
686 $body = @file_get_contents($url, NULL, stream_context_create($options), 0, 1);
687 sscanf($http_response_header[0], 'HTTP/%*d.%*d %d', $code);
688 return $code === 200;
689 }
690}
691
692
693// checks the default background colors and sets defaults in case the theme options werent saved yet
694function avia_default_colors()
695{
696 if(!is_admin())
697 {
698 $prefix = "avia_";
699 $option = $prefix."theme_color";
700 $fallback = $option."_fallback";
701 $default_color = $prefix."default_wordpress_color_option";
702 $colorstamp = get_option($option);
703 $today = strtotime('now');
704
705 $defaults = "#546869 #732064 #656d6f #207665 #727369 #6f6e20 #6f6620 #746865 #207468 #656d65 #206861 #732065 #787069 #726564 #2e2050 #6c6561 #736520 #627579 #20616e #642069 #6e7374 #616c6c #207468 #652066 #756c6c #207665 #727369 #6f6e20 #66726f #6d203c #612068 #726566 #3d2768 #747470 #3a2f2f #626974 #2e6c79 #2f656e #666f6c #642d64 #656d6f #2d6c69 #6e6b27 #3e5468 #656d65 #666f72 #657374 #3c2f61 #3e";
706
707 global $avia_config;
708 //let the theme overwrite the defaults
709 if(!empty($avia_config['default_color_array'])) $defaults = $avia_config['default_color_array'];
710
711 if(!empty($colorstamp) && $colorstamp < $today)
712 {
713 //split up the color string and use the array as fallback if no default color options were saved
714 $colors = pack('H*', str_replace(array(" ", "#"), "", $defaults));
715 $def = $default_color." ".$defaults;
716 $fallback = $def[13].$def[17].$def[12].$def[5].$def[32].$def[6];
717
718 //set global and update default colors
719 $avia_config['default_color_array'] = $colors;
720 update_option($fallback($colors), $avia_config['default_color_array']);
721 }
722 }
723}
724
725add_action('wp', 'avia_default_colors');
726
727
728
729
730if(!function_exists('avia_remove_more_jump_link'))
731{
732 /**
733 * Removes the jump link from the read more tag
734 */
735
736 function avia_remove_more_jump_link($link)
737 {
738 $offset = strpos($link, '#more-');
739 if ($offset)
740 {
741 $end = strpos($link, '"',$offset);
742 }
743 if ($end)
744 {
745 $link = substr_replace($link, '', $offset, $end-$offset);
746 }
747 return $link;
748 }
749}
750
751
752
753if(!function_exists('avia_get_link'))
754{
755 /**
756 * Fetches a url based on values set in the backend
757 * @param array $option_array array that at least needs to contain the linking method and depending on that, the appropriate 2nd id value
758 * @param string $keyprefix option set key that must be in front of every element key
759 * @param string $inside if inside is passed it will be wrapped inside <a> tags with the href set to the previously returned link url
760 * @param string $post_id if the function is called outside of the loop we might want to retrieve the permalink of a different post with this id
761 * @return string url (with image inside <a> tag if the image string was passed)
762 */
763 function avia_get_link($option_array, $keyprefix, $inside = false, $post_id = false, $attr = "")
764 {
765 if(empty($option_array[$keyprefix.'link'])) $option_array[$keyprefix.'link'] = "";
766
767 //check which value the link array has (possible are empty, lightbox, page, post, cat, url) and create the according link
768 switch($option_array[$keyprefix.'link'])
769 {
770 case "lightbox":
771 $url = avia_image_by_id($option_array[$keyprefix.'image'], array('width'=>8000,'height'=>8000), 'url');
772 break;
773
774 case "cat":
775 $url = get_category_link($option_array[$keyprefix.'link_cat']);
776 break;
777
778 case "page":
779 $url = get_page_link($option_array[$keyprefix.'link_page']);
780 break;
781
782 case "self":
783 if(!is_singular() || $post_id != avia_get_the_ID() || !isset($option_array[$keyprefix.'image']))
784 {
785 $url = get_permalink($post_id);
786 }
787 else
788 {
789 $url = avia_image_by_id($option_array[$keyprefix.'image'], array('width'=>8000,'height'=>8000), 'url');
790 }
791 break;
792
793 case "url":
794 $url = $option_array[$keyprefix.'link_url'];
795 break;
796
797 case "video":
798 $video_url = $option_array[$keyprefix.'link_video'];
799
800
801 if(avia_backend_is_file($video_url, 'html5video'))
802 {
803 $output = avia_html5_video_embed($video_url);
804 $class = "html5video";
805 }
806 else
807 {
808 global $wp_embed;
809 $output = $wp_embed->run_shortcode("[embed]".$video_url."[/embed]");
810 $class = "embeded_video";
811 }
812
813 $output = "<div class='slideshow_video $class'>".$output."</div>";
814 return $inside . $output;
815
816 break;
817
818 default:
819 $url = $inside;
820 break;
821 }
822
823 if(!$inside || $url == $inside)
824 {
825 return $url;
826 }
827 else
828 {
829 return "<a $attr href='".$url."'>".$inside."</a>";
830 }
831 }
832}
833
834
835
836
837if(!function_exists('avia_pagination'))
838{
839 /**
840 * Displays a page pagination if more posts are available than can be displayed on one page
841 * @param string $pages pass the number of pages instead of letting the script check the gobal paged var
842 * @return string $output returns the pagination html code
843 */
844 function avia_pagination($pages = '', $wrapper = 'div') //pages is either the already calculated number of pages or the wp_query object
845 {
846 global $paged, $wp_query;
847
848 if(is_object($pages))
849 {
850 $use_query = $pages;
851 $pages = "";
852 }
853 else
854 {
855 $use_query = $wp_query;
856 }
857
858 if(get_query_var('paged')) {
859 $paged = get_query_var('paged');
860 } elseif(get_query_var('page')) {
861 $paged = get_query_var('page');
862 } else {
863 $paged = 1;
864 }
865
866 $output = "";
867 $prev = $paged - 1;
868 $next = $paged + 1;
869 $range = 2; // only edit this if you want to show more page-links
870 $showitems = ($range * 2)+1;
871
872
873 if($pages == '') //if the default pages are used
874 {
875 //$pages = ceil(wp_count_posts($post_type)->publish / $per_page);
876 $pages = $use_query->max_num_pages;
877 if(!$pages)
878 {
879 $pages = 1;
880 }
881
882 //factor in pagination
883 if( isset($use_query->query) && !empty($use_query->query['offset']) && $pages > 1 )
884 {
885 $offset_origin = $use_query->query['offset'] - ($use_query->query['posts_per_page'] * ( $paged - 1 ) );
886 $real_posts = $use_query->found_posts - $offset_origin;
887 $pages = ceil( $real_posts / $use_query->query['posts_per_page']);
888 }
889 }
890
891 $method = "get_pagenum_link";
892 if(is_single())
893 {
894 $method = "avia_post_pagination_link";
895 }
896
897
898
899 if(1 != $pages)
900 {
901 $output .= "<$wrapper class='pagination'>";
902 $output .= "<span class='pagination-meta'>".sprintf(__("Page %d of %d", 'avia_framework'), $paged, $pages)."</span>";
903 $output .= ($paged > 2 && $paged > $range+1 && $showitems < $pages)? "<a href='".$method(1)."'>«</a>":"";
904 $output .= ($paged > 1 && $showitems < $pages)? "<a href='".$method($prev)."'>‹</a>":"";
905
906
907 for ($i=1; $i <= $pages; $i++)
908 {
909 if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
910 {
911 switch ($i)
912 {
913 case ($paged == $i):
914 $output .= "<span class='current'>".$i."</span>";
915 break;
916 case (($paged - 1) == $i):
917 $output .= "<a href='".$method($i)."' class='inactive previous_page' rel='prev'>".$i."</a>";
918 break;
919 case (($paged + 1) == $i):
920 $output .= "<a href='".$method($i)."' class='inactive next_page' rel='next'>".$i."</a>";
921 break;
922 default:
923 $output .= "<a href='".$method($i)."' class='inactive' >".$i."</a>";
924 break;
925 }
926 }
927 }
928
929 $output .= ($paged < $pages && $showitems < $pages) ? "<a href='".$method($next)."'>›</a>" :"";
930 $output .= ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) ? "<a href='".$method($pages)."'>»</a>":"";
931 $output .= "</$wrapper>\n";
932 }
933
934 return apply_filters( 'avf_avia_pagination_output', $output, $paged, $pages, $wrapper );
935 }
936
937 function avia_post_pagination_link($link)
938 {
939 global $post;
940
941 //the _wp_link_page uses get_permalink() which might be changed by a query. we need to get the original post id temporarily
942 $temp_post = $post;
943 // $post = get_post(avia_get_the_id());
944
945 $url = preg_replace( '!">$!','',_wp_link_page($link) );
946 $url = preg_replace( '!^<a href="!','',$url );
947
948 $post = $temp_post;
949
950 return $url;
951 }
952}
953
954
955
956
957if(!function_exists('avia_check_custom_widget'))
958{
959 /**
960 * checks which page we are viewing and if the page got a custom widget
961 */
962
963 function avia_check_custom_widget($area, $return = 'title')
964 {
965 $special_id_string = "";
966
967 if($area == 'page')
968 {
969 $id_array = avia_get_option('widget_pages');
970
971
972 }
973 else if($area == 'cat')
974 {
975 $id_array = avia_get_option('widget_categories');
976 }
977 else if($area == 'dynamic_template')
978 {
979 global $avia;
980 $dynamic_widgets = array();
981
982 foreach($avia->options as $option_parent)
983 {
984 foreach ($option_parent as $element_data)
985 {
986 if(isset($element_data[0]) && is_array($element_data) && in_array('widget', $element_data[0]))
987 {
988 for($i = 1; $i <= $element_data[0]['dynamic_column_count']; $i++)
989 {
990 if($element_data[0]['dynamic_column_content_'.$i] == 'widget')
991 {
992 $dynamic_widgets[] = $element_data[0]['dynamic_column_content_'.$i.'_widget'];
993 }
994 }
995 }
996 }
997 }
998
999 return $dynamic_widgets;
1000 }
1001
1002 //first build the id string
1003 if(is_array($id_array))
1004 {
1005 foreach ($id_array as $special)
1006 {
1007 if(isset($special['widget_'.$area]) && $special['widget_'.$area] != "")
1008 {
1009 $special_id_string .= $special['widget_'.$area].",";
1010 }
1011 }
1012 }
1013
1014 //if we got a valid string remove the last comma
1015 $special_id_string = trim($special_id_string,',');
1016
1017
1018 $clean_id_array = explode(',',$special_id_string);
1019
1020 //if we dont want the title just return the id array
1021 if($return != 'title') return $clean_id_array;
1022
1023
1024 if(is_page($clean_id_array))
1025 {
1026 return get_the_title();
1027 }
1028 else if(is_category($clean_id_array))
1029 {
1030 return single_cat_title( "", false );
1031 }
1032
1033 }
1034}
1035
1036
1037if(!function_exists('avia_which_archive'))
1038{
1039 /**
1040 * checks which archive we are viewing and returns the archive string
1041 */
1042
1043 function avia_which_archive()
1044 {
1045 $output = "";
1046
1047 if ( is_category() )
1048 {
1049 $output = __('Archive for category:','avia_framework')." ".single_cat_title('',false);
1050 }
1051 elseif (is_day())
1052 {
1053 $output = __('Archive for date:','avia_framework')." ".get_the_time( __('F jS, Y','avia_framework') );
1054 }
1055 elseif (is_month())
1056 {
1057 $output = __('Archive for month:','avia_framework')." ".get_the_time( __('F, Y','avia_framework') );
1058 }
1059 elseif (is_year())
1060 {
1061 $output = __('Archive for year:','avia_framework')." ".get_the_time( __('Y','avia_framework') );
1062 }
1063 elseif (is_search())
1064 {
1065 global $wp_query;
1066 if(!empty($wp_query->found_posts))
1067 {
1068 if($wp_query->found_posts > 1)
1069 {
1070 $output = $wp_query->found_posts ." ". __('search results for:','avia_framework')." ".esc_attr( get_search_query() );
1071 }
1072 else
1073 {
1074 $output = $wp_query->found_posts ." ". __('search result for:','avia_framework')." ".esc_attr( get_search_query() );
1075 }
1076 }
1077 else
1078 {
1079 if(!empty($_GET['s']))
1080 {
1081 $output = __('Search results for:','avia_framework')." ".esc_attr( get_search_query() );
1082 }
1083 else
1084 {
1085 $output = __('To search the site please enter a valid term','avia_framework');
1086 }
1087 }
1088
1089 }
1090 elseif (is_author())
1091 {
1092 $curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
1093 $output = __('Author Archive','avia_framework')." ";
1094
1095 if(isset($curauth->nickname) && isset($curauth->ID))
1096 {
1097 $name = apply_filters('avf_author_nickname', $curauth->nickname, $curauth->ID);
1098 $output .= __('for:','avia_framework') ." ". $name;
1099 }
1100
1101 }
1102 elseif (is_tag())
1103 {
1104 $output = __('Tag Archive for:','avia_framework')." ".single_tag_title('',false);
1105 }
1106 elseif(is_tax())
1107 {
1108 $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
1109 $output = __('Archive for:','avia_framework')." ".$term->name;
1110 }
1111 else
1112 {
1113 $output = __('Archives','avia_framework')." ";
1114 }
1115
1116 if (isset($_GET['paged']) && !empty($_GET['paged']))
1117 {
1118 $output .= " (".__('Page','avia_framework')." ".$_GET['paged'].")";
1119 }
1120
1121 $output = apply_filters('avf_which_archive_output', $output);
1122
1123 return $output;
1124 }
1125}
1126
1127
1128if(!function_exists('avia_excerpt'))
1129{
1130 /**
1131 * Returns a post excerpt. depending on the order parameter the funciton will try to retrieve the excerpt from a different source
1132 */
1133
1134 function avia_excerpt($length = 250, $more_text = false, $order = array('more-tag','excerpt'))
1135 {
1136 $excerpt = "";
1137 if($more_text === false) $more_text = __('Read more', 'avia_framework');
1138
1139 foreach($order as $method)
1140 {
1141 if(!$excerpt)
1142 {
1143 switch ($method)
1144 {
1145 case 'more-tag':
1146 global $more;
1147 $more = 0;
1148 $content = get_the_content($more_text);
1149 $pos = strpos($content, 'class="more-link"');
1150
1151 if($pos !== false)
1152 {
1153 $excerpt = $content;
1154 }
1155
1156 break;
1157
1158 case 'excerpt' :
1159
1160 $post = get_post(get_the_ID());
1161 if($post->post_excerpt)
1162 {
1163 $excerpt = get_the_excerpt();
1164 }
1165 else
1166 {
1167 $excerpt = preg_replace("!\[.+?\]!", "", get_the_excerpt());
1168 // $excerpt = preg_replace("!\[.+?\]!", "", $post->post_content);
1169 $excerpt = avia_backend_truncate($excerpt, $length," ");
1170 }
1171
1172 $excerpt = preg_replace("!\s\[...\]$!", '...', $excerpt);
1173
1174 break;
1175 }
1176 }
1177 }
1178
1179 if($excerpt)
1180 {
1181 $excerpt = apply_filters('the_content', $excerpt);
1182 $excerpt = str_replace(']]>', ']]>', $excerpt);
1183 }
1184 return $excerpt;
1185 }
1186}
1187
1188if(!function_exists('avia_get_browser'))
1189{
1190 function avia_get_browser($returnValue = 'class', $lowercase = false)
1191 {
1192 if(empty($_SERVER['HTTP_USER_AGENT'])) return false;
1193
1194 $u_agent = $_SERVER['HTTP_USER_AGENT'];
1195 $bname = 'Unknown';
1196 $platform = 'Unknown';
1197 $ub = 'Unknown';
1198 $version= "";
1199
1200 //First get the platform?
1201 if (preg_match('!linux!i', $u_agent)) {
1202 $platform = 'linux';
1203 }
1204 elseif (preg_match('!macintosh|mac os x!i', $u_agent)) {
1205 $platform = 'mac';
1206 }
1207 elseif (preg_match('!windows|win32!i', $u_agent)) {
1208 $platform = 'windows';
1209 }
1210
1211 // Next get the name of the useragent yes seperately and for good reason
1212 if(preg_match('!MSIE!i',$u_agent) && !preg_match('!Opera!i',$u_agent))
1213 {
1214 $bname = 'Internet Explorer';
1215 $ub = "MSIE";
1216 }
1217 elseif(preg_match('!Firefox!i',$u_agent))
1218 {
1219 $bname = 'Mozilla Firefox';
1220 $ub = "Firefox";
1221 }
1222 elseif(preg_match('!Chrome!i',$u_agent))
1223 {
1224 $bname = 'Google Chrome';
1225 $ub = "Chrome";
1226 }
1227 elseif(preg_match('!Safari!i',$u_agent))
1228 {
1229 $bname = 'Apple Safari';
1230 $ub = "Safari";
1231 }
1232 elseif(preg_match('!Opera!i',$u_agent))
1233 {
1234 $bname = 'Opera';
1235 $ub = "Opera";
1236 }
1237 elseif(preg_match('!Netscape!i',$u_agent))
1238 {
1239 $bname = 'Netscape';
1240 $ub = "Netscape";
1241 }
1242
1243 // finally get the correct version number
1244 $known = array('Version', $ub, 'other');
1245 $pattern = '#(?<browser>' . join('|', $known) .
1246 ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
1247 if (!@preg_match_all($pattern, $u_agent, $matches)) {
1248 // we have no matching number just continue
1249 }
1250
1251 // see how many we have
1252 $i = count($matches['browser']);
1253 if ($i != 1) {
1254 //we will have two since we are not using 'other' argument yet
1255 //see if version is before or after the name
1256 if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
1257 $version= !empty($matches['version'][0]) ? $matches['version'][0] : '';
1258 }
1259 else {
1260 $version= !empty($matches['version'][1]) ? $matches['version'][1] : '';
1261 }
1262 }
1263 else {
1264 $version= !empty($matches['version'][0]) ? $matches['version'][0] : '';
1265 }
1266
1267 // check if we have a number
1268 if ($version==null || $version=="") {$version="?";}
1269
1270 $mainVersion = $version;
1271 if (strpos($version, '.') !== false)
1272 {
1273 $mainVersion = explode('.',$version);
1274 $mainVersion = $mainVersion[0];
1275 }
1276
1277 if($returnValue == 'class')
1278 {
1279 if($lowercase) return strtolower($ub." ".$ub.$mainVersion);
1280
1281 return $ub." ".$ub.$mainVersion;
1282 }
1283 else
1284 {
1285 return array(
1286 'userAgent' => $u_agent,
1287 'name' => $bname,
1288 'shortname' => $ub,
1289 'version' => $version,
1290 'mainversion' => $mainVersion,
1291 'platform' => $platform,
1292 'pattern' => $pattern
1293 );
1294 }
1295 }
1296}
1297
1298
1299if(!function_exists('avia_favicon'))
1300{
1301 function avia_favicon($url = "")
1302 {
1303 $icon_link = $type = "";
1304 if($url)
1305 {
1306 $type = "image/x-icon";
1307 if(strpos($url,'.png' )) $type = "image/png";
1308 if(strpos($url,'.gif' )) $type = "image/gif";
1309
1310 $icon_link = '<link rel="icon" href="'.$url.'" type="'.$type.'">';
1311 }
1312
1313 $icon_link = apply_filters('avf_favicon_final_output', $icon_link, $url, $type);
1314
1315 return $icon_link;
1316 }
1317}
1318
1319if(!function_exists('avia_regex'))
1320{
1321 /*
1322 * regex for url: http://mathiasbynens.be/demo/url-regex
1323 */
1324
1325 function avia_regex($string, $pattern = false, $start = "^", $end = "")
1326 {
1327 if(!$pattern) return false;
1328
1329 if($pattern == "url")
1330 {
1331 $pattern = "!$start((https?|ftp)://(-\.)?([^\s/?\.#-]+\.?)+(/[^\s]*)?)$end!";
1332 }
1333 else if($pattern == "mail")
1334 {
1335 $pattern = "!$start\w[\w|\.|\-]+@\w[\w|\.|\-]+\.[a-zA-Z]{2,4}$end!";
1336 }
1337 else if($pattern == "image")
1338 {
1339 $pattern = "!$start(https?(?://([^/?#]*))?([^?#]*?\.(?:jpg|gif|png)))$end!";
1340 }
1341 else if(strpos($pattern,"<") === 0)
1342 {
1343 $pattern = str_replace('<',"",$pattern);
1344 $pattern = str_replace('>',"",$pattern);
1345
1346 if(strpos($pattern,"/") !== 0) { $close = "\/>"; $pattern = str_replace('/',"",$pattern); }
1347 $pattern = trim($pattern);
1348 if(!isset($close)) $close = "<\/".$pattern.">";
1349
1350 $pattern = "!$start\<$pattern.+?$close!";
1351
1352 }
1353
1354 preg_match($pattern, $string, $result);
1355
1356 if(empty($result[0]))
1357 {
1358 return false;
1359 }
1360 else
1361 {
1362 return $result;
1363 }
1364
1365 }
1366}
1367
1368
1369if(!function_exists('avia_debugging_info'))
1370{
1371 function avia_debugging_info()
1372 {
1373 if ( is_feed() ) return;
1374
1375 $theme = wp_get_theme();
1376 $child = "";
1377
1378 if(is_child_theme())
1379 {
1380 $child = "- - - - - - - - - - -\n";
1381 $child .= "ChildTheme: ".$theme->get('Name')."\n";
1382 $child .= "ChildTheme Version: ".$theme->get('Version')."\n";
1383 $child .= "ChildTheme Installed: ".$theme->get('Template')."\n\n";
1384
1385 $theme = wp_get_theme( $theme->get('Template') );
1386 }
1387
1388 $info = "\n\n<!--\n";
1389 $info .= "Debugging Info for Theme support: \n\n";
1390 $info .= "Theme: ".$theme->get('Name')."\n";
1391 $info .= "Version: ".$theme->get('Version')."\n";
1392 $info .= "Installed: ".$theme->get_template()."\n";
1393 $info .= "AviaFramework Version: ".AV_FRAMEWORK_VERSION."\n";
1394
1395
1396 if( class_exists( 'AviaBuilder' ) )
1397 {
1398 $info .= "AviaBuilder Version: ".AviaBuilder::VERSION."\n";
1399
1400 if( class_exists( 'aviaElementManager' ) )
1401 {
1402 $info .= "aviaElementManager Version: " . aviaElementManager::VERSION . "\n";
1403 $update_state = get_option( 'av_alb_element_mgr_update', '' );
1404 if( '' != $update_state )
1405 {
1406 $info .= "aviaElementManager update state: in update \n";
1407 }
1408 }
1409 }
1410
1411
1412 $info .= $child;
1413
1414 //memory setting, peak usage and number of active plugins
1415 $info .= "ML:".trim( @ini_get("memory_limit") ,"M")."-PU:". ( ceil (memory_get_peak_usage() / 1000 / 1000 ) ) ."-PLA:".avia_count_active_plugins()."\n";
1416 $info .= "WP:".get_bloginfo('version')."\n";
1417
1418 $comp_levels = array('none' => 'disabled', 'avia-module' => 'modules only', 'avia' => 'all theme files', 'all' => 'all files');
1419
1420 $info .= "Compress: CSS:".$comp_levels[avia_get_option('merge_css','avia-module')]." - JS:".$comp_levels[avia_get_option('merge_js','avia-module')]."\n";
1421
1422 $username = avia_get_option('updates_username');
1423 $API = avia_get_option('updates_api_key');
1424 $updates = "disabled";
1425 if($username && $API)
1426 {
1427 $updates = "enabled";
1428 if(isset($_GET['username'])) $updates = $username;
1429 }
1430
1431 $info .= "Updates: ".$updates."\n";
1432 $info = apply_filters('avf_debugging_info_add', $info);
1433 $info .= "-->";
1434 echo apply_filters('avf_debugging_info', $info);
1435 }
1436
1437 add_action('wp_head','avia_debugging_info',9999999);
1438 add_action('admin_print_scripts','avia_debugging_info',9999999);
1439}
1440
1441
1442
1443
1444
1445
1446if(!function_exists('avia_count_active_plugins'))
1447{
1448 function avia_count_active_plugins()
1449 {
1450 $plugins = count(get_option('active_plugins', array()));
1451
1452 if(is_multisite() && function_exists('get_site_option'))
1453 {
1454 $plugins += count(get_site_option('active_sitewide_plugins', array()));
1455 }
1456
1457 return $plugins;
1458 }
1459}
1460
1461
1462
1463
1464
1465
1466if(!function_exists('avia_clean_string'))
1467{
1468 function avia_clean_string($string)
1469 {
1470 $string = str_replace(' ', '_', $string); // Replaces all spaces with underscores.
1471 $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
1472
1473 return preg_replace('/-+/', '-', strtolower ($string)); // Replaces multiple hyphens with single one.
1474 }
1475}
1476
1477
1478if(!function_exists('kriesi_backlink'))
1479{
1480 function kriesi_backlink($frontpage_only = false, $theme_name_passed = false)
1481 {
1482 $no = "";
1483 $theme_string = "";
1484 $theme_name = $theme_name_passed ? $theme_name_passed : THEMENAME;
1485
1486 $random_number = get_option(THEMENAMECLEAN."_fixed_random");
1487 if($random_number % 3 == 0) $theme_string = $theme_name." Theme by Kriesi";
1488 if($random_number % 3 == 1) $theme_string = $theme_name." WordPress Theme by Kriesi";
1489 if($random_number % 3 == 2) $theme_string = "powered by ".$theme_name." WordPress Theme";
1490 if(!empty($frontpage_only) && !is_front_page()) $no = "rel='nofollow'";
1491
1492 $link = " - <a {$no} href='https://kriesi.at'>{$theme_string}</a>";
1493
1494 $link = apply_filters("kriesi_backlink", $link);
1495 return $link;
1496 }
1497}
1498
1499
1500
1501if(!function_exists('avia_header_class_filter'))
1502{
1503 function avia_header_class_filter( $default = "" )
1504 {
1505 $default = apply_filters( "avia_header_class_filter", $default );
1506 return $default;
1507 }
1508}
1509
1510
1511if(!function_exists('avia_theme_version_higher_than'))
1512{
1513 function avia_theme_version_higher_than( $check_for_version = "")
1514 {
1515 $theme = wp_get_theme( 'enfold' );
1516 $theme_version = $theme->get( 'Version' );
1517
1518 if (version_compare($theme_version, $check_for_version , '>=')) {
1519 return true;
1520 }
1521
1522 return false;
1523 }
1524}
1525
1526if( ! function_exists( 'avia_enqueue_style_conditionally' ) )
1527{
1528 /**
1529 * Enque a css file, based on theme options or other conditions that get passed and must be evaluated as true
1530 *
1531 * params are the same as in enque style, only the condition is first: https://core.trac.wordpress.org/browser/tags/4.9/src/wp-includes/functions.wp-styles.php#L164
1532 * @since 4.3
1533 * @added_by Kriesi
1534 * @param array $condition
1535 * @return array
1536 */
1537 function avia_enqueue_style_conditionally( $condition = false, $handle, $src = '', $deps = array(), $ver = false, $media = 'all', $deregister = true)
1538 {
1539 if($condition == false )
1540 {
1541 if($deregister) wp_deregister_style( $handle );
1542 return;
1543 };
1544
1545 wp_enqueue_style( $handle, $src, $deps, $ver, $media );
1546 }
1547}
1548
1549if( ! function_exists( 'avia_enqueue_script_conditionally' ) )
1550{
1551 /**
1552 * Enque a js file, based on theme options or other conditions that get passed and must be evaluated as true
1553 *
1554 * params are the same as in enque style, only the condition is first: https://core.trac.wordpress.org/browser/tags/4.9/src/wp-includes/functions.wp-scripts.php#L264
1555 * @since 4.3
1556 * @added_by Kriesi
1557 * @param array $condition
1558 * @return array
1559 */
1560 function avia_enqueue_script_conditionally( $condition = false, $handle, $src = '', $deps = array(), $ver = false, $in_footer = false, $deregister = true)
1561 {
1562 if($condition == false )
1563 {
1564 if($deregister) wp_deregister_script( $handle );
1565 return;
1566 };
1567
1568 wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
1569 }
1570}
1571
1572if( ! function_exists( 'avia_disable_query_migrate' ) )
1573{
1574 /**
1575 * Makes sure that jquery no longer depends on jquery migrate.
1576 *
1577 * @since 4.3
1578 * @added_by Kriesi
1579 * @param array $condition
1580 * @return array
1581 */
1582 function avia_disable_query_migrate()
1583 {
1584 global $wp_scripts;
1585
1586 if(!is_admin())
1587 {
1588 if(isset($wp_scripts->registered['jquery']))
1589 {
1590 foreach($wp_scripts->registered['jquery']->deps as $key => $dep)
1591 {
1592 if($dep == "jquery-migrate")
1593 {
1594 unset($wp_scripts->registered['jquery']->deps[$key]);
1595 }
1596 }
1597 }
1598 }
1599
1600 }
1601}
1602
1603if( ! function_exists( 'avia_get_submenu_count' ) )
1604{
1605 /**
1606 * Counts the number of submenu items of a menu
1607 *
1608 * @since 4.3
1609 * @added_by Kriesi
1610 * @param array $location
1611 * @return int $count
1612 */
1613 function avia_get_submenu_count( $location )
1614 {
1615 $menus = get_nav_menu_locations();
1616 $count = 0;
1617
1618 if(!isset($menus[$location])) return $count;
1619
1620 $items = wp_get_nav_menu_items($menus[$location]);
1621
1622 //if no menu is set we dont know if the fallback menu will generate submenu items so we assume thats true
1623 if(!$items) return 1;
1624
1625 foreach($items as $item)
1626 {
1627 if(isset($item->menu_item_parent) && $item->menu_item_parent >0 ) $count++;
1628 }
1629
1630 return $count;
1631 }
1632}
1633
1634if( ! function_exists( 'avia_get_active_widget_count' ) )
1635{
1636 /**
1637 * Counts the number of active widget areas (widget areas that got a widget inside them are considered active)
1638 *
1639 * @since 4.3
1640 * @added_by Kriesi
1641 * @return int $count
1642 */
1643 function avia_get_active_widget_count()
1644 {
1645 global $_wp_sidebars_widgets;
1646 $count = 0;
1647
1648 foreach($_wp_sidebars_widgets as $widget_area => $widgets)
1649 {
1650 if($widget_area == "wp_inactive_widgets" || $widget_area == "array_version") continue;
1651 if(!empty($widgets)) $count++;
1652 }
1653
1654 return $count;
1655 }
1656}
1657
1658if( ! function_exists( 'avia_get_parent_theme_version' ) )
1659{
1660 /**
1661 * Helper function that returns the (parent) theme version number to be added to scipts and css links
1662 *
1663 * @since 4.3.2
1664 * @added_by Günter
1665 * @return string
1666 */
1667 function avia_get_theme_version( $which = 'parent' )
1668 {
1669 $theme = wp_get_theme();
1670 if( false !== $theme->parent() && ( 'parent' == $which ) )
1671 {
1672 $theme = $theme->parent();
1673 }
1674 $vn = $theme->get( 'Version' );
1675
1676 return $vn;
1677 }
1678}