From 444c4b283716509524985c0d5e02e017f66fbc74 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 27 Sep 2023 11:05:11 +0200 Subject: [PATCH] Fixes PHP warnings and remote delete (#468) * fix #463 * fix delete /cc #465 @janboddez * add disclaimer to not use the same name as an author login see #470 * check if url is cached before trashing it --- includes/class-signature.php | 7 +++++-- includes/transformer/class-post.php | 30 +++++++++++++++++++++++++-- templates/settings.php | 5 +++++ tests/test-class-activitypub-post.php | 4 ++++ 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/includes/class-signature.php b/includes/class-signature.php index 10cbf39..1789dd6 100644 --- a/includes/class-signature.php +++ b/includes/class-signature.php @@ -240,8 +240,11 @@ class Signature { } // fix route for subdirectory installs - $path = wp_parse_url( get_home_url(), PHP_URL_PATH ); - $path = trim( $path, '/' ); + $path = \wp_parse_url( \get_home_url(), PHP_URL_PATH ); + + if ( \is_string( $path ) ) { + $path = trim( $path, '/' ); + } if ( $path ) { $route = '/' . $path . $route; diff --git a/includes/transformer/class-post.php b/includes/transformer/class-post.php index 0e1463a..286b91f 100644 --- a/includes/transformer/class-post.php +++ b/includes/transformer/class-post.php @@ -115,8 +115,8 @@ class Post { $wp_post = $this->wp_post; $object = new Base_Object(); - $object->set_id( \esc_url( \get_permalink( $wp_post->ID ) ) ); - $object->set_url( \esc_url( \get_permalink( $wp_post->ID ) ) ); + $object->set_id( $this->get_id() ); + $object->set_url( $this->get_url() ); $object->set_type( $this->get_object_type() ); $published = \strtotime( $wp_post->post_date_gmt ); @@ -151,6 +151,32 @@ class Post { return $object; } + /** + * Returns the ID of the Post. + * + * @return string The Posts ID. + */ + public function get_id() { + return $this->get_url(); + } + + /** + * Returns the URL of the Post. + * + * @return string The Posts URL. + */ + public function get_url() { + $post = $this->wp_post; + + if ( 'trash' === get_post_status( $post ) ) { + $permalink = \get_post_meta( $post->ID, 'activitypub_canonical_url', true ); + } else { + $permalink = \get_permalink( $post ); + } + + return \esc_url( $permalink ); + } + /** * Returns the User-URL of the Author of the Post. * diff --git a/templates/settings.php b/templates/settings.php index 18821df..fd80145 100644 --- a/templates/settings.php +++ b/templates/settings.php @@ -55,6 +55,11 @@

+

+ + + +

diff --git a/tests/test-class-activitypub-post.php b/tests/test-class-activitypub-post.php index e995afa..0b5ee6d 100644 --- a/tests/test-class-activitypub-post.php +++ b/tests/test-class-activitypub-post.php @@ -19,5 +19,9 @@ class Test_Activitypub_Post extends WP_UnitTestCase { $activitypub_post = \Activitypub\Transformer\Post::transform( get_post( $post ) )->to_object(); $this->assertEquals( $permalink, $activitypub_post->get_id() ); + + $cached = \get_post_meta( $post, 'activitypub_canonical_url', true ); + + $this->assertEquals( $cached, $activitypub_post->get_id() ); } }