PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / trunk
WP Popular Posts vtrunk
4.0.8 4.0.9 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 5.0.0 5.0.1 5.0.2 5.1.0 5.2.0 5.2.1 5.2.2 5.2.3 5.2.4 5.3.0 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.4.0 5.4.1 5.4.2 5.5.0 5.5.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.1.2 6.1.3 6.1.4 6.2.0 6.2.1 6.3.0 6.3.1 6.3.2 6.3.3 6.3.4 6.4.0 6.4.1 6.4.2 7.0.0 7.0.1 7.1.0 7.2.0 7.3.0 7.3.1 7.3.2 7.3.3 7.3.4 7.3.5 7.3.6 7.3.7 7.3.8 7.4.0 trunk 2.3.7 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.2 4.0.3 4.0.5 4.0.6
wordpress-popular-posts / src / Admin / ListColumnTotalViews.php
wordpress-popular-posts / src / Admin Last commit date
Admin.php 3 weeks ago ListColumnTotalViews.php 3 weeks ago admin-page.php 3 weeks ago screen-debug.php 3 weeks ago screen-stats.php 3 weeks ago screen-tools.php 3 weeks ago
ListColumnTotalViews.php
200 lines
1 <?php
2 /**
3 * Renders a "Views" column on post lists.
4 *
5 * @package WordPressPopularPosts
6 * @subpackage WordPressPopularPosts/Admin
7 * @author Hector Cabrera <me@cabrerahector.com>
8 */
9
10 namespace WordPressPopularPosts\Admin;
11
12 class ListColumnTotalViews {
13
14 /**
15 * Post types array.
16 *
17 * @since 7.4.0
18 * @var array
19 * @access private
20 */
21 private $post_types;
22
23 /**
24 * Admin options.
25 *
26 * @since 7.4.0
27 * @var array
28 * @access private
29 */
30 private $config;
31
32 /**
33 * Construct.
34 *
35 * @param array $config Admin options.
36 */
37 public function __construct(array $config)
38 {
39 $this->config = $config;
40
41 if ( $this->config['tools']['views_column']['post_types'] ) {
42 $registered_post_types = get_post_types(['public' => true], 'names');
43
44 $this->post_types = array_values(
45 array_intersect(
46 array_map('trim', explode(',', $this->config['tools']['views_column']['post_types'])),
47 $registered_post_types
48 )
49 );
50 }
51
52 $this->hooks();
53 }
54
55 /**
56 * Registers all the required admin hooks.
57 *
58 * @since 7.4.0
59 */
60 public function hooks()
61 {
62 if ( $this->post_types ) {
63 foreach($this->post_types as $post_type) {
64 add_filter('manage_' . $post_type . '_posts_columns', [$this, 'add_views_column']);
65 add_action('manage_' . $post_type . '_posts_custom_column', [$this, 'views_column'], 10, 2);
66 add_filter('manage_edit-' . $post_type . '_sortable_columns', [$this, 'sortable_columns']);
67 }
68
69 add_filter('posts_join', [$this, 'query_join'], 10, 2);
70 add_filter('posts_fields', [$this, 'query_fields'], 10, 2);
71 add_filter('posts_orderby', [$this, 'order_items_by'], 10, 2);
72
73 add_action('admin_head', [$this, 'views_column_width']);
74 }
75 }
76
77 /**
78 * Adds Views column to item list.
79 *
80 * @since 7.4.0
81 * @param array $columns
82 * @return array
83 */
84 public function add_views_column(array $columns)
85 {
86 $columns['pageviews'] = __('Views', 'wordpress-popular-posts');
87 return $columns;
88 }
89
90 /**
91 * Displays the actual views count of each post in the list.
92 *
93 * @since 7.4.0
94 * @param array $column_name
95 * @param int $post_id
96 */
97 public function views_column(string $column_name, int $post_id)
98 {
99 if ( $column_name === 'pageviews' ) {
100 echo wpp_get_views($post_id);
101 }
102 }
103
104 /**
105 * Makes the Views column sortable.
106 *
107 * @since 7.4.0
108 * @param array $columns
109 * @return array
110 */
111 public function sortable_columns(array $columns)
112 {
113 $columns['pageviews'] = 'pageviews';
114 return $columns;
115 }
116
117 /**
118 * Adds the pageviews field to the query.
119 *
120 * @since 7.4.0
121 * @param string $fields
122 * @param object $wp_query
123 * @return string
124 */
125 public function query_fields(string $fields, $wp_query)
126 {
127 global $pagenow, $wpdb;
128
129 if (
130 is_admin()
131 && 'edit.php' === $pagenow
132 && isset($wp_query->query['post_type'])
133 && in_array($wp_query->query['post_type'], $this->post_types)
134 ) {
135 $fields .= ", IFNULL({$wpdb->prefix}popularpostsdata.pageviews, 0) AS pageviews";
136 }
137
138 return $fields;
139 }
140
141 /**
142 * Joins WPP's data table.
143 *
144 * @since 7.4.0
145 * @param string $join
146 * @param object $wp_query
147 * @return string
148 */
149 public function query_join(string $join, $wp_query)
150 {
151 global $pagenow, $wpdb;
152
153 if (
154 is_admin()
155 && 'edit.php' === $pagenow
156 && isset($wp_query->query['post_type'])
157 && in_array($wp_query->query['post_type'], $this->post_types)
158 ) {
159 $join .= "LEFT JOIN {$wpdb->prefix}popularpostsdata ON {$wpdb->posts}.ID = {$wpdb->prefix}popularpostsdata.postid ";
160 }
161
162 return $join;
163 }
164
165 /**
166 * Sort items by pageviews on demand.
167 *
168 * @since 7.4.0
169 * @param string $orderby_statement
170 * @param object $wp_query
171 * @return string
172 */
173 public function order_items_by(string $orderby_statement, $wp_query)
174 {
175 global $pagenow;
176
177 if (
178 is_admin()
179 && 'edit.php' === $pagenow
180 && isset($wp_query->query['post_type'])
181 && in_array($wp_query->query['post_type'], $this->post_types)
182 && ( isset($wp_query->query['orderby']) && 'pageviews' === $wp_query->query['orderby'] )
183 ) {
184 $orderby_statement = 'pageviews ' . ( isset($wp_query->query['order']) && $wp_query->query['order'] === 'asc' ? 'ASC' : 'DESC' );
185 }
186
187 return $orderby_statement;
188 }
189
190 /**
191 * Adjust the width of the Views column.
192 *
193 * @since 7.4.0
194 */
195 public function views_column_width()
196 {
197 echo '<style>.column-pageviews { width: 150px; }</style>';
198 }
199 }
200