defaults = array( 'title' => __( 'Top Authors', 'advance-widget-pack' ), 'author_count' => '5', 'rank_by' => 'views', 'show_avatar' => '1', 'show_count' => '1', ); $widget_ops = array( 'classname' => 'awp_top_authors awp_widget', 'description' => __( 'Widget to rank authors by total post views or comments.', 'advance-widget-pack' ), ); $control_ops = array( 'id_base' => 'awp_top_authors', 'width' => 200, 'height' => 250, ); parent::__construct( 'awp_top_authors', __( 'AWP Top Authors', 'advance-widget-pack' ), $widget_ops, $control_ops ); } public function widget( $args, $instance ) { $instance = wp_parse_args( (array) $instance, $this->defaults ); $title = apply_filters( 'widget_title', $instance['title'] ); $author_count = absint( $instance['author_count'] ); $rank_by = 'comments' === $instance['rank_by'] ? 'comments' : 'views'; $show_avatar = ! empty( $instance['show_avatar'] ); $show_count = ! empty( $instance['show_count'] ); $rows = self::get_rows( $rank_by, $author_count ); if ( ! $rows ) { return; } echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- sidebar-provided wrapper markup, trusted per WP core convention. if ( ! empty( $title ) ) { echo $args['before_title'] . esc_html( $title ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- title text is esc_html()'d; before/after_title are sidebar-provided wrapper markup. } self::render_rows( $rows, $rank_by, $show_avatar, $show_count ); echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- sidebar-provided wrapper markup, trusted per WP core convention. } /** * Fetches ranked author rows (author_id, total), using the shared * transient cache. Used by both the widget and the Top Authors block. * * @param string $rank_by 'views' or 'comments'. * @param int $author_count Max number of authors to fetch. * @return array[] Array of objects with author_id and total properties. */ public static function get_rows( $rank_by, $author_count ) { $rank_by = 'comments' === $rank_by ? 'comments' : 'views'; $author_count = absint( $author_count ); // View counts change on nearly every pageview, so (unlike post lists) // this cache is intentionally NOT invalidated via awp_cache_generation() // when a view is recorded — that would defeat caching almost entirely // under real traffic. It refreshes via the 15-minute TTL below, and // still gets invalidated immediately by the generation bump on new/edited posts. $cache_key = 'awp_top_authors_' . $rank_by . '_' . awp_cache_generation() . '_' . $author_count; $rows = get_transient( $cache_key ); if ( false === $rows ) { global $wpdb; if ( 'comments' === $rank_by ) { // Total approved comments received across each author's published posts. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- no core API for this aggregate; result is cached via the transient wrapping this whole block. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT p.post_author AS author_id, COUNT(c.comment_ID) AS total FROM {$wpdb->posts} p INNER JOIN {$wpdb->comments} c ON c.comment_post_ID = p.ID AND c.comment_approved = '1' WHERE p.post_type = 'post' AND p.post_status = 'publish' GROUP BY p.post_author ORDER BY total DESC LIMIT %d", $author_count ) ); } else { // Total views (wp_views postmeta) summed across each author's published posts. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- no core API for this aggregate; result is cached via the transient wrapping this whole block. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT p.post_author AS author_id, SUM(CAST(pm.meta_value AS UNSIGNED)) AS total FROM {$wpdb->posts} p INNER JOIN {$wpdb->postmeta} pm ON pm.post_id = p.ID AND pm.meta_key = 'wp_views' WHERE p.post_type = 'post' AND p.post_status = 'publish' GROUP BY p.post_author ORDER BY total DESC LIMIT %d", $author_count ) ); } set_transient( $cache_key, $rows, 15 * MINUTE_IN_SECONDS ); } return $rows; } /** * Renders the ranked author