columns.php
12 years ago
counter.php
12 years ago
cron.php
12 years ago
frontend.php
12 years ago
functions.php
12 years ago
query.php
12 years ago
settings.php
12 years ago
update.php
12 years ago
widgets.php
12 years ago
widgets.php
212 lines
| 1 | <?php |
| 2 | if(!defined('ABSPATH')) exit; |
| 3 | |
| 4 | new Post_Views_Counter_Widgets(); |
| 5 | |
| 6 | class Post_Views_Counter_Widgets |
| 7 | { |
| 8 | public function __construct() |
| 9 | { |
| 10 | // actions |
| 11 | add_action('widgets_init', array(&$this, 'register_widgets')); |
| 12 | } |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * |
| 17 | */ |
| 18 | public function register_widgets() |
| 19 | { |
| 20 | register_widget('Post_Views_Counter_List_Widget'); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | |
| 25 | class Post_Views_Counter_List_Widget extends WP_Widget |
| 26 | { |
| 27 | private $pvc_options; |
| 28 | private $pvc_defaults; |
| 29 | private $pvc_post_types; |
| 30 | private $pvc_order_types; |
| 31 | private $pvc_image_sizes; |
| 32 | |
| 33 | |
| 34 | public function __construct() |
| 35 | { |
| 36 | parent::__construct( |
| 37 | 'Post_Views_Counter_List_Widget', |
| 38 | __('Most Viewed Posts', 'post-views-counter'), |
| 39 | array( |
| 40 | 'description' => __('Displays a list of the most viewed posts', 'post-views-counter') |
| 41 | ) |
| 42 | ); |
| 43 | |
| 44 | $this->pvc_options = array_merge( |
| 45 | array('general' => get_option('events_maker_general')) |
| 46 | ); |
| 47 | |
| 48 | $this->pvc_defaults = array( |
| 49 | 'title' => __('Most Viewed Posts', 'post-views-counter'), |
| 50 | 'number_of_posts' => 5, |
| 51 | 'thumbnail_size' => 'thumbnail', |
| 52 | 'post_types' => array(), |
| 53 | 'order' => 'desc', |
| 54 | 'show_post_views' => true, |
| 55 | 'show_post_thumbnail' => false, |
| 56 | 'show_post_excerpt' => false, |
| 57 | 'no_posts_message' => __('No Posts found', 'post-views-counter') |
| 58 | ); |
| 59 | |
| 60 | $this->pvc_order_types = array( |
| 61 | 'asc' => __('Ascending', 'post-views-counter'), |
| 62 | 'desc' => __('Descending', 'post-views-counter') |
| 63 | ); |
| 64 | |
| 65 | $this->pvc_image_sizes = array_merge(array('full'), get_intermediate_image_sizes()); |
| 66 | |
| 67 | // sorts image sizes ascending by name |
| 68 | sort($this->pvc_image_sizes, SORT_STRING); |
| 69 | |
| 70 | add_action('wp_loaded', array(&$this, 'load_post_types')); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /** |
| 75 | * |
| 76 | */ |
| 77 | public function load_post_types() |
| 78 | { |
| 79 | $this->pvc_post_types = Post_Views_Counter()->get_instance('settings')->post_types; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /** |
| 84 | * |
| 85 | */ |
| 86 | public function widget($args, $instance) |
| 87 | { |
| 88 | $instance['title'] = apply_filters('widget_title', $instance['title'], $instance, $this->id_base); |
| 89 | |
| 90 | $html = $args['before_widget'].(!empty($instance['title']) ? $args['before_title'].$instance['title'].$args['after_title'] : ''); |
| 91 | $html .= pvc_most_viewed_posts($instance, false); |
| 92 | $html .= $args['after_widget']; |
| 93 | |
| 94 | echo $html; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | /** |
| 99 | * |
| 100 | */ |
| 101 | public function form($instance) |
| 102 | { |
| 103 | $html = ' |
| 104 | <p> |
| 105 | <label for="'.$this->get_field_id('title').'">'.__('Title', 'post-views-counter').':</label> |
| 106 | <input id="'.$this->get_field_id('title').'" class="widefat" name="'.$this->get_field_name('title').'" type="text" value="'.esc_attr(isset($instance['title']) ? $instance['title'] : $this->pvc_defaults['title']).'" /> |
| 107 | </p> |
| 108 | <p> |
| 109 | <label>'.__('Post types', 'post-views-counter').':</label><br />'; |
| 110 | |
| 111 | foreach($this->pvc_post_types as $post_type => $post_type_name) |
| 112 | { |
| 113 | $html .= ' |
| 114 | <input id="'.$this->get_field_id('post_types').'-'.$post_type.'" type="checkbox" name="'.$this->get_field_name('post_types').'[]" value="'.$post_type.'" '.checked((!isset($instance['post_types']) ? true : in_array($post_type, $instance['post_types'], true)), true, false).'><label for="'.$this->get_field_id('post_types').'-'.$post_type.'">'.esc_html($post_type_name).'</label>'; |
| 115 | } |
| 116 | |
| 117 | $show_post_thumbnail = (isset($instance['show_post_thumbnail']) ? $instance['show_post_thumbnail'] : $this->pvc_defaults['show_post_thumbnail']); |
| 118 | |
| 119 | $html .= ' |
| 120 | </select> |
| 121 | </p> |
| 122 | <p> |
| 123 | <label for="'.$this->get_field_id('number_of_posts').'">'.__('Number of posts to show', 'post-views-counter').':</label> |
| 124 | <input id="'.$this->get_field_id('number_of_posts').'" name="'.$this->get_field_name('number_of_posts').'" type="text" size="3" value="'.esc_attr(isset($instance['number_of_posts']) ? $instance['number_of_posts'] : $this->pvc_defaults['number_of_posts']).'" /> |
| 125 | </p> |
| 126 | <p> |
| 127 | <label for="'.$this->get_field_id('no_posts_message').'">'.__('No posts message', 'post-views-counter').':</label> |
| 128 | <input id="'.$this->get_field_id('no_posts_message').'" class="widefat" type="text" name="'.$this->get_field_name('no_posts_message').'" value="'.esc_attr(isset($instance['no_posts_message']) ? $instance['no_posts_message'] : $this->pvc_defaults['no_posts_message']).'" /> |
| 129 | </p> |
| 130 | <p> |
| 131 | <label for="'.$this->get_field_id('order').'">'.__('Order', 'post-views-counter').':</label> |
| 132 | <select id="'.$this->get_field_id('order').'" name="'.$this->get_field_name('order').'">'; |
| 133 | |
| 134 | foreach($this->pvc_order_types as $id => $order) |
| 135 | { |
| 136 | $html .= ' |
| 137 | <option value="'.esc_attr($id).'" '.selected($id, (isset($instance['order']) ? $instance['order'] : $this->pvc_defaults['order']), false).'>'.$order.'</option>'; |
| 138 | } |
| 139 | |
| 140 | $html .= ' |
| 141 | </select> |
| 142 | </p> |
| 143 | <p> |
| 144 | <input id="'.$this->get_field_id('show_post_views').'" type="checkbox" name="'.$this->get_field_name('show_post_views').'" '.checked(true, (isset($instance['show_post_views']) ? $instance['show_post_views'] : $this->pvc_defaults['show_post_views']), false).' /> <label for="'.$this->get_field_id('show_post_views').'">'.__('Display post views?', 'post-views-counter').'</label> |
| 145 | <br /> |
| 146 | <input id="'.$this->get_field_id('show_post_excerpt').'" type="checkbox" name="'.$this->get_field_name('show_post_excerpt').'" '.checked(true, (isset($instance['show_post_excerpt']) ? $instance['show_post_excerpt'] : $this->pvc_defaults['show_post_excerpt']), false).' /> <label for="'.$this->get_field_id('show_post_excerpt').'">'.__('Display post excerpt?', 'post-views-counter').'</label> |
| 147 | <br /> |
| 148 | <input id="'.$this->get_field_id('show_post_thumbnail').'" class="em-show-event-thumbnail" type="checkbox" name="'.$this->get_field_name('show_post_thumbnail').'" '.checked(true, $show_post_thumbnail, false).' /> <label for="'.$this->get_field_id('show_post_thumbnail').'">'.__('Display post thumbnail?', 'post-views-counter').'</label> |
| 149 | </p> |
| 150 | <p class="em-event-thumbnail-size"'.($show_post_thumbnail ? '' : ' style="display: none;"').'> |
| 151 | <label for="'.$this->get_field_id('thumbnail_size').'">'.__('Thumbnail size', 'post-views-counter').':</label> |
| 152 | <select id="'.$this->get_field_id('thumbnail_size').'" name="'.$this->get_field_name('thumbnail_size').'">'; |
| 153 | |
| 154 | $size_type = (isset($instance['thumbnail_size']) ? $instance['thumbnail_size'] : $this->pvc_defaults['thumbnail_size']); |
| 155 | |
| 156 | foreach($this->pvc_image_sizes as $size) |
| 157 | { |
| 158 | $html .= ' |
| 159 | <option value="'.esc_attr($size).'" '.selected($size, $size_type, false).'>'.$size.'</option>'; |
| 160 | } |
| 161 | |
| 162 | $html .= ' |
| 163 | </select> |
| 164 | </p>'; |
| 165 | |
| 166 | echo $html; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | /** |
| 171 | * |
| 172 | */ |
| 173 | public function update($new_instance, $old_instance) |
| 174 | { |
| 175 | // number of events |
| 176 | $old_instance['number_of_posts'] = (int)(isset($new_instance['number_of_posts']) ? $new_instance['number_of_posts'] : $this->pvc_defaults['number_of_posts']); |
| 177 | |
| 178 | // order |
| 179 | $old_instance['order'] = (isset($new_instance['order']) && in_array($new_instance['order'], array_keys($this->pvc_order_types), true) ? $new_instance['order'] : $this->pvc_defaults['order']); |
| 180 | |
| 181 | // thumbnail size |
| 182 | $old_instance['thumbnail_size'] = (isset($new_instance['thumbnail_size']) && in_array($new_instance['thumbnail_size'], $this->pvc_image_sizes, true) ? $new_instance['thumbnail_size'] : $this->pvc_defaults['thumbnail_size']); |
| 183 | |
| 184 | // booleans |
| 185 | $old_instance['show_post_views'] = (isset($new_instance['show_post_views']) ? true : false); |
| 186 | $old_instance['show_post_thumbnail'] = (isset($new_instance['show_post_thumbnail']) ? true : false); |
| 187 | $old_instance['show_post_excerpt'] = (isset($new_instance['show_post_excerpt']) ? true : false); |
| 188 | |
| 189 | // texts |
| 190 | $old_instance['title'] = sanitize_text_field(isset($new_instance['title']) ? $new_instance['title'] : $this->pvc_defaults['title']); |
| 191 | $old_instance['no_posts_message'] = sanitize_text_field(isset($new_instance['no_posts_message']) ? $new_instance['no_posts_message'] : $this->pvc_defaults['no_posts_message']); |
| 192 | |
| 193 | // post types |
| 194 | if(isset($new_instance['post_types'])) |
| 195 | { |
| 196 | $post_types = array(); |
| 197 | |
| 198 | foreach($new_instance['post_types'] as $post_type) |
| 199 | { |
| 200 | if(isset($this->pvc_post_types[$post_type])) |
| 201 | $post_types[] = $post_type; |
| 202 | } |
| 203 | |
| 204 | $old_instance['post_types'] = array_unique($post_types); |
| 205 | } |
| 206 | else |
| 207 | $old_instance['post_types'] = array('post'); |
| 208 | |
| 209 | return $old_instance; |
| 210 | } |
| 211 | } |
| 212 | ?> |