| @@ -1,15 +1,41 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 2 | +define ("TABOOLA_WIDGET_BASE_ID","taboola"); | |
| 3 | +define ("TABOOLA_CONTAINER_PREFIX","inner-"); | |
| 3 | 4 | class WP_Widget_Taboola extends WP_Widget { |
| 4 | 5 | |
| 6 | + private static $counter; | |
| 5 | 7 | function __construct() { |
| 8 | + | |
| 9 | + | |
| 6 | 10 | $widget_ops = array('classname' => 'widget_taboola', 'description' => __( "A taboola widget for your site.") ); |
| 7 | - parent::__construct( 'taboola', _x( 'Taboola Widget', 'Taboola Widget' ), $widget_ops ); | |
| 11 | + parent::__construct( TABOOLA_WIDGET_BASE_ID, _x( 'Taboola Widget', 'Taboola Widget' ), $widget_ops ); | |
| 8 | 12 | $this->plugin_directory = plugin_dir_path(__FILE__); |
| 9 | 13 | } |
| 10 | 14 | |
| 15 | + function get_id($tag){ | |
| 16 | + | |
| 17 | + $start_pos = strpos($tag,"id="); | |
| 18 | + | |
| 19 | + $container_id = null; | |
| 20 | + if ($start_pos !== false){ | |
| 21 | + | |
| 22 | + $end_pos = strpos($tag," ",$start_pos); | |
| 23 | + $extracted_id = str_replace(array('"',"'"),"",substr($tag,$start_pos+3,$end_pos-$start_pos-3)); | |
| 24 | + if ($extracted_id != ""){ | |
| 25 | + $container_id = $extracted_id; | |
| 26 | + } | |
| 27 | + } | |
| 28 | + return $container_id; | |
| 29 | + } | |
| 30 | + | |
| 11 | 31 | function widget( $args, $instance ) { |
| 32 | + if (!isset(WP_Widget_Taboola::$counter)){ | |
| 33 | + WP_Widget_Taboola::$counter = 1; | |
| 34 | + } | |
| 35 | + else{ | |
| 36 | + WP_Widget_Taboola::$counter = WP_Widget_Taboola::$counter + 1; | |
| 37 | + } | |
| 12 | 38 | if (trim($instance['widget_id']) == ''){ |
| 13 | 39 | return; |
| 14 | 40 | } |
| 15 | 41 | extract($args); |
| @@ -16,47 +42,63 @@ | ||
| 16 | 42 | |
| 17 | 43 | /** This filter is documented in wp-includes/default-widgets.php */ |
| 18 | 44 | $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); |
| 19 | 45 | |
| 46 | + $tmp_id = explode("-",$args["widget_id"]); | |
| 47 | + $widget_num = $tmp_id[1]; | |
| 48 | + | |
| 20 | 49 | $widget_id = ! empty( $instance['widget_id'] ) ? $instance['widget_id'] : ''; |
| 21 | - $placement = ! empty( $instance['placement'] ) ? $instance['placement'] : ''; | |
| 22 | 50 | |
| 51 | + // Prefix each placement with "WP Right Rail". Grab the ID of the container (i.e. the sidebar) and append 'widget_num' to make it unique. | |
| 52 | + $placement = "WP Right Rail (". $args["id"]."-".$widget_num.")"; | |
| 53 | + | |
| 23 | 54 | echo $before_widget; |
| 24 | 55 | |
| 25 | - if ( $title ) | |
| 56 | + if ( $title ){ | |
| 26 | 57 | echo $before_title . $title . $after_title; |
| 58 | + } | |
| 27 | 59 | |
| 60 | + $container = $this->get_id($before_widget); | |
| 61 | + | |
| 62 | + if ($container == null){ | |
| 63 | + // widget container (adding a div) | |
| 64 | + $container = TABOOLA_CONTAINER_PREFIX.$this->id; | |
| 65 | + echo '<div id="'.$container.'"></div>'; | |
| 66 | + } | |
| 67 | + | |
| 28 | 68 | //widget content |
| 29 | - $script2_content = str_replace('{{WIDGET_ID}}', $widget_id, file_get_contents($this->plugin_directory.'js/script2.js')); | |
| 30 | - $script2_content = str_replace('{{CONTAINER}}', $this->id, $script2_content); | |
| 31 | - $script2_content = str_replace('{{PLACEMENT}}', $placement, $script2_content); | |
| 32 | - echo '<script type="text/javascript">'.$script2_content.'</script>'; | |
| 69 | + $stringParams = array( | |
| 70 | + "{{WIDGET_ID}}" => $widget_id, | |
| 71 | + "{{CONTAINER}}" => $container, | |
| 72 | + "{{PLACEMENT}}" => $placement | |
| 73 | + ); | |
| 74 | + $widgetInjectionScript_content = str_replace(array_keys($stringParams), array_values($stringParams), file_get_contents($this->plugin_directory.'js/widgetInjectionScript.js')); | |
| 75 | + echo '<script type="text/javascript">'.$widgetInjectionScript_content.'</script>'; | |
| 33 | 76 | |
| 34 | 77 | echo $after_widget; |
| 35 | 78 | } |
| 36 | 79 | |
| 37 | - function form( $instance ) { | |
| 80 | + function form( $instance ) { | |
| 38 | 81 | $instance = wp_parse_args( (array) $instance ); |
| 39 | 82 | $widget_id = esc_attr( $instance['widget_id'] ); |
| 40 | - $placement = esc_attr( $instance['placement'] ); | |
| 41 | 83 | ?> |
| 42 | 84 | <p> |
| 43 | - <label for="<?php echo $this->get_field_id('widget_id'); ?>"><?php _e('Widget ID:'); ?> | |
| 85 | + <label for="<?php echo $this->get_field_id('widget_id'); ?>"><?php _e('Mode (Widget ID):'); ?> | |
| 44 | 86 | <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 | 87 | </label> |
| 46 | 88 | </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 | 89 | <?php |
| 53 | 90 | } |
| 54 | 91 | |
| 55 | 92 | function update( $new_instance, $old_instance ) { |
| 93 | + | |
| 94 | + // canceling save if the field is empty | |
| 95 | + if (strip_tags($new_instance['widget_id']) == ""){ | |
| 96 | + return false; | |
| 97 | + } | |
| 98 | + | |
| 56 | 99 | $instance = $old_instance; |
| 57 | 100 | $instance['widget_id'] = strip_tags($new_instance['widget_id']); |
| 58 | - $instance['placement'] = strip_tags($new_instance['placement']); | |
| 59 | 101 | |
| 60 | 102 | return $instance; |
| 61 | 103 | } |
| 62 | 104 | |