Merge branch 'add/extendable-transformers' of ssh://code.event-federation.eu:2222/Event-Federation/wordpress-activitypub into add/extendable-transformers

This commit is contained in:
André Menrath 2023-11-25 10:24:45 +01:00
commit 2a7929719b
11 changed files with 59 additions and 31 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

@ -25,9 +25,13 @@ class Admin {
} }
add_filter( add_filter(
'activitypub/transformers/is_transformer_enabled', function( $should_register, Transformer_Base $widget_instance ) { 'activitypub/transformers/is_transformer_enabled',
function( $should_register, Transformer_Base $transformer_instance ) {
return ! Options::is_transformer_disabled( $transformer_instance->get_name() ); return ! Options::is_transformer_disabled( $transformer_instance->get_name() );
}, 10, 2 ); },
10,
2
);
} }
/** /**
@ -200,8 +204,7 @@ class Admin {
} }
// 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; use Activitypub\Transformer\Post as Transformer_Post;
/** /**
* ActivityPub Post Class * ActivityPub Post Class
@ -34,7 +34,7 @@ class Post {
_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

@ -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 ) ) {
if ( isset( $param['id'] ) ) {
$param = $param['id']; $param = $param['id'];
} else {
$param = $param['url'];
}
} }
return \esc_url_raw( $param ); return \esc_url_raw( $param );
}, },

View file

@ -37,15 +37,20 @@ abstract class Base {
* @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.
@ -74,7 +79,11 @@ abstract class Base {
$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' );
}
} }
/** /**

View file

@ -137,12 +137,16 @@ class Transformers_Manager {
* *
* @return bool True if the ActivityPub transformer was registered. * @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) {
=======
public function register( \ActivityPub\Transformer\Base $transformer_instance ) {
>>>>>>> ed1b6b7e77ed12f77fb1a9b4bf9f959509b55bf9
if ( ! $transformer_instance instanceof \ActivityPub\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;
@ -152,7 +156,7 @@ class Transformers_Manager {
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 +166,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,7 +176,7 @@ 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;
@ -209,7 +213,7 @@ class Transformers_Manager {
*/ */
private function init_transformers() { private function init_transformers() {
$builtin_transformers = [ $builtin_transformers = [
'post' 'post',
]; ];
$this->transformers = []; $this->transformers = [];
@ -233,7 +237,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 );
} }
@ -290,4 +294,3 @@ class Transformers_Manager {
} }
} }
} }

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',