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