Skip to content

Commit cf9913d

Browse files
authored
Cache expensive operations in Post transformer (#2604)
1 parent 9946b25 commit cf9913d

File tree

3 files changed

+114
-24
lines changed

3 files changed

+114
-24
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: changed
3+
4+
Cache expensive operations in Post transformer to improve performance.

includes/transformer/class-post.php

Lines changed: 105 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,48 @@ class Post extends Base {
4141
*/
4242
private $actor_object = null;
4343

44+
/**
45+
* The content.
46+
*
47+
* @var string|false False indicates not yet computed.
48+
*/
49+
private $content = false;
50+
51+
/**
52+
* The summary.
53+
*
54+
* @var string|null|false False indicates not yet computed.
55+
*/
56+
private $summary = false;
57+
58+
/**
59+
* The tags.
60+
*
61+
* @var array|false False indicates not yet computed.
62+
*/
63+
private $tags = false;
64+
65+
/**
66+
* The attachment.
67+
*
68+
* @var array|false False indicates not yet computed.
69+
*/
70+
private $attachment = false;
71+
72+
/**
73+
* The mentions.
74+
*
75+
* @var array|false False indicates not yet computed.
76+
*/
77+
private $mentions = false;
78+
79+
/**
80+
* The in_reply_to.
81+
*
82+
* @var string|array|null|false False indicates not yet computed.
83+
*/
84+
private $in_reply_to = false;
85+
4486
/**
4587
* Transforms the WP_Post object to an ActivityPub Object
4688
*
@@ -290,12 +332,18 @@ protected function get_icon() {
290332
* @return array The Attachments.
291333
*/
292334
protected function get_attachment() {
335+
if ( false !== $this->attachment ) {
336+
return $this->attachment;
337+
}
338+
293339
/*
294340
* Remove attachments from the Fediverse if a post was federated and then set back to draft.
295341
* Except in preview mode, where we want to show attachments.
296342
*/
297343
if ( ! $this->is_preview() && 'draft' === \get_post_status( $this->item ) ) {
298-
return array();
344+
$this->attachment = array();
345+
346+
return $this->attachment;
299347
}
300348

301349
$max_media = \get_post_meta( $this->item->ID, 'activitypub_max_image_attachments', true );
@@ -316,7 +364,9 @@ protected function get_attachment() {
316364
$max_media = (int) \apply_filters( 'activitypub_max_image_attachments', $max_media );
317365

318366
if ( 0 === $max_media ) {
319-
return array();
367+
$this->attachment = array();
368+
369+
return $this->attachment;
320370
}
321371

322372
$media = array(
@@ -363,7 +413,9 @@ protected function get_attachment() {
363413
*
364414
* @return array The filtered attachments.
365415
*/
366-
return \apply_filters( 'activitypub_attachments', $attachments, $this->item );
416+
$this->attachment = \apply_filters( 'activitypub_attachments', $attachments, $this->item );
417+
418+
return $this->attachment;
367419
}
368420

369421
/**
@@ -381,15 +433,8 @@ protected function get_type() {
381433
return \ucfirst( $post_format_setting );
382434
}
383435

384-
$has_title = \post_type_supports( $this->item->post_type, 'title' );
385-
$content = \wp_strip_all_tags( $this->item->post_content );
386-
387436
// Check if the post has a title.
388-
if (
389-
! $has_title ||
390-
! $this->item->post_title ||
391-
\strlen( $content ) <= ACTIVITYPUB_NOTE_LENGTH
392-
) {
437+
if ( ! \post_type_supports( $this->item->post_type, 'title' ) || ! $this->item->post_title ) {
393438
return 'Note';
394439
}
395440

@@ -430,6 +475,10 @@ public function get_audience() {
430475
* @return array The list of Tags.
431476
*/
432477
protected function get_tag() {
478+
if ( false !== $this->tags ) {
479+
return $this->tags;
480+
}
481+
433482
$tags = parent::get_tag();
434483

435484
$post_tags = \get_the_tags( $this->item->ID );
@@ -448,7 +497,9 @@ protected function get_tag() {
448497
}
449498
}
450499

451-
return \array_unique( $tags, SORT_REGULAR );
500+
$this->tags = \array_unique( $tags, SORT_REGULAR );
501+
502+
return $this->tags;
452503
}
453504

454505
/**
@@ -464,12 +515,20 @@ protected function get_summary() {
464515
return null;
465516
}
466517

518+
if ( false !== $this->summary ) {
519+
return $this->summary;
520+
}
521+
467522
// Remove Teaser from drafts.
468523
if ( ! $this->is_preview() && 'draft' === \get_post_status( $this->item ) ) {
469-
return \__( '(This post is being modified)', 'activitypub' );
524+
$this->summary = \__( '(This post is being modified)', 'activitypub' );
525+
526+
return $this->summary;
470527
}
471528

472-
return generate_post_summary( $this->item );
529+
$this->summary = generate_post_summary( $this->item );
530+
531+
return $this->summary;
473532
}
474533

475534
/**
@@ -506,9 +565,15 @@ protected function get_name() {
506565
* @return string The content.
507566
*/
508567
protected function get_content() {
568+
if ( false !== $this->content ) {
569+
return $this->content;
570+
}
571+
509572
// Remove Content from drafts.
510573
if ( ! $this->is_preview() && 'draft' === \get_post_status( $this->item ) ) {
511-
return \__( '(This post is being modified)', 'activitypub' );
574+
$this->content = \__( '(This post is being modified)', 'activitypub' );
575+
576+
return $this->content;
512577
}
513578

514579
global $post;
@@ -551,7 +616,9 @@ protected function get_content() {
551616
* @param string $content The transformed post content.
552617
* @param \WP_Post $post The post object being transformed.
553618
*/
554-
return \apply_filters( 'activitypub_the_content', $content, $post );
619+
$this->content = \apply_filters( 'activitypub_the_content', $content, $post );
620+
621+
return $this->content;
555622
}
556623

557624
/**
@@ -578,8 +645,13 @@ public function generate_reply_link( $block_content, $block ) {
578645
* @return string|array|null The in-reply-to URL of the post.
579646
*/
580647
protected function get_in_reply_to() {
648+
if ( false !== $this->in_reply_to ) {
649+
return $this->in_reply_to;
650+
}
651+
581652
if ( ! site_supports_blocks() ) {
582-
return null;
653+
$this->in_reply_to = null;
654+
return $this->in_reply_to;
583655
}
584656

585657
$reply_urls = array();
@@ -596,14 +668,20 @@ protected function get_in_reply_to() {
596668
}
597669

598670
if ( empty( $reply_urls ) ) {
599-
return null;
671+
$this->in_reply_to = null;
672+
673+
return $this->in_reply_to;
600674
}
601675

602676
if ( 1 === count( $reply_urls ) ) {
603-
return \current( $reply_urls );
677+
$this->in_reply_to = \current( $reply_urls );
678+
679+
return $this->in_reply_to;
604680
}
605681

606-
return \array_values( \array_unique( $reply_urls ) );
682+
$this->in_reply_to = \array_values( \array_unique( $reply_urls ) );
683+
684+
return $this->in_reply_to;
607685
}
608686

609687
/**
@@ -639,6 +717,10 @@ protected function get_updated() {
639717
* @return array The list of @-Mentions.
640718
*/
641719
protected function get_mentions() {
720+
if ( false !== $this->mentions ) {
721+
return $this->mentions;
722+
}
723+
642724
/**
643725
* Filter the mentions in the post content.
644726
*
@@ -648,12 +730,14 @@ protected function get_mentions() {
648730
*
649731
* @return array The filtered mentions.
650732
*/
651-
return apply_filters(
733+
$this->mentions = apply_filters(
652734
'activitypub_extract_mentions',
653735
array(),
654736
$this->item->post_content . ' ' . $this->item->post_excerpt,
655737
$this->item
656738
);
739+
740+
return $this->mentions;
657741
}
658742

659743
/**

tests/phpunit/tests/includes/transformer/class-test-post.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ public function get_type_provider() {
112112
'post_content' => 'Short content',
113113
),
114114
null,
115-
'Note',
116-
'Should return Note for short content',
115+
'Article',
116+
'Should return Article for short content with title',
117117
),
118118
'no_title' => array(
119119
array(
@@ -426,7 +426,9 @@ public function test_get_attachments_with_zero_max_media_attachments() {
426426

427427
\delete_post_meta( $post_id, 'activitypub_max_image_attachments' );
428428

429-
$result = $method->invoke( $transformer );
429+
// Create a new transformer instance to avoid cached attachment result.
430+
$transformer = new Post( $post );
431+
$result = $method->invoke( $transformer );
430432
$this->assertTrue( (bool) \did_filter( 'activitypub_attachment_ids' ) );
431433
}
432434

0 commit comments

Comments
 (0)