Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add post views column to admin post table #294

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions inc/class-statify-counter-column.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
/**
* Statify: Statify_Counter_Column class
*
* This file contains the derived class for the counter column in the post table.
*
* @package Statify
*/

// Quit if accessed outside WP context.
defined( 'ABSPATH' ) || exit;

/**
* Statify_Counter_Column
*
* This class manages the counter column in the post table to display the number of views for each post.
*/
class Statify_Counter_Column extends Statify {

public const COLUMN_NAME = 'counter-column';
public const SUPPORTED_POST_TYPE = array(
'post',
'page',
);

/**
* Initializes the counter column in the post table.
*
* This method adds the necessary actions to register the counter column and manage its display.
*
* @return void
*/
public static function init(): void {

// Filter user_can_see_stats.
if ( ! self::user_can_see_stats() ) {
return;
}

/**
* Filters the post types that will display the Statify counter column.
*
* @param array $post_types Array of post types.
*/
$post_types = apply_filters( 'statify_counter_post_types', self::SUPPORTED_POST_TYPE );

foreach ( $post_types as $post_type ) {
add_action( "manage_edit-{$post_type}_columns", array( __CLASS__, 'register_counter_column' ) );
add_action( "manage_{$post_type}_posts_custom_column", array( __CLASS__, 'manage_counter_column' ), 10, 2 );
}
}

/**
* Registers the counter column in the post columns.
*
* This method adds the counter column to the post edit screen.
*
* @param array $columns An array of existing columns.
*
* @return array Updated array of columns with the counter column added.
*/
public static function register_counter_column( array $columns ): array {

$columns[ self::COLUMN_NAME ] = __( 'Views', 'statify' );

return $columns;
}

/**
* Manages the display of the counter column for a given post.
*
* This method retrieves and displays the view count for the specified post in the counter column.
*
* @param string $column_name The name of the column being managed.
* @param int $post_id The ID of the post for which the column is being displayed.
*
* @return void
*/
public static function manage_counter_column( string $column_name, int $post_id ): void {

if ( self::COLUMN_NAME !== $column_name ) {
return;
}

$permalink = (string) get_permalink( $post_id );

if ( ! $permalink ) {
return;
}

$target = wp_parse_url( $permalink, PHP_URL_PATH );

echo esc_html( self::post_counter_value( $target ) );
}

/**
* Retrieves the view count for a specific target.
*
* This method queries the database to count the number of views for the given target URL.
*
* @param string $target The target URL path for which the view count is to be retrieved.
*
* @return int The number of views for the specified target.
*/
private static function post_counter_value( string $target ): int {
global $wpdb;

$sql = $wpdb->prepare(
"SELECT COUNT(*) FROM `$wpdb->statify` WHERE `target` = %s",
$target
);

$result = $wpdb->get_var( $sql );

return (int) $result;
}
}
1 change: 1 addition & 0 deletions inc/class-statify.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public static function init(): void {
add_filter( 'plugin_row_meta', array( 'Statify_Backend', 'add_meta_link' ), 10, 2 );
add_filter( 'plugin_action_links_' . STATIFY_BASE, array( 'Statify_Backend', 'add_action_link' ) );
add_action( 'admin_init', array( 'Statify_Settings', 'register_settings' ) );
add_action( 'admin_init', array( 'Statify_Counter_Column', 'init' ) );
add_action( 'admin_menu', array( 'Statify_Settings', 'add_admin_menu' ) );
add_action( 'update_option_statify', array( 'Statify_Settings', 'action_update_options' ), 10, 2 );
} else { // Frontend.
Expand Down
1 change: 1 addition & 0 deletions statify.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ function statify_autoload( $class ) {
'Statify_Settings',
'Statify_Table',
'Statify_Cron',
'Statify_Counter_Column',
);

if ( in_array( $class, $plugin_classes, true ) ) {
Expand Down