From c4daffe5c60c95d06c0b88aeba4d0537cb9813bb Mon Sep 17 00:00:00 2001 From: Matt Wiebe Date: Wed, 18 Oct 2023 16:20:06 -0500 Subject: [PATCH 1/5] Shortcodes: only register when needed --- activitypub.php | 1 - includes/class-shortcodes.php | 20 +++++++++++++------- includes/transformer/class-post.php | 6 ++++++ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/activitypub.php b/activitypub.php index 521a379..f232c57 100644 --- a/activitypub.php +++ b/activitypub.php @@ -69,7 +69,6 @@ function plugin_init() { \add_action( 'init', array( __NAMESPACE__ . '\Collection\Followers', 'init' ) ); \add_action( 'init', array( __NAMESPACE__ . '\Admin', 'init' ) ); \add_action( 'init', array( __NAMESPACE__ . '\Hashtag', 'init' ) ); - \add_action( 'init', array( __NAMESPACE__ . '\Shortcodes', 'init' ) ); \add_action( 'init', array( __NAMESPACE__ . '\Mention', 'init' ) ); \add_action( 'init', array( __NAMESPACE__ . '\Health_Check', 'init' ) ); \add_action( 'init', array( __NAMESPACE__ . '\Scheduler', 'init' ) ); diff --git a/includes/class-shortcodes.php b/includes/class-shortcodes.php index 43be17b..708aa61 100644 --- a/includes/class-shortcodes.php +++ b/includes/class-shortcodes.php @@ -5,14 +5,9 @@ use function Activitypub\esc_hashtag; class Shortcodes { /** - * Class constructor, registering WordPress then Shortcodes + * Register the shortcodes */ - public static function init() { - // do not load on admin pages - if ( is_admin() ) { - return; - } - + public static function register() { foreach ( get_class_methods( self::class ) as $shortcode ) { if ( 'init' !== $shortcode ) { add_shortcode( 'ap_' . $shortcode, array( self::class, $shortcode ) ); @@ -20,6 +15,17 @@ class Shortcodes { } } + /** + * Unregister the shortcodes + */ + public static function unregister() { + foreach ( get_class_methods( self::class ) as $shortcode ) { + if ( 'init' !== $shortcode ) { + remove_shortcode( 'ap_' . $shortcode ); + } + } + } + /** * Generates output for the 'ap_hashtags' shortcode * diff --git a/includes/transformer/class-post.php b/includes/transformer/class-post.php index 6e2f0aa..9250afe 100644 --- a/includes/transformer/class-post.php +++ b/includes/transformer/class-post.php @@ -5,6 +5,7 @@ use WP_Post; use Activitypub\Collection\Users; use Activitypub\Model\Blog_User; use Activitypub\Activity\Base_Object; +use Activitypub\Shortcodes; use function Activitypub\esc_hashtag; use function Activitypub\is_single_user; @@ -466,6 +467,8 @@ class Post { $post = $this->wp_post; $content = $this->get_post_content_template(); + // Register our shortcodes just in time. + Shortcodes::register(); // Fill in the shortcodes. setup_postdata( $post ); $content = do_shortcode( $content ); @@ -477,6 +480,9 @@ class Post { $content = \apply_filters( 'activitypub_the_content', $content, $post ); + // Don't need these any more, should never appear in a post. + Shortcodes::unregister(); + return $content; } From ff58070a5e9cc25223b5504645aab3fa4789f476 Mon Sep 17 00:00:00 2001 From: Matt Wiebe Date: Wed, 18 Oct 2023 16:21:20 -0500 Subject: [PATCH 2/5] Revert "Shortcodes: only register when needed" This reverts commit c4daffe5c60c95d06c0b88aeba4d0537cb9813bb. --- activitypub.php | 1 + includes/class-shortcodes.php | 20 +++++++------------- includes/transformer/class-post.php | 6 ------ 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/activitypub.php b/activitypub.php index f232c57..521a379 100644 --- a/activitypub.php +++ b/activitypub.php @@ -69,6 +69,7 @@ function plugin_init() { \add_action( 'init', array( __NAMESPACE__ . '\Collection\Followers', 'init' ) ); \add_action( 'init', array( __NAMESPACE__ . '\Admin', 'init' ) ); \add_action( 'init', array( __NAMESPACE__ . '\Hashtag', 'init' ) ); + \add_action( 'init', array( __NAMESPACE__ . '\Shortcodes', 'init' ) ); \add_action( 'init', array( __NAMESPACE__ . '\Mention', 'init' ) ); \add_action( 'init', array( __NAMESPACE__ . '\Health_Check', 'init' ) ); \add_action( 'init', array( __NAMESPACE__ . '\Scheduler', 'init' ) ); diff --git a/includes/class-shortcodes.php b/includes/class-shortcodes.php index 708aa61..43be17b 100644 --- a/includes/class-shortcodes.php +++ b/includes/class-shortcodes.php @@ -5,9 +5,14 @@ use function Activitypub\esc_hashtag; class Shortcodes { /** - * Register the shortcodes + * Class constructor, registering WordPress then Shortcodes */ - public static function register() { + public static function init() { + // do not load on admin pages + if ( is_admin() ) { + return; + } + foreach ( get_class_methods( self::class ) as $shortcode ) { if ( 'init' !== $shortcode ) { add_shortcode( 'ap_' . $shortcode, array( self::class, $shortcode ) ); @@ -15,17 +20,6 @@ class Shortcodes { } } - /** - * Unregister the shortcodes - */ - public static function unregister() { - foreach ( get_class_methods( self::class ) as $shortcode ) { - if ( 'init' !== $shortcode ) { - remove_shortcode( 'ap_' . $shortcode ); - } - } - } - /** * Generates output for the 'ap_hashtags' shortcode * diff --git a/includes/transformer/class-post.php b/includes/transformer/class-post.php index 9250afe..6e2f0aa 100644 --- a/includes/transformer/class-post.php +++ b/includes/transformer/class-post.php @@ -5,7 +5,6 @@ use WP_Post; use Activitypub\Collection\Users; use Activitypub\Model\Blog_User; use Activitypub\Activity\Base_Object; -use Activitypub\Shortcodes; use function Activitypub\esc_hashtag; use function Activitypub\is_single_user; @@ -467,8 +466,6 @@ class Post { $post = $this->wp_post; $content = $this->get_post_content_template(); - // Register our shortcodes just in time. - Shortcodes::register(); // Fill in the shortcodes. setup_postdata( $post ); $content = do_shortcode( $content ); @@ -480,9 +477,6 @@ class Post { $content = \apply_filters( 'activitypub_the_content', $content, $post ); - // Don't need these any more, should never appear in a post. - Shortcodes::unregister(); - return $content; } From 33b61ca2b97ad0e7e5afd8c647be81855b3332fa Mon Sep 17 00:00:00 2001 From: Matt Wiebe Date: Thu, 19 Oct 2023 14:46:31 -0500 Subject: [PATCH 3/5] Shortcodes: only register when needed (#526) --- activitypub.php | 1 - includes/class-shortcodes.php | 20 +++++++++++++------- includes/transformer/class-post.php | 6 ++++++ tests/test-class-activitypub-shortcodes.php | 7 +++++++ 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/activitypub.php b/activitypub.php index 521a379..f232c57 100644 --- a/activitypub.php +++ b/activitypub.php @@ -69,7 +69,6 @@ function plugin_init() { \add_action( 'init', array( __NAMESPACE__ . '\Collection\Followers', 'init' ) ); \add_action( 'init', array( __NAMESPACE__ . '\Admin', 'init' ) ); \add_action( 'init', array( __NAMESPACE__ . '\Hashtag', 'init' ) ); - \add_action( 'init', array( __NAMESPACE__ . '\Shortcodes', 'init' ) ); \add_action( 'init', array( __NAMESPACE__ . '\Mention', 'init' ) ); \add_action( 'init', array( __NAMESPACE__ . '\Health_Check', 'init' ) ); \add_action( 'init', array( __NAMESPACE__ . '\Scheduler', 'init' ) ); diff --git a/includes/class-shortcodes.php b/includes/class-shortcodes.php index 43be17b..708aa61 100644 --- a/includes/class-shortcodes.php +++ b/includes/class-shortcodes.php @@ -5,14 +5,9 @@ use function Activitypub\esc_hashtag; class Shortcodes { /** - * Class constructor, registering WordPress then Shortcodes + * Register the shortcodes */ - public static function init() { - // do not load on admin pages - if ( is_admin() ) { - return; - } - + public static function register() { foreach ( get_class_methods( self::class ) as $shortcode ) { if ( 'init' !== $shortcode ) { add_shortcode( 'ap_' . $shortcode, array( self::class, $shortcode ) ); @@ -20,6 +15,17 @@ class Shortcodes { } } + /** + * Unregister the shortcodes + */ + public static function unregister() { + foreach ( get_class_methods( self::class ) as $shortcode ) { + if ( 'init' !== $shortcode ) { + remove_shortcode( 'ap_' . $shortcode ); + } + } + } + /** * Generates output for the 'ap_hashtags' shortcode * diff --git a/includes/transformer/class-post.php b/includes/transformer/class-post.php index 6e2f0aa..9250afe 100644 --- a/includes/transformer/class-post.php +++ b/includes/transformer/class-post.php @@ -5,6 +5,7 @@ use WP_Post; use Activitypub\Collection\Users; use Activitypub\Model\Blog_User; use Activitypub\Activity\Base_Object; +use Activitypub\Shortcodes; use function Activitypub\esc_hashtag; use function Activitypub\is_single_user; @@ -466,6 +467,8 @@ class Post { $post = $this->wp_post; $content = $this->get_post_content_template(); + // Register our shortcodes just in time. + Shortcodes::register(); // Fill in the shortcodes. setup_postdata( $post ); $content = do_shortcode( $content ); @@ -477,6 +480,9 @@ class Post { $content = \apply_filters( 'activitypub_the_content', $content, $post ); + // Don't need these any more, should never appear in a post. + Shortcodes::unregister(); + return $content; } diff --git a/tests/test-class-activitypub-shortcodes.php b/tests/test-class-activitypub-shortcodes.php index 8637a61..b1413e8 100644 --- a/tests/test-class-activitypub-shortcodes.php +++ b/tests/test-class-activitypub-shortcodes.php @@ -1,6 +1,10 @@ assertEquals( '

hallo

', $content ); + Shortcodes::unregister(); } public function test_password_protected_content() { + Shortcodes::register(); global $post; $post_id = -98; // negative ID, to avoid clash with a valid post @@ -54,5 +60,6 @@ class Test_Activitypub_Shortcodes extends WP_UnitTestCase { wp_reset_postdata(); $this->assertEquals( '', $content ); + Shortcodes::unregister(); } } From a40bd8408a9115307a540741e41f65052ab962f7 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Sat, 21 Oct 2023 11:23:05 +0200 Subject: [PATCH 4/5] Various improvements (#527) * remove unused code * check if `$data['object']` is a sting * do not index application user * this fixes GoToSocial errors * do not cache errors * re-added the fragment See https://github.com/superseriousbusiness/gotosocial/issues/2280 * Fix coding standards * do not verify signature on head request --- includes/collection/class-followers.php | 10 ++-------- includes/functions.php | 13 +++---------- includes/model/class-application-user.php | 4 ++++ includes/rest/class-inbox.php | 2 +- includes/rest/class-server.php | 4 ++++ 5 files changed, 14 insertions(+), 19 deletions(-) diff --git a/includes/collection/class-followers.php b/includes/collection/class-followers.php index a9fe298..c2ad01f 100644 --- a/includes/collection/class-followers.php +++ b/includes/collection/class-followers.php @@ -173,8 +173,6 @@ class Followers { return new WP_Error( 'activitypub_invalid_follower', __( 'Invalid Follower', 'activitypub' ), array( 'status' => 400 ) ); } - $error = null; - $follower = new Follower(); $follower->from_array( $meta ); @@ -184,14 +182,10 @@ class Followers { return $id; } - $meta = get_post_meta( $id, 'activitypub_user_id' ); - - if ( $error ) { - self::add_error( $id, $error ); - } + $post_meta = get_post_meta( $id, 'activitypub_user_id' ); // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict - if ( is_array( $meta ) && ! in_array( $user_id, $meta ) ) { + if ( is_array( $post_meta ) && ! in_array( $user_id, $post_meta ) ) { add_post_meta( $id, 'activitypub_user_id', $user_id ); wp_cache_delete( sprintf( self::CACHE_KEY_INBOXES, $user_id ), 'activitypub' ); } diff --git a/includes/functions.php b/includes/functions.php index 883cd3f..99b433f 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -74,32 +74,25 @@ function get_remote_metadata_by_actor( $actor, $cached = true ) { if ( ! \wp_http_validate_url( $actor ) ) { $metadata = new WP_Error( 'activitypub_no_valid_actor_url', \__( 'The "actor" is no valid URL', 'activitypub' ), array( 'status' => 400, 'actor' => $actor ) ); - \set_transient( $transient_key, $metadata, HOUR_IN_SECONDS ); // Cache the error for a shorter period. return $metadata; } - $short_timeout = function() { - return 10; - }; - add_filter( 'activitypub_remote_get_timeout', $short_timeout ); $response = Http::get( $actor ); - remove_filter( 'activitypub_remote_get_timeout', $short_timeout ); + if ( \is_wp_error( $response ) ) { - \set_transient( $transient_key, $response, HOUR_IN_SECONDS ); // Cache the error for a shorter period. return $response; } $metadata = \wp_remote_retrieve_body( $response ); $metadata = \json_decode( $metadata, true ); - \set_transient( $transient_key, $metadata, WEEK_IN_SECONDS ); - if ( ! $metadata ) { $metadata = new WP_Error( 'activitypub_invalid_json', \__( 'No valid JSON data', 'activitypub' ), array( 'status' => 400, 'actor' => $actor ) ); - \set_transient( $transient_key, $metadata, HOUR_IN_SECONDS ); // Cache the error for a shorter period. return $metadata; } + \set_transient( $transient_key, $metadata, WEEK_IN_SECONDS ); + return $metadata; } diff --git a/includes/model/class-application-user.php b/includes/model/class-application-user.php index 1cfcec0..ae8b641 100644 --- a/includes/model/class-application-user.php +++ b/includes/model/class-application-user.php @@ -69,4 +69,8 @@ class Application_User extends Blog_User { public function get_moderators() { return null; } + + public function get_indexable() { + return false; + } } diff --git a/includes/rest/class-inbox.php b/includes/rest/class-inbox.php index 5d65b9b..9c5961a 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -416,7 +416,7 @@ class Inbox { $recipient_items = array_merge( $recipient_items, $recipient ); } - if ( array_key_exists( $i, $data['object'] ) ) { + if ( is_array( $data['object'] ) && array_key_exists( $i, $data['object'] ) ) { if ( is_array( $data['object'][ $i ] ) ) { $recipient = $data['object'][ $i ]; } else { diff --git a/includes/rest/class-server.php b/includes/rest/class-server.php index e1a1037..19d35b2 100644 --- a/includes/rest/class-server.php +++ b/includes/rest/class-server.php @@ -74,6 +74,10 @@ class Server { * @return mixed|WP_Error The response, error, or modified response. */ public static function authorize_activitypub_requests( $response, $handler, $request ) { + if ( 'HEAD' === $request->get_method() ) { + return $response; + } + $route = $request->get_route(); // check if it is an activitypub request and exclude webfinger and nodeinfo endpoints From 247899312acf5e88d920062eb3ee0c526c5966dc Mon Sep 17 00:00:00 2001 From: Chaitanya110703 <116812461+Chaitanya110703@users.noreply.github.com> Date: Mon, 23 Oct 2023 10:56:17 +0530 Subject: [PATCH 5/5] doc(README): remove typo (#528) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 93f3e85..f1c45e0 100644 --- a/README.md +++ b/README.md @@ -210,7 +210,7 @@ Project maintained on GitHub at [automattic/wordpress-activitypub](https://githu * Use shortcodes instead of custom templates, to setup the Activity Post-Content ([#250](https://github.com/pfefferle/wordpress-activitypub/pull/250)) props [@toolstack](https://github.com/toolstack) * Remove custom REST Server, because the needed changes are now merged into Core. * Fix hashtags ([#261](https://github.com/pfefferle/wordpress-activitypub/pull/261)) props [@akirk](https://github.com/akirk) -* Change priorites, to maybe fix the hashtag issue +* Change priorities, to maybe fix the hashtag issue ### 0.15.0 ###