|
<?php |
|
/** |
|
* Plugin Name: WordPress Popular Posts Sortable Columns |
|
* Plugin URI: https://wordpress.org/plugins/wordpress-popular-posts/ |
|
* Description: Adds sortable Views column to the admin area. |
|
* Version: 1.0.2 |
|
* Requires at least: 5.3 |
|
* Requires PHP: 7.2 |
|
* Author: Hector Cabrera |
|
* Author URI: https://cabrerahector.com/ |
|
* License: GPL-2.0+ |
|
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt |
|
* Text Domain: wordpress-popular-posts |
|
* Domain Path: /languages |
|
*/ |
|
|
|
namespace WordPressPopularPosts; |
|
|
|
if ( ! defined('WPINC') ) { |
|
die(); |
|
} |
|
|
|
/** |
|
* Array of post type(s) we want to sort by views count. |
|
*/ |
|
$post_types_with_views_column = array('post', 'page'); |
|
|
|
/** |
|
* Adds Views column to item list. |
|
* |
|
* @param array $columns |
|
* @return array |
|
*/ |
|
function item_list_add_views_column( $columns ) { |
|
$columns['pageviews'] = __('Views', 'wordpress-popular-posts'); |
|
return $columns; |
|
} |
|
|
|
/** |
|
* Displays the actual views count of each post in the list. |
|
* |
|
* @param array $column |
|
* @param int $post_id |
|
*/ |
|
function item_list_views_column($column, $post_id) { |
|
if ( $column == 'pageviews' ){ |
|
echo wpp_get_views($post_id); |
|
} |
|
} |
|
|
|
/** |
|
* Makes the Views column sortable. |
|
* |
|
* @param array $columns |
|
* @return array |
|
*/ |
|
function item_list_sortable_columns($columns) { |
|
$columns['pageviews'] = 'pageviews'; |
|
return $columns; |
|
} |
|
|
|
/** |
|
* Adds the pageviews field to the query. |
|
* |
|
* @param string $fields |
|
* @param object $wp_query |
|
* @return string |
|
*/ |
|
function item_list_query_fields($fields, $wp_query) { |
|
global $pagenow, $wpdb, $post_types_with_views_column; |
|
|
|
if ( |
|
is_admin() |
|
&& 'edit.php' == $pagenow |
|
&& isset($wp_query->query['post_type']) |
|
&& in_array($wp_query->query['post_type'], $post_types_with_views_column) |
|
) { |
|
$fields .= ", IFNULL(`{$wpdb->prefix}popularpostsdata`.`pageviews`, 0) AS pageviews"; |
|
} |
|
|
|
return $fields; |
|
} |
|
add_filter('posts_fields', __NAMESPACE__ . '\item_list_query_fields', 10, 2); |
|
|
|
/** |
|
* Joins WPP's data table. |
|
* |
|
* @param string $join |
|
* @return string |
|
*/ |
|
function item_list_query_join($join) { |
|
global $wp_query, $pagenow, $wpdb, $post_types_with_views_column; |
|
|
|
if ( |
|
is_admin() |
|
&& 'edit.php' == $pagenow |
|
&& isset($wp_query->query['post_type']) |
|
&& in_array($wp_query->query['post_type'], $post_types_with_views_column) |
|
) { |
|
$join .= "LEFT JOIN `{$wpdb->prefix}popularpostsdata` ON `{$wpdb->posts}`.`ID` = `{$wpdb->prefix}popularpostsdata`.`postid` "; |
|
} |
|
|
|
return $join; |
|
} |
|
add_filter('posts_join', __NAMESPACE__ . '\item_list_query_join'); |
|
|
|
/** |
|
* Sort items by pageviews on demand. |
|
* |
|
* @param string $orderby_statement |
|
* @param object $wp_query |
|
* @return string |
|
*/ |
|
function item_list_orderby($orderby_statement, $wp_query) { |
|
global $pagenow, $post_types_with_views_column; |
|
|
|
if ( |
|
is_admin() |
|
&& 'edit.php' == $pagenow |
|
&& isset($wp_query->query['post_type']) |
|
&& in_array($wp_query->query['post_type'], $post_types_with_views_column) |
|
&& ( isset($wp_query->query['orderby']) && 'pageviews' == $wp_query->query['orderby'] ) |
|
) { |
|
$orderby_statement = "pageviews " . ( isset($wp_query->query['order']) && $wp_query->query['order'] == 'asc' ? 'ASC' : 'DESC' ); |
|
} |
|
|
|
return $orderby_statement; |
|
} |
|
add_filter('posts_orderby', __NAMESPACE__ . '\item_list_orderby', 10, 2); |
|
|
|
/** |
|
* Finally, add various hooks so our Views column works! |
|
*/ |
|
foreach($post_types_with_views_column as $post_type) { |
|
add_filter('manage_' . $post_type . '_posts_columns' , __NAMESPACE__ . '\item_list_add_views_column'); |
|
add_action('manage_' . $post_type . '_posts_custom_column' , __NAMESPACE__ . '\item_list_views_column', 10, 2); |
|
add_filter('manage_edit-' . $post_type . '_sortable_columns', __NAMESPACE__ . '\item_list_sortable_columns'); |
|
} |
|
|
|
/** |
|
* Adjust the width of the Views column. |
|
*/ |
|
function views_column_width() { |
|
echo '<style type="text/css">'; |
|
echo '.column-pageviews { overflow: hidden; width: 100px; }'; |
|
echo '</style>'; |
|
} |
|
add_action('admin_head', __NAMESPACE__ . '\views_column_width'); |
Excellent thx! I was stuck on the sorting part :)