mirror of
https://github.com/Automattic/wordpress-activitypub
synced 2024-10-18 16:23:34 +00:00
Remote Reply: limit enqueue to when needed (#725)
This commit is contained in:
parent
c67ef1b658
commit
848055c6a5
1 changed files with 49 additions and 0 deletions
|
@ -387,10 +387,59 @@ class Comment {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a post has remote comments
|
||||
*
|
||||
* @param int $post_id The post ID.
|
||||
*
|
||||
* @return bool True if the post has remote comments, false otherwise.
|
||||
*/
|
||||
private static function post_has_remote_comments( $post_id ) {
|
||||
$comments = \get_comments(
|
||||
array(
|
||||
'post_id' => $post_id,
|
||||
'meta_query' => array(
|
||||
'relation' => 'AND',
|
||||
array(
|
||||
'key' => 'protocol',
|
||||
'value' => 'activitypub',
|
||||
'compare' => '=',
|
||||
),
|
||||
array(
|
||||
'key' => 'source_id',
|
||||
'compare' => 'EXISTS',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
return ! empty( $comments );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue scripts for remote comments
|
||||
*/
|
||||
public static function enqueue_scripts() {
|
||||
if ( ! \is_singular() || \is_user_logged_in() ) {
|
||||
// only on single pages, only for logged out users
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! \post_type_supports( \get_post_type(), 'activitypub' ) ) {
|
||||
// post type does not support ActivityPub
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! \comments_open() || ! \get_comments_number() ) {
|
||||
// no comments, no need to load the script
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! self::post_has_remote_comments( \get_the_ID() ) ) {
|
||||
// no remote comments, no need to load the script
|
||||
return;
|
||||
}
|
||||
|
||||
$handle = 'activitypub-remote-reply';
|
||||
$data = array(
|
||||
'namespace' => ACTIVITYPUB_REST_NAMESPACE,
|
||||
|
|
Loading…
Reference in a new issue