Skip to content

Commit

Permalink
New filter: wzpa_custom_template
Browse files Browse the repository at this point in the history
Fixes #13
  • Loading branch information
ajaydsouza committed Aug 8, 2023
1 parent 8bec874 commit 5528845
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
30 changes: 30 additions & 0 deletions includes/frontend/class-display.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace WebberZone\Popular_Authors\Frontend;

use WebberZone\Popular_Authors\Frontend\Styles_Handler;

if ( ! defined( 'WPINC' ) ) {
die;
}
Expand Down Expand Up @@ -80,13 +82,41 @@ public static function list_popular_authors( $args = array() ) {

$authors = self::get_popular_author_ids( $args );

// Set the post counts for each author.
$post_counts = array();
$post_counts_query = $wpdb->get_results( "SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE " . get_private_posts_cap_sql( 'post' ) . ' GROUP BY post_author' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching

foreach ( (array) $post_counts_query as $row ) {
$post_counts[ $row->post_author ] = $row->count;
}

/**
* Filter to create a custom HTML output for a set of Popular Authors.
*
* @since 1.2.0
*
* @param mixed $custom_template Custom template. Default value null.
* @param object $authors Array of popular author IDs.
* @param array $args Array of settings.
* @param array $post_counts Array of post counts for each author.
* @return string Custom HTML formatted list of popular authors.
*/
$custom_template = apply_filters( 'wzpa_custom_template', null, $authors, $args, $post_counts );
if ( ! empty( $custom_template ) ) {
if ( $args['cache'] ) {
/**
* Filter already documented in /top-10/includes/public/display-posts.php
*/
$cache_time = apply_filters( 'tptn_cache_time', \tptn_get_option( 'cache_time' ), $args );

$output .= "<br /><!-- Cached output. Cached time is {$cache_time} seconds -->";

set_transient( $cache_name, $output, $cache_time );
}

return $custom_template;
}

$style_array = Styles_Handler::get_style( $args['styles'] );

$post_classes = array(
Expand Down
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ parameters:

-
message: "#^Function tptn_get_option not found\\.$#"
count: 1
count: 2
path: includes/frontend/class-display.php

-
Expand All @@ -37,7 +37,7 @@ parameters:

-
message: "#^Variable \\$cache_name might not be defined\\.$#"
count: 1
count: 2
path: includes/frontend/class-display.php

-
Expand Down
2 changes: 2 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ Use `[wzpa_popular_authors]` to display the popular authors. Check [this knowled
Popular Authors has been rewritten to use classes and autoloading.

* Features:
* New style options: Choose between a Card Layout or Left Thumbs
* New filter: `wzpa_custom_template` which can be used to override the Popular Authors HTML output
* New filters: `wzpa_query_fields`, `wzpa_query_join`, `wzpa_query_where`, `wzpa_query_groupby`, `wzpa_query_orderby` and `wzpa_query_limits` to modify the Popular Authors mySQL query

* Bug fixes:
Expand Down

0 comments on commit 5528845

Please sign in to comment.