| @@ -1,15 +1,24 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 2 | +define ("TABOOLA_WIDGET_BASE_ID","taboola"); | |
| 3 | 3 | class WP_Widget_Taboola extends WP_Widget { |
| 4 | 4 | |
| 5 | + private static $counter; | |
| 5 | 6 | function __construct() { |
| 7 | + | |
| 8 | + | |
| 6 | 9 | $widget_ops = array('classname' => 'widget_taboola', 'description' => __( "A taboola widget for your site.") ); |
| 7 | - parent::__construct( 'taboola', _x( 'Taboola Widget', 'Taboola Widget' ), $widget_ops ); | |
| 10 | + parent::__construct( TABOOLA_WIDGET_BASE_ID, _x( 'Taboola Widget', 'Taboola Widget' ), $widget_ops ); | |
| 8 | 11 | $this->plugin_directory = plugin_dir_path(__FILE__); |
| 9 | 12 | } |
| 10 | 13 | |
| 11 | 14 | function widget( $args, $instance ) { |
| 15 | + if (!isset(WP_Widget_Taboola::$counter)){ | |
| 16 | + WP_Widget_Taboola::$counter = 1; | |
| 17 | + } | |
| 18 | + else{ | |
| 19 | + WP_Widget_Taboola::$counter = WP_Widget_Taboola::$counter + 1; | |
| 20 | + } | |
| 12 | 21 | if (trim($instance['widget_id']) == ''){ |
| 13 | 22 | return; |
| 14 | 23 | } |
| 15 | 24 | extract($args); |
| @@ -16,15 +25,21 @@ | ||
| 16 | 25 | |
| 17 | 26 | /** This filter is documented in wp-includes/default-widgets.php */ |
| 18 | 27 | $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); |
| 19 | 28 | |
| 29 | + $tmp_id = explode("-",$args["widget_id"]); | |
| 30 | + $widget_num = $tmp_id[1]; | |
| 31 | + | |
| 20 | 32 | $widget_id = ! empty( $instance['widget_id'] ) ? $instance['widget_id'] : ''; |
| 21 | - $placement = ! empty( $instance['placement'] ) ? $instance['placement'] : ''; | |
| 22 | 33 | |
| 34 | + // use the widget container name as the placement, suffix the widget_id to make it unique | |
| 35 | + $placement=$args["id"]."-".$widget_num; | |
| 36 | + | |
| 23 | 37 | echo $before_widget; |
| 24 | 38 | |
| 25 | - if ( $title ) | |
| 39 | + if ( $title ){ | |
| 26 | 40 | echo $before_title . $title . $after_title; |
| 41 | + } | |
| 27 | 42 | |
| 28 | 43 | //widget content |
| 29 | 44 | $script2_content = str_replace('{{WIDGET_ID}}', $widget_id, file_get_contents($this->plugin_directory.'js/script2.js')); |
| 30 | 45 | $script2_content = str_replace('{{CONTAINER}}', $this->id, $script2_content); |
| @@ -36,9 +51,8 @@ | ||
| 36 | 51 | |
| 37 | 52 | function form( $instance ) { |
| 38 | 53 | $instance = wp_parse_args( (array) $instance ); |
| 39 | 54 | $widget_id = esc_attr( $instance['widget_id'] ); |
| 40 | - $placement = esc_attr( $instance['placement'] ); | |
| 41 | 55 | ?> |
| 42 | 56 | <p> |
| 43 | 57 | <label for="<?php echo $this->get_field_id('widget_id'); ?>"><?php _e('Widget ID:'); ?> |
| 44 | 58 | <input class="widefat" id="<?php echo $this->get_field_id('widget_id'); ?>" name="<?php echo $this->get_field_name('widget_id'); ?>" type="text" value="<?php echo esc_attr($widget_id); ?>" /> |
| @@ -43,20 +57,20 @@ | ||
| 43 | 57 | <label for="<?php echo $this->get_field_id('widget_id'); ?>"><?php _e('Widget ID:'); ?> |
| 44 | 58 | <input class="widefat" id="<?php echo $this->get_field_id('widget_id'); ?>" name="<?php echo $this->get_field_name('widget_id'); ?>" type="text" value="<?php echo esc_attr($widget_id); ?>" /> |
| 45 | 59 | </label> |
| 46 | 60 | </p> |
| 47 | - <p> | |
| 48 | - <label for="<?php echo $this->get_field_id('placement'); ?>"><?php _e('Placement:'); ?> | |
| 49 | - <input class="widefat" id="<?php echo $this->get_field_id('placement'); ?>" name="<?php echo $this->get_field_name('placement'); ?>" type="text" value="<?php echo esc_attr($placement); ?>" /> | |
| 50 | - </label> | |
| 51 | - </p> | |
| 52 | 61 | <?php |
| 53 | 62 | } |
| 54 | 63 | |
| 55 | 64 | function update( $new_instance, $old_instance ) { |
| 65 | + | |
| 66 | + // canceling save if the field is empty | |
| 67 | + if (strip_tags($new_instance['widget_id']) == ""){ | |
| 68 | + return false; | |
| 69 | + } | |
| 70 | + | |
| 56 | 71 | $instance = $old_instance; |
| 57 | 72 | $instance['widget_id'] = strip_tags($new_instance['widget_id']); |
| 58 | - $instance['placement'] = strip_tags($new_instance['placement']); | |
| 59 | 73 | |
| 60 | 74 | return $instance; |
| 61 | 75 | } |
| 62 | 76 | |