| 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( 1 ) ); |
| 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( |
| 129 |
$id, |
| 130 |
'daily', |
| 131 |
$blog_id, |
| 132 |
array( 'from_date' => current_time( 'Y-m-d' ) . ' 00:00:00' ) |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Format output based on column name. |
| 138 |
* |
| 139 |
* @param array $counts Array of count values. |
| 140 |
* @param string $column_name Name of the column. |
| 141 |
* @return string |
| 142 |
*/ |
| 143 |
private static function format_output( $counts, $column_name ) { |
| 144 |
switch ( $column_name ) { |
| 145 |
case 'tptn_total': |
| 146 |
$output = Helpers::number_format_i18n( $counts['total'] ); |
| 147 |
break; |
| 148 |
case 'tptn_daily': |
| 149 |
$output = Helpers::number_format_i18n( $counts['daily'] ); |
| 150 |
break; |
| 151 |
case 'tptn_both': |
| 152 |
$output = Helpers::number_format_i18n( $counts['total'] ) . ' / ' . Helpers::number_format_i18n( $counts['daily'] ); |
| 153 |
break; |
| 154 |
default: |
| 155 |
$output = ''; |
| 156 |
break; |
| 157 |
} |
| 158 |
return $output; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Register the columns as sortable. |
| 163 |
* |
| 164 |
* @since 1.9.8.2 |
| 165 |
* |
| 166 |
* @param array $cols Array with column names. |
| 167 |
* @return array Filtered columns array |
| 168 |
*/ |
| 169 |
public static function add_columns_register_sortable( $cols ) { |
| 170 |
|
| 171 |
if ( \tptn_get_option( 'pv_in_admin' ) ) { |
| 172 |
$cols['tptn_total'] = array( 'tptn_total', true ); |
| 173 |
$cols['tptn_daily'] = array( 'tptn_daily', true ); |
| 174 |
} |
| 175 |
|
| 176 |
return $cols; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Add custom post clauses to sort the columns. |
| 181 |
* |
| 182 |
* @since 3.3.0 |
| 183 |
* |
| 184 |
* @param array $clauses Lookup clauses. |
| 185 |
* @param \WP_Query $wp_query WP Query object. |
| 186 |
* @return array Filtered clauses |
| 187 |
*/ |
| 188 |
public static function add_columns_clauses( $clauses, $wp_query ) { |
| 189 |
global $wpdb; |
| 190 |
|
| 191 |
$orderby = $wp_query->get( 'orderby' ); |
| 192 |
if ( ! in_array( $orderby, array( 'tptn_total', 'tptn_daily' ), true ) ) { |
| 193 |
return $clauses; |
| 194 |
} |
| 195 |
|
| 196 |
$is_daily = ( 'tptn_daily' === $orderby ); |
| 197 |
$table_name = Database::get_table( $is_daily ); |
| 198 |
$blog_id = get_current_blog_id(); |
| 199 |
$order = ( 'ASC' === strtoupper( (string) $wp_query->get( 'order' ) ) ) ? 'ASC' : 'DESC'; |
| 200 |
|
| 201 |
if ( $is_daily ) { |
| 202 |
$from_date = Helpers::get_from_date( current_time( 'Y-m-d' ) . ' 00:00:00', 0, 0 ); |
| 203 |
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is generated internally. |
| 204 |
$clauses['join'] .= $wpdb->prepare( |
| 205 |
" LEFT JOIN {$table_name} AS tptn_counts |
| 206 |
ON {$wpdb->posts}.ID = tptn_counts.postnumber |
| 207 |
AND tptn_counts.blog_id = %d |
| 208 |
AND tptn_counts.dp_date >= %s", |
| 209 |
$blog_id, |
| 210 |
$from_date |
| 211 |
); |
| 212 |
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 213 |
|
| 214 |
$clauses['fields'] .= ', COALESCE(SUM(tptn_counts.cntaccess), 0) AS tptn_visits'; |
| 215 |
if ( empty( $clauses['groupby'] ) ) { |
| 216 |
$clauses['groupby'] = "{$wpdb->posts}.ID"; |
| 217 |
} elseif ( false === strpos( $clauses['groupby'], "{$wpdb->posts}.ID" ) ) { |
| 218 |
$clauses['groupby'] .= ", {$wpdb->posts}.ID"; |
| 219 |
} |
| 220 |
} else { |
| 221 |
$clauses['fields'] .= ', COALESCE(tptn_counts.cntaccess, 0) AS tptn_visits'; |
| 222 |
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is generated internally. |
| 223 |
$clauses['join'] .= $wpdb->prepare( |
| 224 |
" LEFT JOIN {$table_name} AS tptn_counts ON {$wpdb->posts}.ID = tptn_counts.postnumber AND tptn_counts.blog_id = %d", |
| 225 |
$blog_id |
| 226 |
); |
| 227 |
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 228 |
} |
| 229 |
|
| 230 |
$clauses['orderby'] = "tptn_visits {$order}"; |
| 231 |
|
| 232 |
/** |
| 233 |
* Filters the query clauses used to sort the admin posts list by view count. |
| 234 |
* |
| 235 |
* @since 3.4.0-RC2 |
| 236 |
* |
| 237 |
* @param array $clauses The query clauses, keyed by clause name. |
| 238 |
* @param \WP_Query $wp_query The WP_Query instance. |
| 239 |
*/ |
| 240 |
return apply_filters( 'tptn_posts_clauses', $clauses, $wp_query ); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Output CSS for width of new column. |
| 245 |
* |
| 246 |
* @since 1.2 |
| 247 |
*/ |
| 248 |
public static function admin_css() { |
| 249 |
?> |
| 250 |
<style type="text/css"> |
| 251 |
#tptn_total, #tptn_daily, #tptn_both { max-width: 100px; } |
| 252 |
</style> |
| 253 |
<?php |
| 254 |
} |
| 255 |
} |
| 256 |
|