diff --git a/activitypub.php b/activitypub.php index 4a202d3..0c73efe 100644 --- a/activitypub.php +++ b/activitypub.php @@ -33,7 +33,7 @@ require_once __DIR__ . '/includes/functions.php'; \defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "[ap_title]\n\n[ap_content]\n\n[ap_hashtags]\n\n[ap_shortlink]" ); \defined( 'ACTIVITYPUB_AUTHORIZED_FETCH' ) || \define( 'ACTIVITYPUB_AUTHORIZED_FETCH', 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__ ) ); diff --git a/includes/activity/class-event.php b/includes/activity/class-event.php index a38880c..9359336 100644 --- a/includes/activity/class-event.php +++ b/includes/activity/class-event.php @@ -19,5 +19,5 @@ use Activitypub\Activity\Base_Object; * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-event */ class Note extends Base_Object { - protected $type = 'Event'; + protected $type = 'Event'; } diff --git a/includes/activity/class-note.php b/includes/activity/class-note.php index d49be5c..328ec79 100644 --- a/includes/activity/class-note.php +++ b/includes/activity/class-note.php @@ -19,5 +19,5 @@ use Activitypub\Activity\Base_Object; * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-note */ class Note extends Base_Object { - protected $type = 'Note'; + protected $type = 'Note'; } diff --git a/includes/class-admin.php b/includes/class-admin.php index 1105a0a..b5000ff 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -25,9 +25,13 @@ class Admin { } add_filter( - 'activitypub/transformers/is_transformer_enabled', function( $should_register, Transformer_Base $widget_instance ) { - return ! Options::is_transformer_disabled( $transformer_instance->get_name() ); - }, 10, 2 ); + 'activitypub/transformers/is_transformer_enabled', + function( $should_register, Transformer_Base $transformer_instance ) { + return ! Options::is_transformer_disabled( $transformer_instance->get_name() ); + }, + 10, + 2 + ); } /** @@ -187,7 +191,7 @@ class Admin { ), 'sanitize_callback' => function ( $value ) { // Check if $value is an array - if (!is_array($value)) { + if ( ! is_array( $value ) ) { return array(); } $value_keys = array_keys( $value ); @@ -196,12 +200,11 @@ class Admin { // Unset the keys that are missing in $keysToCheck foreach ( array_diff( $value_keys, $all_public_post_types ) as $missing_key ) { - unset($value[$missing_key]); + unset( $value[ $missing_key ] ); } // var_dump($value); return $value; - - } + }, ) ); \register_setting( diff --git a/includes/class-webfinger.php b/includes/class-webfinger.php index b6b0a64..75f7ff6 100644 --- a/includes/class-webfinger.php +++ b/includes/class-webfinger.php @@ -41,9 +41,14 @@ class Webfinger { * @return string|WP_Error The URL or WP_Error */ public static function resolve( $resource ) { + if ( ! $resource ) { + return null; + } + if ( ! preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $resource, $m ) ) { return null; } + $transient_key = 'activitypub_resolve_' . ltrim( $resource, '@' ); $link = \get_transient( $transient_key ); diff --git a/includes/model/class-post.php b/includes/model/class-post.php index ac451d0..6aeea14 100644 --- a/includes/model/class-post.php +++ b/includes/model/class-post.php @@ -1,7 +1,7 @@ post = $post; - $this->object = Post_Transformer->set_wp_post( $post )->to_object(); + $this->object = Transformer_Post->set_wp_post( $post )->to_object(); } /** diff --git a/includes/rest/class-inbox.php b/includes/rest/class-inbox.php index 2cedf15..15fb4b2 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -236,8 +236,12 @@ class Inbox { $params['actor'] = array( 'required' => true, 'sanitize_callback' => function( $param, $request, $key ) { - if ( ! \is_string( $param ) ) { - $param = $param['id']; + if ( \is_array( $param ) ) { + if ( isset( $param['id'] ) ) { + $param = $param['id']; + } else { + $param = $param['url']; + } } return \esc_url_raw( $param ); }, diff --git a/includes/rest/class-outbox.php b/includes/rest/class-outbox.php index e5ff834..56f5220 100644 --- a/includes/rest/class-outbox.php +++ b/includes/rest/class-outbox.php @@ -59,7 +59,7 @@ class Outbox { 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 ); diff --git a/includes/transformer/class-base.php b/includes/transformer/class-base.php index d4062a9..27f4a67 100644 --- a/includes/transformer/class-base.php +++ b/includes/transformer/class-base.php @@ -37,15 +37,20 @@ abstract class Base { * @return void */ public function set_wp_post( WP_Post $wp_post ) { - if ( $this->supports_post_type( get_post_type( $wp_post ) ) ) { - $this->wp_post = $wp_post; - } else { - // TODO Error, this should not happen. + $post_type = get_post_type( $wp_post ); + if ( ! $this->supports_post_type( $post_type ) ) { + _doing_it_wrong( + __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. * You may very likely wish to override this function. @@ -74,7 +79,11 @@ abstract class Base { $plugins_data = get_plugins( '/' . $plugin_directory ); $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' ); + } } /** diff --git a/includes/transformer/class-transformers-manager.php b/includes/transformer/class-transformers-manager.php index 9d8de30..f714890 100644 --- a/includes/transformer/class-transformers-manager.php +++ b/includes/transformer/class-transformers-manager.php @@ -91,7 +91,7 @@ class Transformers_Manager { return get_called_class(); } - /** + /** * Transformers manager constructor. * * Initializing ActivityPub transformers manager. @@ -137,12 +137,16 @@ class Transformers_Manager { * * @return bool True if the ActivityPub transformer was registered. */ +<<<<<<< HEAD public function register( \ActivityPub\Transformer\Base $transformer_instance) { +======= + public function register( \ActivityPub\Transformer\Base $transformer_instance ) { +>>>>>>> ed1b6b7e77ed12f77fb1a9b4bf9f959509b55bf9 if ( ! $transformer_instance instanceof \ActivityPub\Transformer\Base ) { _doing_it_wrong( __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' ); return false; @@ -152,7 +156,7 @@ class Transformers_Manager { if ( preg_match( '/[A-Z]+/', $transformer_name ) ) { _doing_it_wrong( __METHOD__, - __( 'ActivityPub transformer names must not contain uppercase characters.' ), + \esc_html__( 'ActivityPub transformer names must not contain uppercase characters.' ), 'version_number_transformer_management_placeholder' ); return false; @@ -162,7 +166,7 @@ class Transformers_Manager { if ( ! preg_match( $name_matcher, $transformer_name ) ) { _doing_it_wrong( __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' ); return false; @@ -172,7 +176,7 @@ class Transformers_Manager { _doing_it_wrong( __METHOD__, /* 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' ); return false; @@ -190,7 +194,7 @@ class Transformers_Manager { // $should_register = apply_filters( 'activitypub/transformers/is_transformer_enabled', true, $transformer_instance ); // if ( ! $should_register ) { - // return false; + // return false; // } $this->transformers[ $transformer_name ] = $transformer_instance; @@ -209,7 +213,7 @@ class Transformers_Manager { */ private function init_transformers() { $builtin_transformers = [ - 'post' + 'post', ]; $this->transformers = []; @@ -233,7 +237,7 @@ class Transformers_Manager { * * @param Transformers_Manager $this The widgets manager. */ - do_action( 'activitypub/transformers/register', $this ); + do_action( 'activitypub_transformers_register', $this ); } @@ -270,7 +274,7 @@ class Transformers_Manager { * @since version_number_transformer_management_placeholder * @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 \ActivityPub\Transformer\Base|null Registered transformers. */ @@ -290,4 +294,3 @@ class Transformers_Manager { } } } - diff --git a/integration/class-webfinger.php b/integration/class-webfinger.php index 177b417..c9dd565 100644 --- a/integration/class-webfinger.php +++ b/integration/class-webfinger.php @@ -30,6 +30,10 @@ class Webfinger { public static function add_user_discovery( $array, $resource, $user ) { $user = User_Collection::get_by_id( $user->ID ); + if ( ! $user || is_wp_error( $user ) ) { + return $array; + } + $array['links'][] = array( 'rel' => 'self', 'type' => 'application/activity+json',