@@ -183,3 +183,40 @@ function activitypub_get_follower_inboxes( $user_id, $followers ) {
183183
184184 return array_unique ( $ inboxes );
185185}
186+
187+ /**
188+ * Get the excerpt for a post for use outside of the loop.
189+ *
190+ * @param int|WP_Post $post ID or WP_Post object.
191+ * @param int Optional excerpt length.
192+ *
193+ * @return string The excerpt.
194+ */
195+ function activitypub_get_the_excerpt ( $ post , $ excerpt_length = 55 ) {
196+
197+ $ excerpt = get_post_field ( 'post_excerpt ' , $ post );
198+
199+ if ( '' === $ excerpt ) {
200+
201+ $ content = get_post_field ( 'post_content ' , $ post );
202+
203+ // An empty string will make wp_trim_excerpt do stuff we do not want.
204+ if ( '' !== $ content ) {
205+
206+ $ excerpt = strip_shortcodes ( $ content );
207+
208+ /** This filter is documented in wp-includes/post-template.php */
209+ $ excerpt = apply_filters ( 'the_content ' , $ excerpt );
210+ $ excerpt = str_replace ( ']]> ' , ']]> ' , $ excerpt );
211+
212+ $ excerpt_length = apply_filters ( 'excerpt_length ' , $ excerpt_length );
213+
214+ /** This filter is documented in wp-includes/formatting.php */
215+ $ excerpt_more = apply_filters ( 'excerpt_more ' , ' […] ' );
216+
217+ $ excerpt = wp_trim_words ( $ excerpt , $ excerpt_length , $ excerpt_more );
218+ }
219+ }
220+
221+ return apply_filters ( 'the_excerpt ' , $ excerpt );
222+ }
0 commit comments