-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopular-post-widget.php
More file actions
216 lines (176 loc) · 8.07 KB
/
popular-post-widget.php
File metadata and controls
216 lines (176 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
/**
* Plugin Name: Popular Post Widget With Thumbnail
* Plugin URI: https://github.com/ikamal7/Popular-Posts-Widget-with-Thumbnail/
* Description: Popular posts widget show popular posts in your site with thumbnail
* Author: Kamal Hosen
* Version: 1.0
* Author URI: https://ikamal.me/
* License: GPLv2 or later
* Text Domain: popular-post-widget
* Domain Path: /languages/
*/
/*
Copyright (C) 2018 Kamal kamalhosen8920@gmail.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
//Include file
require( plugin_dir_path( __FILE__ ) . '/bootstrap.php' );
/**
* Set Post views
*/
function ppw_set_popular_post_view( $postID ) {
$countkey = 'views';
$count = get_post_meta( $postID, $countkey, true );
if ( '' == $countkey ){
delete_post_meta( $postID, $countkey );
add_post_meta( $postID, $countkey, '0' );
}
else {
$count++;
update_post_meta( $postID, $countkey, $count );
}
}
/**
* Track Post views
*/
function ppw_track_popular_post_view( $post_id ) {
if ( ! is_single() ){
return;
}
if ( empty( $post_id ) ){
global $post;
$post_id = $post->ID;
}
ppw_set_popular_post_view( $post_id );
}
add_action( 'wp_head', 'ppw_track_popular_post_view' );
/**
* new WordPress Widget format
* Wordpress 2.8 and above
* @see http://codex.wordpress.org/Widgets_API#Developing_Widgets
*/
class Popular_Post_Widget extends WP_Widget {
/**
* Constructor
*
* @return void
*/
function __construct() {
$widget_ops = array(
'classname' => 'popularpost',
'description' => __('Popular Posts Widget with Thumbnail', 'popular-post-widget')
);
parent::__construct( 'popularpost', __('Popular Posts Widget with Thumbnail', 'popular-post-widget'), $widget_ops );
}
/**
* Outputs the HTML for this widget.
*
* @param array An array of standard parameters for widgets in this theme
* @param array An array of settings for this widget instance
*
* @return void Echoes it's output
*/
function widget( $args, $instance ) {
$title = ! empty( $instance[ 'title' ] ) ? $instance[ 'title' ] : __( 'Popular Posts', 'popular-post-widget' );
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$number = ( ! empty( $instance[ 'number' ] ) ) ? absint( $instance[ 'number' ] ) : 5;
if ( ! $number ){
$number = 5;
}
$show_date = isset( $instance[ 'show_date' ] ) ? $instance[ 'show_date' ] : false;
echo $args[ 'before_widget' ];
if ( $title ){
echo $args[ 'before_title' ] . $title . $args[ 'after_title' ];
}
$ppw_loop = new WP_Query( array(
'posts_per_page' => $number,
'meta_key' => 'views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'ignore_sticky_posts' => true,
) );
?>
<?php if ( $ppw_loop->have_posts() ) : ?>
<ul>
<?php
while ( $ppw_loop->have_posts() ) : $ppw_loop->the_post(); ?>
<li>
<div class="thumbnail">
<?php the_post_thumbnail( 'thumbnail' ) ?>
</div>
<div class="post-info">
<a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4></a>
<?php if ( $show_date ){
echo get_the_date();
} ?>
</div>
</li>
<?php endwhile;
wp_reset_query(); ?>
</ul>
<?php endif; ?>
<?php
echo $args[ 'after_widget' ];
}
/**
* Deals with the settings when they are saved by the admin. Here is
* where any validation should be dealt with.
*
* @param array An array of new settings as submitted by the admin
* @param array An array of the previous settings
*
* @return array The validated and (if necessary) amended settings
*/
function update( $new_instance, $old_instance ) {
// update logic goes here
$instance = $new_instance;
$instance[ 'title' ] = sanitize_text_field( $new_instance[ 'title' ] );
$instance[ 'number' ] = (int) $new_instance[ 'number' ];
$instance[ 'show_date' ] = isset( $new_instance[ 'show_date' ] ) ? (bool) $new_instance[ 'show_date' ] : false;
return $instance;
}
/**
* Displays the form for this widget on the Widgets page of the WP Admin area.
*
* @param array An array of the current settings for this widget
*
* @return void Echoes it's output
*/
function form( $instance ) {
$title = isset( $instance[ 'title' ] ) ? esc_attr( $instance[ 'title' ] ) : '';
$number = isset( $instance[ 'number' ] ) ? absint( $instance[ 'number' ] ) : 5;
$show_date = isset( $instance[ 'show_date' ] ) ? (bool) $instance[ 'show_date' ] : false;
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title', 'popular-post-widget'); ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>"
value="<?php echo $title; ?>" name="<?php echo $this->get_field_name( 'title' ); ?>"/>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:', 'popular-post-widget') ?></label>
<input type="numberr" class="tiny-text" id="<?php echo $this->get_field_id( 'number' ); ?>"
name="<?php echo $this->get_field_name( 'number' ); ?>" type="number" step="1" min="1"
value="<?php echo $number; ?>" size="3"/>
</p>
<p><input class="checkbox" type="checkbox"<?php checked( $show_date ); ?>
id="<?php echo $this->get_field_id( 'show_date' ); ?>"
name="<?php echo $this->get_field_name( 'show_date' ); ?>"/>
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?', 'popular-post-widget' ); ?></label>
</p>
<?php
}
}
function ppw_register_widget() {
register_widget( 'Popular_Post_Widget' );
}
add_action( 'widgets_init', 'ppw_register_widget' );