class-admin.php
4 years ago
class-columns.php
4 years ago
class-counter.php
4 years ago
class-crawler-detect.php
4 years ago
class-cron.php
4 years ago
class-dashboard.php
4 years ago
class-frontend.php
4 years ago
class-functions.php
4 years ago
class-query.php
4 years ago
class-settings-api.php
4 years ago
class-settings.php
4 years ago
class-update.php
4 years ago
class-widgets.php
4 years ago
functions.php
4 years ago
class-widgets.php
250 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 | /** |
| 14 | * Class constructor. |
| 15 | * |
| 16 | * @return void |
| 17 | */ |
| 18 | public function __construct() { |
| 19 | // actions |
| 20 | add_action( 'widgets_init', [ $this, 'register_widgets' ] ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Register widgets. |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | public function register_widgets() { |
| 29 | register_widget( 'Post_Views_Counter_List_Widget' ); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Post_Views_Counter_List_Widget class. |
| 35 | * |
| 36 | * @class Post_Views_Counter_List_Widget |
| 37 | */ |
| 38 | class Post_Views_Counter_List_Widget extends WP_Widget { |
| 39 | |
| 40 | private $pvc_defaults; |
| 41 | private $pvc_order_types; |
| 42 | private $pvc_list_types; |
| 43 | private $pvc_image_sizes; |
| 44 | |
| 45 | /** |
| 46 | * Class constructor. |
| 47 | * |
| 48 | * @return void |
| 49 | */ |
| 50 | public function __construct() { |
| 51 | // call parent |
| 52 | parent::__construct( |
| 53 | 'Post_Views_Counter_List_Widget', |
| 54 | __( 'Most Viewed Posts', 'post-views-counter' ), |
| 55 | [ |
| 56 | 'description' => __( 'Displays a list of the most viewed posts', 'post-views-counter' ) |
| 57 | ] |
| 58 | ); |
| 59 | |
| 60 | // default settings |
| 61 | $this->pvc_defaults = [ |
| 62 | 'title' => __( 'Most Viewed Posts', 'post-views-counter' ), |
| 63 | 'number_of_posts' => 5, |
| 64 | 'thumbnail_size' => 'thumbnail', |
| 65 | 'post_type' => [], |
| 66 | 'order' => 'desc', |
| 67 | 'list_type' => 'unordered', |
| 68 | 'show_post_views' => true, |
| 69 | 'show_post_thumbnail' => false, |
| 70 | 'show_post_excerpt' => false, |
| 71 | 'show_post_author' => false, |
| 72 | 'no_posts_message' => __( 'No most viewed posts found', 'post-views-counter' ) |
| 73 | ]; |
| 74 | |
| 75 | // order types |
| 76 | $this->pvc_order_types = [ |
| 77 | 'asc' => __( 'Ascending', 'post-views-counter' ), |
| 78 | 'desc' => __( 'Descending', 'post-views-counter' ) |
| 79 | ]; |
| 80 | |
| 81 | // list types |
| 82 | $this->pvc_list_types = [ |
| 83 | 'unordered' => __( 'Unordered list', 'post-views-counter' ), |
| 84 | 'ordered' => __( 'Ordered list', 'post-views-counter' ) |
| 85 | ]; |
| 86 | |
| 87 | // image sizes |
| 88 | $this->pvc_image_sizes = array_merge( [ 'full' ], get_intermediate_image_sizes() ); |
| 89 | |
| 90 | // sort image sizes by name, ascending |
| 91 | sort( $this->pvc_image_sizes, SORT_STRING ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Display widget. |
| 96 | * |
| 97 | * @param array $args |
| 98 | * @param array $instance |
| 99 | * @return void |
| 100 | */ |
| 101 | public function widget( $args, $instance ) { |
| 102 | // empty title? |
| 103 | if ( empty( $instance['title'] ) ) |
| 104 | $instance['title'] = $this->pvc_defaults['title']; |
| 105 | |
| 106 | // filter title |
| 107 | $instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); |
| 108 | |
| 109 | $html = $args['before_widget'] . ( ! empty( $instance['title'] ) ? $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'] : '' ); |
| 110 | $html .= pvc_most_viewed_posts( $instance, false ); |
| 111 | $html .= $args['after_widget']; |
| 112 | |
| 113 | echo $html; |
| 114 | } |
| 115 | |
| 116 | /** Render widget form. |
| 117 | * |
| 118 | * @param array $instance |
| 119 | * @return void |
| 120 | */ |
| 121 | public function form( $instance ) { |
| 122 | $html = ' |
| 123 | <p> |
| 124 | <label for="' . $this->get_field_id( 'title' ) . '">' . __( 'Title', 'post-views-counter' ) . ':</label> |
| 125 | <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'] ) . '" /> |
| 126 | </p> |
| 127 | <p> |
| 128 | <label>' . __( 'Post Types', 'post-views-counter' ) . ':</label><br />'; |
| 129 | |
| 130 | // post types |
| 131 | foreach ( Post_Views_Counter()->functions->get_post_types() as $post_type => $post_type_name ) { |
| 132 | $html .= ' |
| 133 | <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>'; |
| 134 | } |
| 135 | |
| 136 | $show_post_thumbnail = isset( $instance['show_post_thumbnail'] ) ? $instance['show_post_thumbnail'] : $this->pvc_defaults['show_post_thumbnail']; |
| 137 | |
| 138 | $html .= ' |
| 139 | </p> |
| 140 | <p> |
| 141 | <label for="' . $this->get_field_id( 'number_of_posts' ) . '">' . __( 'Number of posts to show', 'post-views-counter' ) . ':</label> |
| 142 | <input id="' . $this->get_field_id( 'number_of_posts' ) . '" name="' . $this->get_field_name( 'number_of_posts' ) . '" type="number" size="3" min="0" max="100" value="' . esc_attr( isset( $instance['number_of_posts'] ) ? $instance['number_of_posts'] : $this->pvc_defaults['number_of_posts'] ) . '" /> |
| 143 | </p> |
| 144 | <p> |
| 145 | <label for="' . $this->get_field_id( 'no_posts_message' ) . '">' . __( 'No posts message', 'post-views-counter' ) . ':</label> |
| 146 | <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'] ) . '" /> |
| 147 | </p> |
| 148 | <p> |
| 149 | <label for="' . $this->get_field_id( 'order' ) . '">' . __( 'Order', 'post-views-counter' ) . ':</label> |
| 150 | <select id="' . $this->get_field_id( 'order' ) . '" name="' . $this->get_field_name( 'order' ) . '">'; |
| 151 | |
| 152 | // order types |
| 153 | foreach ( $this->pvc_order_types as $id => $order ) { |
| 154 | $html .= ' |
| 155 | <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['order'] ) ? $instance['order'] : $this->pvc_defaults['order'] ), false ) . '>' . esc_html( $order ) . '</option>'; |
| 156 | } |
| 157 | |
| 158 | $html .= ' |
| 159 | </select> |
| 160 | </p> |
| 161 | <p> |
| 162 | <label for="' . $this->get_field_id( 'list_type' ) . '">' . __( 'Display Style', 'post-views-counter' ) . ':</label> |
| 163 | <select id="' . $this->get_field_id( 'list_type' ) . '" name="' . $this->get_field_name( 'list_type' ) . '">'; |
| 164 | |
| 165 | // list types |
| 166 | foreach ( $this->pvc_list_types as $id => $list_type ) { |
| 167 | $html .= ' |
| 168 | <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['list_type'] ) ? $instance['list_type'] : $this->pvc_defaults['list_type'] ), false ) . '>' . esc_html( $list_type ) . '</option>'; |
| 169 | } |
| 170 | |
| 171 | $html .= ' |
| 172 | </select> |
| 173 | </p> |
| 174 | <p> |
| 175 | <input id="' . $this->get_field_id( 'show_post_views' ) . '" type="checkbox" name="' . $this->get_field_name( 'show_post_views' ) . '" value="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> |
| 176 | <br /> |
| 177 | <input id="' . $this->get_field_id( 'show_post_excerpt' ) . '" type="checkbox" name="' . $this->get_field_name( 'show_post_excerpt' ) . '" value="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> |
| 178 | <br /> |
| 179 | <input id="' . $this->get_field_id( 'show_post_author' ) . '" type="checkbox" name="' . $this->get_field_name( 'show_post_author' ) . '" value="show_post_author" ' . checked( true, ( isset( $instance['show_post_author'] ) ? $instance['show_post_author'] : $this->pvc_defaults['show_post_author'] ), false ) . ' /> <label for="' . $this->get_field_id( 'show_post_author' ) . '">' . __( 'Display post author?', 'post-views-counter' ) . '</label> |
| 180 | <br /> |
| 181 | <input id="' . $this->get_field_id( 'show_post_thumbnail' ) . '" class="pvc-show-post-thumbnail" type="checkbox" name="' . $this->get_field_name( 'show_post_thumbnail' ) . '" value="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> |
| 182 | </p> |
| 183 | <p class="pvc-post-thumbnail-size"' . ( $show_post_thumbnail ? '' : ' style="display: none;"' ) . '> |
| 184 | <label for="' . $this->get_field_id( 'thumbnail_size' ) . '">' . __( 'Thumbnail size', 'post-views-counter' ) . ':</label> |
| 185 | <select id="' . $this->get_field_id( 'thumbnail_size' ) . '" name="' . $this->get_field_name( 'thumbnail_size' ) . '">'; |
| 186 | |
| 187 | $size_type = isset( $instance['thumbnail_size'] ) ? $instance['thumbnail_size'] : $this->pvc_defaults['thumbnail_size']; |
| 188 | |
| 189 | // image sizes |
| 190 | foreach ( $this->pvc_image_sizes as $size ) { |
| 191 | $html .= ' |
| 192 | <option value="' . esc_attr( $size ) . '" ' . selected( $size, $size_type, false ) . '>' . esc_html( $size ) . '</option>'; |
| 193 | } |
| 194 | |
| 195 | $html .= ' |
| 196 | </select> |
| 197 | </p>'; |
| 198 | |
| 199 | echo $html; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Save widget form. |
| 204 | * |
| 205 | * @param array $new_instance |
| 206 | * @param array $old_instance |
| 207 | * @return array |
| 208 | */ |
| 209 | public function update( $new_instance, $old_instance ) { |
| 210 | // number of posts |
| 211 | $old_instance['number_of_posts'] = (int) (isset( $new_instance['number_of_posts'] ) ? $new_instance['number_of_posts'] : $this->pvc_defaults['number_of_posts']); |
| 212 | |
| 213 | // order |
| 214 | $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']; |
| 215 | |
| 216 | // list type |
| 217 | $old_instance['list_type'] = isset( $new_instance['list_type'] ) && in_array( $new_instance['list_type'], array_keys( $this->pvc_list_types ), true ) ? $new_instance['list_type'] : $this->pvc_defaults['list_type']; |
| 218 | |
| 219 | // thumbnail size |
| 220 | $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']; |
| 221 | |
| 222 | // booleans |
| 223 | $old_instance['show_post_views'] = ! empty( $new_instance['show_post_views'] ); |
| 224 | $old_instance['show_post_thumbnail'] = ! empty( $new_instance['show_post_thumbnail'] ); |
| 225 | $old_instance['show_post_excerpt'] = ! empty( $new_instance['show_post_excerpt'] ); |
| 226 | $old_instance['show_post_author'] = ! empty( $new_instance['show_post_author'] ); |
| 227 | |
| 228 | // texts |
| 229 | $old_instance['title'] = sanitize_text_field( isset( $new_instance['title'] ) ? $new_instance['title'] : $this->pvc_defaults['title'] ); |
| 230 | $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'] ); |
| 231 | |
| 232 | // post types |
| 233 | if ( isset( $new_instance['post_type'] ) ) { |
| 234 | $post_types = []; |
| 235 | |
| 236 | // get post types |
| 237 | $_post_types = Post_Views_Counter()->functions->get_post_types(); |
| 238 | |
| 239 | foreach ( $new_instance['post_type'] as $post_type ) { |
| 240 | if ( isset( $_post_types[$post_type] ) ) |
| 241 | $post_types[] = $post_type; |
| 242 | } |
| 243 | |
| 244 | $old_instance['post_type'] = array_unique( $post_types ); |
| 245 | } else |
| 246 | $old_instance['post_type'] = [ 'post' ]; |
| 247 | |
| 248 | return $old_instance; |
| 249 | } |
| 250 | } |