Source: admin/dashboard-widgets.php
<?php if ( ! defined( 'ABSPATH' ) ) {
die( 'Cheatin’ uh?' );
}
/**
* Dashboard widget: the most shared posts (since 2.6.0).
* Idea and first version by Nicolas Guitton.
*/
if ( ! function_exists( 'jsps_get_most_shared_posts' ) ) {
/**
* The most shared posts, from the share totals (`_jsps_total_shares` post meta, see inc/counters.php).
*
* Two queries (the top posts, the sum of all the shares) of the published public posts,
* cached in a transient.
*
* @return (array) `posts`: array of `post_id => total` (most shared first), `total`: all the counted shares.
*
* @since 2.6.0
* @author Nicolas Guitton
* @author Geoffrey Crofte
*/
function jsps_get_most_shared_posts() {
$cached = get_transient( 'jsps-most-shared-posts' );
if ( is_array( $cached ) && isset( $cached['posts'], $cached['total'] ) ) {
return $cached;
}
global $wpdb;
/**
* Number of posts listed in the "Most shared posts" dashboard widget.
*
* @hook jsps_dashboard_widget_max_posts
*
* @since 2.6.0 First version
* @param {int} $max=10 Number of posts.
* @return {int} Number of posts.
*/
$max = max( 1, (int) apply_filters( 'jsps_dashboard_widget_max_posts', 10 ) );
$post_types = array_values( get_post_types( array( 'public' => true ) ) );
$in = implode( ', ', array_fill( 0, count( $post_types ), '%s' ) );
$from = "FROM {$wpdb->postmeta} pm INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id WHERE pm.meta_key = %s AND p.post_status = 'publish' AND p.post_password = '' AND p.post_type IN ( $in ) AND CAST( pm.meta_value AS UNSIGNED ) > 0";
$args = array_merge( array( JSPS_TOTAL_SHARES_META ), $post_types );
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $from only holds placeholders and table names.
$rows = $wpdb->get_results( $wpdb->prepare( "SELECT pm.post_id, CAST( pm.meta_value AS UNSIGNED ) AS total $from ORDER BY total DESC, p.post_date DESC LIMIT %d", array_merge( $args, array( $max ) ) ) );
$total = (int) $wpdb->get_var( $wpdb->prepare( "SELECT SUM( CAST( pm.meta_value AS UNSIGNED ) ) $from", $args ) );
// phpcs:enable
$posts = array();
foreach ( (array) $rows as $row ) {
$posts[ (int) $row->post_id ] = (int) $row->total;
}
$result = array(
'posts' => $posts,
'total' => $total,
);
/**
* How long the "Most shared posts" dashboard widget data is cached, in seconds.
*
* @hook jsps_dashboard_widget_cache_duration
*
* @since 2.6.0 First version
* @param {int} $duration=900 Duration in seconds (15 minutes).
* @return {int} Duration in seconds.
*/
set_transient( 'jsps-most-shared-posts', $result, max( 60, (int) apply_filters( 'jsps_dashboard_widget_cache_duration', 15 * MINUTE_IN_SECONDS ) ) );
return $result;
}
}
if ( ! function_exists( 'jsps_add_dashboard_widget' ) ) {
/**
* Adds the "Most shared posts" widget to the dashboard.
*
* @return void
*
* @since 2.6.0
* @author Nicolas Guitton
* @author Geoffrey Crofte
*/
function jsps_add_dashboard_widget() {
/**
* Capability needed to see the "Most shared posts" dashboard widget. Return '' to remove the widget.
*
* @hook jsps_dashboard_widget_capability
*
* @since 2.6.0 First version
* @param {string} $capability=edit_posts The capability.
* @return {string} The capability, or '' to remove the widget.
*/
$capability = (string) apply_filters( 'jsps_dashboard_widget_capability', 'edit_posts' );
if ( '' === $capability || ! current_user_can( $capability ) ) {
return;
}
wp_add_dashboard_widget(
'jsps_most_shared_posts',
esc_html__( 'Nobs • Most shared posts', 'juiz-social-post-sharer' ),
'jsps_dashboard_widget_output'
);
}
add_action( 'wp_dashboard_setup', 'jsps_add_dashboard_widget' );
}
if ( ! function_exists( 'jsps_dashboard_widget_output' ) ) {
/**
* Content of the "Most shared posts" dashboard widget.
*
* @return void
*
* @since 2.6.0
* @author Nicolas Guitton
* @author Geoffrey Crofte
*/
function jsps_dashboard_widget_output() {
$data = jsps_get_most_shared_posts();
if ( empty( $data['posts'] ) ) {
$options = jsps_get_option();
$counters = is_array( $options ) && ! empty( $options['juiz_sps_counter'] );
echo '<p>';
if ( $counters ) {
esc_html_e( 'No share counted yet. The most shared posts will be listed here.', 'juiz-social-post-sharer' );
} else {
printf(
/* translators: %1$s and %2$s: opening and closing tags of the link to the settings page. */
esc_html__( 'Shares are counted when the counters are activated. You can activate them in the %1$sNobs settings%2$s.', 'juiz-social-post-sharer' ),
'<a href="' . esc_url( jsps_get_settings_url() ) . '">',
'</a>'
);
}
echo '</p>';
return;
}
// One query for the listed posts, instead of one per get_permalink() / get_the_title().
_prime_post_caches( array_keys( $data['posts'] ), false, false );
echo '<p class="jsps-most-shared-total">' . esc_html(
sprintf(
/* translators: %s: number of shares. */
_n( '%s share counted on your website.', '%s shares counted on your website.', $data['total'], 'juiz-social-post-sharer' ),
number_format_i18n( $data['total'] )
)
) . '</p>';
echo '<ol class="jsps-most-shared-list">';
foreach ( $data['posts'] as $post_id => $count ) {
$title = get_the_title( $post_id );
$title = '' !== trim( wp_strip_all_tags( $title ) ) ? $title : __( '(no title)', 'juiz-social-post-sharer' );
echo '<li><a href="' . esc_url( get_permalink( $post_id ) ) . '">' . esc_html( wp_strip_all_tags( $title ) ) . '</a> <span class="jsps-most-shared-count">— ' . esc_html(
sprintf(
/* translators: %s: number of shares. */
_n( '%s share', '%s shares', $count, 'juiz-social-post-sharer' ),
number_format_i18n( $count )
)
) . '</span></li>';
}
echo '</ol>';
}
}
if ( ! function_exists( 'jsps_dashboard_widget_styles' ) ) {
/**
* A few styles for the dashboard widget (dashboard page only).
*
* @param (string) $hook_suffix The current admin page.
* @return void
*
* @since 2.6.0
*/
function jsps_dashboard_widget_styles( $hook_suffix ) {
if ( 'index.php' !== $hook_suffix ) {
return;
}
wp_add_inline_style(
'dashboard',
'#jsps_most_shared_posts .jsps-most-shared-list{margin:0 0 0 1.5em}'
. '#jsps_most_shared_posts .jsps-most-shared-list li{margin-bottom:.5em}'
. '#jsps_most_shared_posts .jsps-most-shared-count{flex-shrink:0;color:#50575e}'
);
}
add_action( 'admin_enqueue_scripts', 'jsps_dashboard_widget_styles' );
}
Plugin and documentation by Geoffrey Crofte - All Right Reserved