PluginProbe
WebberZone Top 10 — Popular Posts / 4.3.1
WebberZone Top 10 — Popular Posts v4.3.1
4.5.1 4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 1.6.1 All 117 releases
top-10 / includes / admin / class-columns.php

class-columns.php in WebberZone Top 10 — Popular Posts 4.3.1, at includes/admin/class-columns.php

242 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Columns class.
4 *
5 * @package WebberZone\Top_Ten\Admin
6 */
7
8 namespace WebberZone\Top_Ten\Admin;
9
10 use WebberZone\Top_Ten\Database;
11 use WebberZone\Top_Ten\Counter;
12 use WebberZone\Top_Ten\Util\Helpers;
13 use WebberZone\Top_Ten\Util\Hook_Registry;
14
15 if ( ! defined( 'WPINC' ) ) {
16 die;
17 }
18
19 /**
20 * Admin Columns Class.
21 *
22 * @since 3.3.0
23 */
24 class Columns {
25
26 /**
27 * Constructor class.
28 *
29 * @since 3.3.0
30 */
31 public function __construct() {
32 $this->init();
33 }
34
35 /**
36 * Initialize hooks and filters.
37 *
38 * @since 4.0.0
39 */
40 private function init() {
41
42 /**
43 * Filter the post types to add the columns to.
44 *
45 * @since 4.0.0
46 *
47 * @param array $post_types Array of post types.
48 */
49 $post_types = apply_filters( 'tptn_admin_column_post_types', array( 'post', 'page' ) );
50
51 foreach ( $post_types as $post_type ) {
52 Hook_Registry::add_filter( "manage_{$post_type}_posts_columns", array( $this, 'add_columns' ) );
53 Hook_Registry::add_action( "manage_{$post_type}_posts_custom_column", array( $this, 'get_value' ), 10, 2 );
54 Hook_Registry::add_filter( "manage_edit-{$post_type}_sortable_columns", array( $this, 'add_columns_register_sortable' ) );
55 }
56
57 Hook_Registry::add_filter( 'posts_clauses', array( $this, 'add_columns_clauses' ), 10, 2 );
58 Hook_Registry::add_action( 'admin_head', array( $this, 'admin_css' ) );
59 }
60
61 /**
62 * Add an extra column to the All Posts page to display the page views.
63 *
64 * @param array $cols Array of all columns on posts page.
65 * @return array Modified array of columns.
66 */
67 public static function add_columns( $cols ) {
68
69 if ( ( current_user_can( 'manage_options' ) ) || ( \tptn_get_option( 'show_count_non_admins' ) ) ) {
70 if ( \tptn_get_option( 'pv_in_admin' ) ) {
71 $cols['tptn_total'] = __( 'Total Views', 'top-10' );
72 $cols['tptn_daily'] = __( "Today's Views", 'top-10' );
73 $cols['tptn_both'] = __( 'Views', 'top-10' );
74 }
75 }
76 return $cols;
77 }
78
79 /**
80 * Display page views for each column.
81 *
82 * @since 4.0.0
83 *
84 * @param string $column_name Name of the column.
85 * @param int|string $id Post ID.
86 */
87 public static function get_value( $column_name, $id ) {
88 if ( ! \tptn_get_option( 'pv_in_admin' ) ) {
89 return;
90 }
91
92 global $wpdb;
93 $blog_id = get_current_blog_id();
94
95 $counts = array();
96
97 if ( in_array( $column_name, array( 'tptn_total', 'tptn_both' ), true ) ) {
98 $counts['total'] = self::get_total_count( $id, $blog_id );
99 }
100
101 if ( in_array( $column_name, array( 'tptn_daily', 'tptn_both' ), true ) ) {
102 $counts['daily'] = self::get_daily_count( $id, $blog_id );
103 }
104
105 echo esc_html( self::format_output( $counts, $column_name ) );
106 }
107
108 /**
109 * Get total view count.
110 *
111 * @param int $id Post ID.
112 * @param int $blog_id Blog ID.
113 * @return int
114 */
115 private static function get_total_count( $id, $blog_id ) {
116 return Counter::get_post_count_only( $id, 'total', $blog_id );
117 }
118
119 /**
120 * Get daily view count.
121 *
122 * @param int $id Post ID.
123 * @param int $blog_id Blog ID.
124 * @return int
125 */
126 private static function get_daily_count( $id, $blog_id ) {
127 return Counter::get_post_count_only( $id, 'daily', $blog_id );
128 }
129
130 /**
131 * Format output based on column name.
132 *
133 * @param array $counts Array of count values.
134 * @param string $column_name Name of the column.
135 * @return string
136 */
137 private static function format_output( $counts, $column_name ) {
138 switch ( $column_name ) {
139 case 'tptn_total':
140 $output = Helpers::number_format_i18n( $counts['total'] );
141 break;
142 case 'tptn_daily':
143 $output = Helpers::number_format_i18n( $counts['daily'] );
144 break;
145 case 'tptn_both':
146 $output = Helpers::number_format_i18n( $counts['total'] ) . ' / ' . Helpers::number_format_i18n( $counts['daily'] );
147 break;
148 default:
149 $output = '';
150 break;
151 }
152 return $output;
153 }
154
155 /**
156 * Register the columns as sortable.
157 *
158 * @since 1.9.8.2
159 *
160 * @param array $cols Array with column names.
161 * @return array Filtered columns array
162 */
163 public static function add_columns_register_sortable( $cols ) {
164
165 if ( \tptn_get_option( 'pv_in_admin' ) ) {
166 $cols['tptn_total'] = array( 'tptn_total', true );
167 $cols['tptn_daily'] = array( 'tptn_daily', true );
168 }
169
170 return $cols;
171 }
172
173 /**
174 * Add custom post clauses to sort the columns.
175 *
176 * @since 3.3.0
177 *
178 * @param array $clauses Lookup clauses.
179 * @param \WP_Query $wp_query WP Query object.
180 * @return array Filtered clauses
181 */
182 public static function add_columns_clauses( $clauses, $wp_query ) {
183 global $wpdb;
184
185 $orderby = $wp_query->get( 'orderby' );
186 if ( ! in_array( $orderby, array( 'tptn_total', 'tptn_daily' ), true ) ) {
187 return $clauses;
188 }
189
190 $is_daily = ( 'tptn_daily' === $orderby );
191 $table_name = Database::get_table( $is_daily );
192 $blog_id = get_current_blog_id();
193 $order = ( 'ASC' === strtoupper( (string) $wp_query->get( 'order' ) ) ) ? 'ASC' : 'DESC';
194
195 if ( $is_daily ) {
196 $from_date = Helpers::get_from_date();
197 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is generated internally.
198 $clauses['join'] .= $wpdb->prepare(
199 " LEFT JOIN {$table_name} AS tptn_counts
200 ON {$wpdb->posts}.ID = tptn_counts.postnumber
201 AND tptn_counts.blog_id = %d
202 AND tptn_counts.dp_date >= %s",
203 $blog_id,
204 $from_date
205 );
206 // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
207
208 $clauses['fields'] .= ', COALESCE(SUM(tptn_counts.cntaccess), 0) AS tptn_visits';
209 if ( empty( $clauses['groupby'] ) ) {
210 $clauses['groupby'] = "{$wpdb->posts}.ID";
211 } elseif ( false === strpos( $clauses['groupby'], "{$wpdb->posts}.ID" ) ) {
212 $clauses['groupby'] .= ", {$wpdb->posts}.ID";
213 }
214 } else {
215 $clauses['fields'] .= ', COALESCE(tptn_counts.cntaccess, 0) AS tptn_visits';
216 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is generated internally.
217 $clauses['join'] .= $wpdb->prepare(
218 " LEFT JOIN {$table_name} AS tptn_counts ON {$wpdb->posts}.ID = tptn_counts.postnumber AND tptn_counts.blog_id = %d",
219 $blog_id
220 );
221 // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
222 }
223
224 $clauses['orderby'] = "tptn_visits {$order}";
225
226 return apply_filters( 'tptn_posts_clauses', $clauses, $wp_query );
227 }
228
229 /**
230 * Output CSS for width of new column.
231 *
232 * @since 1.2
233 */
234 public static function admin_css() {
235 ?>
236 <style type="text/css">
237 #tptn_total, #tptn_daily, #tptn_both { max-width: 100px; }
238 </style>
239 <?php
240 }
241 }
242