Skip to content

Update the stats icon and add pageview count in the post list #42218

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

Merged
merged 38 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
3068f8b
Admin menu: use wpcom_get_custom_admin_menu_class instead of the clas…
BogdanUngureanu Feb 27, 2025
6183ec8
remove the quick fix from dashboard_switcher_scripts
BogdanUngureanu Feb 27, 2025
0ef329e
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu Mar 3, 2025
a2d0586
Stats: Update UI and move the code in the package
BogdanUngureanu Mar 5, 2025
f83072f
Stats: Update UI and move the code in the package
BogdanUngureanu Mar 5, 2025
a6be580
Fixed fatals
BogdanUngureanu Mar 5, 2025
493ce1a
Fix linting
BogdanUngureanu Mar 5, 2025
799aef7
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu Mar 5, 2025
dece4dc
Add client support for simple sites
BogdanUngureanu Mar 7, 2025
00c5918
Fix linting
BogdanUngureanu Mar 7, 2025
3e1bd48
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu Mar 7, 2025
e713032
Try to fix the linting
BogdanUngureanu Mar 7, 2025
f1c8af8
Add wpcom to the stubs
BogdanUngureanu Mar 7, 2025
e980d13
Refactor WPCOM_Stats implementation and add unit testing
BogdanUngureanu Mar 7, 2025
834040f
Refactor some code, add some changes in the phan directory and add mo…
BogdanUngureanu Mar 10, 2025
6e52101
Handle a scenario and fix failing tests.
BogdanUngureanu Mar 10, 2025
09a8bb0
use onlyMethods
BogdanUngureanu Mar 10, 2025
550139d
Disable phan rule since we already check if it exists.
BogdanUngureanu Mar 10, 2025
e59a54a
Fix linting
BogdanUngureanu Mar 10, 2025
534844d
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu Mar 10, 2025
89301d0
Create a single instance of the formatter
BogdanUngureanu Mar 11, 2025
f554a8a
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu Mar 11, 2025
2a84ebd
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu Mar 11, 2025
d724afe
Update baseline
BogdanUngureanu Mar 11, 2025
248c47e
Add fallback to NumberFormatter
BogdanUngureanu Mar 12, 2025
df9ec3f
Fix phan linting.
BogdanUngureanu Mar 12, 2025
1503dff
echo the title
BogdanUngureanu Mar 12, 2025
1f2958c
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu Mar 12, 2025
6eaa7c1
Remove target and update docblock
BogdanUngureanu Mar 18, 2025
4298307
Revert "Update baseline"
BogdanUngureanu Mar 18, 2025
826d256
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu Mar 18, 2025
6bf68d4
update baseline
BogdanUngureanu Mar 18, 2025
64fde57
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu Mar 18, 2025
b8835c3
Update number of days to 30
BogdanUngureanu Mar 19, 2025
e09c5b7
Update the link title
BogdanUngureanu Mar 26, 2025
bbe7bc7
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu Mar 26, 2025
afbe47f
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu Mar 31, 2025
e25fa08
Update title that better reflects the information on the page and the…
BogdanUngureanu Mar 31, 2025
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
4 changes: 4 additions & 0 deletions projects/packages/stats-admin/changelog/update-stats-icon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Add page view count in the post and page list and updated the icon
273 changes: 273 additions & 0 deletions projects/packages/stats-admin/src/class-admin-post-list-column.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
<?php
/**
* A class that adds a stats column to wp-admin Post List.
*
* @package automattic/jetpack-stats-admin
*/

namespace Automattic\Jetpack\Stats_Admin;

use Automattic\Jetpack\Connection\Manager as Connection_Manager;
use Automattic\Jetpack\Redirect;
use Automattic\Jetpack\Stats\Options as Stats_Options;
use Automattic\Jetpack\Stats\WPCOM_Stats;
use Automattic\Jetpack\Status\Host;
use NumberFormatter;

/**
* Add a Stats column in the post and page lists.
*/
class Admin_Post_List_Column {
Copy link
Contributor Author

@BogdanUngureanu BogdanUngureanu Mar 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the code here is just moved from modules/stats.php.

We need to move it in a separate class to also use it on Simple Sites.


/**
* Create the object.
*
* @return self
*/
public static function register() {
return new self();
}

/**
* A list of NumberFormatters.
*
* @var \NumberFormatter[]
*/
private $formatter;

/**
* The constructor.
*/
public function __construct() {
// Add an icon to see stats in WordPress.com for a particular post.
add_action( 'admin_print_styles-edit.php', array( $this, 'stats_load_admin_css' ) );

add_filter( 'manage_posts_columns', array( $this, 'add_stats_post_table' ) );
add_filter( 'manage_pages_columns', array( $this, 'add_stats_post_table' ) );

add_action( 'manage_posts_custom_column', array( $this, 'add_stats_post_table_cell' ), 10, 2 );
add_action( 'manage_pages_custom_column', array( $this, 'add_stats_post_table_cell' ), 10, 2 );
}

/**
* Load CSS needed for Stats column width in WP-Admin area.
*
* @since 4.7.0
*/
public function stats_load_admin_css() {
?>
<style type="text/css">
.fixed .column-stats {
width: 5em;
}
</style>
<?php
}

/**
* Set content for cell with link to an entry's stats in Odyssey Stats.
*
* @param string $column The name of the column to display.
* @param int $post_id The current post ID.
*
* @since 4.7.0
*/
public function add_stats_post_table_cell( $column, $post_id ) {
if ( 'stats' === $column ) {
if ( 'publish' !== get_post_status( $post_id ) ) {
printf(
'<span aria-hidden="true">—</span><span class="screen-reader-text">%s</span>',
esc_html__( 'No stats', 'jetpack-stats-admin' )
);
} else {
// Link to the wp-admin stats page.
$stats_post_url = admin_url( sprintf( 'admin.php?page=stats#!/stats/post/%d/%d', $post_id, \Jetpack_Options::get_option( 'id', 0 ) ) );
// Unless the user is on a Default style WOA site, in which case link to Calypso.
if ( ( new Host() )->is_woa_site() && Stats_Options::get_option( 'enable_odyssey_stats' ) && 'wp-admin' !== get_option( 'wpcom_admin_interface' ) ) {
$stats_post_url = Redirect::get_url(
'calypso-stats-post',
array(
'path' => $post_id,
)
);
}

static $post_views = null;

/**
* Jetpack_stats_get_post_page_views_for_current_list makes a request with all post ids in the current $wp_query.
* This way, we'll make a single API request instead of making one for each post.
*
* For this reason, we'll cache the result with the static $post_views variable.
*/
if ( null === $post_views ) {
$post_views = $this->get_post_page_views_for_current_list();
}

$views = $post_views[ $post_id ] ?? null;

$current_locale = get_bloginfo( 'language' );

$formatted_views = class_exists( '\NumberFormatter' ) ? $this->get_formatter( $current_locale )->format( $views ) : $this->get_fallback_format_to_compact_version( $views );

?>
<a href="<?php echo esc_url( $stats_post_url ); ?>"
title="<?php echo esc_html__( 'Views for the last thirty days. Click for detailed stats', 'jetpack-stats-admin' ); ?>">
<span
class="dashicons dashicons-visibility"></span>&nbsp;<span><?php echo null !== $views ? esc_html( $formatted_views ) : ''; ?></span>
</a>
<?php
}
}
}

/**
* Set header for column that allows to view an entry's stats.
*
* @param array $columns An array of column names.
*
* @return mixed
*/
public function add_stats_post_table( $columns ) {
/*
* Stats can be accessed in wp-admin or in Calypso,
* depending on what version of the stats screen is enabled on your site.
*
* In both cases, the user must be allowed to access stats.
*
* If the Odyssey Stats experience isn't enabled, the user will need to go to Calypso,
* so they need to be connected to WordPress.com to be able to access that page.
*/
if (
! current_user_can( 'view_stats' )
|| (
! Stats_Options::get_option( 'enable_odyssey_stats' )
&& ! ( new Connection_Manager( 'jetpack' ) )->is_user_connected()
)
) {
return $columns;
}

// Array-Fu to add before comments.
$pos = array_search( 'comments', array_keys( $columns ), true );

// Fallback to the last position if the post type does not support comments.
if ( ! is_int( $pos ) ) {
$pos = count( $columns );
}

$chunks = array_chunk( $columns, $pos, true );
$chunks[0]['stats'] = esc_html__( 'Stats', 'jetpack-stats-admin' );

return call_user_func_array( 'array_merge', $chunks );
}

/**
* Get a list of post views for each post id from the global $wp_query.
*
* @return array
*/
public function get_post_page_views_for_current_list(): array {
global $wp_query;

if ( ! $wp_query->posts ) {
return array();
}

$post_ids = wp_list_pluck( $wp_query->posts, 'ID' );

$wpcom_stats = $this->get_stats();
$post_views = $wpcom_stats->get_total_post_views(
array(
'num' => 30,
'post_ids' => implode( ',', $post_ids ),
)
);

if ( is_wp_error( $post_views ) || empty( $post_views ) ) {
return array();
}

$views = array();

foreach ( $post_views['posts'] as $post ) {
$views[ $post['ID'] ] = $post['views'];
}

return $views;
}

/**
* Get the stats object.
*
* @return WPCOM_Stats
*/
protected function get_stats() {
return new WPCOM_Stats();
}

/**
* Get the NumberFormatter instance.
*
* @param string $locale The current locale.
*
* @return NumberFormatter
*/
protected function get_formatter( string $locale ): \NumberFormatter {
if ( isset( $this->formatter[ $locale ] ) ) {
return $this->formatter[ $locale ];
}

/**
* PHP's NumberFormatter is just a wrapper over the ICU C library. The library does support decimal compact short formatter, but PHP doesn't have a stub for it (=< PHP 8.4).
*
* @see https://unicode-org.github.io/icu-docs/apidoc/dev/icu4c/unum_8h.html UNUM_DECIMAL_COMPACT_SHORT constant.
*/
$compact_decimal_short = 14;

/**
* NumberFormatter::DECIMAL_COMPACT_SHORT only exists in PHP 8.5 and later. At this time, NumberFormatter::DECIMAL_COMPACT_SHORT only exists in PHP `main` branch.
*
* Use the constant if it's defined since it's safer.
*/
if ( defined( '\NumberFormatter::DECIMAL_COMPACT_SHORT' ) ) {
// @phan-suppress-next-line PhanUndeclaredConstantOfClass
$compact_decimal_short = NumberFormatter::DECIMAL_COMPACT_SHORT;
}

try {
$formatter = new \NumberFormatter( $locale, $compact_decimal_short );
$formatter->setAttribute( \NumberFormatter::MAX_FRACTION_DIGITS, 1 );
} catch ( \Exception $e ) {
// Fallback to decimal if for some reason it fails to work.
$formatter = new \NumberFormatter( $locale, \NumberFormatter::DECIMAL );
}

$this->formatter[ $locale ] = $formatter;

return $formatter;
}

/**
* Fallback Format a number to a compact version if the Intl extension is not available.
*
* @param int $views The given number.
*
* @return string
*/
public function get_fallback_format_to_compact_version( $views ) {
if ( $views >= 10000000 ) {
return round( $views / 1000000 ) . 'M';
} elseif ( $views >= 1000000 ) {
$views = round( $views / 1000000, 1 );
return preg_replace( '/\.0$/', '', (string) $views ) . 'M';
} elseif ( $views >= 10000 ) {
return round( $views / 1000 ) . 'K';
} elseif ( $views >= 1000 ) {
$views = round( $views / 1000, 1 );
return preg_replace( '/\.0$/', '', (string) $views ) . 'K';
}

return (string) $views;
}
}
Loading
Loading