From 4abd5aefb45e651af0f31b328f47817de46999e8 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Tue, 9 May 2023 10:28:23 +0200 Subject: [PATCH 1/8] cache inbox list --- includes/collection/class-followers.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/includes/collection/class-followers.php b/includes/collection/class-followers.php index c6c6223..a6a8ba4 100644 --- a/includes/collection/class-followers.php +++ b/includes/collection/class-followers.php @@ -19,6 +19,7 @@ use function Activitypub\get_remote_metadata_by_actor; */ class Followers { const TAXONOMY = 'activitypub-followers'; + const CACHE_KEY_INBOXES = 'activitypub_follower_inboxes_for_%s'; /** * Register WordPress hooks/actions and register Taxonomy @@ -225,6 +226,8 @@ class Followers { if ( is_wp_error( $result ) ) { return $result; } else { + $cache_key = sprintf( self::CACHE_KEY_INBOXES, $user_id ); + wp_cache_delete( $cache_key ); return $follower; } } @@ -238,6 +241,8 @@ class Followers { * @return bool|WP_Error True on success, false or WP_Error on failure. */ public static function remove_follower( $user_id, $actor ) { + $cache_key = sprintf( self::CACHE_KEY_INBOXES, $user_id ); + wp_cache_delete( $cache_key ); return wp_remove_object_terms( $user_id, $actor, self::TAXONOMY ); } @@ -369,6 +374,13 @@ class Followers { * @return array The list of Inboxes */ public static function get_inboxes( $user_id ) { + $cache_key = sprintf( self::CACHE_KEY_INBOXES, $user_id ); + $inboxes = wp_cache_get( $cache_key ); + + if ( $inboxes ) { + return $inboxes; + } + // get all Followers of a ID of the WordPress User $terms = new WP_Term_Query( array( @@ -402,6 +414,9 @@ class Followers { ) ); - return array_filter( $results ); + $inboxes = array_filter( $results ); + wp_cache_set( $cache_key, $inboxes ); + + return $inboxes; } } From 74be5d6b5159bed33b67e377a7fbf6383d66fee1 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 10 May 2023 09:04:33 +0200 Subject: [PATCH 2/8] implemented feedback of @akirk --- includes/collection/class-followers.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/includes/collection/class-followers.php b/includes/collection/class-followers.php index a6a8ba4..5e5e532 100644 --- a/includes/collection/class-followers.php +++ b/includes/collection/class-followers.php @@ -19,7 +19,7 @@ use function Activitypub\get_remote_metadata_by_actor; */ class Followers { const TAXONOMY = 'activitypub-followers'; - const CACHE_KEY_INBOXES = 'activitypub_follower_inboxes_for_%s'; + const CACHE_KEY_INBOXES = 'follower_inboxes_%s'; /** * Register WordPress hooks/actions and register Taxonomy @@ -226,8 +226,7 @@ class Followers { if ( is_wp_error( $result ) ) { return $result; } else { - $cache_key = sprintf( self::CACHE_KEY_INBOXES, $user_id ); - wp_cache_delete( $cache_key ); + wp_cache_delete( sprintf( self::CACHE_KEY_INBOXES, $user_id ), 'activitypub' ); return $follower; } } @@ -241,8 +240,7 @@ class Followers { * @return bool|WP_Error True on success, false or WP_Error on failure. */ public static function remove_follower( $user_id, $actor ) { - $cache_key = sprintf( self::CACHE_KEY_INBOXES, $user_id ); - wp_cache_delete( $cache_key ); + wp_cache_delete( sprintf( self::CACHE_KEY_INBOXES, $user_id ), 'activitypub' ); return wp_remove_object_terms( $user_id, $actor, self::TAXONOMY ); } @@ -375,7 +373,7 @@ class Followers { */ public static function get_inboxes( $user_id ) { $cache_key = sprintf( self::CACHE_KEY_INBOXES, $user_id ); - $inboxes = wp_cache_get( $cache_key ); + $inboxes = wp_cache_get( $cache_key, 'activitypub' ); if ( $inboxes ) { return $inboxes; @@ -415,7 +413,7 @@ class Followers { ); $inboxes = array_filter( $results ); - wp_cache_set( $cache_key, $inboxes ); + wp_cache_set( $cache_key, $inboxes, 'activitypub' ); return $inboxes; } From a1753242f3ea19b085a19c83fd0a09753720016d Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 19 May 2023 18:03:05 +0200 Subject: [PATCH 3/8] fix missing namespace --- includes/rest/class-outbox.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/rest/class-outbox.php b/includes/rest/class-outbox.php index d6b0757..2138f71 100644 --- a/includes/rest/class-outbox.php +++ b/includes/rest/class-outbox.php @@ -8,6 +8,7 @@ use WP_REST_Response; use Activitypub\Model\Post; use Activitypub\Model\Activity; +use function Activitypub\get_context; use function Activitypub\get_rest_url_by_path; /** From 25b53887efaeaa24e26b844f8218a02f0b6fb4bf Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 19 May 2023 22:37:05 +0200 Subject: [PATCH 4/8] code improvements --- activitypub.php | 4 ++-- integration/class-buddypress.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/activitypub.php b/activitypub.php index 3f582ea..a1cd6df 100644 --- a/activitypub.php +++ b/activitypub.php @@ -50,7 +50,7 @@ function init() { Health_Check::init(); Scheduler::init(); } -\add_action( 'plugins_loaded', '\Activitypub\init' ); +\add_action( 'plugins_loaded', __NAMESPACE__ . '\init' ); /** * Class Autoloader @@ -143,7 +143,7 @@ function enable_buddypress_features() { require_once \dirname( __FILE__ ) . '/integration/class-buddypress.php'; Integration\Buddypress::init(); } -add_action( 'bp_include', '\Activitypub\enable_buddypress_features' ); +add_action( 'bp_include', __NAMESPACE__ . '\enable_buddypress_features' ); /** * `get_plugin_data` wrapper diff --git a/integration/class-buddypress.php b/integration/class-buddypress.php index 1087f47..e4ec40e 100644 --- a/integration/class-buddypress.php +++ b/integration/class-buddypress.php @@ -3,7 +3,7 @@ namespace Activitypub\Integration; class Buddypress { public static function init() { - \add_filter( 'activitypub_json_author_array', array( 'Activitypub\Integration\Buddypress', 'add_user_metadata' ), 11, 2 ); + \add_filter( 'activitypub_json_author_array', array( self::class, 'add_user_metadata' ), 11, 2 ); } public static function add_user_metadata( $object, $author_id ) { From 68002db291e8952fd3b79c24363333f87d564a49 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 22 May 2023 10:58:13 +0200 Subject: [PATCH 5/8] prevent sweeping of followers taxonomies thanks @akirk https://github.com/polylang/polylang/commit/b0db9db87ea4c765f85c4478454bc4b95ebf8917 --- activitypub.php | 25 ++++++++++--- includes/collection/class-followers.php | 19 ++++++++++ integration/class-wp-sweep.php | 50 +++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 integration/class-wp-sweep.php diff --git a/activitypub.php b/activitypub.php index a1cd6df..e19751f 100644 --- a/activitypub.php +++ b/activitypub.php @@ -27,6 +27,7 @@ function init() { \defined( 'ACTIVITYPUB_HASHTAGS_REGEXP' ) || \define( 'ACTIVITYPUB_HASHTAGS_REGEXP', '(?:(?<=\s)|(?<=

)|(?<=
)|^)#([A-Za-z0-9_]+)(?:(?=\s|[[:punct:]]|$))' ); \defined( 'ACTIVITYPUB_USERNAME_REGEXP' ) || \define( 'ACTIVITYPUB_USERNAME_REGEXP', '(?:([A-Za-z0-9_-]+)@((?:[A-Za-z0-9_-]+\.)+[A-Za-z]+))' ); \defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "[ap_title]\n\n[ap_content]\n\n[ap_hashtags]\n\n[ap_shortlink]" ); + \define( 'ACTIVITYPUB_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) ); @@ -139,11 +140,25 @@ function plugin_settings_link( $actions ) { /** * Only load code that needs BuddyPress to run once BP is loaded and initialized. */ -function enable_buddypress_features() { - require_once \dirname( __FILE__ ) . '/integration/class-buddypress.php'; - Integration\Buddypress::init(); -} -add_action( 'bp_include', __NAMESPACE__ . '\enable_buddypress_features' ); +add_action( + 'bp_include', + function() { + require_once \dirname( __FILE__ ) . '/integration/class-buddypress.php'; + Integration\Buddypress::init(); + }, + 0 +); + +add_action( + 'plugins_loaded', + function() { + if ( defined( 'WP_SWEEP_VERSION' ) ) { + require_once \dirname( __FILE__ ) . '/integration/class-wp-sweep.php'; + Integration\Wp_Sweep::init(); + } + }, + 0 +); /** * `get_plugin_data` wrapper diff --git a/includes/collection/class-followers.php b/includes/collection/class-followers.php index 8a271f6..c7e55c2 100644 --- a/includes/collection/class-followers.php +++ b/includes/collection/class-followers.php @@ -338,6 +338,25 @@ class Followers { return $items; } + /** + * Get all Followers + * + * @param array $args The WP_Term_Query arguments. + * + * @return array The Term list of Followers. + */ + public static function get_all_followers( $args = array() ) { + $defaults = array( + 'taxonomy' => self::TAXONOMY, + 'hide_empty' => false, + ); + + $args = wp_parse_args( $args, $defaults ); + $terms = new WP_Term_Query( $args ); + + return $terms->get_terms(); + } + /** * Count the total number of followers * diff --git a/integration/class-wp-sweep.php b/integration/class-wp-sweep.php new file mode 100644 index 0000000..730edba --- /dev/null +++ b/integration/class-wp-sweep.php @@ -0,0 +1,50 @@ + 'ids' ) ); + + $excluded_term_ids = array_merge( $excluded_term_ids, $followers ); + + return array_unique( $excluded_term_ids ); + } +} From c1b644aee1f9059fba4714d402284755a94150b3 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 22 May 2023 11:33:02 +0200 Subject: [PATCH 6/8] Fix #339 --- README.md | 4 +++- readme.txt | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d7a9662..e434e69 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ In order for webfinger to work, it must be mapped to the root directory of the U Add the following to the .htaccess file in the root directory: - RedirectMatch "^\/\.well-known(.*)$" "\/blog\/\.well-known$1" + RedirectMatch "^\/\.well-known/(webfinger|nodeinfo|x-nodeinfo2)(.*)$" "\/blog\/\.well-known$1" Where 'blog' is the path to the subdirectory at which your blog resides. @@ -115,6 +115,8 @@ Project maintained on GitHub at [automattic/wordpress-activitypub](https://githu ### Next ### +* Compatibility: add a new conditional, `\Activitypub\is_activitypub_request()`, to allow third-party plugins to detect ActivityPub requests. +* Compatibility: add hooks to allow modifying images returned in ActivityPub requests. * Compatibility: indicate that the plugin is compatible and has been tested with the latest version of WordPress, 6.2. ### 0.17.0 ### diff --git a/readme.txt b/readme.txt index c08f455..85a02e8 100644 --- a/readme.txt +++ b/readme.txt @@ -94,7 +94,7 @@ In order for webfinger to work, it must be mapped to the root directory of the U Add the following to the .htaccess file in the root directory: - RedirectMatch "^\/\.well-known(.*)$" "\/blog\/\.well-known$1" + RedirectMatch "^\/\.well-known/(webfinger|nodeinfo|x-nodeinfo2)(.*)$" "\/blog\/\.well-known$1" Where 'blog' is the path to the subdirectory at which your blog resides. From 653b1f9fae989aa2ca3d3fff9569c83a44b076e1 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 22 May 2023 13:38:44 +0200 Subject: [PATCH 7/8] added missing `$2` see #339 --- README.md | 2 +- readme.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e434e69..3a3a360 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ In order for webfinger to work, it must be mapped to the root directory of the U Add the following to the .htaccess file in the root directory: - RedirectMatch "^\/\.well-known/(webfinger|nodeinfo|x-nodeinfo2)(.*)$" "\/blog\/\.well-known$1" + RedirectMatch "^\/\.well-known/(webfinger|nodeinfo|x-nodeinfo2)(.*)$" "\/blog\/\.well-known$1$2" Where 'blog' is the path to the subdirectory at which your blog resides. diff --git a/readme.txt b/readme.txt index 85a02e8..0d2727b 100644 --- a/readme.txt +++ b/readme.txt @@ -94,7 +94,7 @@ In order for webfinger to work, it must be mapped to the root directory of the U Add the following to the .htaccess file in the root directory: - RedirectMatch "^\/\.well-known/(webfinger|nodeinfo|x-nodeinfo2)(.*)$" "\/blog\/\.well-known$1" + RedirectMatch "^\/\.well-known/(webfinger|nodeinfo|x-nodeinfo2)(.*)$" "\/blog\/\.well-known$1$2" Where 'blog' is the path to the subdirectory at which your blog resides. From 2c7f0687cc88c236defe00ede0eee7615ec44e6d Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 22 May 2023 14:47:20 +0200 Subject: [PATCH 8/8] fix #271 thanks @janboddez --- templates/settings.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/settings.php b/templates/settings.php index 2bfaed4..0daca17 100644 --- a/templates/settings.php +++ b/templates/settings.php @@ -120,8 +120,8 @@

  • - name, $support_post_types, true ) ); ?> /> - + name, $support_post_types, true ) ); ?> /> +