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