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

Stats: add link to view stats for a certain post in WordPress.com #6370

Merged
merged 1 commit into from
Feb 17, 2017
Merged
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
73 changes: 73 additions & 0 deletions modules/stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ function stats_load() {


add_filter( 'pre_option_db_version', 'stats_ignore_db_version' );

// Add an icon to see stats in WordPress.com for a particular post
add_action( 'admin_print_styles-edit.php', 'jetpack_stats_load_admin_css' );
add_filter( 'manage_posts_columns', 'jetpack_stats_post_table' );
add_filter( 'manage_pages_columns', 'jetpack_stats_post_table' );
add_action( 'manage_posts_custom_column', 'jetpack_stats_post_table_cell', 10, 2 );
add_action( 'manage_pages_custom_column', 'jetpack_stats_post_table_cell', 10, 2 );
}

/**
Expand Down Expand Up @@ -1343,3 +1350,69 @@ function stats_get_from_restapi( $args = array(), $resource = '' ) {

return $data;
}

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

/**
* Set header for column that allows to go to WordPress.com to see an entry's stats.
*
* @param array $columns An array of column names.
*
* @since 4.7.0
*
* @return mixed
*/
function jetpack_stats_post_table( $columns ) { // Adds a stats link on the edit posts page
if ( ! current_user_can( 'view_stats' ) || ! Jetpack::is_user_connected() ) {
return $columns;
}
// Array-Fu to add before comments
$pos = array_search( 'comments', array_keys( $columns ) );
if ( ! is_int( $pos ) ) {
return $columns;
}
$chunks = array_chunk( $columns, $pos, true );
$chunks[0]['stats'] = esc_html__( 'Stats', 'jetpack' );

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

/**
* Set content for cell with link to an entry's stats in WordPress.com.
*
* @param string $column The name of the column to display.
* @param int $post_id The current post ID.
*
* @since 4.7.0
*
* @return mixed
*/
function jetpack_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' )
);
} else {
printf(
'<a href="%s" title="%s" class="dashicons dashicons-chart-bar" target="_blank"></a>',
esc_url( "https://wordpress.com/stats/post/$post_id/" . Jetpack::build_raw_urls( get_home_url() ) ),
esc_html__( 'View stats for this post in WordPress.com', 'jetpack' )
);
}
}
}