From 5addca1aff5637e3025975f7ba36e6139f71987b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Mon, 4 Dec 2023 19:42:38 +0100 Subject: [PATCH] transformer: make get_supported_post_types static and refactor --- includes/transformer/class-base.php | 95 +++++++++++++++-------------- 1 file changed, 49 insertions(+), 46 deletions(-) diff --git a/includes/transformer/class-base.php b/includes/transformer/class-base.php index 532d9c6..cb89ee2 100644 --- a/includes/transformer/class-base.php +++ b/includes/transformer/class-base.php @@ -19,6 +19,11 @@ use function Activitypub\site_supports_blocks; /** * Base class to implement WordPress to ActivityPub transformers. + * + * All have to be extended from this one. It provides some WordPress typical + * getter functions, that you might want to use: + * + * */ abstract class Base { /** @@ -28,6 +33,41 @@ abstract class Base { */ protected $wp_post; + /** + * Get the name used for registering the transformer with the ActivityPub plugin. + * + * @since version_number_transformer_management_placeholder + * @return string name + */ + abstract public function get_name(); + + /** + * Get the display name for the ActivityPub transformer. + * + * @since version_number_transformer_management_placeholder + * @return string display name + */ + abstract public function get_label(); + + /** + * Returns the ActivityStreams 2.0 Object-Type for a Post. + * + * @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types + * + * @return string The Object-Type. + */ + abstract protected function get_object_type(); + + /** + * Transforms the WP_Post object to an ActivityPub Object + * + * @see \Activitypub\Activity\Base_Object + * @param WP_Post $wp_post The WordPress Post + * + * @return \Activitypub\Activity\Base_Object The ActivityPub Object + */ + abstract public function transform(); + /** * Getter function for this wp_post. * @@ -67,7 +107,7 @@ abstract class Base { * @since version_number_transformer_management_placeholder * @return string[] An array containing all the supported post types. */ - public function get_supported_post_types() { + public static function get_supported_post_types() { return \get_post_types( array( 'public' => true ), 'names' ); } @@ -78,7 +118,7 @@ abstract class Base { * @since version_number_transformer_management_placeholder * @return string Plugin name */ - private function get_plugin_name_from_transformer_instance( $transformer ) { + final private function get_plugin_name_from_transformer_instance( $transformer ) { $class_reflection = new \ReflectionClass( $transformer ); $plugin_basename = plugin_basename( $class_reflection->getFileName() ); @@ -105,31 +145,6 @@ abstract class Base { return in_array( $post_type, $this->get_supported_post_types(), true ); } - /** - * Get the name used for registering the transformer with the ActivityPub plugin. - * - * @since version_number_transformer_management_placeholder - * @return string name - */ - abstract public function get_name(); - - /** - * Get the display name for the ActivityPub transformer. - * - * @since version_number_transformer_management_placeholder - * @return string display name - */ - abstract public function get_label(); - - /** - * Returns the ActivityStreams 2.0 Object-Type for a Post. - * - * @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types - * - * @return string The Object-Type. - */ - abstract protected function get_object_type(); - /** * Returns the content for the ActivityPub Item. * @@ -481,7 +496,6 @@ abstract class Base { return $image; } - /** * Helper function to get the @-Mentions from the post content. * @@ -491,13 +505,13 @@ abstract class Base { return apply_filters( 'activitypub_extract_mentions', array(), $this->wp_post->post_content, $this->wp_post ); } - /** - * Returns a list of Mentions, used in the Post. - * - * @see https://docs.joinmastodon.org/spec/activitypub/#Mention - * - * @return array The list of Mentions. - */ + /** + * Returns a list of Mentions, used in the Post. + * + * @see https://docs.joinmastodon.org/spec/activitypub/#Mention + * + * @return array The list of Mentions. + */ protected function get_cc() { $cc = array(); @@ -548,7 +562,6 @@ abstract class Base { return $tags; } - /** * Returns the locale of the post. * @@ -582,14 +595,4 @@ abstract class Base { $this->get_locale() => $this->get_content(), ); } - - /** - * Transforms the WP_Post object to an ActivityPub Object - * - * @see \Activitypub\Activity\Base_Object - * @param WP_Post $wp_post The WordPress Post - * - * @return \Activitypub\Activity\Base_Object The ActivityPub Object - */ - abstract public function transform(); }