PluginProbe
ActivityPub / 9.2.1
ActivityPub v9.2.1
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 +297 -669 2.1.19.2.1 View file →
@@ -1,829 +1,457 @@
1 1 <?php
2 -namespace Activitypub;
3 -
4 -use WP_Error;
5 -use Activitypub\Http;
6 -use Activitypub\Comment;
7 -use Activitypub\Webfinger;
8 -use Activitypub\Activity\Activity;
9 -use Activitypub\Collection\Followers;
10 -use Activitypub\Collection\Users;
11 -
12 2 /**
13 - * Returns the ActivityPub default JSON-context
3 + * Functions file.
14 4 *
15 - * @return array the activitypub context
16 - */
17 -function get_context() {
18 - $context = Activity::JSON_LD_CONTEXT;
19 -
20 - return \apply_filters( 'activitypub_json_context', $context );
21 -}
22 -
23 -function safe_remote_post( $url, $body, $user_id ) {
24 - return Http::post( $url, $body, $user_id );
25 -}
26 -
27 -function safe_remote_get( $url ) {
28 - return Http::get( $url );
29 -}
30 -
31 -/**
32 - * Returns a users WebFinger "resource"
5 + * General utility functions for the ActivityPub plugin.
33 6 *
34 - * @param int $user_id The User-ID.
35 - *
36 - * @return string The User-Resource.
7 + * @package Activitypub
37 8 */
38 -function get_webfinger_resource( $user_id ) {
39 - return Webfinger::get_user_resource( $user_id );
40 -}
41 9
42 -/**
43 - * Requests the Meta-Data from the Actors profile
44 - *
45 - * @param string $actor The Actor URL.
46 - * @param bool $cached If the result should be cached.
47 - *
48 - * @return array|WP_Error The Actor profile as array or WP_Error on failure.
49 - */
50 -function get_remote_metadata_by_actor( $actor, $cached = true ) {
51 - $pre = apply_filters( 'pre_get_remote_metadata_by_actor', false, $actor );
52 - if ( $pre ) {
53 - return $pre;
54 - }
55 - if ( preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $actor ) ) {
56 - $actor = Webfinger::resolve( $actor );
57 - }
10 +namespace Activitypub;
58 11
59 - if ( ! $actor ) {
60 - return new WP_Error( 'activitypub_no_valid_actor_identifier', \__( 'The "actor" identifier is not valid', 'activitypub' ), array( 'status' => 404, 'actor' => $actor ) );
61 - }
62 -
63 - if ( is_wp_error( $actor ) ) {
64 - return $actor;
65 - }
66 -
67 - $transient_key = 'activitypub_' . $actor;
68 -
69 - // only check the cache if needed.
70 - if ( $cached ) {
71 - $metadata = \get_transient( $transient_key );
72 -
73 - if ( $metadata ) {
74 - return $metadata;
75 - }
76 - }
77 -
78 - if ( ! \wp_http_validate_url( $actor ) ) {
79 - $metadata = new WP_Error( 'activitypub_no_valid_actor_url', \__( 'The "actor" is no valid URL', 'activitypub' ), array( 'status' => 400, 'actor' => $actor ) );
80 - return $metadata;
81 - }
82 -
83 - $response = Http::get( $actor );
84 -
85 - if ( \is_wp_error( $response ) ) {
86 - return $response;
87 - }
88 -
89 - $metadata = \wp_remote_retrieve_body( $response );
90 - $metadata = \json_decode( $metadata, true );
91 -
92 - if ( ! $metadata ) {
93 - $metadata = new WP_Error( 'activitypub_invalid_json', \__( 'No valid JSON data', 'activitypub' ), array( 'status' => 400, 'actor' => $actor ) );
94 - return $metadata;
95 - }
96 -
97 - \set_transient( $transient_key, $metadata, WEEK_IN_SECONDS );
98 -
99 - return $metadata;
100 -}
101 -
102 12 /**
103 - * Returns the followers of a given user.
13 + * Get the ActivityPub ID for a WordPress object.
104 14 *
105 - * @param int $user_id The User-ID.
15 + * Returns the canonical ActivityPub URI for a WP_Post or WP_Comment.
106 16 *
107 - * @return array The followers.
108 - */
109 -function get_followers( $user_id ) {
110 - return Followers::get_followers( $user_id );
111 -}
112 -
113 -/**
114 - * Count the number of followers for a given user.
17 + * @param \WP_Post|\WP_Comment $wp_object The WordPress post or comment.
115 18 *
116 - * @param int $user_id The User-ID.
117 - *
118 - * @return int The number of followers.
19 + * @return string|null The ActivityPub ID (a URL), or null if unsupported type.
119 20 */
120 -function count_followers( $user_id ) {
121 - return Followers::count_followers( $user_id );
122 -}
123 -
124 -/**
125 - * Examine a url and try to determine the author ID it represents.
126 - *
127 - * Checks are supposedly from the hosted site blog.
128 - *
129 - * @param string $url Permalink to check.
130 - *
131 - * @return int User ID, or 0 on failure.
132 - */
133 -function url_to_authorid( $url ) {
134 - global $wp_rewrite;
135 -
136 - // check if url hase the same host
137 - if ( \wp_parse_url( \site_url(), \PHP_URL_HOST ) !== \wp_parse_url( $url, \PHP_URL_HOST ) ) {
138 - return 0;
21 +function get_object_id( $wp_object ) {
22 + if ( $wp_object instanceof \WP_Post ) {
23 + return get_post_id( $wp_object->ID );
139 24 }
140 25
141 - // first, check to see if there is a 'author=N' to match against
142 - if ( \preg_match( '/[?&]author=(\d+)/i', $url, $values ) ) {
143 - $id = \absint( $values[1] );
144 - if ( $id ) {
145 - return $id;
146 - }
26 + if ( $wp_object instanceof \WP_Comment ) {
27 + return get_comment_id( $wp_object );
147 28 }
148 29
149 - // check to see if we are using rewrite rules
150 - $rewrite = $wp_rewrite->wp_rewrite_rules();
151 -
152 - // not using rewrite rules, and 'author=N' method failed, so we're out of options
153 - if ( empty( $rewrite ) ) {
154 - return 0;
155 - }
156 -
157 - // generate rewrite rule for the author url
158 - $author_rewrite = $wp_rewrite->get_author_permastruct();
159 - $author_regexp = \str_replace( '%author%', '', $author_rewrite );
160 -
161 - // match the rewrite rule with the passed url
162 - if ( \preg_match( '/https?:\/\/(.+)' . \preg_quote( $author_regexp, '/' ) . '([^\/]+)/i', $url, $match ) ) {
163 - $user = \get_user_by( 'slug', $match[2] );
164 - if ( $user ) {
165 - return $user->ID;
166 - }
167 - }
168 -
169 - return 0;
30 + return null;
170 31 }
171 32
172 33 /**
173 - * Verify if url is a wp_ap_comment,
174 - * Or if it is a previously received remote comment
175 - *
176 - * @return int comment_id
177 - */
178 -function is_comment() {
179 - $comment_id = get_query_var( 'c', null );
180 -
181 - if ( ! is_null( $comment_id ) ) {
182 - $comment = \get_comment( $comment_id );
183 -
184 - // Only return local origin comments
185 - if ( $comment && $comment->user_id ) {
186 - return $comment_id;
187 - }
188 - }
189 -
190 - return false;
191 -}
192 -
193 -/**
194 - * Check for Tombstone Objects
195 - *
196 - * @see https://www.w3.org/TR/activitypub/#delete-activity-outbox
197 - *
198 - * @param WP_Error $wp_error A WP_Error-Response of an HTTP-Request
199 - *
200 - * @return boolean true if HTTP-Code is 410 or 404
201 - */
202 -function is_tombstone( $wp_error ) {
203 - if ( ! is_wp_error( $wp_error ) ) {
204 - return false;
205 - }
206 -
207 - if ( in_array( (int) $wp_error->get_error_code(), array( 404, 410 ), true ) ) {
208 - return true;
209 - }
210 -
211 - return false;
212 -}
213 -
214 -/**
215 - * Get the REST URL relative to this plugin's namespace.
216 - *
217 - * @param string $path Optional. REST route path. Otherwise this plugin's namespaced root.
218 - *
219 - * @return string REST URL relative to this plugin's namespace.
220 - */
221 -function get_rest_url_by_path( $path = '' ) {
222 - // we'll handle the leading slash.
223 - $path = ltrim( $path, '/' );
224 - $namespaced_path = sprintf( '/%s/%s', ACTIVITYPUB_REST_NAMESPACE, $path );
225 - return \get_rest_url( null, $namespaced_path );
226 -}
227 -
228 -/**
229 34 * Convert a string from camelCase to snake_case.
230 35 *
231 - * @param string $string The string to convert.
36 + * @param string $input The string to convert.
232 37 *
233 38 * @return string The converted string.
234 39 */
235 -// phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.stringFound
236 -function camel_to_snake_case( $string ) {
237 - return strtolower( preg_replace( '/(?<!^)[A-Z]/', '_$0', $string ) );
40 +function camel_to_snake_case( $input ) {
41 + return \strtolower( \preg_replace( '/(?<!^)[A-Z]/', '_$0', $input ) );
238 42 }
239 43
240 44 /**
241 45 * Convert a string from snake_case to camelCase.
242 46 *
243 - * @param string $string The string to convert.
47 + * @param string $input The string to convert.
244 48 *
245 49 * @return string The converted string.
246 50 */
247 -// phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.stringFound
248 -function snake_to_camel_case( $string ) {
249 - return lcfirst( str_replace( '_', '', ucwords( $string, '_' ) ) );
51 +function snake_to_camel_case( $input ) {
52 + return \lcfirst( \str_replace( '_', '', \ucwords( $input, '_' ) ) );
250 53 }
251 54
252 55 /**
253 - * Escapes a Tag, to be used as a hashtag.
56 + * Convert seconds to ISO 8601 duration format.
254 57 *
255 - * @param string $string The string to escape.
58 + * @param int $seconds The duration in seconds.
256 59 *
257 - * @return string The escaped hastag.
60 + * @return string The duration in ISO 8601 format (e.g., "PT1H23M45S").
258 61 */
259 -function esc_hashtag( $string ) {
62 +function seconds_to_iso8601( $seconds ) {
63 + $seconds = (int) $seconds;
260 64
261 - $hashtag = \wp_specialchars_decode( $string, ENT_QUOTES );
262 - // Remove all characters that are not letters, numbers, or underscores.
263 - $hashtag = \preg_replace( '/emoji-regex(*SKIP)(?!)|[^\p{L}\p{Nd}_]+/u', '_', $hashtag );
264 -
265 - // Capitalize every letter that is preceded by an underscore.
266 - $hashtag = preg_replace_callback(
267 - '/_(.)/',
268 - function ( $matches ) {
269 - return '' . strtoupper( $matches[1] );
270 - },
271 - $hashtag
272 - );
273 -
274 - // Add a hashtag to the beginning of the string.
275 - $hashtag = ltrim( $hashtag, '#' );
276 - $hashtag = '#' . $hashtag;
277 -
278 - /**
279 - * Allow defining your own custom hashtag generation rules.
280 - *
281 - * @param string $hashtag The hashtag to be returned.
282 - * @param string $string The original string.
283 - */
284 - $hashtag = apply_filters( 'activitypub_esc_hashtag', $hashtag, $string );
285 -
286 - return esc_html( $hashtag );
287 -}
288 -
289 -/**
290 - * Check if a request is for an ActivityPub request.
291 - *
292 - * @return bool False by default.
293 - */
294 -function is_activitypub_request() {
295 - global $wp_query;
296 -
297 - /*
298 - * ActivityPub requests are currently only made for
299 - * author archives, singular posts, and the homepage.
300 - */
301 - if ( ! \is_author() && ! \is_singular() && ! \is_home() && ! defined( '\REST_REQUEST' ) ) {
302 - return false;
65 + if ( $seconds <= 0 ) {
66 + return 'PT0S';
303 67 }
304 68
305 - // Check if the current post type supports ActivityPub.
306 - if ( \is_singular() ) {
307 - $queried_object = \get_queried_object();
308 - $post_type = \get_post_type( $queried_object );
69 + $hours = \floor( $seconds / 3600 );
70 + $minutes = \floor( ( $seconds % 3600 ) / 60 );
71 + $secs = $seconds % 60;
309 72
310 - if ( ! \post_type_supports( $post_type, 'activitypub' ) ) {
311 - return false;
312 - }
313 - }
73 + $duration = 'PT';
314 74
315 - // Check if header already sent.
316 - if ( ! \headers_sent() && ACTIVITYPUB_SEND_VARY_HEADER ) {
317 - // Send Vary header for Accept header.
318 - \header( 'Vary: Accept' );
75 + if ( $hours > 0 ) {
76 + $duration .= $hours . 'H';
319 77 }
320 78
321 - // One can trigger an ActivityPub request by adding ?activitypub to the URL.
322 - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration
323 - global $wp_query;
324 - if ( isset( $wp_query->query_vars['activitypub'] ) ) {
325 - return true;
79 + if ( $minutes > 0 ) {
80 + $duration .= $minutes . 'M';
326 81 }
327 82
328 - /*
329 - * The other (more common) option to make an ActivityPub request
330 - * is to send an Accept header.
331 - */
332 - if ( isset( $_SERVER['HTTP_ACCEPT'] ) ) {
333 - $accept = sanitize_text_field( wp_unslash( $_SERVER['HTTP_ACCEPT'] ) );
334 -
335 - /*
336 - * $accept can be a single value, or a comma separated list of values.
337 - * We want to support both scenarios,
338 - * and return true when the header includes at least one of the following:
339 - * - application/activity+json
340 - * - application/ld+json
341 - * - application/json
342 - */
343 - if ( preg_match( '/(application\/(ld\+json|activity\+json|json))/i', $accept ) ) {
344 - return true;
345 - }
83 + if ( $secs > 0 || ( 0 === $hours && 0 === $minutes ) ) {
84 + $duration .= $secs . 'S';
346 85 }
347 86
348 - return false;
87 + return $duration;
349 88 }
350 89
351 90 /**
352 - * This function checks if a user is disabled for ActivityPub.
91 + * Check if a site supports the block editor.
353 92 *
354 - * @param int $user_id The User-ID.
355 - *
356 - * @return boolean True if the user is disabled, false otherwise.
93 + * @return boolean True if the site supports the block editor, false otherwise.
357 94 */
358 -function is_user_disabled( $user_id ) {
359 - $return = false;
360 -
361 - switch ( $user_id ) {
362 - // if the user is the application user, it's always enabled.
363 - case \Activitypub\Collection\Users::APPLICATION_USER_ID:
364 - $return = false;
365 - break;
366 - // if the user is the blog user, it's only enabled in single-user mode.
367 - case \Activitypub\Collection\Users::BLOG_USER_ID:
368 - if ( is_user_type_disabled( 'blog' ) ) {
369 - $return = true;
370 - break;
371 - }
372 -
373 - $return = false;
374 - break;
375 - // if the user is any other user, it's enabled if it can publish posts.
376 - default:
377 - if ( ! \get_user_by( 'id', $user_id ) ) {
378 - $return = true;
379 - break;
380 - }
381 -
382 - if ( is_user_type_disabled( 'user' ) ) {
383 - $return = true;
384 - break;
385 - }
386 -
387 - if ( ! \user_can( $user_id, 'publish_posts' ) ) {
388 - $return = true;
389 - break;
390 - }
391 -
392 - $return = false;
393 - break;
394 - }
395 -
396 - return apply_filters( 'activitypub_is_user_disabled', $return, $user_id );
95 +function site_supports_blocks() {
96 + /**
97 + * Allow plugins to disable block editor support,
98 + * thus disabling blocks registered by the ActivityPub plugin.
99 + *
100 + * @param boolean $supports_blocks True if the site supports the block editor, false otherwise.
101 + */
102 + return \apply_filters( 'activitypub_site_supports_blocks', true );
397 103 }
398 104
399 105 /**
400 - * Checks if a User-Type is disabled for ActivityPub.
106 + * Get the icon Image object for site-wide ActivityPub actors.
401 107 *
402 - * This function is used to check if the 'blog' or 'user'
403 - * type is disabled for ActivityPub.
108 + * Tries the site icon first, then the custom logo, and falls back to the
109 + * bundled WordPress logo.
404 110 *
405 - * @param enum $type Can be 'blog' or 'user'.
111 + * @since 9.1.0
406 112 *
407 - * @return boolean True if the user type is disabled, false otherwise.
113 + * @return array The icon array with 'type' and 'url'.
408 114 */
409 -function is_user_type_disabled( $type ) {
410 - switch ( $type ) {
411 - case 'blog':
412 - if ( \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) ) {
413 - if ( ACTIVITYPUB_SINGLE_USER_MODE ) {
414 - $return = false;
415 - break;
416 - }
417 - }
115 +function site_icon() {
116 + // Try site icon first.
117 + $icon_id = \get_option( 'site_icon' );
418 118
419 - if ( \defined( 'ACTIVITYPUB_DISABLE_BLOG_USER' ) ) {
420 - $return = ACTIVITYPUB_DISABLE_BLOG_USER;
421 - break;
422 - }
119 + // Try custom logo second.
120 + if ( ! $icon_id ) {
121 + $icon_id = \get_theme_mod( 'custom_logo' );
122 + }
423 123
424 - if ( '1' !== \get_option( 'activitypub_enable_blog_user', '0' ) ) {
425 - $return = true;
426 - break;
427 - }
124 + $icon_url = false;
428 125
429 - $return = false;
430 - break;
431 - case 'user':
432 - if ( \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) ) {
433 - if ( ACTIVITYPUB_SINGLE_USER_MODE ) {
434 - $return = true;
435 - break;
436 - }
437 - }
438 -
439 - if ( \defined( 'ACTIVITYPUB_DISABLE_USER' ) ) {
440 - $return = ACTIVITYPUB_DISABLE_USER;
441 - break;
442 - }
443 -
444 - if ( '1' !== \get_option( 'activitypub_enable_users', '1' ) ) {
445 - $return = true;
446 - break;
447 - }
448 -
449 - $return = false;
450 - break;
451 - default:
452 - $return = new WP_Error( 'activitypub_wrong_user_type', __( 'Wrong user type', 'activitypub' ), array( 'status' => 400 ) );
453 - break;
126 + if ( $icon_id ) {
127 + $icon = \wp_get_attachment_image_src( $icon_id, 'full' );
128 + if ( $icon ) {
129 + $icon_url = $icon[0];
130 + }
454 131 }
455 132
456 - return apply_filters( 'activitypub_is_user_type_disabled', $return, $type );
457 -}
458 -
459 -/**
460 - * Check if the blog is in single-user mode.
461 - *
462 - * @return boolean True if the blog is in single-user mode, false otherwise.
463 - */
464 -function is_single_user() {
465 - if (
466 - false === is_user_type_disabled( 'blog' ) &&
467 - true === is_user_type_disabled( 'user' )
468 - ) {
469 - return true;
133 + if ( ! $icon_url ) {
134 + // Fallback to default icon.
135 + $icon_url = \plugins_url( '/assets/img/wp-logo.png', ACTIVITYPUB_PLUGIN_FILE );
470 136 }
471 137
472 - return false;
138 + return array(
139 + 'type' => 'Image',
140 + 'url' => \esc_url_raw( $icon_url ),
141 + );
473 142 }
474 143
475 144 /**
476 - * Check if a site supports the block editor.
145 + * Check whether a blog is public based on the `blog_public` option.
477 146 *
478 - * @return boolean True if the site supports the block editor, false otherwise.
147 + * @return bool True if public, false if not
479 148 */
480 -function site_supports_blocks() {
481 - if ( \version_compare( \get_bloginfo( 'version' ), '5.9', '<' ) ) {
482 - return false;
483 - }
484 -
485 - if ( ! \function_exists( 'register_block_type_from_metadata' ) ) {
486 - return false;
487 - }
488 -
149 +function is_blog_public() {
489 150 /**
490 - * Allow plugins to disable block editor support,
491 - * thus disabling blocks registered by the ActivityPub plugin.
151 + * Filter whether the blog is public.
492 152 *
493 - * @param boolean $supports_blocks True if the site supports the block editor, false otherwise.
153 + * @param bool $public Whether the blog is public.
494 154 */
495 - return apply_filters( 'activitypub_site_supports_blocks', true );
155 + return (bool) \apply_filters( 'activitypub_is_blog_public', \get_option( 'blog_public', 1 ) );
496 156 }
497 157
498 158 /**
499 - * Check if data is valid JSON.
159 + * Get the masked WordPress version to only show the major and minor version.
500 160 *
501 - * @param string $data The data to check.
502 - *
503 - * @return boolean True if the data is JSON, false otherwise.
161 + * @return string The masked version.
504 162 */
505 -function is_json( $data ) {
506 - return \is_array( \json_decode( $data, true ) ) ? true : false;
163 +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 );
170 +
171 + return \implode( '.', $version );
507 172 }
508 173
509 174 /**
510 - * Check if a blog is public based on the `blog_public` option
175 + * Check if a plugin is active, loading plugin.php if necessary.
511 176 *
512 - * @return bollean True if public, false if not
513 - */
514 -function is_blog_public() {
515 - return (bool) apply_filters( 'activitypub_is_blog_public', \get_option( 'blog_public', 1 ) );
516 -}
517 -
518 -/**
519 - * Sanitize a URL
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.
520 180 *
521 - * @param string $value The URL to sanitize
181 + * @param string $plugin Plugin basename (e.g., 'plugin-folder/plugin-file.php').
522 182 *
523 - * @return string|null The sanitized URL or null if invalid
183 + * @return bool True if the plugin is active, false otherwise.
524 184 */
525 -function sanitize_url( $value ) {
526 - if ( filter_var( $value, FILTER_VALIDATE_URL ) === false ) {
527 - return null;
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';
528 189 }
529 190
530 - return esc_url_raw( $value );
191 + return \is_plugin_active( $plugin );
531 192 }
532 193
533 194 /**
534 - * Extract recipient URLs from Activity object
195 + * Returns the website hosts allowed to credit this blog.
535 196 *
536 - * @param array $data
537 - *
538 - * @return array The list of user URLs
197 + * @return array|null The attribution domains or null if not found.
539 198 */
540 -function extract_recipients_from_activity( $data ) {
541 - $recipient_items = array();
542 -
543 - foreach ( array( 'to', 'bto', 'cc', 'bcc', 'audience' ) as $i ) {
544 - if ( array_key_exists( $i, $data ) ) {
545 - if ( is_array( $data[ $i ] ) ) {
546 - $recipient = $data[ $i ];
547 - } else {
548 - $recipient = array( $data[ $i ] );
549 - }
550 - $recipient_items = array_merge( $recipient_items, $recipient );
551 - }
552 -
553 - if ( is_array( $data['object'] ) && array_key_exists( $i, $data['object'] ) ) {
554 - if ( is_array( $data['object'][ $i ] ) ) {
555 - $recipient = $data['object'][ $i ];
556 - } else {
557 - $recipient = array( $data['object'][ $i ] );
558 - }
559 - $recipient_items = array_merge( $recipient_items, $recipient );
560 - }
199 +function get_attribution_domains() {
200 + if ( '1' !== \get_option( 'activitypub_use_opengraph', '1' ) ) {
201 + return null;
561 202 }
562 203
563 - $recipients = array();
204 + $domains = \get_option( 'activitypub_attribution_domains', home_host() );
205 + $domains = \explode( PHP_EOL, $domains );
564 206
565 - // flatten array
566 - foreach ( $recipient_items as $recipient ) {
567 - if ( is_array( $recipient ) ) {
568 - // check if recipient is an object
569 - if ( array_key_exists( 'id', $recipient ) ) {
570 - $recipients[] = $recipient['id'];
571 - }
572 - } else {
573 - $recipients[] = $recipient;
574 - }
207 + if ( ! $domains ) {
208 + $domains = null;
575 209 }
576 210
577 - return array_unique( $recipients );
211 + return $domains;
578 212 }
579 213
580 214 /**
581 - * Check if passed Activity is Public
215 + * Change the display of large numbers on the site.
582 216 *
583 - * @param array $data The Activity object as array
217 + * @author Jeremy Herve
584 218 *
585 - * @return boolean True if public, false if not
586 - */
587 -function is_activity_public( $data ) {
588 - $recipients = extract_recipients_from_activity( $data );
589 -
590 - return in_array( 'https://www.w3.org/ns/activitystreams#Public', $recipients, true );
591 -}
592 -
593 -/**
594 - * Get active users based on a given duration
219 + * @see https://wordpress.org/support/topic/abbreviate-numbers-with-k/
595 220 *
596 - * @param int $duration The duration to check in month(s)
221 + * @param string $formatted Converted number in string format.
222 + * @param float $number The number to convert based on locale.
597 223 *
598 - * @return int The number of active users
224 + * @return string Converted number in string format.
599 225 */
600 -function get_active_users( $duration = 1 ) {
226 +function custom_large_numbers( $formatted, $number ) {
227 + global $wp_locale;
601 228
602 - $duration = intval( $duration );
603 - $transient_key = sprintf( 'monthly_active_users_%d', $duration );
604 - $count = get_transient( $transient_key );
229 + $decimals = 0;
230 + $decimal_point = '.';
231 + $thousands_sep = ',';
605 232
606 - if ( false === $count ) {
607 - global $wpdb;
608 - $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 )";
609 - $query = $wpdb->prepare( $query, $duration );
610 - $count = $wpdb->get_var( $query ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
611 -
612 - set_transient( $transient_key, $count, DAY_IN_SECONDS );
233 + if ( isset( $wp_locale ) ) {
234 + $decimals = (int) $wp_locale->number_format['decimal_point'];
235 + $decimal_point = $wp_locale->number_format['decimal_point'];
236 + $thousands_sep = $wp_locale->number_format['thousands_sep'];
613 237 }
614 238
615 - // if 0 authors where active
616 - if ( 0 === $count ) {
617 - return 0;
239 + if ( $number < 1000 ) { // Any number less than a Thousand.
240 + return \number_format( $number, $decimals, $decimal_point, $thousands_sep );
241 + } elseif ( $number < 1000000 ) { // Any number less than a million.
242 + return \number_format( $number / 1000, $decimals, $decimal_point, $thousands_sep ) . 'K';
243 + } elseif ( $number < 1000000000 ) { // Any number less than a billion.
244 + return \number_format( $number / 1000000, $decimals, $decimal_point, $thousands_sep ) . 'M';
245 + } else { // At least a billion.
246 + return \number_format( $number / 1000000000, $decimals, $decimal_point, $thousands_sep ) . 'B';
618 247 }
619 -
620 - // if single user mode
621 - if ( is_single_user() ) {
622 - return 1;
623 - }
624 -
625 - // if blog user is disabled
626 - if ( is_user_disabled( Users::BLOG_USER_ID ) ) {
627 - return $count;
628 - }
629 -
630 - // also count blog user
631 - return $count + 1;
632 248 }
633 249
634 250 /**
635 - * Get the total number of users
251 + * Escapes a Tag, to be used as a hashtag.
636 252 *
637 - * @return int The total number of users
253 + * @param string $input The string to escape.
254 + *
255 + * @return string The escaped hashtag.
638 256 */
639 -function get_total_users() {
640 - // if single user mode
641 - if ( is_single_user() ) {
642 - return 1;
643 - }
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 );
644 261
645 - $users = \get_users(
646 - array(
647 - 'capability__in' => array( 'publish_posts' ),
648 - )
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
649 269 );
650 270
651 - if ( is_array( $users ) ) {
652 - $users = count( $users );
653 - } else {
654 - $users = 1;
655 - }
271 + // Add a hashtag to the beginning of the string.
272 + $hashtag = \ltrim( $hashtag, '#' );
273 + $hashtag = \trim( $hashtag, '-' );
274 + $hashtag = '#' . $hashtag;
656 275
657 - // if blog user is disabled
658 - if ( is_user_disabled( Users::BLOG_USER_ID ) ) {
659 - return $users;
660 - }
276 + /**
277 + * Allow defining your own custom hashtag generation rules.
278 + *
279 + * @param string $hashtag The hashtag to be returned.
280 + * @param string $input The original string.
281 + */
282 + $hashtag = \apply_filters( 'activitypub_esc_hashtag', $hashtag, $input );
661 283
662 - return $users + 1;
284 + return \esc_html( $hashtag );
663 285 }
664 286
665 287 /**
666 - * Examine a comment ID and look up an existing comment it represents.
288 + * Replace content with links, mentions or hashtags by Regex callback and not affect protected tags.
667 289 *
668 - * @param string $id ActivityPub object ID (usually a URL) to check.
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.
669 293 *
670 - * @return int|boolean Comment ID, or false on failure.
294 + * @return string The content with links, mentions, hashtags, etc.
671 295 */
672 -function object_id_to_comment( $id ) {
673 - return Comment::object_id_to_comment( $id );
674 -}
296 +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 ) {
299 + return $content;
300 + }
301 + $tag_stack = array();
302 + $protected_tags = array(
303 + 'pre',
304 + 'code',
305 + 'textarea',
306 + 'style',
307 + 'a',
308 + );
309 + $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 ) ) {
313 + $content_with_links .= $chunk;
314 + continue;
315 + }
675 316
676 -/**
677 - * Verify if URL is a local comment,
678 - * Or if it is a previously received remote comment
679 - * (For threading comments locally)
680 - *
681 - * @param string $url The URL to check.
682 - *
683 - * @return int comment_ID or null if not found
684 - */
685 -function url_to_commentid( $url ) {
686 - return Comment::url_to_commentid( $url );
687 -}
317 + if ( \preg_match( '#^<(/)?([a-z-]+)\b[^>]*>$#i', $chunk, $m ) ) {
318 + $tag = \strtolower( $m[2] );
319 + if ( '/' === $m[1] ) {
320 + // Closing tag.
321 + $i = \array_search( $tag, $tag_stack, true );
322 + // We can only remove the tag from the stack if it is in the stack.
323 + if ( false !== $i ) {
324 + $tag_stack = \array_slice( $tag_stack, 0, $i );
325 + }
326 + } else {
327 + // Opening tag, add it to the stack.
328 + $tag_stack[] = $tag;
329 + }
688 330
689 -/**
690 - * Get the URI of an ActivityPub object
691 - *
692 - * @param array $object The ActivityPub object
693 - *
694 - * @return string The URI of the ActivityPub object
695 - */
696 -function object_to_uri( $object ) {
697 - // check if it is already simple
698 - if ( ! $object || is_string( $object ) ) {
699 - return $object;
700 - }
331 + // If we're in a protected tag, the tag_stack contains at least one protected tag string.
332 + // 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 );
701 334
702 - // check if it is a list, then take first item
703 - // this plugin does not support collections
704 - if ( array_is_list( $object ) ) {
705 - $object = $object[0];
706 - }
335 + // Never inspect tags.
336 + $content_with_links .= $chunk;
337 + continue;
338 + }
707 339
708 - // check if it is simplified now
709 - if ( is_string( $object ) ) {
710 - return $object;
711 - }
340 + if ( $in_protected_tag ) {
341 + // Don't inspect a chunk inside an inspected tag.
342 + $content_with_links .= $chunk;
343 + continue;
344 + }
712 345
713 - // return part of Object that makes most sense
714 - switch ( $object['type'] ) {
715 - case 'Link':
716 - $object = $object['href'];
717 - break;
718 - default:
719 - $object = $object['id'];
720 - break;
346 + // Only reachable when there is no protected tag in the stack.
347 + $content_with_links .= \preg_replace_callback( $regex, $regex_callback, $chunk );
721 348 }
722 349
723 - return $object;
350 + return $content_with_links;
724 351 }
725 352
726 353 /**
727 - * Check if a comment should be federated.
354 + * Get an ActivityPub embed HTML for a URL.
728 355 *
729 - * We consider a comment should be federated if it is authored by a user that is
730 - * not disabled for federation and if it is a reply directly to the post or to a
731 - * federated comment.
356 + * @param string $url The URL to get the embed for.
357 + * @param boolean $inline_css Whether to inline CSS. Default true.
732 358 *
733 - * @param mixed $comment Comment object or ID.
734 - *
735 - * @return boolean True if the comment should be federated, false otherwise.
359 + * @return string|false The embed HTML or false if not found.
736 360 */
737 -function should_comment_be_federated( $comment ) {
738 - return Comment::should_be_federated( $comment );
361 +function get_embed_html( $url, $inline_css = true ) {
362 + return Embed::get_html( $url, $inline_css );
739 363 }
740 364
741 365 /**
742 - * Check if a comment was federated.
366 + * Get the client IP address for rate-limiting purposes.
743 367 *
744 - * This function checks if a comment was federated via ActivityPub.
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.
745 376 *
746 - * @param mixed $comment Comment object or ID.
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.
747 380 *
748 - * @return boolean True if the comment was federated, false otherwise.
749 - */
750 -function was_comment_sent( $comment ) {
751 - return Comment::was_sent( $comment );
752 -}
753 -
754 -/**
755 - * Check if a comment is federated.
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.
756 384 *
757 - * We consider a comment federated if comment was received via ActivityPub.
385 + * @since 8.1.0
758 386 *
759 - * Use this function to check if it is comment that was received via ActivityPub.
760 - *
761 - * @param mixed $comment Comment object or ID.
762 - *
763 - * @return boolean True if the comment is federated, false otherwise.
387 + * @return string A valid IP address, or '' when no IP could be determined.
764 388 */
765 -function was_comment_received( $comment ) {
766 - return Comment::was_received( $comment );
767 -}
389 +function get_client_ip() {
390 + // phpcs:disable WordPressVIPMinimum.Variables.ServerVariables.UserControlledHeaders
391 + $ip = '';
768 392
769 -/**
770 - * Check if a comment is local only.
771 - *
772 - * This function checks if a comment is local only and was not sent or received via ActivityPub.
773 - *
774 - * @param mixed $comment Comment object or ID.
775 - *
776 - * @return boolean True if the comment is local only, false otherwise.
777 - */
778 -function is_local_comment( $comment ) {
779 - return Comment::is_local( $comment );
780 -}
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' ) );
781 420
782 -/**
783 - * Mark a WordPress object as federated.
784 - *
785 - * @param WP_Comment|WP_Post|mixed $wp_object
786 - * @return void
787 - */
788 -function set_wp_object_state( $wp_object, $state ) {
789 - $meta_key = 'activitypub_status';
421 + if ( ! \is_array( $sources ) ) {
422 + $sources = array( 'REMOTE_ADDR' );
423 + }
790 424
791 - if ( $wp_object instanceof \WP_Post ) {
792 - \update_post_meta( $wp_object->ID, $meta_key, $state );
793 - } elseif ( $wp_object instanceof \WP_Comment ) {
794 - \update_comment_meta( $wp_object->comment_ID, $meta_key, $state );
795 - } else {
796 - \apply_filters( 'activitypub_mark_wp_object_as_federated', $wp_object );
425 + foreach ( $sources as $source ) {
426 + if ( ! \is_string( $source ) || empty( $_SERVER[ $source ] ) ) {
427 + continue;
428 + }
429 +
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] );
433 +
434 + if ( \filter_var( $candidate, FILTER_VALIDATE_IP ) ) {
435 + $ip = $candidate;
436 + break;
437 + }
797 438 }
798 -}
439 + // phpcs:enable WordPressVIPMinimum.Variables.ServerVariables.UserControlledHeaders
799 440
800 -/**
801 - * Get the description of a post type.
802 - *
803 - * Set some default descriptions for the default post types.
804 - *
805 - * @param WP_Post_Type $post_type The post type object.
806 - *
807 - * @return string The description of the post type.
808 - */
809 -function get_post_type_description( $post_type ) {
810 - $description = '';
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 );
811 449
812 - switch ( $post_type->name ) {
813 - case 'post':
814 - $description = '';
815 - break;
816 - case 'page':
817 - $description = '';
818 - break;
819 - case 'attachment':
820 - $description = ' - ' . __( 'The attachments that you have uploaded to a post (images, videos, documents or other files).', 'activitypub' );
821 - break;
822 - default:
823 - if ( ! empty( $post_type->description ) ) {
824 - $description = ' - ' . $post_type->description;
825 - }
450 + // Tolerate surrounding whitespace from filter callbacks; FILTER_VALIDATE_IP would otherwise reject it.
451 + if ( \is_string( $ip ) ) {
452 + $ip = \trim( $ip );
826 453 }
827 454
828 - return apply_filters( 'activitypub_post_type_description', $description, $post_type->name, $post_type );
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 : '';
829 457 }