Compare commits

...

9 commits

Author SHA1 Message Date
ed1b6b7e77 change hook name for registering transformers
follow WordPress coding style and coding style of this plugin
2023-11-22 14:58:06 +01:00
804cb0af1a fix phpcs errors 2023-11-22 14:24:25 +01:00
0bd6eccfed typo 2023-11-22 13:59:45 +01:00
c857eee616 transformer: move all related code to \Activitypub\Transformer namespace 2023-11-22 13:59:39 +01:00
Matthias Pfefferle
3a5b530111
Merge branch 'master' into add/extendable-transformers 2023-11-21 15:25:07 +01:00
Matthias Pfefferle
ba44ac701b remove var_dump 2023-11-21 15:05:47 +01:00
Matthias Pfefferle
1af821621b check if ID is set
fallback to URL
2023-11-21 15:05:12 +01:00
Matthias Pfefferle
d00e5a03c8 check if $resource is set 2023-11-21 15:00:39 +01:00
Matthias Pfefferle
60148a3b65 check if user is available 2023-11-21 14:57:44 +01:00
19 changed files with 154 additions and 139 deletions

View file

@ -33,7 +33,7 @@ require_once __DIR__ . '/includes/functions.php';
\defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "<strong>[ap_title]</strong>\n\n[ap_content]\n\n[ap_hashtags]\n\n[ap_shortlink]" ); \defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "<strong>[ap_title]</strong>\n\n[ap_content]\n\n[ap_hashtags]\n\n[ap_shortlink]" );
\defined( 'ACTIVITYPUB_AUTHORIZED_FETCH' ) || \define( 'ACTIVITYPUB_AUTHORIZED_FETCH', false ); \defined( 'ACTIVITYPUB_AUTHORIZED_FETCH' ) || \define( 'ACTIVITYPUB_AUTHORIZED_FETCH', false );
\defined( 'ACTIVITYPUB_DISABLE_REWRITES' ) || \define( 'ACTIVITYPUB_DISABLE_REWRITES', false ); \defined( 'ACTIVITYPUB_DISABLE_REWRITES' ) || \define( 'ACTIVITYPUB_DISABLE_REWRITES', false );
\defined( 'ACTIVITYPUB_DEFAULT_TRANSFORMER' ) || \define( 'ACTIVITYPUB_DEFAULT_TRANSFORMER', 'activitypub/defualt' ); \defined( 'ACTIVITYPUB_DEFAULT_TRANSFORMER' ) || \define( 'ACTIVITYPUB_DEFAULT_TRANSFORMER', 'activitypub/default' );
\define( 'ACTIVITYPUB_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );

View file

@ -19,5 +19,5 @@ use Activitypub\Activity\Base_Object;
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-event * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-event
*/ */
class Note extends Base_Object { class Note extends Base_Object {
protected $type = 'Event'; protected $type = 'Event';
} }

View file

@ -19,5 +19,5 @@ use Activitypub\Activity\Base_Object;
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-note * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-note
*/ */
class Note extends Base_Object { class Note extends Base_Object {
protected $type = 'Note'; protected $type = 'Note';
} }

View file

@ -5,7 +5,7 @@ use WP_Post;
use Activitypub\Activity\Activity; use Activitypub\Activity\Activity;
use Activitypub\Collection\Users; use Activitypub\Collection\Users;
use Activitypub\Collection\Followers; use Activitypub\Collection\Followers;
use Activitypub\Transformers_Manager; use Activitypub\Transformer\Transformers_Manager;
use function Activitypub\is_single_user; use function Activitypub\is_single_user;
use function Activitypub\is_user_disabled; use function Activitypub\is_user_disabled;
@ -65,7 +65,7 @@ class Activity_Dispatcher {
return; return;
} }
$transformer = \Activitypub\Transformers_Manager::instance()->get_transformer( $wp_post ); $transformer = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( $wp_post );
$object = $transformer->to_object(); $object = $transformer->to_object();
$activity = new Activity(); $activity = new Activity();
@ -102,7 +102,7 @@ class Activity_Dispatcher {
return; return;
} }
$transformer = \Activitypub\Transformers_Manager::instance()->get_transformer( $wp_post ); $transformer = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( $wp_post );
$object = $transformer->to_object(); $object = $transformer->to_object();
$activity = new Activity(); $activity = new Activity();

View file

@ -3,7 +3,7 @@ namespace Activitypub;
use WP_User_Query; use WP_User_Query;
use Activitypub\Model\Blog_User; use Activitypub\Model\Blog_User;
use Activitypub\Base\Transformer_Base; use Activitypub\Base\Transformer\Base as Transformer_Base;
/** /**
* ActivityPub Admin Class * ActivityPub Admin Class
@ -24,9 +24,14 @@ class Admin {
\add_action( 'show_user_profile', array( self::class, 'add_profile' ) ); \add_action( 'show_user_profile', array( self::class, 'add_profile' ) );
} }
add_filter( 'activitypub/transformers/is_transformer_enabled', function( $should_register, Transformer_Base $widget_instance ) { add_filter(
return ! Options::is_transformer_disabled( $transformer_instance->get_name() ); 'activitypub/transformers/is_transformer_enabled',
}, 10, 2 ); function( $should_register, Transformer_Base $transformer_instance ) {
return ! Options::is_transformer_disabled( $transformer_instance->get_name() );
},
10,
2
);
} }
/** /**
@ -162,8 +167,8 @@ class Admin {
/** /**
* Flexible activation of post_types together with mapping ActivityPub transformers. * Flexible activation of post_types together with mapping ActivityPub transformers.
* *
* If a post-type is not mapped to any ActivtiyPub transformer it means it is not activated * If a post-type is not mapped to any ActivtiyPub transformer it means it is not activated
* for ActivityPub federation. * for ActivityPub federation.
* *
* @since version_number_transformer_management_placeholder * @since version_number_transformer_management_placeholder
@ -186,7 +191,7 @@ class Admin {
), ),
'sanitize_callback' => function ( $value ) { 'sanitize_callback' => function ( $value ) {
// Check if $value is an array // Check if $value is an array
if (!is_array($value)) { if ( ! is_array( $value ) ) {
return array(); return array();
} }
$value_keys = array_keys( $value ); $value_keys = array_keys( $value );
@ -195,12 +200,11 @@ class Admin {
// Unset the keys that are missing in $keysToCheck // Unset the keys that are missing in $keysToCheck
foreach ( array_diff( $value_keys, $all_public_post_types ) as $missing_key ) { foreach ( array_diff( $value_keys, $all_public_post_types ) as $missing_key ) {
unset($value[$missing_key]); unset( $value[ $missing_key ] );
} }
// var_dump($value); // var_dump($value);
return $value; return $value;
},
}
) )
); );
\register_setting( \register_setting(

View file

@ -41,9 +41,14 @@ class Webfinger {
* @return string|WP_Error The URL or WP_Error * @return string|WP_Error The URL or WP_Error
*/ */
public static function resolve( $resource ) { public static function resolve( $resource ) {
if ( ! $resource ) {
return null;
}
if ( ! preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $resource, $m ) ) { if ( ! preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $resource, $m ) ) {
return null; return null;
} }
$transient_key = 'activitypub_resolve_' . ltrim( $resource, '@' ); $transient_key = 'activitypub_resolve_' . ltrim( $resource, '@' );
$link = \get_transient( $transient_key ); $link = \get_transient( $transient_key );

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Activitypub\Model; namespace Activitypub\Model;
use Activitypub\Transformer_Post as Post_Transformer; use Activitypub\Transformer\Post as Transformer_Post;
/** /**
* ActivityPub Post Class * ActivityPub Post Class
@ -31,10 +31,10 @@ class Post {
*/ */
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
public function __construct( $post, $post_author = null ) { public function __construct( $post, $post_author = null ) {
_deprecated_function( __CLASS__, '1.0.0', '\Activitypub\Transformer_Post' ); _deprecated_function( __CLASS__, '1.0.0', '\Activitypub\Transformer\Post' );
$this->post = $post; $this->post = $post;
$this->object = Post_Transformer->set_wp_post( $post )->to_object(); $this->object = Transformer_Post->set_wp_post( $post )->to_object();
} }
/** /**

View file

@ -4,7 +4,7 @@ namespace Activitypub\Rest;
use WP_Error; use WP_Error;
use WP_REST_Server; use WP_REST_Server;
use WP_REST_Response; use WP_REST_Response;
use Activitypub\Transformers_Manager; use Activitypub\Transformer\Transformers_Manager;
use Activitypub\Activity\Activity; use Activitypub\Activity\Activity;
use Activitypub\Collection\Users as User_Collection; use Activitypub\Collection\Users as User_Collection;

View file

@ -236,8 +236,12 @@ class Inbox {
$params['actor'] = array( $params['actor'] = array(
'required' => true, 'required' => true,
'sanitize_callback' => function( $param, $request, $key ) { 'sanitize_callback' => function( $param, $request, $key ) {
if ( ! \is_string( $param ) ) { if ( \is_array( $param ) ) {
$param = $param['id']; if ( isset( $param['id'] ) ) {
$param = $param['id'];
} else {
$param = $param['url'];
}
} }
return \esc_url_raw( $param ); return \esc_url_raw( $param );
}, },

View file

@ -5,7 +5,7 @@ use stdClass;
use WP_Error; use WP_Error;
use WP_REST_Server; use WP_REST_Server;
use WP_REST_Response; use WP_REST_Response;
use Activitypub\Transformers_Manager; use Activitypub\Transformer\Transformers_Manager;
use Activitypub\Activity\Activity; use Activitypub\Activity\Activity;
use Activitypub\Collection\Users as User_Collection; use Activitypub\Collection\Users as User_Collection;
@ -59,7 +59,7 @@ class Outbox {
return $user; return $user;
} }
$post_types = array_keys( \get_option( 'activitypub_transformer_mapping', array( 'post' => 'activitypub/default', 'page' => 'activitypub/default') ) ); $post_types = array_keys( \get_option( 'activitypub_transformer_mapping', array( 'post' => 'activitypub/default', 'page' => 'activitypub/default' ) ) );
$page = $request->get_param( 'page', 1 ); $page = $request->get_param( 'page', 1 );
@ -105,7 +105,7 @@ class Outbox {
); );
foreach ( $posts as $post ) { foreach ( $posts as $post ) {
$transformer = \Activitypub\Transformers_Manager::instance()->get_transformer( $post ); $transformer = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( $post );
$post = $transformer->to_object(); $post = $transformer->to_object();
$activity = new Activity(); $activity = new Activity();
$activity->set_type( 'Create' ); $activity->set_type( 'Create' );

View file

@ -4,15 +4,13 @@
* *
* @link https://github.com/landrok/activitypub * @link https://github.com/landrok/activitypub
*/ */
namespace Activitypub\Transformer;
namespace Activitypub;
use WP_Post; use WP_Post;
use Activitypub\Collection\Users; use Activitypub\Collection\Users;
use Activitypub\Model\Blog_User; use Activitypub\Model\Blog_User;
use Activitypub\Activity\Base_Object; use Activitypub\Activity\Base_Object;
use Activitypub\Shortcodes; use Activitypub\Shortcodes;
use Activitypub\Transformer_Base;
use function Activitypub\esc_hashtag; use function Activitypub\esc_hashtag;
use function Activitypub\is_single_user; use function Activitypub\is_single_user;
@ -22,7 +20,7 @@ use function Activitypub\site_supports_blocks;
/** /**
* Base class to implement WordPress to ActivityPub transformers. * Base class to implement WordPress to ActivityPub transformers.
*/ */
abstract class Transformer_Base { abstract class Base {
/** /**
* The WP_Post object. * The WP_Post object.
* *
@ -35,40 +33,43 @@ abstract class Transformer_Base {
* *
* This helps to chain the output of the Transformer. * This helps to chain the output of the Transformer.
* *
* @param WP_Post $wp_post The WP_Post object * @param WP_Post $wp_post The WP_Post object.
*
* @return void * @return void
*/ */
public function set_wp_post( WP_Post $wp_post ) { public function set_wp_post( WP_Post $wp_post ) {
if ( $this->supports_post_type( get_post_type( $wp_post ) ) ) { $post_type = get_post_type( $wp_post );
$this->wp_post = $wp_post; if ( ! $this->supports_post_type( $post_type ) ) {
} else { _doing_it_wrong(
//TODO Error, this should not happen. __METHOD__,
} /* translators: %s: Block name. */
sprintf( 'The Transformer "%s" does not support the post type "%s".', esc_html( $this->get_label() ), esc_html( $post_type ) ),
'version_number_transformer_management_placeholder'
);
}
$this->wp_post = $wp_post;
} }
/** /**
* Get the supported WP post_types that the transformer can use as an input. * Get the supported WP post types that the transformer can use as an input.
* *
* By default all post types are supported. * By default all post types are supported.
* You may very likely wish to override this function. * You may very likely wish to override this function.
* *
* @since version_number_transformer_management_placeholder * @since version_number_transformer_management_placeholder
* @return string[] An array containing all the supported post types. * @return string[] An array containing all the supported post types.
*/ */
public function get_supported_post_types() { public function get_supported_post_types() {
return \get_post_types( array( 'public' => true ), 'names' ); return \get_post_types( array( 'public' => true ), 'names' );
} }
/** /**
* Get the name of the plugin that registered the transformer. * Get the name of the plugin that registered the transformer.
* *
* @see Forked from the WordPress elementor plugin. * @see Forked from the WordPress elementor plugin.
*
* @since version_number_transformer_management_placeholder * @since version_number_transformer_management_placeholder
* @return string Plugin name * @return string Plugin name
*/ */
private function get_plugin_name_from_transformer_instance( $transformer ) { private function get_plugin_name_from_transformer_instance( $transformer ) {
$class_reflection = new \ReflectionClass( $transformer ); $class_reflection = new \ReflectionClass( $transformer );
$plugin_basename = plugin_basename( $class_reflection->getFileName() ); $plugin_basename = plugin_basename( $class_reflection->getFileName() );
@ -76,39 +77,40 @@ abstract class Transformer_Base {
$plugin_directory = strtok( $plugin_basename, '/' ); $plugin_directory = strtok( $plugin_basename, '/' );
$plugins_data = get_plugins( '/' . $plugin_directory ); $plugins_data = get_plugins( '/' . $plugin_directory );
$plugin_data = array_shift( $plugins_data ); $plugin_data = array_shift( $plugins_data );
return $plugin_data['Name'] ?? esc_html__( 'Unknown', 'activitypub' ); if ( isset( $plugin_data['Name'] ) ) {
return $plugin_data['Name'];
} else {
return esc_html__( 'Unknown', 'activitypub' );
}
} }
/** /**
* Return whether the transformer supports a post type. * Return whether the transformer supports a post type.
* *
* @since version_number_transformer_management_placeholder * @since version_number_transformer_management_placeholder
* * @return string post_type Post type name.
* @return string post_type Post type name.
*/ */
final public function supports_post_type( $post_type ) { final public function supports_post_type( $post_type ) {
return in_array( $post_type, $this->get_supported_post_types() ); return in_array( $post_type, $this->get_supported_post_types() );
} }
/** /**
* Get the name used for registering the transformer with the ActivityPub plugin. * Get the name used for registering the transformer with the ActivityPub plugin.
* *
* @since version_number_transformer_management_placeholder * @since version_number_transformer_management_placeholder
* * @return string name
* @return string name
*/ */
abstract public function get_name(); abstract public function get_name();
/** /**
* Get the display name for the ActivityPub transformer. * Get the display name for the ActivityPub transformer.
* *
* @since version_number_transformer_management_placeholder * @since version_number_transformer_management_placeholder
* * @return string display name
* @return string display name
*/ */
abstract public function get_label(); abstract public function get_label();
/** /**
* Returns the ActivityStreams 2.0 Object-Type for a Post. * Returns the ActivityStreams 2.0 Object-Type for a Post.
@ -118,7 +120,7 @@ abstract class Transformer_Base {
* @return string The Object-Type. * @return string The Object-Type.
*/ */
abstract protected function get_object_type(); abstract protected function get_object_type();
/** /**
* Returns the content for the ActivityPub Item. * Returns the content for the ActivityPub Item.
* *
@ -162,7 +164,7 @@ abstract class Transformer_Base {
} }
/** /**
* Returns the ID of the Post. * Returns the ID of the Post.
* *
* @return string The Posts ID. * @return string The Posts ID.
@ -244,7 +246,7 @@ abstract class Transformer_Base {
} }
if ( $max_media > 0 ) { if ( $max_media > 0 ) {
$blocks = \parse_blocks( $this->wp_post->post_content ); $blocks = \parse_blocks( $this->wp_post->post_content );
$media_ids = self::get_media_ids_from_blocks( $blocks, $media_ids, $max_media ); $media_ids = self::get_media_ids_from_blocks( $blocks, $media_ids, $max_media );
} }
@ -278,12 +280,12 @@ abstract class Transformer_Base {
if ( $max_images > 0 ) { if ( $max_images > 0 ) {
$query = new \WP_Query( $query = new \WP_Query(
array( array(
'post_parent' => $id, 'post_parent' => $id,
'post_status' => 'inherit', 'post_status' => 'inherit',
'post_type' => 'attachment', 'post_type' => 'attachment',
'post_mime_type' => 'image', 'post_mime_type' => 'image',
'order' => 'ASC', 'order' => 'ASC',
'orderby' => 'menu_order ID', 'orderby' => 'menu_order ID',
'posts_per_page' => $max_images, 'posts_per_page' => $max_images,
) )
); );
@ -300,9 +302,10 @@ abstract class Transformer_Base {
/** /**
* Recursively get media IDs from blocks. * Recursively get media IDs from blocks.
*
* @param array $blocks The blocks to search for media IDs * @param array $blocks The blocks to search for media IDs
* @param array $media_ids The media IDs to append new IDs to * @param array $media_ids The media IDs to append new IDs to
* @param int $max_media The maximum number of media to return. * @param int $max_media The maximum number of media to return.
* *
* @return array The image IDs. * @return array The image IDs.
*/ */
@ -361,8 +364,8 @@ abstract class Transformer_Base {
* @return array The ActivityPub Attachment. * @return array The ActivityPub Attachment.
*/ */
public static function wp_attachment_to_activity_attachment( $id ) { public static function wp_attachment_to_activity_attachment( $id ) {
$attachment = array(); $attachment = array();
$mime_type = \get_post_mime_type( $id ); $mime_type = \get_post_mime_type( $id );
$mime_type_parts = \explode( '/', $mime_type ); $mime_type_parts = \explode( '/', $mime_type );
// switching on image/audio/video // switching on image/audio/video
switch ( $mime_type_parts[0] ) { switch ( $mime_type_parts[0] ) {
@ -406,10 +409,10 @@ abstract class Transformer_Base {
'url' => \wp_get_attachment_url( $id ), 'url' => \wp_get_attachment_url( $id ),
'name' => \get_the_title( $id ), 'name' => \get_the_title( $id ),
); );
$meta = wp_get_attachment_metadata( $id ); $meta = wp_get_attachment_metadata( $id );
// height and width for videos // height and width for videos
if ( isset( $meta['width'] ) && isset( $meta['height'] ) ) { if ( isset( $meta['width'] ) && isset( $meta['height'] ) ) {
$attachment['width'] = $meta['width']; $attachment['width'] = $meta['width'];
$attachment['height'] = $meta['height']; $attachment['height'] = $meta['height'];
} }
// @todo: add `icon` support for audio/video attachments. Maybe use post thumbnail? // @todo: add `icon` support for audio/video attachments. Maybe use post thumbnail?
@ -460,12 +463,12 @@ abstract class Transformer_Base {
} }
/** /**
* Returns a list of Mentions, used in the Post. * Returns a list of Mentions, used in the Post.
* *
* @see https://docs.joinmastodon.org/spec/activitypub/#Mention * @see https://docs.joinmastodon.org/spec/activitypub/#Mention
* *
* @return array The list of Mentions. * @return array The list of Mentions.
*/ */
protected function get_cc() { protected function get_cc() {
$cc = array(); $cc = array();
@ -492,7 +495,7 @@ abstract class Transformer_Base {
$post_tags = \get_the_tags( $this->wp_post->ID ); $post_tags = \get_the_tags( $this->wp_post->ID );
if ( $post_tags ) { if ( $post_tags ) {
foreach ( $post_tags as $post_tag ) { foreach ( $post_tags as $post_tag ) {
$tag = array( $tag = array(
'type' => 'Hashtag', 'type' => 'Hashtag',
'href' => \esc_url( \get_tag_link( $post_tag->term_id ) ), 'href' => \esc_url( \get_tag_link( $post_tag->term_id ) ),
'name' => esc_hashtag( $post_tag->name ), 'name' => esc_hashtag( $post_tag->name ),
@ -504,7 +507,7 @@ abstract class Transformer_Base {
$mentions = $this->get_mentions(); $mentions = $this->get_mentions();
if ( $mentions ) { if ( $mentions ) {
foreach ( $mentions as $mention => $url ) { foreach ( $mentions as $mention => $url ) {
$tag = array( $tag = array(
'type' => 'Mention', 'type' => 'Mention',
'href' => \esc_url( $url ), 'href' => \esc_url( $url ),
'name' => \esc_html( $mention ), 'name' => \esc_html( $mention ),
@ -540,9 +543,9 @@ abstract class Transformer_Base {
/** /**
* Gets the contentMap * Gets the contentMap
* *
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-contentmap * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-contentmap
* *
* @return array the contenmap * @return array the contenmap
*/ */
protected function get_content_map() { protected function get_content_map() {
@ -560,7 +563,7 @@ abstract class Transformer_Base {
*/ */
public function to_object() { public function to_object() {
$wp_post = $this->wp_post; $wp_post = $this->wp_post;
$object = new Base_Object(); $object = new Base_Object();
$object->set_id( $this->get_id() ); $object->set_id( $this->get_id() );
$object->set_url( $this->get_url() ); $object->set_url( $this->get_url() );
@ -593,5 +596,4 @@ abstract class Transformer_Base {
return $object; return $object;
} }
}
}

View file

@ -1,12 +1,12 @@
<?php <?php
namespace Activitypub; namespace Activitypub\Transformer;
use WP_Post; use WP_Post;
use Activitypub\Collection\Users; use Activitypub\Collection\Users;
use Activitypub\Model\Blog_User; use Activitypub\Model\Blog_User;
use Activitypub\Activity\Base_Object; use Activitypub\Activity\Base_Object;
use Activitypub\Shortcodes; use Activitypub\Shortcodes;
use Activitypub\Transformer_Base; use Activitypub\Transformer\Base;
use function Activitypub\esc_hashtag; use function Activitypub\esc_hashtag;
use function Activitypub\is_single_user; use function Activitypub\is_single_user;
@ -18,12 +18,10 @@ use function Activitypub\site_supports_blocks;
* The Post Transformer is responsible for transforming a WP_Post object into different othe * The Post Transformer is responsible for transforming a WP_Post object into different othe
* Object-Types. * Object-Types.
* *
*
* Currently supported are: * Currently supported are:
*
* - Activitypub\Activity\Base_Object * - Activitypub\Activity\Base_Object
*/ */
class Transformer_Post extends Transformer_Base { class Post extends Base {
/** /**
* Getter function for the name of the transformer. * Getter function for the name of the transformer.
* *
@ -57,7 +55,7 @@ class Transformer_Post extends Transformer_Base {
// Default to Article. // Default to Article.
$object_type = 'Article'; $object_type = 'Article';
$post_type = \get_post_type( $this->wp_post ); $post_type = \get_post_type( $this->wp_post );
switch ( $post_type ) { switch ( $post_type ) {
case 'post': case 'post':
$post_format = \get_post_format( $this->wp_post ); $post_format = \get_post_format( $this->wp_post );
@ -87,7 +85,7 @@ class Transformer_Post extends Transformer_Base {
$object_type = 'Page'; $object_type = 'Page';
break; break;
case 'attachment': case 'attachment':
$mime_type = \get_post_mime_type(); $mime_type = \get_post_mime_type();
$media_type = \preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type ); $media_type = \preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type );
switch ( $media_type ) { switch ( $media_type ) {
case 'audio': case 'audio':
@ -129,5 +127,4 @@ class Transformer_Post extends Transformer_Base {
return \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT ); return \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
} }
} }

View file

@ -3,9 +3,10 @@
* Inspired by the way elementor handles addons. * Inspired by the way elementor handles addons.
* *
* @link https://github.com/elementor/elementor/ * @link https://github.com/elementor/elementor/
* @package Activitypub
*/ */
namespace Activitypub; namespace Activitypub\Transformer;
use WP_Post; use WP_Post;
use WP_Comment; use WP_Comment;
@ -25,7 +26,6 @@ if ( ! defined( 'ABSPATH' ) ) {
* *
* @since version_number_transformer_management_placeholder * @since version_number_transformer_management_placeholder
*/ */
class Transformers_Manager { class Transformers_Manager {
const DEFAULT_TRANSFORMER_MAPPING = array( const DEFAULT_TRANSFORMER_MAPPING = array(
'post' => ACTIVITYPUB_DEFAULT_TRANSFORMER, 'post' => ACTIVITYPUB_DEFAULT_TRANSFORMER,
@ -40,7 +40,7 @@ class Transformers_Manager {
* @since version_number_transformer_management_placeholder * @since version_number_transformer_management_placeholder
* @access private * @access private
* *
* @var \ActivityPub\Transformer_Base[] * @var \ActivityPub\Transformer\Base[]
*/ */
private $transformers = null; private $transformers = null;
@ -91,14 +91,14 @@ class Transformers_Manager {
return get_called_class(); return get_called_class();
} }
/** /**
* Transformers manager constructor. * Transformers manager constructor.
* *
* Initializing ActivityPub transformers manager. * Initializing ActivityPub transformers manager.
* *
* @since version_number_transformer_management_placeholder * @since version_number_transformer_management_placeholder
* @access public * @access public
*/ */
public function __construct() { public function __construct() {
$this->require_files(); $this->require_files();
} }
@ -110,9 +110,9 @@ class Transformers_Manager {
* *
* @since version_number_transformer_management_placeholder * @since version_number_transformer_management_placeholder
* @access private * @access private
*/ */
private function require_files() { private function require_files() {
require ACTIVITYPUB_PLUGIN_DIR . 'includes/class-transformer-base.php'; require ACTIVITYPUB_PLUGIN_DIR . 'includes/transformer/class-base.php';
} }
/** /**
@ -133,26 +133,26 @@ class Transformers_Manager {
* @since version_number_transformer_management_placeholder * @since version_number_transformer_management_placeholder
* @access public * @access public
* *
* @param \ActivityPub\Transformer_Base $transformer_instance ActivityPub Transformer. * @param \ActivityPub\Transformer\Base $transformer_instance ActivityPub Transformer.
* *
* @return bool True if the ActivityPub transformer was registered. * @return bool True if the ActivityPub transformer was registered.
*/ */
public function register( Transformer_Base $transformer_instance) { public function register( \ActivityPub\Transformer\Base $transformer_instance ) {
if ( ! $transformer_instance instanceof Transformer_Base ) { if ( ! $transformer_instance instanceof \ActivityPub\Transformer\Base ) {
_doing_it_wrong( _doing_it_wrong(
__METHOD__, __METHOD__,
__( 'ActivityPub transformer instance must be a of \ActivityPub\Transformer_Base class.' ), \esc_html__( 'ActivityPub transformer instance must be a of \ActivityPub\Transformer_Base class.' ),
'version_number_transformer_management_placeholder' 'version_number_transformer_management_placeholder'
); );
return false; return false;
} }
$transformer_name = $transformer_instance->get_name(); $transformer_name = $transformer_instance->get_name();
if ( preg_match( '/[A-Z]+/', $transformer_name ) ) { if ( preg_match( '/[A-Z]+/', $transformer_name ) ) {
_doing_it_wrong( _doing_it_wrong(
__METHOD__, __METHOD__,
__( 'ActivityPub transformer names must not contain uppercase characters.' ), \esc_html__( 'ActivityPub transformer names must not contain uppercase characters.' ),
'version_number_transformer_management_placeholder' 'version_number_transformer_management_placeholder'
); );
return false; return false;
@ -162,7 +162,7 @@ class Transformers_Manager {
if ( ! preg_match( $name_matcher, $transformer_name ) ) { if ( ! preg_match( $name_matcher, $transformer_name ) ) {
_doing_it_wrong( _doing_it_wrong(
__METHOD__, __METHOD__,
__( 'ActivityPub transformer names must contain a namespace prefix. Example: my-plugin/my-custom-transformer' ), \esc_html__( 'ActivityPub transformer names must contain a namespace prefix. Example: my-plugin/my-custom-transformer' ),
'version_number_transformer_management_placeholder' 'version_number_transformer_management_placeholder'
); );
return false; return false;
@ -172,11 +172,11 @@ class Transformers_Manager {
_doing_it_wrong( _doing_it_wrong(
__METHOD__, __METHOD__,
/* translators: %s: Block name. */ /* translators: %s: Block name. */
sprintf( __( 'ActivityPub transformer with name "%s" is already registered.' ), $transformer_name ), sprintf( 'ActivityPub transformer with name "%s" is already registered.', esc_html( $transformer_name ) ),
'version_number_transformer_management_placeholder' 'version_number_transformer_management_placeholder'
); );
return false; return false;
} }
/** /**
* Should the ActivityPub transformer be registered. * Should the ActivityPub transformer be registered.
@ -184,13 +184,13 @@ class Transformers_Manager {
* @since version_number_transformer_management_placeholder * @since version_number_transformer_management_placeholder
* *
* @param bool $should_register Should the ActivityPub transformer be registered. Default is `true`. * @param bool $should_register Should the ActivityPub transformer be registered. Default is `true`.
* @param \ActivityPub\Transformer_Base $transformer_instance Widget instance. * @param \ActivityPub\Transformer\Base $transformer_instance Widget instance.
*/ */
// TODO: does not implementing this slow down the website? -> compare with gutenberg block registration. // TODO: does not implementing this slow down the website? -> compare with gutenberg block registration.
// $should_register = apply_filters( 'activitypub/transformers/is_transformer_enabled', true, $transformer_instance ); // $should_register = apply_filters( 'activitypub/transformers/is_transformer_enabled', true, $transformer_instance );
// if ( ! $should_register ) { // if ( ! $should_register ) {
// return false; // return false;
// } // }
$this->transformers[ $transformer_name ] = $transformer_instance; $this->transformers[ $transformer_name ] = $transformer_instance;
@ -206,10 +206,10 @@ class Transformers_Manager {
* *
* @since version_number_transformer_management_placeholder * @since version_number_transformer_management_placeholder
* @access private * @access private
*/ */
private function init_transformers() { private function init_transformers() {
$builtin_transformers = [ $builtin_transformers = [
'post' 'post',
]; ];
$this->transformers = []; $this->transformers = [];
@ -219,7 +219,7 @@ class Transformers_Manager {
$class_name = ucfirst( $transformer_name ); $class_name = ucfirst( $transformer_name );
$class_name = '\Activitypub\Transformer_' . $class_name; $class_name = '\Activitypub\Transformer\\' . $class_name;
$this->register( new $class_name() ); $this->register( new $class_name() );
} }
@ -233,7 +233,7 @@ class Transformers_Manager {
* *
* @param Transformers_Manager $this The widgets manager. * @param Transformers_Manager $this The widgets manager.
*/ */
do_action( 'activitypub/transformers/register', $this ); do_action( 'activitypub_transformers_register', $this );
} }
@ -248,7 +248,7 @@ class Transformers_Manager {
* *
* @param string $transformers Optional. Transformer name. Default is null. * @param string $transformers Optional. Transformer name. Default is null.
* *
* @return Transformer_Base|Transformer_Base[]|null Registered transformers. * @return Base|Base[]|null Registered transformers.
*/ */
public function get_transformers( $transformer_name = null ) { public function get_transformers( $transformer_name = null ) {
if ( is_null( $this->transformers ) ) { if ( is_null( $this->transformers ) ) {
@ -264,15 +264,15 @@ class Transformers_Manager {
/** /**
* Get the mapped ActivityPub transformer. * Get the mapped ActivityPub transformer.
* *
* Returns a new instance of the needed WordPress to ActivityPub transformer. * Returns a new instance of the needed WordPress to ActivityPub transformer.
* *
* @since version_number_transformer_management_placeholder * @since version_number_transformer_management_placeholder
* @access public * @access public
* *
* @param WP_Post|WP_Comment $wp_post The WordPress Post/Comment. * @param WP_Post|WP_Comment $wp_post The WordPress Post/Comment.
* *
* @return Transformer_Base|null Registered transformers. * @return \ActivityPub\Transformer\Base|null Registered transformers.
*/ */
public function get_transformer( $object ) { public function get_transformer( $object ) {
switch ( get_class( $object ) ) { switch ( get_class( $object ) ) {
@ -289,5 +289,4 @@ class Transformers_Manager {
return apply_filters( 'activitypub_transformer', null, $object, get_class( $object ) ); return apply_filters( 'activitypub_transformer', null, $object, get_class( $object ) );
} }
} }
} }

View file

@ -30,6 +30,10 @@ class Webfinger {
public static function add_user_discovery( $array, $resource, $user ) { public static function add_user_discovery( $array, $resource, $user ) {
$user = User_Collection::get_by_id( $user->ID ); $user = User_Collection::get_by_id( $user->ID );
if ( ! $user || is_wp_error( $user ) ) {
return $array;
}
$array['links'][] = array( $array['links'][] = array(
'rel' => 'self', 'rel' => 'self',
'type' => 'application/activity+json', 'type' => 'application/activity+json',

View file

@ -2,7 +2,7 @@
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$post = \get_post(); $post = \get_post();
$transformer = \Activitypub\Transformers_Manager::instance()->get_transformer( $post ); $transformer = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( $post );
$json = \array_merge( array( '@context' => \Activitypub\get_context() ), $transformer->to_object()->to_array() ); $json = \array_merge( array( '@context' => \Activitypub\get_context() ), $transformer->to_object()->to_array() );

View file

@ -216,7 +216,7 @@
<?php \do_settings_fields( 'activitypub', 'activity' ); ?> <?php \do_settings_fields( 'activitypub', 'activity' ); ?>
</div> </div>
<!-- OUR FORK HERE --> <!-- OUR FORK HERE -->
<div class="box"> <div class="box">
<h3><?php \esc_html_e( 'Enable ActivityPub support for post type', 'activitypub' ); ?></h3> <h3><?php \esc_html_e( 'Enable ActivityPub support for post type', 'activitypub' ); ?></h3>
@ -237,19 +237,19 @@
return $object->name; return $object->name;
}, $all_public_post_types); }, $all_public_post_types);
$transformer_manager = \Activitypub\Transformers_Manager::instance(); $transformer_manager = \Activitypub\Transformer\Transformers_Manager::instance();
$transformers = $transformer_manager->get_transformers(); $transformers = $transformer_manager->get_transformers();
?> ?>
<script> <script>
// TODO Probably we should use checkboxes and not select and make this less buggy and insert the js at the right place. // TODO Probably we should use checkboxes and not select and make this less buggy and insert the js at the right place.
document.addEventListener('DOMContentLoaded', function () { document.addEventListener('DOMContentLoaded', function () {
var radioGroups = {}; var radioGroups = {};
var radioButtons = document.querySelectorAll('input[type="radio"]'); var radioButtons = document.querySelectorAll('input[type="radio"]');
radioButtons.forEach(function (radioButton) { radioButtons.forEach(function (radioButton) {
radioButton.addEventListener('click', function () { radioButton.addEventListener('click', function () {
var name = this.name; var name = this.name;
@ -286,7 +286,7 @@
// Generate radio inputs for each transformer, considering support for the post type // Generate radio inputs for each transformer, considering support for the post type
foreach ($transformers as $transformer) { foreach ($transformers as $transformer) {
$disabled_attribute = $transformer->supports_post_type( $post_type->name ) ? '' : ' disabled'; $disabled_attribute = $transformer->supports_post_type( $post_type->name ) ? '' : ' disabled';
$is_selected = ( is_array( $transformer_mapping ) && isset( $transformer_mapping[ $post_type->name ] ) && $transformer_mapping[ $post_type->name ] === $transformer->get_name() ) ? ' checked ' : ''; $is_selected = ( is_array( $transformer_mapping ) && isset( $transformer_mapping[ $post_type->name ] ) && $transformer_mapping[ $post_type->name ] === $transformer->get_name() ) ? ' checked ' : '';
echo '<td><input type="radio" name="activitypub_transformer_mapping[' . $post_type->name . ']" value="' . $transformer->get_name() . '"' . $is_selected . $disabled_attribute . '></td>'; echo '<td><input type="radio" name="activitypub_transformer_mapping[' . $post_type->name . ']" value="' . $transformer->get_name() . '"' . $is_selected . $disabled_attribute . '></td>';
} }
@ -296,7 +296,7 @@
</tbody> </tbody>
</table> </table>
</td> </td>
</tr> </tr>
</tbody> </tbody>

View file

@ -16,9 +16,9 @@ class Test_Activitypub_Activity extends WP_UnitTestCase {
}, },
10 10
); );
$wp_post = get_post( $post ); $wp_post = get_post( $post );
$activitypub_post = \Activitypub\Transformers_Manager::instance()->get_transformer( $post )->to_object(); $activitypub_post = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( $post )->to_object();
$activitypub_activity = new \Activitypub\Activity\Activity(); $activitypub_activity = new \Activitypub\Activity\Activity();
$activitypub_activity->set_type( 'Create' ); $activitypub_activity->set_type( 'Create' );

View file

@ -10,13 +10,13 @@ class Test_Activitypub_Post extends WP_UnitTestCase {
$permalink = \get_permalink( $post ); $permalink = \get_permalink( $post );
$activitypub_post = \Activitypub\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object(); $activitypub_post = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object();
$this->assertEquals( $permalink, $activitypub_post->get_id() ); $this->assertEquals( $permalink, $activitypub_post->get_id() );
\wp_trash_post( $post ); \wp_trash_post( $post );
$activitypub_post = \Activitypub\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object(); $activitypub_post = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object();
$this->assertEquals( $permalink, $activitypub_post->get_id() ); $this->assertEquals( $permalink, $activitypub_post->get_id() );

View file

@ -10,7 +10,7 @@ class Test_Activitypub_Signature_Verification extends WP_UnitTestCase {
) )
); );
$remote_actor = \get_author_posts_url( 2 ); $remote_actor = \get_author_posts_url( 2 );
$activitypub_post = \Activitypub\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object(); $activitypub_post = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object();
$activitypub_activity = new Activitypub\Activity\Activity( 'Create' ); $activitypub_activity = new Activitypub\Activity\Activity( 'Create' );
$activitypub_activity->set_type( 'Create' ); $activitypub_activity->set_type( 'Create' );
$activitypub_activity->set_object( $activitypub_post ); $activitypub_activity->set_object( $activitypub_post );
@ -82,7 +82,7 @@ class Test_Activitypub_Signature_Verification extends WP_UnitTestCase {
); );
$remote_actor = \get_author_posts_url( 2 ); $remote_actor = \get_author_posts_url( 2 );
$remote_actor_inbox = Activitypub\get_rest_url_by_path( '/inbox' ); $remote_actor_inbox = Activitypub\get_rest_url_by_path( '/inbox' );
$activitypub_post = \Activitypub\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object(); $activitypub_post = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object();
$activitypub_activity = new Activitypub\Activity\Activity(); $activitypub_activity = new Activitypub\Activity\Activity();
$activitypub_activity->set_type( 'Create' ); $activitypub_activity->set_type( 'Create' );
$activitypub_activity->set_object( $activitypub_post ); $activitypub_activity->set_object( $activitypub_post );