-
Notifications
You must be signed in to change notification settings - Fork 2
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
Issue-142: Add option to pull Trending posts #143
Changes from all commits
5c73763
a461992
d537250
6def2e9
880dca9
ddb7cfb
497e156
7f80641
92a3455
bc9c92c
c62bd5c
adfd7f9
80c6060
a093e95
e2f410e
3c46a7c
ef65e5c
00def61
f6d4411
d88feb7
ec758c7
c70d5be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/** | ||
* Trending_Post_Queries class file | ||
* | ||
* @package wp-type-extensions | ||
*/ | ||
|
||
namespace Alley\WP\WP_Curate; | ||
|
||
use Alley\WP\Types\Post_Queries; | ||
use Alley\WP\Types\Post_Query; | ||
use Alley\WP\Post_Query\WP_Query_Envelope; | ||
use Alley\WP\WP_Curate\Features\Parsely_Support; | ||
use Alley\WP\Post_Query\Post_IDs_Query; | ||
|
||
/** | ||
* Pull trending posts from Parsely. | ||
*/ | ||
final class Trending_Post_Queries implements Post_Queries { | ||
/** | ||
* Set up. | ||
* | ||
* @param Post_Queries $origin Post_Queries object. | ||
* @param Parsely_Support $parsely Parsely_Support object. | ||
*/ | ||
public function __construct( | ||
private readonly Post_Queries $origin, | ||
private readonly Parsely_Support $parsely | ||
) {} | ||
|
||
/** | ||
* Query for posts using literal arguments. | ||
* | ||
* @param array<string, mixed> $args The arguments to be used in the query. | ||
* @return Post_Query | ||
*/ | ||
public function query( array $args ): Post_Query { | ||
if ( isset( $args['orderby'] ) && 'trending' === $args['orderby'] ) { | ||
$trending = $this->parsely->get_trending_posts( $args ); | ||
if ( ! empty( $trending ) ) { | ||
return new Post_IDs_Query( $trending ); | ||
} | ||
} | ||
|
||
return $this->origin->query( $args ); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
<?php | ||
/** | ||
* Parsely_Support class file | ||
* | ||
* @package wp-curate | ||
*/ | ||
|
||
namespace Alley\WP\WP_Curate\Features; | ||
|
||
use Alley\WP\Types\Feature; | ||
use Parsely\RemoteAPI\Analytics_Posts_API; | ||
use Parsely\Parsely; | ||
|
||
/** | ||
* Add support for Parsely, if the plugin is installed. | ||
*/ | ||
final class Parsely_Support implements Feature { | ||
/** | ||
* Set up. | ||
*/ | ||
public function __construct() {} | ||
|
||
/** | ||
* Boot the feature. | ||
*/ | ||
public function boot(): void { | ||
if ( ! class_exists( 'Parsely\Parsely' ) ) { | ||
return; | ||
} | ||
// Elsewhere in the plugin, we'll use $GLOBALS['parsely'], but it is not available here. | ||
$parsely = new \Parsely\Parsely(); | ||
// If we don't have the API secret, we can't use the Parsely API. | ||
if ( ! $parsely->api_secret_is_set() ) { | ||
return; | ||
} | ||
add_filter( 'wp_curate_use_parsely', '__return_true' ); | ||
add_filter( 'wp_curate_trending_posts_query', [ $this, 'add_parsely_trending_posts_query' ], 10, 2 ); | ||
} | ||
|
||
/** | ||
* Gets the trending posts from Parsely. | ||
* | ||
* @param array<number> $posts The posts, which should be an empty array. | ||
* @param array<string, mixed> $args The WP_Query args. | ||
* @return array<number> Array of post IDs. | ||
*/ | ||
public function add_parsely_trending_posts_query( array $posts, array $args ): array { | ||
$parsely = $GLOBALS['parsely']; | ||
if ( ! $parsely->api_secret_is_set() ) { | ||
return $posts; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is going to be checked again here (which seems sensible), why worry about also checking it in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dlh01 I want to conditionally run
only if the secret is set, which I'm doing in |
||
$trending_posts = $this->get_trending_posts( $args ); | ||
return $trending_posts; | ||
} | ||
|
||
/** | ||
* Gets the trending posts from Parsely. | ||
* | ||
* @param array<string, mixed> $args The WP_Query args. | ||
* @return array<int> An array of post IDs. | ||
*/ | ||
public function get_trending_posts( array $args ): array { | ||
if ( ! class_exists( '\Parsely\Parsely' ) || ! isset( $GLOBALS['parsely'] ) || ! $GLOBALS['parsely'] instanceof Parsely ) { | ||
return []; | ||
} | ||
if ( ! class_exists( '\Parsely\RemoteAPI\Analytics_Posts_API' ) ) { | ||
return []; | ||
} | ||
|
||
$parsely_options = $GLOBALS['parsely']->get_options(); | ||
/** | ||
* Filter the period start for the Parsely API. | ||
* | ||
* @param string $period_start The period start. | ||
* @param array<string, mixed> $args The WP_Query args. | ||
*/ | ||
$period_start = apply_filters( 'wp_curate_parsely_period_start', '1d', $args ); | ||
/** | ||
* Filter the period end for the Parsely API. | ||
* | ||
* @param string $period_end The period end. | ||
* @param array<string, mixed> $args The WP_Query args. | ||
*/ | ||
$period_end = apply_filters( 'wp_curate_parsely_period_end', 'now', $args ); | ||
$parsely_args = [ | ||
'limit' => $args['posts_per_page'] ?? get_option( 'posts_per_page' ), | ||
'sort' => 'views', | ||
'period_start' => $period_start, | ||
'period_end' => $period_end, | ||
]; | ||
if ( isset( $args['tax_query'] ) && is_array( $args['tax_query'] ) ) { | ||
foreach ( $args['tax_query'] as $tax_query ) { | ||
if ( isset( $tax_query['taxonomy'] ) && $parsely_options['custom_taxonomy_section'] === $tax_query['taxonomy'] ) { | ||
$parsely_args['section'] = implode( ', ', $this->get_slugs_from_term_ids( $tax_query['terms'], $tax_query['taxonomy'] ) ); | ||
mogmarsh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
if ( isset( $tax_query['taxonomy'] ) && 'post_tag' === $tax_query['taxonomy'] ) { | ||
$parsely_args['tag'] = implode( ', ', $this->get_slugs_from_term_ids( $tax_query['terms'], $tax_query['taxonomy'] ) ); | ||
} | ||
} | ||
if ( $parsely_options['cats_as_tags'] ) { | ||
$tags = explode( ', ', $parsely_args['tag'] ?? '' ); | ||
$sections = explode( ', ', $parsely_args['section'] ?? '' ); | ||
$parsely_args['tag'] = implode( ', ', array_merge( $tags, $sections ) ); | ||
} | ||
} | ||
$cache_key = 'parsely_trending_posts_' . md5( wp_json_encode( $parsely_args ) ); // @phpstan-ignore-line - wp_Json_encode not likely to return false. | ||
$ids = wp_cache_get( $cache_key ); | ||
if ( false === $ids || ! is_array( $ids ) ) { | ||
$api = new Analytics_Posts_API( $GLOBALS['parsely'] ); | ||
$posts = $api->get_posts_analytics( $parsely_args ); | ||
$ids = array_map( | ||
function ( $post ) { | ||
// Check if the metadata contains post_id, if not, use the URL to get the post ID. | ||
$metadata = json_decode( $post['metadata'] ?? '', true ); | ||
if ( is_array( $metadata ) && isset( $metadata['post_id'] ) ) { | ||
$post_id = (int) $metadata['post_id']; | ||
} elseif ( function_exists( 'wpcom_vip_url_to_postid' ) ) { | ||
$post_id = wpcom_vip_url_to_postid( $post['url'] ); | ||
} else { | ||
$post_id = url_to_postid( $post['url'] ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.url_to_postid_url_to_postid | ||
} | ||
/** | ||
* Filters the post ID derived from Parsely post object. | ||
* | ||
* @param int $post_id The post ID. | ||
* @param array $post The Parsely post object. | ||
*/ | ||
return apply_filters( 'wp_curate_parsely_post_to_post_id', $post_id, $post ); | ||
}, | ||
$posts | ||
); | ||
/** | ||
* Filters the cache duration for the trending posts from Parsely. | ||
* | ||
* @param int $cache_duration The cache duration. | ||
* @param array<string, mixed> $args The WP_Query args. | ||
*/ | ||
$cache_duration = apply_filters( 'wp_curate_parsely_trending_posts_cache_duration', 10 * MINUTE_IN_SECONDS, $args ); | ||
if ( 300 > $cache_duration ) { | ||
$cache_duration = 300; | ||
} | ||
wp_cache_set( $cache_key, $ids, '', $cache_duration ); // phpcs:ignore WordPressVIPMinimum.Performance.LowExpiryCacheTime.CacheTimeUndetermined | ||
} | ||
|
||
/** | ||
* Filters the trending posts from Parsely. | ||
* | ||
* @param array<int> $ids The list of post IDs. | ||
* @param array<string, mixed> $parsely_args The Parsely API args. | ||
* @param array<string, mixed> $args The WP_Query args. | ||
*/ | ||
$ids = apply_filters( 'wp_curate_parsely_trending_posts', $ids, $parsely_args, $args ); | ||
|
||
return $ids; | ||
} | ||
|
||
/** | ||
* Get slugs from term IDs. | ||
* | ||
* @param array<int> $ids The list of term ids. | ||
* @param string $taxonomy The taxonomy. | ||
* @return array<string> The list of term slugs. | ||
*/ | ||
private function get_slugs_from_term_ids( $ids, $taxonomy ) { | ||
$terms = array_filter( | ||
array_map( | ||
function ( $id ) use ( $taxonomy ) { | ||
$term = get_term( $id, $taxonomy ); | ||
if ( $term instanceof \WP_Term ) { | ||
return $term->slug; | ||
} | ||
}, | ||
$ids | ||
) | ||
); | ||
return $terms; | ||
} | ||
} |
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.
If this is specific to Parse.ly, then it should be named as such. If it's intended to be a generic trending posts class, then it should be structured so that it supports multiple providers, of which Parse.ly will be the first (and we could add other providers like GA4 later).
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.
@kevinfodness supporting multiple providers was my plan... I kind of figured we would sort out how structure it when we added the second provider.
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.
Ah, we're using named arguments instead of positional ones, this works for me!