From 39b311d6428ae7865007a72ed98af74359339824 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Thu, 16 May 2024 15:12:15 +0200 Subject: [PATCH 01/22] add extra fields settings --- includes/class-activitypub.php | 44 ++++++++++++++++++++++++++++++++++ includes/model/class-user.php | 34 ++++++++++++++++++++++---- templates/user-settings.php | 12 ++++++++++ 3 files changed, 85 insertions(+), 5 deletions(-) diff --git a/includes/class-activitypub.php b/includes/class-activitypub.php index bc2e24b7c..767723385 100644 --- a/includes/class-activitypub.php +++ b/includes/class-activitypub.php @@ -44,6 +44,8 @@ public static function init() { \add_action( 'in_plugin_update_message-' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'plugin_update_message' ) ); + \add_action( 'pre_get_posts', array( self::class, 'filter_get_posts_query' ) ); + // register several post_types self::register_post_types(); } @@ -463,6 +465,35 @@ private static function register_post_types() { ) ); + \register_post_type( + 'ap_extrafield', + array( + 'labels' => array( + 'name' => _x( 'Extra fields', 'post_type plural name', 'activitypub' ), + 'singular_name' => _x( 'Extra field', 'post_type single name', 'activitypub' ), + 'add_new' => __( 'Add new', 'activitypub' ), + 'add_new_item' => __( 'Add new extra field', 'activitypub' ), + 'new_item' => __( 'New extra field', 'activitypub' ), + 'edit_item' => __( 'Edit extra field', 'activitypub' ), + 'view_item' => __( 'View extra field', 'activitypub' ), + 'all_items' => __( 'All extra fields', 'activitypub' ), + ), + 'public' => false, + 'hierarchical' => false, + 'query_var' => false, + 'has_archive' => false, + 'publicly_queryable' => false, + 'show_in_menu' => false, + 'delete_with_user' => true, + 'can_export' => true, + 'exclude_from_search' => true, + 'show_in_rest' => true, + 'map_meta_cap' => true, + 'show_ui' => true, + 'supports' => array( 'title', 'editor', 'author' ), + ) + ); + \do_action( 'activitypub_after_register_post_type' ); } @@ -478,4 +509,17 @@ public static function user_register( $user_id ) { $user->add_cap( 'activitypub' ); } } + + /** + * Filter the query to only show the current author's extra fields + * + * @param WP_Query $query The WP_Query instance (passed by reference). + * + * @return void + */ + public static function filter_get_posts_query( $query ) { + if ( $query->get( 'post_type' ) === 'ap_extrafield' ) { + $query->set( 'author', get_current_user_id() ); + } + } } diff --git a/includes/model/class-user.php b/includes/model/class-user.php index 5f2e3b2ec..4ac2ab16f 100644 --- a/includes/model/class-user.php +++ b/includes/model/class-user.php @@ -250,9 +250,33 @@ public function get_endpoints() { * @return array The extended User-Output. */ public function get_attachment() { - $array = array(); + $extra_fields = new WP_Query( + array( + 'post_type' => 'ap_extrafield', + 'posts_per_page' => -1, + 'author' => $this->get__id(), + ) + ); + + if ( $extra_fields->have_posts() ) { + $attachments = array(); + foreach ( $extra_fields->posts as $post ) { + $attachments[] = array( + 'type' => 'PropertyValue', + 'name' => \get_the_title( $post ), + 'value' => \html_entity_decode( + \get_the_content( null, false, $post ), + \ENT_QUOTES, + 'UTF-8' + ), + ); + } + return $attachments; + } + + $extra_fields = array(); - $array[] = array( + $extra_fields[] = array( 'type' => 'PropertyValue', 'name' => \__( 'Blog', 'activitypub' ), 'value' => \html_entity_decode( @@ -262,7 +286,7 @@ public function get_attachment() { ), ); - $array[] = array( + $extra_fields[] = array( 'type' => 'PropertyValue', 'name' => \__( 'Profile', 'activitypub' ), 'value' => \html_entity_decode( @@ -273,7 +297,7 @@ public function get_attachment() { ); if ( \get_the_author_meta( 'user_url', $this->get__id() ) ) { - $array[] = array( + $extra_fields[] = array( 'type' => 'PropertyValue', 'name' => \__( 'Website', 'activitypub' ), 'value' => \html_entity_decode( @@ -284,7 +308,7 @@ public function get_attachment() { ); } - return $array; + return $extra_fields; } /** diff --git a/templates/user-settings.php b/templates/user-settings.php index 0d7b3f6eb..3b2375783 100644 --- a/templates/user-settings.php +++ b/templates/user-settings.php @@ -28,5 +28,17 @@ + + + + + +

+ get_attachment() as $attachment ) { ?> + + +

+ + From d3228ca457b04b48b90febda97b75ee3bb524de0 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 17 May 2024 14:41:34 +0200 Subject: [PATCH 02/22] add settings --- includes/class-scheduler.php | 10 +++++++++- includes/model/class-user.php | 13 ++++++++++++- templates/user-settings.php | 17 ++++++++++++++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/includes/class-scheduler.php b/includes/class-scheduler.php index 1f761c757..c03d4e7bc 100644 --- a/includes/class-scheduler.php +++ b/includes/class-scheduler.php @@ -117,6 +117,11 @@ public static function deregister_schedules() { public static function schedule_post_activity( $new_status, $old_status, $post ) { $post = get_post( $post ); + if ( 'ap_extrafield' === $post->post_type ) { + self::schedule_profile_update( $post->post_author ); + return; + } + // Do not send activities if post is password protected. if ( \post_password_required( $post ) ) { return; @@ -311,7 +316,9 @@ public static function user_update( $user_id ) { /** * Theme mods only have a dynamic filter so we fudge it like this. - * @param mixed $value + * + * @param mixed $value + * * @return mixed */ public static function blog_user_update( $value = null ) { @@ -321,6 +328,7 @@ public static function blog_user_update( $value = null ) { /** * Send a profile update to all followers. Gets hooked into all relevant options/meta etc. + * * @param int $user_id The user ID to update (Could be 0 for Blog-User). */ public static function schedule_profile_update( $user_id ) { diff --git a/includes/model/class-user.php b/includes/model/class-user.php index 4ac2ab16f..963b788a7 100644 --- a/includes/model/class-user.php +++ b/includes/model/class-user.php @@ -254,6 +254,7 @@ public function get_attachment() { array( 'post_type' => 'ap_extrafield', 'posts_per_page' => -1, + 'status' => 'publish', 'author' => $this->get__id(), ) ); @@ -261,11 +262,21 @@ public function get_attachment() { if ( $extra_fields->have_posts() ) { $attachments = array(); foreach ( $extra_fields->posts as $post ) { + $content = \get_the_content( null, false, $post ); + $content = \make_clickable( $content ); + $content = \do_blocks( $content ); + $content = \wptexturize( $content ); + $content = \wp_filter_content_tags( $content ); + // replace script and style elements + $content = \preg_replace( '@<(script|style)[^>]*?>.*?@si', '', $content ); + $content = \strip_shortcodes( $content ); + $content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) ); + $attachments[] = array( 'type' => 'PropertyValue', 'name' => \get_the_title( $post ), 'value' => \html_entity_decode( - \get_the_content( null, false, $post ), + $content, \ENT_QUOTES, 'UTF-8' ), diff --git a/templates/user-settings.php b/templates/user-settings.php index 3b2375783..6d464af59 100644 --- a/templates/user-settings.php +++ b/templates/user-settings.php @@ -34,8 +34,23 @@

- get_attachment() as $attachment ) { ?> + 'ap_extrafield', + 'posts_per_page' => -1, + 'status' => 'publish', + 'author' => get_current_user_id(), + ) + ); + foreach ( $extra_fields->posts as $extra_field ) { + ?> +

+ + + +

From 2af121c310dff0eadabb2ad97c5ef0c0d214be60 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 17 May 2024 15:06:23 +0200 Subject: [PATCH 03/22] add settings link --- templates/user-settings.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/templates/user-settings.php b/templates/user-settings.php index 6d464af59..12f7a2e61 100644 --- a/templates/user-settings.php +++ b/templates/user-settings.php @@ -52,7 +52,10 @@

-

+

+ + +

From 6dafa000990cc97529d811dcae7980f42903691f Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 17 May 2024 16:55:14 +0200 Subject: [PATCH 04/22] allow users to only edit their extra fields --- includes/class-activitypub.php | 1 + includes/class-admin.php | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/includes/class-activitypub.php b/includes/class-activitypub.php index 767723385..61daae5d5 100644 --- a/includes/class-activitypub.php +++ b/includes/class-activitypub.php @@ -501,6 +501,7 @@ private static function register_post_types() { * Add the 'activitypub' query variable so WordPress won't mangle it. * * @param int $user_id User ID. + * * @param array $userdata The raw array of data passed to wp_insert_user(). */ public static function user_register( $user_id ) { diff --git a/includes/class-admin.php b/includes/class-admin.php index 5df037d4c..1ac53ee89 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -21,6 +21,7 @@ public static function init() { \add_action( 'admin_menu', array( self::class, 'admin_menu' ) ); \add_action( 'admin_init', array( self::class, 'register_settings' ) ); \add_action( 'load-comment.php', array( self::class, 'edit_comment' ) ); + \add_action( 'load-post.php', array( self::class, 'edit_post' ) ); \add_action( 'personal_options_update', array( self::class, 'save_user_description' ) ); \add_action( 'admin_enqueue_scripts', array( self::class, 'enqueue_scripts' ) ); \add_action( 'admin_notices', array( self::class, 'admin_notices' ) ); @@ -345,6 +346,32 @@ function ( $allcaps, $caps, $arg ) { ); } + public static function edit_post() { + // Disable the edit_post capability for federated posts. + \add_filter( + 'user_has_cap', + function ( $allcaps, $caps, $arg ) { + if ( 'edit_post' !== $arg[0] ) { + return $allcaps; + } + + $post = get_post( $arg[2] ); + + if ( 'ap_extrafield' !== $post->post_type ) { + return $allcaps; + } + + if ( (int) get_current_user_id() !== (int) $post->post_author ) { + return false; + } + + return $allcaps; + }, + 1, + 3 + ); + } + public static function comment_row_actions( $actions, $comment ) { if ( was_comment_received( $comment ) ) { unset( $actions['edit'] ); From 57ea0dfa7b123a41e59a0bdc5f7d5e79d98e62d4 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Thu, 30 May 2024 09:31:05 +0200 Subject: [PATCH 05/22] use nopaging instead of posts_per_page thanks @mattwiebe --- templates/user-settings.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/templates/user-settings.php b/templates/user-settings.php index 12f7a2e61..5bd8e57a0 100644 --- a/templates/user-settings.php +++ b/templates/user-settings.php @@ -37,10 +37,10 @@ 'ap_extrafield', - 'posts_per_page' => -1, - 'status' => 'publish', - 'author' => get_current_user_id(), + 'post_type' => 'ap_extrafield', + 'nopaging' => true, + 'status' => 'publish', + 'author' => get_current_user_id(), ) ); From b6cb15502f6df0495157f6d36d394c0899f5ed17 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 31 May 2024 09:30:36 +0200 Subject: [PATCH 06/22] load filters only on admin page and remove the view actions --- includes/class-activitypub.php | 17 +---------------- includes/class-admin.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/includes/class-activitypub.php b/includes/class-activitypub.php index 61daae5d5..c84a5a40c 100644 --- a/includes/class-activitypub.php +++ b/includes/class-activitypub.php @@ -44,8 +44,6 @@ public static function init() { \add_action( 'in_plugin_update_message-' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'plugin_update_message' ) ); - \add_action( 'pre_get_posts', array( self::class, 'filter_get_posts_query' ) ); - // register several post_types self::register_post_types(); } @@ -498,7 +496,7 @@ private static function register_post_types() { } /** - * Add the 'activitypub' query variable so WordPress won't mangle it. + * Add the 'activitypub' capability to users who can publish posts. * * @param int $user_id User ID. * @@ -510,17 +508,4 @@ public static function user_register( $user_id ) { $user->add_cap( 'activitypub' ); } } - - /** - * Filter the query to only show the current author's extra fields - * - * @param WP_Query $query The WP_Query instance (passed by reference). - * - * @return void - */ - public static function filter_get_posts_query( $query ) { - if ( $query->get( 'post_type' ) === 'ap_extrafield' ) { - $query->set( 'author', get_current_user_id() ); - } - } } diff --git a/includes/class-admin.php b/includes/class-admin.php index 2b9e0440f..425eae177 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -22,6 +22,7 @@ public static function init() { \add_action( 'admin_init', array( self::class, 'register_settings' ) ); \add_action( 'load-comment.php', array( self::class, 'edit_comment' ) ); \add_action( 'load-post.php', array( self::class, 'edit_post' ) ); + \add_action( 'load-edit.php', array( self::class, 'list_posts' ) ); \add_action( 'personal_options_update', array( self::class, 'save_user_description' ) ); \add_action( 'admin_enqueue_scripts', array( self::class, 'enqueue_scripts' ) ); \add_action( 'admin_notices', array( self::class, 'admin_notices' ) ); @@ -372,6 +373,39 @@ function ( $allcaps, $caps, $arg ) { ); } + /** + * Add ActivityPub specific actions/filters to the post list view + * + * @return void + */ + public static function list_posts() { + // Show only the user's extra fields. + \add_action( + 'pre_get_posts', + function ( $query ) { + if ( + $query->get( 'post_type' ) === 'ap_extrafield' + ) { + $query->set( 'author', get_current_user_id() ); + } + } + ); + + // Remove all views for the extra fields. + $screen_id = get_current_screen()->id; + + add_filter( + "views_{$screen_id}", + function ( $views ) { + if ( 'ap_extrafield' === get_post_type() ) { + return array(); + } + + return $views; + } + ); + } + public static function comment_row_actions( $actions, $comment ) { if ( was_comment_received( $comment ) ) { unset( $actions['edit'] ); From 3fc673b59192b156cdb3e6ee2a4535507f0bd4b4 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 31 May 2024 10:57:41 +0200 Subject: [PATCH 07/22] author should not be editable --- includes/class-activitypub.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-activitypub.php b/includes/class-activitypub.php index c84a5a40c..dd89d781c 100644 --- a/includes/class-activitypub.php +++ b/includes/class-activitypub.php @@ -488,7 +488,7 @@ private static function register_post_types() { 'show_in_rest' => true, 'map_meta_cap' => true, 'show_ui' => true, - 'supports' => array( 'title', 'editor', 'author' ), + 'supports' => array( 'title', 'editor' ), ) ); From 0c6fc35dec11b418ae55d8a0b178d02412a8854a Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Thu, 4 Jul 2024 17:59:37 +0200 Subject: [PATCH 08/22] some improvements and extra informations --- includes/class-admin.php | 12 ++++++++++ includes/class-migration.php | 40 ++++++++++++++++++++++++++++++++++ includes/model/class-user.php | 41 ++++------------------------------- templates/user-settings.php | 14 +++++++----- 4 files changed, 65 insertions(+), 42 deletions(-) diff --git a/includes/class-admin.php b/includes/class-admin.php index a8849e0f6..e7572e254 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -64,6 +64,8 @@ public static function admin_menu() { $followers_list_page = \add_users_page( \__( 'Followers', 'activitypub' ), \__( 'Followers', 'activitypub' ), 'read', 'activitypub-followers-list', array( self::class, 'followers_list_page' ) ); \add_action( 'load-' . $followers_list_page, array( self::class, 'add_followers_list_help_tab' ) ); + + \add_users_page( \__( 'Extra Fields', 'activitypub' ), \__( 'Extra Fields', 'activitypub' ), 'read', esc_url( admin_url( '/edit.php?post_type=ap_extrafield' ) ) ); } } @@ -78,6 +80,16 @@ public static function admin_notices() { $admin_notice = \__( 'You are using the ActivityPub plugin with a permalink structure of "plain". This will prevent ActivityPub from working. Please go to "Settings" / "Permalinks" and choose a permalink structure other than "plain".', 'activitypub' ); self::show_admin_notice( $admin_notice, 'error' ); } + + $current_screen = get_current_screen(); + + if ( isset( $current_screen->id ) && 'edit-ap_extrafield' === $current_screen->id ) { + ?> +
+ +
+ add_cap( 'activitypub' ); } } + + public static function generate_extra_fields( $user_id ) { + $blog = array( + 'post_type' => 'ap_extrafield', + 'post_title' => \__( 'Blog', 'activitypub' ), + 'post_status' => 'publish', + 'post_author' => $user_id, + // format to block editor format + 'post_content' => '

' . \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) . '

', + 'comment_status' => 'closed', + ); + + wp_insert_post( $blog ); + + $profile = array( + 'post_type' => 'ap_extrafield', + 'post_title' => \__( 'Profile', 'activitypub' ), + 'post_status' => 'publish', + 'post_author' => $user_id, + // format to block editor format + 'post_content' => '

' . \wp_parse_url( \get_author_posts_url( $user_id ), \PHP_URL_HOST ) . '

', + 'comment_status' => 'closed', + ); + + wp_insert_post( $profile ); + + if ( \get_the_author_meta( 'user_url', $user_id ) ) { + $homepage = array( + 'post_type' => 'ap_extrafield', + 'post_title' => \__( 'Homepage', 'activitypub' ), + 'post_status' => 'publish', + 'post_author' => $user_id, + // format to block editor format + 'post_content' => '

' . \wp_parse_url( \get_the_author_meta( 'user_url', $user_id ), \PHP_URL_HOST ) . '

', + 'comment_status' => 'closed', + ); + + wp_insert_post( $homepage ); + } + } } diff --git a/includes/model/class-user.php b/includes/model/class-user.php index d8778f77d..cfcf0f8b9 100644 --- a/includes/model/class-user.php +++ b/includes/model/class-user.php @@ -3,6 +3,7 @@ use WP_Query; use WP_Error; +use Activitypub\Migration; use Activitypub\Signature; use Activitypub\Model\Blog; use Activitypub\Activity\Actor; @@ -242,8 +243,9 @@ public function get_attachment() { ) ); + $attachments = array(); + if ( $extra_fields->have_posts() ) { - $attachments = array(); foreach ( $extra_fields->posts as $post ) { $content = \get_the_content( null, false, $post ); $content = \make_clickable( $content ); @@ -265,44 +267,9 @@ public function get_attachment() { ), ); } - return $attachments; - } - - $extra_fields = array(); - - $extra_fields[] = array( - 'type' => 'PropertyValue', - 'name' => \__( 'Blog', 'activitypub' ), - 'value' => \html_entity_decode( - '' . \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) . '', - \ENT_QUOTES, - 'UTF-8' - ), - ); - - $extra_fields[] = array( - 'type' => 'PropertyValue', - 'name' => \__( 'Profile', 'activitypub' ), - 'value' => \html_entity_decode( - '' . \wp_parse_url( \get_author_posts_url( $this->get__id() ), \PHP_URL_HOST ) . '', - \ENT_QUOTES, - 'UTF-8' - ), - ); - - if ( \get_the_author_meta( 'user_url', $this->get__id() ) ) { - $extra_fields[] = array( - 'type' => 'PropertyValue', - 'name' => \__( 'Website', 'activitypub' ), - 'value' => \html_entity_decode( - '' . \wp_parse_url( \get_the_author_meta( 'user_url', $this->get__id() ), \PHP_URL_HOST ) . '', - \ENT_QUOTES, - 'UTF-8' - ), - ); } - return $extra_fields; + return $attachments; } /** diff --git a/templates/user-settings.php b/templates/user-settings.php index 5bd8e57a0..2dcdc5761 100644 --- a/templates/user-settings.php +++ b/templates/user-settings.php @@ -34,6 +34,8 @@

+ + posts as $extra_field ) { ?> -

- - - -

+ + + + + + +

From 039bba2d13bdbf130110f5d0351416efe5d3d652 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 5 Jul 2024 07:24:06 +0200 Subject: [PATCH 09/22] description --- includes/class-admin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-admin.php b/includes/class-admin.php index e7572e254..fa1f41696 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -86,7 +86,7 @@ public static function admin_notices() { if ( isset( $current_screen->id ) && 'edit-ap_extrafield' === $current_screen->id ) { ?>

- +
Date: Fri, 5 Jul 2024 08:23:31 +0200 Subject: [PATCH 10/22] use nopaging instead --- includes/model/class-user.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/model/class-user.php b/includes/model/class-user.php index cfcf0f8b9..93591e8e2 100644 --- a/includes/model/class-user.php +++ b/includes/model/class-user.php @@ -236,10 +236,10 @@ public function get_endpoints() { public function get_attachment() { $extra_fields = new WP_Query( array( - 'post_type' => 'ap_extrafield', - 'posts_per_page' => -1, - 'status' => 'publish', - 'author' => $this->get__id(), + 'post_type' => 'ap_extrafield', + 'nopaging' => true, + 'status' => 'publish', + 'author' => $this->get__id(), ) ); From cfe9a11e3e688a059aea24171e28bd8e7639ed95 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 8 Jul 2024 11:43:05 +0200 Subject: [PATCH 11/22] add blog url --- includes/model/class-blog.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/includes/model/class-blog.php b/includes/model/class-blog.php index cc515385c..70109683b 100644 --- a/includes/model/class-blog.php +++ b/includes/model/class-blog.php @@ -368,4 +368,28 @@ public function get_indexable() { return false; } } + + /** + * Extend the User-Output with Attachments. + * + * @return array The extended User-Output. + */ + public function get_attachment() { + $array = array(); + + $array[] = array( + 'type' => 'PropertyValue', + 'name' => \__( 'Blog', 'activitypub' ), + 'value' => \html_entity_decode( + sprintf( + '%s', + \esc_attr( \home_url( '/' ) ), + \home_url( '/' ), + \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) + ), + \ENT_QUOTES, + 'UTF-8' + ), + ); + } } From 3ddbdae7115737fb3def146ee594594758f970fb Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 8 Jul 2024 11:43:12 +0200 Subject: [PATCH 12/22] formatting --- includes/class-migration.php | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/includes/class-migration.php b/includes/class-migration.php index 78ca6288e..0123759cd 100644 --- a/includes/class-migration.php +++ b/includes/class-migration.php @@ -287,24 +287,24 @@ private static function add_activitypub_capability() { public static function generate_extra_fields( $user_id ) { $blog = array( - 'post_type' => 'ap_extrafield', - 'post_title' => \__( 'Blog', 'activitypub' ), - 'post_status' => 'publish', - 'post_author' => $user_id, + 'post_type' => 'ap_extrafield', + 'post_title' => \__( 'Blog', 'activitypub' ), + 'post_status' => 'publish', + 'post_author' => $user_id, // format to block editor format - 'post_content' => '

' . \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) . '

', + 'post_content' => '

' . \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) . '

', 'comment_status' => 'closed', ); wp_insert_post( $blog ); $profile = array( - 'post_type' => 'ap_extrafield', - 'post_title' => \__( 'Profile', 'activitypub' ), - 'post_status' => 'publish', - 'post_author' => $user_id, + 'post_type' => 'ap_extrafield', + 'post_title' => \__( 'Profile', 'activitypub' ), + 'post_status' => 'publish', + 'post_author' => $user_id, // format to block editor format - 'post_content' => '

' . \wp_parse_url( \get_author_posts_url( $user_id ), \PHP_URL_HOST ) . '

', + 'post_content' => '

' . \wp_parse_url( \get_author_posts_url( $user_id ), \PHP_URL_HOST ) . '

', 'comment_status' => 'closed', ); @@ -312,12 +312,12 @@ public static function generate_extra_fields( $user_id ) { if ( \get_the_author_meta( 'user_url', $user_id ) ) { $homepage = array( - 'post_type' => 'ap_extrafield', - 'post_title' => \__( 'Homepage', 'activitypub' ), - 'post_status' => 'publish', - 'post_author' => $user_id, + 'post_type' => 'ap_extrafield', + 'post_title' => \__( 'Homepage', 'activitypub' ), + 'post_status' => 'publish', + 'post_author' => $user_id, // format to block editor format - 'post_content' => '

' . \wp_parse_url( \get_the_author_meta( 'user_url', $user_id ), \PHP_URL_HOST ) . '

', + 'post_content' => '

' . \wp_parse_url( \get_the_author_meta( 'user_url', $user_id ), \PHP_URL_HOST ) . '

', 'comment_status' => 'closed', ); From 483b13620eb95bb21bbdabdd0849c639e0fe0e34 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 8 Jul 2024 12:52:01 +0200 Subject: [PATCH 13/22] formatting --- includes/class-admin.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/includes/class-admin.php b/includes/class-admin.php index fa1f41696..e2f4e0989 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -403,9 +403,7 @@ public static function list_posts() { \add_action( 'pre_get_posts', function ( $query ) { - if ( - $query->get( 'post_type' ) === 'ap_extrafield' - ) { + if ( $query->get( 'post_type' ) === 'ap_extrafield' ) { $query->set( 'author', get_current_user_id() ); } } From 6e45ed148de199070edf4eda387cc8e7fe3723c4 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 8 Jul 2024 15:40:56 +0200 Subject: [PATCH 14/22] some cosmetics --- includes/class-admin.php | 44 ++++++++++++++++++++++++++++++++++--- templates/user-settings.php | 14 +++++++++--- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/includes/class-admin.php b/includes/class-admin.php index e2f4e0989..267a5982c 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -31,10 +31,13 @@ public static function init() { \add_filter( 'comment_row_actions', array( self::class, 'comment_row_actions' ), 10, 2 ); \add_filter( 'manage_edit-comments_columns', array( static::class, 'manage_comment_columns' ) ); - \add_filter( 'manage_comments_custom_column', array( static::class, 'manage_comments_custom_column' ), 9, 2 ); + \add_action( 'manage_comments_custom_column', array( static::class, 'manage_comments_custom_column' ), 9, 2 ); + + \add_filter( 'manage_posts_columns', array( static::class, 'manage_post_columns' ), 10, 2 ); + \add_action( 'manage_posts_custom_column', array( self::class, 'manage_posts_custom_column' ), 10, 2 ); \add_filter( 'manage_users_columns', array( self::class, 'manage_users_columns' ), 10, 1 ); - \add_filter( 'manage_users_custom_column', array( self::class, 'manage_users_custom_column' ), 10, 3 ); + \add_action( 'manage_users_custom_column', array( self::class, 'manage_users_custom_column' ), 10, 3 ); \add_filter( 'bulk_actions-users', array( self::class, 'user_bulk_options' ) ); \add_filter( 'handle_bulk_actions-users', array( self::class, 'handle_bulk_request' ), 10, 3 ); @@ -453,12 +456,28 @@ public static function manage_users_columns( $columns ) { * @param array $columns the list of column names */ public static function manage_comment_columns( $columns ) { - $columns['comment_type'] = esc_attr__( 'Comment-Type', 'activitypub' ); + $columns['comment_type'] = esc_attr__( 'Comment-Type', 'activitypub' ); $columns['comment_protocol'] = esc_attr__( 'Protocol', 'activitypub' ); return $columns; } + /** + * Add "post_content" as column for Extra-Fields in WP-Admin + * + * @param array $columns Tthe list of column names. + * @param string $post_type The post type. + */ + public static function manage_post_columns( $columns, $post_type ) { + if ( 'ap_extrafield' === $post_type ) { + $after_key = 'title'; + $index = array_search( $after_key, array_keys( $columns ), true ); + $columns = array_slice( $columns, 0, $index + 1 ) + array( 'extra_field_content' => esc_attr__( 'Content', 'activitypub' ) ) + $columns; + } + + return $columns; + } + /** * Add "comment-type" and "protocol" as column in WP-Admin * @@ -500,6 +519,25 @@ public static function manage_users_custom_column( $output, $column_name, $user_ } } + /** + * Add a column "extra_field_content" to the post list view + * + * @param string $column_name The column name. + * @param int $post_id The post ID. + * + * @return void + */ + public static function manage_posts_custom_column( $column_name, $post_id ) { + $post = get_post( $post_id ); + + if ( 'extra_field_content' === $column_name ) { + $post = get_post( $post_id ); + if ( 'ap_extrafield' === $post->post_type ) { + echo esc_attr( wp_strip_all_tags( $post->post_content ) ); + } + } + } + /** * Add options to the Bulk dropdown on the users page * diff --git a/templates/user-settings.php b/templates/user-settings.php index 2dcdc5761..9c548ab9a 100644 --- a/templates/user-settings.php +++ b/templates/user-settings.php @@ -51,14 +51,22 @@ post_title ); ?> - + + + + +

- - + + + + + +

From 896d82594d4fb741f0ba956872a9c23d26c63aec Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 8 Jul 2024 16:29:07 +0200 Subject: [PATCH 15/22] return attachments --- includes/model/class-blog.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/includes/model/class-blog.php b/includes/model/class-blog.php index 70109683b..864644d33 100644 --- a/includes/model/class-blog.php +++ b/includes/model/class-blog.php @@ -391,5 +391,7 @@ public function get_attachment() { 'UTF-8' ), ); + + return $array; } } From 300c4362677e70e13d94b4836403ad16a8d69a08 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 8 Jul 2024 16:47:39 +0200 Subject: [PATCH 16/22] formatting --- integration/class-buddypress.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/integration/class-buddypress.php b/integration/class-buddypress.php index 45cfc0d63..8d71c1d23 100644 --- a/integration/class-buddypress.php +++ b/integration/class-buddypress.php @@ -32,7 +32,12 @@ public static function add_user_metadata( $object, $author_id ) { 'type' => 'PropertyValue', 'name' => \__( 'Profile', 'activitypub' ), 'value' => \html_entity_decode( - '' . \wp_parse_url( \bp_core_get_user_domain( $author_id ), \PHP_URL_HOST ) . '', + sprintf( + '%s', + \esc_attr( bp_core_get_user_domain( $author_id ) ), + \bp_core_get_user_domain( $author_id ), + \wp_parse_url( \bp_core_get_user_domain( $author_id ), \PHP_URL_HOST ) + ), \ENT_QUOTES, 'UTF-8' ), @@ -51,7 +56,12 @@ public static function add_user_metadata( $object, $author_id ) { 'type' => 'PropertyValue', 'name' => $blog->blogname, 'value' => \html_entity_decode( - '' . \wp_parse_url( $blog->siteurl, \PHP_URL_HOST ) . '', + sprintf( + '%s', + \esc_attr( $blog->siteurl ), + $blog->siteurl, + \wp_parse_url( $blog->siteurl, \PHP_URL_HOST ) + ), \ENT_QUOTES, 'UTF-8' ), From f7d9b7d7dfaf379bf6f65529f11f634052050a6f Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 8 Jul 2024 16:51:03 +0200 Subject: [PATCH 17/22] formatting --- includes/class-webfinger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-webfinger.php b/includes/class-webfinger.php index 3233fcb5c..78c8b4b2f 100644 --- a/includes/class-webfinger.php +++ b/includes/class-webfinger.php @@ -174,7 +174,7 @@ public static function get_data( $uri ) { return $data; } - $webfinger_url = 'https://' . $host . '/.well-known/webfinger?resource=' . rawurlencode( $identifier ); + $webfinger_url = sprintf( 'https://%s/.well-known/webfinger?resource=%s', $host, rawurlencode( $identifier ) ); $response = wp_safe_remote_get( $webfinger_url, From 68404f0fd55100d261fdc65dd9ed4196c42c71b6 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 8 Jul 2024 18:50:45 +0200 Subject: [PATCH 18/22] add default extra fields --- includes/collection/class-users.php | 37 ++---------- includes/functions.php | 90 +++++++++++++++++++++++++++++ includes/model/class-user.php | 52 +++++++---------- templates/user-settings.php | 13 +---- 4 files changed, 121 insertions(+), 71 deletions(-) diff --git a/includes/collection/class-users.php b/includes/collection/class-users.php index b210504e9..e4596b878 100644 --- a/includes/collection/class-users.php +++ b/includes/collection/class-users.php @@ -8,6 +8,8 @@ use Activitypub\Model\Application; use function Activitypub\object_to_uri; +use function Activitypub\normalize_url; +use function Activitypub\normalize_host; use function Activitypub\url_to_authorid; use function Activitypub\is_user_disabled; @@ -182,8 +184,8 @@ public static function get_by_resource( $resource ) { // check for http(s)://blog.example.com/ if ( - self::normalize_url( site_url() ) === self::normalize_url( $resource ) || - self::normalize_url( home_url() ) === self::normalize_url( $resource ) + normalize_url( site_url() ) === normalize_url( $resource ) || + normalize_url( home_url() ) === normalize_url( $resource ) ) { return self::get_by_id( self::BLOG_USER_ID ); } @@ -197,8 +199,8 @@ public static function get_by_resource( $resource ) { case 'acct': $resource = \str_replace( 'acct:', '', $resource ); $identifier = \substr( $resource, 0, \strrpos( $resource, '@' ) ); - $host = self::normalize_host( \substr( \strrchr( $resource, '@' ), 1 ) ); - $blog_host = self::normalize_host( \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) ); + $host = normalize_host( \substr( \strrchr( $resource, '@' ), 1 ) ); + $blog_host = normalize_host( \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) ); if ( $blog_host !== $host ) { return new WP_Error( @@ -253,33 +255,6 @@ public static function get_by_various( $id ) { return self::get_by_username( $id ); } - /** - * Normalize a host. - * - * @param string $host The host. - * - * @return string The normalized host. - */ - public static function normalize_host( $host ) { - return \str_replace( 'www.', '', $host ); - } - - /** - * Normalize a URL. - * - * @param string $url The URL. - * - * @return string The normalized URL. - */ - public static function normalize_url( $url ) { - $url = \untrailingslashit( $url ); - $url = \str_replace( 'https://', '', $url ); - $url = \str_replace( 'http://', '', $url ); - $url = \str_replace( 'www.', '', $url ); - - return $url; - } - /** * Get the User collection. * diff --git a/includes/functions.php b/includes/functions.php index bf923a159..9fb51ceba 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1,6 +1,7 @@ 'ap_extrafield', + 'nopaging' => true, + 'status' => 'publish', + 'author' => $user_id, + ) + ); + + if ( $extra_fields->have_posts() ) { + return $extra_fields->posts; + } + + $extra_fields = array(); + $already_migrated = \get_user_meta( $user_id, 'activitypub_default_extra_fields', true ); + + if ( $already_migrated ) { + return $extra_fields; + } + + $defaults = array( + \__( 'Blog', 'activitypub' ) => \home_url( '/' ), + \__( 'Profile', 'activitypub' ) => \get_author_posts_url( $user_id ), + \__( 'Homepage', 'activitypub' ) => \get_the_author_meta( 'user_url', $user_id ), + ); + + foreach ( $defaults as $title => $url ) { + if ( ! $url ) { + continue; + } + + $extra_field = array( + 'post_type' => 'ap_extrafield', + 'post_title' => $title, + 'post_status' => 'publish', + 'post_author' => $user_id, + 'post_content' => sprintf( + '

%s

', + \esc_attr( $url ), + $url, + \wp_parse_url( $url, \PHP_URL_HOST ) + ), + 'comment_status' => 'closed', + ); + + $extra_field_id = wp_insert_post( $extra_field ); + $extra_fields[] = get_post( $extra_field_id ); + } + + \update_user_meta( $user_id, 'activitypub_default_extra_fields', true ); + + return $extra_fields; +} diff --git a/includes/model/class-user.php b/includes/model/class-user.php index 93591e8e2..ac7879c66 100644 --- a/includes/model/class-user.php +++ b/includes/model/class-user.php @@ -11,6 +11,7 @@ use function Activitypub\is_user_disabled; use function Activitypub\get_rest_url_by_path; +use function Activitypub\get_actor_extra_fields; class User extends Actor { /** @@ -234,39 +235,30 @@ public function get_endpoints() { * @return array The extended User-Output. */ public function get_attachment() { - $extra_fields = new WP_Query( - array( - 'post_type' => 'ap_extrafield', - 'nopaging' => true, - 'status' => 'publish', - 'author' => $this->get__id(), - ) - ); + $extra_fields = get_actor_extra_fields( \get_current_user_id() ); $attachments = array(); - if ( $extra_fields->have_posts() ) { - foreach ( $extra_fields->posts as $post ) { - $content = \get_the_content( null, false, $post ); - $content = \make_clickable( $content ); - $content = \do_blocks( $content ); - $content = \wptexturize( $content ); - $content = \wp_filter_content_tags( $content ); - // replace script and style elements - $content = \preg_replace( '@<(script|style)[^>]*?>.*?@si', '', $content ); - $content = \strip_shortcodes( $content ); - $content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) ); - - $attachments[] = array( - 'type' => 'PropertyValue', - 'name' => \get_the_title( $post ), - 'value' => \html_entity_decode( - $content, - \ENT_QUOTES, - 'UTF-8' - ), - ); - } + foreach ( $extra_fields as $post ) { + $content = \get_the_content( null, false, $post ); + $content = \make_clickable( $content ); + $content = \do_blocks( $content ); + $content = \wptexturize( $content ); + $content = \wp_filter_content_tags( $content ); + // replace script and style elements + $content = \preg_replace( '@<(script|style)[^>]*?>.*?@si', '', $content ); + $content = \strip_shortcodes( $content ); + $content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) ); + + $attachments[] = array( + 'type' => 'PropertyValue', + 'name' => \get_the_title( $post ), + 'value' => \html_entity_decode( + $content, + \ENT_QUOTES, + 'UTF-8' + ), + ); } return $attachments; diff --git a/templates/user-settings.php b/templates/user-settings.php index 9c548ab9a..9489acbd8 100644 --- a/templates/user-settings.php +++ b/templates/user-settings.php @@ -37,17 +37,10 @@ 'ap_extrafield', - 'nopaging' => true, - 'status' => 'publish', - 'author' => get_current_user_id(), - ) - ); + $extra_fields = \Activitypub\get_actor_extra_fields( \get_current_user_id() ); - foreach ( $extra_fields->posts as $extra_field ) { - ?> + foreach ( $extra_fields as $extra_field ) { + ?> From 63b0568737ac8f48be95eb73e73e4beaa2760508 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 8 Jul 2024 19:02:17 +0200 Subject: [PATCH 19/22] remove unused migration --- includes/class-migration.php | 40 ------------------------------------ 1 file changed, 40 deletions(-) diff --git a/includes/class-migration.php b/includes/class-migration.php index 0123759cd..acfe88a55 100644 --- a/includes/class-migration.php +++ b/includes/class-migration.php @@ -284,44 +284,4 @@ private static function add_activitypub_capability() { $user->add_cap( 'activitypub' ); } } - - public static function generate_extra_fields( $user_id ) { - $blog = array( - 'post_type' => 'ap_extrafield', - 'post_title' => \__( 'Blog', 'activitypub' ), - 'post_status' => 'publish', - 'post_author' => $user_id, - // format to block editor format - 'post_content' => '

' . \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) . '

', - 'comment_status' => 'closed', - ); - - wp_insert_post( $blog ); - - $profile = array( - 'post_type' => 'ap_extrafield', - 'post_title' => \__( 'Profile', 'activitypub' ), - 'post_status' => 'publish', - 'post_author' => $user_id, - // format to block editor format - 'post_content' => '

' . \wp_parse_url( \get_author_posts_url( $user_id ), \PHP_URL_HOST ) . '

', - 'comment_status' => 'closed', - ); - - wp_insert_post( $profile ); - - if ( \get_the_author_meta( 'user_url', $user_id ) ) { - $homepage = array( - 'post_type' => 'ap_extrafield', - 'post_title' => \__( 'Homepage', 'activitypub' ), - 'post_status' => 'publish', - 'post_author' => $user_id, - // format to block editor format - 'post_content' => '

' . \wp_parse_url( \get_the_author_meta( 'user_url', $user_id ), \PHP_URL_HOST ) . '

', - 'comment_status' => 'closed', - ); - - wp_insert_post( $homepage ); - } - } } From fbbdd97710ed3c2d06248717bf49e0f3f2d37668 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Tue, 9 Jul 2024 08:58:45 +0200 Subject: [PATCH 20/22] better defaults handling --- includes/class-admin.php | 6 ++++++ includes/functions.php | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/includes/class-admin.php b/includes/class-admin.php index 267a5982c..85345687d 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -9,6 +9,7 @@ use function Activitypub\is_user_disabled; use function Activitypub\was_comment_received; use function Activitypub\is_comment_federatable; +use function Activitypub\add_default_actor_extra_fields; /** * ActivityPub Admin Class @@ -425,6 +426,11 @@ function ( $views ) { return $views; } ); + + // Set defaults for new extra fields. + if ( 'edit-ap_extrafield' === $screen_id ) { + add_default_actor_extra_fields( get_current_user_id() ); + } } public static function comment_row_actions( $actions, $comment ) { diff --git a/includes/functions.php b/includes/functions.php index 9fb51ceba..739653a92 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1039,6 +1039,17 @@ function get_actor_extra_fields( $user_id ) { return $extra_fields->posts; } + return add_default_actor_extra_fields( $user_id ); +} + +/** + * Add default extra fields to an actor. + * + * @param int $user_id The User-ID. + * + * @return array The extra fields. + */ +function add_default_actor_extra_fields( $user_id ) { $extra_fields = array(); $already_migrated = \get_user_meta( $user_id, 'activitypub_default_extra_fields', true ); @@ -1071,8 +1082,8 @@ function get_actor_extra_fields( $user_id ) { 'comment_status' => 'closed', ); - $extra_field_id = wp_insert_post( $extra_field ); - $extra_fields[] = get_post( $extra_field_id ); + $extra_field_id = wp_insert_post( $extra_field ); + $extra_fields[] = get_post( $extra_field_id ); } \update_user_meta( $user_id, 'activitypub_default_extra_fields', true ); From 6be70aec2d10a81139fa90a8f318158a4c812119 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Thu, 11 Jul 2024 08:21:19 +0200 Subject: [PATCH 21/22] use a filter to be able to unhook/replace default extra fields --- includes/class-activitypub.php | 55 ++++++++++++++++++++++++++++++++++ includes/class-admin.php | 3 +- includes/functions.php | 55 +++------------------------------- 3 files changed, 61 insertions(+), 52 deletions(-) diff --git a/includes/class-activitypub.php b/includes/class-activitypub.php index 56467c51b..7e31f344a 100644 --- a/includes/class-activitypub.php +++ b/includes/class-activitypub.php @@ -45,6 +45,8 @@ public static function init() { \add_action( 'in_plugin_update_message-' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'plugin_update_message' ) ); + \add_filter( 'activitypub_get_actor_extra_fields', array( self::class, 'default_actor_extra_fields' ), 10, 2 ); + // register several post_types self::register_post_types(); } @@ -503,4 +505,57 @@ public static function user_register( $user_id ) { $user->add_cap( 'activitypub' ); } } + + /** + * Add default extra fields to an actor. + * + * @param array $extra_fields The extra fields. + * @param int $user_id The User-ID. + * + * @return array The extra fields. + */ + public static function default_actor_extra_fields( $extra_fields, $user_id ) { + if ( $extra_fields ) { + return $extra_fields; + } + + $already_migrated = \get_user_meta( $user_id, 'activitypub_default_extra_fields', true ); + + if ( $already_migrated ) { + return $extra_fields; + } + + $defaults = array( + \__( 'Blog', 'activitypub' ) => \home_url( '/' ), + \__( 'Profile', 'activitypub' ) => \get_author_posts_url( $user_id ), + \__( 'Homepage', 'activitypub' ) => \get_the_author_meta( 'user_url', $user_id ), + ); + + foreach ( $defaults as $title => $url ) { + if ( ! $url ) { + continue; + } + + $extra_field = array( + 'post_type' => 'ap_extrafield', + 'post_title' => $title, + 'post_status' => 'publish', + 'post_author' => $user_id, + 'post_content' => sprintf( + '

%s

', + \esc_attr( $url ), + $url, + \wp_parse_url( $url, \PHP_URL_HOST ) + ), + 'comment_status' => 'closed', + ); + + $extra_field_id = wp_insert_post( $extra_field ); + $extra_fields[] = get_post( $extra_field_id ); + } + + \update_user_meta( $user_id, 'activitypub_default_extra_fields', true ); + + return $extra_fields; + } } diff --git a/includes/class-admin.php b/includes/class-admin.php index 85345687d..de24daf36 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -3,6 +3,7 @@ use WP_User_Query; use Activitypub\Model\Blog; +use Activitypub\Activitypub; use Activitypub\Collection\Users; use function Activitypub\count_followers; @@ -429,7 +430,7 @@ function ( $views ) { // Set defaults for new extra fields. if ( 'edit-ap_extrafield' === $screen_id ) { - add_default_actor_extra_fields( get_current_user_id() ); + Activitypub::default_actor_extra_fields( array(), get_current_user_id() ); } } diff --git a/includes/functions.php b/includes/functions.php index 739653a92..2fa038982 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1036,57 +1036,10 @@ function get_actor_extra_fields( $user_id ) { ); if ( $extra_fields->have_posts() ) { - return $extra_fields->posts; - } - - return add_default_actor_extra_fields( $user_id ); -} - -/** - * Add default extra fields to an actor. - * - * @param int $user_id The User-ID. - * - * @return array The extra fields. - */ -function add_default_actor_extra_fields( $user_id ) { - $extra_fields = array(); - $already_migrated = \get_user_meta( $user_id, 'activitypub_default_extra_fields', true ); - - if ( $already_migrated ) { - return $extra_fields; - } - - $defaults = array( - \__( 'Blog', 'activitypub' ) => \home_url( '/' ), - \__( 'Profile', 'activitypub' ) => \get_author_posts_url( $user_id ), - \__( 'Homepage', 'activitypub' ) => \get_the_author_meta( 'user_url', $user_id ), - ); - - foreach ( $defaults as $title => $url ) { - if ( ! $url ) { - continue; - } - - $extra_field = array( - 'post_type' => 'ap_extrafield', - 'post_title' => $title, - 'post_status' => 'publish', - 'post_author' => $user_id, - 'post_content' => sprintf( - '

%s

', - \esc_attr( $url ), - $url, - \wp_parse_url( $url, \PHP_URL_HOST ) - ), - 'comment_status' => 'closed', - ); - - $extra_field_id = wp_insert_post( $extra_field ); - $extra_fields[] = get_post( $extra_field_id ); + $extra_fields = $extra_fields->posts; + } else { + $extra_fields = array(); } - \update_user_meta( $user_id, 'activitypub_default_extra_fields', true ); - - return $extra_fields; + return apply_filters( 'activitypub_get_actor_extra_fields', $extra_fields, $user_id ); } From 812f4d2c9b62fb1754c7241b457f6f853de9660c Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 15 Jul 2024 09:41:27 +0200 Subject: [PATCH 22/22] should fix #810 --- FEDERATION.md | 1 + includes/model/class-blog.php | 10 ++++++++- includes/model/class-user.php | 41 +++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/FEDERATION.md b/FEDERATION.md index 77f84b90b..b1dabe415 100644 --- a/FEDERATION.md +++ b/FEDERATION.md @@ -16,6 +16,7 @@ The WordPress plugin largely follows ActivityPub's server-to-server specificatio - [FEP-5feb: Search indexing consent for actors](https://codeberg.org/fediverse/fep/src/branch/main/fep/5feb/fep-5feb.md) - [FEP-2677: Identifying the Application Actor](https://codeberg.org/fediverse/fep/src/branch/main/fep/2677/fep-2677.md) - [FEP-2c59: Discovery of a Webfinger address from an ActivityPub actor](https://codeberg.org/fediverse/fep/src/branch/main/fep/2c59/fep-2c59.md) +- [FEP-fb2a: Actor metadata](https://codeberg.org/fediverse/fep/src/branch/main/fep/fb2a/fep-fb2a.md) Partially supported FEPs diff --git a/includes/model/class-blog.php b/includes/model/class-blog.php index 864644d33..8dbde45c0 100644 --- a/includes/model/class-blog.php +++ b/includes/model/class-blog.php @@ -384,7 +384,7 @@ public function get_attachment() { sprintf( '%s', \esc_attr( \home_url( '/' ) ), - \home_url( '/' ), + \esc_url( \home_url( '/' ) ), \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) ), \ENT_QUOTES, @@ -392,6 +392,14 @@ public function get_attachment() { ), ); + // Add support for FEP-fb2a, for more information see FEDERATION.md + $array[] = array( + 'type' => 'Link', + 'name' => \__( 'Blog', 'activitypub' ), + 'href' => \esc_url( \home_url( '/' ) ), + 'rel' => array( 'me' ), + ); + return $array; } } diff --git a/includes/model/class-user.php b/includes/model/class-user.php index ac7879c66..64233eee8 100644 --- a/includes/model/class-user.php +++ b/includes/model/class-user.php @@ -259,6 +259,47 @@ public function get_attachment() { 'UTF-8' ), ); + + $link_added = false; + + // Add support for FEP-fb2a, for more information see FEDERATION.md + if ( \class_exists( '\WP_HTML_Tag_Processor' ) ) { + $tags = new \WP_HTML_Tag_Processor( $content ); + $tags->next_tag(); + + if ( 'P' === $tags->get_tag() ) { + $tags->next_tag(); + } + + if ( 'A' === $tags->get_tag() ) { + $tags->set_bookmark( 'link' ); + if ( ! $tags->next_tag() ) { + $tags->seek( 'link' ); + $attachment = array( + 'type' => 'Link', + 'name' => \get_the_title( $post ), + 'href' => \esc_url( $tags->get_attribute( 'href' ) ), + 'rel' => explode( ' ', $tags->get_attribute( 'rel' ) ), + ); + + $link_added = true; + } + } + } + + if ( ! $link_added ) { + $attachment = array( + 'type' => 'Note', + 'name' => \get_the_title( $post ), + 'content' => \html_entity_decode( + $content, + \ENT_QUOTES, + 'UTF-8' + ), + ); + } + + $attachments[] = $attachment; } return $attachments;