some code cleanup

This commit is contained in:
Matthias Pfefferle 2023-04-20 15:22:11 +02:00
parent cf426ab8ab
commit c32eec2390
16 changed files with 102 additions and 86 deletions

View file

@ -39,50 +39,50 @@ function init() {
require_once \dirname( __FILE__ ) . '/includes/model/class-post.php';
require_once \dirname( __FILE__ ) . '/includes/class-activity-dispatcher.php';
\Activitypub\Activity_Dispatcher::init();
Activity_Dispatcher::init();
require_once \dirname( __FILE__ ) . '/includes/class-activitypub.php';
\Activitypub\Activitypub::init();
Activitypub::init();
// Configure the REST API route
require_once \dirname( __FILE__ ) . '/includes/rest/class-outbox.php';
\Activitypub\Rest\Outbox::init();
Rest\Outbox::init();
require_once \dirname( __FILE__ ) . '/includes/rest/class-inbox.php';
\Activitypub\Rest\Inbox::init();
Rest\Inbox::init();
require_once \dirname( __FILE__ ) . '/includes/rest/class-followers.php';
\Activitypub\Rest\Followers::init();
Rest\Followers::init();
require_once \dirname( __FILE__ ) . '/includes/rest/class-following.php';
\Activitypub\Rest\Following::init();
Rest\Following::init();
require_once \dirname( __FILE__ ) . '/includes/rest/class-webfinger.php';
\Activitypub\Rest\Webfinger::init();
Rest\Webfinger::init();
// load NodeInfo endpoints only if blog is public
if ( true === (bool) \get_option( 'blog_public', 1 ) ) {
require_once \dirname( __FILE__ ) . '/includes/rest/class-nodeinfo.php';
\Activitypub\Rest\NodeInfo::init();
Rest\NodeInfo::init();
}
require_once \dirname( __FILE__ ) . '/includes/class-admin.php';
\Activitypub\Admin::init();
Admin::init();
require_once \dirname( __FILE__ ) . '/includes/class-hashtag.php';
\Activitypub\Hashtag::init();
Hashtag::init();
require_once \dirname( __FILE__ ) . '/includes/class-shortcodes.php';
\Activitypub\Shortcodes::init();
Shortcodes::init();
require_once \dirname( __FILE__ ) . '/includes/class-mention.php';
\Activitypub\Mention::init();
Mention::init();
require_once \dirname( __FILE__ ) . '/includes/class-debug.php';
\Activitypub\Debug::init();
Debug::init();
require_once \dirname( __FILE__ ) . '/includes/class-health-check.php';
\Activitypub\Health_Check::init();
Health_Check::init();
if ( \WP_DEBUG ) {
require_once \dirname( __FILE__ ) . '/includes/debug.php';
@ -136,6 +136,6 @@ function flush_rewrite_rules() {
*/
function enable_buddypress_features() {
require_once \dirname( __FILE__ ) . '/integration/class-buddypress.php';
\Activitypub\Integration\Buddypress::init();
Integration\Buddypress::init();
}
add_action( 'bp_include', '\Activitypub\enable_buddypress_features' );

View file

@ -1,6 +1,9 @@
<?php
namespace Activitypub;
use Activitypub\Model\Post;
use Activitypub\Model\Activity;
/**
* ActivityPub Activity_Dispatcher Class
*
@ -14,52 +17,53 @@ class Activity_Dispatcher {
*/
public static function init() {
// legacy
\add_action( 'activitypub_send_post_activity', array( '\Activitypub\Activity_Dispatcher', 'send_create_activity' ) );
\add_action( 'activitypub_send_post_activity', array( self::class, 'send_create_activity' ) );
\add_action( 'activitypub_send_create_activity', array( '\Activitypub\Activity_Dispatcher', 'send_create_activity' ) );
\add_action( 'activitypub_send_update_activity', array( '\Activitypub\Activity_Dispatcher', 'send_update_activity' ) );
\add_action( 'activitypub_send_delete_activity', array( '\Activitypub\Activity_Dispatcher', 'send_delete_activity' ) );
\add_action( 'activitypub_send_create_activity', array( self::class, 'send_create_activity' ) );
\add_action( 'activitypub_send_update_activity', array( self::class, 'send_update_activity' ) );
\add_action( 'activitypub_send_delete_activity', array( self::class, 'send_delete_activity' ) );
}
/**
* Send "create" activities.
*
* @param \Activitypub\Model\Post $activitypub_post
* @param Activitypub\Model\Post $activitypub_post
*/
public static function send_create_activity( Model\Post $activitypub_post ) {
public static function send_create_activity( Post $activitypub_post ) {
self::send_activity( $activitypub_post, 'Create' );
}
/**
* Send "update" activities.
*
* @param \Activitypub\Model\Post $activitypub_post
* @param Activitypub\Model\Post $activitypub_post
*/
public static function send_update_activity( Model\Post $activitypub_post ) {
public static function send_update_activity( Post $activitypub_post ) {
self::send_activity( $activitypub_post, 'Update' );
}
/**
* Send "delete" activities.
*
* @param \Activitypub\Model\Post $activitypub_post
* @param Activitypub\Model\Post $activitypub_post
*/
public static function send_delete_activity( Model\Post $activitypub_post ) {
public static function send_delete_activity( Post $activitypub_post ) {
self::send_activity( $activitypub_post, 'Delete' );
}
/**
* Undocumented function
*
* @param [type] $activitypub_post
* @param Activitypub\Model\Post $activitypub_post
* @param [type] $activity_type
*
* @return void
*/
public static function send_activity( Model\Post $activitypub_post, $activity_type ) {
public static function send_activity( Post $activitypub_post, $activity_type ) {
// get latest version of post
$user_id = $activitypub_post->get_post_author();
$activitypub_activity = new \Activitypub\Model\Activity( $activity_type );
$activitypub_activity = new Activity( $activity_type );
$activitypub_activity->from_post( $activitypub_post );
$inboxes = \Activitypub\get_follower_inboxes( $user_id, $activitypub_activity->get_cc() );

View file

@ -11,9 +11,9 @@ class Activitypub {
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
\add_filter( 'template_include', array( '\Activitypub\Activitypub', 'render_json_template' ), 99 );
\add_filter( 'query_vars', array( '\Activitypub\Activitypub', 'add_query_vars' ) );
\add_filter( 'pre_get_avatar_data', array( '\Activitypub\Activitypub', 'pre_get_avatar_data' ), 11, 2 );
\add_filter( 'template_include', array( self::class, 'render_json_template' ), 99 );
\add_filter( 'query_vars', array( self::class, 'add_query_vars' ) );
\add_filter( 'pre_get_avatar_data', array( self::class, 'pre_get_avatar_data' ), 11, 2 );
// Add support for ActivityPub to custom post types
$post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ) ? \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ) : array();
@ -22,9 +22,9 @@ class Activitypub {
\add_post_type_support( $post_type, 'activitypub' );
}
\add_action( 'transition_post_status', array( '\Activitypub\Activitypub', 'schedule_post_activity' ), 33, 3 );
\add_action( 'wp_trash_post', array( '\Activitypub\Activitypub', 'trash_post' ), 1 );
\add_action( 'untrash_post', array( '\Activitypub\Activitypub', 'untrash_post' ), 1 );
\add_action( 'transition_post_status', array( self::class, 'schedule_post_activity' ), 33, 3 );
\add_action( 'wp_trash_post', array( self::class, 'trash_post' ), 1 );
\add_action( 'untrash_post', array( self::class, 'untrash_post' ), 1 );
}
/**

View file

@ -11,10 +11,10 @@ class Admin {
* Initialize the class, registering WordPress hooks
*/
public static function init() {
\add_action( 'admin_menu', array( '\Activitypub\Admin', 'admin_menu' ) );
\add_action( 'admin_init', array( '\Activitypub\Admin', 'register_settings' ) );
\add_action( 'show_user_profile', array( '\Activitypub\Admin', 'add_fediverse_profile' ) );
\add_action( 'admin_enqueue_scripts', array( '\Activitypub\Admin', 'enqueue_scripts' ) );
\add_action( 'admin_menu', array( self::class, 'admin_menu' ) );
\add_action( 'admin_init', array( self::class, 'register_settings' ) );
\add_action( 'show_user_profile', array( self::class, 'add_fediverse_profile' ) );
\add_action( 'admin_enqueue_scripts', array( self::class, 'enqueue_scripts' ) );
}
/**
@ -26,14 +26,14 @@ class Admin {
'ActivityPub',
'manage_options',
'activitypub',
array( '\Activitypub\Admin', 'settings_page' )
array( self::class, 'settings_page' )
);
\add_action( 'load-' . $settings_page, array( '\Activitypub\Admin', 'add_settings_help_tab' ) );
\add_action( 'load-' . $settings_page, array( self::class, 'add_settings_help_tab' ) );
$followers_list_page = \add_users_page( \__( 'Followers', 'activitypub' ), \__( 'Followers (Fediverse)', 'activitypub' ), 'read', 'activitypub-followers-list', array( '\Activitypub\Admin', 'followers_list_page' ) );
$followers_list_page = \add_users_page( \__( 'Followers', 'activitypub' ), \__( 'Followers (Fediverse)', 'activitypub' ), 'read', 'activitypub-followers-list', array( self::class, 'followers_list_page' ) );
\add_action( 'load-' . $followers_list_page, array( '\Activitypub\Admin', 'add_followers_list_help_tab' ) );
\add_action( 'load-' . $followers_list_page, array( self::class, 'add_followers_list_help_tab' ) );
}
/**

View file

@ -1,6 +1,9 @@
<?php
namespace Activitypub;
use WP_DEBUG;
use WP_DEBUG_LOG;
/**
* ActivityPub Debug Class
*
@ -12,7 +15,7 @@ class Debug {
*/
public static function init() {
if ( WP_DEBUG && WP_DEBUG_LOG ) {
\add_action( 'activitypub_safe_remote_post_response', array( '\Activitypub\Debug', 'log_remote_post_responses' ), 10, 4 );
\add_action( 'activitypub_safe_remote_post_response', array( self::class, 'log_remote_post_responses' ), 10, 4 );
}
}

View file

@ -12,8 +12,8 @@ class Hashtag {
*/
public static function init() {
if ( '1' === \get_option( 'activitypub_use_hashtags', '1' ) ) {
\add_filter( 'wp_insert_post', array( '\Activitypub\Hashtag', 'insert_post' ), 10, 2 );
\add_filter( 'the_content', array( '\Activitypub\Hashtag', 'the_content' ), 10, 2 );
\add_filter( 'wp_insert_post', array( self::class, 'insert_post' ), 10, 2 );
\add_filter( 'the_content', array( self::class, 'the_content' ), 10, 2 );
}
}

View file

@ -14,19 +14,19 @@ class Health_Check {
* @return void
*/
public static function init() {
\add_filter( 'site_status_tests', array( '\Activitypub\Health_Check', 'add_tests' ) );
\add_filter( 'debug_information', array( '\Activitypub\Health_Check', 'debug_information' ) );
\add_filter( 'site_status_tests', array( self::class, 'add_tests' ) );
\add_filter( 'debug_information', array( self::class, 'debug_information' ) );
}
public static function add_tests( $tests ) {
$tests['direct']['activitypub_test_author_url'] = array(
'label' => \__( 'Author URL test', 'activitypub' ),
'test' => array( '\Activitypub\Health_Check', 'test_author_url' ),
'test' => array( self::class, 'test_author_url' ),
);
$tests['direct']['activitypub_test_webfinger'] = array(
'label' => __( 'WebFinger Test', 'activitypub' ),
'test' => array( '\Activitypub\Health_Check', 'test_webfinger' ),
'test' => array( self::class, 'test_webfinger' ),
);
return $tests;

View file

@ -11,8 +11,8 @@ class Mention {
* Initialize the class, registering WordPress hooks
*/
public static function init() {
\add_filter( 'the_content', array( '\Activitypub\Mention', 'the_content' ), 99, 2 );
\add_filter( 'activitypub_extract_mentions', array( '\Activitypub\Mention', 'extract_mentions' ), 99, 2 );
\add_filter( 'the_content', array( self::class, 'the_content' ), 99, 2 );
\add_filter( 'activitypub_extract_mentions', array( self::class, 'extract_mentions' ), 99, 2 );
}
/**
@ -46,7 +46,7 @@ class Mention {
$the_content
);
$the_content = \preg_replace_callback( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/', array( '\Activitypub\Mention', 'replace_with_links' ), $the_content );
$the_content = \preg_replace_callback( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/', array( self::class, 'replace_with_links' ), $the_content );
$the_content = str_replace( array_reverse( array_keys( $protected_tags ) ), array_reverse( array_values( $protected_tags ) ), $the_content );

View file

@ -1,6 +1,8 @@
<?php
namespace Activitypub;
use WP_Error;
/**
* ActivityPub WebFinger Class
*
@ -43,7 +45,7 @@ class Webfinger {
$url = \add_query_arg( 'resource', 'acct:' . ltrim( $account, '@' ), 'https://' . $m[2] . '/.well-known/webfinger' );
if ( ! \wp_http_validate_url( $url ) ) {
$response = new \WP_Error( 'invalid_webfinger_url', null, $url );
$response = new WP_Error( 'invalid_webfinger_url', null, $url );
\set_transient( $transient_key, $response, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
return $response;
}
@ -59,7 +61,7 @@ class Webfinger {
);
if ( \is_wp_error( $response ) ) {
$link = new \WP_Error( 'webfinger_url_not_accessible', null, $url );
$link = new WP_Error( 'webfinger_url_not_accessible', null, $url );
\set_transient( $transient_key, $link, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
return $link;
}
@ -68,7 +70,7 @@ class Webfinger {
$body = \json_decode( $body, true );
if ( empty( $body['links'] ) ) {
$link = new \WP_Error( 'webfinger_url_invalid_response', null, $url );
$link = new WP_Error( 'webfinger_url_invalid_response', null, $url );
\set_transient( $transient_key, $link, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
return $link;
}
@ -80,7 +82,7 @@ class Webfinger {
}
}
$link = new \WP_Error( 'webfinger_url_no_activity_pub', null, $body );
$link = new WP_Error( 'webfinger_url_no_activity_pub', null, $body );
\set_transient( $transient_key, $link, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
return $link;
}

View file

@ -1,6 +1,8 @@
<?php
namespace Activitypub\Model;
use function Activitypub\get_context;
/**
* ActivityPub Post Class
*

View file

@ -13,7 +13,7 @@ class Followers {
* Initialize the class, registering WordPress hooks
*/
public static function init() {
\add_action( 'rest_api_init', array( '\Activitypub\Rest\Followers', 'register_routes' ) );
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
}
/**
@ -26,7 +26,7 @@ class Followers {
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( '\Activitypub\Rest\Followers', 'get' ),
'callback' => array( self::class, 'get' ),
'args' => self::request_parameters(),
'permission_callback' => '__return_true',
),

View file

@ -13,7 +13,7 @@ class Following {
* Initialize the class, registering WordPress hooks
*/
public static function init() {
\add_action( 'rest_api_init', array( '\Activitypub\Rest\Following', 'register_routes' ) );
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
}
/**
@ -26,7 +26,7 @@ class Following {
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( '\Activitypub\Rest\Following', 'get' ),
'callback' => array( self::class, 'get' ),
'args' => self::request_parameters(),
'permission_callback' => '__return_true',
),

View file

@ -1,6 +1,8 @@
<?php
namespace Activitypub\Rest;
use Activitypub\Model\Activity;
/**
* ActivityPub Inbox REST-Class
*
@ -13,13 +15,13 @@ class Inbox {
* Initialize the class, registering WordPress hooks
*/
public static function init() {
\add_action( 'rest_api_init', array( '\Activitypub\Rest\Inbox', 'register_routes' ) );
\add_filter( 'rest_pre_serve_request', array( '\Activitypub\Rest\Inbox', 'serve_request' ), 11, 4 );
\add_action( 'activitypub_inbox_follow', array( '\Activitypub\Rest\Inbox', 'handle_follow' ), 10, 2 );
\add_action( 'activitypub_inbox_undo', array( '\Activitypub\Rest\Inbox', 'handle_unfollow' ), 10, 2 );
//\add_action( 'activitypub_inbox_like', array( '\Activitypub\Rest\Inbox', 'handle_reaction' ), 10, 2 );
//\add_action( 'activitypub_inbox_announce', array( '\Activitypub\Rest\Inbox', 'handle_reaction' ), 10, 2 );
\add_action( 'activitypub_inbox_create', array( '\Activitypub\Rest\Inbox', 'handle_create' ), 10, 2 );
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
\add_filter( 'rest_pre_serve_request', array( self::class, 'serve_request' ), 11, 4 );
\add_action( 'activitypub_inbox_follow', array( self::class, 'handle_follow' ), 10, 2 );
\add_action( 'activitypub_inbox_undo', array( self::class, 'handle_unfollow' ), 10, 2 );
//\add_action( 'activitypub_inbox_like', array( self::class, 'handle_reaction' ), 10, 2 );
//\add_action( 'activitypub_inbox_announce', array( self::class, 'handle_reaction' ), 10, 2 );
\add_action( 'activitypub_inbox_create', array( self::class, 'handle_create' ), 10, 2 );
}
/**
@ -32,7 +34,7 @@ class Inbox {
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( '\Activitypub\Rest\Inbox', 'shared_inbox_post' ),
'callback' => array( self::class, 'shared_inbox_post' ),
'args' => self::shared_inbox_post_parameters(),
'permission_callback' => '__return_true',
),
@ -45,13 +47,13 @@ class Inbox {
array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( '\Activitypub\Rest\Inbox', 'user_inbox_post' ),
'callback' => array( self::class, 'user_inbox_post' ),
'args' => self::user_inbox_post_parameters(),
'permission_callback' => '__return_true',
),
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( '\Activitypub\Rest\Inbox', 'user_inbox_get' ),
'callback' => array( self::class, 'user_inbox_get' ),
'args' => self::user_inbox_get_parameters(),
'permission_callback' => '__return_true',
),
@ -356,7 +358,7 @@ class Inbox {
$inbox = \Activitypub\get_inbox_by_actor( $object['actor'] );
// send "Accept" activity
$activity = new \Activitypub\Model\Activity( 'Accept' );
$activity = new Activity( 'Accept' );
$activity->set_object( $object );
$activity->set_actor( \get_author_posts_url( $user_id ) );
$activity->set_to( $object['actor'] );

View file

@ -13,9 +13,9 @@ class Nodeinfo {
* Initialize the class, registering WordPress hooks
*/
public static function init() {
\add_action( 'rest_api_init', array( '\Activitypub\Rest\Nodeinfo', 'register_routes' ) );
\add_filter( 'nodeinfo_data', array( '\Activitypub\Rest\Nodeinfo', 'add_nodeinfo_discovery' ), 10, 2 );
\add_filter( 'nodeinfo2_data', array( '\Activitypub\Rest\Nodeinfo', 'add_nodeinfo2_discovery' ), 10 );
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
\add_filter( 'nodeinfo_data', array( self::class, 'add_nodeinfo_discovery' ), 10, 2 );
\add_filter( 'nodeinfo2_data', array( self::class, 'add_nodeinfo2_discovery' ), 10 );
}
/**
@ -28,7 +28,7 @@ class Nodeinfo {
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( '\Activitypub\Rest\Nodeinfo', 'discovery' ),
'callback' => array( self::class, 'discovery' ),
'permission_callback' => '__return_true',
),
)
@ -40,7 +40,7 @@ class Nodeinfo {
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( '\Activitypub\Rest\Nodeinfo', 'nodeinfo' ),
'callback' => array( self::class, 'nodeinfo' ),
'permission_callback' => '__return_true',
),
)
@ -52,7 +52,7 @@ class Nodeinfo {
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( '\Activitypub\Rest\Nodeinfo', 'nodeinfo2' ),
'callback' => array( self::class, 'nodeinfo2' ),
'permission_callback' => '__return_true',
),
)

View file

@ -13,7 +13,7 @@ class Outbox {
* Initialize the class, registering WordPress hooks
*/
public static function init() {
\add_action( 'rest_api_init', array( '\Activitypub\Rest\Outbox', 'register_routes' ) );
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
}
/**
@ -26,7 +26,7 @@ class Outbox {
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( '\Activitypub\Rest\Outbox', 'user_outbox_get' ),
'callback' => array( self::class, 'user_outbox_get' ),
'args' => self::request_parameters(),
'permission_callback' => '__return_true',
),

View file

@ -1,6 +1,9 @@
<?php
namespace Activitypub\Rest;
use WP_Error;
use WP_REST_Response;
/**
* ActivityPub WebFinger REST-Class
*
@ -13,8 +16,8 @@ class Webfinger {
* Initialize the class, registering WordPress hooks
*/
public static function init() {
\add_action( 'rest_api_init', array( '\Activitypub\Rest\Webfinger', 'register_routes' ) );
\add_action( 'webfinger_user_data', array( '\Activitypub\Rest\Webfinger', 'add_webfinger_discovery' ), 10, 3 );
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
\add_action( 'webfinger_user_data', array( self::class, 'add_webfinger_discovery' ), 10, 3 );
}
/**
@ -27,7 +30,7 @@ class Webfinger {
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( '\Activitypub\Rest\Webfinger', 'webfinger' ),
'callback' => array( self::class, 'webfinger' ),
'args' => self::request_parameters(),
'permission_callback' => '__return_true',
),
@ -45,7 +48,7 @@ class Webfinger {
$resource = $request->get_param( 'resource' );
if ( \strpos( $resource, '@' ) === false ) {
return new \WP_Error( 'activitypub_unsupported_resource', \__( 'Resource is invalid', 'activitypub' ), array( 'status' => 400 ) );
return new WP_Error( 'activitypub_unsupported_resource', \__( 'Resource is invalid', 'activitypub' ), array( 'status' => 400 ) );
}
$resource = \str_replace( 'acct:', '', $resource );
@ -54,13 +57,13 @@ class Webfinger {
$resource_host = \substr( \strrchr( $resource, '@' ), 1 );
if ( \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) !== $resource_host ) {
return new \WP_Error( 'activitypub_wrong_host', \__( 'Resource host does not match blog host', 'activitypub' ), array( 'status' => 404 ) );
return new WP_Error( 'activitypub_wrong_host', \__( 'Resource host does not match blog host', 'activitypub' ), array( 'status' => 404 ) );
}
$user = \get_user_by( 'login', \esc_sql( $resource_identifier ) );
if ( ! $user || ! user_can( $user, 'publish_posts' ) ) {
return new \WP_Error( 'activitypub_user_not_found', \__( 'User not found', 'activitypub' ), array( 'status' => 404 ) );
return new WP_Error( 'activitypub_user_not_found', \__( 'User not found', 'activitypub' ), array( 'status' => 404 ) );
}
$json = array(
@ -82,7 +85,7 @@ class Webfinger {
),
);
return new \WP_REST_Response( $json, 200 );
return new WP_REST_Response( $json, 200 );
}
/**