From 40b2651b7e4ad2ce08fbfd5bb6c56a40d070f482 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Sun, 24 Feb 2019 12:07:41 +0100 Subject: [PATCH] big refactoring to use namespaces for a better code structure --- .gitignore | 1 + LICENSE | 2 +- README.md | 2 +- activitypub.php | 82 ++++++-------- ...itypub-activity.php => class-activity.php} | 6 +- includes/class-activitypub.php | 16 ++- ...-activitypub-admin.php => class-admin.php} | 17 ++- ...ivitypub-hashtag.php => class-hashtag.php} | 11 +- ...ss-activitypub-post.php => class-post.php} | 34 +++--- ...ypub-signature.php => class-signature.php} | 3 +- .../class-followers.php} | 13 +-- includes/functions.php | 50 ++++----- .../class-followers.php} | 30 +++--- .../class-inbox.php} | 51 +++++---- .../class-nodeinfo.php} | 52 +++++---- includes/rest/class-ostatus.php | 19 ++++ .../class-outbox.php} | 69 +++++++----- .../class-webfinger.php} | 19 ++-- languages/activitypub.pot | 100 +++++++++--------- readme.txt | 2 +- templates/json-author.php | 4 +- templates/json-post.php | 4 +- templates/settings-page.php | 6 +- tests/test-class-db-activitypub-followers.php | 2 +- 24 files changed, 342 insertions(+), 253 deletions(-) rename includes/{class-activitypub-activity.php => class-activity.php} (95%) rename includes/{class-activitypub-admin.php => class-admin.php} (86%) rename includes/{class-activitypub-hashtag.php => class-hashtag.php} (77%) rename includes/{class-activitypub-post.php => class-post.php} (91%) rename includes/{class-activitypub-signature.php => class-signature.php} (98%) rename includes/{class-db-activitypub-followers.php => db/class-followers.php} (83%) rename includes/{class-rest-activitypub-followers.php => rest/class-followers.php} (59%) rename includes/{class-rest-activitypub-inbox.php => rest/class-inbox.php} (69%) rename includes/{class-rest-activitypub-nodeinfo.php => rest/class-nodeinfo.php} (67%) create mode 100644 includes/rest/class-ostatus.php rename includes/{class-rest-activitypub-outbox.php => rest/class-outbox.php} (54%) rename includes/{class-rest-activitypub-webfinger.php => rest/class-webfinger.php} (69%) 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 @@ @@ -193,8 +195,8 @@ function activitypub_get_identifier_settings( $user_id ) { @@ -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\d+)/inbox', array( array( - 'methods' => WP_REST_Server::EDITABLE, - 'callback' => array( 'Rest_Activitypub_Inbox', 'user_inbox' ), + 'methods' => \WP_REST_Server::EDITABLE, + 'callback' => array( '\Activitypub\Rest\Inbox', 'user_inbox' ), 'args' => self::request_parameters(), ), ) @@ -56,7 +67,7 @@ class Rest_Activitypub_Inbox { $headers = $request->get_headers(); - //Activitypub_Signature::verify_signature( $headers, $key ); + //\Activitypub\Signature::verify_signature( $headers, $key ); return $served; } @@ -79,13 +90,13 @@ class Rest_Activitypub_Inbox { } if ( ! is_array( $data ) || ! array_key_exists( 'type', $data ) ) { - return new WP_Error( 'rest_invalid_data', __( 'Invalid payload', 'activitypub' ), array( 'status' => 422 ) ); + return new \WP_Error( 'rest_invalid_data', __( 'Invalid payload', 'activitypub' ), array( 'status' => 422 ) ); } do_action( 'activitypub_inbox', $data, $author_id, $type ); do_action( "activitypub_inbox_{$type}", $data, $author_id ); - return new WP_REST_Response( array(), 202 ); + return new \WP_REST_Response( array(), 202 ); } /** @@ -97,7 +108,7 @@ class Rest_Activitypub_Inbox { */ public static function global_inbox( $request ) { // Create the response object - return new WP_Error( 'rest_not_implemented', __( 'This method is not yet implemented', 'activitypub' ), array( 'status' => 501 ) ); + return new \WP_Error( 'rest_not_implemented', __( 'This method is not yet implemented', 'activitypub' ), array( 'status' => 501 ) ); } /** @@ -128,24 +139,24 @@ class Rest_Activitypub_Inbox { */ public static function handle_follow( $object, $user_id ) { if ( ! array_key_exists( 'actor', $object ) ) { - return new WP_Error( 'activitypub_no_actor', __( 'No "Actor" found', 'activitypub' ), $metadata ); + return new \WP_Error( 'activitypub_no_actor', __( 'No "Actor" found', 'activitypub' ), $metadata ); } // save follower - Db_Activitypub_Followers::add_follower( $object['actor'], $user_id ); + \Activitypub\Db\Followers::add_follower( $object['actor'], $user_id ); // get inbox - $inbox = activitypub_get_inbox_by_actor( $object['actor'] ); + $inbox = \Activitypub\get_inbox_by_actor( $object['actor'] ); // send "Accept" activity - $activity = new Activitypub_Activity( 'Accept', Activitypub_Activity::TYPE_SIMPLE ); + $activity = new \Activitypub\Activity( 'Accept', \Activitypub\Activity::TYPE_SIMPLE ); $activity->set_object( $object ); $activity->set_actor( get_author_posts_url( $user_id ) ); $activity->set_to( $object['actor'] ); $activity = $activity->to_simple_json(); - $response = activitypub_safe_remote_post( $inbox, $activity, $user_id ); + $response = \Activitypub\safe_remote_post( $inbox, $activity, $user_id ); } /** @@ -156,10 +167,10 @@ class Rest_Activitypub_Inbox { */ public static function handle_unfollow( $object, $user_id ) { if ( ! array_key_exists( 'actor', $object ) ) { - return new WP_Error( 'activitypub_no_actor', __( 'No "Actor" found', 'activitypub' ), $metadata ); + return new \WP_Error( 'activitypub_no_actor', __( 'No "Actor" found', 'activitypub' ), $metadata ); } - Db_Activitypub_Followers::remove_follower( $object['actor'], $user_id ); + \Activitypub\Db\Followers::remove_follower( $object['actor'], $user_id ); } /** @@ -170,10 +181,10 @@ class Rest_Activitypub_Inbox { */ public static function handle_reaction( $object, $user_id ) { if ( ! array_key_exists( 'actor', $object ) ) { - return new WP_Error( 'activitypub_no_actor', __( 'No "Actor" found', 'activitypub' ), $metadata ); + return new \WP_Error( 'activitypub_no_actor', __( 'No "Actor" found', 'activitypub' ), $metadata ); } - $meta = activitypub_get_remote_metadata_by_actor( $object['actor'] ); + $meta = \Activitypub\get_remote_metadata_by_actor( $object['actor'] ); $commentdata = array( 'comment_post_ID' => url_to_postid( $object['object'] ), @@ -207,10 +218,10 @@ class Rest_Activitypub_Inbox { */ public static function handle_create( $object, $user_id ) { if ( ! array_key_exists( 'actor', $object ) ) { - return new WP_Error( 'activitypub_no_actor', __( 'No "Actor" found', 'activitypub' ), $metadata ); + return new \WP_Error( 'activitypub_no_actor', __( 'No "Actor" found', 'activitypub' ), $metadata ); } - $meta = activitypub_get_remote_metadata_by_actor( $object['actor'] ); + $meta = \Activitypub\get_remote_metadata_by_actor( $object['actor'] ); $commentdata = array( 'comment_post_ID' => url_to_postid( $object['object']['inReplyTo'] ), diff --git a/includes/class-rest-activitypub-nodeinfo.php b/includes/rest/class-nodeinfo.php similarity index 67% rename from includes/class-rest-activitypub-nodeinfo.php rename to includes/rest/class-nodeinfo.php index 74ceb95..e63aace 100644 --- a/includes/class-rest-activitypub-nodeinfo.php +++ b/includes/rest/class-nodeinfo.php @@ -1,6 +1,12 @@ WP_REST_Server::READABLE, - 'callback' => array( 'Rest_Activitypub_Nodeinfo', 'discovery' ), + 'methods' => \WP_REST_Server::READABLE, + 'callback' => array( '\Activitypub\Rest\Nodeinfo', 'discovery' ), ), ) ); @@ -17,8 +23,8 @@ class Rest_Activitypub_Nodeinfo { register_rest_route( 'activitypub/1.0', '/nodeinfo', array( array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( 'Rest_Activitypub_Nodeinfo', 'nodeinfo' ), + 'methods' => \WP_REST_Server::READABLE, + 'callback' => array( '\Activitypub\Rest\Nodeinfo', 'nodeinfo' ), ), ) ); @@ -26,8 +32,8 @@ class Rest_Activitypub_Nodeinfo { register_rest_route( 'activitypub/1.0', '/nodeinfo2', array( array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( 'Rest_Activitypub_Nodeinfo', 'nodeinfo2' ), + 'methods' => \WP_REST_Server::READABLE, + 'callback' => array( '\Activitypub\Rest\Nodeinfo', 'nodeinfo2' ), ), ) ); @@ -37,6 +43,7 @@ class Rest_Activitypub_Nodeinfo { * Render NodeInfo file * * @param WP_REST_Request $request + * * @return WP_REST_Response */ public static function nodeinfo( $request ) { @@ -61,22 +68,23 @@ class Rest_Activitypub_Nodeinfo { ); $nodeinfo['openRegistrations'] = false; - $nodeinfo['protocols'] = array('activitypub'); + $nodeinfo['protocols'] = array( 'activitypub' ); $nodeinfo['services'] = array( 'inbound' => array(), 'outbound' => array(), ); - $nodeinfo['metadata'] = new stdClass; + $nodeinfo['metadata'] = new \stdClass(); - return new WP_REST_Response( $nodeinfo, 200 ); + return new \WP_REST_Response( $nodeinfo, 200 ); } /** * Render NodeInfo file * * @param WP_REST_Request $request + * * @return WP_REST_Response */ public static function nodeinfo2( $request ) { @@ -103,22 +111,23 @@ class Rest_Activitypub_Nodeinfo { ); $nodeinfo['openRegistrations'] = false; - $nodeinfo['protocols'] = array('activitypub'); + $nodeinfo['protocols'] = array( 'activitypub' ); $nodeinfo['services'] = array( 'inbound' => array(), 'outbound' => array(), ); - $nodeinfo['metadata'] = new stdClass; + $nodeinfo['metadata'] = new \stdClass(); - return new WP_REST_Response( $nodeinfo, 200 ); + return new \WP_REST_Response( $nodeinfo, 200 ); } /** * Render NodeInfo discovery file * * @param WP_REST_Request $request + * * @return WP_REST_Response */ public static function discovery( $request ) { @@ -130,33 +139,38 @@ class Rest_Activitypub_Nodeinfo { ), ); - return new WP_REST_Response( $discovery, 200 ); + return new \WP_REST_Response( $discovery, 200 ); } /** * Extend NodeInfo data * - * @param array $nodeinfo NodeInfo data - * @param array updated data + * @param array $nodeinfo NodeInfo data + * @param string The NodeInfo Version + * + * @return array The extended array */ public static function add_nodeinfo_discovery( $nodeinfo, $version ) { - if ( '2.0' == $version) { + if ( '2.0' === $version ) { $nodeinfo['protocols'][] = 'activitypub'; } else { $nodeinfo['protocols']['inbound'][] = 'activitypub'; $nodeinfo['protocols']['outbound'][] = 'activitypub'; } + return $nodeinfo; } /** * Extend NodeInfo2 data * - * @param array $nodeinfo NodeInfo2 data - * @param array updated data + * @param array $nodeinfo NodeInfo2 data + * + * @return array The extended array */ public static function add_nodeinfo2_discovery( $nodeinfo ) { $nodeinfo['protocols'][] = 'activitypub'; + return $nodeinfo; } } diff --git a/includes/rest/class-ostatus.php b/includes/rest/class-ostatus.php new file mode 100644 index 0000000..d959b3d --- /dev/null +++ b/includes/rest/class-ostatus.php @@ -0,0 +1,19 @@ + \WP_REST_Server::READABLE, + 'callback' => array( '\Activitypub\Rest\Ostatus', 'webfinger' ), + 'args' => self::request_parameters(), + ), + ) + ); + } +} diff --git a/includes/class-rest-activitypub-outbox.php b/includes/rest/class-outbox.php similarity index 54% rename from includes/class-rest-activitypub-outbox.php rename to includes/rest/class-outbox.php index 0abf285..42c18ed 100644 --- a/includes/class-rest-activitypub-outbox.php +++ b/includes/rest/class-outbox.php @@ -1,10 +1,22 @@ \d+)/outbox', array( array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( 'Rest_Activitypub_Outbox', 'user_outbox' ), + 'methods' => \WP_REST_Server::READABLE, + 'callback' => array( '\Activitypub\Rest\Outbox', 'user_outbox' ), 'args' => self::request_parameters(), ), ) @@ -32,10 +44,11 @@ class Rest_Activitypub_Outbox { $author = get_user_by( 'ID', $user_id ); if ( ! $author ) { - 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' ), + ), ) ); } @@ -46,17 +59,17 @@ class Rest_Activitypub_Outbox { */ do_action( 'activitypub_outbox_pre' ); - $json = new stdClass(); + $json = new \stdClass(); - $json->{'@context'} = get_activitypub_context(); - $json->id = home_url( add_query_arg( NULL, NULL ) ); + $json->{'@context'} = \Activitypub\get_context(); + $json->id = home_url( add_query_arg( null, null ) ); $json->generator = 'http://wordpress.org/?v=' . get_bloginfo_rss( 'version' ); $json->actor = get_author_posts_url( $user_id ); $json->type = 'OrderedCollectionPage'; $json->partOf = get_rest_url( null, "/activitypub/1.0/users/$user_id/outbox" ); // phpcs:ignore $count_posts = wp_count_posts(); - $json->totalItems = intval( $count_posts->publish ); + $json->totalItems = intval( $count_posts->publish ); // phpcs:ignore $posts = get_posts( array( 'posts_per_page' => 10, @@ -64,16 +77,16 @@ class Rest_Activitypub_Outbox { 'offset' => $page * 10, ) ); - $json->first = add_query_arg( 'page', 0, $json->partOf ); - $json->last = add_query_arg( 'page', ( ceil ( $json->totalItems / 10 ) ) - 1, $json->partOf ); + $json->first = add_query_arg( 'page', 0, $json->partOf ); // phpcs:ignore + $json->last = add_query_arg( 'page', ( ceil ( $json->totalItems / 10 ) ) - 1, $json->partOf ); // phpcs:ignore - if ( ( ceil ( $json->totalItems / 10 ) ) - 1 > $page ) { - $json->next = add_query_arg( 'page', ++$page, $json->partOf ); + if ( ( ceil ( $json->totalItems / 10 ) ) - 1 > $page ) { // phpcs:ignore + $json->next = add_query_arg( 'page', ++$page, $json->partOf ); // phpcs:ignore } foreach ( $posts as $post ) { - $activitypub_post = new Activitypub_Post( $post ); - $activitypub_activity = new Activitypub_Activity( 'Create', Activitypub_Activity::TYPE_NONE ); + $activitypub_post = new \Activitypub\Post( $post ); + $activitypub_activity = new \Activitypub\Activity( 'Create', \Activitypub\Activity::TYPE_NONE ); $activitypub_activity->from_post( $activitypub_post->to_array() ); $json->orderedItems[] = $activitypub_activity->to_array(); // phpcs:ignore } @@ -86,7 +99,7 @@ class Rest_Activitypub_Outbox { */ do_action( 'activitypub_outbox_post' ); - $response = new WP_REST_Response( $json, 200 ); + $response = new \WP_REST_Response( $json, 200 ); $response->header( 'Content-Type', 'application/activity+json' ); @@ -112,16 +125,16 @@ class Rest_Activitypub_Outbox { $post = get_post( $post_id ); $user_id = $post->post_author; - $activitypub_post = new Activitypub_Post( $post ); - $activitypub_activity = new Activitypub_Activity( 'Create', Activitypub_Activity::TYPE_FULL ); + $activitypub_post = new \Activitypub\Post( $post ); + $activitypub_activity = new \Activitypub\Activity( 'Create', \Activitypub\Activity::TYPE_FULL ); $activitypub_activity->from_post( $activitypub_post->to_array() ); $activity = $activitypub_activity->to_json(); // phpcs:ignore - $followers = Db_Activitypub_Followers::get_followers( $user_id ); + $followers = \Activitypub\Db\Followers::get_followers( $user_id ); - foreach ( activitypub_get_follower_inboxes( $user_id, $followers ) as $inbox ) { - $response = activitypub_safe_remote_post( $inbox, $activity, $user_id ); + foreach ( \Activitypub\get_follower_inboxes( $user_id, $followers ) as $inbox ) { + $response = \Activitypub\safe_remote_post( $inbox, $activity, $user_id ); } } @@ -129,16 +142,16 @@ class Rest_Activitypub_Outbox { $post = get_post( $post_id ); $user_id = $post->post_author; - $activitypub_post = new Activitypub_Post( $post ); - $activitypub_activity = new Activitypub_Activity( 'Update', Activitypub_Activity::TYPE_FULL ); + $activitypub_post = new \Activitypub\Post( $post ); + $activitypub_activity = new \Activitypub\Activity( 'Update', \Activitypub\Activity::TYPE_FULL ); $activitypub_activity->from_post( $activitypub_post->to_array() ); $activity = $activitypub_activity->to_json(); // phpcs:ignore - $followers = Db_Activitypub_Followers::get_followers( $user_id ); + $followers = \Activitypub\Db\Followers::get_followers( $user_id ); - foreach ( activitypub_get_follower_inboxes( $user_id, $followers ) as $inbox ) { - $response = activitypub_safe_remote_post( $inbox, $activity, $user_id ); + foreach ( \Activitypub\get_follower_inboxes( $user_id, $followers ) as $inbox ) { + $response = \Activitypub\safe_remote_post( $inbox, $activity, $user_id ); } } } diff --git a/includes/class-rest-activitypub-webfinger.php b/includes/rest/class-webfinger.php similarity index 69% rename from includes/class-rest-activitypub-webfinger.php rename to includes/rest/class-webfinger.php index eb194c5..a3d13b0 100644 --- a/includes/class-rest-activitypub-webfinger.php +++ b/includes/rest/class-webfinger.php @@ -1,6 +1,11 @@ WP_REST_Server::READABLE, - 'callback' => array( 'Rest_Activitypub_Webfinger', 'webfinger' ), + 'methods' => \WP_REST_Server::READABLE, + 'callback' => array( '\Activitypub\Rest\Webfinger', 'webfinger' ), 'args' => self::request_parameters(), ), ) @@ -29,20 +34,20 @@ class Rest_Activitypub_Webfinger { $matched = preg_match( '/^acct:([^@]+)@(.+)$/', $resource, $matches ); if ( ! $matched ) { - return new WP_Error( 'activitypub_unsupported_resource', __( 'Resouce is invalid', 'activitypub' ), array( 'status' => 400 ) ); + return new \WP_Error( 'activitypub_unsupported_resource', __( 'Resouce is invalid', 'activitypub' ), array( 'status' => 400 ) ); } $resource_identifier = $matches[1]; $resource_host = $matches[2]; if ( wp_parse_url( home_url( '/' ), PHP_URL_HOST ) !== $resource_host ) { - return new WP_Error( 'activitypub_wrong_host', __( 'Resouce host does not match blog host', 'activitypub' ), array( 'status' => 404 ) ); + return new \WP_Error( 'activitypub_wrong_host', __( 'Resouce host does not match blog host', 'activitypub' ), array( 'status' => 404 ) ); } $user = get_user_by( 'login', esc_sql( $resource_identifier ) ); if ( ! $user ) { - 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( @@ -64,7 +69,7 @@ class Rest_Activitypub_Webfinger { ), ); - return new WP_REST_Response( $json, 200 ); + return new \WP_REST_Response( $json, 200 ); } /** diff --git a/languages/activitypub.pot b/languages/activitypub.pot index 5984606..552d1b7 100644 --- a/languages/activitypub.pot +++ b/languages/activitypub.pot @@ -2,9 +2,9 @@ # This file is distributed under the MIT. msgid "" msgstr "" -"Project-Id-Version: ActivityPub 0.4.3\n" +"Project-Id-Version: ActivityPub 0.5.0\n" "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/activitypub\n" -"POT-Creation-Date: 2019-02-20 20:05:05+00:00\n" +"POT-Creation-Date: 2019-02-24 11:06:41+00:00\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" @@ -13,24 +13,24 @@ msgstr "" "Language-Team: LANGUAGE \n" "X-Generator: grunt-wp-i18n1.0.2\n" -#: includes/class-activitypub-admin.php:35 +#: includes/class-admin.php:42 msgid "Use summary or full content" msgstr "" -#: includes/class-activitypub-admin.php:47 +#: includes/class-admin.php:54 msgid "The Activity-Object-Type" msgstr "" -#: includes/class-activitypub-admin.php:59 -#: includes/class-activitypub-admin.php:66 templates/settings-page.php:33 +#: includes/class-admin.php:66 includes/class-admin.php:73 +#: templates/settings-page.php:33 msgid "Use the Shortlink instead of the permalink" msgstr "" -#: includes/class-activitypub-admin.php:76 +#: includes/class-admin.php:83 msgid "Overview" msgstr "" -#: includes/class-activitypub-admin.php:78 +#: includes/class-admin.php:85 msgid "" "ActivityPub is a decentralized social networking protocol based on the " "ActivityStreams 2.0 data format. ActivityPub is an official W3C recommended " @@ -40,91 +40,87 @@ msgid "" "subscribing to content." msgstr "" -#: includes/class-activitypub-admin.php:83 +#: includes/class-admin.php:90 msgid "For more information:" msgstr "" -#: includes/class-activitypub-admin.php:84 +#: includes/class-admin.php:91 msgid "Test Suite" msgstr "" -#: includes/class-activitypub-admin.php:85 +#: includes/class-admin.php:92 msgid "W3C Spec" msgstr "" -#: includes/class-activitypub-admin.php:86 +#: includes/class-admin.php:93 msgid "" "Give " "us feedback" msgstr "" -#: includes/class-activitypub-admin.php:88 +#: includes/class-admin.php:95 msgid "Donate" msgstr "" -#: includes/class-activitypub-admin.php:94 +#: includes/class-admin.php:101 msgid "Fediverse" msgstr "" -#: includes/class-db-activitypub-followers.php:41 +#: includes/db/class-followers.php:42 msgid "Unknown Actor schema" msgstr "" -#: includes/class-rest-activitypub-followers.php:24 -#: includes/class-rest-activitypub-followers.php:26 -#: includes/class-rest-activitypub-outbox.php:35 -#: includes/class-rest-activitypub-outbox.php:37 -#: includes/class-rest-activitypub-webfinger.php:45 -msgid "User not found" -msgstr "" - -#: includes/class-rest-activitypub-inbox.php:82 -msgid "Invalid payload" -msgstr "" - -#: includes/class-rest-activitypub-inbox.php:100 -msgid "This method is not yet implemented" -msgstr "" - -#: includes/class-rest-activitypub-inbox.php:131 -#: includes/class-rest-activitypub-inbox.php:159 -#: includes/class-rest-activitypub-inbox.php:173 -#: includes/class-rest-activitypub-inbox.php:210 -msgid "No \"Actor\" found" -msgstr "" - -#: includes/class-rest-activitypub-webfinger.php:32 -msgid "Resouce is invalid" -msgstr "" - -#: includes/class-rest-activitypub-webfinger.php:39 -msgid "Resouce host does not match blog host" -msgstr "" - -#: includes/functions.php:97 +#: includes/functions.php:99 msgid "The \"actor\" is no valid URL" msgstr "" -#: includes/functions.php:121 +#: includes/functions.php:123 msgid "No valid JSON data" msgstr "" -#: includes/functions.php:149 +#: includes/functions.php:151 msgid "No \"Inbox\" found" msgstr "" -#: includes/functions.php:175 +#: includes/functions.php:177 msgid "No \"Public-Key\" found" msgstr "" -#: includes/functions.php:193 +#: includes/functions.php:195 msgid "Profile identifier" msgstr "" -#: includes/functions.php:197 +#: includes/functions.php:199 msgid "Try to follow \"@%s\" in the mastodon/friendi.ca search field." msgstr "" +#: includes/rest/class-followers.php:29 includes/rest/class-followers.php:32 +#: includes/rest/class-outbox.php:47 includes/rest/class-outbox.php:50 +#: includes/rest/class-webfinger.php:50 +msgid "User not found" +msgstr "" + +#: includes/rest/class-inbox.php:93 +msgid "Invalid payload" +msgstr "" + +#: includes/rest/class-inbox.php:111 +msgid "This method is not yet implemented" +msgstr "" + +#: includes/rest/class-inbox.php:142 includes/rest/class-inbox.php:170 +#: includes/rest/class-inbox.php:184 includes/rest/class-inbox.php:221 +msgid "No \"Actor\" found" +msgstr "" + +#: includes/rest/class-webfinger.php:37 +msgid "Resouce is invalid" +msgstr "" + +#: includes/rest/class-webfinger.php:44 +msgid "Resouce host does not match blog host" +msgstr "" + #: templates/json-author.php:49 msgid "Blog" msgstr "" diff --git a/readme.txt b/readme.txt index 82d824e..11ed2b3 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Donate link: https://notiz.blog/donate/ 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/templates/json-author.php b/templates/json-author.php index b924b01..11c93bf 100644 --- a/templates/json-author.php +++ b/templates/json-author.php @@ -3,7 +3,7 @@ $author_id = get_the_author_meta( 'ID' ); $json = new stdClass(); -$json->{'@context'} = get_activitypub_context(); +$json->{'@context'} = \Activitypub\get_context(); $json->id = get_author_posts_url( $author_id ); $json->type = 'Person'; $json->name = get_the_author_meta( 'display_name', $author_id ); @@ -38,7 +38,7 @@ $json->manuallyApprovesFollowers = apply_filters( 'activitypub_json_manually_app $json->publicKey = array( 'id' => get_author_posts_url( $author_id ) . '#main-key', 'owner' => get_author_posts_url( $author_id ), - 'publicKeyPem' => trim( Activitypub_Signature::get_public_key( $author_id ) ), + 'publicKeyPem' => trim( \Activitypub\Signature::get_public_key( $author_id ) ), ); $json->tag = array(); diff --git a/templates/json-post.php b/templates/json-post.php index e600270..f6b5ef4 100644 --- a/templates/json-post.php +++ b/templates/json-post.php @@ -1,8 +1,8 @@ from_post( $activitypub_post->to_array() ); // filter output diff --git a/templates/settings-page.php b/templates/settings-page.php index 03d165a..577571e 100644 --- a/templates/settings-page.php +++ b/templates/settings-page.php @@ -80,7 +80,7 @@

- + @@ -95,9 +95,9 @@
-

or

-

+

or

+

- +
    - +
diff --git a/tests/test-class-db-activitypub-followers.php b/tests/test-class-db-activitypub-followers.php index 61f0f3d..eaeecbd 100644 --- a/tests/test-class-db-activitypub-followers.php +++ b/tests/test-class-db-activitypub-followers.php @@ -9,7 +9,7 @@ class Test_Db_Activitypub_Followers extends WP_UnitTestCase { ); update_user_meta( 1, 'activitypub_followers', $followers ); - $db_followers = Db_Activitypub_Followers::get_followers( 1 ); + $db_followers = \Activitypub\Db\Followers::get_followers( 1 ); $this->assertEquals( 3, count( $db_followers ) );