· 5 years ago · Nov 26, 2020, 03:40 PM
1<?php if ( ! defined('AVIA_FW')) exit('No direct script access allowed');
2/**
3 * This file holds several widgets exclusive to the framework
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
14if ( ! class_exists( 'Avia_Widget' ) )
15{
16 abstract class Avia_Widget extends WP_Widget
17 {
18
19 /**
20 *
21 * @since 4.3.2
22 * @var array
23 */
24 protected $field_names;
25
26 public function __construct( $id_base, $name, $widget_options = array(), $control_options = array() )
27 {
28 parent::__construct( $id_base, $name, $widget_options, $control_options );
29
30 $this->field_names = array();
31 }
32
33
34 /**
35 * @since 4.3.2
36 */
37 public function __destruct()
38 {
39 if( method_exists( $this, 'parent::__destruct' ) )
40 {
41 parent::__destruct();
42 }
43
44 unset( $this->field_names );
45 }
46
47 /**
48 * Returns an array that contains all default instance members filled with default values
49 *
50 * @since 4.3.2
51 * @param array $instance
52 * @return array
53 */
54 abstract protected function parse_args_instance( array $instance );
55
56
57 /**
58 * Returns an array of all default fields
59 *
60 * @since 4.3.2
61 * @return array
62 */
63 protected function get_field_names()
64 {
65 if( empty( $this->field_names ) )
66 {
67 $fields = $this->parse_args_instance( array() );
68 $this->field_names = array_keys( $fields );
69 }
70
71 return $this->field_names;
72 }
73
74 /**
75 * Iterates over form elements (uses structure as in admin options page)
76 *
77 * @since 4.7.3.1
78 * @param array $elements
79 * @return string
80 */
81 protected function render_form_elements( array $elements )
82 {
83 $html = new avia_htmlhelper();
84 $output = '';
85
86 foreach( $elements as $element )
87 {
88 $output .= $html->render_single_element( $element );
89 }
90
91 return $output;
92 }
93
94
95 /**
96 * Output the <option> tag for a series of numbers and set the selected attribute
97 *
98 * @since 4.3.2
99 * @added_by günter
100 * @param int $start
101 * @param int $end
102 * @param string $selected
103 */
104 static public function number_options( $start = 1, $end = 50, $selected = 1 )
105 {
106 $options = array();
107
108 for( $i = $start; $i <= $end; $i++ )
109 {
110 $options[ $i ] = $i;
111 }
112
113 return Avia_Widget::options_from_array( $options, $selected );
114 }
115
116 /**
117 * Output the <option> tag for a key - value array and set the selected attribute
118 *
119 * @since 4.3.2
120 * @added_by günter
121 * @param array $options
122 * @param type $selected
123 * @return string
124 */
125 static public function options_from_array( array $options, $selected )
126 {
127 $out = '';
128
129 foreach( $options as $key => $value )
130 {
131 $out .= '<option value="' . $key . '" ' . selected( $key, $selected ) . '>' . esc_html( $value ) . '</option>';
132 }
133 return $out;
134 }
135
136 }
137
138}
139
140
141
142
143/**
144 * AVIA FACEBOOK WIDGET
145 */
146
147if ( ! class_exists( 'avia_fb_likebox' ) )
148{
149 class avia_fb_likebox extends Avia_Widget
150 {
151 const AJAX_NONCE = 'avia_fb_likebox_nonce';
152 const FB_SCRIPT_ID = 'facebook-jssdk';
153
154 /**
155 *
156 * @var int
157 */
158 static protected $script_loaded = 0;
159
160
161
162
163
164 /**
165 *
166 */
167 public function __construct()
168 {
169 //Constructor
170 $widget_ops = array(
171 'classname' => 'avia_fb_likebox',
172 'description' => __( 'A widget that displays a facebook Likebox to a facebook page of your choice', 'avia_framework' )
173 );
174
175 parent::__construct( 'avia_fb_likebox', THEMENAME.' Facebook Likebox', $widget_ops );
176
177 add_action( 'init', array( $this, 'handler_wp_register_scripts' ), 500 );
178 add_action( 'wp_enqueue_scripts', array( $this, 'handler_wp_enqueue_scripts' ), 500 );
179
180 }
181
182
183 /**
184 * @since 4.3.2
185 */
186 public function __destruct()
187 {
188 parent::__destruct();
189 }
190
191 /**
192 *
193 * @since 4.3.2
194 */
195 public function handler_wp_register_scripts()
196 {
197 $vn = avia_get_theme_version();
198
199 wp_register_script( 'avia_facebook_front_script' , AVIA_JS_URL . 'conditional_load/avia_facebook_front.js', array( 'jquery' ), $vn, true );
200 }
201
202 /**
203 * @since 4.3.2
204 */
205 public function handler_wp_enqueue_scripts()
206 {
207 $instances = $this->get_settings();
208 if( count( $instances ) > 0 )
209 {
210 $need_js = array( 'confirm_link' );
211
212 foreach( $instances as $instance )
213 {
214 if( isset( $instance['fb_link'] ) && in_array( $instance['fb_link'], $need_js ) )
215 {
216 wp_enqueue_script( 'avia_facebook_front_script' );
217 break;
218 }
219 }
220 }
221 }
222
223 /**
224 *
225 * @since 4.3.2
226 * @param array $instance
227 * @return array
228 */
229 protected function parse_args_instance( array $instance )
230 {
231 $new_instance = wp_parse_args( $instance, array(
232 'url' => 'https://www.facebook.com/kriesi.at',
233 'title' => __( 'Follow us on Facebook', 'avia_framework' ),
234 'fb_link' => '',
235 'fb_banner' => '',
236 'page_title' => '',
237 'fb_logo' => '',
238 'content' => '',
239 'add_info' => __( 'Join our Facebook community', 'avia_framework' ),
240 'confirm_button' => __( 'Click to load facebook widget', 'avia_framework' ),
241 'page_link_text' => __( 'Open facebook page now', 'avia_framework' )
242 ) );
243
244 return $new_instance;
245 }
246
247
248 /**
249 * Outputs the widget
250 *
251 * @param array $args
252 * @param array $instance
253 */
254 public function widget( $args, $instance )
255 {
256 $instance = $this->parse_args_instance( $instance );
257
258 extract( $args, EXTR_SKIP );
259 extract( $instance, EXTR_SKIP );
260
261 if( empty( $url ) )
262 {
263 return;
264 }
265
266 /**
267 * Allow to change the conditional display setting - e.g. if user is opt in and allows to connect directly
268 *
269 * @since 4.4
270 * @param string $google_link '' | 'confirm_link' | 'page_only'
271 * @param string $context
272 * @param mixed $object
273 * @param array $args
274 * @param array $instance
275 * @return string
276 */
277
278 $original_fb_link = $fb_link;
279 $fb_link = apply_filters( 'avf_conditional_setting_external_links', $fb_link, __CLASS__, $this, $args, $instance );
280 if( ! in_array( $fb_link, array( '', 'confirm_link', 'page_only' ) ) )
281 {
282 $fb_link = $original_fb_link;
283 }
284
285 $title = apply_filters( 'widget_title', $title );
286
287 echo $before_widget;
288
289 if ( ! empty( $title ) )
290 {
291 echo $before_title . $title . $after_title;
292 };
293
294 $banner_bg = "";
295
296 if( ! empty( $fb_link ) )
297 {
298 if( ! empty( $fb_banner ) && ! empty( $fb_link ) )
299 {
300 $banner_bg = 'style="background-image:url(' . $fb_banner . ');"';
301 }
302
303 $link_title = avia_targeted_link_rel( '<a href="' . $url . '" target="_blank" title="' . esc_html( $page_title ) . '">' . esc_html( $page_title ) . '</a>' );
304
305 echo '<div class="av_facebook_widget_main_wrap" ' . $banner_bg . '>';
306
307 echo '<div class="av_facebook_widget_page_title_container">';
308 echo '<span class="av_facebook_widget_title">';
309 echo $link_title;
310 echo '</span>';
311 echo '<span class="av_facebook_widget_content">';
312 echo esc_html( $content );
313 echo '</span>';
314 echo '</div>';
315
316
317 $html_logo = '';
318
319 if( ! empty( $fb_logo ) )
320 {
321 $html_logo .= '<div class="av_facebook_widget_logo_image">';
322 $html_logo .= '<img src="' . $fb_logo . '" alt="' . __( 'Logo image', 'avia_framework' ) . '">';
323 $html_logo .= '</div>';
324 }
325
326 echo '<div class="av_facebook_widget_main_wrap_shadow"></div>';
327 echo '<div class="av_facebook_widget_logo av_widget_img_text_confirm">';
328
329 echo $html_logo;
330
331 echo '</div>';
332
333 $data = "";
334 if( 'confirm_link' == $fb_link )
335 {
336 $data = ' data-fbhtml="' . htmlentities( $this->html_facebook_page( $url ), ENT_QUOTES, get_bloginfo( 'charset' ) ) . '"';
337 $data .= ' data-fbscript="' . htmlentities( $this->get_fb_page_js_src(), ENT_QUOTES, get_bloginfo( 'charset' ) ) . '"';
338 $data .= ' data-fbscript_id="' . avia_fb_likebox::FB_SCRIPT_ID . '"';
339 }
340
341 $btn_text = ( 'confirm_link' == $fb_link ) ? $confirm_button : $page_link_text;
342 $icon = "<span class='av_facebook_widget_icon' " . av_icon_string('facebook') . "></span>";
343 echo avia_targeted_link_rel( '<a href="' . $url . '" target="_blank" class="av_facebook_widget_button av_facebook_widget_' . $fb_link . '"' . $data . '>' .$icon . esc_html( $btn_text ) . '</a>' );
344
345 if( ! empty( $fb_link ) )
346 {
347 echo '<div class="av_facebook_widget_add_info">';
348 echo '<div class="av_facebook_widget_add_info_inner">';
349 echo '<span class="av_facebook_widget_add_info_inner_wrap">';
350 echo esc_html( $add_info );
351 echo '</span>';
352 echo '<div class="av_facebook_widget_imagebar">';
353 echo '</div>';
354 echo '</div>';
355 echo '</div>';
356 }
357
358 echo '</div>'; // class="av_facebook_widget_main_wrap"
359 }
360
361 if( empty( $fb_link ) )
362 {
363 echo $this->html_facebook_page( $url );
364 add_action( 'wp_footer', array( $this,'handler_output_fb_page_script' ), 10 );
365 }
366
367 echo $after_widget;
368 }
369
370 /**
371 * Create the HTML for the facebook page widget
372 *
373 * @since 4.3.2
374 * @param string $url
375 * @return string
376 */
377 protected function html_facebook_page( $url )
378 {
379 $extraClass = '';
380 $style = '';
381
382// $height = 151; // remainings from original widget ?????
383// $faces = "true";
384// $extraClass = "";
385// $style = "";
386//
387//
388// if( strpos( $height, "%" ) !== false )
389// {
390// $extraClass = "av_facebook_widget_wrap_positioner";
391// $style = "style='padding-bottom:{$height}%'";
392// $height = "100%";
393// }
394
395 $html = '';
396 $html .= "<div class='av_facebook_widget_wrap {$extraClass}' {$style}>";
397 $html .= '<div class="fb-page" data-width="500" data-href="' . $url . '" data-small-header="false" data-adapt-container-width="true" data-hide-cover="false" data-show-facepile="true" data-show-posts="false">';
398 $html .= '<div class="fb-xfbml-parse-ignore"></div>';
399 $html .= '</div>';
400 $html .= "</div>";
401
402 return $html;
403 }
404
405 /**
406 *
407 * @since 4.3.2
408 */
409 public function handler_output_fb_page_script()
410 {
411 if( self::$script_loaded >= 1 )
412 {
413 return;
414 }
415
416 self::$script_loaded = 1;
417
418 $script = '
419<script>(function(d, s, id) {
420 var js, fjs = d.getElementsByTagName(s)[0];
421 if (d.getElementById(id)) return;
422 js = d.createElement(s); js.id = id;
423 js.src = "' . $this->get_fb_page_js_src() . '";
424 fjs.parentNode.insertBefore(js, fjs);
425}(document, "script", "' . avia_fb_likebox::FB_SCRIPT_ID . '"));</script>';
426
427 echo $script;
428 }
429
430
431 /**
432 * Return the js function
433 * @since 4.3.2
434 * @return string
435 */
436 protected function get_fb_page_js_src()
437 {
438 $langcode = get_locale();
439
440 /**
441 * Change language code for facebook page widget
442 *
443 * @used_by enfold\config-wpml\config.php 10
444 * @since 4.3.2
445 */
446 $langcode = apply_filters( 'avf_fb_widget_lang_code', $langcode, 'fb-page' );
447
448 $src = '//connect.facebook.net/'. $langcode .'/sdk.js#xfbml=1&version=v2.7';
449
450 return $src;
451 }
452
453
454 /**
455 *
456 * @param array $new_instance
457 * @param array $old_instance
458 * @return array
459 */
460 public function update( $new_instance, $old_instance )
461 {
462 $instance = $this->parse_args_instance( $old_instance );
463 $fields = $this->get_field_names();
464
465 foreach( $new_instance as $key => $value )
466 {
467 if( in_array( $key, $fields ) )
468 {
469 $instance[ $key ] = strip_tags( $value );
470 }
471 }
472
473 return $instance;
474 }
475
476
477 /**
478 * Outputs Widgetform in backend
479 *
480 * @param array $instance
481 */
482 public function form( $instance )
483 {
484 $instance = $this->parse_args_instance( $instance );
485 $fields = $this->get_field_names();
486
487 foreach( $instance as $key => $value )
488 {
489 if( in_array( $key, $fields ) )
490 {
491 $instance[ $key ] = esc_attr( $value );
492 }
493 }
494
495 extract( $instance );
496
497 $html = new avia_htmlhelper();
498
499 $banner_element = array(
500 'name' => __( 'Banner image', 'avia_framework' ),
501 'desc' => __( 'Upload a banner image or enter the URL', 'avia_framework' ),
502 'id' => $this->get_field_id( 'fb_banner'),
503 'id_name' => $this->get_field_name( 'fb_banner' ),
504 'std' => $fb_banner,
505 'type' => 'upload',
506 'label' => __('Use image as banner', 'avia_framework')
507 );
508
509 $logo_element = array(
510 'name' => __( 'Logo', 'avia_framework' ),
511 'desc' => __( 'Upload a logo or enter the URL', 'avia_framework' ),
512 'id' => $this->get_field_id( 'fb_logo'),
513 'id_name' => $this->get_field_name( 'fb_logo' ),
514 'std' => $fb_logo,
515 'type' => 'upload',
516 'label' => __('Use image as logo', 'avia_framework')
517 );
518
519 ?>
520 <div class="avia_widget_form avia_widget_conditional_form avia_fb_likebox_form <?php echo $fb_link;?>">
521 <p>
522 <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'avia_framework'); ?>
523 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label>
524 </p>
525
526 <p>
527 <label for="<?php echo $this->get_field_id('url'); ?>"><?php _e('Enter the url to the Page. Please note that it needs to be a link to a <strong>facebook fanpage</strong>. Personal profiles are not allowed!', 'avia_framework'); ?>
528 <input class="widefat" id="<?php echo $this->get_field_id('url'); ?>" name="<?php echo $this->get_field_name('url'); ?>" type="text" value="<?php echo $url; ?>" /></label>
529 </p>
530
531 <p>
532 <label for="<?php echo $this->get_field_id( 'fb_link' ); ?>"><?php _e( 'Link to facebook', 'avia_framework' ); ?>:</label>
533 <select id="<?php echo $this->get_field_id( 'fb_link' ); ?>" name="<?php echo $this->get_field_name( 'fb_link' ); ?>" class="widefat avia-coditional-widget-select">
534 <option value="" <?php selected( '', $fb_link ) ?>><?php _e( 'Show facebook page widget "Share/Like" directly', 'avia_framework' ); ?></option>
535 <option value="confirm_link" <?php selected( 'confirm_link', $fb_link ) ?>><?php _e( 'User must accept to show facebook page widget "Share/Like"', 'avia_framework' ); ?></option>
536 <option value="page_only" <?php selected( 'page_only', $fb_link ) ?>><?php _e( 'Only open the facebook page - no data are sent', 'avia_framework' ); ?></option>
537 </select>
538 </p>
539
540 <p class="av-confirm_link">
541 <label for="<?php echo $this->get_field_id('confirm_button'); ?>"><?php _e('Button text confirm link to facebook:', 'avia_framework'); ?>
542 <input class="widefat" id="<?php echo $this->get_field_id('confirm_button'); ?>" name="<?php echo $this->get_field_name('confirm_button'); ?>" type="text" value="<?php echo $confirm_button; ?>" /></label>
543 </p>
544
545 <p class="av-page_only">
546 <label for="<?php echo $this->get_field_id('page_link_text'); ?>"><?php _e('Direct link to FB-page text:', 'avia_framework'); ?>
547 <input class="widefat" id="<?php echo $this->get_field_id('page_link_text'); ?>" name="<?php echo $this->get_field_name('page_link_text'); ?>" type="text" value="<?php echo $page_link_text; ?>" /></label>
548 </p>
549
550 <div class="avia_fb_likebox_upload avia-fb-banner av-widgets-upload">
551 <?php echo $html->render_single_element( $banner_element );?>
552 </div>
553
554 <p class="av-page-title">
555 <label for="<?php echo $this->get_field_id('page_title'); ?>"><?php _e('Facebook Page Title:', 'avia_framework'); ?>
556 <input class="widefat" id="<?php echo $this->get_field_id('page_title'); ?>" name="<?php echo $this->get_field_name('page_title'); ?>" type="text" value="<?php echo $page_title; ?>" placeholder="<?php _e('Enter some info to the page', 'avia_framework'); ?>" /></label>
557 </p>
558
559 <div class="avia_fb_likebox_upload avia-fb-logo av-widgets-upload">
560 <?php echo $html->render_single_element( $logo_element );?>
561 </div>
562
563 <p class="av-content">
564 <label for="<?php echo $this->get_field_id('content'); ?>"><?php _e('Static like count:', 'avia_framework'); ?>
565 <input class="widefat" id="<?php echo $this->get_field_id('content'); ?>" name="<?php echo $this->get_field_name('content'); ?>" rows="5" placeholder="<?php _e('2k+ likes', 'avia_framework'); ?>" value='<?php echo $content; ?>' />
566 </label>
567 </p>
568
569 <p class="av-add_info">
570 <label for="<?php echo $this->get_field_id('add_info'); ?>"><?php _e('Additional Information:', 'avia_framework'); ?>
571 <input class="widefat" id="<?php echo $this->get_field_id('add_info'); ?>" name="<?php echo $this->get_field_name('add_info'); ?>" rows="5" placeholder="<?php _e('Info displayed above the fake user profiles', 'avia_framework'); ?>" value='<?php echo $add_info; ?>' />
572 </label>
573 </p>
574 </div>
575 <?php
576
577 }
578 }
579}
580
581
582
583
584
585
586
587
588
589
590/**
591 * AVIA TWEETBOX
592 *
593 * Widget that creates a list of latest tweets
594 *
595 * @package AviaFramework
596 * @todo replace the widget system with a dynamic one, based on config files for easier widget creation
597 */
598
599
600
601/*
602Twitter widget only for compatibility reasons with older themes present. no onger used since API will be shut down by twitter
603*/
604if (!class_exists('avia_tweetbox'))
605{
606 class avia_tweetbox extends WP_Widget {
607
608 function __construct() {
609 //Constructor
610 $widget_ops = array('classname' => 'tweetbox', 'description' => 'A widget to display your latest twitter messages' );
611 parent::__construct( 'tweetbox', THEMENAME.' Twitter Widget', $widget_ops );
612 }
613
614 function widget($args, $instance) {
615 // prints the widget
616
617 extract($args, EXTR_SKIP);
618 echo $before_widget;
619
620 $title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
621 $count = empty($instance['count']) ? '' : $instance['count'];
622 $username = empty($instance['username']) ? '' : $instance['username'];
623 $exclude_replies = empty($instance['exclude_replies']) ? '' : $instance['exclude_replies'];
624 $time = empty($instance['time']) ? 'no' : $instance['time'];
625 $display_image = empty($instance['display_image']) ? 'no' : $instance['display_image'];
626
627 if ( !empty( $title ) ) { echo $before_title . "<a href='http://twitter.com/$username/' title='".strip_tags($title)."'>".$title ."</a>". $after_title; };
628
629 $messages = tweetbox_get_tweet($count, $username, $widget_id, $time, $exclude_replies, $display_image);
630 echo $messages;
631
632 echo $after_widget;
633
634
635 }
636
637 function update($new_instance, $old_instance) {
638 //save the widget
639 $instance = $old_instance;
640 foreach($new_instance as $key=>$value)
641 {
642 $instance[$key] = strip_tags($new_instance[$key]);
643 }
644
645 delete_transient(THEMENAME.'_tweetcache_id_'.$instance['username'].'_'.$this->id_base."-".$this->number);
646 return $instance;
647 }
648
649 function form($instance) {
650 //widgetform in backend
651
652 $instance = wp_parse_args( (array) $instance, array( 'title' => 'Latest Tweets', 'count' => '3', 'username' => avia_get_option('twitter') ) );
653 $title = isset($instance['title']) ? strip_tags($instance['title']): "";
654 $count = isset($instance['count']) ? strip_tags($instance['count']): "";
655 $username = isset($instance['username']) ? strip_tags($instance['username']): "";
656 $exclude_replies = isset($instance['exclude_replies']) ? strip_tags($instance['exclude_replies']): "";
657 $time = isset($instance['time']) ? strip_tags($instance['time']): "";
658 $display_image = isset($instance['display_image']) ? strip_tags($instance['display_image']): "";
659 ?>
660 <p>
661 <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'avia_framework'); ?>
662 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
663
664 <p><label for="<?php echo $this->get_field_id('username'); ?>">Enter your twitter username:
665 <input class="widefat" id="<?php echo $this->get_field_id('username'); ?>" name="<?php echo $this->get_field_name('username'); ?>" type="text" value="<?php echo esc_attr($username); ?>" /></label></p>
666
667 <p>
668 <label for="<?php echo $this->get_field_id('count'); ?>">How many entries do you want to display: </label>
669 <select class="widefat" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>">
670 <?php
671 $list = "";
672 for ($i = 1; $i <= 20; $i++ )
673 {
674 $selected = "";
675 if($count == $i) $selected = 'selected="selected"';
676
677 $list .= "<option $selected value='$i'>$i</option>";
678 }
679 $list .= "</select>";
680 echo $list;
681 ?>
682
683
684 </p>
685
686 <p>
687 <label for="<?php echo $this->get_field_id('exclude_replies'); ?>">Exclude @replies: </label>
688 <select class="widefat" id="<?php echo $this->get_field_id('exclude_replies'); ?>" name="<?php echo $this->get_field_name('exclude_replies'); ?>">
689 <?php
690 $list = "";
691 $answers = array('yes','no');
692 foreach ($answers as $answer)
693 {
694 $selected = "";
695 if($answer == $exclude_replies) $selected = 'selected="selected"';
696
697 $list .= "<option $selected value='$answer'>$answer</option>";
698 }
699 $list .= "</select>";
700 echo $list;
701 ?>
702
703
704 </p>
705
706 <p>
707 <label for="<?php echo $this->get_field_id('time'); ?>">Display time of tweet</label>
708 <select class="widefat" id="<?php echo $this->get_field_id('time'); ?>" name="<?php echo $this->get_field_name('time'); ?>">
709 <?php
710 $list = "";
711 $answers = array('yes','no');
712 foreach ($answers as $answer)
713 {
714 $selected = "";
715 if($answer == $time) $selected = 'selected="selected"';
716
717 $list .= "<option $selected value='$answer'>$answer</option>";
718 }
719 $list .= "</select>";
720 echo $list;
721 ?>
722
723
724 </p>
725
726 <p>
727 <label for="<?php echo $this->get_field_id('display_image'); ?>">Display Twitter User Avatar</label>
728 <select class="widefat" id="<?php echo $this->get_field_id('display_image'); ?>" name="<?php echo $this->get_field_name('display_image'); ?>">
729 <?php
730 $list = "";
731 $answers = array('yes','no');
732 foreach ($answers as $answer)
733 {
734 $selected = "";
735 if($answer == $display_image) $selected = 'selected="selected"';
736
737 $list .= "<option $selected value='$answer'>$answer</option>";
738 }
739 $list .= "</select>";
740 echo $list;
741 ?>
742 </p>
743
744
745
746 <?php
747 }
748 }
749}
750
751if(!function_exists('tweetbox_get_tweet'))
752{
753 function tweetbox_get_tweet($count, $username, $widget_id, $time='yes', $exclude_replies='yes', $avatar = 'yes')
754 {
755 $filtered_message = "";
756 $output = "";
757 $iterations = 0;
758
759 $cache = get_transient(THEMENAME.'_tweetcache_id_'.$username.'_'.$widget_id);
760
761 if($cache)
762 {
763 $tweets = get_option(THEMENAME.'_tweetcache_'.$username.'_'.$widget_id);
764 }
765 else
766 {
767 //$response = wp_remote_get( 'http://api.twitter.com/1/statuses/user_timeline.xml?screen_name='.$username );
768 $response = wp_remote_get( 'http://api.twitter.com/1/statuses/user_timeline.xml?include_rts=true&screen_name='.$username );
769 if (!is_wp_error($response))
770 {
771 $xml = @simplexml_load_string($response['body']);
772 //follower: (int) $xml->status->user->followers_count
773
774 if( empty( $xml->error ) )
775 {
776 if ( isset($xml->status[0]))
777 {
778
779 $tweets = array();
780 foreach ($xml->status as $tweet)
781 {
782 if($iterations == $count) break;
783
784 $text = (string) $tweet->text;
785 if($exclude_replies == 'no' || ($exclude_replies == 'yes' && $text[0] != "@"))
786 {
787 $iterations++;
788 $tweets[] = array(
789 'text' => tweetbox_filter( $text ),
790 'created' => strtotime( $tweet->created_at ),
791 'user' => array(
792 'name' => (string)$tweet->user->name,
793 'screen_name' => (string)$tweet->user->screen_name,
794 'image' => (string)$tweet->user->profile_image_url,
795 'utc_offset' => (int) $tweet->user->utc_offset[0],
796 'follower' => (int) $tweet->user->followers_count
797
798 ));
799 }
800 }
801
802 set_transient(THEMENAME.'_tweetcache_id_'.$username.'_'.$widget_id, 'true', 60*30);
803 update_option(THEMENAME.'_tweetcache_'.$username.'_'.$widget_id, $tweets);
804 }
805 }
806 }
807 }
808
809
810
811 if(!isset($tweets[0]))
812 {
813 $tweets = get_option(THEMENAME.'_tweetcache_'.$username.'_'.$widget_id);
814 }
815
816 if(isset($tweets[0]))
817 {
818 $time_format = apply_filters( 'avia_widget_time', get_option('date_format')." - ".get_option('time_format'), 'tweetbox' );
819
820 foreach ($tweets as $message)
821 {
822 $output .= '<li class="tweet">';
823 if($avatar == "yes") $output .= '<div class="tweet-thumb"><a href="http://twitter.com/'.$username.'" title=""><img src="'.$message['user']['image'].'" alt="" /></a></div>';
824 $output .= '<div class="tweet-text avatar_'.$avatar.'">'.$message['text'];
825 if($time == "yes") $output .= '<div class="tweet-time">'.date_i18n( $time_format, $message['created'] + $message['user']['utc_offset']).'</div>';
826 $output .= '</div></li>';
827 }
828 }
829
830
831 if($output != "")
832 {
833 $filtered_message = "<ul class='tweets'>$output</ul>";
834 }
835 else
836 {
837 $filtered_message = "<ul class='tweets'><li>No public Tweets found</li></ul>";
838 }
839
840 return $filtered_message;
841 }
842}
843
844if(!function_exists('tweetbox_filter'))
845{
846 function tweetbox_filter($text) {
847 // Props to Allen Shaw & webmancers.com & Michael Voigt
848 $text = preg_replace('/\b([a-zA-Z]+:\/\/[\w_.\-]+\.[a-zA-Z]{2,6}[\/\w\-~.?=&%#+$*!]*)\b/i',"<a href=\"$1\" class=\"twitter-link\">$1</a>", $text);
849 $text = preg_replace('/\b(?<!:\/\/)(www\.[\w_.\-]+\.[a-zA-Z]{2,6}[\/\w\-~.?=&%#+$*!]*)\b/i',"<a href=\"http://$1\" class=\"twitter-link\">$1</a>", $text);
850 $text = preg_replace("/\b([a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]*\@[a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]{2,6})\b/i","<a href=\"mailto://$1\" class=\"twitter-link\">$1</a>", $text);
851 $text = preg_replace("/#([\p{L}\p{Mn}]+)/u", "<a class=\"twitter-link\" href=\"http://search.twitter.com/search?q=\\1\">#\\1</a>", $text);
852 $text = preg_replace("/@([\p{L}\p{Mn}]+)/u", "<a class=\"twitter-link\" href=\"http://twitter.com/\\1\">@\\1</a>", $text);
853
854 return $text;
855 }
856}
857
858
859
860
861
862
863
864
865
866/**
867 * AVIA NEWSBOX
868 *
869 * Widget that creates a list of latest news entries
870 *
871 * @package AviaFramework
872 * @todo replace the widget system with a dynamic one, based on config files for easier widget creation
873 */
874
875if (!class_exists('avia_newsbox'))
876{
877 class avia_newsbox extends WP_Widget {
878
879 var $avia_term = '';
880 var $avia_post_type = '';
881 var $avia_new_query = '';
882
883 function __construct()
884 {
885 $widget_ops = array('classname' => 'newsbox', 'description' => __('A Sidebar widget to display latest post entries in your sidebar', 'avia_framework') );
886
887 parent::__construct( 'newsbox', THEMENAME.' Latest News', $widget_ops );
888 }
889
890 function widget($args, $instance)
891 {
892 global $avia_config;
893
894 extract($args, EXTR_SKIP);
895 echo $before_widget;
896
897 $title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
898 $count = empty($instance['count']) ? '' : $instance['count'];
899 $cat = empty($instance['cat']) ? '' : $instance['cat'];
900 $excerpt = empty($instance['excerpt']) ? '' : $instance['excerpt'];
901 $image_size = isset($avia_config['widget_image_size']) ? $avia_config['widget_image_size'] : 'widget';
902
903 /**
904 * @since 4.5.4
905 * @return string
906 */
907 $image_size = apply_filters( 'avf_newsbox_image_size', $image_size, $args, $instance );
908
909 if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
910
911
912 if(empty($this->avia_term))
913 {
914 $additional_loop = new WP_Query("cat=".$cat."&posts_per_page=".$count);
915 }
916 else
917 {
918 $catarray = explode(',', $cat);
919
920
921 if(empty($catarray[0]))
922 {
923 $new_query = array("posts_per_page"=>$count,"post_type"=>$this->avia_post_type);
924 }
925 else
926 {
927 if($this->avia_new_query)
928 {
929 $new_query = $this->avia_new_query;
930 }
931 else
932 {
933 $new_query = array( "posts_per_page"=>$count, 'tax_query' => array(
934 array( 'taxonomy' => $this->avia_term,
935 'field' => 'id',
936 'terms' => explode(',', $cat),
937 'operator' => 'IN')
938 )
939 );
940 }
941 }
942
943 $additional_loop = new WP_Query($new_query);
944 }
945
946 if($additional_loop->have_posts()) :
947
948
949
950 echo '<ul class="news-wrap image_size_'.$image_size.'">';
951 while ($additional_loop->have_posts()) : $additional_loop->the_post();
952
953 $format = "";
954 if(empty($this->avia_post_type)) $format = $this->avia_post_type;
955 if(empty($format)) $format = get_post_format();
956 if(empty($format)) $format = 'standard';
957
958 $the_id = get_the_ID();
959 $link = get_post_meta( $the_id ,'_portfolio_custom_link', true) != "" ? get_post_meta( $the_id ,'_portfolio_custom_link_url', true) : get_permalink();
960
961
962 echo '<li class="news-content post-format-'.$format.'">';
963
964 //check for preview images:
965 $image = "";
966
967 if(!current_theme_supports('force-post-thumbnails-in-widget'))
968 {
969 $slides = avia_post_meta(get_the_ID(), 'slideshow', true);
970
971 if( $slides != "" && !empty( $slides[0]['slideshow_image'] ) )
972 {
973 $image = avia_image_by_id($slides[0]['slideshow_image'], $image_size, 'image');
974 }
975 }
976
977 if(current_theme_supports( 'post-thumbnails' ) && !$image )
978 {
979 $image = get_the_post_thumbnail( $the_id, $image_size );
980 }
981
982 $time_format = apply_filters( 'avia_widget_time', get_option('date_format')." - ".get_option('time_format'), 'avia_newsbox' );
983
984
985 echo "<a class='news-link' title='".get_the_title()."' href='".$link."'>";
986
987 $nothumb = (!$image) ? 'no-news-thumb' : '';
988
989 echo "<span class='news-thumb $nothumb'>";
990 echo $image;
991 echo "</span>";
992 if(empty($avia_config['widget_image_size']) || 'display title and excerpt' != $excerpt)
993 {
994 echo "<strong class='news-headline'>".get_the_title();
995
996 if($time_format)
997 {
998 echo "<span class='news-time'>".get_the_time($time_format)."</span>";
999 }
1000
1001 echo "</strong>";
1002 }
1003 echo "</a>";
1004
1005 if( 'display title and excerpt' == $excerpt )
1006 {
1007 echo "<div class='news-excerpt'>";
1008
1009 if(!empty($avia_config['widget_image_size']))
1010 {
1011 echo "<a class='news-link-inner' title='".get_the_title()."' href='".$link."'>";
1012 echo "<strong class='news-headline'>".get_the_title()."</strong>";
1013 echo "</a>";
1014 if($time_format)
1015 {
1016 echo "<span class='news-time'>".get_the_time($time_format)."</span>";
1017 }
1018
1019 }
1020 the_excerpt();
1021 echo "<div class='read-more-link'><a href='".$link. "' title='".get_the_title()."' class='more-link'> ". __( "Read more", "avia_framework" ) . "<span class='more-link-arrow'></span></a></div>";
1022 echo "</div>";
1023 }
1024
1025 echo '</li>';
1026
1027
1028 endwhile;
1029 echo "</ul>";
1030 wp_reset_postdata();
1031 endif;
1032
1033
1034 echo $after_widget;
1035
1036 }
1037
1038
1039 function update($new_instance, $old_instance)
1040 {
1041 $instance = $old_instance;
1042 $instance['title'] = strip_tags($new_instance['title']);
1043 $instance['count'] = strip_tags($new_instance['count']);
1044 $instance['excerpt'] = strip_tags($new_instance['excerpt']);
1045 $instance['cat'] = implode(',',$new_instance['cat']);
1046 return $instance;
1047 }
1048
1049
1050
1051 function form($instance)
1052 {
1053 $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'count' => '', 'cat' => '', 'excerpt'=>'' ) );
1054 $title = strip_tags($instance['title']);
1055 $count = strip_tags($instance['count']);
1056 $excerpt = strip_tags($instance['excerpt']);
1057
1058
1059 $elementCat = array("name" => __("Which categories should be used for the portfolio?", 'avia_framework'),
1060 "desc" => __("You can select multiple categories here", 'avia_framework'),
1061 "id" => $this->get_field_name('cat')."[]",
1062 "type" => "select",
1063 "std" => strip_tags($instance['cat']),
1064 "class" => "",
1065 "multiple"=>6,
1066 "subtype" => "cat");
1067 //check if a different taxonomy than the default is set
1068 if(!empty($this->avia_term))
1069 {
1070 $elementCat['taxonomy'] = $this->avia_term;
1071 }
1072
1073
1074
1075
1076 $html = new avia_htmlhelper();
1077
1078 ?>
1079 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'avia_framework'); ?>
1080 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
1081
1082 <p>
1083 <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('How many entries do you want to display: ', 'avia_framework'); ?></label>
1084 <select class="widefat" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>">
1085 <?php
1086 $list = "";
1087 for ($i = 1; $i <= 20; $i++ )
1088 {
1089 $selected = "";
1090 if($count == $i) $selected = 'selected="selected"';
1091
1092 $list .= "<option $selected value='$i'>$i</option>";
1093 }
1094 $list .= "</select>";
1095 echo $list;
1096 ?>
1097
1098
1099 </p>
1100
1101 <p><label for="<?php echo $this->get_field_id('cat'); ?>"><?php _e('Choose the categories you want to display (multiple selection possible):', 'avia_framework'); ?>
1102 <?php echo $html->select($elementCat); ?>
1103 </label></p>
1104
1105 <p>
1106 <label for="<?php echo $this->get_field_id('excerpt'); ?>"><?php _e('Display title only or title & excerpt', 'avia_framework'); ?></label>
1107 <select class="widefat" id="<?php echo $this->get_field_id('excerpt'); ?>" name="<?php echo $this->get_field_name('excerpt'); ?>">
1108 <?php
1109 $list = "";
1110 $answers = array(
1111 'show title only' => __( 'show title only', 'avia_framework' ),
1112 'display title and excerpt' => __('display title and excerpt', 'avia_framework')
1113 );
1114
1115 foreach ( $answers as $key => $answer )
1116 {
1117 $selected = "";
1118 if( $key == $excerpt ) $selected = 'selected="selected"';
1119
1120 $list .= "<option $selected value='$key'>$answer</option>";
1121 }
1122 $list .= "</select>";
1123 echo $list;
1124 ?>
1125
1126
1127 </p>
1128
1129
1130 <?php
1131 }
1132 }
1133}
1134
1135
1136/**
1137 * AVIA PORTFOLIOBOX
1138 *
1139 * Widget that creates a list of latest portfolio entries. Basically the same widget as the newsbox with some minor modifications, therefore it just extends the Newsbox
1140 *
1141 * @package AviaFramework
1142 * @todo replace the widget system with a dynamic one, based on config files for easier widget creation
1143 */
1144
1145if (!class_exists('avia_portfoliobox'))
1146{
1147 class avia_portfoliobox extends avia_newsbox
1148 {
1149 function __construct()
1150 {
1151 $this->avia_term = 'portfolio_entries';
1152 $this->avia_post_type = 'portfolio';
1153 $this->avia_new_query = ''; //set a custom query here
1154
1155
1156 $widget_ops = array('classname' => 'newsbox', 'description' => __('A Sidebar widget to display latest portfolio entries in your sidebar', 'avia_framework') );
1157
1158 WP_Widget::__construct( 'portfoliobox', THEMENAME.' Latest Portfolio', $widget_ops );
1159 }
1160 }
1161}
1162
1163
1164
1165/**
1166 * AVIA SOCIALCOUNT
1167 *
1168 * Widget that retrieves, stores and displays the number of twitter and rss followers
1169 *
1170 * @package AviaFramework
1171 * @todo replace the widget system with a dynamic one, based on config files for easier widget creation
1172 */
1173
1174if (!class_exists('avia_socialcount'))
1175{
1176 class avia_socialcount extends WP_Widget {
1177
1178 function __construct() {
1179 //Constructor
1180 $widget_ops = array('classname' => 'avia_socialcount', 'description' => __('A widget to display a link to your twitter profile and rss feed', 'avia_framework') );
1181 parent::__construct( 'avia_socialcount', THEMENAME.' RSS Link and Twitter Account', $widget_ops );
1182 }
1183
1184 function widget($args, $instance) {
1185 // prints the widget
1186
1187 extract($args, EXTR_SKIP);
1188 $twitter = empty($instance['twitter']) ? '' : $instance['twitter'];
1189 $rss = empty($instance['rss']) ? '' : $instance['rss'];
1190 $rss = preg_replace('!https?:\/\/feeds.feedburner.com\/!','',$rss);
1191
1192
1193 if(!empty($twitter) || !empty($rss))
1194 {
1195 $addClass = "asc_multi_count";
1196 if(!isset($twitter) || !isset($rss)) $addClass = 'asc_single_count';
1197
1198 echo $before_widget;
1199 $output = "";
1200 if(!empty($twitter))
1201 {
1202 $link = 'http://twitter.com/'.$twitter.'/';
1203 $before = apply_filters('avf_social_widget', "", 'twitter');
1204 $output .= "<a href='$link' class='asc_twitter $addClass'>{$before}<strong class='asc_count'>".__('Follow','avia_framework')."</strong><span>".__('on Twitter','avia_framework')."</span></a>";
1205
1206 }
1207
1208 if($rss)
1209 {
1210 $output .= "<a href='$rss' class='asc_rss $addClass'>".apply_filters('avf_social_widget',"", 'rss')."<strong class='asc_count'>".__('Subscribe','avia_framework')."</strong><span>".__('to RSS Feed','avia_framework')."</span></a>";
1211 }
1212
1213 echo $output;
1214 echo $after_widget;
1215 }
1216 }
1217
1218
1219
1220 function update($new_instance, $old_instance) {
1221 //save the widget
1222 $instance = $old_instance;
1223 foreach($new_instance as $key=>$value)
1224 {
1225 $instance[$key] = strip_tags($new_instance[$key]);
1226 }
1227
1228 return $instance;
1229 }
1230
1231 function form($instance) {
1232 //widgetform in backend
1233
1234 $instance = wp_parse_args( (array) $instance, array('rss' => avia_get_option('feedburner'), 'twitter' => avia_get_option('twitter') ) );
1235 $twitter = empty($instance['twitter']) ? '' : strip_tags($instance['twitter']);
1236 $rss = empty($instance['rss']) ? '' : strip_tags($instance['rss']);
1237 ?>
1238 <p>
1239 <label for="<?php echo $this->get_field_id('twitter'); ?>"><?php _e('Twitter Username:', 'avia_framework'); ?>
1240 <input class="widefat" id="<?php echo $this->get_field_id('twitter'); ?>" name="<?php echo $this->get_field_name('twitter'); ?>" type="text" value="<?php echo esc_attr($twitter); ?>" /></label></p>
1241
1242 <p><label for="<?php echo $this->get_field_id('rss'); ?>"><?php _e('Enter your feed url:', 'avia_framework'); ?>
1243 <input class="widefat" id="<?php echo $this->get_field_id('rss'); ?>" name="<?php echo $this->get_field_name('rss'); ?>" type="text" value="<?php echo esc_attr($rss); ?>" /></label></p>
1244
1245
1246
1247 <?php
1248 }
1249 }
1250}
1251
1252
1253
1254
1255/**
1256 * AVIA ADVERTISING WIDGET
1257 *
1258 * Widget that retrieves, stores and displays the number of twitter and rss followers
1259 *
1260 * @package AviaFramework
1261 * @todo replace the widget system with a dynamic one, based on config files for easier widget creation
1262 */
1263
1264
1265//multiple images
1266if (!class_exists('avia_partner_widget'))
1267{
1268 class avia_partner_widget extends WP_Widget {
1269
1270 function __construct() {
1271
1272 $this->add_cont = 2;
1273 //Constructor
1274 $widget_ops = array('classname' => 'avia_partner_widget', 'description' => __('An advertising widget that displays 2 images with 125 x 125 px in size', 'avia_framework') );
1275 parent::__construct( 'avia_partner_widget', THEMENAME.' Advertising Area', $widget_ops );
1276 }
1277
1278 function widget($args, $instance)
1279 {
1280 extract($args, EXTR_SKIP);
1281 echo $before_widget;
1282
1283 global $kriesiaddwidget, $firsttitle;
1284 $kriesiaddwidget ++;
1285
1286 $title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
1287 $image_url = empty($instance['image_url']) ? '<span class="avia_parnter_empty"><span>'.__('Advertise here','avia_framework').'</span></span>' : '<img class="rounded" src="'.$instance['image_url'].'" title="'.$title.'" alt="'.$title.'"/>';
1288 $ref_url = empty($instance['ref_url']) ? '#' : apply_filters('widget_comments_title', $instance['ref_url']);
1289 $image_url2 = empty($instance['image_url2']) ? '<span class="avia_parnter_empty"><span>'.__('Advertise here','avia_framework').'</span></span>' : '<img class="rounded" src="'.$instance['image_url2'].'" title="'.$title.'" alt="'.$title.'"/>';
1290 $ref_url2 = empty($instance['ref_url2']) ? '#' : apply_filters('widget_comments_title', $instance['ref_url2']);
1291
1292 if ( ! empty( $title ) )
1293 {
1294 echo $before_title . $title . $after_title;
1295 }
1296
1297 echo avia_targeted_link_rel( '<a target="_blank" href="' . $ref_url . '" class="preloading_background avia_partner1 link_list_item' . $kriesiaddwidget . ' ' . $firsttitle . '" >' . $image_url . '</a>' );
1298
1299 if( $this->add_cont == 2 )
1300 {
1301 echo avia_targeted_link_rel( '<a target="_blank" href="' . $ref_url2 . '" class="preloading_background avia_partner2 link_list_item' . $kriesiaddwidget . ' ' . $firsttitle . '" >' . $image_url2 . '</a>' );
1302 }
1303 echo $after_widget;
1304
1305 if($title == '')
1306 {
1307 $firsttitle = 'no_top_margin';
1308 }
1309
1310 }
1311
1312
1313 function update($new_instance, $old_instance) {
1314 //save the widget
1315 $instance = $old_instance;
1316 foreach($new_instance as $key=>$value)
1317 {
1318 $instance[$key] = strip_tags($new_instance[$key]);
1319 }
1320 return $instance;
1321 }
1322
1323
1324
1325 function form($instance)
1326 {
1327 $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'image_url' => '', 'ref_url' => '', 'image_url2' => '', 'ref_url2' => '' ) );
1328 $title = strip_tags($instance['title']);
1329 $image_url = strip_tags($instance['image_url']);
1330 $ref_url = strip_tags($instance['ref_url']);
1331 $image_url2 = strip_tags($instance['image_url2']);
1332 $ref_url2 = strip_tags($instance['ref_url2']);
1333 ?>
1334 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'avia_framework'); ?>
1335 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
1336
1337 <p><label for="<?php echo $this->get_field_id('image_url'); ?>"><?php _e('Image URL:', 'avia_framework'); ?> <?php if($this->add_cont == 2) echo "(125px * 125px):"; ?>
1338 <input class="widefat" id="<?php echo $this->get_field_id('image_url'); ?>" name="<?php echo $this->get_field_name('image_url'); ?>" type="text" value="<?php echo esc_attr($image_url); ?>" /></label></p>
1339
1340 <p><label for="<?php echo $this->get_field_id('ref_url'); ?>"><?php _e('Referal URL:', 'avia_framework'); ?>
1341 <input class="widefat" id="<?php echo $this->get_field_id('ref_url'); ?>" name="<?php echo $this->get_field_name('ref_url'); ?>" type="text" value="<?php echo esc_attr($ref_url); ?>" /></label></p>
1342
1343 <?php if($this->add_cont == 2)
1344 { ?>
1345
1346 <p><label for="<?php echo $this->get_field_id('image_url2'); ?>"><?php _e('Image URL 2: (125px * 125px):', 'avia_framework'); ?>
1347 <input class="widefat" id="<?php echo $this->get_field_id('image_url2'); ?>" name="<?php echo $this->get_field_name('image_url2'); ?>" type="text" value="<?php echo esc_attr($image_url2); ?>" /></label></p>
1348
1349 <p><label for="<?php echo $this->get_field_id('ref_url2'); ?>"><?php _e('Referal URL 2:', 'avia_framework'); ?>
1350 <input class="widefat" id="<?php echo $this->get_field_id('ref_url2'); ?>" name="<?php echo $this->get_field_name('ref_url2'); ?>" type="text" value="<?php echo esc_attr($ref_url2); ?>" /></label></p>
1351
1352 <?php }?>
1353
1354 <?php
1355 }
1356 }
1357}
1358
1359
1360if (!class_exists('avia_one_partner_widget'))
1361{
1362 //one image
1363 class avia_one_partner_widget extends avia_partner_widget
1364 {
1365 function __construct()
1366 {
1367
1368 $this->add_cont = 1;
1369
1370 $widget_ops = array('classname' => 'avia_one_partner_widget', 'description' => __('An advertising widget that displays 1 big image', 'avia_framework') );
1371
1372 parent::__construct( 'avia_one_partner_widget', THEMENAME.' Big Advertising Area', $widget_ops );
1373 }
1374 }
1375}
1376
1377
1378
1379/**
1380 *
1381 *
1382 * Widget that retrieves, stores and displays the number of twitter and rss followers
1383 *
1384 *
1385 * @todo replace the widget system with a dynamic one, based on config files for easier widget creation
1386 */
1387
1388if( ! class_exists( 'avia_combo_widget' ) )
1389{
1390 /**
1391 * AVIA COMBO WIDGET
1392 *
1393 * Widget that displays your popular posts, recent posts, recent comments and a tagcloud in a tabbed section
1394 *
1395 * @package AviaFramework
1396 *
1397 * @since 4.4.2 extended and modified by günter
1398 */
1399 class avia_combo_widget extends Avia_Widget
1400 {
1401 /**
1402 * Constructor
1403 */
1404 public function __construct()
1405 {
1406 $widget_ops = array(
1407 'classname' => 'avia_combo_widget',
1408 'description' => __( 'A widget that displays your popular posts, recent posts, recent comments and a tagcloud', 'avia_framework' )
1409 );
1410
1411 parent::__construct( 'avia_combo_widget', THEMENAME.' Combo Widget', $widget_ops );
1412
1413 /**
1414 * Hook to enable
1415 */
1416 add_filter( 'avf_disable_frontend_assets', array( $this, 'handler_enable_shortcodes' ), 50, 1 );
1417 }
1418
1419 /**
1420 *
1421 * @since 4.4.2
1422 */
1423 public function __destruct()
1424 {
1425 parent::__destruct();
1426 }
1427
1428 /**
1429 *
1430 * @since 4.4.2
1431 * @param array $instance
1432 * @return array
1433 */
1434 protected function parse_args_instance( array $instance )
1435 {
1436 /**
1437 * Backwards comp. only
1438 *
1439 * @since 4.4.2 'count' was removed
1440 */
1441 $fallback = isset( $instance['count'] );
1442
1443 $new_instance = wp_parse_args( $instance, array(
1444 'show_popular' => 4,
1445 'show_recent' => 4,
1446 'show_comments' => 4,
1447 'show_tags' => 45,
1448 'tab_1' => 'popular',
1449 'tab_2' => 'recent',
1450 'tab_3' => 'comments',
1451 'tab_4' => 'tagcloud',
1452 ) );
1453
1454 if( $fallback )
1455 {
1456 $new_instance['show_popular'] = $instance['count'];
1457 $new_instance['show_recent'] = $instance['count'];
1458 $new_instance['show_comments'] = $instance['count'];
1459 unset( $new_instance['count'] );
1460 }
1461
1462 return $new_instance;
1463 }
1464
1465 /**
1466 * prints the widget
1467 *
1468 * @param array $args
1469 * @param array $instance
1470 */
1471 public function widget( $args, $instance )
1472 {
1473 $instance = $this->parse_args_instance( $instance );
1474
1475 extract( $args );
1476
1477 echo $before_widget;
1478
1479 $used_tabs = 0;
1480
1481 for( $tab_nr = 1; $tab_nr < 5; $tab_nr++ )
1482 {
1483 $key = 'tab_' . $tab_nr;
1484
1485 if( empty( $instance[ $key ] ) )
1486 {
1487 continue;
1488 }
1489
1490 if( ! in_array( $instance[ $key ], array( 'popular', 'recent', 'comments', 'tagcloud' ) ) )
1491 {
1492 continue;
1493 }
1494
1495 $used_tabs++;
1496 $add_class = '';
1497 $add_class2 = '';
1498
1499 if( 1 == $used_tabs )
1500 {
1501 echo "<div class='tabcontainer border_tabs top_tab tab_initial_open tab_initial_open__1'>";
1502 $add_class = ' first_tab active_tab ';
1503 $add_class2 = 'active_tab_content';
1504 }
1505
1506 switch( $instance[ $key ] )
1507 {
1508 case 'popular':
1509 $args = array(
1510 'posts_per_page' => $instance['show_popular'],
1511 'orderby' => 'comment_count',
1512 'order' => 'desc'
1513 );
1514
1515 echo '<div class="tab widget_tab_popular' . $add_class . '"><span>' . __( 'Popular', 'avia_framework' ) . '</span></div>';
1516 echo "<div class='tab_content {$add_class2}'>";
1517 avia_combo_widget::get_post_list( $args );
1518 echo "</div>";
1519 break;
1520 case 'recent':
1521 $args = array(
1522 'posts_per_page' => $instance['show_recent'],
1523 'orderby' => 'post_date',
1524 'order' => 'desc'
1525 );
1526 echo '<div class="tab widget_tab_recent' . $add_class . '"><span>'.__('Recent', 'avia_framework').'</span></div>';
1527 echo "<div class='tab_content {$add_class2}'>";
1528 avia_combo_widget::get_post_list( $args );
1529 echo "</div>";
1530 break;
1531 case 'comments':
1532 $args = array(
1533 'number' => $instance['show_comments'],
1534 'status' => 'approve',
1535 'order' => 'DESC'
1536 );
1537 echo '<div class="tab widget_tab_comments' . $add_class . '"><span>'.__('Comments', 'avia_framework').'</span></div>';
1538 echo "<div class='tab_content {$add_class2}'>";
1539 avia_combo_widget::get_comment_list( $args );
1540 echo "</div>";
1541 break;
1542 case 'tagcloud':
1543 $args = array(
1544 'number' => $instance['show_tags'],
1545 'smallest' => 12,
1546 'largest' => 12,
1547 'unit' => 'px'
1548 );
1549 echo '<div class="tab last_tab widget_tab_tags' . $add_class . '"><span>'.__('Tags', 'avia_framework').'</span></div>';
1550 echo "<div class='tab_content tagcloud {$add_class2}'>";
1551 wp_tag_cloud( $args );
1552 echo "</div>";
1553 break;
1554 }
1555 }
1556
1557 if( $used_tabs > 0 )
1558 {
1559 echo "</div>";
1560 }
1561
1562 echo $after_widget;
1563 }
1564
1565
1566 /**
1567 *
1568 * @param array $new_instance
1569 * @param array $old_instance
1570 * @return array
1571 */
1572 public function update( $new_instance, $old_instance )
1573 {
1574 $instance = $this->parse_args_instance( $old_instance );
1575 $fields = $this->get_field_names();
1576
1577 foreach( $new_instance as $key => $value )
1578 {
1579 if( in_array( $key, $fields ) )
1580 {
1581 $instance[ $key ] = strip_tags( $value );
1582 }
1583 }
1584
1585 return $instance;
1586 }
1587
1588
1589 /**
1590 * Widgetform in backend
1591 *
1592 * @param array $instance
1593 */
1594 public function form( $instance )
1595 {
1596 $instance = $this->parse_args_instance( $instance );
1597
1598 extract( $instance );
1599
1600 $tab_content = array(
1601 0 => __( 'No content', 'avia_framework' ),
1602 'popular' => __( 'Popular posts', 'avia_framework' ),
1603 'recent' => __( 'Recent posts', 'avia_framework' ),
1604 'comments' => __( 'Newest comments', 'avia_framework' ),
1605 'tagcloud' => __( 'Tag cloud', 'avia_framework' ),
1606
1607 );
1608 ?>
1609 <p><label for="<?php echo $this->get_field_id( 'show_popular' ); ?>"><?php _e( 'Number of popular posts', 'avia_framework' ); ?>:</label>
1610 <select id="<?php echo $this->get_field_id( 'show_popular' ); ?>" name="<?php echo $this->get_field_name( 'show_popular' ); ?>" class="widefat">
1611 <?php
1612 echo Avia_Widget::number_options( 1, 30, $show_popular );
1613 ?>
1614 </select>
1615 </p>
1616 <p><label for="<?php echo $this->get_field_id( 'show_recent' ); ?>"><?php _e( 'Number of recent posts', 'avia_framework' ); ?>:</label>
1617 <select id="<?php echo $this->get_field_id( 'show_recent' ); ?>" name="<?php echo $this->get_field_name( 'show_recent' ); ?>" class="widefat">
1618 <?php
1619 echo Avia_Widget::number_options( 1, 30, $show_recent );
1620 ?>
1621 </select>
1622 </p>
1623 <p><label for="<?php echo $this->get_field_id( 'show_comments' ); ?>"><?php _e( 'Number of newest comments', 'avia_framework' ); ?>:</label>
1624 <select id="<?php echo $this->get_field_id( 'show_comments' ); ?>" name="<?php echo $this->get_field_name( 'show_comments' ); ?>" class="widefat">
1625 <?php
1626 echo Avia_Widget::number_options( 1, 30, $show_comments );
1627 ?>
1628 </select>
1629 </p>
1630 <p><label for="<?php echo $this->get_field_id( 'show_tags' ); ?>"><?php _e( 'Number of tags for tag cloud', 'avia_framework' ); ?>:</label>
1631 <select id="<?php echo $this->get_field_id( 'show_tags' ); ?>" name="<?php echo $this->get_field_name( 'show_tags' ); ?>" class="widefat">
1632 <?php
1633 echo Avia_Widget::number_options( 1, 100, $show_tags );
1634 ?>
1635 </select>
1636 </p>
1637 <p><label for="<?php echo $this->get_field_id( 'tab_1' ); ?>"><?php _e( 'Content of first tab', 'avia_framework' ); ?>:</label>
1638 <select id="<?php echo $this->get_field_id( 'tab_1' ); ?>" name="<?php echo $this->get_field_name( 'tab_1' ); ?>" class="widefat">
1639 <?php
1640 $tab_content_first = $tab_content;
1641 unset( $tab_content_first[0] );
1642 echo Avia_Widget::options_from_array( $tab_content_first, $tab_1 );
1643 ?>
1644 </select>
1645 </p>
1646 <p><label for="<?php echo $this->get_field_id( 'tab_2' ); ?>"><?php _e( 'Content of next tab', 'avia_framework' ); ?>:</label>
1647 <select id="<?php echo $this->get_field_id( 'tab_2' ); ?>" name="<?php echo $this->get_field_name( 'tab_2' ); ?>" class="widefat">
1648 <?php
1649 echo Avia_Widget::options_from_array( $tab_content, $tab_2 );
1650 ?>
1651 </select>
1652 </p>
1653 <p><label for="<?php echo $this->get_field_id( 'tab_3' ); ?>"><?php _e( 'Content of next tab', 'avia_framework' ); ?>:</label>
1654 <select id="<?php echo $this->get_field_id( 'tab_3' ); ?>" name="<?php echo $this->get_field_name( 'tab_3' ); ?>" class="widefat">
1655 <?php
1656 echo Avia_Widget::options_from_array( $tab_content, $tab_3 );
1657 ?>
1658 </select>
1659 </p>
1660 <p><label for="<?php echo $this->get_field_id( 'tab_4' ); ?>"><?php _e( 'Content of next tab', 'avia_framework' ); ?>:</label>
1661 <select id="<?php echo $this->get_field_id( 'tab_4' ); ?>" name="<?php echo $this->get_field_name( 'tab_4' ); ?>" class="widefat">
1662 <?php
1663 echo Avia_Widget::options_from_array( $tab_content, $tab_4 );
1664 ?>
1665 </select>
1666 </p>
1667 <?php
1668 }
1669
1670
1671 /**
1672 * This widget needs tab.css and tab.js to work properly.
1673 *
1674 * @since 4.4.2
1675 * @added_by Günter
1676 * @param array $disabled
1677 * @return array
1678 */
1679 public function handler_enable_shortcodes( array $disabled )
1680 {
1681 $settings = $this->get_settings();
1682
1683 /**
1684 * Search page might lead to no result and in this case we activate this widget manually
1685 */
1686 if( ( count( $settings ) > 0 ) || is_search() )
1687 {
1688 unset( $disabled['av_tab_container'] );
1689 }
1690
1691 return $disabled;
1692 }
1693
1694 /**
1695 * Get postlist by query args
1696 * (up to 4.4.2 this was function avia_get_post_list( $avia_new_query , $excerpt = false)
1697 *
1698 * @since 4.4.2
1699 * @added_by Günter
1700 * @param array $args
1701 * @param type $excerpt
1702 */
1703 static public function get_post_list( array $args , $excerpt = false )
1704 {
1705 global $avia_config;
1706
1707 $image_size = isset( $avia_config['widget_image_size'] ) ? $avia_config['widget_image_size'] : 'widget';
1708
1709 $additional_loop = new WP_Query($args);
1710
1711 if( $additional_loop->have_posts() )
1712 {
1713 echo '<ul class="news-wrap">';
1714
1715 while ( $additional_loop->have_posts() )
1716 {
1717 $additional_loop->the_post();
1718
1719 $format = "";
1720 if( get_post_type() != 'post' )
1721 {
1722 $format = get_post_type();
1723 }
1724
1725 if( empty( $format ) )
1726 {
1727 $format = get_post_format();
1728 }
1729 if( empty( $format ) )
1730 {
1731 $format = 'standard';
1732 }
1733
1734 echo '<li class="news-content post-format-' . $format . '">';
1735
1736 //check for preview images:
1737 $image = "";
1738
1739 if( ! current_theme_supports( 'force-post-thumbnails-in-widget' ) )
1740 {
1741 $slides = avia_post_meta( get_the_ID(), 'slideshow' );
1742
1743 if( $slides != "" && ! empty( $slides[0]['slideshow_image'] ) )
1744 {
1745 $image = avia_image_by_id( $slides[0]['slideshow_image'], 'widget', 'image' );
1746 }
1747 }
1748
1749 if( ! $image && current_theme_supports( 'post-thumbnails' ) )
1750 {
1751 $image = get_the_post_thumbnail( get_the_ID(), $image_size );
1752 }
1753
1754 $time_format = apply_filters( 'avia_widget_time', get_option('date_format') . " - " . get_option('time_format'), 'avia_get_post_list' );
1755
1756 $nothumb = ( ! $image) ? 'no-news-thumb' : '';
1757
1758 echo "<a class='news-link' title='" . get_the_title() . "' href='" . get_permalink() . "'>";
1759 echo "<span class='news-thumb $nothumb'>";
1760 echo $image;
1761 echo "</span>";
1762 echo "<strong class='news-headline'>".avia_backend_truncate(get_the_title(), 55," ");
1763 echo "<span class='news-time'>".get_the_time($time_format)."</span>";
1764 echo "</strong>";
1765 echo "</a>";
1766
1767 if( 'display title and excerpt' == $excerpt )
1768 {
1769 echo "<div class='news-excerpt'>";
1770 the_excerpt();
1771 echo "</div>";
1772 }
1773
1774 echo '</li>';
1775 }
1776
1777 echo "</ul>";
1778 wp_reset_postdata();
1779 }
1780 }
1781
1782 /**
1783 * Get commentlist by query args
1784 * (up to 4.4.2 this was function avia_get_comment_list( $avia_new_query )
1785 *
1786 * @since 4.4.2
1787 * @added_by Günter
1788 * @param array $args
1789 */
1790 static public function get_comment_list( array $args )
1791 {
1792 $time_format = apply_filters( 'avia_widget_time', get_option( 'date_format' ) . " - " . get_option( 'time_format' ), 'avia_get_comment_list' );
1793
1794 $comments = get_comments( $args );
1795
1796 if( ! empty( $comments ) )
1797 {
1798 echo '<ul class="news-wrap">';
1799
1800 foreach( $comments as $comment )
1801 {
1802 if( $comment->comment_author != 'ActionScheduler' )
1803 {
1804 $gravatar_alt = esc_html( $comment->comment_author );
1805
1806 echo '<li class="news-content">';
1807 echo "<a class='news-link' title='" . get_the_title( $comment->comment_post_ID ) . "' href='" . get_comment_link($comment) . "'>";
1808 echo "<span class='news-thumb'>";
1809 echo get_avatar( $comment, '48', '', $gravatar_alt );
1810 echo "</span>";
1811 echo "<strong class='news-headline'>" . avia_backend_truncate( $comment->comment_content, 55," " );
1812
1813 if($time_format)
1814 {
1815 echo "<span class='news-time'>" . get_comment_date( $time_format, $comment->comment_ID ) . " " . __( 'by', 'avia_framework' ) . " " . $comment->comment_author . "</span>";
1816 }
1817 echo "</strong>";
1818 echo "</a>";
1819 echo '</li>';
1820 }
1821 }
1822
1823 echo "</ul>";
1824 wp_reset_postdata();
1825 }
1826 }
1827 }
1828}
1829
1830/*-----------------------------------------------------------------------------------
1831get posts posts
1832-----------------------------------------------------------------------------------*/
1833if ( ! function_exists('avia_get_post_list'))
1834{
1835 function avia_get_post_list( $avia_new_query , $excerpt = false)
1836 {
1837 _deprecated_function( 'avia_get_post_list', '4.4.2', 'avia_combo_widget::get_post_list( $args )');
1838
1839 $avia_new_query = wp_parse_args( $avia_new_query );
1840 avia_combo_widget::get_post_list( $avia_new_query, $excerpt );
1841 }
1842
1843}
1844
1845if (!function_exists('avia_get_comment_list'))
1846{
1847 function avia_get_comment_list( $avia_new_query )
1848 {
1849 _deprecated_function( 'avia_get_comment_list', '4.4.2', 'avia_combo_widget::get_comment_list( $args )');
1850
1851 $avia_new_query = wp_parse_args( $avia_new_query );
1852 avia_combo_widget::get_comment_list( $avia_new_query);
1853 }
1854
1855
1856}
1857
1858
1859
1860/*
1861 Google Maps Widget
1862
1863 Copyright 2009 Clark Nikdel Powell (email : taylor@cnpstudio.com)
1864
1865 This program is free software; you can redistribute it and/or modify
1866 it under the terms of the GNU General Public License as published by
1867 the Free Software Foundation; either version 2 of the License, or
1868 (at your option) any later version.
1869
1870 This program is distributed in the hope that it will be useful,
1871 but WITHOUT ANY WARRANTY; without even the implied warranty of
1872 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1873 GNU General Public License for more details.
1874
1875 You should have received a copy of the GNU General Public License
1876 along with this program; if not, write to the Free Software
1877 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1878*/
1879
1880if( ! class_exists('avia_google_maps') )
1881{
1882 class avia_google_maps extends Avia_Widget
1883 {
1884
1885 /**
1886 *
1887 */
1888 public function __construct()
1889 {
1890 $widget_ops = array(
1891 'classname' => 'avia_google_maps',
1892 'description' => __( 'Add a google map to your blog or site', 'avia_framework' )
1893 );
1894
1895 parent::__construct( 'avia_google_maps', THEMENAME.' Google Maps Widget', $widget_ops );
1896
1897// add_action( 'admin_enqueue_scripts', array( $this,'handler_print_google_maps_scripts' ) );
1898 }
1899
1900
1901 /**
1902 * @since 4.3.2
1903 */
1904 public function __destruct()
1905 {
1906 parent::__destruct();
1907 }
1908
1909 /**
1910 *
1911 * @since 4.3.2
1912 * @param array $instance
1913 * @return array
1914 */
1915 protected function parse_args_instance( array $instance )
1916 {
1917 $SGMoptions = get_option( 'SGMoptions', array() ); // get options defined in admin page ????
1918 $SGMoptions = wp_parse_args( $SGMoptions, array(
1919 'zoom' => '15', // 1 - 19
1920 'type' => 'ROADMAP', // ROADMAP, SATELLITE, HYBRID, TERRAIN
1921 'content' => '',
1922 ) );
1923
1924 $new_instance = wp_parse_args( $instance, array(
1925 'title' => '',
1926 'lat' => '0',
1927 'lng' => '0',
1928 'zoom' => $SGMoptions['zoom'],
1929 'type' => $SGMoptions['type'],
1930 'directionsto' => '',
1931 'content' => $SGMoptions['content'],
1932 'width' => '',
1933 'height' => '',
1934 'street-address' => '',
1935 'city' => '',
1936 'state' => '',
1937 'postcode' => '',
1938 'country' => '',
1939 'icon' => '',
1940 'google_link' => '',
1941 'confirm_button' => __( 'Click to load Google Maps', 'avia_framework' ),
1942 'page_link_text' => __( 'Open Google Maps in a new window', 'avia_framework' ),
1943 'google_fallback' => ''
1944 ) );
1945
1946 return $new_instance;
1947 }
1948
1949
1950 /**
1951 * Output the content of the widget
1952 *
1953 * @param array $args
1954 * @param array $instance
1955 */
1956 public function widget( $args, $instance )
1957 {
1958 if( true === Av_Google_Maps()->is_loading_prohibited() )
1959 {
1960 if( current_user_can( 'edit_posts' ) )
1961 {
1962 echo '<p style="font-weight: 700;color: red;">' . __( 'Widget Google Maps is disabled in theme options "Google Services".', 'avia_framework' ) . '</p>';
1963 echo '<p style="font-weight: 400;color: red;">' . __( '(Visible to admins only.)', 'avia_framework' ) . '</p>';
1964 }
1965
1966 return;
1967 }
1968
1969 $instance = $this->parse_args_instance( $instance );
1970 $fields = $this->get_field_names();
1971
1972 foreach( $instance as $key => $value )
1973 {
1974 if( in_array( $key, $fields ) )
1975 {
1976 $instance[ $key ] = esc_attr( $value );
1977 }
1978 }
1979
1980 extract( $args );
1981 extract( $instance );
1982
1983 $street_address = $instance['street-address'];
1984
1985 if( empty( $lat ) || empty( $lng ) )
1986 {
1987 return;
1988 }
1989
1990 /**
1991 * Allow to change the conditional display setting - e.g. if user is opt in and allows to connect directly
1992 *
1993 * @since 4.4
1994 * @param string $google_link '' | 'confirm_link' | 'page_only'
1995 * @param string $context
1996 * @param mixed $object
1997 * @param array $args
1998 * @param array $instance
1999 * @return string
2000 */
2001 $original_google_link = $google_link;
2002 $google_link = apply_filters( 'avf_conditional_setting_external_links', $google_link, __CLASS__, $this, $args, $instance );
2003 if( ! in_array( $google_link, array( '', 'confirm_link', 'page_only' ) ) )
2004 {
2005 $google_link = $original_google_link;
2006 }
2007
2008 $title = apply_filters('widget_title', $title );
2009
2010 echo $before_widget;
2011
2012 if( ! empty( $title ) )
2013 {
2014 echo $before_title . $title . $after_title;
2015 }
2016
2017
2018 $html_fallback_url = '';
2019 if( ! empty( $google_fallback ) )
2020 {
2021 $html_fallback_url .= 'background-image:url(' . $google_fallback . ');';
2022 }
2023
2024 $html_overlay = '';
2025 if( ( 'confirm_link' == $google_link ) || ( 'page_only' == $google_link ) )
2026 {
2027 $button_class = empty( $html_fallback_url ) ? ' av_text_confirm_link_visible' : '';
2028
2029 $text_overlay = '';
2030 if( 'confirm_link' == $google_link )
2031 {
2032 $html_overlay = '<a href="#" class="av_gmaps_confirm_link av_text_confirm_link' . $button_class . '">';
2033 $text_overlay = esc_html( $confirm_button );
2034 }
2035 else
2036 {
2037 if( empty( $street_address ) )
2038 {
2039 $adress1 = $lat;
2040 $adress2 = $lng;
2041 }
2042 else
2043 {
2044 $adress1 = $street_address . ' ' . $postcode . ' ' . $city . ' ' . $state . ' ' . $country;
2045 $adress2 = '';
2046 }
2047
2048 $url = av_google_maps::api_destination_url( $adress1, $adress2 );
2049 $html_overlay = avia_targeted_link_rel( '<a class="av_gmaps_page_only av_text_confirm_link' . $button_class . '" href="' . $url . '" target="_blank">' );
2050 $text_overlay = esc_html( $page_link_text );
2051 }
2052
2053 $html_overlay .= '<span>' . $text_overlay . '</span></a>';
2054
2055 /**
2056 * @since 4.4.2
2057 * @param string output string
2058 * @param object context
2059 * @param array
2060 * @param array
2061 */
2062 $filter_args = array(
2063 $html_overlay,
2064 $this,
2065 $args,
2066 $instance
2067 );
2068 $html_overlay = apply_filters_ref_array( 'avf_google_maps_confirm_overlay', $filter_args );
2069 }
2070
2071 $map_id = '';
2072 if( 'page_only' != $google_link )
2073 {
2074 /**
2075 * Add map data to js
2076 */
2077 $content = htmlspecialchars( $content, ENT_QUOTES );
2078 $content = str_replace( '<', '<', $content );
2079 $content = str_replace( '>', '>', $content );
2080 $content = str_replace( '"', '"', $content );
2081 $content = str_replace( ''', '"', $content );
2082// $content = json_encode( $content );
2083 $content = wpautop( $content );
2084
2085 $data = array(
2086 'hue' => '',
2087 'zoom' => $zoom,
2088 'saturation' => '',
2089 'zoom_control' => true,
2090// 'pan_control' => true, not needed in > 4.3.2
2091 'streetview_control' => false,
2092 'mobile_drag_control' => true,
2093 'maptype_control' => 'dropdown',
2094 'maptype_id' => $type
2095 );
2096
2097 $data['marker'] = array();
2098
2099 $data['marker'][0] = array(
2100 'address' => $postcode . ' ' . $street_address,
2101 'city' => $city,
2102 'country' => $country,
2103 'state' => $state,
2104 'long' => $lng,
2105 'lat' => $lat,
2106 'icon' => $icon,
2107 'imagesize' => 40,
2108 'content' => $content,
2109 );
2110
2111 /**
2112 * Does not work since 4.4
2113 */
2114 if( ! empty( $directionsto ) )
2115 {
2116 $data['marker'][0]['directionsto'] = $directionsto;
2117 }
2118
2119 $add = empty( $google_link ) ? 'unconditionally' : 'delayed';
2120
2121 /**
2122 * Allow to filter Google Maps data array
2123 *
2124 * @since 4.4
2125 * @param array $data
2126 * @param string context
2127 * @param object
2128 * @param array additional args
2129 * @return array
2130 */
2131 $data = apply_filters( 'avf_google_maps_data', $data, __CLASS__, $this, array( $args, $instance ) );
2132
2133 $map_id = Av_Google_Maps()->add_map( $data, $add );
2134 }
2135
2136 switch( $google_link )
2137 {
2138 case 'confirm_link':
2139 $show_class = 'av_gmaps_show_delayed';
2140 break;
2141 case 'page_only':
2142 $show_class = 'av_gmaps_show_page_only';
2143 break;
2144 case '':
2145 default:
2146 $show_class = 'av_gmaps_show_unconditionally';
2147 break;
2148 }
2149
2150 if( empty( $html_fallback_url ) )
2151 {
2152 $show_class .= ' av-no-fallback-img';
2153 }
2154
2155 $style = ''; // $this->define_height($height)
2156 if( ! empty( $height ) )
2157 {
2158 $height = str_replace( ';', '', $height );
2159 $style .= " height: {$height};";
2160 }
2161 if( ! empty( $width ) )
2162 {
2163 $width = str_replace( ';', '', $width );
2164 $style .= " width: {$width};";
2165 }
2166 if( ! empty( $html_fallback_url ) )
2167 {
2168 $html_fallback_url = str_replace( ';', '', $html_fallback_url );
2169 $style .= " {$html_fallback_url};";
2170 }
2171
2172 if( ! empty( $style ) )
2173 {
2174 $style = "style='{$style}'";
2175 }
2176
2177 echo '<div class="av_gmaps_widget_main_wrap av_gmaps_main_wrap">';
2178
2179 if( empty( $map_id ) )
2180 {
2181 echo "<div class='avia-google-map-container avia-google-map-widget {$show_class}' {$style}>";
2182 }
2183 else
2184 {
2185 echo "<div id='{$map_id}' class='avia-google-map-container avia-google-map-widget {$show_class}' data-mapid='{$map_id}' {$style}>";
2186 }
2187
2188 echo $html_overlay;
2189 echo '</div>';
2190
2191
2192 echo '</div>';
2193
2194 echo $after_widget;
2195 }
2196
2197 /**
2198 * Process widget options to be saved
2199 *
2200 * @param array $new_instance
2201 * @param array $old_instance
2202 * @return array
2203 */
2204 public function update( $new_instance, $old_instance )
2205 {
2206 $instance = $this->parse_args_instance( $old_instance );
2207
2208 $fields = $this->get_field_names();
2209
2210 foreach( $new_instance as $key => $value )
2211 {
2212 if( in_array( $key, $fields ) )
2213 {
2214 $instance[ $key ] = strip_tags( $value );
2215 }
2216 }
2217
2218 return $instance;
2219 }
2220
2221
2222 /**
2223 * output the options form on admin
2224 *
2225 * @param array $instance
2226 */
2227 public function form( $instance )
2228 {
2229 $instance = $this->parse_args_instance( $instance );
2230 $fields = $this->get_field_names();
2231
2232 foreach( $instance as $key => $value )
2233 {
2234 if( in_array( $key, $fields ) )
2235 {
2236 $instance[ $key ] = esc_attr( $value );
2237 }
2238 }
2239
2240 extract( $instance );
2241
2242 $street_address = $instance['street-address'];
2243
2244 $html = new avia_htmlhelper();
2245
2246 $marker_icon_element = array(
2247 'name' => __( 'Custom Marker Icon', 'avia_framework' ),
2248 'desc' => __( 'Upload a custom marker icon or enter the URL', 'avia_framework' ),
2249 'id' => $this->get_field_id( 'icon'),
2250 'id_name' => $this->get_field_name( 'icon' ),
2251 'std' => $icon,
2252 'type' => 'upload',
2253 'label' => __('Use image as custom marker icon', 'avia_framework')
2254 );
2255
2256 $fallback_element = array(
2257 'name' => __( 'Fallback image to replace Google Maps', 'avia_framework' ),
2258 'desc' => __( 'Upload a fallback image or enter the URL to an image to replace Google Maps or until Google Maps is loaded', 'avia_framework' ),
2259 'id' => $this->get_field_id( 'google_fallback'),
2260 'id_name' => $this->get_field_name( 'google_fallback' ),
2261 'std' => $google_fallback,
2262 'type' => 'upload',
2263 'label' => __('Use image as Google Maps fallback image', 'avia_framework')
2264 );
2265
2266 ?>
2267 <div class="avia_widget_form avia_widget_conditional_form avia_google_maps_form <?php echo $google_link;?>">
2268 <p>
2269 <label for="<?php print $this->get_field_id('title'); ?>"><?php _e('Title:','avia_framework'); ?></label>
2270 <input class="widefat" id="<?php print $this->get_field_id('title'); ?>" name="<?php print $this->get_field_name('title'); ?>" type="text" value="<?php print $title; ?>" />
2271 </p>
2272 <p>
2273 <?php _e('Enter the latitude and longitude of the location you want to display. Need help finding the latitude and longitude?', 'avia_framework'); ?> <a href="#" class="avia-coordinates-help-link button"><?php _e('Click here to enter an address.','avia_framework'); ?></a>
2274 </p>
2275 <div class="avia-find-coordinates-wrapper">
2276 <p>
2277 <label for="<?php print $this->get_field_id('street-address'); ?>"><?php _e('Street Address:','avia_framework'); ?></label>
2278 <input class='widefat avia-map-street-address' id="<?php print $this->get_field_id('street-address'); ?>" name="<?php print $this->get_field_name('street-address'); ?>" type="text" value="<?php print $street_address; ?>" />
2279 </p>
2280 <p>
2281 <label for="<?php print $this->get_field_id('city'); ?>"><?php _e('City:','avia_framework'); ?></label>
2282 <input class='widefat avia-map-city' id="<?php print $this->get_field_id('city'); ?>" name="<?php print $this->get_field_name('city'); ?>" type="text" value="<?php print $city; ?>" />
2283 </p>
2284 <p>
2285 <label for="<?php print $this->get_field_id('state'); ?>"><?php _e('State:','avia_framework'); ?></label>
2286 <input class='widefat avia-map-state' id="<?php print $this->get_field_id('state'); ?>" name="<?php print $this->get_field_name('state'); ?>" type="text" value="<?php print $state; ?>" />
2287 </p>
2288 <p>
2289 <label for="<?php print $this->get_field_id('postcode'); ?>"><?php _e('Postcode:','avia_framework'); ?></label>
2290 <input class='widefat avia-map-postcode' id="<?php print $this->get_field_id('postcode'); ?>" name="<?php print $this->get_field_name('postcode'); ?>" type="text" value="<?php print $postcode; ?>" />
2291 </p>
2292 <p>
2293 <label for="<?php print $this->get_field_id('country'); ?>"><?php _e('Country:','avia_framework'); ?></label>
2294 <input class='widefat avia-map-country' id="<?php print $this->get_field_id('country'); ?>" name="<?php print $this->get_field_name('country'); ?>" type="text" value="<?php print $country; ?>" />
2295 </p>
2296 <p>
2297 <a class="button avia-populate-coordinates"><?php _e('Fetch coordinates!','avia_framework'); ?></a>
2298 <div class='avia-loading-coordinates'><?php _e('Fetching the coordinates. Please wait...','avia_framework'); ?></div>
2299 </p>
2300 </div>
2301 <div class="avia-coordinates-wrapper">
2302 <p>
2303 <label for="<?php print $this->get_field_id('lat'); ?>"><?php _e('Latitude:','avia_framework'); ?></label>
2304 <input class='widefat avia-map-lat' id="<?php print $this->get_field_id('lat'); ?>" name="<?php print $this->get_field_name('lat'); ?>" type="text" value="<?php print $lat; ?>" />
2305 </p>
2306 <p>
2307 <label for="<?php print $this->get_field_id('lng'); ?>"><?php _e('Longitude:','avia_framework'); ?></label>
2308 <input class='widefat avia-map-lng' id="<?php print $this->get_field_id('lng'); ?>" name="<?php print $this->get_field_name('lng'); ?>" type="text" value="<?php print $lng; ?>" />
2309 </p>
2310 </div>
2311 <p>
2312 <label for="<?php print $this->get_field_id('zoom'); ?>"><?php echo __('Zoom Level:','avia_framework').' <small>'.__('(1-19)','avia_framework').'</small>'; ?></label>
2313 <select class="widefat" id="<?php echo $this->get_field_id('zoom'); ?>" name="<?php echo $this->get_field_name('zoom'); ?>">
2314 <?php
2315 $answers = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);
2316 foreach( $answers as $answer )
2317 {
2318 ?><option value="<?php echo $answer;?>" <?php selected( $answer, $zoom ); ?>><?php echo $answer;?></option><?php
2319 }?>
2320 </select>
2321 </p>
2322 <p>
2323 <label for="<?php print $this->get_field_id('type'); ?>"><?php _e('Map Type:','avia_framework'); ?></label>
2324 <select class="widefat" id="<?php echo $this->get_field_id('type'); ?>" name="<?php echo $this->get_field_name('type'); ?>">
2325 <?php
2326 $answers = array('ROADMAP', 'SATELLITE', 'HYBRID', 'TERRAIN');
2327 foreach( $answers as $answer )
2328 {
2329 ?><option value="<?php echo $answer;?>" <?php selected( $answer, $type ); ?>><?php echo $answer;?></option><?php
2330 }?>
2331 </select>
2332 </p>
2333 <p style="display:none;">
2334 <label for="<?php print $this->get_field_id('directionsto'); ?>"><?php _e('Display a Route by entering a address here. (If address is added Zoom will be ignored)','avia_framework'); ?>:</label>
2335 <input class="widefat" id="<?php print $this->get_field_id('directionsto'); ?>" name="<?php print $this->get_field_name('directionsto'); ?>" type="text" value="<?php print $directionsto; ?>" />
2336 </p>
2337 <p>
2338 <label for="<?php print $this->get_field_id('content'); ?>"><?php _e('Info Bubble Content:','avia_framework'); ?></label>
2339 <textarea rows="7" class="widefat" id="<?php print $this->get_field_id('content'); ?>" name="<?php print $this->get_field_name('content'); ?>"><?php print $content; ?></textarea>
2340 </p>
2341
2342 <div class="avia_gm_marker_icon_upload avia_google_marker_icon av-widgets-upload">
2343 <?php echo $html->render_single_element( $marker_icon_element );?>
2344 </div>
2345 <p>
2346 <label for="<?php print $this->get_field_id('width'); ?>"><?php _e('Enter the width in px or % (100% width will be used if you leave this field empty)','avia_framework'); ?>:</label>
2347 <input class="widefat" id="<?php print $this->get_field_id('width'); ?>" name="<?php print $this->get_field_name('width'); ?>" type="text" value="<?php print $width; ?>" />
2348 </p>
2349 <p>
2350 <label for="<?php print $this->get_field_id('height'); ?>"><?php _e('Enter the height in px or %','avia_framework'); ?>:</label>
2351 <input class="widefat" id="<?php print $this->get_field_id('height'); ?>" name="<?php print $this->get_field_name('height'); ?>" type="text" value="<?php print $height; ?>" />
2352 </p>
2353 <p>
2354 <label for="<?php echo $this->get_field_id( 'google_link' ); ?>"><?php _e( 'Link to Google Maps', 'avia_framework' ); ?>:</label>
2355 <select id="<?php echo $this->get_field_id( 'google_link' ); ?>" name="<?php echo $this->get_field_name( 'google_link' ); ?>" class="widefat avia-coditional-widget-select">
2356 <option value="" <?php selected( '', $google_link ) ?>><?php _e( 'Show Google Maps immediately', 'avia_framework' ); ?></option>
2357 <option value="confirm_link" <?php selected( 'confirm_link', $google_link ) ?>><?php _e( 'User must accept to show Google Maps', 'avia_framework' ); ?></option>
2358 <option value="page_only" <?php selected( 'page_only', $google_link ) ?>><?php _e( 'Only open Google Maps in new window', 'avia_framework' ); ?></option>
2359 </select>
2360 </p>
2361
2362 <p class="av-confirm_link">
2363 <label for="<?php echo $this->get_field_id('confirm_button'); ?>"><?php _e('Button text confirm to load Google Maps:', 'avia_framework'); ?>
2364 <input class="widefat" id="<?php echo $this->get_field_id('confirm_button'); ?>" name="<?php echo $this->get_field_name('confirm_button'); ?>" type="text" value="<?php echo $confirm_button; ?>" /></label>
2365 </p>
2366
2367 <p class="av-page_only">
2368 <label for="<?php echo $this->get_field_id('page_link_text'); ?>"><?php _e('Direct link to Google Maps page:', 'avia_framework'); ?>
2369 <input class="widefat" id="<?php echo $this->get_field_id('page_link_text'); ?>" name="<?php echo $this->get_field_name('page_link_text'); ?>" type="text" value="<?php echo $page_link_text; ?>" /></label>
2370 </p>
2371
2372 <div class="avia_gm_fallback_upload avia_google_fallback av-widgets-upload">
2373 <?php echo $html->render_single_element( $fallback_element );?>
2374 </div>
2375<?php
2376 if( true === Av_Google_Maps()->is_loading_prohibited() )
2377 {
2378 echo '<p style="font-weight: 700;color: red;">' . __( 'This element is disabled in frontend with theme option', 'avia_framework' ) . '</p>';
2379 }
2380?>
2381 </div>
2382 <?php
2383 }
2384
2385 /**
2386 * Output scripts in backend
2387 */
2388 public function handler_print_google_maps_scripts()
2389 {
2390 return;
2391
2392 $api_key = avia_get_option( 'gmap_api' );
2393 $api_url = av_google_maps::api_url( $api_key );
2394
2395 wp_register_script( 'avia-google-maps-api', $api_url, array('jquery'), null, true );
2396
2397 $load_google_map_api = apply_filters( 'avf_load_google_map_api', true, 'avia_google_map_widget' );
2398
2399 if( $load_google_map_api )
2400 {
2401 wp_enqueue_script( 'avia-google-maps-api' );
2402 }
2403
2404 $is_widget_edit_page = in_array( basename( $_SERVER['PHP_SELF'] ), array( 'widgets.php' ) );
2405 if( $is_widget_edit_page )
2406 {
2407 wp_register_script( 'avia-google-maps-widget', AVIA_JS_URL . 'conditional_load/avia_google_maps_widget_admin.js', array( 'jquery','media-upload','media-views' ), '1.0.0', true);
2408 wp_enqueue_script( 'avia-google-maps-widget' );
2409
2410 $args = array(
2411 'toomanyrequests' => __( "Too many requests at once, please refresh the page to complete geocoding", 'avia_framework' ),
2412 'latitude' => __( "Latitude and longitude for", 'avia_framework' ),
2413 'notfound' => __( "couldn't be found by Google, please add them manually", 'avia_framework' ),
2414 'insertaddress' => __( "Please insert a valid address in the fields above", 'avia_framework' )
2415 );
2416
2417 if( $load_google_map_api )
2418 {
2419 wp_localize_script( 'avia-google-maps-api', 'AviaMapTranslation', $args );
2420 }
2421 }
2422 }
2423
2424 /**
2425 * Returns the js script
2426 *
2427 * @param string $lat
2428 * @param string $lng
2429 * @param string $zoom
2430 * @param string $type
2431 * @param string $content
2432 * @param string $directionsto
2433 * @param string $width
2434 * @param string $height
2435 * @param string $icon
2436 * @return string
2437 *
2438 * @deprecated since version 4.4 no longer needed
2439 */
2440 protected function print_map( $lat, $lng, $zoom, $type, $content, $directionsto, $width, $height, $icon )
2441 {
2442 global $avia_config;
2443
2444 _deprecated_function( 'print_map', '4.4', 'see class av_google_maps' );
2445
2446 $output = "";
2447 $unique = uniqid();
2448
2449 $prefix = isset($_SERVER['HTTPS'] ) ? "https" : "http";
2450 $width = ! empty( $width ) ? 'width:'.$width.';' : 'width:100%;';
2451 $height = ! empty( $height ) ? 'height:'.$height.';' : '';
2452
2453 $content = htmlspecialchars( $content, ENT_QUOTES );
2454 $content = str_replace( '<', '<', $content );
2455 $content = str_replace( '>', '>', $content );
2456 $content = str_replace( '"', '"', $content );
2457 $content = str_replace( ''', '"', $content );
2458 $content = json_encode( $content );
2459
2460
2461 $directionsForm = "";
2462 if( empty( $avia_config['g_maps_widget_active'] ) )
2463 {
2464 $avia_config['g_maps_widget_active'] = 0;
2465 }
2466
2467 if( apply_filters( 'avia_google_maps_widget_load_api', true, $avia_config[ 'g_maps_widget_active'] ) )
2468 {
2469 $api_key = avia_get_option('gmap_api');
2470 $api_url = av_google_maps::api_url( $api_key );
2471
2472 wp_register_script( 'avia-google-maps-api', $api_url, array( 'jquery' ), null, true );
2473 wp_enqueue_script( 'avia-google-maps-api' );
2474 }
2475
2476 $avia_config['g_maps_widget_active'] ++;
2477
2478 $output .= "<script type='text/javascript'>
2479 function makeMap_" . $avia_config['g_maps_widget_active'] . "() {\n";
2480
2481 $avia_maps_config = "
2482 var directionsDisplay;
2483 directionsDisplay = new google.maps.DirectionsRenderer;
2484 var directionsService = new google.maps.DirectionsService;
2485 var map;
2486 var latlng = new google.maps.LatLng(" . $lat . ", " . $lng . ");
2487 var directionsto = '" . $directionsto . "';
2488 var myOptions = {
2489 zoom:" . $zoom . ",
2490 mapTypeControl:true,
2491 mapTypeId:google.maps.MapTypeId." . $type . ",
2492 mapTypeControlOptions:{style:google.maps.MapTypeControlStyle.DROPDOWN_MENU},
2493 navigationControl:true,
2494 navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL},
2495 center:latlng
2496 };
2497 var map = new google.maps.Map(document.getElementById('avia_google_maps_$unique'), myOptions);
2498
2499 if(directionsto.length > 5)
2500 {
2501 directionsDisplay.setMap(map);
2502 var request = {
2503 origin:directionsto,
2504 destination:latlng,
2505 travelMode:google.maps.DirectionsTravelMode.DRIVING
2506 };
2507 directionsService.route(request, function(response, status) {
2508 if(status == google.maps.DirectionsStatus.OK) {
2509 directionsDisplay.setDirections(response)
2510 }
2511 })
2512 }
2513 else
2514 {
2515 var contentString = " . $content . ";
2516 var infowindow = new google.maps.InfoWindow({
2517 content: contentString
2518 });
2519 var marker = new google.maps.Marker({
2520 position: latlng,
2521 map: map,
2522 icon: '" . $icon . "',
2523 title: ''
2524 });
2525
2526 google.maps.event.addListener(marker, 'click', function() {
2527 infowindow.open(map,marker);
2528 });
2529 }";
2530
2531 $output .= apply_filters( 'avia_google_maps_widget_config', $avia_maps_config, $lat, $lng, $directionsto, $zoom, $type, $unique, $content, $icon );
2532
2533 $output .= "\n}\n\n
2534 jQuery(document).ready(function() {
2535 makeMap_" . $avia_config['g_maps_widget_active'] . "()
2536 });
2537 </script>
2538 <div id='avia_google_maps_$unique' style='$height $width' class='avia_google_map_container'></div>";
2539
2540 return $output;
2541 }
2542
2543
2544 } // SGMwidget widget
2545}
2546
2547
2548if( ! class_exists('avia_instagram_widget') )
2549{
2550 /**
2551 * Extended and improved version.
2552 * Adds a background caching of images on own server to avoid to access instagram to display the images
2553 *
2554 * @since 4.3.1
2555 * @by Günter
2556 */
2557 class avia_instagram_widget extends Avia_Widget
2558 {
2559
2560 /**
2561 *
2562 * @since 4.3.1
2563 * @var array
2564 */
2565 protected $upload_folders;
2566
2567 /**
2568 * Stores the expire time for cached images in seconds.
2569 * Do not make intervall too short to avoid unnecessary requests.
2570 * Also make it large enough to allow a complete update of all instances in that period.
2571 *
2572 * @since 4.3.1
2573 * @var int
2574 */
2575 protected $expire_time;
2576
2577 /**
2578 *
2579 * @since 4.3.1
2580 * @var boolean
2581 */
2582 protected $activate_cron;
2583
2584
2585 /**
2586 * Holds all caching info for each widget instance.
2587 *
2588 * @since 4.3.1
2589 * @var array
2590 */
2591 protected $cache;
2592
2593
2594 /**
2595 *
2596 * @since 4.3.1
2597 * @var array
2598 */
2599 protected $cached_file_sizes;
2600
2601
2602 /**
2603 *
2604 */
2605 public function __construct()
2606 {
2607 parent::__construct(
2608 'avia-instagram-feed',
2609 THEMENAME ." ". __( 'Instagram', 'avia_framework' ),
2610 array( 'classname' => 'avia-instagram-feed', 'description' => __( 'Displays your latest Instagram photos', 'avia_framework' ) )
2611 );
2612
2613 $this->upload_folders = wp_upload_dir();
2614
2615 if( is_ssl() )
2616 {
2617 $this->upload_folders['baseurl'] = str_replace( 'http://', 'https://', $this->upload_folders['baseurl'] );
2618 }
2619
2620 $folder = apply_filters( 'avf_instagram_cache_folder_name', 'avia_instagram_cache' );
2621
2622 $this->upload_folders['instagram_dir'] = trailingslashit( trailingslashit( $this->upload_folders['basedir'] ) . $folder );
2623 $this->upload_folders['instagram_url'] = trailingslashit( trailingslashit( $this->upload_folders['baseurl'] ) . $folder );
2624
2625 $this->expire_time = HOUR_IN_SECONDS * 2;
2626
2627 $this->expire_time = apply_filters_deprecated( 'null_instagram_cache_time', array( $this->expire_time ), '4.3.1', 'avf_instagram_file_cache_time', __( 'Adding possible file caching on server might need a longer period of time to invalidate cache.', 'avia_framework' ) );
2628 $this->expire_time = apply_filters( 'avf_instagram_file_cache_time', $this->expire_time );
2629
2630 $this->activate_cron = ! ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON );
2631 $this->activate_cron = apply_filters( 'avf_instagram_activate_cron', $this->activate_cron );
2632
2633 $this->cache = $this->get_cache();
2634
2635 $this->cached_file_sizes = array( 'thumbnail', 'small', 'large', 'original' );
2636
2637 /**
2638 * WP Cron job events
2639 */
2640 if( $this->activate_cron )
2641 {
2642 add_action( 'av_instagram_scheduled_filecheck', array( $this, 'handler_scheduled_filecheck' ), 10 );
2643 }
2644
2645 /**
2646 * Makes sure to keep cron job alive as fallback
2647 */
2648 if( is_admin() )
2649 {
2650 add_action( 'admin_init', array( $this, 'handler_init_filecheck' ), 99999 );
2651 add_action( 'delete_widget', array( $this, 'handler_delete_widget' ), 10, 3 );
2652 }
2653 else
2654 {
2655 add_action( 'init', array( $this, 'handler_init_filecheck' ), 99999 );
2656 }
2657
2658 }
2659
2660 /**
2661 *
2662 * @since 4.3.1
2663 */
2664 public function __destruct()
2665 {
2666 parent::__destruct();
2667
2668 unset( $this->upload_folders );
2669 unset( $this->cache );
2670 unset( $this->cached_file_sizes );
2671 }
2672
2673
2674 /**
2675 * Returns the cache info array
2676 *
2677 * @since 4.3.1
2678 * @return array
2679 */
2680 public function get_cache()
2681 {
2682 if( is_null( $this->cache ) )
2683 {
2684 $cache = get_option( 'avia_instagram_widgets_cache', '' );
2685
2686 /**
2687 * backwards comp only
2688 */
2689 if( is_array( $cache ) )
2690 {
2691 $this->cache = $cache;
2692 }
2693 else if( ! is_string( $cache ) || empty( $cache ) )
2694 {
2695 $this->cache = null;
2696 }
2697 else
2698 {
2699 $cache = json_decode( $cache, true );
2700 $this->cache = is_array( $cache ) ? $cache : null;
2701 }
2702
2703 if( empty( $this->cache ) )
2704 {
2705 $this->cache = array(
2706 'last_updated' => 0, // time() when last complete check has run
2707 'instances' => array()
2708 );
2709 }
2710 }
2711
2712 return $this->cache;
2713 }
2714
2715
2716 /**
2717 * Update the cache array in DB
2718 *
2719 * @since 4.3.1
2720 * @param array|null $cache
2721 */
2722 public function update_cache( array $cache = null )
2723 {
2724 if( ! is_null( $cache) )
2725 {
2726 $this->cache = $cache;
2727 }
2728
2729 $save = json_encode( $this->cache );
2730 update_option( 'avia_instagram_widgets_cache', $save );
2731 }
2732
2733
2734 /**
2735 * Ensure a valid instance array filled with defaults
2736 *
2737 * @since 4.3.1
2738 * @param array $instance
2739 * @return array
2740 */
2741 protected function parse_args_instance( array $instance )
2742 {
2743 $instance = wp_parse_args( $instance, array(
2744 'title' => __( 'Instagram', 'avia_framework' ),
2745 'username' => '',
2746 'cache' => apply_filters( 'avf_instagram_default_cache_location', '' ), // '' | 'server'
2747 'number' => 9,
2748 'columns' => 3,
2749 'size' => 'thumbnail',
2750 'target' => 'lightbox' ,
2751 'link' => __( 'Follow Me!', 'avia_framework' ),
2752 'avia_key' => ''
2753 )
2754 );
2755
2756 return $instance;
2757 }
2758
2759 /**
2760 * Ensure a valid instance array filled with defaults
2761 *
2762 * @since 4.3.1
2763 * @param array $instance_cache
2764 * @return array
2765 */
2766 protected function parse_args_instance_cache( array $instance_cache )
2767 {
2768 $instance_cache = wp_parse_args( (array) $instance_cache, array(
2769 'upload_folder' => '', // not the complete path, only the last folder name
2770 'path_error' => '', // Error message if upload_folder could not be created
2771 'instagram_error' => '',
2772 'upload_errors' => false, // number of errors found when caching files to show
2773 'last_update' => 0, // time() of last update
2774 'cached_list' => array(), // in the order how to display the files and file info on server
2775 'instagram_list' => array() // returned info from instagramm
2776 ));
2777
2778 return $instance_cache;
2779 }
2780
2781
2782 /**
2783 * Creates a unique key for the given instance for our cache array
2784 *
2785 * @since 4.3.1
2786 * @param array $instance
2787 * @param string $id_widget
2788 * @return string
2789 */
2790 protected function create_avia_key( array $instance, $id_widget )
2791 {
2792 $k = 0;
2793 $key = str_replace( $this->id_base . '-', '', $id_widget ) . '-' . AviaHelper::save_string( $instance['title'], '-' );
2794
2795 $orig_key = $key;
2796 while( array_key_exists( $key, $this->cache['instances'] ) )
2797 {
2798 $key = $orig_key . "-{$k}";
2799 $k++;
2800 }
2801
2802 return $key;
2803 }
2804
2805
2806 /**
2807 * Output the widget in frontend
2808 *
2809 * @param array $args
2810 * @param array $instance
2811 */
2812 public function widget( $args, $instance )
2813 {
2814 $instance = $this->parse_args_instance( $instance );
2815
2816 $fields = $this->get_field_names();
2817
2818 foreach( $instance as $key => $value )
2819 {
2820 if( in_array( $key, $fields ) )
2821 {
2822 $instance[ $key ] = esc_attr( $value );
2823 }
2824 }
2825
2826 extract( $args, EXTR_SKIP );
2827 extract( $instance, EXTR_SKIP );
2828
2829 /**
2830 * Allow to change the conditional display setting - e.g. if user is opt in and allows to connect directly
2831 *
2832 * @since 4.4
2833 * @param string $google_link '' | 'server'
2834 * @param string $context
2835 * @param mixed $object
2836 * @param array $args
2837 * @param array $instance
2838 * @return string
2839 */
2840 $original_cache = $cache;
2841 $cache = apply_filters( 'avf_conditional_setting_external_links', $cache, __CLASS__, $this, $args, $instance );
2842 if( ! in_array( $cache, array( '', 'server' ) ) )
2843 {
2844 $cache = $original_cache;
2845 }
2846
2847 $title = apply_filters( 'widget_title', $title, $args );
2848
2849 /**
2850 * Skip for non logged in users in frontend
2851 */
2852 if( ( trim( $username ) == '' ) && ! is_user_logged_in() && ! current_user_can( 'edit_posts' ) )
2853 {
2854 return;
2855 }
2856
2857 echo $before_widget;
2858
2859 if ( ! empty( $title ) )
2860 {
2861 echo $before_title . $title . $after_title;
2862 }
2863
2864 do_action( 'aviw_before_widget', $instance );
2865
2866 if( $username != '' )
2867 {
2868 $errors = array();
2869 $media_array = array();
2870
2871 $instance_cache = isset( $this->cache['instances'][ $instance['avia_key'] ] ) ? $this->cache['instances'][ $instance['avia_key'] ] : null;
2872
2873 if( ! is_null( $instance_cache ) )
2874 {
2875 if( ! empty( $instance_cache['instagram_error'] ) )
2876 {
2877 $errors = array( $instance_cache['instagram_error'] );
2878 }
2879 if( ! empty( $instance_cache['upload_errors'] ) && ( 'server' == $instance['cache'] ) )
2880 {
2881 foreach( $instance_cache['cached_list'] as $img )
2882 {
2883 if( ! empty( $img['errors'] ) )
2884 {
2885 $errors = array_merge( $errors, $img['errors'] );
2886 }
2887 }
2888 }
2889
2890 if( 'server' == $instance['cache'] )
2891 {
2892 $media_array = $instance_cache['cached_list'];
2893
2894 $url = trailingslashit( trailingslashit( $this->upload_folders['instagram_url'] ) . $instance_cache['upload_folder'] );
2895
2896 foreach( $media_array as $key => $media )
2897 {
2898 if( ! empty( $media['errors'] ) )
2899 {
2900 $errors = array_merge( $errors, $media['errors'] );
2901 }
2902
2903 if( ! empty( $media[ $size ] ) )
2904 {
2905 $media_array[ $key ][ $size ] = $url . $media[ $size ];
2906 }
2907 if( ! empty( $media[ 'original' ] ) )
2908 {
2909 $media_array[ $key ]['original'] = $url . $media['original'];
2910 }
2911 }
2912 }
2913 else
2914 {
2915 $media_array = $instance_cache['instagram_list'];
2916 }
2917 }
2918
2919 /**
2920 * Only show error messages to admins and authors
2921 */
2922 if( ! empty( $errors ) && is_user_logged_in() && current_user_can( 'edit_posts' ) )
2923 {
2924 $errors = array_map( 'esc_html__', $errors );
2925
2926 $out = '';
2927 $out .= '<div class="av-instagram-errors">';
2928
2929 $out .= '<p class="av-instagram-errors-msg av-instagram-admin">' . esc_html__( 'Only visible for admins:', 'avia_framework' ) . '</p>';
2930
2931 $out .= '<p class="av-instagram-errors-msg av-instagram-admin">';
2932 $out .= implode( '<br />', $errors );
2933 $out .= '</p>';
2934
2935 $out .= '</div>';
2936
2937 echo $out;
2938 }
2939
2940 if( count( $media_array ) > 0 )
2941 {
2942 // filters for custom classes
2943 $ulclass = esc_attr( apply_filters( 'aviw_list_class', 'av-instagram-pics av-instagram-size-' . $size ) );
2944 $rowclass = esc_attr( apply_filters( 'aviw_row_class', 'av-instagram-row' ) );
2945 $liclass = esc_attr( apply_filters( 'aviw_item_class', 'av-instagram-item' ) );
2946 $aclass = esc_attr( apply_filters( 'aviw_a_class', '' ) );
2947 $imgclass = esc_attr( apply_filters( 'aviw_img_class', '' ) );
2948
2949 ?><div class="<?php echo esc_attr( $ulclass ); ?>"><?php
2950
2951 $last_id = end( $media_array );
2952 $last_id = $last_id['id'];
2953
2954 $rowcount = 0;
2955 $itemcount = 0;
2956 foreach ( $media_array as $item )
2957 {
2958 if( empty( $item[ $size ] ) )
2959 {
2960 continue;
2961 }
2962
2963 if( $rowcount == 0 )
2964 {
2965 echo "<div class='{$rowclass}'>";
2966 }
2967
2968 $rowcount ++;
2969 $itemcount ++;
2970
2971 $targeting = $target;
2972 if( $target == "lightbox" )
2973 {
2974 $targeting = "";
2975 $item['link'] = ! empty( $item['original'] ) ? $item['original'] : $item[ $size ];
2976 }
2977
2978 echo '<div class="' . $liclass . '">';
2979 echo '<a href="' . esc_url( $item['link'] ) . '" target="' . esc_attr( $targeting ) . '" class="' . $aclass . ' ' . $imgclass . '" title="' . esc_attr( $item['description'] ) . '" style="background-image:url(' . esc_url( $item[ $size ] ) . ');">';
2980 echo '</a></div>';
2981
2982 if( $rowcount % $columns == 0 || $last_id == $item['id'] || ( $itemcount >= $number ) )
2983 {
2984 echo '</div>';
2985 $rowcount = 0;
2986
2987 if( $itemcount >= $number )
2988 {
2989 break;
2990 }
2991 }
2992 }
2993 echo '</div>';
2994 }
2995 else
2996 {
2997 echo '<p class="av-instagram-errors-msg">' . esc_html__( 'No images available at the moment', 'avia_framework' ) . '</p>';
2998 }
2999 }
3000 else
3001 {
3002 echo '<p class="av-instagram-errors-msg av-instagram-admin">' . esc_html__( 'For admins only: Missing intagram user name !!', 'avia_framework' ) . '</p>';
3003 }
3004
3005 if ( $link != '' )
3006 {
3007 ?>
3008 <a class="av-instagram-follow avia-button" href="https://instagram.com/<?php echo esc_attr( trim( $username ) ); ?>" rel="me" target="<?php echo esc_attr( $target ); ?>"><?php echo $link; ?></a><?php
3009 }
3010
3011 do_action( 'aviw_after_widget', $instance );
3012
3013 echo $after_widget;
3014 }
3015
3016
3017 /**
3018 * Output the form in backend
3019 *
3020 * @param array $instance
3021 */
3022 public function form( $instance )
3023 {
3024 $instance = $this->parse_args_instance( $instance );
3025 $fields = $this->get_field_names();
3026
3027 foreach( $instance as $key => $value )
3028 {
3029 if( in_array( $key, $fields ) )
3030 {
3031 switch( $key )
3032 {
3033 case 'number':
3034 case 'columns':
3035 $instance[ $key ] = absint( $value );
3036 break;
3037 default:
3038 $instance[ $key ] = esc_attr( $value );
3039 break;
3040 }
3041 }
3042 }
3043
3044 extract( $instance );
3045
3046 ?>
3047 <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title', 'avia_framework' ); ?>: <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
3048 <p><label for="<?php echo $this->get_field_id( 'username' ); ?>"><?php _e( 'Username', 'avia_framework' ); ?>: <input class="widefat" id="<?php echo $this->get_field_id( 'username' ); ?>" name="<?php echo $this->get_field_name( 'username' ); ?>" type="text" value="<?php echo $username; ?>" /></label></p>
3049 <p><label for="<?php echo $this->get_field_id( 'cache' ); ?>"><?php _e( 'Location of your photos or videos', 'avia_framework' ); ?>:</label>
3050 <select id="<?php echo $this->get_field_id( 'cache' ); ?>" name="<?php echo $this->get_field_name( 'cache' ); ?>" class="widefat">
3051 <option value="" <?php selected( '', $cache ) ?>><?php _e( 'Get from your instagram account (instagram server connection needed)', 'avia_framework' ); ?></option>
3052 <option value="server" <?php selected( 'server', $cache ) ?>><?php _e( 'Cache on your server - no instagram connection needed on pageload', 'avia_framework' ); ?></option>
3053 </select>
3054 </p>
3055 <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of photos', 'avia_framework' ); ?>:</label>
3056 <select id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" class="widefat">
3057 <option value="1" <?php selected( 1, $number ) ?>>1</option>
3058 <option value="2" <?php selected( 2, $number ) ?>>2</option>
3059 <option value="3" <?php selected( 3, $number ) ?>>3</option>
3060 <option value="4" <?php selected( 4, $number ) ?>>4</option>
3061 <option value="5" <?php selected( 5, $number ) ?>>5</option>
3062 <option value="6" <?php selected( 6, $number ) ?>>6</option>
3063 <option value="7" <?php selected( 7, $number ) ?>>7</option>
3064 <option value="8" <?php selected( 8, $number ) ?>>8</option>
3065 <option value="9" <?php selected( 9, $number ) ?>>9</option>
3066 <option value="10" <?php selected( 10, $number ) ?>>10</option>
3067 <option value="11" <?php selected( 11, $number ) ?>>11</option>
3068 <option value="12" <?php selected( 12, $number ) ?>>12</option>
3069 </select>
3070 </p>
3071 <p><label for="<?php echo $this->get_field_id( 'columns' ); ?>"><?php _e( 'Number of columns', 'avia_framework' ); ?>:</label>
3072 <select id="<?php echo $this->get_field_id( 'columns' ); ?>" name="<?php echo $this->get_field_name( 'columns' ); ?>" class="widefat">
3073 <option value="1" <?php selected( 1, $columns ) ?>>1</option>
3074 <option value="2" <?php selected( 2, $columns ) ?>>2</option>
3075 <option value="3" <?php selected( 3, $columns ) ?>>3</option>
3076 <option value="4" <?php selected( 4, $columns ) ?>>4</option>
3077 <option value="5" <?php selected( 5, $columns ) ?>>5</option>
3078 <option value="6" <?php selected( 6, $columns ) ?>>6</option>
3079 </select>
3080 </p>
3081 <p><label for="<?php echo $this->get_field_id( 'size' ); ?>"><?php _e( 'Thumbnail size', 'avia_framework' ); ?>:</label>
3082 <select id="<?php echo $this->get_field_id( 'size' ); ?>" name="<?php echo $this->get_field_name( 'size' ); ?>" class="widefat">
3083 <option value="thumbnail" <?php selected( 'thumbnail', $size ) ?>><?php _e( 'Thumbnail', 'avia_framework' ); ?></option>
3084 <option value="small" <?php selected( 'small', $size ) ?>><?php _e( 'Small', 'avia_framework' ); ?></option>
3085 <option value="large" <?php selected( 'large', $size ) ?>><?php _e( 'Large', 'avia_framework' ); ?></option>
3086 <option value="original" <?php selected( 'original', $size ) ?>><?php _e( 'Original', 'avia_framework' ); ?></option>
3087 </select>
3088 </p>
3089 <p><label for="<?php echo $this->get_field_id( 'target' ); ?>"><?php _e( 'Open links in', 'avia_framework' ); ?>:</label>
3090 <select id="<?php echo $this->get_field_id( 'target' ); ?>" name="<?php echo $this->get_field_name( 'target' ); ?>" class="widefat">
3091 <option value="lightbox" <?php selected( 'lightbox', $target ) ?>><?php _e( 'Lightbox', 'avia_framework' ); ?></option>
3092 <option value="_self" <?php selected( '_self', $target ) ?>><?php _e( 'Current window (_self)', 'avia_framework' ); ?></option>
3093 <option value="_blank" <?php selected( '_blank', $target ) ?>><?php _e( 'New window (_blank)', 'avia_framework' ); ?></option>
3094 </select>
3095 </p>
3096 <p><label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e( 'Link text', 'avia_framework' ); ?>: <input class="widefat" id="<?php echo $this->get_field_id( 'link' ); ?>" name="<?php echo $this->get_field_name( 'link' ); ?>" type="text" value="<?php echo $link; ?>" /></label></p>
3097
3098 <?php
3099
3100 if( ! $this->activate_cron )
3101 {
3102 echo '<p class="av-instagram-no-cron">';
3103 echo __( 'WP Cron jobs are disabled. To assure a regular update of cached data and an optimal pageload in frontend and backend we recommend to activate this.', 'avia_framework' );
3104 echo '</p>';
3105
3106 $timestamp = ( $this->cache['last_updated'] != 0 ) ? $this->cache['last_updated'] + $this->expire_time : false;
3107 $time = ( false !== $timestamp ) ? date( 'Y/m/d H:i a', $timestamp ) . __( ' UTC', 'avia_framework' ) : __( 'No time available', 'avia_framework' );
3108
3109 echo '<p class="av-instagram-next-update">';
3110 echo __( 'The widget preloads and caches Instagram data for better performance.', 'avia_framework' )." ";
3111 echo sprintf( __( 'Next update: %s', 'avia_framework' ), $time );
3112 echo '</p>';
3113 }
3114 else
3115 {
3116 $timestamp = wp_next_scheduled( 'av_instagram_scheduled_filecheck' );
3117 $time = ( false !== $timestamp ) ? date( "Y/m/d H:i", $timestamp ) . __( ' UTC', 'avia_framework' ) : __( 'No time available', 'avia_framework' );
3118
3119 echo '<p class="av-instagram-next-update">';
3120 echo __( 'The widget preloads and caches Instagram data for better performance.', 'avia_framework' )." ";
3121 echo sprintf( __( 'Next update: %s', 'avia_framework' ), $time );
3122 echo '</p>';
3123 }
3124
3125 if( empty( $instance['avia_key'] ) )
3126 {
3127 return;
3128 }
3129
3130 if( empty( $this->cache['instances'][ $instance['avia_key'] ] ) )
3131 {
3132 return;
3133 }
3134
3135 $instance_cache = $this->cache['instances'][ $instance['avia_key'] ];
3136 $errors = array();
3137
3138 if( ! empty( $instance_cache['instagram_error'] ) )
3139 {
3140 $errors = (array) $instance_cache['instagram_error'];
3141 }
3142
3143 if( 'server' == $instance['cache'] )
3144 {
3145 foreach( $instance_cache['cached_list'] as $image )
3146 {
3147 if( ! empty( $image['errors'] ) )
3148 {
3149 $errors = array_merge( $errors, $image['errors'] );
3150 }
3151 }
3152 }
3153
3154 if( ! empty( $errors ) )
3155 {
3156 $errors = array_map( 'esc_html__', $errors );
3157
3158 $out = '<div class="av-instagram-errors">';
3159
3160 $out .= '<p class="av-instagram-errors-msg av-instagram-error-headline">' . esc_html__( 'Errors found:', 'avia_framework' ) . '</p>';
3161
3162 $out .= '<p class="av-instagram-errors-msg">';
3163 $out .= implode( '<br />', $errors );
3164 $out .= '</p>';
3165
3166 $out .= '</div>';
3167
3168 echo $out;
3169 }
3170
3171 }
3172
3173 /**
3174 * Update the form data
3175 *
3176 * @param array $new_instance
3177 * @param array $old_instance
3178 * @return array
3179 */
3180 public function update( $new_instance, $old_instance )
3181 {
3182 $instance = $this->parse_args_instance( $old_instance );
3183
3184 $instance['title'] = strip_tags( $new_instance['title'] );
3185 $instance['username'] = trim( strip_tags( $new_instance['username'] ) );
3186 $instance['cache'] = ( $new_instance['cache'] == 'server' || $new_instance['cache'] == '' ) ? $new_instance['cache'] : apply_filters( 'avf_instagram_default_cache_location', 'server' );
3187 $instance['number'] = ! absint( $new_instance['number'] ) ? 9 : $new_instance['number'];
3188 $instance['columns'] = ! absint( $new_instance['columns'] ) ? 3 : $new_instance['columns'];
3189 $instance['size'] = ( $new_instance['size'] == 'thumbnail' || $new_instance['size'] == 'large' || $new_instance['size'] == 'small' || $new_instance['size'] == 'original' ) ? $new_instance['size'] : 'large';
3190 $instance['target'] = ( $new_instance['target'] == '_self' || $new_instance['target'] == '_blank'|| $new_instance['target'] == 'lightbox' ) ? $new_instance['target'] : '_self';
3191 $instance['link'] = strip_tags( $new_instance['link'] );
3192
3193
3194 /**
3195 * We have a new widget (or an existing from an older theme version)
3196 */
3197 if( empty( $instance['avia_key'] ) )
3198 {
3199 $key = $this->create_avia_key( $instance, $this->id );
3200 $instance['avia_key'] = $key;
3201 $this->cache['instances'][ $key ] = array();
3202 $this->update_cache();
3203 }
3204
3205 $this->update_single_instance( $instance, $this->id );
3206
3207 if( $this->activate_cron )
3208 {
3209 $this->restart_cron_job();
3210 }
3211
3212 return $instance;
3213 }
3214
3215
3216 /**
3217 * Get info from instagram
3218 * based on https://gist.github.com/cosmocatalano/4544576
3219 *
3220 * @param string $username
3221 *
3222 * @return array|\WP_Error
3223 */
3224 protected function scrape_instagram( $username )
3225 {
3226 $username = strtolower( $username );
3227 $username = str_replace( '@', '', $username );
3228
3229 $remote = wp_remote_get( 'https://www.instagram.com/' . trim( $username ), array( 'sslverify' => false, 'timeout' => 60 ) );
3230
3231 if ( is_wp_error( $remote ) )
3232 {
3233 return new WP_Error( 'site_down', __( 'Unable to communicate with Instagram.', 'avia_framework' ) );
3234 }
3235
3236 $code = wp_remote_retrieve_response_code( $remote );
3237 if ( 200 != $code )
3238 {
3239 $msg = wp_remote_retrieve_response_message( $remote );
3240 if( empty( $msg ) )
3241 {
3242 $msg = __( 'Unknown error code', 'avia_framework' );
3243 }
3244 return new WP_Error( 'invalid_response', sprintf( __( 'Instagram returned error %d (= %s).', 'avia_framework' ), $code, $msg ) );
3245 }
3246
3247 $shards = explode( 'window._sharedData = ', $remote['body'] );
3248 $insta_json = explode( ';</script>', $shards[1] );
3249 $insta_array = json_decode( $insta_json[0], true );
3250
3251 if ( ! $insta_array )
3252 {
3253 return new WP_Error( 'bad_json', __( 'Instagram has returned invalid data.', 'avia_framework' ) );
3254 }
3255
3256 if ( isset( $insta_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'] ) )
3257 {
3258 $images = $insta_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'];
3259 }
3260 else
3261 {
3262 return new WP_Error( 'bad_json_2', __( 'Instagram has returned invalid data.', 'avia_framework' ) );
3263 }
3264
3265 if ( ! is_array( $images ) )
3266 {
3267 return new WP_Error( 'bad_array', __( 'Instagram has returned invalid data.', 'avia_framework' ) );
3268 }
3269
3270 $instagram = array();
3271
3272 foreach ( $images as $image )
3273 {
3274 // see https://github.com/stevenschobert/instafeed.js/issues/549
3275 if ( $image['node']['is_video'] == true )
3276 {
3277 $type = 'video';
3278 }
3279 else
3280 {
3281 $type = 'image';
3282 }
3283
3284 $caption = __( 'Instagram Image', 'avia_framework' );
3285
3286 if ( ! empty( $image['node']['edge_media_to_caption']['edges'][0]['node']['text'] ) )
3287 {
3288 $caption = wp_kses( $image['node']['edge_media_to_caption']['edges'][0]['node']['text'], array() );
3289 }
3290
3291 $instagram[] = array(
3292 'description' => $caption,
3293 'link' => trailingslashit( '//instagram.com/p/' . $image['node']['shortcode'] ),
3294 'time' => $image['node']['taken_at_timestamp'],
3295 'comments' => $image['node']['edge_media_to_comment']['count'],
3296 'likes' => $image['node']['edge_liked_by']['count'],
3297 'thumbnail' => preg_replace( '/^https?\:/i', '', $image['node']['thumbnail_resources'][0]['src'] ),
3298 'small' => preg_replace( '/^https?\:/i', '', $image['node']['thumbnail_resources'][2]['src'] ),
3299 'large' => preg_replace( '/^https?\:/i', '', $image['node']['thumbnail_resources'][4]['src'] ),
3300 'original' => preg_replace( '/^https?\:/i', '', $image['node']['display_url'] ),
3301 'type' => $type,
3302 'id' => $image['node']['id']
3303 );
3304 }
3305
3306 $aviw_images_only = false;
3307 $aviw_images_only = apply_filters_deprecated( 'aviw_images_only', array( $aviw_images_only ), '4.3.1', 'avf_instagram_filter_files', __( 'Filter extended to filter images or videos', 'avia_framework' ) );
3308
3309 /**
3310 * Filter which type of elements will be displayed.
3311 * Return an empty array to show all files.
3312 *
3313 * Possible values: 'video' | 'image'
3314 *
3315 * @since 4.3.1
3316 * @return array
3317 */
3318 $show = $aviw_images_only ? array( 'image' ) : array();
3319 $show = apply_filters( 'avf_instagram_filter_files', $show, $username );
3320
3321 if( ! empty( $show ) )
3322 {
3323 foreach( $instagram as $key => $media_item )
3324 {
3325 if( ! in_array( $media_item['type'], $show ) )
3326 {
3327 unset( $instagram[ $key ] );
3328 }
3329 }
3330
3331 $instagram = array_merge( $instagram );
3332 }
3333
3334 if ( empty( $instagram ) )
3335 {
3336 return new WP_Error( 'no_images', __( 'Instagram did not return any images.', 'avia_framework' ) );
3337 }
3338
3339 return $instagram;
3340 }
3341
3342
3343 /**
3344 * WP Cron handler for background uploads
3345 *
3346 * @since 4.3.1
3347 */
3348 public function handler_scheduled_filecheck()
3349 {
3350 if( defined( 'WP_DEBUG ') && WP_DEBUG )
3351 {
3352 error_log( '****************** In avia_instagram_widget::handler_scheduled_filecheck started' );
3353 }
3354
3355 /**
3356 * Create a scheduled event to prevent double checks running on parallel pageloads
3357 */
3358 $this->schedule_cron_job( $this->expire_time * 2 );
3359
3360 $settings = $this->get_settings();
3361 if( ! empty( $settings ) )
3362 {
3363 $this->check_all_instances();
3364 }
3365
3366 $this->schedule_cron_job( $this->expire_time * 2 );
3367
3368 $this->sync_data();
3369
3370 $this->schedule_cron_job( $this->expire_time );
3371
3372 if( defined( 'WP_DEBUG ') && WP_DEBUG )
3373 {
3374 error_log( '****************** In avia_instagram_widget::handler_scheduled_filecheck ended' );
3375 }
3376 }
3377
3378
3379 /**
3380 * Synchronises directory and cache data structure.
3381 * It might happen, that the update cronjob is running and user removes the last widget.
3382 * This leads to an inconsistent cache and directory structure.
3383 *
3384 * As user might have added new widgets again we have to sync cache with latest settings
3385 *
3386 * @since 4.3.1
3387 */
3388 public function sync_data()
3389 {
3390 if( defined( 'WP_DEBUG ') && WP_DEBUG )
3391 {
3392 error_log( '****************** In avia_instagram_widget::sync_data started' );
3393 }
3394
3395 $settings = $this->get_settings();
3396
3397 if( empty( $settings ) && empty( $this->cache['instances'] ) )
3398 {
3399 if( is_dir( $this->upload_folders['instagram_dir'] ) )
3400 {
3401 avia_backend_delete_folder( $this->upload_folders['instagram_dir'] );
3402 $this->cache['last_updated'] = time();
3403 $this->update_cache();
3404 }
3405 return;
3406 }
3407
3408 $instance_infos = (array) $this->cache['instances'];
3409
3410 /**
3411 * Remove all entries from cache that have no more entry in settings
3412 */
3413 $keys = array_keys( $instance_infos );
3414 $keys_to_keep = array();
3415
3416 foreach ( $settings as $index => $setting )
3417 {
3418 if( in_array( $setting['avia_key'], $keys ) )
3419 {
3420 $keys_to_keep[] = $setting['avia_key'];
3421 }
3422 }
3423
3424 $keys_to_remove = array_diff( $keys, $keys_to_keep );
3425
3426 foreach( $keys_to_remove as $key )
3427 {
3428 $folder = $this->upload_folders['instagram_dir'] . $instance_infos[ $key ]['upload_folder'];
3429 avia_backend_delete_folder( $folder );
3430 unset( $this->cache['instances'][ $key ] );
3431 }
3432
3433 /**
3434 * Now we check that all directories belong to a cache entry
3435 */
3436 $cache_dirs = is_dir( $this->upload_folders['instagram_dir'] ) ? scandir( $this->upload_folders['instagram_dir'] ) : false;
3437 if( ! is_array( $cache_dirs ) )
3438 {
3439 /**
3440 * Something went wrong reading directory - folder does not exist, access denied, .....
3441 * There is nothing we can do.
3442 */
3443 return;
3444 }
3445
3446 $cache_dirs = array_diff( $cache_dirs, array( '.', '..' ) );
3447
3448 $ref_dirs = array();
3449 foreach( $this->cache['instances'] as $key => $instance_info )
3450 {
3451 if( isset( $instance_info['upload_folder'] ) )
3452 {
3453 $ref_dirs[] = $instance_info['upload_folder'];
3454 }
3455 }
3456
3457 $remove_dirs = array_diff( $cache_dirs, $ref_dirs );
3458
3459 foreach( $remove_dirs as $remove_dir )
3460 {
3461 avia_backend_delete_folder( $this->upload_folders['instagram_dir'] . $remove_dir );
3462 }
3463
3464
3465 if( empty( $this->cache['instances'] ) )
3466 {
3467 avia_backend_delete_folder( $this->upload_folders['instagram_dir'] );
3468 }
3469
3470 $this->cache['last_updated'] = time();
3471 $this->update_cache();
3472 }
3473
3474 /**
3475 * WP Cron is disabled - we have to load files during pageload in admin area
3476 *
3477 * @since 4.3.1
3478 */
3479 public function handler_init_filecheck()
3480 {
3481 $settings = $this->get_settings();
3482 if( empty( $settings ) )
3483 {
3484 /**
3485 * Keep alive to allow to clean up in case when deleting a widget and check_all_instances() have run at same time.
3486 * Due to internal WP caching this might have lead to inconsistent data structure.
3487 */
3488 if( $this->activate_cron )
3489 {
3490 $this->restart_cron_job();
3491 }
3492 return;
3493 }
3494
3495 /**
3496 * Fallback on version update - we need to switch to new data structure
3497 * Can be removed in very very future versions.
3498 *
3499 * @since 4.3.1
3500 */
3501 $instance = array_shift( $settings );
3502 if( ! isset( $instance['avia_key'] ) || empty( $instance['avia_key'] ) )
3503 {
3504 $instances = $this->get_settings();
3505 foreach( $instances as $key => &$instance )
3506 {
3507 $key = $this->create_avia_key( $instance, $this->id_base . "-{$key}" );
3508 $instance['avia_key'] = $key;
3509 $this->cache['instances'][ $key ] = array();
3510 }
3511 unset( $instance );
3512 $this->save_settings( $instances );
3513
3514 $this->cache['last_updated'] = 0;
3515 $this->update_cache();
3516
3517 $this->check_all_instances();
3518 }
3519
3520 if( $this->activate_cron )
3521 {
3522 $this->restart_cron_job();
3523 return;
3524 }
3525
3526 /**
3527 * Check if we need to run an update
3528 */
3529 if( $this->cache['last_updated'] + $this->expire_time > time() )
3530 {
3531 return;
3532 }
3533
3534 /**
3535 * Only run update in backend
3536 */
3537 if( is_admin() )
3538 {
3539 $this->check_all_instances();
3540 }
3541 }
3542
3543
3544 /**
3545 * Is called, when an instance of a widget is deleted - Both from active sidebars or inactive widget area.
3546 *
3547 * @since 4.3.1
3548 * @param string $widget_id
3549 * @param string $sidebar_id
3550 * @param string $id_base
3551 */
3552 public function handler_delete_widget( $widget_id, $sidebar_id, $id_base )
3553 {
3554 $id = str_replace( $id_base . '-', '', $widget_id );
3555
3556 $settings = $this->get_settings();
3557 if( empty( $settings ) || empty( $settings[ $id ] ) )
3558 {
3559 return;
3560 }
3561
3562 $instance = $settings[ $id ];
3563
3564 $instance_info = isset( $this->cache['instances'][ $instance['avia_key'] ] ) ? $this->cache['instances'][ $instance['avia_key'] ] : array();
3565 if( empty( $instance_info ) )
3566 {
3567 return;
3568 }
3569
3570 $instance = $this->parse_args_instance( $instance );
3571 $instance_info = $this->parse_args_instance_cache( $instance_info );
3572
3573 if( count( $settings ) <= 1 )
3574 {
3575 avia_backend_delete_folder( $this->upload_folders['instagram_dir'] );
3576 $this->cache['instances'] = array();
3577 }
3578 else
3579 {
3580 $folder = $this->upload_folders['instagram_dir'] . $instance_info['upload_folder'];
3581 avia_backend_delete_folder( $folder );
3582 unset( $this->cache['instances'][ $instance['avia_key'] ] );
3583 }
3584
3585 $this->update_cache();
3586 }
3587
3588
3589 /**
3590 * This is a fallback function to ensure that the cron job is running
3591 *
3592 * @since 4.3.1
3593 */
3594 protected function restart_cron_job()
3595 {
3596 $timestamp = wp_next_scheduled( 'av_instagram_scheduled_filecheck' );
3597 if( false === $timestamp )
3598 {
3599 $this->schedule_cron_job( $this->expire_time );
3600 return;
3601 }
3602
3603 /**
3604 * This is a fallback to prevent a blocking of updates
3605 */
3606 if( $timestamp > ( time() + $this->expire_time * 2 ) )
3607 {
3608 $this->schedule_cron_job( $this->expire_time * 2 );
3609 }
3610 }
3611
3612 /**
3613 * Removes an existing cron job and creates a new one
3614 *
3615 * @since 4.3.1
3616 * @param int $delay_seconds
3617 * @return boolean
3618 */
3619 protected function schedule_cron_job( $delay_seconds = 0 )
3620 {
3621 $timestamp = wp_next_scheduled( 'av_instagram_scheduled_filecheck' );
3622 if( false !== $timestamp )
3623 {
3624 wp_unschedule_hook( 'av_instagram_scheduled_filecheck' );
3625 }
3626
3627 $timestamp = time() + $delay_seconds;
3628
3629 $scheduled = wp_schedule_single_event( $timestamp, 'av_instagram_scheduled_filecheck' );
3630
3631 return false !== $scheduled;
3632 }
3633
3634
3635 /**
3636 * Scan all instances of this widget and update cache data
3637 *
3638 * @since 4.3.1
3639 */
3640 protected function check_all_instances()
3641 {
3642 $settings = $this->get_settings();
3643
3644 foreach ( $settings as $key => $instance )
3645 {
3646 $id_widget = $this->id_base . "-{$key}";
3647
3648 if( false === is_active_widget( false, $id_widget, $this->id_base, false ) )
3649 {
3650 continue;
3651 }
3652
3653 $this->update_single_instance( $instance, $id_widget );
3654 }
3655
3656 $this->cache['last_updated'] = time();
3657 $this->update_cache();
3658 }
3659
3660
3661 /**
3662 * Updates the cache for the given instance.
3663 * As a fallback for older versions the instance is updated and returned.
3664 *
3665 * @since 4.3.1
3666 * @param array $instance
3667 * @param string $id_widget
3668 * @return array
3669 */
3670 protected function update_single_instance( array $instance, $id_widget )
3671 {
3672 set_time_limit( 0 );
3673
3674 $instance = $this->parse_args_instance( $instance );
3675
3676 /**
3677 * Fallback for old versions - update to new datastructure
3678 */
3679 if( empty( $instance['avia_key'] ) )
3680 {
3681 $key = $this->create_avia_key( $instance, $id_widget );
3682 $instance['avia_key'] = $key;
3683 $this->cache['instances'][ $key ] = array();
3684 }
3685
3686 $instance_cache = isset( $this->cache['instances'][ $instance['avia_key'] ] ) ? $this->cache['instances'][ $instance['avia_key'] ] : array();
3687 $instance_cache = $this->parse_args_instance_cache( $instance_cache );
3688
3689 /**
3690 * Create upload directory if not exist. Upload directory will be deleted when widget instance is removed.
3691 */
3692 if( ( 'server' == $instance['cache'] ) && empty( $instance_cache['upload_folder'] ) && ! empty( $instance['username'] ) )
3693 {
3694 $id = str_replace( $this->id_base . '-', '', $id_widget );
3695 $f = empty( $instance['title'] ) ? $instance['username'] : $instance['title'];
3696 $folder_name = substr( AviaHelper::save_string( $id . '-' . $f, '-' ), 0, 30 );
3697 $folder = $this->upload_folders['instagram_dir'] . $folder_name;
3698
3699 $created = avia_backend_create_folder( $folder, false, 'unique' );
3700 if( $created )
3701 {
3702 $split = pathinfo( $folder );
3703 $instance_cache['upload_folder'] = $split['filename'];
3704 $instance_cache['path_error'] = '';
3705 $instance_cache['cached_list'] = array();
3706 }
3707 else
3708 {
3709 $instance_cache['path_error'] = sprintf( __( 'Unable to create cache folder "%s". Files will be loaded directly from instagram', 'avia_framework' ), $folder );
3710 }
3711 }
3712
3713 $username = $instance['username'];
3714 $number = $instance['number'];
3715
3716 if( ! empty( $username) )
3717 {
3718 $media_array = $this->scrape_instagram( $username );
3719
3720 if ( ! is_wp_error( $media_array ) )
3721 {
3722 $instance_cache['instagram_error'] = '';
3723 $instance_cache['instagram_list'] = array_slice( $media_array, 0, $number );
3724
3725 if( 'server' == $instance['cache'] )
3726 {
3727 $instance_cache = $this->cache_files_in_upload_directory( $media_array, $instance, $instance_cache );
3728 }
3729 }
3730 else
3731 {
3732 /**
3733 * We only store error message but keep existing files for fallback so we do not break widget
3734 */
3735 $instance_cache['instagram_error'] = $media_array->get_error_message();
3736 }
3737 }
3738 else
3739 {
3740 $instance_cache['instagram_error'] = __( 'You need to specify an Instagram username.', 'avia_framework' );
3741 $instance_cache['instagram_list'] = array();
3742 $instance_cache['cached_list'] = array();
3743 }
3744
3745 $instance_cache['last_update'] = time();
3746
3747 $this->cache['instances'][ $instance['avia_key'] ] = $instance_cache;
3748 $this->update_cache();
3749
3750 return $instance;
3751 }
3752
3753
3754 /**
3755 * Updates the local stored files in upload directory
3756 * Already downloaded files are not updated.
3757 * If an error occurs, we try to download more files as fallback to provide requested number of files
3758 * in frontend.
3759 *
3760 * No longer needed files are removed from cache.
3761 *
3762 * @since 4.3.1
3763 * @param array $instagram_files
3764 * @param array $instance
3765 * @param array $instance_cache
3766 * @return array
3767 */
3768 protected function cache_files_in_upload_directory( array $instagram_files, array $instance, array $instance_cache )
3769 {
3770 set_time_limit( 0 );
3771
3772 $cached_files = $instance_cache['cached_list'];
3773
3774 $new_cached_files = array();
3775 $no_errors = 0;
3776
3777 foreach( $instagram_files as $instagram_file )
3778 {
3779 $id = $instagram_file['id'];
3780
3781 $found = false;
3782 foreach( $cached_files as $key_cache => $cached_file )
3783 {
3784 if( $id == $cached_file['id'] )
3785 {
3786 /**
3787 * If an error occurred in a previous file load we try to reload all files again
3788 */
3789 if( ! empty( $cached_file['errors'] ) )
3790 {
3791 $this->remove_single_cached_files( $cached_file, $instance_cache );
3792 unset( $cached_files[ $key_cache ] );
3793 break;
3794 }
3795
3796 /**
3797 * As a fallback (or if other sizes were added later) we check if the cached files exist
3798 */
3799 $path = trailingslashit( $this->upload_folders['instagram_dir'] . $instance_cache['upload_folder'] );
3800 foreach( $this->cached_file_sizes as $size )
3801 {
3802 if( empty( $cached_file[ $size ] ) || ! file_exists( $path . $cached_file[ $size ] ) )
3803 {
3804 $this->remove_single_cached_files( $cached_file, $instance_cache );
3805 unset( $cached_files[ $key_cache ] );
3806 break;
3807 }
3808 }
3809
3810 if( ! isset( $cached_files[ $key_cache ] ) )
3811 {
3812 break;
3813 }
3814
3815 $ncf = $cached_file;
3816
3817 $ncf['description'] = $instagram_file['description'];
3818 $ncf['link'] = $instagram_file['link'];
3819 $ncf['time'] = $instagram_file['time'];
3820 $ncf['comments'] = $instagram_file['comments'];
3821 $ncf['likes'] = $instagram_file['likes'];
3822 $ncf['type'] = $instagram_file['type'];
3823
3824 $new_cached_files[] = $ncf;
3825
3826 unset( $cached_files[ $key_cache ] );
3827 $found = true;
3828 break;
3829 }
3830 }
3831
3832 if( ! $found )
3833 {
3834 $new_cached_files[] = $this->download_from_instagram( $instagram_file, $instance, $instance_cache );
3835 }
3836
3837 $last = $new_cached_files[ count( $new_cached_files ) - 1 ];
3838
3839 /**
3840 * Check if we could cache the file in requested size - we might have got a warning from chmod
3841 */
3842 if( empty( $last['errors'] ) || ! empty( $last[ $instance['size'] ] ) )
3843 {
3844 $no_errors++;
3845 }
3846
3847 /**
3848 * Also break if we get too many errors
3849 */
3850 if( $no_errors >= $instance['number'] || count( $new_cached_files ) > ( $instance['number'] * 2 ) )
3851 {
3852 break;
3853 }
3854 }
3855
3856 /**
3857 * Now we add all remaining cached files to fill up requested number of files
3858 */
3859 if( $no_errors < $instance['number'] )
3860 {
3861 foreach( $cached_files as $key_cache => $cached_file )
3862 {
3863 $new_cached_files[] = $cached_file;
3864 if( empty( $cached_file['errors'] ) )
3865 {
3866 $no_errors++;
3867 }
3868
3869 unset( $cached_files[ $key_cache ] );
3870
3871 if( $no_errors >= $instance['number'] )
3872 {
3873 break;
3874 }
3875 }
3876 }
3877
3878 /**
3879 * Now we delete no longer needed files
3880 */
3881 foreach( $cached_files as $key_cache => $cached_file )
3882 {
3883 $this->remove_single_cached_files( $cached_file, $instance_cache );
3884 unset( $cached_files[ $key_cache ] );
3885 }
3886
3887 /**
3888 * Save results and count errors
3889 */
3890 $err_cnt = 0;
3891 $count = 1;
3892
3893 foreach( $new_cached_files as $new_file )
3894 {
3895 if( ! empty( $new_file['errors'] ) )
3896 {
3897 $err_cnt++;
3898 }
3899 $count++;
3900
3901 if( $count > $instance['number'] )
3902 {
3903 break;
3904 }
3905 }
3906
3907 $instance_cache['upload_errors'] = ( 0 == $err_cnt ) ? false : $err_cnt;
3908 $instance_cache['cached_list'] = $new_cached_files;
3909
3910 return $instance_cache;
3911 }
3912
3913 /**
3914 * Downloads the files from instagram and stores them in local cache
3915 *
3916 * @since 4.3.1
3917 * @param array $instagram_file
3918 * @param array $instance
3919 * @param array $instance_cache
3920 * @return array
3921 */
3922 protected function download_from_instagram( array $instagram_file, array $instance, array $instance_cache )
3923 {
3924 $new_cached_file = $instagram_file;
3925 $new_cached_file['errors'] = array();
3926 $instagram_schema = 'https:';
3927
3928 $cache_path = trailingslashit( $this->upload_folders['instagram_dir'] . $instance_cache['upload_folder'] );
3929
3930 foreach( $this->cached_file_sizes as $size )
3931 {
3932 $file_array = array();
3933
3934 // instagram returns link to file with ?......
3935 $fn = explode( '?', basename( $instagram_file[ $size ] ) );
3936 $file_array['name'] = $fn[0];
3937
3938 // Download file to temp location - include file if called from frontend.
3939 if( ! function_exists( 'download_url' ) )
3940 {
3941 $s = trailingslashit( ABSPATH ) . 'wp-admin/includes/file.php';
3942 require_once $s;
3943 }
3944
3945 $file_array['tmp_name'] = download_url( $instagram_schema . $instagram_file[ $size ] );
3946
3947 // If error storing temporarily, return the error.
3948 if( is_wp_error( $file_array['tmp_name'] ) )
3949 {
3950 $new_cached_file[ $size ] = '';
3951 $new_cached_file['errors'] = array_merge( $new_cached_file['errors'], $file_array['tmp_name']->get_error_messages() );
3952 continue;
3953 }
3954
3955 $new_file_name = $size . '_' . $file_array['name'];
3956 $new_name = $cache_path . $new_file_name;
3957
3958 $moved = avia_backend_rename_file( $file_array['tmp_name'], $new_name );
3959 if( is_wp_error( $moved ) )
3960 {
3961 $new_cached_file[ $size ] = '';
3962 $new_cached_file['errors'] = array_merge( $new_cached_file['errors'], $moved->get_error_messages() );
3963 continue;
3964 }
3965
3966 /**
3967 * Try to change accessability of file
3968 */
3969 if( ! chmod( $new_name, 0777 ) )
3970 {
3971 $new_cached_file['errors'][] = sprintf( __( 'Could not change user rights of file %s to 777 - file might not be visible in frontend.', 'avia_framework' ), $new_name );
3972 }
3973
3974 $new_cached_file[ $size ] = $new_file_name;
3975 }
3976
3977 return $new_cached_file;
3978 }
3979
3980
3981 /**
3982 * Removes all cashed files from $cached_file_info
3983 *
3984 * @since 4.3.1
3985 * @param array $cached_file_info
3986 * @param array $instance_cache
3987 * @return array
3988 */
3989 protected function remove_single_cached_files( array $cached_file_info, array $instance_cache )
3990 {
3991 $cache_path = trailingslashit( $this->upload_folders['instagram_dir'] . $instance_cache['upload_folder'] );
3992
3993 foreach( $this->cached_file_sizes as $size )
3994 {
3995 if( ! empty( $cached_file_info[ $size ] ) )
3996 {
3997 $file = $cache_path . $cached_file_info[ $size ];
3998
3999 if( file_exists( $file ) )
4000 {
4001 unlink( $file );
4002 }
4003 $cached_file_info[ $size ] = '';
4004 }
4005 }
4006
4007 return $cached_file_info;
4008 }
4009 }
4010}
4011
4012
4013
4014/**
4015 * AVIA TABLE OF CONTENTS WIDGET
4016 *
4017 * Widget that displays a 'table of contents' genereated from the headlines of the page it is viewed on
4018 *
4019 * @package AviaFramework
4020 * @author tinabillinger
4021 * @todo replace the widget system with a dynamic one, based on config files for easier widget creation
4022 */
4023
4024if (!class_exists('avia_auto_toc'))
4025{
4026 class avia_auto_toc extends WP_Widget {
4027
4028 static $script_loaded = 0;
4029
4030 function __construct() {
4031 //Constructor
4032 $widget_ops = array('classname' => 'avia_auto_toc', 'description' => __('Widget that displays a table of contents genereated from the headlines of the page it is viewed on', 'avia_framework') );
4033 parent::__construct( 'avia_auto_toc', THEMENAME.' Table of Contents', $widget_ops );
4034 }
4035
4036 function widget($args, $instance) {
4037 extract($args, EXTR_SKIP);
4038
4039 if ($instance['single_only'] && ! is_single()) return false;
4040
4041 $title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
4042 $indent_class = $instance['indent'] ? ' avia-toc-indent' : '';
4043 $smoothscroll_class = $instance['smoothscroll'] ? ' avia-toc-smoothscroll' : '';
4044
4045 echo $before_widget;
4046
4047 if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
4048
4049 $exclude = "";
4050 if ( $instance['exclude'] !== '' ){
4051 $exclude = 'data-exclude="'.$instance['exclude'].'"';
4052 }
4053
4054 $instance['style'] = "elegant";
4055
4056 echo '<div class="avia-toc-container avia-toc-style-'.$instance['style'].$indent_class.$smoothscroll_class.'" data-level="'.$instance['level'].'" '.$exclude.'></div>';
4057
4058 echo $after_widget;
4059 }
4060
4061 function update($new_instance, $old_instance) {
4062 $instance = $old_instance;
4063 $instance['title'] = trim(strip_tags($new_instance['title']));
4064 $instance['exclude'] = strip_tags($new_instance['exclude']);
4065 $instance['style'] = strip_tags($new_instance['style']);
4066 $instance['level'] = implode(',',$new_instance['level']);
4067 $instance['single_only'] = isset( $new_instance['single_only'] ) ? 1 : 0;
4068 $instance['indent'] = isset( $new_instance['indent'] ) ? 1 : 0;
4069 $instance['smoothscroll'] = isset( $new_instance['smoothscroll'] ) ? 1 : 0;
4070
4071 return $instance;
4072 }
4073
4074 function form( $instance ) {
4075
4076 $instance = wp_parse_args( (array) $instance, array(
4077 'exclude' => '',
4078 'level' => 'h1',
4079 'title' => '',
4080 'style' => 'simple',
4081 ) );
4082
4083 $title = sanitize_text_field( $instance['title'] );
4084 $single_only = isset( $instance['single_only'] ) ? (bool) $instance['single_only'] : true;
4085 $indent = isset( $instance['indent'] ) ? (bool) $instance['indent'] : true;
4086 $smoothscroll = isset( $instance['smoothscroll'] ) ? (bool) $instance['smoothscroll'] : true;
4087
4088 $levels = array(
4089 'h1' => 'H1 Headlines',
4090 'h2' => 'H2 Headlines',
4091 'h3' => 'H3 Headlines',
4092 'h4' => 'H4 Headlines',
4093 'h5' => 'H5 Headlines',
4094 'h6' => 'H6 Headlines'
4095 );
4096
4097 $styles = array(
4098 'simple' => 'Simple',
4099 'elegant' => 'Elegant',
4100 );
4101
4102 ?>
4103 <p>
4104 <label for="<?php echo $this->get_field_id('Title'); ?>"><?php _e('Title:', 'avia_framework'); ?>
4105 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label>
4106 </p>
4107
4108 <p>
4109 <label for="<?php echo $this->get_field_id('exclude'); ?>"><?php _e('Exclude headlines by class:', 'avia_framework'); ?>
4110 <input class="widefat" id="<?php echo $this->get_field_id('exclude'); ?>" name="<?php echo $this->get_field_name('exclude'); ?>" type="text" value="<?php echo esc_attr($instance['exclude']); ?>" /></label>
4111 <small>Provice a classname without a dot</small>
4112 </p>
4113
4114 <p>
4115 <label for="<?php echo $this->get_field_id('level'); ?>"><?php _e('Select headlines to include:', 'avia_framework'); ?><br/>
4116 <select class="widefat" id="<?php echo $this->get_field_id('level'); ?>" name="<?php echo $this->get_field_name('level'); ?>[]" multiple="multiple">
4117 <?php
4118 $selected_levels = explode(',', $instance['level']);
4119
4120 foreach ( $levels as $k => $l) {
4121 $selected = '';
4122 if (in_array($k,$selected_levels)){
4123 $selected = ' selected="selected"';
4124 }
4125 ?>
4126 <option<?php echo $selected;?> value="<?php echo $k; ?>"><?php echo $l; ?></option>
4127 <?php
4128 }
4129 ?>
4130 </select>
4131 </label>
4132 </p>
4133 <!--
4134 <p>
4135 <label for="<?php echo $this->get_field_id('style'); ?>"><?php _e('Select a style', 'avia_framework'); ?><br/>
4136 <select class="widefat" id="<?php echo $this->get_field_id('style'); ?>" name="<?php echo $this->get_field_name('style'); ?>">
4137 <?php
4138
4139 foreach ( $styles as $sk => $sv) {
4140
4141 $selected = '';
4142 if ($sk == $instance['style']){
4143 $selected = ' selected="selected"';
4144 }
4145 ?>
4146 <option<?php echo $selected;?> value="<?php echo $sk; ?>"><?php echo $sv; ?></option>
4147 <?php
4148 }
4149 ?>
4150 </select>
4151 </label>
4152 </p>
4153 -->
4154
4155
4156 <p>
4157 <input class="checkbox" id="<?php echo $this->get_field_id('single_only'); ?>" name="<?php echo $this->get_field_name('single_only'); ?>" type="checkbox" <?php checked( $single_only ); ?> />
4158 <label for="<?php echo $this->get_field_id('single_only'); ?>"><?php _e('Display on Single Blog Posts only', 'avia_framework'); ?></label>
4159 </br>
4160 <input class="checkbox" id="<?php echo $this->get_field_id('indent'); ?>" name="<?php echo $this->get_field_name('indent'); ?>" type="checkbox" <?php checked( $indent ); ?> />
4161 <label for="<?php echo $this->get_field_id('indent'); ?>"><?php _e('Hierarchy Indentation', 'avia_framework'); ?></label>
4162 </br>
4163 <input class="checkbox" id="<?php echo $this->get_field_id('smoothscroll'); ?>" name="<?php echo $this->get_field_name('smoothscroll'); ?>" type="checkbox" <?php checked( $smoothscroll ); ?> />
4164 <label for="<?php echo $this->get_field_id('smoothscroll'); ?>"><?php _e('Enable Smooth Scrolling', 'avia_framework'); ?></label>
4165 </p>
4166
4167 <?php
4168
4169 }
4170
4171 }
4172}
4173