diff --git a/.gitignore b/.gitignore index 566a865..f5e9294 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ package-lock.json composer.lock .DS_Store +.idea/ diff --git a/LICENSE b/LICENSE index d81f590..e59f0cc 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018 Matthias Pfefferle +Copyright (c) 2019 Matthias Pfefferle Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 6895c15..1303e0a 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ **Tags:** OStatus, fediverse, activitypub, activitystream **Requires at least:** 4.7 **Tested up to:** 5.1 -**Stable tag:** 0.4.4 +**Stable tag:** 0.5.0 **Requires PHP:** 5.6 **License:** MIT **License URI:** http://opensource.org/licenses/MIT diff --git a/activitypub.php b/activitypub.php index 71b7e33..8b6a12a 100644 --- a/activitypub.php +++ b/activitypub.php @@ -3,7 +3,7 @@ * Plugin Name: 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. - * Version: 0.4.4 + * Version: 0.5.0 * Author: Matthias Pfefferle * Author URI: https://notiz.blog/ * License: MIT @@ -12,77 +12,61 @@ * Domain Path: /languages */ +namespace Activitypub; + /** - * Initialize plugin + * Initialize pluginut */ -function activitypub_init() { +function init() { defined( 'ACTIVITYPUB_HASHTAGS_REGEXP' ) || define( 'ACTIVITYPUB_HASHTAGS_REGEXP', '(^|\s|>)#([^\s<>]+)\b' ); - require_once dirname( __FILE__ ) . '/includes/class-activitypub-signature.php'; - require_once dirname( __FILE__ ) . '/includes/class-activitypub-activity.php'; - require_once dirname( __FILE__ ) . '/includes/class-db-activitypub-followers.php'; + require_once dirname( __FILE__ ) . '/includes/class-signature.php'; + require_once dirname( __FILE__ ) . '/includes/class-activity.php'; + require_once dirname( __FILE__ ) . '/includes/db/class-followers.php'; require_once dirname( __FILE__ ) . '/includes/functions.php'; - require_once dirname( __FILE__ ) . '/includes/class-activitypub-post.php'; - add_filter( 'activitypub_the_summary', array( 'Activitypub_Post', 'add_backlink' ), 10, 2 ); - add_filter( 'activitypub_the_content', array( 'Activitypub_Post', 'add_backlink' ), 10, 2 ); + require_once dirname( __FILE__ ) . '/includes/class-post.php'; + \Activitypub\Post::init(); require_once dirname( __FILE__ ) . '/includes/class-activitypub.php'; - add_filter( 'template_include', array( 'Activitypub', 'render_json_template' ), 99 ); - add_filter( 'query_vars', array( 'Activitypub', 'add_query_vars' ) ); - add_action( 'init', array( 'Activitypub', 'add_rewrite_endpoint' ) ); - add_filter( 'pre_get_avatar_data', array( 'Activitypub', 'pre_get_avatar_data' ), 11, 2 ); + \Activitypub\ActivityPub::init(); // Configure the REST API route - require_once dirname( __FILE__ ) . '/includes/class-rest-activitypub-outbox.php'; - add_action( 'rest_api_init', array( 'Rest_Activitypub_Outbox', 'register_routes' ) ); - add_action( 'activitypub_send_post_activity', array( 'Rest_Activitypub_Outbox', 'send_post_activity' ) ); - add_action( 'activitypub_send_update_activity', array( 'Rest_Activitypub_Outbox', 'send_update_activity' ) ); + require_once dirname( __FILE__ ) . '/includes/rest/class-outbox.php'; + \Activitypub\Rest\Outbox::init(); - require_once dirname( __FILE__ ) . '/includes/class-rest-activitypub-inbox.php'; - add_action( 'rest_api_init', array( 'Rest_Activitypub_Inbox', 'register_routes' ) ); - //add_filter( 'rest_pre_serve_request', array( 'Rest_Activitypub_Inbox', 'serve_request' ), 11, 4 ); - add_action( 'activitypub_inbox_follow', array( 'Rest_Activitypub_Inbox', 'handle_follow' ), 10, 2 ); - add_action( 'activitypub_inbox_unfollow', array( 'Rest_Activitypub_Inbox', 'handle_unfollow' ), 10, 2 ); - //add_action( 'activitypub_inbox_like', array( 'Rest_Activitypub_Inbox', 'handle_reaction' ), 10, 2 ); - //add_action( 'activitypub_inbox_announce', array( 'Rest_Activitypub_Inbox', 'handle_reaction' ), 10, 2 ); - add_action( 'activitypub_inbox_create', array( 'Rest_Activitypub_Inbox', 'handle_create' ), 10, 2 ); + require_once dirname( __FILE__ ) . '/includes/rest/class-inbox.php'; + \Activitypub\Rest\Inbox::init(); - require_once dirname( __FILE__ ) . '/includes/class-rest-activitypub-followers.php'; - add_action( 'rest_api_init', array( 'Rest_Activitypub_Followers', 'register_routes' ) ); + require_once dirname( __FILE__ ) . '/includes/rest/class-followers.php'; + \Activitypub\Rest\Followers::init(); - require_once dirname( __FILE__ ) . '/includes/class-rest-activitypub-webfinger.php'; - add_action( 'rest_api_init', array( 'Rest_Activitypub_Webfinger', 'register_routes' ) ); - add_action( 'webfinger_user_data', array( 'Rest_Activitypub_Webfinger', 'add_webfinger_discovery' ), 10, 3 ); + require_once dirname( __FILE__ ) . '/includes/rest/class-webfinger.php'; + \Activitypub\Rest\Webfinger::init(); - require_once dirname( __FILE__ ) . '/includes/class-rest-activitypub-nodeinfo.php'; - add_action( 'rest_api_init', array( 'Rest_Activitypub_Nodeinfo', 'register_routes' ) ); - add_filter( 'nodeinfo_data', array( 'Rest_Activitypub_Nodeinfo', 'add_nodeinfo_discovery' ), 10, 2 ); - add_filter( 'nodeinfo2_data', array( 'Rest_Activitypub_Nodeinfo', 'add_nodeinfo2_discovery' ), 10 ); + require_once dirname( __FILE__ ) . '/includes/rest/class-nodeinfo.php'; + \Activitypub\Rest\NodeInfo::init(); add_post_type_support( 'post', 'activitypub' ); add_post_type_support( 'page', 'activitypub' ); $post_types = get_post_types_by_support( 'activitypub' ); - add_action( 'transition_post_status', array( 'Activitypub', 'schedule_post_activity' ), 10, 3 ); + add_action( 'transition_post_status', array( '\Activitypub\Activitypub', 'schedule_post_activity' ), 10, 3 ); - require_once dirname( __FILE__ ) . '/includes/class-activitypub-admin.php'; - 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' ) ); + require_once dirname( __FILE__ ) . '/includes/class-admin.php'; + \Activitypub\Admin::init(); if ( '1' === get_option( 'activitypub_use_hashtags', '1' ) ) { - require_once dirname( __FILE__ ) . '/includes/class-activitypub-hashtag.php'; - add_filter( 'wp_insert_post', array( 'Activitypub_Hashtag', 'insert_post' ), 99, 2 ); - add_filter( 'the_content', array( 'Activitypub_Hashtag', 'the_content' ), 99, 2 ); + require_once dirname( __FILE__ ) . '/includes/class-hashtag.php'; + \Activitypub\Hashtag::init(); } } -add_action( 'plugins_loaded', 'activitypub_init' ); +add_action( 'plugins_loaded', '\Activitypub\init' ); /** * Add rewrite rules */ -function activitypub_add_rewrite_rules() { +function add_rewrite_rules() { if ( ! class_exists( 'Webfinger' ) ) { add_rewrite_rule( '^.well-known/webfinger', 'index.php?rest_route=/activitypub/1.0/webfinger', 'top' ); } @@ -92,14 +76,14 @@ function activitypub_add_rewrite_rules() { add_rewrite_rule( '^.well-known/x-nodeinfo2', 'index.php?rest_route=/activitypub/1.0/nodeinfo2', 'top' ); } } -add_action( 'init', 'activitypub_add_rewrite_rules', 1 ); +add_action( 'init', '\Activitypub\add_rewrite_rules', 1 ); /** * Flush rewrite rules; */ -function activitypub_flush_rewrite_rules() { - activitypub_add_rewrite_rules(); +function flush_rewrite_rules() { + \Activitypub\add_rewrite_rules(); flush_rewrite_rules(); } -register_activation_hook( __FILE__, 'activitypub_flush_rewrite_rules' ); -register_deactivation_hook( __FILE__, 'flush_rewrite_rules' ); +register_activation_hook( __FILE__, '\Activitypub\flush_rewrite_rules' ); +register_deactivation_hook( __FILE__, '\flush_rewrite_rules' ); diff --git a/includes/class-activitypub-activity.php b/includes/class-activity.php similarity index 95% rename from includes/class-activitypub-activity.php rename to includes/class-activity.php index 8fb13c9..190dd96 100644 --- a/includes/class-activitypub-activity.php +++ b/includes/class-activity.php @@ -1,10 +1,12 @@ context = null; } elseif ( 'full' === $context ) { - $this->context = get_activitypub_context(); + $this->context = \Activitypub\get_context(); } $this->type = ucfirst( $type ); diff --git a/includes/class-activitypub.php b/includes/class-activitypub.php index 74b33e3..de4d35a 100644 --- a/includes/class-activitypub.php +++ b/includes/class-activitypub.php @@ -1,10 +1,18 @@ comment_type ) || - $id_or_email->user_id ) { + if ( + ! $id_or_email instanceof \WP_Comment || + ! isset( $id_or_email->comment_type ) || + $id_or_email->user_id + ) { return $args; } diff --git a/includes/class-activitypub-admin.php b/includes/class-admin.php similarity index 86% rename from includes/class-activitypub-admin.php rename to includes/class-admin.php index 1231920..9cacdd7 100644 --- a/includes/class-activitypub-admin.php +++ b/includes/class-admin.php @@ -1,8 +1,15 @@ + ?>
- ID ); } } diff --git a/includes/class-activitypub-hashtag.php b/includes/class-hashtag.php similarity index 77% rename from includes/class-activitypub-hashtag.php rename to includes/class-hashtag.php index efa3c2b..042f478 100644 --- a/includes/class-activitypub-hashtag.php +++ b/includes/class-hashtag.php @@ -1,8 +1,15 @@ post = get_post( $post ); } @@ -65,7 +75,7 @@ class Activitypub_Post { $max_images--; } // then list any image attachments - $query = new WP_Query( + $query = new \WP_Query( array( 'post_parent' => $id, 'post_status' => 'inherit', @@ -77,7 +87,7 @@ class Activitypub_Post { ) ); foreach ( $query->get_posts() as $attachment ) { - if ( ! in_array( $attachment->ID, $image_ids ) ) { + if ( ! in_array( $attachment->ID, $image_ids, true ) ) { $image_ids[] = $attachment->ID; } } @@ -89,7 +99,7 @@ class Activitypub_Post { if ( $thumbnail ) { $images[] = array( 'url' => $thumbnail[0], - 'type' => $mimetype + 'type' => $mimetype, ); } } @@ -100,9 +110,9 @@ class Activitypub_Post { if ( $images ) { foreach ( $images as $image ) { $attachment = array( - "type" => "Image", - "url" => $image['url'], - "mediaType" => $image['type'], + 'type' => 'Image', + 'url' => $image['url'], + 'mediaType' => $image['type'], ); $attachments[] = $attachment; } @@ -118,9 +128,9 @@ class Activitypub_Post { if ( $post_tags ) { foreach( $post_tags as $post_tag ) { $tag = array( - "type" => "Hashtag", - "href" => get_tag_link( $post_tag->term_id ), - "name" => '#' . $post_tag->slug, + 'type' => 'Hashtag', + 'href' => get_tag_link( $post_tag->term_id ), + 'name' => '#' . $post_tag->slug, ); $tags[] = $tag; } @@ -266,7 +276,7 @@ class Activitypub_Post { $allowed_html = apply_filters( 'activitypub_allowed_html', '' );
- return trim( preg_replace( '/[\r\n]{2,}/', '', strip_tags( $decoded_content, $allowed_html) ) );
+ return trim( preg_replace( '/[\r\n]{2,}/', '', strip_tags( $decoded_content, $allowed_html ) ) );
}
/**
@@ -286,7 +296,7 @@ class Activitypub_Post {
$allowed_html = apply_filters( 'activitypub_allowed_html', ' ' );
- return trim( preg_replace( '/[\r\n]{2,}/', '', strip_tags( $decoded_summary, $allowed_html) ) );
+ return trim( preg_replace( '/[\r\n]{2,}/', '', strip_tags( $decoded_summary, $allowed_html ) ) );
}
public static function add_backlink( $content, $post ) {
diff --git a/includes/class-activitypub-signature.php b/includes/class-signature.php
similarity index 98%
rename from includes/class-activitypub-signature.php
rename to includes/class-signature.php
index 7fc6a79..68ae556 100644
--- a/includes/class-activitypub-signature.php
+++ b/includes/class-signature.php
@@ -1,6 +1,7 @@
404
+ return new \WP_Error( 'invalid_actor_object', __( 'Unknown Actor schema', 'activitypub' ), array(
+ 'status' => 404,
) );
}
@@ -58,8 +59,8 @@ class Db_Activitypub_Followers {
$followers = get_user_option( 'activitypub_followers', $author_id );
foreach ( $followers as $key => $value ) {
- if ( $value === $actor) {
- unset( $followers[$key] );
+ if ( $value === $actor ) {
+ unset( $followers[ $key ] );
}
}
diff --git a/includes/functions.php b/includes/functions.php
index 2730c22..a6aef96 100644
--- a/includes/functions.php
+++ b/includes/functions.php
@@ -1,10 +1,12 @@
-
|
@@ -202,8 +204,8 @@ function activitypub_get_identifier_settings( $user_id ) {
\d+)/followers', array(
array(
- 'methods' => WP_REST_Server::READABLE,
- 'callback' => array( 'Rest_Activitypub_Followers', 'get' ),
+ 'methods' => \WP_REST_Server::READABLE,
+ 'callback' => array( '\Activitypub\Rest\Followers', 'get' ),
'args' => self::request_parameters(),
),
)
@@ -21,10 +26,11 @@ class Rest_Activitypub_Followers {
$user = get_user_by( 'ID', $user_id );
if ( ! $user ) {
- return new WP_Error( 'rest_invalid_param', __( 'User not found', 'activitypub' ), array(
- 'status' => 404, 'params' => array(
- 'user_id' => __( 'User not found', 'activitypub' )
- )
+ return new \WP_Error( 'rest_invalid_param', __( 'User not found', 'activitypub' ), array(
+ 'status' => 404,
+ 'params' => array(
+ 'user_id' => __( 'User not found', 'activitypub' ),
+ ),
) );
}
@@ -35,20 +41,20 @@ class Rest_Activitypub_Followers {
*/
do_action( 'activitypub_outbox_pre' );
- $json = new stdClass();
+ $json = new \stdClass();
$json->{'@context'} = get_activitypub_context();
- $followers = Db_Activitypub_Followers::get_followers( $user_id );
+ $followers = \Activitypub\Db\Followers::get_followers( $user_id );
if ( ! is_array( $followers ) ) {
$followers = array();
}
- $json->totlaItems = count( $followers );
- $json->orderedItems = $followers;
+ $json->totlaItems = count( $followers ); // phpcs:ignore
+ $json->orderedItems = $followers; // phpcs:ignore
- $response = new WP_REST_Response( $json, 200 );
+ $response = new \WP_REST_Response( $json, 200 );
$response->header( 'Content-Type', 'application/activity+json' );
return $response;
diff --git a/includes/class-rest-activitypub-inbox.php b/includes/rest/class-inbox.php
similarity index 69%
rename from includes/class-rest-activitypub-inbox.php
rename to includes/rest/class-inbox.php
index ba5cfaf..89cf6b8 100644
--- a/includes/class-rest-activitypub-inbox.php
+++ b/includes/rest/class-inbox.php
@@ -1,10 +1,21 @@
WP_REST_Server::EDITABLE,
- 'callback' => array( 'Rest_Activitypub_Inbox', 'global_inbox' ),
+ 'methods' => \WP_REST_Server::EDITABLE,
+ 'callback' => array( '\Activitypub\Rest\Inbox', 'global_inbox' ),
),
)
);
@@ -21,8 +32,8 @@ class Rest_Activitypub_Inbox {
register_rest_route(
'activitypub/1.0', '/users/(?P
-
+
|