Compare commits

..

13 commits

27 changed files with 205 additions and 305 deletions

View file

@ -3,7 +3,7 @@
**Tags:** OStatus, fediverse, activitypub, activitystream **Tags:** OStatus, fediverse, activitypub, activitystream
**Requires at least:** 4.7 **Requires at least:** 4.7
**Tested up to:** 6.4 **Tested up to:** 6.4
**Stable tag:** 1.2.0 **Stable tag:** 1.1.0
**Requires PHP:** 5.6 **Requires PHP:** 5.6
**License:** MIT **License:** MIT
**License URI:** http://opensource.org/licenses/MIT **License URI:** http://opensource.org/licenses/MIT
@ -105,15 +105,6 @@ Where 'blog' is the path to the subdirectory at which your blog resides.
Project maintained on GitHub at [automattic/wordpress-activitypub](https://github.com/automattic/wordpress-activitypub). Project maintained on GitHub at [automattic/wordpress-activitypub](https://github.com/automattic/wordpress-activitypub).
### 1.2.0 ###
* Add: Search and order followerer lists
* Add: Have a filter to defer signature verification
* Improved: "Follow Me" styles for dark themes
* Improved: Allow `p` and `br` tags only for AP comments
* Fixed: Deduplicate attachments earlier to prevent incorrect max_media
### 1.1.0 ### ### 1.1.0 ###
* Improved: audio and video attachments are now supported! * Improved: audio and video attachments are now supported!

View file

@ -3,7 +3,7 @@
* Plugin Name: ActivityPub * Plugin Name: ActivityPub
* Plugin URI: https://github.com/pfefferle/wordpress-activitypub/ * Plugin URI: https://github.com/pfefferle/wordpress-activitypub/
* Description: The ActivityPub protocol is a decentralized social networking protocol based upon the ActivityStreams 2.0 data format. * Description: The ActivityPub protocol is a decentralized social networking protocol based upon the ActivityStreams 2.0 data format.
* Version: 1.2.0 * Version: 1.1.0
* Author: Matthias Pfefferle & Automattic * Author: Matthias Pfefferle & Automattic
* Author URI: https://automattic.com/ * Author URI: https://automattic.com/
* License: MIT * License: MIT
@ -33,15 +33,12 @@ 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/default' );
\define( 'ACTIVITYPUB_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
\define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
\define( 'ACTIVITYPUB_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) );
\define( 'ACTIVITYPUB_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
/** /**
* Initialize REST routes. * Initialize REST routes.
*/ */

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\Transformer\Transformers_Manager; use Activitypub\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,8 +65,9 @@ class Activity_Dispatcher {
return; return;
} }
$transformer = Transformers_Manager::instance()->get_transformer( $wp_post ); $transformer = Transformers_Manager::get_transformer( $wp_post );
$object = $transformer->to_object(); $transformer->transform( $wp_post );
$transformer->to_object();
$activity = new Activity(); $activity = new Activity();
$activity->set_type( $type ); $activity->set_type( $type );
@ -102,8 +103,9 @@ class Activity_Dispatcher {
return; return;
} }
$transformer = Transformers_Manager::instance()->get_transformer( $wp_post ); $transformer = Transformers_Manager::get_transformer( $wp_post );
$object = $transformer->to_object(); $transformer->transform( $wp_post );
$transformer->to_object();
$activity = new Activity(); $activity = new Activity();
$activity->set_type( 'Announce' ); $activity->set_type( 'Announce' );

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 as Transformer_Base; use Activitypub\Base\Transformer_Base;
/** /**
* ActivityPub Admin Class * ActivityPub Admin Class
@ -24,14 +24,9 @@ class Admin {
\add_action( 'show_user_profile', array( self::class, 'add_profile' ) ); \add_action( 'show_user_profile', array( self::class, 'add_profile' ) );
} }
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
);
} }
/** /**
@ -204,7 +199,8 @@ class Admin {
} }
// var_dump($value); // var_dump($value);
return $value; return $value;
},
}
) )
); );
\register_setting( \register_setting(

View file

@ -4,7 +4,6 @@ namespace Activitypub;
use Activitypub\Activitypub; use Activitypub\Activitypub;
use Activitypub\Model\Blog_User; use Activitypub\Model\Blog_User;
use Activitypub\Collection\Followers; use Activitypub\Collection\Followers;
use Activitypub\Admin;
/** /**
* ActivityPub Migration Class * ActivityPub Migration Class
@ -115,30 +114,12 @@ class Migration {
if ( version_compare( $version_from_db, '1.0.0', '<' ) ) { if ( version_compare( $version_from_db, '1.0.0', '<' ) ) {
self::migrate_from_0_17(); self::migrate_from_0_17();
} }
if ( version_compare( $version_from_db, 'version_number_transformer_management_placeholder', '<' ) ) {
self::migrate_from_version_number_transformer_management_placeholder();
}
update_option( 'activitypub_db_version', self::get_target_version() ); update_option( 'activitypub_db_version', self::get_target_version() );
self::unlock(); self::unlock();
} }
/**
* Updates the supported post type settings to the mapped transformer setting.
* TODO: Test this
* @return void
*/
private static function migrate_from_version_number_transformer_management_placeholder() {
$supported_post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) );
Admin::register_settings();
$transformer_mapping = array();
foreach ( $supported_post_types as $supported_post_type ) {
$transformer_mapping[ $supported_post_type ] = ACTIVITYPUB_DEFAULT_TRANSFORMER;
}
update_option( 'activitypub_transformer_mapping', $transformer_mapping );
}
/** /**
* Updates the DB-schema of the followers-list * Updates the DB-schema of the followers-list
* *

View file

@ -4,13 +4,15 @@
* *
* @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;
@ -20,7 +22,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 Base { abstract class Transformer_Base {
/** /**
* The WP_Post object. * The WP_Post object.
* *
@ -33,24 +35,20 @@ abstract class 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 ) {
$post_type = get_post_type( $wp_post ); if ( $this->supports_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; $this->wp_post = $wp_post;
} else {
//TODO Error, this should not happen.
}
} }
/** /**
* 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.
@ -66,6 +64,7 @@ abstract class Base {
* 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
*/ */
@ -79,27 +78,25 @@ 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 );
if ( isset( $plugin_data['Name'] ) ) { return $plugin_data['Name'] ?? esc_html__( 'Unknown', 'activitypub' );
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(), true ); 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();
@ -108,6 +105,7 @@ abstract class Base {
* 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();
@ -163,26 +161,6 @@ abstract class Base {
return $content; return $content;
} }
/**
* Gets the template to use to generate the content of the activitypub item.
*
* @return string The Template.
*/
protected function get_post_content_template() {
if ( 'excerpt' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
return "[ap_excerpt]\n\n[ap_permalink type=\"html\"]";
}
if ( 'title' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
return "[ap_title]\n\n[ap_permalink type=\"html\"]";
}
if ( 'content' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
return "[ap_content]\n\n[ap_permalink type=\"html\"]\n\n[ap_hashtags]";
}
return \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
}
/** /**
* Returns the ID of the Post. * Returns the ID of the Post.
@ -269,6 +247,7 @@ abstract class Base {
$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 );
} }
$media_ids = \array_unique( $media_ids );
return \array_filter( \array_map( array( self::class, 'wp_attachment_to_activity_attachment' ), $media_ids ) ); return \array_filter( \array_map( array( self::class, 'wp_attachment_to_activity_attachment' ), $media_ids ) );
} }
@ -322,7 +301,6 @@ abstract class 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.
@ -363,9 +341,6 @@ abstract class Base {
break; break;
} }
// depupe
$media_ids = \array_unique( $media_ids );
// stop doing unneeded work // stop doing unneeded work
if ( count( $media_ids ) >= $max_media ) { if ( count( $media_ids ) >= $max_media ) {
break; break;
@ -616,4 +591,5 @@ abstract class Base {
return $object; return $object;
} }
} }

View file

@ -3,10 +3,9 @@
* 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\Transformer; namespace Activitypub;
use WP_Post; use WP_Post;
use WP_Comment; use WP_Comment;
@ -26,12 +25,8 @@ if ( ! defined( 'ABSPATH' ) ) {
* *
* @since version_number_transformer_management_placeholder * @since version_number_transformer_management_placeholder
*/ */
class Transformers_Manager {
const DEFAULT_TRANSFORMER_MAPPING = array(
'post' => ACTIVITYPUB_DEFAULT_TRANSFORMER,
'page' => ACTIVITYPUB_DEFAULT_TRANSFORMER,
);
class Transformers_Manager {
/** /**
* Transformers. * Transformers.
* *
@ -40,19 +35,19 @@ 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;
/** /**
* Transformer_Manager instance. * Module instance.
* *
* Holds the transformer instance. * Holds the transformer instance.
* *
* @since version_number_transformer_management_placeholder * @since version_number_transformer_management_placeholder
* @access protected * @access protected
* *
* @var Transformer_Manager * @var Module
*/ */
protected static $_instances = []; protected static $_instances = [];
@ -66,7 +61,7 @@ class Transformers_Manager {
* @access public * @access public
* @static * @static
* *
* @return Transformer_Manager An instance of the class. * @return Module An instance of the class.
*/ */
public static function instance() { public static function instance() {
$class_name = static::class_name(); $class_name = static::class_name();
@ -112,7 +107,7 @@ class Transformers_Manager {
* @access private * @access private
*/ */
private function require_files() { private function require_files() {
require ACTIVITYPUB_PLUGIN_DIR . 'includes/transformer/class-base.php'; require ACTIVITYPUB_PLUGIN_DIR . 'includes/class-transformer-base.php';
} }
/** /**
@ -133,16 +128,16 @@ 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( \ActivityPub\Transformer\Base $transformer_instance ) { public function register( Transformer_Base $transformer_instance) {
if ( ! $transformer_instance instanceof \ActivityPub\Transformer\Base ) { if ( ! $transformer_instance instanceof Transformer_Base ) {
_doing_it_wrong( _doing_it_wrong(
__METHOD__, __METHOD__,
\esc_html__( 'ActivityPub transformer instance must be a of \ActivityPub\Transformer_Base class.' ), __( '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 +147,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__,
\esc_html__( 'ActivityPub transformer names must not contain uppercase characters.' ), __( 'ActivityPub transformer names must not contain uppercase characters.' ),
'version_number_transformer_management_placeholder' 'version_number_transformer_management_placeholder'
); );
return false; return false;
@ -162,7 +157,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__,
\esc_html__( 'ActivityPub transformer names must contain a namespace prefix. Example: my-plugin/my-custom-transformer' ), __( '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 +167,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.', esc_html( $transformer_name ) ), sprintf( __( 'ActivityPub transformer with name "%s" is already registered.' ), $transformer_name ),
'version_number_transformer_management_placeholder' 'version_number_transformer_management_placeholder'
); );
return false; return false;
@ -184,7 +179,7 @@ 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 );
@ -209,7 +204,7 @@ class Transformers_Manager {
*/ */
private function init_transformers() { private function init_transformers() {
$builtin_transformers = [ $builtin_transformers = [
'post', 'post'
]; ];
$this->transformers = []; $this->transformers = [];
@ -219,7 +214,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,9 +228,10 @@ 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 );
} }
/** /**
* Get available ActivityPub transformers. * Get available ActivityPub transformers.
* *
@ -245,9 +241,9 @@ class Transformers_Manager {
* @since version_number_transformer_management_placeholder * @since version_number_transformer_management_placeholder
* @access public * @access public
* *
* @param string $transformer_name Optional. Transformer name. Default is null. * @param string $transformers Optional. Transformer name. Default is null.
* *
* @return Base|Base[]|null Registered transformers. * @return Transformer_Base|Transformer_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,25 +260,20 @@ class Transformers_Manager {
/** /**
* Get the mapped ActivityPub transformer. * Get the mapped 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 $object The WordPress Post/Comment. * @param WP_Post|WP_Comment $wp_post The WordPress Post/Comment.
* *
* @return \ActivityPub\Transformer\Base|null Registered transformers. * @return Transformer_Base|null Registered transformers.
*/ */
public function get_transformer( $object ) { public function get_transformer( $object ) {
switch ( get_class( $object ) ) { switch ( get_class( $object ) ) {
case 'WP_Post': case 'WP_Post':
$post_type = get_post_type( $object ); $post_type = get_post_type( $object );
$transformer_mapping = \get_option( 'activitypub_transformer_mapping', self::DEFAULT_TRANSFORMER_MAPPING ); $transformer_mapping = \get_option( 'activitypub_transformer_mapping', array( 'post' => 'activitypub/default', 'page' => 'activitypub/default' ) ) ? \get_option( 'activitypub_transformer_mapping', array( 'post' => 'activitypub/default', 'page' => 'activitypub/default' ) ) : array();
$transformer_name = $transformer_mapping[ $post_type ]; $transformer_name = $transformer_mapping[ $post_type ];
$transformer_class = $this->get_transformers( $transformer_name ); return new ( $this->get_transformers( $transformer_name ) );
$transformer_instance = new $transformer_class();
$transformer_instance->set_wp_post( $object );
return $transformer_instance;
case 'WP_Comment': case 'WP_Comment':
return new Comment( $object ); return new Comment( $object );
default: default:
@ -290,3 +281,4 @@ class Transformers_Manager {
} }
} }
} }

View file

@ -41,14 +41,9 @@ 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

@ -279,16 +279,6 @@ function is_activitypub_request() {
return false; return false;
} }
// Check if the current post type supports ActivityPub.
if ( \is_singular() ) {
$queried_object = \get_queried_object();
$post_type = \get_post_type( $queried_object );
if ( ! \post_type_supports( $post_type, 'activitypub' ) ) {
return false;
}
}
// One can trigger an ActivityPub request by adding ?activitypub to the URL. // One can trigger an ActivityPub request by adding ?activitypub to the URL.
// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration
global $wp_query; global $wp_query;

View file

@ -315,7 +315,7 @@ class Follower extends Actor {
$object->set_id( $post->guid ); $object->set_id( $post->guid );
$object->set_name( $post->post_title ); $object->set_name( $post->post_title );
$object->set_summary( $post->post_excerpt ); $object->set_summary( $post->post_excerpt );
$object->set_published( gmdate( 'Y-m-d H:i:s', strtotime( $post->post_date ) ) ); $object->set_published( gmdate( 'Y-m-d H:i:s', strtotime( $post->post_published ) ) );
$object->set_updated( gmdate( 'Y-m-d H:i:s', strtotime( $post->post_modified ) ) ); $object->set_updated( gmdate( 'Y-m-d H:i:s', strtotime( $post->post_modified ) ) );
return $object; return $object;

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Activitypub\Model; namespace Activitypub\Model;
use Activitypub\Transformer\Post as Transformer_Post; use Activitypub\Transformer_Post as Post_Transformer;
/** /**
* ActivityPub Post Class * ActivityPub Post Class
@ -31,11 +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;
$transformer = new Transformer_Post(); $this->object = Post_Transformer->set_wp_post( $post )->to_object();
$this->object = $transformer->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\Transformer\Transformers_Manager; use Activitypub\Transformers_Manager;
use Activitypub\Activity\Activity; use Activitypub\Activity\Activity;
use Activitypub\Collection\Users as User_Collection; use Activitypub\Collection\Users as User_Collection;
@ -168,7 +168,7 @@ class Collection {
); );
foreach ( $posts as $post ) { foreach ( $posts as $post ) {
$response['orderedItems'][] = Transformers_Manager::instance()->get_transformer( $post )->to_object()->to_array(); $response['orderedItems'][] = Transformers_Manager::get_transformer( $post )->transform( $post )->to_object()->to_array();
} }
$rest_response = new WP_REST_Response( $response, 200 ); $rest_response = new WP_REST_Response( $response, 200 );

View file

@ -236,12 +236,8 @@ 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_array( $param ) ) { if ( ! \is_string( $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

@ -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\Transformer\Transformers_Manager; use Activitypub\Transformers_Manager;
use Activitypub\Activity\Activity; use Activitypub\Activity\Activity;
use Activitypub\Collection\Users as User_Collection; use Activitypub\Collection\Users as User_Collection;
@ -105,7 +105,8 @@ class Outbox {
); );
foreach ( $posts as $post ) { foreach ( $posts as $post ) {
$transformer = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( $post ); $transformer = Transformers_Manager::get_transformer( $wp_post );
$transformer->transform( $wp_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

@ -32,22 +32,17 @@ class Followers extends WP_List_Table {
return array( return array(
'cb' => '<input type="checkbox" />', 'cb' => '<input type="checkbox" />',
'avatar' => \__( 'Avatar', 'activitypub' ), 'avatar' => \__( 'Avatar', 'activitypub' ),
'post_title' => \__( 'Name', 'activitypub' ), 'name' => \__( 'Name', 'activitypub' ),
'username' => \__( 'Username', 'activitypub' ), 'username' => \__( 'Username', 'activitypub' ),
'url' => \__( 'URL', 'activitypub' ), 'url' => \__( 'URL', 'activitypub' ),
'published' => \__( 'Followed', 'activitypub' ), 'updated' => \__( 'Last updated', 'activitypub' ),
'modified' => \__( 'Last updated', 'activitypub' ), //'errors' => \__( 'Errors', 'activitypub' ),
//'latest-error' => \__( 'Latest Error Message', 'activitypub' ),
); );
} }
public function get_sortable_columns() { public function get_sortable_columns() {
$sortable_columns = array( return array();
'post_title' => array( 'post_title', true ),
'modified' => array( 'modified', false ),
'published' => array( 'published', false ),
);
return $sortable_columns;
} }
public function prepare_items() { public function prepare_items() {
@ -60,32 +55,8 @@ class Followers extends WP_List_Table {
$page_num = $this->get_pagenum(); $page_num = $this->get_pagenum();
$per_page = 20; $per_page = 20;
$args = array(); $followers = FollowerCollection::get_followers( $this->user_id, $per_page, $page_num );
$counter = FollowerCollection::count_followers( $this->user_id );
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['orderby'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$args['orderby'] = sanitize_text_field( wp_unslash( $_GET['orderby'] ) );
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['order'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$args['order'] = sanitize_text_field( wp_unslash( $_GET['order'] ) );
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['s'] ) && isset( $_REQUEST['_wpnonce'] ) ) {
$nonce = sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) );
if ( wp_verify_nonce( $nonce, 'bulk-' . $this->_args['plural'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$args['s'] = sanitize_text_field( wp_unslash( $_GET['s'] ) );
}
}
$followers_with_count = FollowerCollection::get_followers_with_count( $this->user_id, $per_page, $page_num, $args );
$followers = $followers_with_count['followers'];
$counter = $followers_with_count['total'];
$this->items = array(); $this->items = array();
$this->set_pagination_args( $this->set_pagination_args(
@ -99,12 +70,13 @@ class Followers extends WP_List_Table {
foreach ( $followers as $follower ) { foreach ( $followers as $follower ) {
$item = array( $item = array(
'icon' => esc_attr( $follower->get_icon_url() ), 'icon' => esc_attr( $follower->get_icon_url() ),
'post_title' => esc_attr( $follower->get_name() ), 'name' => esc_attr( $follower->get_name() ),
'username' => esc_attr( $follower->get_preferred_username() ), 'username' => esc_attr( $follower->get_preferred_username() ),
'url' => esc_attr( $follower->get_url() ), 'url' => esc_attr( $follower->get_url() ),
'identifier' => esc_attr( $follower->get_id() ), 'identifier' => esc_attr( $follower->get_id() ),
'published' => esc_attr( $follower->get_published() ), 'updated' => esc_attr( $follower->get_updated() ),
'modified' => esc_attr( $follower->get_updated() ), 'errors' => $follower->count_errors(),
'latest-error' => $follower->get_latest_error_message(),
); );
$this->items[] = $item; $this->items[] = $item;
@ -144,11 +116,11 @@ class Followers extends WP_List_Table {
} }
public function process_action() { public function process_action() {
if ( ! isset( $_REQUEST['followers'] ) || ! isset( $_REQUEST['_wpnonce'] ) ) { if ( ! isset( $_REQUEST['followers'] ) || ! isset( $_REQUEST['_apnonce'] ) ) {
return false; return false;
} }
$nonce = sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ); $nonce = sanitize_text_field( wp_unslash( $_REQUEST['_apnonce'] ) );
if ( ! wp_verify_nonce( $nonce, 'bulk-' . $this->_args['plural'] ) ) { if ( ! wp_verify_nonce( $nonce, 'activitypub-followers-list' ) ) {
return false; return false;
} }

View file

@ -1,12 +1,12 @@
<?php <?php
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 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,10 +18,12 @@ 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 Post extends Base { class Transformer_Post extends Transformer_Base {
/** /**
* Getter function for the name of the transformer. * Getter function for the name of the transformer.
* *
@ -106,4 +108,26 @@ class Post extends Base {
return $object_type; return $object_type;
} }
/**
* Gets the template to use to generate the content of the activitypub item.
*
* @return string The Template.
*/
protected function get_post_content_template() {
if ( 'excerpt' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
return "[ap_excerpt]\n\n[ap_permalink type=\"html\"]";
}
if ( 'title' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
return "[ap_title]\n\n[ap_permalink type=\"html\"]";
}
if ( 'content' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
return "[ap_content]\n\n[ap_permalink type=\"html\"]\n\n[ap_hashtags]";
}
return \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
}
} }

View file

@ -30,10 +30,6 @@ 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

@ -3,7 +3,7 @@ Contributors: automattic, pfefferle, mediaformat, mattwiebe, akirk, jeherve, nur
Tags: OStatus, fediverse, activitypub, activitystream Tags: OStatus, fediverse, activitypub, activitystream
Requires at least: 4.7 Requires at least: 4.7
Tested up to: 6.4 Tested up to: 6.4
Stable tag: 1.2.0 Stable tag: 1.1.0
Requires PHP: 5.6 Requires PHP: 5.6
License: MIT License: MIT
License URI: http://opensource.org/licenses/MIT License URI: http://opensource.org/licenses/MIT
@ -105,15 +105,6 @@ Where 'blog' is the path to the subdirectory at which your blog resides.
Project maintained on GitHub at [automattic/wordpress-activitypub](https://github.com/automattic/wordpress-activitypub). Project maintained on GitHub at [automattic/wordpress-activitypub](https://github.com/automattic/wordpress-activitypub).
= 1.2.0 =
* Add: Search and order followerer lists
* Add: Have a filter to defer signature verification
* Improved: "Follow Me" styles for dark themes
* Improved: Allow `p` and `br` tags only for AP comments
* Fixed: Deduplicate attachments earlier to prevent incorrect max_media
= 1.1.0 = = 1.1.0 =
* Improved: audio and video attachments are now supported! * Improved: audio and video attachments are now supported!

View file

@ -21,8 +21,8 @@ $followers_template = _n( 'Your blog profile currently has %s follower.', 'Your
<input type="hidden" name="tab" value="followers" /> <input type="hidden" name="tab" value="followers" />
<?php <?php
$table->prepare_items(); $table->prepare_items();
$table->search_box( 'Search', 'search' );
$table->display(); $table->display();
?> ?>
<?php wp_nonce_field( 'activitypub-followers-list', '_apnonce' ); ?>
</form> </form>
</div> </div>

View file

@ -2,7 +2,8 @@
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$post = \get_post(); $post = \get_post();
$transformer = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( $post ); $transformer = \Activitypub\Transformers_Manager::instance()->get_transformer( $post );
$transformer->set_wp_post( $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

@ -237,7 +237,7 @@
return $object->name; return $object->name;
}, $all_public_post_types); }, $all_public_post_types);
$transformer_manager = \Activitypub\Transformer\Transformers_Manager::instance(); $transformer_manager = \Activitypub\Transformers_Manager::instance();
$transformers = $transformer_manager->get_transformers(); $transformers = $transformer_manager->get_transformers();
?> ?>

View file

@ -14,8 +14,8 @@ $followers_template = _n( 'Your author profile currently has %s follower.', 'You
<input type="hidden" name="page" value="activitypub-followers-list" /> <input type="hidden" name="page" value="activitypub-followers-list" />
<?php <?php
$table->prepare_items(); $table->prepare_items();
$table->search_box( 'Search', 'search' );
$table->display(); $table->display();
?> ?>
<?php wp_nonce_field( 'activitypub-followers-list', '_apnonce' ); ?>
</form> </form>
</div> </div>

View file

@ -18,7 +18,7 @@ class Test_Activitypub_Activity extends WP_UnitTestCase {
); );
$wp_post = get_post( $post ); $wp_post = get_post( $post );
$activitypub_post = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( $post )->to_object(); $activitypub_post = \Activitypub\Transformers_Manager::get_transforemr( $wp_post )->transform( $wp_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\Transformer\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object(); $activitypub_post = \Activitypub\Transformers_Manager::get_transformer( get_post( $post ) )->transform( 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\Transformer\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object(); $activitypub_post = \Activitypub\Transformers_Manager::get_transformer( get_post( $post ) )->transform( 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\Transformer\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object(); $activitypub_post = \Activitypub\Transformers_Manager::get_transformer( get_post( $post ) )->transform( 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\Transformer\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object(); $activitypub_post = \Activitypub\Transformers_Manager::get_transformer( get_post( $post ) )->transform( 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 );