| @@ -1,250 +1,225 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Functions file. | |
| 4 | + * | |
| 5 | + * General utility functions for the ActivityPub plugin. | |
| 6 | + * | |
| 7 | + * @package Activitypub | |
| 8 | + */ | |
| 9 | + | |
| 2 | 10 | namespace Activitypub; |
| 3 | 11 | |
| 4 | -use WP_Error; | |
| 5 | -use Activitypub\Http; | |
| 6 | -use Activitypub\Activity\Activity; | |
| 7 | -use Activitypub\Collection\Followers; | |
| 8 | -use Activitypub\Collection\Users; | |
| 9 | - | |
| 10 | 12 | /** |
| 11 | - * Returns the ActivityPub default JSON-context | |
| 13 | + * Convert a string from camelCase to snake_case. | |
| 12 | 14 | * |
| 13 | - * @return array the activitypub context | |
| 15 | + * @param string $input The string to convert. | |
| 16 | + * | |
| 17 | + * @return string The converted string. | |
| 14 | 18 | */ |
| 15 | -function get_context() { | |
| 16 | - $context = Activity::CONTEXT; | |
| 17 | - | |
| 18 | - return \apply_filters( 'activitypub_json_context', $context ); | |
| 19 | +function camel_to_snake_case( $input ) { | |
| 20 | + return strtolower( preg_replace( '/(?<!^)[A-Z]/', '_$0', $input ) ); | |
| 19 | 21 | } |
| 20 | 22 | |
| 21 | -function safe_remote_post( $url, $body, $user_id ) { | |
| 22 | - return Http::post( $url, $body, $user_id ); | |
| 23 | -} | |
| 24 | - | |
| 25 | -function safe_remote_get( $url ) { | |
| 26 | - return Http::get( $url ); | |
| 27 | -} | |
| 28 | - | |
| 29 | 23 | /** |
| 30 | - * Returns a users WebFinger "resource" | |
| 24 | + * Convert a string from snake_case to camelCase. | |
| 31 | 25 | * |
| 32 | - * @param int $user_id The User-ID. | |
| 26 | + * @param string $input The string to convert. | |
| 33 | 27 | * |
| 34 | - * @return string The User-Resource. | |
| 28 | + * @return string The converted string. | |
| 35 | 29 | */ |
| 36 | -function get_webfinger_resource( $user_id ) { | |
| 37 | - return Webfinger::get_user_resource( $user_id ); | |
| 30 | +function snake_to_camel_case( $input ) { | |
| 31 | + return lcfirst( str_replace( '_', '', ucwords( $input, '_' ) ) ); | |
| 38 | 32 | } |
| 39 | 33 | |
| 40 | 34 | /** |
| 41 | - * Requests the Meta-Data from the Actors profile | |
| 35 | + * Convert seconds to ISO 8601 duration format. | |
| 42 | 36 | * |
| 43 | - * @param string $actor The Actor URL. | |
| 44 | - * @param bool $cached If the result should be cached. | |
| 37 | + * @param int $seconds The duration in seconds. | |
| 45 | 38 | * |
| 46 | - * @return array|WP_Error The Actor profile as array or WP_Error on failure. | |
| 39 | + * @return string The duration in ISO 8601 format (e.g., "PT1H23M45S"). | |
| 47 | 40 | */ |
| 48 | -function get_remote_metadata_by_actor( $actor, $cached = true ) { | |
| 49 | - $pre = apply_filters( 'pre_get_remote_metadata_by_actor', false, $actor ); | |
| 50 | - if ( $pre ) { | |
| 51 | - return $pre; | |
| 52 | - } | |
| 53 | - if ( preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $actor ) ) { | |
| 54 | - $actor = Webfinger::resolve( $actor ); | |
| 55 | - } | |
| 41 | +function seconds_to_iso8601( $seconds ) { | |
| 42 | + $seconds = (int) $seconds; | |
| 56 | 43 | |
| 57 | - if ( ! $actor ) { | |
| 58 | - return new WP_Error( 'activitypub_no_valid_actor_identifier', \__( 'The "actor" identifier is not valid', 'activitypub' ), array( 'status' => 404, 'actor' => $actor ) ); | |
| 44 | + if ( $seconds <= 0 ) { | |
| 45 | + return 'PT0S'; | |
| 59 | 46 | } |
| 60 | 47 | |
| 61 | - if ( is_wp_error( $actor ) ) { | |
| 62 | - return $actor; | |
| 63 | - } | |
| 48 | + $hours = floor( $seconds / 3600 ); | |
| 49 | + $minutes = floor( ( $seconds % 3600 ) / 60 ); | |
| 50 | + $secs = $seconds % 60; | |
| 64 | 51 | |
| 65 | - $transient_key = 'activitypub_' . $actor; | |
| 52 | + $duration = 'PT'; | |
| 66 | 53 | |
| 67 | - // only check the cache if needed. | |
| 68 | - if ( $cached ) { | |
| 69 | - $metadata = \get_transient( $transient_key ); | |
| 70 | - | |
| 71 | - if ( $metadata ) { | |
| 72 | - return $metadata; | |
| 73 | - } | |
| 54 | + if ( $hours > 0 ) { | |
| 55 | + $duration .= $hours . 'H'; | |
| 74 | 56 | } |
| 75 | 57 | |
| 76 | - if ( ! \wp_http_validate_url( $actor ) ) { | |
| 77 | - $metadata = new WP_Error( 'activitypub_no_valid_actor_url', \__( 'The "actor" is no valid URL', 'activitypub' ), array( 'status' => 400, 'actor' => $actor ) ); | |
| 78 | - return $metadata; | |
| 58 | + if ( $minutes > 0 ) { | |
| 59 | + $duration .= $minutes . 'M'; | |
| 79 | 60 | } |
| 80 | 61 | |
| 81 | - $response = Http::get( $actor ); | |
| 82 | - | |
| 83 | - if ( \is_wp_error( $response ) ) { | |
| 84 | - return $response; | |
| 62 | + if ( $secs > 0 || ( 0 === $hours && 0 === $minutes ) ) { | |
| 63 | + $duration .= $secs . 'S'; | |
| 85 | 64 | } |
| 86 | 65 | |
| 87 | - $metadata = \wp_remote_retrieve_body( $response ); | |
| 88 | - $metadata = \json_decode( $metadata, true ); | |
| 66 | + return $duration; | |
| 67 | +} | |
| 89 | 68 | |
| 90 | - if ( ! $metadata ) { | |
| 91 | - $metadata = new WP_Error( 'activitypub_invalid_json', \__( 'No valid JSON data', 'activitypub' ), array( 'status' => 400, 'actor' => $actor ) ); | |
| 92 | - return $metadata; | |
| 93 | - } | |
| 69 | +/** | |
| 70 | + * Check if a site supports the block editor. | |
| 71 | + * | |
| 72 | + * @return boolean True if the site supports the block editor, false otherwise. | |
| 73 | + */ | |
| 74 | +function site_supports_blocks() { | |
| 75 | + /** | |
| 76 | + * Allow plugins to disable block editor support, | |
| 77 | + * thus disabling blocks registered by the ActivityPub plugin. | |
| 78 | + * | |
| 79 | + * @param boolean $supports_blocks True if the site supports the block editor, false otherwise. | |
| 80 | + */ | |
| 81 | + return apply_filters( 'activitypub_site_supports_blocks', true ); | |
| 82 | +} | |
| 94 | 83 | |
| 95 | - \set_transient( $transient_key, $metadata, WEEK_IN_SECONDS ); | |
| 84 | +/** | |
| 85 | + * Check if data is valid JSON. | |
| 86 | + * | |
| 87 | + * @deprecated 7.1.0 Use {@see \json_decode}. | |
| 88 | + * | |
| 89 | + * @param string $data The data to check. | |
| 90 | + * | |
| 91 | + * @return boolean True if the data is JSON, false otherwise. | |
| 92 | + */ | |
| 93 | +function is_json( $data ) { | |
| 94 | + \_deprecated_function( __FUNCTION__, '7.1.0', 'json_decode' ); | |
| 96 | 95 | |
| 97 | - return $metadata; | |
| 96 | + return \is_array( \json_decode( $data, true ) ); | |
| 98 | 97 | } |
| 99 | 98 | |
| 100 | 99 | /** |
| 101 | - * Returns the followers of a given user. | |
| 100 | + * Check whether a blog is public based on the `blog_public` option. | |
| 102 | 101 | * |
| 103 | - * @param int $user_id The User-ID. | |
| 104 | - * | |
| 105 | - * @return array The followers. | |
| 102 | + * @return bool True if public, false if not | |
| 106 | 103 | */ |
| 107 | -function get_followers( $user_id ) { | |
| 108 | - return Followers::get_followers( $user_id ); | |
| 104 | +function is_blog_public() { | |
| 105 | + /** | |
| 106 | + * Filter whether the blog is public. | |
| 107 | + * | |
| 108 | + * @param bool $public Whether the blog is public. | |
| 109 | + */ | |
| 110 | + return (bool) apply_filters( 'activitypub_is_blog_public', \get_option( 'blog_public', 1 ) ); | |
| 109 | 111 | } |
| 110 | 112 | |
| 111 | 113 | /** |
| 112 | - * Count the number of followers for a given user. | |
| 114 | + * Get the masked WordPress version to only show the major and minor version. | |
| 113 | 115 | * |
| 114 | - * @param int $user_id The User-ID. | |
| 115 | - * | |
| 116 | - * @return int The number of followers. | |
| 116 | + * @return string The masked version. | |
| 117 | 117 | */ |
| 118 | -function count_followers( $user_id ) { | |
| 119 | - return Followers::count_followers( $user_id ); | |
| 118 | +function get_masked_wp_version() { | |
| 119 | + // Only show the major and minor version. | |
| 120 | + $version = get_bloginfo( 'version' ); | |
| 121 | + // Strip the RC or beta part. | |
| 122 | + $version = preg_replace( '/-.*$/', '', $version ); | |
| 123 | + $version = explode( '.', $version ); | |
| 124 | + $version = array_slice( $version, 0, 2 ); | |
| 125 | + | |
| 126 | + return implode( '.', $version ); | |
| 120 | 127 | } |
| 121 | 128 | |
| 122 | 129 | /** |
| 123 | - * Examine a url and try to determine the author ID it represents. | |
| 130 | + * Check if a plugin is active, loading plugin.php if necessary. | |
| 124 | 131 | * |
| 125 | - * Checks are supposedly from the hosted site blog. | |
| 132 | + * This is a wrapper around the core is_plugin_active() function that ensures | |
| 133 | + * the function is available by loading wp-admin/includes/plugin.php if needed. | |
| 134 | + * This is useful when checking plugin status outside of the admin context. | |
| 126 | 135 | * |
| 127 | - * @param string $url Permalink to check. | |
| 136 | + * @param string $plugin Plugin basename (e.g., 'plugin-folder/plugin-file.php'). | |
| 128 | 137 | * |
| 129 | - * @return int User ID, or 0 on failure. | |
| 138 | + * @return bool True if the plugin is active, false otherwise. | |
| 130 | 139 | */ |
| 131 | -function url_to_authorid( $url ) { | |
| 132 | - global $wp_rewrite; | |
| 133 | - | |
| 134 | - // check if url hase the same host | |
| 135 | - if ( \wp_parse_url( \site_url(), \PHP_URL_HOST ) !== \wp_parse_url( $url, \PHP_URL_HOST ) ) { | |
| 136 | - return 0; | |
| 140 | +function is_plugin_active( $plugin ) { | |
| 141 | + // Include plugin.php if not already loaded (needed for core is_plugin_active). | |
| 142 | + if ( ! \function_exists( 'is_plugin_active' ) ) { | |
| 143 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 137 | 144 | } |
| 138 | 145 | |
| 139 | - // first, check to see if there is a 'author=N' to match against | |
| 140 | - if ( \preg_match( '/[?&]author=(\d+)/i', $url, $values ) ) { | |
| 141 | - $id = \absint( $values[1] ); | |
| 142 | - if ( $id ) { | |
| 143 | - return $id; | |
| 144 | - } | |
| 145 | - } | |
| 146 | - | |
| 147 | - // check to see if we are using rewrite rules | |
| 148 | - $rewrite = $wp_rewrite->wp_rewrite_rules(); | |
| 149 | - | |
| 150 | - // not using rewrite rules, and 'author=N' method failed, so we're out of options | |
| 151 | - if ( empty( $rewrite ) ) { | |
| 152 | - return 0; | |
| 153 | - } | |
| 154 | - | |
| 155 | - // generate rewrite rule for the author url | |
| 156 | - $author_rewrite = $wp_rewrite->get_author_permastruct(); | |
| 157 | - $author_regexp = \str_replace( '%author%', '', $author_rewrite ); | |
| 158 | - | |
| 159 | - // match the rewrite rule with the passed url | |
| 160 | - if ( \preg_match( '/https?:\/\/(.+)' . \preg_quote( $author_regexp, '/' ) . '([^\/]+)/i', $url, $match ) ) { | |
| 161 | - $user = \get_user_by( 'slug', $match[2] ); | |
| 162 | - if ( $user ) { | |
| 163 | - return $user->ID; | |
| 164 | - } | |
| 165 | - } | |
| 166 | - | |
| 167 | - return 0; | |
| 146 | + return \is_plugin_active( $plugin ); | |
| 168 | 147 | } |
| 169 | 148 | |
| 170 | 149 | /** |
| 171 | - * Check for Tombstone Objects | |
| 150 | + * Returns the website hosts allowed to credit this blog. | |
| 172 | 151 | * |
| 173 | - * @see https://www.w3.org/TR/activitypub/#delete-activity-outbox | |
| 174 | - * | |
| 175 | - * @param WP_Error $wp_error A WP_Error-Response of an HTTP-Request | |
| 176 | - * | |
| 177 | - * @return boolean true if HTTP-Code is 410 or 404 | |
| 152 | + * @return array|null The attribution domains or null if not found. | |
| 178 | 153 | */ |
| 179 | -function is_tombstone( $wp_error ) { | |
| 180 | - if ( ! is_wp_error( $wp_error ) ) { | |
| 181 | - return false; | |
| 154 | +function get_attribution_domains() { | |
| 155 | + if ( '1' !== \get_option( 'activitypub_use_opengraph', '1' ) ) { | |
| 156 | + return null; | |
| 182 | 157 | } |
| 183 | 158 | |
| 184 | - if ( in_array( (int) $wp_error->get_error_code(), array( 404, 410 ), true ) ) { | |
| 185 | - return true; | |
| 159 | + $domains = \get_option( 'activitypub_attribution_domains', home_host() ); | |
| 160 | + $domains = explode( PHP_EOL, $domains ); | |
| 161 | + | |
| 162 | + if ( ! $domains ) { | |
| 163 | + $domains = null; | |
| 186 | 164 | } |
| 187 | 165 | |
| 188 | - return false; | |
| 166 | + return $domains; | |
| 189 | 167 | } |
| 190 | 168 | |
| 191 | 169 | /** |
| 192 | - * Get the REST URL relative to this plugin's namespace. | |
| 170 | + * Change the display of large numbers on the site. | |
| 193 | 171 | * |
| 194 | - * @param string $path Optional. REST route path. Otherwise this plugin's namespaced root. | |
| 172 | + * @author Jeremy Herve | |
| 195 | 173 | * |
| 196 | - * @return string REST URL relative to this plugin's namespace. | |
| 197 | - */ | |
| 198 | -function get_rest_url_by_path( $path = '' ) { | |
| 199 | - // we'll handle the leading slash. | |
| 200 | - $path = ltrim( $path, '/' ); | |
| 201 | - $namespaced_path = sprintf( '/%s/%s', ACTIVITYPUB_REST_NAMESPACE, $path ); | |
| 202 | - return \get_rest_url( null, $namespaced_path ); | |
| 203 | -} | |
| 204 | - | |
| 205 | -/** | |
| 206 | - * Convert a string from camelCase to snake_case. | |
| 174 | + * @see https://wordpress.org/support/topic/abbreviate-numbers-with-k/ | |
| 207 | 175 | * |
| 208 | - * @param string $string The string to convert. | |
| 176 | + * @param string $formatted Converted number in string format. | |
| 177 | + * @param float $number The number to convert based on locale. | |
| 209 | 178 | * |
| 210 | - * @return string The converted string. | |
| 179 | + * @return string Converted number in string format. | |
| 211 | 180 | */ |
| 212 | -// phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.stringFound | |
| 213 | -function camel_to_snake_case( $string ) { | |
| 214 | - return strtolower( preg_replace( '/(?<!^)[A-Z]/', '_$0', $string ) ); | |
| 215 | -} | |
| 181 | +function custom_large_numbers( $formatted, $number ) { | |
| 182 | + global $wp_locale; | |
| 216 | 183 | |
| 217 | -/** | |
| 218 | - * Convert a string from snake_case to camelCase. | |
| 219 | - * | |
| 220 | - * @param string $string The string to convert. | |
| 221 | - * | |
| 222 | - * @return string The converted string. | |
| 223 | - */ | |
| 224 | -// phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.stringFound | |
| 225 | -function snake_to_camel_case( $string ) { | |
| 226 | - return lcfirst( str_replace( '_', '', ucwords( $string, '_' ) ) ); | |
| 184 | + $decimals = 0; | |
| 185 | + $decimal_point = '.'; | |
| 186 | + $thousands_sep = ','; | |
| 187 | + | |
| 188 | + if ( isset( $wp_locale ) ) { | |
| 189 | + $decimals = (int) $wp_locale->number_format['decimal_point']; | |
| 190 | + $decimal_point = $wp_locale->number_format['decimal_point']; | |
| 191 | + $thousands_sep = $wp_locale->number_format['thousands_sep']; | |
| 192 | + } | |
| 193 | + | |
| 194 | + if ( $number < 1000 ) { // Any number less than a Thousand. | |
| 195 | + return \number_format( $number, $decimals, $decimal_point, $thousands_sep ); | |
| 196 | + } elseif ( $number < 1000000 ) { // Any number less than a million. | |
| 197 | + return \number_format( $number / 1000, $decimals, $decimal_point, $thousands_sep ) . 'K'; | |
| 198 | + } elseif ( $number < 1000000000 ) { // Any number less than a billion. | |
| 199 | + return \number_format( $number / 1000000, $decimals, $decimal_point, $thousands_sep ) . 'M'; | |
| 200 | + } else { // At least a billion. | |
| 201 | + return \number_format( $number / 1000000000, $decimals, $decimal_point, $thousands_sep ) . 'B'; | |
| 202 | + } | |
| 227 | 203 | } |
| 228 | 204 | |
| 229 | 205 | /** |
| 230 | 206 | * Escapes a Tag, to be used as a hashtag. |
| 231 | 207 | * |
| 232 | - * @param string $string The string to escape. | |
| 208 | + * @param string $input The string to escape. | |
| 233 | 209 | * |
| 234 | - * @return string The escaped hastag. | |
| 210 | + * @return string The escaped hashtag. | |
| 235 | 211 | */ |
| 236 | -function esc_hashtag( $string ) { | |
| 212 | +function esc_hashtag( $input ) { | |
| 213 | + $hashtag = \wp_specialchars_decode( $input, ENT_QUOTES ); | |
| 214 | + // Remove all characters that are not letters, numbers, or hyphens. | |
| 215 | + $hashtag = \preg_replace( '/[^\p{L}\p{Nd}-]+/u', '-', $hashtag ); | |
| 237 | 216 | |
| 238 | - $hashtag = \wp_specialchars_decode( $string, ENT_QUOTES ); | |
| 239 | - // Remove all characters that are not letters, numbers, or underscores. | |
| 240 | - $hashtag = \preg_replace( '/emoji-regex(*SKIP)(?!)|[^\p{L}\p{Nd}_]+/u', '_', $hashtag ); | |
| 241 | - | |
| 242 | - // Capitalize every letter that is preceded by an underscore. | |
| 217 | + // Capitalize every letter that is preceded by a hyphen. | |
| 243 | 218 | $hashtag = preg_replace_callback( |
| 244 | - '/_(.)/', | |
| 245 | - function ( $matches ) { | |
| 246 | - return '' . strtoupper( $matches[1] ); | |
| 219 | + '/-+(.)/', | |
| 220 | + static function ( $matches ) { | |
| 221 | + return strtoupper( $matches[1] ); | |
| 247 | 222 | }, |
| 248 | 223 | $hashtag |
| 249 | 224 | ); |
| 250 | 225 | |
| @@ -249,8 +224,9 @@ | ||
| 249 | 224 | ); |
| 250 | 225 | |
| 251 | 226 | // Add a hashtag to the beginning of the string. |
| 252 | 227 | $hashtag = ltrim( $hashtag, '#' ); |
| 228 | + $hashtag = trim( $hashtag, '-' ); | |
| 253 | 229 | $hashtag = '#' . $hashtag; |
| 254 | 230 | |
| 255 | 231 | /** |
| 256 | 232 | * Allow defining your own custom hashtag generation rules. |
| @@ -255,295 +231,88 @@ | ||
| 255 | 231 | /** |
| 256 | 232 | * Allow defining your own custom hashtag generation rules. |
| 257 | 233 | * |
| 258 | 234 | * @param string $hashtag The hashtag to be returned. |
| 259 | - * @param string $string The original string. | |
| 235 | + * @param string $input The original string. | |
| 260 | 236 | */ |
| 261 | - $hashtag = apply_filters( 'activitypub_esc_hashtag', $hashtag, $string ); | |
| 237 | + $hashtag = apply_filters( 'activitypub_esc_hashtag', $hashtag, $input ); | |
| 262 | 238 | |
| 263 | 239 | return esc_html( $hashtag ); |
| 264 | 240 | } |
| 265 | 241 | |
| 266 | 242 | /** |
| 267 | - * Check if a request is for an ActivityPub request. | |
| 243 | + * Replace content with links, mentions or hashtags by Regex callback and not affect protected tags. | |
| 268 | 244 | * |
| 269 | - * @return bool False by default. | |
| 245 | + * @param string $content The content that should be changed. | |
| 246 | + * @param string $regex The regex to use. | |
| 247 | + * @param callable $regex_callback Callback for replacement logic. | |
| 248 | + * | |
| 249 | + * @return string The content with links, mentions, hashtags, etc. | |
| 270 | 250 | */ |
| 271 | -function is_activitypub_request() { | |
| 272 | - global $wp_query; | |
| 273 | - | |
| 274 | - /* | |
| 275 | - * ActivityPub requests are currently only made for | |
| 276 | - * author archives, singular posts, and the homepage. | |
| 277 | - */ | |
| 278 | - if ( ! \is_author() && ! \is_singular() && ! \is_home() && ! defined( '\REST_REQUEST' ) ) { | |
| 279 | - return false; | |
| 251 | +function enrich_content_data( $content, $regex, $regex_callback ) { | |
| 252 | + // Small protection against execution timeouts: limit to 1 MB. | |
| 253 | + if ( mb_strlen( $content ) > MB_IN_BYTES ) { | |
| 254 | + return $content; | |
| 280 | 255 | } |
| 281 | - | |
| 282 | - // One can trigger an ActivityPub request by adding ?activitypub to the URL. | |
| 283 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration | |
| 284 | - global $wp_query; | |
| 285 | - if ( isset( $wp_query->query_vars['activitypub'] ) ) { | |
| 286 | - return true; | |
| 287 | - } | |
| 288 | - | |
| 289 | - /* | |
| 290 | - * The other (more common) option to make an ActivityPub request | |
| 291 | - * is to send an Accept header. | |
| 292 | - */ | |
| 293 | - if ( isset( $_SERVER['HTTP_ACCEPT'] ) ) { | |
| 294 | - $accept = sanitize_text_field( wp_unslash( $_SERVER['HTTP_ACCEPT'] ) ); | |
| 295 | - | |
| 296 | - /* | |
| 297 | - * $accept can be a single value, or a comma separated list of values. | |
| 298 | - * We want to support both scenarios, | |
| 299 | - * and return true when the header includes at least one of the following: | |
| 300 | - * - application/activity+json | |
| 301 | - * - application/ld+json | |
| 302 | - * - application/json | |
| 303 | - */ | |
| 304 | - if ( preg_match( '/(application\/(ld\+json|activity\+json|json))/i', $accept ) ) { | |
| 305 | - return true; | |
| 256 | + $tag_stack = array(); | |
| 257 | + $protected_tags = array( | |
| 258 | + 'pre', | |
| 259 | + 'code', | |
| 260 | + 'textarea', | |
| 261 | + 'style', | |
| 262 | + 'a', | |
| 263 | + ); | |
| 264 | + $content_with_links = ''; | |
| 265 | + $in_protected_tag = false; | |
| 266 | + foreach ( wp_html_split( $content ) as $chunk ) { | |
| 267 | + if ( preg_match( '#^<!--[\s\S]*-->$#i', $chunk, $m ) ) { | |
| 268 | + $content_with_links .= $chunk; | |
| 269 | + continue; | |
| 306 | 270 | } |
| 307 | - } | |
| 308 | 271 | |
| 309 | - return false; | |
| 310 | -} | |
| 311 | - | |
| 312 | -/** | |
| 313 | - * This function checks if a user is disabled for ActivityPub. | |
| 314 | - * | |
| 315 | - * @param int $user_id The User-ID. | |
| 316 | - * | |
| 317 | - * @return boolean True if the user is disabled, false otherwise. | |
| 318 | - */ | |
| 319 | -function is_user_disabled( $user_id ) { | |
| 320 | - $return = false; | |
| 321 | - | |
| 322 | - switch ( $user_id ) { | |
| 323 | - // if the user is the application user, it's always enabled. | |
| 324 | - case \Activitypub\Collection\Users::APPLICATION_USER_ID: | |
| 325 | - $return = false; | |
| 326 | - break; | |
| 327 | - // if the user is the blog user, it's only enabled in single-user mode. | |
| 328 | - case \Activitypub\Collection\Users::BLOG_USER_ID: | |
| 329 | - if ( is_user_type_disabled( 'blog' ) ) { | |
| 330 | - $return = true; | |
| 331 | - break; | |
| 332 | - } | |
| 333 | - | |
| 334 | - $return = false; | |
| 335 | - break; | |
| 336 | - // if the user is any other user, it's enabled if it can publish posts. | |
| 337 | - default: | |
| 338 | - if ( ! \get_user_by( 'id', $user_id ) ) { | |
| 339 | - $return = true; | |
| 340 | - break; | |
| 341 | - } | |
| 342 | - | |
| 343 | - if ( is_user_type_disabled( 'user' ) ) { | |
| 344 | - $return = true; | |
| 345 | - break; | |
| 346 | - } | |
| 347 | - | |
| 348 | - if ( ! \user_can( $user_id, 'publish_posts' ) ) { | |
| 349 | - $return = true; | |
| 350 | - break; | |
| 351 | - } | |
| 352 | - | |
| 353 | - $return = false; | |
| 354 | - break; | |
| 355 | - } | |
| 356 | - | |
| 357 | - return apply_filters( 'activitypub_is_user_disabled', $return, $user_id ); | |
| 358 | -} | |
| 359 | - | |
| 360 | -/** | |
| 361 | - * Checks if a User-Type is disabled for ActivityPub. | |
| 362 | - * | |
| 363 | - * This function is used to check if the 'blog' or 'user' | |
| 364 | - * type is disabled for ActivityPub. | |
| 365 | - * | |
| 366 | - * @param enum $type Can be 'blog' or 'user'. | |
| 367 | - * | |
| 368 | - * @return boolean True if the user type is disabled, false otherwise. | |
| 369 | - */ | |
| 370 | -function is_user_type_disabled( $type ) { | |
| 371 | - switch ( $type ) { | |
| 372 | - case 'blog': | |
| 373 | - if ( \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) ) { | |
| 374 | - if ( ACTIVITYPUB_SINGLE_USER_MODE ) { | |
| 375 | - $return = false; | |
| 376 | - break; | |
| 272 | + if ( preg_match( '#^<(/)?([a-z-]+)\b[^>]*>$#i', $chunk, $m ) ) { | |
| 273 | + $tag = strtolower( $m[2] ); | |
| 274 | + if ( '/' === $m[1] ) { | |
| 275 | + // Closing tag. | |
| 276 | + $i = array_search( $tag, $tag_stack, true ); | |
| 277 | + // We can only remove the tag from the stack if it is in the stack. | |
| 278 | + if ( false !== $i ) { | |
| 279 | + $tag_stack = array_slice( $tag_stack, 0, $i ); | |
| 377 | 280 | } |
| 281 | + } else { | |
| 282 | + // Opening tag, add it to the stack. | |
| 283 | + $tag_stack[] = $tag; | |
| 378 | 284 | } |
| 379 | 285 | |
| 380 | - if ( \defined( 'ACTIVITYPUB_DISABLE_BLOG_USER' ) ) { | |
| 381 | - $return = ACTIVITYPUB_DISABLE_BLOG_USER; | |
| 382 | - break; | |
| 383 | - } | |
| 286 | + // If we're in a protected tag, the tag_stack contains at least one protected tag string. | |
| 287 | + // The protected tag state can only change when we encounter a start or end tag. | |
| 288 | + $in_protected_tag = array_intersect( $tag_stack, $protected_tags ); | |
| 384 | 289 | |
| 385 | - if ( '1' !== \get_option( 'activitypub_enable_blog_user', '0' ) ) { | |
| 386 | - $return = true; | |
| 387 | - break; | |
| 388 | - } | |
| 290 | + // Never inspect tags. | |
| 291 | + $content_with_links .= $chunk; | |
| 292 | + continue; | |
| 293 | + } | |
| 389 | 294 | |
| 390 | - $return = false; | |
| 391 | - break; | |
| 392 | - case 'user': | |
| 393 | - if ( \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) ) { | |
| 394 | - if ( ACTIVITYPUB_SINGLE_USER_MODE ) { | |
| 395 | - $return = true; | |
| 396 | - break; | |
| 397 | - } | |
| 398 | - } | |
| 295 | + if ( $in_protected_tag ) { | |
| 296 | + // Don't inspect a chunk inside an inspected tag. | |
| 297 | + $content_with_links .= $chunk; | |
| 298 | + continue; | |
| 299 | + } | |
| 399 | 300 | |
| 400 | - if ( \defined( 'ACTIVITYPUB_DISABLE_USER' ) ) { | |
| 401 | - $return = ACTIVITYPUB_DISABLE_USER; | |
| 402 | - break; | |
| 403 | - } | |
| 404 | - | |
| 405 | - if ( '1' !== \get_option( 'activitypub_enable_users', '1' ) ) { | |
| 406 | - $return = true; | |
| 407 | - break; | |
| 408 | - } | |
| 409 | - | |
| 410 | - $return = false; | |
| 411 | - break; | |
| 412 | - default: | |
| 413 | - $return = new WP_Error( 'activitypub_wrong_user_type', __( 'Wrong user type', 'activitypub' ), array( 'status' => 400 ) ); | |
| 414 | - break; | |
| 301 | + // Only reachable when there is no protected tag in the stack. | |
| 302 | + $content_with_links .= \preg_replace_callback( $regex, $regex_callback, $chunk ); | |
| 415 | 303 | } |
| 416 | 304 | |
| 417 | - return apply_filters( 'activitypub_is_user_type_disabled', $return, $type ); | |
| 305 | + return $content_with_links; | |
| 418 | 306 | } |
| 419 | 307 | |
| 420 | 308 | /** |
| 421 | - * Check if the blog is in single-user mode. | |
| 309 | + * Get an ActivityPub embed HTML for a URL. | |
| 422 | 310 | * |
| 423 | - * @return boolean True if the blog is in single-user mode, false otherwise. | |
| 424 | - */ | |
| 425 | -function is_single_user() { | |
| 426 | - if ( | |
| 427 | - false === is_user_type_disabled( 'blog' ) && | |
| 428 | - true === is_user_type_disabled( 'user' ) | |
| 429 | - ) { | |
| 430 | - return true; | |
| 431 | - } | |
| 432 | - | |
| 433 | - return false; | |
| 434 | -} | |
| 435 | - | |
| 436 | -/** | |
| 437 | - * Check if a site supports the block editor. | |
| 311 | + * @param string $url The URL to get the embed for. | |
| 312 | + * @param boolean $inline_css Whether to inline CSS. Default true. | |
| 438 | 313 | * |
| 439 | - * @return boolean True if the site supports the block editor, false otherwise. | |
| 314 | + * @return string|false The embed HTML or false if not found. | |
| 440 | 315 | */ |
| 441 | -function site_supports_blocks() { | |
| 442 | - if ( \version_compare( \get_bloginfo( 'version' ), '5.9', '<' ) ) { | |
| 443 | - return false; | |
| 444 | - } | |
| 445 | - | |
| 446 | - if ( ! \function_exists( 'register_block_type_from_metadata' ) ) { | |
| 447 | - return false; | |
| 448 | - } | |
| 449 | - | |
| 450 | - /** | |
| 451 | - * Allow plugins to disable block editor support, | |
| 452 | - * thus disabling blocks registered by the ActivityPub plugin. | |
| 453 | - * | |
| 454 | - * @param boolean $supports_blocks True if the site supports the block editor, false otherwise. | |
| 455 | - */ | |
| 456 | - return apply_filters( 'activitypub_site_supports_blocks', true ); | |
| 457 | -} | |
| 458 | - | |
| 459 | -/** | |
| 460 | - * Check if data is valid JSON. | |
| 461 | - * | |
| 462 | - * @param string $data The data to check. | |
| 463 | - * | |
| 464 | - * @return boolean True if the data is JSON, false otherwise. | |
| 465 | - */ | |
| 466 | -function is_json( $data ) { | |
| 467 | - return \is_array( \json_decode( $data, true ) ) ? true : false; | |
| 468 | -} | |
| 469 | - | |
| 470 | -/** | |
| 471 | - * Check if a blog is public based on the `blog_public` option | |
| 472 | - * | |
| 473 | - * @return bollean True if public, false if not | |
| 474 | - */ | |
| 475 | -function is_blog_public() { | |
| 476 | - return (bool) apply_filters( 'activitypub_is_blog_public', \get_option( 'blog_public', 1 ) ); | |
| 477 | -} | |
| 478 | - | |
| 479 | -/** | |
| 480 | - * Get active users based on a given duration | |
| 481 | - * | |
| 482 | - * @param int $duration The duration to check in month(s) | |
| 483 | - * | |
| 484 | - * @return int The number of active users | |
| 485 | - */ | |
| 486 | -function get_active_users( $duration = 1 ) { | |
| 487 | - | |
| 488 | - $duration = intval( $duration ); | |
| 489 | - $transient_key = sprintf( 'monthly_active_users_%d', $duration ); | |
| 490 | - $count = get_transient( $transient_key ); | |
| 491 | - | |
| 492 | - if ( false === $count ) { | |
| 493 | - global $wpdb; | |
| 494 | - $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 )"; | |
| 495 | - $query = $wpdb->prepare( $query, $duration ); | |
| 496 | - $count = $wpdb->get_var( $query ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery | |
| 497 | - | |
| 498 | - set_transient( $transient_key, $count, DAY_IN_SECONDS ); | |
| 499 | - } | |
| 500 | - | |
| 501 | - // if 0 authors where active | |
| 502 | - if ( 0 === $count ) { | |
| 503 | - return 0; | |
| 504 | - } | |
| 505 | - | |
| 506 | - // if single user mode | |
| 507 | - if ( is_single_user() ) { | |
| 508 | - return 1; | |
| 509 | - } | |
| 510 | - | |
| 511 | - // if blog user is disabled | |
| 512 | - if ( is_user_disabled( Users::BLOG_USER_ID ) ) { | |
| 513 | - return $count; | |
| 514 | - } | |
| 515 | - | |
| 516 | - // also count blog user | |
| 517 | - return $count + 1; | |
| 518 | -} | |
| 519 | - | |
| 520 | -/** | |
| 521 | - * Get the total number of users | |
| 522 | - * | |
| 523 | - * @return int The total number of users | |
| 524 | - */ | |
| 525 | -function get_total_users() { | |
| 526 | - // if single user mode | |
| 527 | - if ( is_single_user() ) { | |
| 528 | - return 1; | |
| 529 | - } | |
| 530 | - | |
| 531 | - $users = \get_users( | |
| 532 | - array( | |
| 533 | - 'capability__in' => array( 'publish_posts' ), | |
| 534 | - ) | |
| 535 | - ); | |
| 536 | - | |
| 537 | - if ( is_array( $users ) ) { | |
| 538 | - $users = count( $users ); | |
| 539 | - } else { | |
| 540 | - $users = 1; | |
| 541 | - } | |
| 542 | - | |
| 543 | - // if blog user is disabled | |
| 544 | - if ( is_user_disabled( Users::BLOG_USER_ID ) ) { | |
| 545 | - return $users; | |
| 546 | - } | |
| 547 | - | |
| 548 | - return $users + 1; | |
| 316 | +function get_embed_html( $url, $inline_css = true ) { | |
| 317 | + return Embed::get_html( $url, $inline_css ); | |
| 549 | 318 | } |