PluginProbe
ActivityPub / 3.2.5
ActivityPub v3.2.5
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
← All changes | includes/functions.php +990 -230 9.3.03.2.5 View file →
@@ -1,99 +1,524 @@
1 1 <?php
2 +namespace Activitypub;
3 +
4 +use WP_Query;
5 +use WP_Error;
6 +use Activitypub\Http;
7 +use Activitypub\Comment;
8 +use Activitypub\Webfinger;
9 +use Activitypub\Activity\Activity;
10 +use Activitypub\Collection\Followers;
11 +use Activitypub\Collection\Users;
12 +use Activitypub\Collection\Extra_Fields;
13 +
2 14 /**
3 - * Functions file.
15 + * Returns the ActivityPub default JSON-context
4 16 *
5 - * General utility functions for the ActivityPub plugin.
17 + * @return array the activitypub context
18 + */
19 +function get_context() {
20 + $context = Activity::JSON_LD_CONTEXT;
21 +
22 + return \apply_filters( 'activitypub_json_context', $context );
23 +}
24 +
25 +function safe_remote_post( $url, $body, $user_id ) {
26 + return Http::post( $url, $body, $user_id );
27 +}
28 +
29 +function safe_remote_get( $url ) {
30 + return Http::get( $url );
31 +}
32 +
33 +/**
34 + * Returns a users WebFinger "resource"
6 35 *
7 - * @package Activitypub
36 + * @param int $user_id The User-ID.
37 + *
38 + * @return string The User-Resource.
8 39 */
40 +function get_webfinger_resource( $user_id ) {
41 + return Webfinger::get_user_resource( $user_id );
42 +}
9 43
10 -namespace Activitypub;
44 +/**
45 + * Requests the Meta-Data from the Actors profile
46 + *
47 + * @param string $actor The Actor URL.
48 + * @param bool $cached If the result should be cached.
49 + *
50 + * @return array|WP_Error The Actor profile as array or WP_Error on failure.
51 + */
52 +function get_remote_metadata_by_actor( $actor, $cached = true ) {
53 + $pre = apply_filters( 'pre_get_remote_metadata_by_actor', false, $actor );
54 + if ( $pre ) {
55 + return $pre;
56 + }
11 57
58 + if ( is_array( $actor ) ) {
59 + if ( array_key_exists( 'id', $actor ) ) {
60 + $actor = $actor['id'];
61 + } elseif ( array_key_exists( 'url', $actor ) ) {
62 + $actor = $actor['url'];
63 + } else {
64 + return new WP_Error(
65 + 'activitypub_no_valid_actor_identifier',
66 + \__( 'The "actor" identifier is not valid', 'activitypub' ),
67 + array( 'status' => 404, 'actor' => $actor )
68 + );
69 + }
70 + }
71 +
72 + if ( preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $actor ) ) {
73 + $actor = Webfinger::resolve( $actor );
74 + }
75 +
76 + if ( ! $actor ) {
77 + return new WP_Error(
78 + 'activitypub_no_valid_actor_identifier',
79 + \__( 'The "actor" identifier is not valid', 'activitypub' ),
80 + array( 'status' => 404, 'actor' => $actor )
81 + );
82 + }
83 +
84 + if ( is_wp_error( $actor ) ) {
85 + return $actor;
86 + }
87 +
88 + $transient_key = 'activitypub_' . $actor;
89 +
90 + // only check the cache if needed.
91 + if ( $cached ) {
92 + $metadata = \get_transient( $transient_key );
93 +
94 + if ( $metadata ) {
95 + return $metadata;
96 + }
97 + }
98 +
99 + if ( ! \wp_http_validate_url( $actor ) ) {
100 + $metadata = new WP_Error(
101 + 'activitypub_no_valid_actor_url',
102 + \__( 'The "actor" is no valid URL', 'activitypub' ),
103 + array( 'status' => 400, 'actor' => $actor )
104 + );
105 + return $metadata;
106 + }
107 +
108 + $response = Http::get( $actor );
109 +
110 + if ( \is_wp_error( $response ) ) {
111 + return $response;
112 + }
113 +
114 + $metadata = \wp_remote_retrieve_body( $response );
115 + $metadata = \json_decode( $metadata, true );
116 +
117 + if ( ! $metadata ) {
118 + $metadata = new WP_Error(
119 + 'activitypub_invalid_json',
120 + \__( 'No valid JSON data', 'activitypub' ),
121 + array( 'status' => 400, 'actor' => $actor )
122 + );
123 + return $metadata;
124 + }
125 +
126 + \set_transient( $transient_key, $metadata, WEEK_IN_SECONDS );
127 +
128 + return $metadata;
129 +}
130 +
12 131 /**
13 - * Get the ActivityPub ID for a WordPress object.
132 + * Returns the followers of a given user.
14 133 *
15 - * Returns the canonical ActivityPub URI for a WP_Post or WP_Comment.
134 + * @param int $user_id The User-ID.
16 135 *
17 - * @param \WP_Post|\WP_Comment $wp_object The WordPress post or comment.
136 + * @return array The followers.
137 + */
138 +function get_followers( $user_id ) {
139 + return Followers::get_followers( $user_id );
140 +}
141 +
142 +/**
143 + * Count the number of followers for a given user.
18 144 *
19 - * @return string|null The ActivityPub ID (a URL), or null if unsupported type.
145 + * @param int $user_id The User-ID.
146 + *
147 + * @return int The number of followers.
20 148 */
21 -function get_object_id( $wp_object ) {
22 - if ( $wp_object instanceof \WP_Post ) {
23 - return get_post_id( $wp_object->ID );
149 +function count_followers( $user_id ) {
150 + return Followers::count_followers( $user_id );
151 +}
152 +
153 +/**
154 + * Examine a url and try to determine the author ID it represents.
155 + *
156 + * Checks are supposedly from the hosted site blog.
157 + *
158 + * @param string $url Permalink to check.
159 + *
160 + * @return int User ID, or 0 on failure.
161 + */
162 +function url_to_authorid( $url ) {
163 + global $wp_rewrite;
164 +
165 + // check if url hase the same host
166 + if ( \wp_parse_url( \home_url(), \PHP_URL_HOST ) !== \wp_parse_url( $url, \PHP_URL_HOST ) ) {
167 + return 0;
24 168 }
25 169
26 - if ( $wp_object instanceof \WP_Comment ) {
27 - return get_comment_id( $wp_object );
170 + // first, check to see if there is a 'author=N' to match against
171 + if ( \preg_match( '/[?&]author=(\d+)/i', $url, $values ) ) {
172 + $id = \absint( $values[1] );
173 + if ( $id ) {
174 + return $id;
175 + }
28 176 }
29 177
30 - return null;
178 + // check to see if we are using rewrite rules
179 + $rewrite = $wp_rewrite->wp_rewrite_rules();
180 +
181 + // not using rewrite rules, and 'author=N' method failed, so we're out of options
182 + if ( empty( $rewrite ) ) {
183 + return 0;
184 + }
185 +
186 + // generate rewrite rule for the author url
187 + $author_rewrite = $wp_rewrite->get_author_permastruct();
188 + $author_regexp = \str_replace( '%author%', '', $author_rewrite );
189 +
190 + // match the rewrite rule with the passed url
191 + if ( \preg_match( '/https?:\/\/(.+)' . \preg_quote( $author_regexp, '/' ) . '([^\/]+)/i', $url, $match ) ) {
192 + $user = \get_user_by( 'slug', $match[2] );
193 + if ( $user ) {
194 + return $user->ID;
195 + }
196 + }
197 +
198 + return 0;
31 199 }
32 200
33 201 /**
202 + * Verify if url is a wp_ap_comment,
203 + * Or if it is a previously received remote comment
204 + *
205 + * @return int comment_id
206 + */
207 +function is_comment() {
208 + $comment_id = get_query_var( 'c', null );
209 +
210 + if ( ! is_null( $comment_id ) ) {
211 + $comment = \get_comment( $comment_id );
212 +
213 + if ( $comment ) {
214 + return $comment_id;
215 + }
216 + }
217 +
218 + return false;
219 +}
220 +
221 +/**
222 + * Check for Tombstone Objects
223 + *
224 + * @see https://www.w3.org/TR/activitypub/#delete-activity-outbox
225 + *
226 + * @param WP_Error $wp_error A WP_Error-Response of an HTTP-Request
227 + *
228 + * @return boolean true if HTTP-Code is 410 or 404
229 + */
230 +function is_tombstone( $wp_error ) {
231 + if ( ! is_wp_error( $wp_error ) ) {
232 + return false;
233 + }
234 +
235 + if ( in_array( (int) $wp_error->get_error_code(), array( 404, 410 ), true ) ) {
236 + return true;
237 + }
238 +
239 + return false;
240 +}
241 +
242 +/**
243 + * Get the REST URL relative to this plugin's namespace.
244 + *
245 + * @param string $path Optional. REST route path. Otherwise this plugin's namespaced root.
246 + *
247 + * @return string REST URL relative to this plugin's namespace.
248 + */
249 +function get_rest_url_by_path( $path = '' ) {
250 + // we'll handle the leading slash.
251 + $path = ltrim( $path, '/' );
252 + $namespaced_path = sprintf( '/%s/%s', ACTIVITYPUB_REST_NAMESPACE, $path );
253 + return \get_rest_url( null, $namespaced_path );
254 +}
255 +
256 +/**
34 257 * Convert a string from camelCase to snake_case.
35 258 *
36 - * @param string $input The string to convert.
259 + * @param string $string The string to convert.
37 260 *
38 261 * @return string The converted string.
39 262 */
40 -function camel_to_snake_case( $input ) {
41 - return \strtolower( \preg_replace( '/(?<!^)[A-Z]/', '_$0', $input ) );
263 +// phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.stringFound
264 +function camel_to_snake_case( $string ) {
265 + return strtolower( preg_replace( '/(?<!^)[A-Z]/', '_$0', $string ) );
42 266 }
43 267
44 268 /**
45 269 * Convert a string from snake_case to camelCase.
46 270 *
47 - * @param string $input The string to convert.
271 + * @param string $string The string to convert.
48 272 *
49 273 * @return string The converted string.
50 274 */
51 -function snake_to_camel_case( $input ) {
52 - return \lcfirst( \str_replace( '_', '', \ucwords( $input, '_' ) ) );
275 +// phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.stringFound
276 +function snake_to_camel_case( $string ) {
277 + return lcfirst( str_replace( '_', '', ucwords( $string, '_' ) ) );
53 278 }
54 279
55 280 /**
56 - * Convert seconds to ISO 8601 duration format.
281 + * Escapes a Tag, to be used as a hashtag.
57 282 *
58 - * @param int $seconds The duration in seconds.
283 + * @param string $string The string to escape.
59 284 *
60 - * @return string The duration in ISO 8601 format (e.g., "PT1H23M45S").
285 + * @return string The escaped hastag.
61 286 */
62 -function seconds_to_iso8601( $seconds ) {
63 - $seconds = (int) $seconds;
287 +function esc_hashtag( $string ) {
64 288
65 - if ( $seconds <= 0 ) {
66 - return 'PT0S';
289 + $hashtag = \wp_specialchars_decode( $string, ENT_QUOTES );
290 + // Remove all characters that are not letters, numbers, or underscores.
291 + $hashtag = \preg_replace( '/emoji-regex(*SKIP)(?!)|[^\p{L}\p{Nd}_]+/u', '_', $hashtag );
292 +
293 + // Capitalize every letter that is preceded by an underscore.
294 + $hashtag = preg_replace_callback(
295 + '/_(.)/',
296 + function ( $matches ) {
297 + return '' . strtoupper( $matches[1] );
298 + },
299 + $hashtag
300 + );
301 +
302 + // Add a hashtag to the beginning of the string.
303 + $hashtag = ltrim( $hashtag, '#' );
304 + $hashtag = '#' . $hashtag;
305 +
306 + /**
307 + * Allow defining your own custom hashtag generation rules.
308 + *
309 + * @param string $hashtag The hashtag to be returned.
310 + * @param string $string The original string.
311 + */
312 + $hashtag = apply_filters( 'activitypub_esc_hashtag', $hashtag, $string );
313 +
314 + return esc_html( $hashtag );
315 +}
316 +
317 +/**
318 + * Check if a request is for an ActivityPub request.
319 + *
320 + * @return bool False by default.
321 + */
322 +function is_activitypub_request() {
323 + global $wp_query;
324 +
325 + /*
326 + * ActivityPub requests are currently only made for
327 + * author archives, singular posts, and the homepage.
328 + */
329 + if ( ! \is_author() && ! \is_singular() && ! \is_home() && ! defined( '\REST_REQUEST' ) ) {
330 + return false;
67 331 }
68 332
69 - $hours = \floor( $seconds / 3600 );
70 - $minutes = \floor( ( $seconds % 3600 ) / 60 );
71 - $secs = $seconds % 60;
333 + // Check if the current post type supports ActivityPub.
334 + if ( \is_singular() ) {
335 + $queried_object = \get_queried_object();
336 + $post_type = \get_post_type( $queried_object );
72 337
73 - $duration = 'PT';
338 + if ( ! \post_type_supports( $post_type, 'activitypub' ) ) {
339 + return false;
340 + }
341 + }
74 342
75 - if ( $hours > 0 ) {
76 - $duration .= $hours . 'H';
343 + // Check if header already sent.
344 + if ( ! \headers_sent() && ACTIVITYPUB_SEND_VARY_HEADER ) {
345 + // Send Vary header for Accept header.
346 + \header( 'Vary: Accept' );
77 347 }
78 348
79 - if ( $minutes > 0 ) {
80 - $duration .= $minutes . 'M';
349 + // One can trigger an ActivityPub request by adding ?activitypub to the URL.
350 + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration
351 + global $wp_query;
352 + if ( isset( $wp_query->query_vars['activitypub'] ) ) {
353 + return true;
81 354 }
82 355
83 - if ( $secs > 0 || ( 0 === $hours && 0 === $minutes ) ) {
84 - $duration .= $secs . 'S';
356 + /*
357 + * The other (more common) option to make an ActivityPub request
358 + * is to send an Accept header.
359 + */
360 + if ( isset( $_SERVER['HTTP_ACCEPT'] ) ) {
361 + $accept = sanitize_text_field( wp_unslash( $_SERVER['HTTP_ACCEPT'] ) );
362 +
363 + /*
364 + * $accept can be a single value, or a comma separated list of values.
365 + * We want to support both scenarios,
366 + * and return true when the header includes at least one of the following:
367 + * - application/activity+json
368 + * - application/ld+json
369 + * - application/json
370 + */
371 + if ( preg_match( '/(application\/(ld\+json|activity\+json|json))/i', $accept ) ) {
372 + return true;
373 + }
85 374 }
86 375
87 - return $duration;
376 + return false;
88 377 }
89 378
90 379 /**
380 + * This function checks if a user is disabled for ActivityPub.
381 + *
382 + * @param int $user_id The User-ID.
383 + *
384 + * @return boolean True if the user is disabled, false otherwise.
385 + */
386 +function is_user_disabled( $user_id ) {
387 + $return = false;
388 +
389 + switch ( $user_id ) {
390 + // if the user is the application user, it's always enabled.
391 + case \Activitypub\Collection\Users::APPLICATION_USER_ID:
392 + $return = false;
393 + break;
394 + // if the user is the blog user, it's only enabled in single-user mode.
395 + case \Activitypub\Collection\Users::BLOG_USER_ID:
396 + if ( is_user_type_disabled( 'blog' ) ) {
397 + $return = true;
398 + break;
399 + }
400 +
401 + $return = false;
402 + break;
403 + // if the user is any other user, it's enabled if it can publish posts.
404 + default:
405 + if ( ! \get_user_by( 'id', $user_id ) ) {
406 + $return = true;
407 + break;
408 + }
409 +
410 + if ( is_user_type_disabled( 'user' ) ) {
411 + $return = true;
412 + break;
413 + }
414 +
415 + if ( ! \user_can( $user_id, 'activitypub' ) ) {
416 + $return = true;
417 + break;
418 + }
419 +
420 + $return = false;
421 + break;
422 + }
423 +
424 + return apply_filters( 'activitypub_is_user_disabled', $return, $user_id );
425 +}
426 +
427 +/**
428 + * Checks if a User-Type is disabled for ActivityPub.
429 + *
430 + * This function is used to check if the 'blog' or 'user'
431 + * type is disabled for ActivityPub.
432 + *
433 + * @param enum $type Can be 'blog' or 'user'.
434 + *
435 + * @return boolean True if the user type is disabled, false otherwise.
436 + */
437 +function is_user_type_disabled( $type ) {
438 + switch ( $type ) {
439 + case 'blog':
440 + if ( \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) ) {
441 + if ( ACTIVITYPUB_SINGLE_USER_MODE ) {
442 + $return = false;
443 + break;
444 + }
445 + }
446 +
447 + if ( \defined( 'ACTIVITYPUB_DISABLE_BLOG_USER' ) ) {
448 + $return = ACTIVITYPUB_DISABLE_BLOG_USER;
449 + break;
450 + }
451 +
452 + if ( '1' !== \get_option( 'activitypub_enable_blog_user', '0' ) ) {
453 + $return = true;
454 + break;
455 + }
456 +
457 + $return = false;
458 + break;
459 + case 'user':
460 + if ( \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) ) {
461 + if ( ACTIVITYPUB_SINGLE_USER_MODE ) {
462 + $return = true;
463 + break;
464 + }
465 + }
466 +
467 + if ( \defined( 'ACTIVITYPUB_DISABLE_USER' ) ) {
468 + $return = ACTIVITYPUB_DISABLE_USER;
469 + break;
470 + }
471 +
472 + if ( '1' !== \get_option( 'activitypub_enable_users', '1' ) ) {
473 + $return = true;
474 + break;
475 + }
476 +
477 + $return = false;
478 + break;
479 + default:
480 + $return = new WP_Error(
481 + 'activitypub_wrong_user_type',
482 + __( 'Wrong user type', 'activitypub' ),
483 + array( 'status' => 400 )
484 + );
485 + break;
486 + }
487 +
488 + return apply_filters( 'activitypub_is_user_type_disabled', $return, $type );
489 +}
490 +
491 +/**
492 + * Check if the blog is in single-user mode.
493 + *
494 + * @return boolean True if the blog is in single-user mode, false otherwise.
495 + */
496 +function is_single_user() {
497 + if (
498 + false === is_user_type_disabled( 'blog' ) &&
499 + true === is_user_type_disabled( 'user' )
500 + ) {
501 + return true;
502 + }
503 +
504 + return false;
505 +}
506 +
507 +/**
91 508 * Check if a site supports the block editor.
92 509 *
93 510 * @return boolean True if the site supports the block editor, false otherwise.
94 511 */
95 512 function site_supports_blocks() {
513 + if ( \version_compare( \get_bloginfo( 'version' ), '5.9', '<' ) ) {
514 + return false;
515 + }
516 +
517 + if ( ! \function_exists( 'register_block_type_from_metadata' ) ) {
518 + return false;
519 + }
520 +
96 521 /**
97 522 * Allow plugins to disable block editor support,
98 523 * thus disabling blocks registered by the ActivityPub plugin.
99 524 *
@@ -98,118 +523,458 @@
98 523 * thus disabling blocks registered by the ActivityPub plugin.
99 524 *
100 525 * @param boolean $supports_blocks True if the site supports the block editor, false otherwise.
101 526 */
102 - return \apply_filters( 'activitypub_site_supports_blocks', true );
527 + return apply_filters( 'activitypub_site_supports_blocks', true );
103 528 }
104 529
105 530 /**
106 - * Get the icon Image object for site-wide ActivityPub actors.
531 + * Check if data is valid JSON.
107 532 *
108 - * Tries the site icon first, then the custom logo, and falls back to the
109 - * bundled WordPress logo.
533 + * @param string $data The data to check.
110 534 *
111 - * @since 9.1.0
535 + * @return boolean True if the data is JSON, false otherwise.
536 + */
537 +function is_json( $data ) {
538 + return \is_array( \json_decode( $data, true ) ) ? true : false;
539 +}
540 +
541 +/**
542 + * Check if a blog is public based on the `blog_public` option
112 543 *
113 - * @return array The icon array with 'type' and 'url'.
544 + * @return bollean True if public, false if not
114 545 */
115 -function site_icon() {
116 - // Try site icon first.
117 - $icon_id = \get_option( 'site_icon' );
546 +function is_blog_public() {
547 + return (bool) apply_filters( 'activitypub_is_blog_public', \get_option( 'blog_public', 1 ) );
548 +}
118 549
119 - // Try custom logo second.
120 - if ( ! $icon_id ) {
121 - $icon_id = \get_theme_mod( 'custom_logo' );
550 +/**
551 + * Sanitize a URL
552 + *
553 + * @param string $value The URL to sanitize
554 + *
555 + * @return string|null The sanitized URL or null if invalid
556 + */
557 +function sanitize_url( $value ) {
558 + if ( filter_var( $value, FILTER_VALIDATE_URL ) === false ) {
559 + return null;
122 560 }
123 561
124 - $icon_url = false;
562 + return esc_url_raw( $value );
563 +}
125 564
126 - if ( $icon_id ) {
127 - $icon = \wp_get_attachment_image_src( $icon_id, 'full' );
128 - if ( $icon ) {
129 - $icon_url = $icon[0];
565 +/**
566 + * Extract recipient URLs from Activity object
567 + *
568 + * @param array $data
569 + *
570 + * @return array The list of user URLs
571 + */
572 +function extract_recipients_from_activity( $data ) {
573 + $recipient_items = array();
574 +
575 + foreach ( array( 'to', 'bto', 'cc', 'bcc', 'audience' ) as $i ) {
576 + if ( array_key_exists( $i, $data ) ) {
577 + if ( is_array( $data[ $i ] ) ) {
578 + $recipient = $data[ $i ];
579 + } else {
580 + $recipient = array( $data[ $i ] );
581 + }
582 + $recipient_items = array_merge( $recipient_items, $recipient );
130 583 }
584 +
585 + if ( is_array( $data['object'] ) && array_key_exists( $i, $data['object'] ) ) {
586 + if ( is_array( $data['object'][ $i ] ) ) {
587 + $recipient = $data['object'][ $i ];
588 + } else {
589 + $recipient = array( $data['object'][ $i ] );
590 + }
591 + $recipient_items = array_merge( $recipient_items, $recipient );
592 + }
131 593 }
132 594
133 - if ( ! $icon_url ) {
134 - // Fallback to default icon.
135 - $icon_url = \plugins_url( '/assets/img/wp-logo.png', ACTIVITYPUB_PLUGIN_FILE );
595 + $recipients = array();
596 +
597 + // flatten array
598 + foreach ( $recipient_items as $recipient ) {
599 + if ( is_array( $recipient ) ) {
600 + // check if recipient is an object
601 + if ( array_key_exists( 'id', $recipient ) ) {
602 + $recipients[] = $recipient['id'];
603 + }
604 + } else {
605 + $recipients[] = $recipient;
606 + }
136 607 }
137 608
138 - return array(
139 - 'type' => 'Image',
140 - 'url' => \esc_url_raw( $icon_url ),
609 + return array_unique( $recipients );
610 +}
611 +
612 +/**
613 + * Check if passed Activity is Public
614 + *
615 + * @param array $data The Activity object as array
616 + *
617 + * @return boolean True if public, false if not
618 + */
619 +function is_activity_public( $data ) {
620 + $recipients = extract_recipients_from_activity( $data );
621 +
622 + return in_array( 'https://www.w3.org/ns/activitystreams#Public', $recipients, true );
623 +}
624 +
625 +/**
626 + * Get active users based on a given duration
627 + *
628 + * @param int $duration The duration to check in month(s)
629 + *
630 + * @return int The number of active users
631 + */
632 +function get_active_users( $duration = 1 ) {
633 +
634 + $duration = intval( $duration );
635 + $transient_key = sprintf( 'monthly_active_users_%d', $duration );
636 + $count = get_transient( $transient_key );
637 +
638 + if ( false === $count ) {
639 + global $wpdb;
640 + $query = "SELECT COUNT( DISTINCT post_author ) FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' AND post_date <= DATE_SUB( NOW(), INTERVAL %d MONTH )";
641 + $query = $wpdb->prepare( $query, $duration );
642 + $count = $wpdb->get_var( $query ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
643 +
644 + set_transient( $transient_key, $count, DAY_IN_SECONDS );
645 + }
646 +
647 + // if 0 authors where active
648 + if ( 0 === $count ) {
649 + return 0;
650 + }
651 +
652 + // if single user mode
653 + if ( is_single_user() ) {
654 + return 1;
655 + }
656 +
657 + // if blog user is disabled
658 + if ( is_user_disabled( Users::BLOG_USER_ID ) ) {
659 + return (int) $count;
660 + }
661 +
662 + // also count blog user
663 + return (int) $count + 1;
664 +}
665 +
666 +/**
667 + * Get the total number of users
668 + *
669 + * @return int The total number of users
670 + */
671 +function get_total_users() {
672 + // if single user mode
673 + if ( is_single_user() ) {
674 + return 1;
675 + }
676 +
677 + $users = \get_users(
678 + array(
679 + 'capability__in' => array( 'activitypub' ),
680 + )
141 681 );
682 +
683 + if ( is_array( $users ) ) {
684 + $users = count( $users );
685 + } else {
686 + $users = 1;
687 + }
688 +
689 + // if blog user is disabled
690 + if ( is_user_disabled( Users::BLOG_USER_ID ) ) {
691 + return (int) $users;
692 + }
693 +
694 + return (int) $users + 1;
142 695 }
143 696
144 697 /**
145 - * Check whether a blog is public based on the `blog_public` option.
698 + * Examine a comment ID and look up an existing comment it represents.
146 699 *
147 - * @return bool True if public, false if not
700 + * @param string $id ActivityPub object ID (usually a URL) to check.
701 + *
702 + * @return int|boolean Comment ID, or false on failure.
148 703 */
149 -function is_blog_public() {
150 - /**
151 - * Filter whether the blog is public.
152 - *
153 - * @param bool $public Whether the blog is public.
154 - */
155 - return (bool) \apply_filters( 'activitypub_is_blog_public', \get_option( 'blog_public', 1 ) );
704 +function object_id_to_comment( $id ) {
705 + return Comment::object_id_to_comment( $id );
156 706 }
157 707
158 708 /**
709 + * Verify if URL is a local comment,
710 + * Or if it is a previously received remote comment
711 + * (For threading comments locally)
712 + *
713 + * @param string $url The URL to check.
714 + *
715 + * @return int comment_ID or null if not found
716 + */
717 +function url_to_commentid( $url ) {
718 + return Comment::url_to_commentid( $url );
719 +}
720 +
721 +/**
722 + * Get the URI of an ActivityPub object
723 + *
724 + * @param array $object The ActivityPub object
725 + *
726 + * @return string The URI of the ActivityPub object
727 + */
728 +function object_to_uri( $object ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.objectFound
729 + // check if it is already simple
730 + if ( ! $object || is_string( $object ) ) {
731 + return $object;
732 + }
733 +
734 + // check if it is a list, then take first item
735 + // this plugin does not support collections
736 + if ( array_is_list( $object ) ) {
737 + $object = $object[0];
738 + }
739 +
740 + // check if it is simplified now
741 + if ( is_string( $object ) ) {
742 + return $object;
743 + }
744 +
745 + $type = 'Object';
746 + if ( isset( $object['type'] ) ) {
747 + $type = $object['type'];
748 + }
749 +
750 + // return part of Object that makes most sense
751 + switch ( $type ) {
752 + case 'Link':
753 + $object = $object['href'];
754 + break;
755 + default:
756 + $object = $object['id'];
757 + break;
758 + }
759 +
760 + return $object;
761 +}
762 +
763 +/**
764 + * Check if a comment should be federated.
765 + *
766 + * We consider a comment should be federated if it is authored by a user that is
767 + * not disabled for federation and if it is a reply directly to the post or to a
768 + * federated comment.
769 + *
770 + * @param mixed $comment Comment object or ID.
771 + *
772 + * @return boolean True if the comment should be federated, false otherwise.
773 + */
774 +function should_comment_be_federated( $comment ) {
775 + return Comment::should_be_federated( $comment );
776 +}
777 +
778 +/**
779 + * Check if a comment was federated.
780 + *
781 + * This function checks if a comment was federated via ActivityPub.
782 + *
783 + * @param mixed $comment Comment object or ID.
784 + *
785 + * @return boolean True if the comment was federated, false otherwise.
786 + */
787 +function was_comment_sent( $comment ) {
788 + return Comment::was_sent( $comment );
789 +}
790 +
791 +/**
792 + * Check if a comment is federated.
793 + *
794 + * We consider a comment federated if comment was received via ActivityPub.
795 + *
796 + * Use this function to check if it is comment that was received via ActivityPub.
797 + *
798 + * @param mixed $comment Comment object or ID.
799 + *
800 + * @return boolean True if the comment is federated, false otherwise.
801 + */
802 +function was_comment_received( $comment ) {
803 + return Comment::was_received( $comment );
804 +}
805 +
806 +/**
807 + * Check if a comment is local only.
808 + *
809 + * This function checks if a comment is local only and was not sent or received via ActivityPub.
810 + *
811 + * @param mixed $comment Comment object or ID.
812 + *
813 + * @return boolean True if the comment is local only, false otherwise.
814 + */
815 +function is_local_comment( $comment ) {
816 + return Comment::is_local( $comment );
817 +}
818 +
819 +/**
820 + * Mark a WordPress object as federated.
821 + *
822 + * @param WP_Comment|WP_Post|mixed $wp_object
823 + *
824 + * @return void
825 + */
826 +function set_wp_object_state( $wp_object, $state ) {
827 + $meta_key = 'activitypub_status';
828 +
829 + if ( $wp_object instanceof \WP_Post ) {
830 + \update_post_meta( $wp_object->ID, $meta_key, $state );
831 + } elseif ( $wp_object instanceof \WP_Comment ) {
832 + \update_comment_meta( $wp_object->comment_ID, $meta_key, $state );
833 + } else {
834 + \apply_filters( 'activitypub_mark_wp_object_as_federated', $wp_object );
835 + }
836 +}
837 +
838 +/**
839 + * Get the federation state of a WordPress object.
840 + *
841 + * @param WP_Comment|WP_Post|mixed $wp_object
842 + *
843 + * @return string|false The state of the object or false if not found.
844 + */
845 +function get_wp_object_state( $wp_object ) {
846 + $meta_key = 'activitypub_status';
847 +
848 + if ( $wp_object instanceof \WP_Post ) {
849 + return \get_post_meta( $wp_object->ID, $meta_key, true );
850 + } elseif ( $wp_object instanceof \WP_Comment ) {
851 + return \get_comment_meta( $wp_object->comment_ID, $meta_key, true );
852 + } else {
853 + return \apply_filters( 'activitypub_get_wp_object_state', false, $wp_object );
854 + }
855 +}
856 +
857 +/**
858 + * Get the description of a post type.
859 + *
860 + * Set some default descriptions for the default post types.
861 + *
862 + * @param WP_Post_Type $post_type The post type object.
863 + *
864 + * @return string The description of the post type.
865 + */
866 +function get_post_type_description( $post_type ) {
867 + $description = '';
868 +
869 + switch ( $post_type->name ) {
870 + case 'post':
871 + $description = '';
872 + break;
873 + case 'page':
874 + $description = '';
875 + break;
876 + case 'attachment':
877 + $description = ' - ' . __( 'The attachments that you have uploaded to a post (images, videos, documents or other files).', 'activitypub' );
878 + break;
879 + default:
880 + if ( ! empty( $post_type->description ) ) {
881 + $description = ' - ' . $post_type->description;
882 + }
883 + }
884 +
885 + return apply_filters( 'activitypub_post_type_description', $description, $post_type->name, $post_type );
886 +}
887 +
888 +/**
159 889 * Get the masked WordPress version to only show the major and minor version.
160 890 *
161 891 * @return string The masked version.
162 892 */
163 893 function get_masked_wp_version() {
164 - // Only show the major and minor version.
165 - $version = \get_bloginfo( 'version' );
166 - // Strip the RC or beta part.
167 - $version = \preg_replace( '/-.*$/', '', $version );
168 - $version = \explode( '.', $version );
169 - $version = \array_slice( $version, 0, 2 );
894 + // only show the major and minor version
895 + $version = get_bloginfo( 'version' );
896 + // strip the RC or beta part
897 + $version = preg_replace( '/-.*$/', '', $version );
898 + $version = explode( '.', $version );
899 + $version = array_slice( $version, 0, 2 );
170 900
171 - return \implode( '.', $version );
901 + return implode( '.', $version );
172 902 }
173 903
174 904 /**
175 - * Check if a plugin is active, loading plugin.php if necessary.
905 + * Get the enclosures of a post.
176 906 *
177 - * This is a wrapper around the core is_plugin_active() function that ensures
178 - * the function is available by loading wp-admin/includes/plugin.php if needed.
179 - * This is useful when checking plugin status outside of the admin context.
907 + * @param int $post_id The post ID.
180 908 *
181 - * @param string $plugin Plugin basename (e.g., 'plugin-folder/plugin-file.php').
182 - *
183 - * @return bool True if the plugin is active, false otherwise.
909 + * @return array The enclosures.
184 910 */
185 -function is_plugin_active( $plugin ) {
186 - // Include plugin.php if not already loaded (needed for core is_plugin_active).
187 - if ( ! \function_exists( 'is_plugin_active' ) ) {
188 - require_once ABSPATH . 'wp-admin/includes/plugin.php';
911 +function get_enclosures( $post_id ) {
912 + $enclosures = get_post_meta( $post_id, 'enclosure' );
913 +
914 + if ( ! $enclosures ) {
915 + return array();
189 916 }
190 917
191 - return \is_plugin_active( $plugin );
918 + $enclosures = array_map(
919 + function ( $enclosure ) {
920 + $attributes = explode( "\n", $enclosure );
921 +
922 + if ( ! isset( $attributes[0] ) || ! \wp_http_validate_url( $attributes[0] ) ) {
923 + return false;
924 + }
925 +
926 + return array(
927 + 'url' => $attributes[0],
928 + 'length' => isset( $attributes[1] ) ? trim( $attributes[1] ) : null,
929 + 'mediaType' => isset( $attributes[2] ) ? trim( $attributes[2] ) : null,
930 + );
931 + },
932 + $enclosures
933 + );
934 +
935 + return array_filter( $enclosures );
192 936 }
193 937
194 938 /**
195 - * Returns the website hosts allowed to credit this blog.
939 + * Retrieves the IDs of the ancestors of a comment.
196 940 *
197 - * @return array|null The attribution domains or null if not found.
941 + * Adaption of `get_post_ancestors` from WordPress core.
942 + *
943 + * @see https://developer.wordpress.org/reference/functions/get_post_ancestors/
944 + *
945 + * @param int|WP_Comment $comment Comment ID or comment object.
946 + *
947 + * @return WP_Comment[] Array of ancestor comments or empty array if there are none.
198 948 */
199 -function get_attribution_domains() {
200 - if ( '1' !== \get_option( 'activitypub_use_opengraph', '1' ) ) {
201 - return null;
949 +function get_comment_ancestors( $comment ) {
950 + $comment = \get_comment( $comment );
951 +
952 + // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
953 + if ( ! $comment || empty( $comment->comment_parent ) || $comment->comment_parent == $comment->comment_ID ) {
954 + return array();
202 955 }
203 956
204 - $domains = \get_option( 'activitypub_attribution_domains', home_host() );
205 - $domains = \explode( PHP_EOL, $domains );
957 + $ancestors = array();
206 958
207 - if ( ! $domains ) {
208 - $domains = null;
959 + $id = (int) $comment->comment_parent;
960 + $ancestors[] = $id;
961 +
962 + // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
963 + while ( $id > 0 ) {
964 + $ancestor = \get_comment( $id );
965 + $parent_id = (int) $ancestor->comment_parent;
966 +
967 + // Loop detection: If the ancestor has been seen before, break.
968 + if ( empty( $parent_id ) || ( $parent_id === (int) $comment->comment_ID ) || in_array( $parent_id, $ancestors, true ) ) {
969 + break;
970 + }
971 +
972 + $id = $parent_id;
973 + $ancestors[] = $id;
209 974 }
210 975
211 - return $domains;
976 + return $ancestors;
212 977 }
213 978
214 979 /**
215 980 * Change the display of large numbers on the site.
@@ -219,12 +984,13 @@
219 984 * @see https://wordpress.org/support/topic/abbreviate-numbers-with-k/
220 985 *
221 986 * @param string $formatted Converted number in string format.
222 987 * @param float $number The number to convert based on locale.
988 + * @param int $decimals Precision of the number of decimal places.
223 989 *
224 990 * @return string Converted number in string format.
225 991 */
226 -function custom_large_numbers( $formatted, $number ) {
992 +function custom_large_numbers( $formatted, $number, $decimals ) {
227 993 global $wp_locale;
228 994
229 995 $decimals = 0;
230 996 $decimal_point = '.';
@@ -235,72 +1001,110 @@
235 1001 $decimal_point = $wp_locale->number_format['decimal_point'];
236 1002 $thousands_sep = $wp_locale->number_format['thousands_sep'];
237 1003 }
238 1004
239 - if ( $number < 1000 ) { // Any number less than a Thousand.
1005 + if ( $number < 1000 ) { // any number less than a Thousand.
240 1006 return \number_format( $number, $decimals, $decimal_point, $thousands_sep );
241 - } elseif ( $number < 1000000 ) { // Any number less than a million.
1007 + } elseif ( $number < 1000000 ) { // any number less than a million
242 1008 return \number_format( $number / 1000, $decimals, $decimal_point, $thousands_sep ) . 'K';
243 - } elseif ( $number < 1000000000 ) { // Any number less than a billion.
1009 + } elseif ( $number < 1000000000 ) { // any number less than a billion
244 1010 return \number_format( $number / 1000000, $decimals, $decimal_point, $thousands_sep ) . 'M';
245 - } else { // At least a billion.
1011 + } else { // at least a billion
246 1012 return \number_format( $number / 1000000000, $decimals, $decimal_point, $thousands_sep ) . 'B';
247 1013 }
1014 +
1015 + // Default fallback. We should not get here.
1016 + return $formatted;
248 1017 }
249 1018
250 1019 /**
251 - * Escapes a Tag, to be used as a hashtag.
1020 + * Registers a ActivityPub comment type.
252 1021 *
253 - * @param string $input The string to escape.
254 1022 *
255 - * @return string The escaped hashtag.
1023 + * @param string $comment_type Key for comment type.
1024 + * @param array $args Arguments.
1025 + *
1026 + * @return array The registered Activitypub comment type.
256 1027 */
257 -function esc_hashtag( $input ) {
258 - $hashtag = \wp_specialchars_decode( $input, ENT_QUOTES );
259 - // Remove all characters that are not letters, numbers, or hyphens.
260 - $hashtag = \preg_replace( '/[^\p{L}\p{Nd}-]+/u', '-', $hashtag );
1028 +function register_comment_type( $comment_type, $args = array() ) {
1029 + global $activitypub_comment_types;
261 1030
262 - // Capitalize every letter that is preceded by a hyphen.
263 - $hashtag = \preg_replace_callback(
264 - '/-+(.)/',
265 - static function ( $matches ) {
266 - return \strtoupper( $matches[1] );
267 - },
268 - $hashtag
269 - );
1031 + if ( ! is_array( $activitypub_comment_types ) ) {
1032 + $activitypub_comment_types = array();
1033 + }
270 1034
271 - // Add a hashtag to the beginning of the string.
272 - $hashtag = \ltrim( $hashtag, '#' );
273 - $hashtag = \trim( $hashtag, '-' );
274 - $hashtag = '#' . $hashtag;
1035 + // Sanitize comment type name.
1036 + $comment_type = sanitize_key( $comment_type );
275 1037
1038 + $activitypub_comment_types[ $comment_type ] = $args;
1039 +
276 1040 /**
277 - * Allow defining your own custom hashtag generation rules.
1041 + * Fires after a ActivityPub comment type is registered.
278 1042 *
279 - * @param string $hashtag The hashtag to be returned.
280 - * @param string $input The original string.
1043 + *
1044 + * @param string $comment_type Comment type.
1045 + * @param array $args Arguments used to register the comment type.
281 1046 */
282 - $hashtag = \apply_filters( 'activitypub_esc_hashtag', $hashtag, $input );
1047 + do_action( 'activitypub_registered_comment_type', $comment_type, $args );
283 1048
284 - return \esc_html( $hashtag );
1049 + return $args;
285 1050 }
286 1051
287 1052 /**
1053 + * Normalize a URL.
1054 + *
1055 + * @param string $url The URL.
1056 + *
1057 + * @return string The normalized URL.
1058 + */
1059 +function normalize_url( $url ) {
1060 + $url = \untrailingslashit( $url );
1061 + $url = \str_replace( 'https://', '', $url );
1062 + $url = \str_replace( 'http://', '', $url );
1063 + $url = \str_replace( 'www.', '', $url );
1064 +
1065 + return $url;
1066 +}
1067 +
1068 +/**
1069 + * Normalize a host.
1070 + *
1071 + * @param string $host The host.
1072 + *
1073 + * @return string The normalized host.
1074 + */
1075 +function normalize_host( $host ) {
1076 + return \str_replace( 'www.', '', $host );
1077 +}
1078 +
1079 +/**
1080 + * Get the reply intent URI.
1081 + *
1082 + * @return string The reply intent URI.
1083 + */
1084 +function get_reply_intent_uri() {
1085 + return sprintf(
1086 + 'javascript:(()=>{window.open(\'%s\'+encodeURIComponent(window.location.href));})();',
1087 + esc_url( \admin_url( 'post-new.php?in_reply_to=' ) )
1088 + );
1089 +}
1090 +
1091 +/**
288 1092 * Replace content with links, mentions or hashtags by Regex callback and not affect protected tags.
289 1093 *
290 - * @param string $content The content that should be changed.
291 - * @param string $regex The regex to use.
292 - * @param callable $regex_callback Callback for replacement logic.
1094 + * @param $content string The content that should be changed
1095 + * @param $regex string The regex to use
1096 + * @param $regex_callback callable Callback for replacement logic
293 1097 *
294 1098 * @return string The content with links, mentions, hashtags, etc.
295 1099 */
296 1100 function enrich_content_data( $content, $regex, $regex_callback ) {
297 - // Small protection against execution timeouts: limit to 1 MB.
298 - if ( \mb_strlen( $content ) > MB_IN_BYTES ) {
1101 + // small protection against execution timeouts: limit to 1 MB
1102 + if ( mb_strlen( $content ) > MB_IN_BYTES ) {
299 1103 return $content;
300 1104 }
301 - $tag_stack = array();
302 - $protected_tags = array(
1105 + $tag_stack = array();
1106 + $protected_tags = array(
303 1107 'pre',
304 1108 'code',
305 1109 'textarea',
306 1110 'style',
@@ -306,23 +1110,23 @@
306 1110 'style',
307 1111 'a',
308 1112 );
309 1113 $content_with_links = '';
310 - $in_protected_tag = false;
311 - foreach ( \wp_html_split( $content ) as $chunk ) {
312 - if ( \preg_match( '#^<!--[\s\S]*-->$#i', $chunk, $m ) ) {
1114 + $in_protected_tag = false;
1115 + foreach ( wp_html_split( $content ) as $chunk ) {
1116 + if ( preg_match( '#^<!--[\s\S]*-->$#i', $chunk, $m ) ) {
313 1117 $content_with_links .= $chunk;
314 1118 continue;
315 1119 }
316 1120
317 - if ( \preg_match( '#^<(/)?([a-z-]+)\b[^>]*>$#i', $chunk, $m ) ) {
318 - $tag = \strtolower( $m[2] );
1121 + if ( preg_match( '#^<(/)?([a-z-]+)\b[^>]*>$#i', $chunk, $m ) ) {
1122 + $tag = strtolower( $m[2] );
319 1123 if ( '/' === $m[1] ) {
320 1124 // Closing tag.
321 - $i = \array_search( $tag, $tag_stack, true );
1125 + $i = array_search( $tag, $tag_stack, true );
322 1126 // We can only remove the tag from the stack if it is in the stack.
323 1127 if ( false !== $i ) {
324 - $tag_stack = \array_slice( $tag_stack, 0, $i );
1128 + $tag_stack = array_slice( $tag_stack, 0, $i );
325 1129 }
326 1130 } else {
327 1131 // Opening tag, add it to the stack.
328 1132 $tag_stack[] = $tag;
@@ -329,9 +1133,9 @@
329 1133 }
330 1134
331 1135 // If we're in a protected tag, the tag_stack contains at least one protected tag string.
332 1136 // The protected tag state can only change when we encounter a start or end tag.
333 - $in_protected_tag = \array_intersect( $tag_stack, $protected_tags );
1137 + $in_protected_tag = array_intersect( $tag_stack, $protected_tags );
334 1138
335 1139 // Never inspect tags.
336 1140 $content_with_links .= $chunk;
337 1141 continue;
@@ -350,108 +1154,64 @@
350 1154 return $content_with_links;
351 1155 }
352 1156
353 1157 /**
354 - * Get an ActivityPub embed HTML for a URL.
1158 + * Generate a summary of a post.
355 1159 *
356 - * @param string $url The URL to get the embed for.
357 - * @param boolean $inline_css Whether to inline CSS. Default true.
1160 + * This function generates a summary of a post by extracting:
358 1161 *
359 - * @return string|false The embed HTML or false if not found.
360 - */
361 -function get_embed_html( $url, $inline_css = true ) {
362 - return Embed::get_html( $url, $inline_css );
363 -}
364 -
365 -/**
366 - * Get the client IP address for rate-limiting purposes.
1162 + * 1. The post excerpt if it exists.
1163 + * 2. The first part of the post content if it contains the <!--more--> tag.
1164 + * 3. An excerpt of the post content if it is longer than the specified length.
367 1165 *
368 - * Walks the ordered list of $_SERVER keys returned by the
369 - * `activitypub_client_ip_sources` filter (default: `['REMOTE_ADDR']`) and
370 - * returns the first value that parses as a valid IP literal, validated via
371 - * `filter_var( ..., FILTER_VALIDATE_IP )`. The result can be overridden
372 - * outright via the `activitypub_client_ip` filter; that filter's output is
373 - * also validated and replaced with `''` when it isn't a valid IP, so a
374 - * misbehaving filter can't collide all callers into the same rate-limit
375 - * bucket.
1166 + * @param int|WP_Post $post The post ID or post object.
1167 + * @param integer $length The maximum length of the summary.
1168 + * Default is 500. It will ne ignored if the post excerpt
1169 + * and the content above the <!--more--> tag.
376 1170 *
377 - * Trusting any source other than `REMOTE_ADDR` is only safe behind a
378 - * reverse proxy that sets and overwrites the corresponding header — see
379 - * the `activitypub_client_ip_sources` filter docblock for guidance.
380 - *
381 - * Callers using the return value as a rate-limit key should treat an
382 - * empty return as "client unidentifiable" and fail closed rather than
383 - * share a single bucket across every such request.
384 - *
385 - * @since 8.1.0
386 - *
387 - * @return string A valid IP address, or '' when no IP could be determined.
1171 + * @return string The generated post summary.
388 1172 */
389 -function get_client_ip() {
390 - // phpcs:disable WordPressVIPMinimum.Variables.ServerVariables.UserControlledHeaders
391 - $ip = '';
1173 +function generate_post_summary( $post, $length = 500 ) {
1174 + $post = get_post( $post );
392 1175
393 - /**
394 - * Filter the ordered list of $_SERVER keys to consult as a source for the
395 - * client IP. The first key whose value parses as a valid IP wins.
396 - *
397 - * Default: array( 'REMOTE_ADDR' ) — the actual TCP peer, the only value
398 - * that an HTTP client cannot spoof. Trusting any other $_SERVER key is
399 - * only safe when a reverse proxy in front of the site sets that key and
400 - * overwrites any client-supplied version; otherwise an attacker can spoof
401 - * the value and bypass the per-IP rate limits that depend on it.
402 - *
403 - * Common operator overrides:
404 - * array( 'HTTP_CF_CONNECTING_IP' ) on Cloudflare.
405 - * array( 'HTTP_TRUE_CLIENT_IP', 'REMOTE_ADDR' ) Akamai with a fallback.
406 - * array( 'HTTP_X_REAL_IP' ) nginx that strips the client copy.
407 - *
408 - * X-Forwarded-For pitfall: even with a trusted proxy, an attacker can
409 - * prepend their own value before the proxy appends the real client IP.
410 - * This helper takes the leftmost entry, which is correct only when the
411 - * trusted proxy fully overwrites the header. If you trust X-Forwarded-For
412 - * end-to-end, prefer to resolve from the right by your known proxy count
413 - * via the activitypub_client_ip filter.
414 - *
415 - * @since 8.2.0
416 - *
417 - * @param string[] $sources $_SERVER keys to consult, in priority order.
418 - */
419 - $sources = \apply_filters( 'activitypub_client_ip_sources', array( 'REMOTE_ADDR' ) );
1176 + if ( ! $post ) {
1177 + return '';
1178 + }
420 1179
421 - if ( ! \is_array( $sources ) ) {
422 - $sources = array( 'REMOTE_ADDR' );
1180 + $content = \sanitize_post_field( 'post_excerpt', $post->post_excerpt, $post->ID );
1181 +
1182 + if ( $content ) {
1183 + return \apply_filters( 'the_excerpt', $content );
423 1184 }
424 1185
425 - foreach ( $sources as $source ) {
426 - if ( ! \is_string( $source ) || empty( $_SERVER[ $source ] ) ) {
427 - continue;
428 - }
1186 + $content = \sanitize_post_field( 'post_content', $post->post_content, $post->ID );
1187 + $content_parts = \get_extended( $content );
429 1188
430 - // Some headers (e.g. X-Forwarded-For) may contain a comma-separated list; use the first IP.
431 - $ip_list = \sanitize_text_field( \wp_unslash( $_SERVER[ $source ] ) );
432 - $candidate = \trim( \explode( ',', $ip_list )[0] );
1189 + $excerpt_more = \apply_filters( 'activitypub_excerpt_more', '[…]' );
1190 + $length = $length - strlen( $excerpt_more );
433 1191
434 - if ( \filter_var( $candidate, FILTER_VALIDATE_IP ) ) {
435 - $ip = $candidate;
436 - break;
437 - }
1192 + // Check for the <!--more--> tag.
1193 + if (
1194 + ! empty( $content_parts['extended'] ) &&
1195 + ! empty( $content_parts['main'] )
1196 + ) {
1197 + $content = $content_parts['main'] . ' ' . $excerpt_more;
1198 + $length = null;
438 1199 }
439 - // phpcs:enable WordPressVIPMinimum.Variables.ServerVariables.UserControlledHeaders
440 1200
441 - /**
442 - * Filter the client IP address used for rate limiting.
443 - *
444 - * @since 8.1.0
445 - *
446 - * @param string $ip The detected client IP address (empty when none could be determined).
447 - */
448 - $ip = \apply_filters( 'activitypub_client_ip', $ip );
1201 + $content = \html_entity_decode( $content );
1202 + $content = \wp_strip_all_tags( $content );
1203 + $content = \trim( $content );
1204 + $content = \preg_replace( '/\R+/m', "\n\n", $content );
1205 + $content = \preg_replace( '/[\r\t]/', '', $content );
449 1206
450 - // Tolerate surrounding whitespace from filter callbacks; FILTER_VALIDATE_IP would otherwise reject it.
451 - if ( \is_string( $ip ) ) {
452 - $ip = \trim( $ip );
1207 + if ( $length && \strlen( $content ) > $length ) {
1208 + $content = \wordwrap( $content, $length, '</activitypub-summary>' );
1209 + $content = \explode( '</activitypub-summary>', $content, 2 );
1210 + $content = $content[0] . ' ' . $excerpt_more;
453 1211 }
454 1212
455 - // Re-validate so a misbehaving filter can't return a sentinel string that would collapse all callers into one bucket.
456 - return \is_string( $ip ) && \filter_var( $ip, FILTER_VALIDATE_IP ) ? $ip : '';
1213 + /* Removed until this is merged: https://github.com/mastodon/mastodon/pull/28629
1214 + return \apply_filters( 'the_excerpt', $content );
1215 + */
1216 + return $content;
457 1217 }