PluginProbe
WebberZone Top 10 — Popular Posts / 4.3.2
WebberZone Top 10 — Popular Posts v4.3.2
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.2, at includes/admin/class-columns.php

243 lines 6.5 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 /* translators: %s: Custom period label (e.g. Daily, Custom (7 days)). */
73 $cols['tptn_daily'] = sprintf( __( '%s Views', 'top-10' ), Helpers::get_daily_range_label() );
74 $cols['tptn_both'] = __( 'Views', 'top-10' );
75 }
76 }
77 return $cols;
78 }
79
80 /**
81 * Display page views for each column.
82 *
83 * @since 4.0.0
84 *
85 * @param string $column_name Name of the column.
86 * @param int|string $id Post ID.
87 */
88 public static function get_value( $column_name, $id ) {
89 if ( ! \tptn_get_option( 'pv_in_admin' ) ) {
90 return;
91 }
92
93 global $wpdb;
94 $blog_id = get_current_blog_id();
95
96 $counts = array();
97
98 if ( in_array( $column_name, array( 'tptn_total', 'tptn_both' ), true ) ) {
99 $counts['total'] = self::get_total_count( $id, $blog_id );
100 }
101
102 if ( in_array( $column_name, array( 'tptn_daily', 'tptn_both' ), true ) ) {
103 $counts['daily'] = self::get_daily_count( $id, $blog_id );
104 }
105
106 echo esc_html( self::format_output( $counts, $column_name ) );
107 }
108
109 /**
110 * Get total view count.
111 *
112 * @param int $id Post ID.
113 * @param int $blog_id Blog ID.
114 * @return int
115 */
116 private static function get_total_count( $id, $blog_id ) {
117 return Counter::get_post_count_only( $id, 'total', $blog_id );
118 }
119
120 /**
121 * Get daily view count.
122 *
123 * @param int $id Post ID.
124 * @param int $blog_id Blog ID.
125 * @return int
126 */
127 private static function get_daily_count( $id, $blog_id ) {
128 return Counter::get_post_count_only( $id, 'daily', $blog_id );
129 }
130
131 /**
132 * Format output based on column name.
133 *
134 * @param array $counts Array of count values.
135 * @param string $column_name Name of the column.
136 * @return string
137 */
138 private static function format_output( $counts, $column_name ) {
139 switch ( $column_name ) {
140 case 'tptn_total':
141 $output = Helpers::number_format_i18n( $counts['total'] );
142 break;
143 case 'tptn_daily':
144 $output = Helpers::number_format_i18n( $counts['daily'] );
145 break;
146 case 'tptn_both':
147 $output = Helpers::number_format_i18n( $counts['total'] ) . ' / ' . Helpers::number_format_i18n( $counts['daily'] );
148 break;
149 default:
150 $output = '';
151 break;
152 }
153 return $output;
154 }
155
156 /**
157 * Register the columns as sortable.
158 *
159 * @since 1.9.8.2
160 *
161 * @param array $cols Array with column names.
162 * @return array Filtered columns array
163 */
164 public static function add_columns_register_sortable( $cols ) {
165
166 if ( \tptn_get_option( 'pv_in_admin' ) ) {
167 $cols['tptn_total'] = array( 'tptn_total', true );
168 $cols['tptn_daily'] = array( 'tptn_daily', true );
169 }
170
171 return $cols;
172 }
173
174 /**
175 * Add custom post clauses to sort the columns.
176 *
177 * @since 3.3.0
178 *
179 * @param array $clauses Lookup clauses.
180 * @param \WP_Query $wp_query WP Query object.
181 * @return array Filtered clauses
182 */
183 public static function add_columns_clauses( $clauses, $wp_query ) {
184 global $wpdb;
185
186 $orderby = $wp_query->get( 'orderby' );
187 if ( ! in_array( $orderby, array( 'tptn_total', 'tptn_daily' ), true ) ) {
188 return $clauses;
189 }
190
191 $is_daily = ( 'tptn_daily' === $orderby );
192 $table_name = Database::get_table( $is_daily );
193 $blog_id = get_current_blog_id();
194 $order = ( 'ASC' === strtoupper( (string) $wp_query->get( 'order' ) ) ) ? 'ASC' : 'DESC';
195
196 if ( $is_daily ) {
197 $from_date = Helpers::get_from_date();
198 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is generated internally.
199 $clauses['join'] .= $wpdb->prepare(
200 " LEFT JOIN {$table_name} AS tptn_counts
201 ON {$wpdb->posts}.ID = tptn_counts.postnumber
202 AND tptn_counts.blog_id = %d
203 AND tptn_counts.dp_date >= %s",
204 $blog_id,
205 $from_date
206 );
207 // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
208
209 $clauses['fields'] .= ', COALESCE(SUM(tptn_counts.cntaccess), 0) AS tptn_visits';
210 if ( empty( $clauses['groupby'] ) ) {
211 $clauses['groupby'] = "{$wpdb->posts}.ID";
212 } elseif ( false === strpos( $clauses['groupby'], "{$wpdb->posts}.ID" ) ) {
213 $clauses['groupby'] .= ", {$wpdb->posts}.ID";
214 }
215 } else {
216 $clauses['fields'] .= ', COALESCE(tptn_counts.cntaccess, 0) AS tptn_visits';
217 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is generated internally.
218 $clauses['join'] .= $wpdb->prepare(
219 " LEFT JOIN {$table_name} AS tptn_counts ON {$wpdb->posts}.ID = tptn_counts.postnumber AND tptn_counts.blog_id = %d",
220 $blog_id
221 );
222 // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
223 }
224
225 $clauses['orderby'] = "tptn_visits {$order}";
226
227 return apply_filters( 'tptn_posts_clauses', $clauses, $wp_query );
228 }
229
230 /**
231 * Output CSS for width of new column.
232 *
233 * @since 1.2
234 */
235 public static function admin_css() {
236 ?>
237 <style type="text/css">
238 #tptn_total, #tptn_daily, #tptn_both { max-width: 100px; }
239 </style>
240 <?php
241 }
242 }
243