| @@ -1,21 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Extra Fields collection file. | |
| 4 | - * | |
| 5 | - * @package Activitypub | |
| 6 | - */ | |
| 7 | 2 | |
| 8 | 3 | namespace Activitypub\Collection; |
| 9 | 4 | |
| 10 | 5 | use Activitypub\Link; |
| 11 | -use Activitypub\Sanitize; | |
| 6 | +use WP_Query; | |
| 7 | +use Activitypub\Collection\Users; | |
| 12 | 8 | |
| 13 | -use function Activitypub\site_supports_blocks; | |
| 14 | - | |
| 15 | -/** | |
| 16 | - * Extra Fields collection. | |
| 17 | - */ | |
| 18 | 9 | class Extra_Fields { |
| 19 | 10 | |
| 20 | 11 | const USER_POST_TYPE = 'ap_extrafield'; |
| 21 | 12 | const BLOG_POST_TYPE = 'ap_extrafield_blog'; |
| @@ -27,11 +18,11 @@ | ||
| 27 | 18 | * |
| 28 | 19 | * @return \WP_Post[] The extra fields. |
| 29 | 20 | */ |
| 30 | 21 | public static function get_actor_fields( $user_id ) { |
| 31 | - $is_blog = self::is_blog( $user_id ); | |
| 22 | + $is_blog = self::is_blog( $user_id ); | |
| 32 | 23 | $post_type = $is_blog ? self::BLOG_POST_TYPE : self::USER_POST_TYPE; |
| 33 | - $args = array( | |
| 24 | + $args = array( | |
| 34 | 25 | 'post_type' => $post_type, |
| 35 | 26 | 'nopaging' => true, |
| 36 | 27 | 'orderby' => 'menu_order', |
| 37 | 28 | 'order' => 'ASC', |
| @@ -39,66 +30,17 @@ | ||
| 39 | 30 | if ( ! $is_blog ) { |
| 40 | 31 | $args['author'] = $user_id; |
| 41 | 32 | } |
| 42 | 33 | |
| 43 | - // Limit to 20 fields to prevent response size issues. | |
| 44 | - if ( ! is_admin() ) { | |
| 45 | - /** | |
| 46 | - * Filters the number of extra fields to retrieve for an ActivityPub actor. | |
| 47 | - * | |
| 48 | - * @param int $limit The number of extra fields to retrieve. Default 20. | |
| 49 | - */ | |
| 50 | - $args['posts_per_page'] = apply_filters( 'activitypub_actor_extra_fields_limit', 20 ); | |
| 51 | - $args['nopaging'] = false; | |
| 52 | - } | |
| 53 | - | |
| 54 | - $query = new \WP_Query( $args ); | |
| 34 | + $query = new \WP_Query( $args ); | |
| 55 | 35 | $fields = $query->posts ?? array(); |
| 56 | 36 | |
| 57 | - /** | |
| 58 | - * Filters the extra fields for an ActivityPub actor. | |
| 59 | - * | |
| 60 | - * This filter allows developers to modify or add custom fields to an actor's | |
| 61 | - * profile. | |
| 62 | - * | |
| 63 | - * @param \WP_Post[] $fields Array of WP_Post objects representing the extra fields. | |
| 64 | - * @param int $user_id The ID of the user whose fields are being retrieved. | |
| 65 | - */ | |
| 66 | 37 | return apply_filters( 'activitypub_get_actor_extra_fields', $fields, $user_id ); |
| 67 | 38 | } |
| 68 | 39 | |
| 69 | 40 | /** |
| 70 | - * Get formatted content for an extra field. | |
| 41 | + * Transforms the Extra Fields (Cutom Post Types) to ActivityPub Actor-Attachments. | |
| 71 | 42 | * |
| 72 | - * @param \WP_Post $post The post. | |
| 73 | - * | |
| 74 | - * @return string The formatted content. | |
| 75 | - */ | |
| 76 | - public static function get_formatted_content( $post ) { | |
| 77 | - $content = \get_the_content( null, false, $post ); | |
| 78 | - $content = Link::the_content( $content ); | |
| 79 | - if ( site_supports_blocks() ) { | |
| 80 | - $content = \do_blocks( $content ); | |
| 81 | - } | |
| 82 | - $content = \wptexturize( $content ); | |
| 83 | - $content = \wp_filter_content_tags( $content ); | |
| 84 | - | |
| 85 | - $content = \strip_shortcodes( $content ); | |
| 86 | - $content = Sanitize::clean_html( $content ); | |
| 87 | - $content = Sanitize::strip_whitespace( $content ); | |
| 88 | - | |
| 89 | - /** | |
| 90 | - * Filters the content of an extra field. | |
| 91 | - * | |
| 92 | - * @param string $content The content. | |
| 93 | - * @param \WP_Post $post The post. | |
| 94 | - */ | |
| 95 | - return \apply_filters( 'activitypub_extra_field_content', $content, $post ); | |
| 96 | - } | |
| 97 | - | |
| 98 | - /** | |
| 99 | - * Transforms the Extra Fields (Custom Post Types) to ActivityPub Actor-Attachments. | |
| 100 | - * | |
| 101 | 43 | * @param \WP_Post[] $fields The extra fields. |
| 102 | 44 | * |
| 103 | 45 | * @return array ActivityPub attachments. |
| 104 | 46 | */ |
| @@ -103,22 +45,41 @@ | ||
| 103 | 45 | * @return array ActivityPub attachments. |
| 104 | 46 | */ |
| 105 | 47 | public static function fields_to_attachments( $fields ) { |
| 106 | 48 | $attachments = array(); |
| 107 | - \add_filter( 'activitypub_link_rel', array( self::class, 'add_rel_me' ) ); | |
| 49 | + \add_filter( | |
| 50 | + 'activitypub_link_rel', | |
| 51 | + function( $rel ) { | |
| 52 | + $rel .= ' me'; | |
| 108 | 53 | |
| 54 | + return $rel; | |
| 55 | + } | |
| 56 | + ); | |
| 57 | + | |
| 109 | 58 | foreach ( $fields as $post ) { |
| 110 | - $title = \html_entity_decode( \get_the_title( $post ), \ENT_QUOTES, 'UTF-8' ); | |
| 111 | - $content = self::get_formatted_content( $post ); | |
| 59 | + $content = \get_the_content( null, false, $post ); | |
| 60 | + $content = \do_blocks( $content ); | |
| 61 | + $content = \wptexturize( $content ); | |
| 62 | + $content = \wp_filter_content_tags( $content ); | |
| 63 | + // replace script and style elements | |
| 64 | + $content = \preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content ); | |
| 65 | + $content = \strip_shortcodes( $content ); | |
| 66 | + $content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) ); | |
| 67 | + $content = \apply_filters( 'activitypub_extra_field_content', $content, $post ); | |
| 68 | + | |
| 112 | 69 | $attachments[] = array( |
| 113 | - 'type' => 'PropertyValue', | |
| 114 | - 'name' => $title, | |
| 115 | - 'value' => \html_entity_decode( $content, \ENT_QUOTES, 'UTF-8' ), | |
| 70 | + 'type' => 'PropertyValue', | |
| 71 | + 'name' => \get_the_title( $post ), | |
| 72 | + 'value' => \html_entity_decode( | |
| 73 | + $content, | |
| 74 | + \ENT_QUOTES, | |
| 75 | + 'UTF-8' | |
| 76 | + ), | |
| 116 | 77 | ); |
| 117 | 78 | |
| 118 | - $attachment = false; | |
| 79 | + $link_added = false; | |
| 119 | 80 | |
| 120 | - // Add support for FEP-fb2a, for more information see FEDERATION.md. | |
| 81 | + // Add support for FEP-fb2a, for more information see FEDERATION.md | |
| 121 | 82 | $link_content = \trim( \strip_tags( $content, '<a>' ) ); |
| 122 | 83 | if ( |
| 123 | 84 | \stripos( $link_content, '<a' ) === 0 && |
| 124 | 85 | \stripos( $link_content, '<a', 3 ) === false && |
| @@ -130,25 +91,26 @@ | ||
| 130 | 91 | |
| 131 | 92 | if ( 'A' === $tags->get_tag() ) { |
| 132 | 93 | $attachment = array( |
| 133 | 94 | 'type' => 'Link', |
| 134 | - 'name' => $title, | |
| 95 | + 'name' => \get_the_title( $post ), | |
| 135 | 96 | 'href' => \esc_url( $tags->get_attribute( 'href' ) ), |
| 97 | + 'rel' => explode( ' ', $tags->get_attribute( 'rel' ) ), | |
| 136 | 98 | ); |
| 137 | 99 | |
| 138 | - $rel = $tags->get_attribute( 'rel' ); | |
| 139 | - | |
| 140 | - if ( $rel && \is_string( $rel ) ) { | |
| 141 | - $attachment['rel'] = \explode( ' ', $rel ); | |
| 142 | - } | |
| 100 | + $link_added = true; | |
| 143 | 101 | } |
| 144 | 102 | } |
| 145 | 103 | |
| 146 | - if ( ! $attachment ) { | |
| 104 | + if ( ! $link_added ) { | |
| 147 | 105 | $attachment = array( |
| 148 | 106 | 'type' => 'Note', |
| 149 | - 'name' => $title, | |
| 150 | - 'content' => \html_entity_decode( $content, \ENT_QUOTES, 'UTF-8' ), | |
| 107 | + 'name' => \get_the_title( $post ), | |
| 108 | + 'content' => \html_entity_decode( | |
| 109 | + $content, | |
| 110 | + \ENT_QUOTES, | |
| 111 | + 'UTF-8' | |
| 112 | + ), | |
| 151 | 113 | ); |
| 152 | 114 | } |
| 153 | 115 | |
| 154 | 116 | $attachments[] = $attachment; |
| @@ -153,10 +115,8 @@ | ||
| 153 | 115 | |
| 154 | 116 | $attachments[] = $attachment; |
| 155 | 117 | } |
| 156 | 118 | |
| 157 | - \remove_filter( 'activitypub_link_rel', array( self::class, 'add_rel_me' ) ); | |
| 158 | - | |
| 159 | 119 | return $attachments; |
| 160 | 120 | } |
| 161 | 121 | |
| 162 | 122 | /** |
| @@ -205,9 +165,9 @@ | ||
| 205 | 165 | if ( ! empty( $extra_fields ) ) { |
| 206 | 166 | return $extra_fields; |
| 207 | 167 | } |
| 208 | 168 | |
| 209 | - $is_blog = self::is_blog( $user_id ); | |
| 169 | + $is_blog = self::is_blog( $user_id ); | |
| 210 | 170 | $already_migrated = $is_blog |
| 211 | 171 | ? \get_option( 'activitypub_default_extra_fields' ) |
| 212 | 172 | : \get_user_meta( $user_id, 'activitypub_default_extra_fields', true ); |
| 213 | 173 | |
| @@ -216,9 +176,9 @@ | ||
| 216 | 176 | } |
| 217 | 177 | |
| 218 | 178 | \add_filter( |
| 219 | 179 | 'activitypub_link_rel', |
| 220 | - static function ( $rel ) { | |
| 180 | + function( $rel ) { | |
| 221 | 181 | $rel .= ' me'; |
| 222 | 182 | |
| 223 | 183 | return $rel; |
| 224 | 184 | } |
| @@ -228,9 +188,9 @@ | ||
| 228 | 188 | \__( 'Blog', 'activitypub' ) => \home_url( '/' ), |
| 229 | 189 | ); |
| 230 | 190 | |
| 231 | 191 | if ( ! $is_blog ) { |
| 232 | - $author_url = \get_the_author_meta( 'user_url', $user_id ); | |
| 192 | + $author_url = \get_the_author_meta( 'user_url', $user_id ); | |
| 233 | 193 | $author_posts_url = \get_author_posts_url( $user_id ); |
| 234 | 194 | |
| 235 | 195 | $defaults[ \__( 'Profile', 'activitypub' ) ] = $author_posts_url; |
| 236 | 196 | if ( $author_url !== $author_posts_url ) { |
| @@ -250,14 +210,17 @@ | ||
| 250 | 210 | 'post_type' => $post_type, |
| 251 | 211 | 'post_title' => $title, |
| 252 | 212 | 'post_status' => 'publish', |
| 253 | 213 | 'post_author' => $user_id, |
| 254 | - 'post_content' => self::make_paragraph_block( Link::the_content( $url ) ), | |
| 214 | + 'post_content' => sprintf( | |
| 215 | + '<!-- wp:paragraph --><p>%s</p><!-- /wp:paragraph -->', | |
| 216 | + Link::the_content( $url ) | |
| 217 | + ), | |
| 255 | 218 | 'comment_status' => 'closed', |
| 256 | 219 | 'menu_order' => $menu_order, |
| 257 | 220 | ); |
| 258 | 221 | |
| 259 | - $menu_order += 10; | |
| 222 | + $menu_order += 10; | |
| 260 | 223 | $extra_field_id = wp_insert_post( $extra_field ); |
| 261 | 224 | $extra_fields[] = get_post( $extra_field_id ); |
| 262 | 225 | } |
| 263 | 226 | |
| @@ -268,37 +231,12 @@ | ||
| 268 | 231 | return $extra_fields; |
| 269 | 232 | } |
| 270 | 233 | |
| 271 | 234 | /** |
| 272 | - * Create a paragraph block. | |
| 273 | - * | |
| 274 | - * @param string $content The content. | |
| 275 | - * | |
| 276 | - * @return string The paragraph block. | |
| 277 | - */ | |
| 278 | - public static function make_paragraph_block( $content ) { | |
| 279 | - if ( ! site_supports_blocks() ) { | |
| 280 | - return $content; | |
| 281 | - } | |
| 282 | - return '<!-- wp:paragraph --><p>' . $content . '</p><!-- /wp:paragraph -->'; | |
| 283 | - } | |
| 284 | - | |
| 285 | - /** | |
| 286 | - * Add the 'me' rel to the link. | |
| 287 | - * | |
| 288 | - * @param string $rel The rel attribute. | |
| 289 | - * @return string The modified rel attribute. | |
| 290 | - */ | |
| 291 | - public static function add_rel_me( $rel ) { | |
| 292 | - return $rel . ' me'; | |
| 293 | - } | |
| 294 | - | |
| 295 | - /** | |
| 296 | 235 | * Checks if the user is the blog user. |
| 297 | - * | |
| 298 | 236 | * @param int $user_id The user ID. |
| 299 | 237 | * @return bool True if the user is the blog user, otherwise false. |
| 300 | 238 | */ |
| 301 | 239 | private static function is_blog( $user_id ) { |
| 302 | - return Actors::BLOG_USER_ID === $user_id; | |
| 240 | + return Users::BLOG_USER_ID === $user_id; | |
| 303 | 241 | } |
| 304 | 242 | } |