-
Notifications
You must be signed in to change notification settings - Fork 818
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
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 6183ec8
remove the quick fix from dashboard_switcher_scripts
BogdanUngureanu 0ef329e
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu a2d0586
Stats: Update UI and move the code in the package
BogdanUngureanu f83072f
Stats: Update UI and move the code in the package
BogdanUngureanu a6be580
Fixed fatals
BogdanUngureanu 493ce1a
Fix linting
BogdanUngureanu 799aef7
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu dece4dc
Add client support for simple sites
BogdanUngureanu 00c5918
Fix linting
BogdanUngureanu 3e1bd48
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu e713032
Try to fix the linting
BogdanUngureanu f1c8af8
Add wpcom to the stubs
BogdanUngureanu e980d13
Refactor WPCOM_Stats implementation and add unit testing
BogdanUngureanu 834040f
Refactor some code, add some changes in the phan directory and add mo…
BogdanUngureanu 6e52101
Handle a scenario and fix failing tests.
BogdanUngureanu 09a8bb0
use onlyMethods
BogdanUngureanu 550139d
Disable phan rule since we already check if it exists.
BogdanUngureanu e59a54a
Fix linting
BogdanUngureanu 534844d
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu 89301d0
Create a single instance of the formatter
BogdanUngureanu f554a8a
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu 2a84ebd
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu d724afe
Update baseline
BogdanUngureanu 248c47e
Add fallback to NumberFormatter
BogdanUngureanu df9ec3f
Fix phan linting.
BogdanUngureanu 1503dff
echo the title
BogdanUngureanu 1f2958c
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu 6eaa7c1
Remove target and update docblock
BogdanUngureanu 4298307
Revert "Update baseline"
BogdanUngureanu 826d256
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu 6bf68d4
update baseline
BogdanUngureanu 64fde57
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu b8835c3
Update number of days to 30
BogdanUngureanu e09c5b7
Update the link title
BogdanUngureanu bbe7bc7
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu afbe47f
Merge branch 'trunk' into update/stats-icon
BogdanUngureanu e25fa08
Update title that better reflects the information on the page and the…
BogdanUngureanu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
273
projects/packages/stats-admin/src/class-admin-post-list-column.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
/** | ||
* 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> <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 ) { | ||
BogdanUngureanu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.