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