explicit use of global functions

This commit is contained in:
Matthias Pfefferle 2019-09-27 10:12:59 +02:00
parent 91f0dd9841
commit 2f5bf1ccc4
23 changed files with 406 additions and 364 deletions

View file

@ -19,7 +19,7 @@ namespace Activitypub;
* Initialize plugin * Initialize plugin
*/ */
function init() { function init() {
defined( 'ACTIVITYPUB_HASHTAGS_REGEXP' ) || define( 'ACTIVITYPUB_HASHTAGS_REGEXP', '(?:(?<=\s)|^)#(\w*[A-Za-z_]+\w*)' ); \defined( 'ACTIVITYPUB_HASHTAGS_REGEXP' ) || \define( 'ACTIVITYPUB_HASHTAGS_REGEXP', '(?:(?<=\s)|^)#(\w*[A-Za-z_]+\w*)' );
require_once dirname( __FILE__ ) . '/includes/table/followers-list.php'; require_once dirname( __FILE__ ) . '/includes/table/followers-list.php';
require_once dirname( __FILE__ ) . '/includes/class-signature.php'; require_once dirname( __FILE__ ) . '/includes/class-signature.php';
@ -70,16 +70,16 @@ add_action( 'plugins_loaded', '\Activitypub\init' );
* Add rewrite rules * Add rewrite rules
*/ */
function add_rewrite_rules() { function add_rewrite_rules() {
if ( ! class_exists( 'Webfinger' ) ) { if ( ! \class_exists( 'Webfinger' ) ) {
add_rewrite_rule( '^.well-known/webfinger', 'index.php?rest_route=/activitypub/1.0/webfinger', 'top' ); \add_rewrite_rule( '^.well-known/webfinger', 'index.php?rest_route=/activitypub/1.0/webfinger', 'top' );
} }
if ( ! class_exists( 'Nodeinfo' ) ) { if ( ! \class_exists( 'Nodeinfo' ) ) {
add_rewrite_rule( '^.well-known/nodeinfo', 'index.php?rest_route=/activitypub/1.0/nodeinfo/discovery', 'top' ); \add_rewrite_rule( '^.well-known/nodeinfo', 'index.php?rest_route=/activitypub/1.0/nodeinfo/discovery', 'top' );
add_rewrite_rule( '^.well-known/x-nodeinfo2', 'index.php?rest_route=/activitypub/1.0/nodeinfo2', 'top' ); \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; * Flush rewrite rules;
@ -88,5 +88,5 @@ function flush_rewrite_rules() {
\Activitypub\add_rewrite_rules(); \Activitypub\add_rewrite_rules();
\flush_rewrite_rules(); \flush_rewrite_rules();
} }
register_activation_hook( __FILE__, '\Activitypub\flush_rewrite_rules' ); \register_activation_hook( __FILE__, '\Activitypub\flush_rewrite_rules' );
register_deactivation_hook( __FILE__, '\flush_rewrite_rules' ); \register_deactivation_hook( __FILE__, '\flush_rewrite_rules' );

View file

@ -3,7 +3,7 @@
"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.",
"type": "wordpress-plugin", "type": "wordpress-plugin",
"require": { "require": {
"php": ">=5.3.0", "php": ">=5.6.0",
"composer/installers": "~1.0" "composer/installers": "~1.0"
}, },
"require-dev": { "require-dev": {

View file

@ -13,9 +13,9 @@ class Activity_Dispatcher {
* Initialize the class, registering WordPress hooks * Initialize the class, registering WordPress hooks
*/ */
public static function init() { public static function init() {
add_action( 'activitypub_send_post_activity', array( '\Activitypub\Activity_Dispatcher', 'send_post_activity' ) ); \add_action( 'activitypub_send_post_activity', array( '\Activitypub\Activity_Dispatcher', 'send_post_activity' ) );
add_action( 'activitypub_send_update_activity', array( '\Activitypub\Activity_Dispatcher', 'send_update_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_delete_activity', array( '\Activitypub\Activity_Dispatcher', 'send_delete_activity' ) );
} }
/** /**
@ -24,7 +24,7 @@ class Activity_Dispatcher {
* @param int $post_id * @param int $post_id
*/ */
public static function send_post_activity( $post_id ) { public static function send_post_activity( $post_id ) {
$post = get_post( $post_id ); $post = \get_post( $post_id );
$user_id = $post->post_author; $user_id = $post->post_author;
$activitypub_post = new \Activitypub\Post( $post ); $activitypub_post = new \Activitypub\Post( $post );
@ -45,7 +45,7 @@ class Activity_Dispatcher {
* @param int $post_id * @param int $post_id
*/ */
public static function send_update_activity( $post_id ) { public static function send_update_activity( $post_id ) {
$post = get_post( $post_id ); $post = \get_post( $post_id );
$user_id = $post->post_author; $user_id = $post->post_author;
$activitypub_post = new \Activitypub\Post( $post ); $activitypub_post = new \Activitypub\Post( $post );
@ -66,7 +66,7 @@ class Activity_Dispatcher {
* @param int $post_id * @param int $post_id
*/ */
public static function send_delete_activity( $post_id ) { public static function send_delete_activity( $post_id ) {
$post = get_post( $post_id ); $post = \get_post( $post_id );
$user_id = $post->post_author; $user_id = $post->post_author;
$activitypub_post = new \Activitypub\Post( $post ); $activitypub_post = new \Activitypub\Post( $post );

View file

@ -29,18 +29,18 @@ class Activity {
$this->context = \Activitypub\get_context(); $this->context = \Activitypub\get_context();
} }
$this->type = ucfirst( $type ); $this->type = \ucfirst( $type );
$this->published = date( 'Y-m-d\TH:i:s\Z', strtotime( 'now' ) ); $this->published = \date( 'Y-m-d\TH:i:s\Z', strtotime( 'now' ) );
} }
public function __call( $method, $params ) { public function __call( $method, $params ) {
$var = strtolower( substr( $method, 4 ) ); $var = \strtolower( \substr( $method, 4 ) );
if ( strncasecmp( $method, 'get', 3 ) === 0 ) { if ( \strncasecmp( $method, 'get', 3 ) === 0 ) {
return $this->$var; return $this->$var;
} }
if ( strncasecmp( $method, 'set', 3 ) === 0 ) { if ( \strncasecmp( $method, 'set', 3 ) === 0 ) {
$this->$var = $params[0]; $this->$var = $params[0];
} }
} }
@ -57,7 +57,7 @@ class Activity {
} }
public function to_array() { public function to_array() {
$array = get_object_vars( $this ); $array = \get_object_vars( $this );
if ( $this->context ) { if ( $this->context ) {
$array = array( '@context' => $this->context ) + $array; $array = array( '@context' => $this->context ) + $array;
@ -69,7 +69,7 @@ class Activity {
} }
public function to_json() { public function to_json() {
return wp_json_encode( $this->to_array(), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_QUOT ); return \wp_json_encode( $this->to_array(), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_QUOT );
} }
public function to_simple_array() { public function to_simple_array() {
@ -90,6 +90,6 @@ class Activity {
} }
public function to_simple_json() { public function to_simple_json() {
return wp_json_encode( $this->to_simple_array(), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_QUOT ); return \wp_json_encode( $this->to_simple_array(), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_QUOT );
} }
} }

View file

@ -11,15 +11,15 @@ class Activitypub {
* Initialize the class, registering WordPress hooks * Initialize the class, registering WordPress hooks
*/ */
public static function init() { public static function init() {
add_filter( 'template_include', array( '\Activitypub\Activitypub', 'render_json_template' ), 99 ); \add_filter( 'template_include', array( '\Activitypub\Activitypub', 'render_json_template' ), 99 );
add_filter( 'query_vars', array( '\Activitypub\Activitypub', 'add_query_vars' ) ); \add_filter( 'query_vars', array( '\Activitypub\Activitypub', 'add_query_vars' ) );
add_action( 'init', array( '\Activitypub\Activitypub', 'add_rewrite_endpoint' ) ); \add_action( 'init', array( '\Activitypub\Activitypub', 'add_rewrite_endpoint' ) );
add_filter( 'pre_get_avatar_data', array( '\Activitypub\Activitypub', 'pre_get_avatar_data' ), 11, 2 ); \add_filter( 'pre_get_avatar_data', array( '\Activitypub\Activitypub', 'pre_get_avatar_data' ), 11, 2 );
add_post_type_support( 'post', 'activitypub' ); \add_post_type_support( 'post', 'activitypub' );
add_post_type_support( 'page', 'activitypub' ); \add_post_type_support( 'page', 'activitypub' );
add_action( 'transition_post_status', array( '\Activitypub\Activitypub', 'schedule_post_activity' ), 10, 3 ); \add_action( 'transition_post_status', array( '\Activitypub\Activitypub', 'schedule_post_activity' ), 10, 3 );
} }
/** /**
@ -30,14 +30,14 @@ class Activitypub {
* @return string the new path to the JSON template * @return string the new path to the JSON template
*/ */
public static function render_json_template( $template ) { public static function render_json_template( $template ) {
if ( ! is_author() && ! is_singular() ) { if ( ! \is_author() && ! \is_singular() ) {
return $template; return $template;
} }
if ( is_author() ) { if ( \is_author() ) {
$json_template = dirname( __FILE__ ) . '/../templates/json-author.php'; $json_template = \dirname( __FILE__ ) . '/../templates/json-author.php';
} elseif ( is_singular() ) { } elseif ( \is_singular() ) {
$json_template = dirname( __FILE__ ) . '/../templates/json-post.php'; $json_template = \dirname( __FILE__ ) . '/../templates/json-post.php';
} }
global $wp_query; global $wp_query;
@ -51,19 +51,19 @@ class Activitypub {
} }
// interpret accept header // interpret accept header
$pos = stripos( $_SERVER['HTTP_ACCEPT'], ';' ); $pos = \stripos( $_SERVER['HTTP_ACCEPT'], ';' );
if ( $pos ) { if ( $pos ) {
$accept_header = substr( $_SERVER['HTTP_ACCEPT'], 0, $pos ); $accept_header = \substr( $_SERVER['HTTP_ACCEPT'], 0, $pos );
} else { } else {
$accept_header = $_SERVER['HTTP_ACCEPT']; $accept_header = $_SERVER['HTTP_ACCEPT'];
} }
// accept header as an array // accept header as an array
$accept = explode( ',', trim( $accept_header ) ); $accept = \explode( ',', trim( $accept_header ) );
if ( if (
! in_array( 'application/activity+json', $accept, true ) && ! \in_array( 'application/activity+json', $accept, true ) &&
! in_array( 'application/ld+json', $accept, true ) && ! \in_array( 'application/ld+json', $accept, true ) &&
! in_array( 'application/json', $accept, true ) ! \in_array( 'application/json', $accept, true )
) { ) {
return $template; return $template;
} }
@ -85,7 +85,7 @@ class Activitypub {
* Add our rewrite endpoint to permalinks and pages. * Add our rewrite endpoint to permalinks and pages.
*/ */
public static function add_rewrite_endpoint() { public static function add_rewrite_endpoint() {
add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES ); \add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES );
} }
/** /**
@ -95,20 +95,20 @@ class Activitypub {
*/ */
public static function schedule_post_activity( $new_status, $old_status, $post ) { public static function schedule_post_activity( $new_status, $old_status, $post ) {
// do not send activities if post is password protected // do not send activities if post is password protected
if ( post_password_required( $post ) ) { if ( \post_password_required( $post ) ) {
return; return;
} }
// check if post-type supports ActivityPub // check if post-type supports ActivityPub
$post_types = get_post_types_by_support( 'activitypub' ); $post_types = \get_post_types_by_support( 'activitypub' );
if ( ! in_array( $post->post_type, $post_types, true ) ) { if ( ! \in_array( $post->post_type, $post_types, true ) ) {
return; return;
} }
if ( 'publish' === $new_status && 'publish' !== $old_status ) { if ( 'publish' === $new_status && 'publish' !== $old_status ) {
wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_post_activity', array( $post->ID ) ); \wp_schedule_single_event( time() + \wp_rand( 0, 120 ), 'activitypub_send_post_activity', array( $post->ID ) );
} elseif ( 'publish' === $new_status ) { } elseif ( 'publish' === $new_status ) {
wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_update_activity', array( $post->ID ) ); \wp_schedule_single_event( time() + \wp_rand( 0, 120 ), 'activitypub_send_update_activity', array( $post->ID ) );
} }
} }
@ -129,22 +129,22 @@ class Activitypub {
return $args; return $args;
} }
$allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); $allowed_comment_types = \apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types, true ) ) { if ( ! empty( $id_or_email->comment_type ) && ! \in_array( $id_or_email->comment_type, (array) $allowed_comment_types, true ) ) {
$args['url'] = false; $args['url'] = false;
/** This filter is documented in wp-includes/link-template.php */ /** This filter is documented in wp-includes/link-template.php */
return apply_filters( 'get_avatar_data', $args, $id_or_email ); return \apply_filters( 'get_avatar_data', $args, $id_or_email );
} }
// check if comment has an avatar // check if comment has an avatar
$avatar = self::get_avatar_url( $id_or_email->comment_ID ); $avatar = self::get_avatar_url( $id_or_email->comment_ID );
if ( $avatar ) { if ( $avatar ) {
if ( ! isset( $args['class'] ) || ! is_array( $args['class'] ) ) { if ( ! isset( $args['class'] ) || ! \is_array( $args['class'] ) ) {
$args['class'] = array( 'u-photo' ); $args['class'] = array( 'u-photo' );
} else { } else {
$args['class'][] = 'u-photo'; $args['class'][] = 'u-photo';
$args['class'] = array_unique( $args['class'] ); $args['class'] = \array_unique( $args['class'] );
} }
$args['url'] = $avatar; $args['url'] = $avatar;
$args['class'][] = 'avatar-activitypub'; $args['class'][] = 'avatar-activitypub';
@ -162,9 +162,9 @@ class Activitypub {
* @return string $url * @return string $url
*/ */
public static function get_avatar_url( $comment ) { public static function get_avatar_url( $comment ) {
if ( is_numeric( $comment ) ) { if ( \is_numeric( $comment ) ) {
$comment = get_comment( $comment ); $comment = \get_comment( $comment );
} }
return get_comment_meta( $comment->comment_ID, 'avatar_url', true ); return \get_comment_meta( $comment->comment_ID, 'avatar_url', true );
} }
} }

View file

@ -11,16 +11,16 @@ class Admin {
* Initialize the class, registering WordPress hooks * Initialize the class, registering WordPress hooks
*/ */
public static function init() { public static function init() {
add_action( 'admin_menu', array( '\Activitypub\Admin', 'admin_menu' ) ); \add_action( 'admin_menu', array( '\Activitypub\Admin', 'admin_menu' ) );
add_action( 'admin_init', array( '\Activitypub\Admin', 'register_settings' ) ); \add_action( 'admin_init', array( '\Activitypub\Admin', 'register_settings' ) );
add_action( 'show_user_profile', array( '\Activitypub\Admin', 'add_fediverse_profile' ) ); \add_action( 'show_user_profile', array( '\Activitypub\Admin', 'add_fediverse_profile' ) );
} }
/** /**
* Add admin menu entry * Add admin menu entry
*/ */
public static function admin_menu() { public static function admin_menu() {
$settings_page = add_options_page( $settings_page = \add_options_page(
'ActivityPub', 'ActivityPub',
'ActivityPub', 'ActivityPub',
'manage_options', 'manage_options',
@ -28,35 +28,35 @@ class Admin {
array( '\Activitypub\Admin', 'settings_page' ) array( '\Activitypub\Admin', 'settings_page' )
); );
add_action( 'load-' . $settings_page, array( '\Activitypub\Admin', 'add_settings_help_tab' ) ); \add_action( 'load-' . $settings_page, array( '\Activitypub\Admin', '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( '\Activitypub\Admin', 'followers_list_page' ) );
add_action( 'load-' . $followers_list_page, array( '\Activitypub\Admin', 'add_followers_list_help_tab' ) ); \add_action( 'load-' . $followers_list_page, array( '\Activitypub\Admin', 'add_followers_list_help_tab' ) );
} }
/** /**
* Load settings page * Load settings page
*/ */
public static function settings_page() { public static function settings_page() {
load_template( dirname( __FILE__ ) . '/../templates/settings.php' ); \load_template( dirname( __FILE__ ) . '/../templates/settings.php' );
} }
/** /**
* Load user settings page * Load user settings page
*/ */
public static function followers_list_page() { public static function followers_list_page() {
load_template( dirname( __FILE__ ) . '/../templates/followers-list.php' ); \load_template( dirname( __FILE__ ) . '/../templates/followers-list.php' );
} }
/** /**
* Register PubSubHubbub settings * Register PubSubHubbub settings
*/ */
public static function register_settings() { public static function register_settings() {
register_setting( \register_setting(
'activitypub', 'activitypub_post_content_type', array( 'activitypub', 'activitypub_post_content_type', array(
'type' => 'string', 'type' => 'string',
'description' => __( 'Use summary or full content', 'activitypub' ), 'description' => \__( 'Use summary or full content', 'activitypub' ),
'show_in_rest' => array( 'show_in_rest' => array(
'schema' => array( 'schema' => array(
'enum' => array( 'excerpt', 'content' ), 'enum' => array( 'excerpt', 'content' ),
@ -65,10 +65,10 @@ class Admin {
'default' => 'content', 'default' => 'content',
) )
); );
register_setting( \register_setting(
'activitypub', 'activitypub_object_type', array( 'activitypub', 'activitypub_object_type', array(
'type' => 'string', 'type' => 'string',
'description' => __( 'The Activity-Object-Type', 'activitypub' ), 'description' => \__( 'The Activity-Object-Type', 'activitypub' ),
'show_in_rest' => array( 'show_in_rest' => array(
'schema' => array( 'schema' => array(
'enum' => array( 'note', 'article', 'wordpress-post-format' ), 'enum' => array( 'note', 'article', 'wordpress-post-format' ),
@ -77,46 +77,46 @@ class Admin {
'default' => 'note', 'default' => 'note',
) )
); );
register_setting( \register_setting(
'activitypub', 'activitypub_use_shortlink', array( 'activitypub', 'activitypub_use_shortlink', array(
'type' => 'boolean', 'type' => 'boolean',
'description' => __( 'Use the Shortlink instead of the permalink', 'activitypub' ), 'description' => \__( 'Use the Shortlink instead of the permalink', 'activitypub' ),
'default' => 0, 'default' => 0,
) )
); );
register_setting( \register_setting(
'activitypub', 'activitypub_use_hashtags', array( 'activitypub', 'activitypub_use_hashtags', array(
'type' => 'boolean', 'type' => 'boolean',
'description' => __( 'Add hashtags in the content as native tags and replace the #tag with the tag-link', 'activitypub' ), 'description' => \__( 'Add hashtags in the content as native tags and replace the #tag with the tag-link', 'activitypub' ),
'default' => 0, 'default' => 0,
) )
); );
register_setting( \register_setting(
'activitypub', 'activitypub_add_tags_as_hashtags', array( 'activitypub', 'activitypub_add_tags_as_hashtags', array(
'type' => 'boolean', 'type' => 'boolean',
'description' => __( 'Add all tags as hashtags at the end of each activity', 'activitypub' ), 'description' => \__( 'Add all tags as hashtags at the end of each activity', 'activitypub' ),
'default' => 0, 'default' => 0,
) )
); );
} }
public static function add_settings_help_tab() { public static function add_settings_help_tab() {
get_current_screen()->add_help_tab( \get_current_screen()->add_help_tab(
array( array(
'id' => 'overview', 'id' => 'overview',
'title' => __( 'Overview', 'activitypub' ), 'title' => \__( 'Overview', 'activitypub' ),
'content' => 'content' =>
'<p>' . __( 'ActivityPub is a decentralized social networking protocol based on the ActivityStreams 2.0 data format. ActivityPub is an official W3C recommended standard published by the W3C Social Web Working Group. It provides a client to server API for creating, updating and deleting content, as well as a federated server to server API for delivering notifications and subscribing to content.', 'activitypub' ) . '</p>', '<p>' . \__( 'ActivityPub is a decentralized social networking protocol based on the ActivityStreams 2.0 data format. ActivityPub is an official W3C recommended standard published by the W3C Social Web Working Group. It provides a client to server API for creating, updating and deleting content, as well as a federated server to server API for delivering notifications and subscribing to content.', 'activitypub' ) . '</p>',
) )
); );
get_current_screen()->set_help_sidebar( \get_current_screen()->set_help_sidebar(
'<p><strong>' . __( 'For more information:', 'activitypub' ) . '</strong></p>' . '<p><strong>' . \__( 'For more information:', 'activitypub' ) . '</strong></p>' .
'<p>' . __( '<a href="https://activitypub.rocks/">Test Suite</a>', 'activitypub' ) . '</p>' . '<p>' . \__( '<a href="https://activitypub.rocks/">Test Suite</a>', 'activitypub' ) . '</p>' .
'<p>' . __( '<a href="https://www.w3.org/TR/activitypub/">W3C Spec</a>', 'activitypub' ) . '</p>' . '<p>' . \__( '<a href="https://www.w3.org/TR/activitypub/">W3C Spec</a>', 'activitypub' ) . '</p>' .
'<p>' . __( '<a href="https://github.com/pfefferle/wordpress-activitypub/issues">Give us feedback</a>', 'activitypub' ) . '</p>' . '<p>' . \__( '<a href="https://github.com/pfefferle/wordpress-activitypub/issues">Give us feedback</a>', 'activitypub' ) . '</p>' .
'<hr />' . '<hr />' .
'<p>' . __( '<a href="https://notiz.blog/donate">Donate</a>', 'activitypub' ) . '</p>' '<p>' . \__( '<a href="https://notiz.blog/donate">Donate</a>', 'activitypub' ) . '</p>'
); );
} }
@ -126,7 +126,7 @@ class Admin {
public static function add_fediverse_profile( $user ) { public static function add_fediverse_profile( $user ) {
?> ?>
<h2><?php esc_html_e( 'Fediverse', 'activitypub' ); ?></h2> <h2><?php \esc_html_e( 'Fediverse', 'activitypub' ); ?></h2>
<?php <?php
\Activitypub\get_identifier_settings( $user->ID ); \Activitypub\get_identifier_settings( $user->ID );
} }

30
includes/class-debug.php Normal file
View file

@ -0,0 +1,30 @@
<?php
namespace Activitypub;
/**
* ActivityPub Debug Class
*
* @author Matthias Pfefferle
*/
class Debug {
/**
* Initialize the class, registering WordPress hooks
*/
public static function init() {
if ( WP_DEBUG_LOG ) {
\add_action( 'activitypub_safe_remote_post_response', array( '\Activitypub\Debug', 'log_remote_post_responses' ), 10, 4 );
}
}
public static function log_remote_post_responses( $response, $url, $body, $user_id ) {
\error_log( "Request to: {$url} with response: " . \print_r( $response, 'true' ) );
}
public static function write_log( $log ) {
if ( is_array( $log ) || is_object( $log ) ) {
\error_log( \print_r( $log, true ) );
} else {
\error_log( $log );
}
}
}

View file

@ -11,13 +11,13 @@ class Hashtag {
* Initialize the class, registering WordPress hooks * Initialize the class, registering WordPress hooks
*/ */
public static function init() { public static function init() {
if ( '1' === get_option( 'activitypub_use_hashtags', '1' ) ) { if ( '1' === \get_option( 'activitypub_use_hashtags', '1' ) ) {
add_filter( 'wp_insert_post', array( '\Activitypub\Hashtag', 'insert_post' ), 99, 2 ); \add_filter( 'wp_insert_post', array( '\Activitypub\Hashtag', 'insert_post' ), 99, 2 );
add_filter( 'the_content', array( '\Activitypub\Hashtag', 'the_content' ), 99, 2 ); \add_filter( 'the_content', array( '\Activitypub\Hashtag', 'the_content' ), 99, 2 );
} }
if ( '1' === get_option( 'activitypub_add_tags_as_hashtags', '0' ) ) { if ( '1' === \get_option( 'activitypub_add_tags_as_hashtags', '0' ) ) {
add_filter( 'activitypub_the_summary', array( '\Activitypub\Hashtag', 'add_hashtags_to_content' ), 10, 2 ); \add_filter( 'activitypub_the_summary', array( '\Activitypub\Hashtag', 'add_hashtags_to_content' ), 10, 2 );
add_filter( 'activitypub_the_content', array( '\Activitypub\Hashtag', 'add_hashtags_to_content' ), 10, 2 ); \add_filter( 'activitypub_the_content', array( '\Activitypub\Hashtag', 'add_hashtags_to_content' ), 10, 2 );
} }
} }
@ -30,10 +30,10 @@ class Hashtag {
* @return * @return
*/ */
public static function insert_post( $id, $data ) { public static function insert_post( $id, $data ) {
if ( preg_match_all( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', $data->post_content, $match ) ) { if ( \preg_match_all( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', $data->post_content, $match ) ) {
$tags = implode( ', ', $match[1] ); $tags = \implode( ', ', $match[1] );
wp_add_post_tags( $data->post_parent, $tags ); \wp_add_post_tags( $data->post_parent, $tags );
} }
return $id; return $id;
@ -47,7 +47,7 @@ class Hashtag {
* @return string the filtered post-content * @return string the filtered post-content
*/ */
public static function the_content( $the_content ) { public static function the_content( $the_content ) {
$the_content = preg_replace_callback( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', array( '\Activitypub\Hashtag', 'replace_with_links' ), $the_content ); $the_content = \preg_replace_callback( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', array( '\Activitypub\Hashtag', 'replace_with_links' ), $the_content );
return $the_content; return $the_content;
} }
@ -60,11 +60,11 @@ class Hashtag {
*/ */
public static function replace_with_links( $result ) { public static function replace_with_links( $result ) {
$tag = $result[1]; $tag = $result[1];
$tag_object = get_term_by( 'name', $tag, 'post_tag' ); $tag_object = \get_term_by( 'name', $tag, 'post_tag' );
if ( $tag_object ) { if ( $tag_object ) {
$link = get_term_link( $tag_object, 'post_tag' ); $link = \get_term_link( $tag_object, 'post_tag' );
return sprintf( '<a rel="tag" class="u-tag u-category" href="%s">#%s</a>', $link, $tag ); return \sprintf( '<a rel="tag" class="u-tag u-category" href="%s">#%s</a>', $link, $tag );
} }
return '#' . $tag; return '#' . $tag;
@ -79,7 +79,7 @@ class Hashtag {
* @return string * @return string
*/ */
public static function add_hashtags_to_content( $content, $post ) { public static function add_hashtags_to_content( $content, $post ) {
$tags = get_the_tags( $post->ID ); $tags = \get_the_tags( $post->ID );
if ( ! $tags ) { if ( ! $tags ) {
return $content; return $content;
@ -88,9 +88,9 @@ class Hashtag {
$hash_tags = array(); $hash_tags = array();
foreach ( $tags as $tag ) { foreach ( $tags as $tag ) {
$hash_tags[] = sprintf( '<a rel="tag" class="u-tag u-category" href="%s">#%s</a>', get_tag_link( $tag ), $tag->slug ); $hash_tags[] = \sprintf( '<a rel="tag" class="u-tag u-category" href="%s">#%s</a>', \get_tag_link( $tag ), $tag->slug );
} }
return $content . '<p>' . implode( ' ', $hash_tags ) . '</p>'; return $content . '<p>' . \implode( ' ', $hash_tags ) . '</p>';
} }
} }

View file

@ -0,0 +1,12 @@
<?php
namespace Activitypub;
/**
* ActivityPub Health_Check Class
*
* @author Matthias Pfefferle
*/
class Health_Check {
public static function init() {
}
}

View file

@ -13,12 +13,12 @@ class Post {
* Initialize the class, registering WordPress hooks * Initialize the class, registering WordPress hooks
*/ */
public static function init() { public static function init() {
add_filter( 'activitypub_the_summary', array( '\Activitypub\Post', 'add_backlink_to_content' ), 15, 2 ); \add_filter( 'activitypub_the_summary', array( '\Activitypub\Post', 'add_backlink_to_content' ), 15, 2 );
add_filter( 'activitypub_the_content', array( '\Activitypub\Post', 'add_backlink_to_content' ), 15, 2 ); \add_filter( 'activitypub_the_content', array( '\Activitypub\Post', 'add_backlink_to_content' ), 15, 2 );
} }
public function __construct( $post = null ) { public function __construct( $post = null ) {
$this->post = get_post( $post ); $this->post = \get_post( $post );
} }
public function get_post() { public function get_post() {
@ -33,15 +33,15 @@ class Post {
$post = $this->post; $post = $this->post;
$array = array( $array = array(
'id' => get_permalink( $post ), 'id' => \get_permalink( $post ),
'type' => $this->get_object_type(), 'type' => $this->get_object_type(),
'published' => date( 'Y-m-d\TH:i:s\Z', strtotime( $post->post_date ) ), 'published' => \date( 'Y-m-d\TH:i:s\Z', \strtotime( $post->post_date ) ),
'attributedTo' => get_author_posts_url( $post->post_author ), 'attributedTo' => \get_author_posts_url( $post->post_author ),
'summary' => $this->get_the_summary(), 'summary' => $this->get_the_summary(),
'inReplyTo' => null, 'inReplyTo' => null,
'content' => $this->get_the_content(), 'content' => $this->get_the_content(),
'contentMap' => array( 'contentMap' => array(
strstr( get_locale(), '_', true ) => $this->get_the_content(), \strstr( \get_locale(), '_', true ) => $this->get_the_content(),
), ),
'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ), 'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
'cc' => array( 'https://www.w3.org/ns/activitystreams#Public' ), 'cc' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
@ -49,15 +49,15 @@ class Post {
'tag' => $this->get_tags(), 'tag' => $this->get_tags(),
); );
return apply_filters( 'activitypub_post', $array ); return \apply_filters( 'activitypub_post', $array );
} }
public function to_json() { public function to_json() {
return wp_json_encode( $this->to_array(), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_QUOT ); return \wp_json_encode( $this->to_array(), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_QUOT );
} }
public function get_attachments() { public function get_attachments() {
$max_images = apply_filters( 'activitypub_max_images', 3 ); $max_images = \apply_filters( 'activitypub_max_images', 3 );
$images = array(); $images = array();
@ -70,8 +70,8 @@ class Post {
$image_ids = array(); $image_ids = array();
// list post thumbnail first if this post has one // list post thumbnail first if this post has one
if ( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail( $id ) ) { if ( \function_exists( 'has_post_thumbnail' ) && \has_post_thumbnail( $id ) ) {
$image_ids[] = get_post_thumbnail_id( $id ); $image_ids[] = \get_post_thumbnail_id( $id );
$max_images--; $max_images--;
} }
// then list any image attachments // then list any image attachments
@ -87,14 +87,14 @@ class Post {
) )
); );
foreach ( $query->get_posts() as $attachment ) { foreach ( $query->get_posts() as $attachment ) {
if ( ! in_array( $attachment->ID, $image_ids, true ) ) { if ( ! \in_array( $attachment->ID, $image_ids, true ) ) {
$image_ids[] = $attachment->ID; $image_ids[] = $attachment->ID;
} }
} }
// get URLs for each image // get URLs for each image
foreach ( $image_ids as $id ) { foreach ( $image_ids as $id ) {
$thumbnail = wp_get_attachment_image_src( $id, 'full' ); $thumbnail = \wp_get_attachment_image_src( $id, 'full' );
$mimetype = get_post_mime_type( $id ); $mimetype = \get_post_mime_type( $id );
if ( $thumbnail ) { if ( $thumbnail ) {
$images[] = array( $images[] = array(
@ -124,12 +124,12 @@ class Post {
public function get_tags() { public function get_tags() {
$tags = array(); $tags = array();
$post_tags = get_the_tags( $this->post->ID ); $post_tags = \get_the_tags( $this->post->ID );
if ( $post_tags ) { if ( $post_tags ) {
foreach ( $post_tags as $post_tag ) { foreach ( $post_tags as $post_tag ) {
$tag = array( $tag = array(
'type' => 'Hashtag', 'type' => 'Hashtag',
'href' => get_tag_link( $post_tag->term_id ), 'href' => \get_tag_link( $post_tag->term_id ),
'name' => '#' . $post_tag->slug, 'name' => '#' . $post_tag->slug,
); );
$tags[] = $tag; $tags[] = $tag;
@ -148,14 +148,14 @@ class Post {
* @return string the object-type * @return string the object-type
*/ */
public function get_object_type() { public function get_object_type() {
if ( 'wordpress-post-format' !== get_option( 'activitypub_object_type', 'note' ) ) { if ( 'wordpress-post-format' !== \get_option( 'activitypub_object_type', 'note' ) ) {
return ucfirst( get_option( 'activitypub_object_type', 'note' ) ); return \ucfirst( \get_option( 'activitypub_object_type', 'note' ) );
} }
$post_type = get_post_type( $this->post ); $post_type = \get_post_type( $this->post );
switch ( $post_type ) { switch ( $post_type ) {
case 'post': case 'post':
$post_format = get_post_format( $this->post ); $post_format = \get_post_format( $this->post );
switch ( $post_format ) { switch ( $post_format ) {
case 'aside': case 'aside':
case 'status': case 'status':
@ -182,8 +182,8 @@ class Post {
$object_type = 'Page'; $object_type = 'Page';
break; break;
case 'attachment': case 'attachment':
$mime_type = get_post_mime_type(); $mime_type = \get_post_mime_type();
$media_type = preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type ); $media_type = \preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type );
switch ( $media_type ) { switch ( $media_type ) {
case 'audio': case 'audio':
$object_type = 'Audio'; $object_type = 'Audio';
@ -205,7 +205,7 @@ class Post {
} }
public function get_the_content() { public function get_the_content() {
if ( 'excerpt' === get_option( 'activitypub_post_content_type', 'content' ) ) { if ( 'excerpt' === \get_option( 'activitypub_post_content_type', 'content' ) ) {
return $this->get_the_post_summary(); return $this->get_the_post_summary();
} }
@ -216,7 +216,7 @@ class Post {
if ( 'Article' === $this->get_object_type() ) { if ( 'Article' === $this->get_object_type() ) {
$excerpt = $this->get_the_post_excerpt( 400 ); $excerpt = $this->get_the_post_excerpt( 400 );
return html_entity_decode( $excerpt, ENT_QUOTES, 'UTF-8' ); return \html_entity_decode( $excerpt, ENT_QUOTES, 'UTF-8' );
} }
return null; return null;
@ -232,27 +232,27 @@ class Post {
public function get_the_post_excerpt( $excerpt_length = 400 ) { public function get_the_post_excerpt( $excerpt_length = 400 ) {
$post = $this->post; $post = $this->post;
$excerpt = get_post_field( 'post_excerpt', $post ); $excerpt = \get_post_field( 'post_excerpt', $post );
if ( '' === $excerpt ) { if ( '' === $excerpt ) {
$content = get_post_field( 'post_content', $post ); $content = \get_post_field( 'post_content', $post );
// An empty string will make wp_trim_excerpt do stuff we do not want. // An empty string will make wp_trim_excerpt do stuff we do not want.
if ( '' !== $content ) { if ( '' !== $content ) {
$excerpt = strip_shortcodes( $content ); $excerpt = \strip_shortcodes( $content );
/** This filter is documented in wp-includes/post-template.php */ /** This filter is documented in wp-includes/post-template.php */
$excerpt = apply_filters( 'the_content', $excerpt ); $excerpt = \apply_filters( 'the_content', $excerpt );
$excerpt = str_replace( ']]>', ']]>', $excerpt ); $excerpt = \str_replace( ']]>', ']]>', $excerpt );
$excerpt_length = apply_filters( 'excerpt_length', $excerpt_length ); $excerpt_length = \apply_filters( 'excerpt_length', $excerpt_length );
/** This filter is documented in wp-includes/formatting.php */ /** This filter is documented in wp-includes/formatting.php */
$excerpt_more = apply_filters( 'excerpt_more', ' [...]' ); $excerpt_more = \apply_filters( 'excerpt_more', ' [...]' );
$excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more ); $excerpt = \wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
} }
} }
@ -264,19 +264,19 @@ class Post {
* *
* @return string The content. * @return string The content.
*/ */
public function get_the_post_content( $with_link = true ) { public function get_the_post_content() {
$post = $this->post; $post = $this->post;
$content = get_post_field( 'post_content', $post ); $content = \get_post_field( 'post_content', $post );
$filtered_content = apply_filters( 'the_content', $content ); $filtered_content = \apply_filters( 'the_content', $content );
$filtered_content = apply_filters( 'activitypub_the_content', $filtered_content, $this->post ); $filtered_content = \apply_filters( 'activitypub_the_content', $filtered_content, $this->post );
$decoded_content = html_entity_decode( $filtered_content, ENT_QUOTES, 'UTF-8' ); $decoded_content = \html_entity_decode( $filtered_content, ENT_QUOTES, 'UTF-8' );
$allowed_html = apply_filters( 'activitypub_allowed_html', '<a><p>' ); $allowed_html = \apply_filters( 'activitypub_allowed_html', '<a><p>' );
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 ) ) );
} }
/** /**
@ -289,14 +289,14 @@ class Post {
public function get_the_post_summary( $summary_length = 400 ) { public function get_the_post_summary( $summary_length = 400 ) {
$summary = $this->get_the_post_excerpt( $summary_length ); $summary = $this->get_the_post_excerpt( $summary_length );
$filtered_summary = apply_filters( 'the_excerpt', $summary ); $filtered_summary = \apply_filters( 'the_excerpt', $summary );
$filtered_summary = apply_filters( 'activitypub_the_summary', $filtered_summary, $this->post ); $filtered_summary = \apply_filters( 'activitypub_the_summary', $filtered_summary, $this->post );
$decoded_summary = html_entity_decode( $filtered_summary, ENT_QUOTES, 'UTF-8' ); $decoded_summary = \html_entity_decode( $filtered_summary, ENT_QUOTES, 'UTF-8' );
$allowed_html = apply_filters( 'activitypub_allowed_html', '<a><p>' ); $allowed_html = \apply_filters( 'activitypub_allowed_html', '<a><p>' );
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 ) ) );
} }
/** /**
@ -310,10 +310,10 @@ class Post {
public static function add_backlink_to_content( $content, $post ) { public static function add_backlink_to_content( $content, $post ) {
$link = ''; $link = '';
if ( get_option( 'activitypub_use_shortlink', 0 ) ) { if ( \get_option( 'activitypub_use_shortlink', 0 ) ) {
$link = esc_url( wp_get_shortlink( $post->ID ) ); $link = \esc_url( \wp_get_shortlink( $post->ID ) );
} else { } else {
$link = esc_url( get_permalink( $post->ID ) ); $link = \esc_url( \get_permalink( $post->ID ) );
} }
return $content . '<p><a href="' . $link . '">' . $link . '</a></p>'; return $content . '<p><a href="' . $link . '">' . $link . '</a></p>';

View file

@ -14,14 +14,14 @@ class Signature {
* @return mixed * @return mixed
*/ */
public static function get_public_key( $user_id, $force = false ) { public static function get_public_key( $user_id, $force = false ) {
$key = get_user_meta( $user_id, 'magic_sig_public_key' ); $key = \get_user_meta( $user_id, 'magic_sig_public_key' );
if ( $key && ! $force ) { if ( $key && ! $force ) {
return $key[0]; return $key[0];
} }
self::generate_key_pair( $user_id ); self::generate_key_pair( $user_id );
$key = get_user_meta( $user_id, 'magic_sig_public_key' ); $key = \get_user_meta( $user_id, 'magic_sig_public_key' );
return $key[0]; return $key[0];
} }
@ -32,14 +32,14 @@ class Signature {
* @return mixed * @return mixed
*/ */
public static function get_private_key( $user_id, $force = false ) { public static function get_private_key( $user_id, $force = false ) {
$key = get_user_meta( $user_id, 'magic_sig_private_key' ); $key = \get_user_meta( $user_id, 'magic_sig_private_key' );
if ( $key && ! $force ) { if ( $key && ! $force ) {
return $key[0]; return $key[0];
} }
self::generate_key_pair( $user_id ); self::generate_key_pair( $user_id );
$key = get_user_meta( $user_id, 'magic_sig_private_key' ); $key = \get_user_meta( $user_id, 'magic_sig_private_key' );
return $key[0]; return $key[0];
} }
@ -56,24 +56,24 @@ class Signature {
'private_key_type' => OPENSSL_KEYTYPE_RSA, 'private_key_type' => OPENSSL_KEYTYPE_RSA,
); );
$key = openssl_pkey_new( $config ); $key = \openssl_pkey_new( $config );
$priv_key = null; $priv_key = null;
openssl_pkey_export( $key, $priv_key ); \openssl_pkey_export( $key, $priv_key );
// private key // private key
update_user_meta( $user_id, 'magic_sig_private_key', $priv_key ); \update_user_meta( $user_id, 'magic_sig_private_key', $priv_key );
$detail = openssl_pkey_get_details( $key ); $detail = \openssl_pkey_get_details( $key );
// public key // public key
update_user_meta( $user_id, 'magic_sig_public_key', $detail['key'] ); \update_user_meta( $user_id, 'magic_sig_public_key', $detail['key'] );
} }
public static function generate_signature( $user_id, $url, $date ) { public static function generate_signature( $user_id, $url, $date ) {
$key = self::get_private_key( $user_id ); $key = self::get_private_key( $user_id );
$url_parts = wp_parse_url( $url ); $url_parts = \wp_parse_url( $url );
$host = $url_parts['host']; $host = $url_parts['host'];
$path = '/'; $path = '/';
@ -91,12 +91,12 @@ class Signature {
$signed_string = "(request-target): post $path\nhost: $host\ndate: $date"; $signed_string = "(request-target): post $path\nhost: $host\ndate: $date";
$signature = null; $signature = null;
openssl_sign( $signed_string, $signature, $key, OPENSSL_ALGO_SHA256 ); \openssl_sign( $signed_string, $signature, $key, OPENSSL_ALGO_SHA256 );
$signature = base64_encode( $signature ); // phpcs:ignore $signature = \base64_encode( $signature ); // phpcs:ignore
$key_id = get_author_posts_url( $user_id ) . '#main-key'; $key_id = \get_author_posts_url( $user_id ) . '#main-key';
return sprintf( 'keyId="%s",algorithm="rsa-sha256",headers="(request-target) host date",signature="%s"', $key_id, $signature ); return \sprintf( 'keyId="%s",algorithm="rsa-sha256",headers="(request-target) host date",signature="%s"', $key_id, $signature );
} }
public static function verify_signature( $headers, $signature ) { public static function verify_signature( $headers, $signature ) {

View file

@ -9,7 +9,7 @@ namespace Activitypub\Db;
class Followers { class Followers {
public static function get_followers( $author_id ) { public static function get_followers( $author_id ) {
$followers = get_user_option( 'activitypub_followers', $author_id ); $followers = \get_user_option( 'activitypub_followers', $author_id );
if ( ! $followers ) { if ( ! $followers ) {
return array(); return array();
@ -17,11 +17,11 @@ class Followers {
foreach ( $followers as $key => $follower ) { foreach ( $followers as $key => $follower ) {
if ( if (
is_array( $follower ) && \is_array( $follower ) &&
isset( $follower['type'] ) && isset( $follower['type'] ) &&
'Person' === $follower['type'] && 'Person' === $follower['type'] &&
isset( $follower['id'] ) && isset( $follower['id'] ) &&
false !== filter_var( $follower['id'], FILTER_VALIDATE_URL ) false !== \filter_var( $follower['id'], FILTER_VALIDATE_URL )
) { ) {
$followers[ $key ] = $follower['id']; $followers[ $key ] = $follower['id'];
} }
@ -33,41 +33,41 @@ class Followers {
public static function count_followers( $author_id ) { public static function count_followers( $author_id ) {
$followers = self::get_followers( $author_id ); $followers = self::get_followers( $author_id );
return count( $followers ); return \count( $followers );
} }
public static function add_follower( $actor, $author_id ) { public static function add_follower( $actor, $author_id ) {
$followers = get_user_option( 'activitypub_followers', $author_id ); $followers = \get_user_option( 'activitypub_followers', $author_id );
if ( ! is_string( $actor ) ) { if ( ! \is_string( $actor ) ) {
if ( if (
is_array( $actor ) && \is_array( $actor ) &&
isset( $actor['type'] ) && isset( $actor['type'] ) &&
'Person' === $actor['type'] && 'Person' === $actor['type'] &&
isset( $actor['id'] ) && isset( $actor['id'] ) &&
false !== filter_var( $actor['id'], FILTER_VALIDATE_URL ) false !== \filter_var( $actor['id'], FILTER_VALIDATE_URL )
) { ) {
$actor = $actor['id']; $actor = $actor['id'];
} }
return new \WP_Error( 'invalid_actor_object', __( 'Unknown Actor schema', 'activitypub' ), array( return new \WP_Error( 'invalid_actor_object', \__( 'Unknown Actor schema', 'activitypub' ), array(
'status' => 404, 'status' => 404,
) ); ) );
} }
if ( ! is_array( $followers ) ) { if ( ! \is_array( $followers ) ) {
$followers = array( $actor ); $followers = array( $actor );
} else { } else {
$followers[] = $actor; $followers[] = $actor;
} }
$followers = array_unique( $followers ); $followers = \array_unique( $followers );
update_user_meta( $author_id, 'activitypub_followers', $followers ); \update_user_meta( $author_id, 'activitypub_followers', $followers );
} }
public static function remove_follower( $actor, $author_id ) { public static function remove_follower( $actor, $author_id ) {
$followers = get_user_option( 'activitypub_followers', $author_id ); $followers = \get_user_option( 'activitypub_followers', $author_id );
foreach ( $followers as $key => $value ) { foreach ( $followers as $key => $value ) {
if ( $value === $actor ) { if ( $value === $actor ) {
@ -75,6 +75,6 @@ class Followers {
} }
} }
update_user_meta( $author_id, 'activitypub_followers', $followers ); \update_user_meta( $author_id, 'activitypub_followers', $followers );
} }
} }

View file

@ -38,15 +38,15 @@ function get_context() {
), ),
); );
return apply_filters( 'activitypub_json_context', $context ); return \apply_filters( 'activitypub_json_context', $context );
} }
function safe_remote_post( $url, $body, $user_id ) { function safe_remote_post( $url, $body, $user_id ) {
$date = gmdate( 'D, d M Y H:i:s T' ); $date = \gmdate( 'D, d M Y H:i:s T' );
$signature = \Activitypub\Signature::generate_signature( $user_id, $url, $date ); $signature = \Activitypub\Signature::generate_signature( $user_id, $url, $date );
$wp_version = get_bloginfo( 'version' ); $wp_version = \get_bloginfo( 'version' );
$user_agent = apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) ); $user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );
$args = array( $args = array(
'timeout' => 100, 'timeout' => 100,
'limit_response_size' => 1048576, 'limit_response_size' => 1048576,
@ -61,9 +61,9 @@ function safe_remote_post( $url, $body, $user_id ) {
'body' => $body, 'body' => $body,
); );
$response = wp_safe_remote_post( $url, $args ); $response = \wp_safe_remote_post( $url, $args );
do_action( 'activitypub_safe_remote_post_response', $response, $url, $body, $user_id ); \do_action( 'activitypub_safe_remote_post_response', $response, $url, $body, $user_id );
return $response; return $response;
} }
@ -77,13 +77,13 @@ function safe_remote_post( $url, $body, $user_id ) {
*/ */
function get_webfinger_resource( $user_id ) { function get_webfinger_resource( $user_id ) {
// use WebFinger plugin if installed // use WebFinger plugin if installed
if ( function_exists( '\get_webfinger_resource' ) ) { if ( \function_exists( '\get_webfinger_resource' ) ) {
return \get_webfinger_resource( $user_id, false ); return \get_webfinger_resource( $user_id, false );
} }
$user = get_user_by( 'id', $user_id ); $user = \get_user_by( 'id', $user_id );
return $user->user_login . '@' . wp_parse_url( home_url(), PHP_URL_HOST ); return $user->user_login . '@' . \wp_parse_url( \home_url(), PHP_URL_HOST );
} }
/** /**
@ -94,19 +94,19 @@ function get_webfinger_resource( $user_id ) {
* @return array * @return array
*/ */
function get_remote_metadata_by_actor( $actor ) { function get_remote_metadata_by_actor( $actor ) {
$metadata = get_transient( 'activitypub_' . $actor ); $metadata = \get_transient( 'activitypub_' . $actor );
if ( $metadata ) { if ( $metadata ) {
return $metadata; return $metadata;
} }
if ( ! wp_http_validate_url( $actor ) ) { if ( ! \wp_http_validate_url( $actor ) ) {
return new \WP_Error( 'activitypub_no_valid_actor_url', __( 'The "actor" is no valid URL', 'activitypub' ), $actor ); return new \WP_Error( 'activitypub_no_valid_actor_url', \__( 'The "actor" is no valid URL', 'activitypub' ), $actor );
} }
$wp_version = get_bloginfo( 'version' ); $wp_version = \get_bloginfo( 'version' );
$user_agent = apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) ); $user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );
$args = array( $args = array(
'timeout' => 100, 'timeout' => 100,
'limit_response_size' => 1048576, 'limit_response_size' => 1048576,
@ -115,20 +115,20 @@ function get_remote_metadata_by_actor( $actor ) {
'headers' => array( 'accept' => 'application/activity+json' ), 'headers' => array( 'accept' => 'application/activity+json' ),
); );
$response = wp_safe_remote_get( $actor, $args ); $response = \wp_safe_remote_get( $actor, $args );
if ( is_wp_error( $response ) ) { if ( \is_wp_error( $response ) ) {
return $response; return $response;
} }
$metadata = wp_remote_retrieve_body( $response ); $metadata = \wp_remote_retrieve_body( $response );
$metadata = json_decode( $metadata, true ); $metadata = \json_decode( $metadata, true );
if ( ! $metadata ) { if ( ! $metadata ) {
return new \WP_Error( 'activitypub_invalid_json', __( 'No valid JSON data', 'activitypub' ), $actor ); return new \WP_Error( 'activitypub_invalid_json', \__( 'No valid JSON data', 'activitypub' ), $actor );
} }
set_transient( 'activitypub_' . $actor, $metadata, WEEK_IN_SECONDS ); \set_transient( 'activitypub_' . $actor, $metadata, WEEK_IN_SECONDS );
return $metadata; return $metadata;
} }
@ -141,7 +141,7 @@ function get_remote_metadata_by_actor( $actor ) {
function get_inbox_by_actor( $actor ) { function get_inbox_by_actor( $actor ) {
$metadata = \Activitypub\get_remote_metadata_by_actor( $actor ); $metadata = \Activitypub\get_remote_metadata_by_actor( $actor );
if ( is_wp_error( $metadata ) ) { if ( \is_wp_error( $metadata ) ) {
return $metadata; return $metadata;
} }
@ -149,7 +149,7 @@ function get_inbox_by_actor( $actor ) {
return $metadata['endpoints']['sharedInbox']; return $metadata['endpoints']['sharedInbox'];
} }
if ( array_key_exists( 'inbox', $metadata ) ) { if ( \array_key_exists( 'inbox', $metadata ) ) {
return $metadata['inbox']; return $metadata['inbox'];
} }
@ -164,7 +164,7 @@ function get_inbox_by_actor( $actor ) {
function get_publickey_by_actor( $actor, $key_id ) { function get_publickey_by_actor( $actor, $key_id ) {
$metadata = \Activitypub\get_remote_metadata_by_actor( $actor ); $metadata = \Activitypub\get_remote_metadata_by_actor( $actor );
if ( is_wp_error( $metadata ) ) { if ( \is_wp_error( $metadata ) ) {
return $metadata; return $metadata;
} }
@ -179,7 +179,7 @@ function get_publickey_by_actor( $actor, $key_id ) {
return $metadata['publicKey']['publicKeyPem']; return $metadata['publicKey']['publicKeyPem'];
} }
return new \WP_Error( 'activitypub_no_public_key', __( 'No "Public-Key" found', 'activitypub' ), $metadata ); return new \WP_Error( 'activitypub_no_public_key', \__( 'No "Public-Key" found', 'activitypub' ), $metadata );
} }
function get_follower_inboxes( $user_id ) { function get_follower_inboxes( $user_id ) {
@ -188,7 +188,7 @@ function get_follower_inboxes( $user_id ) {
foreach ( $followers as $follower ) { foreach ( $followers as $follower ) {
$inbox = \Activitypub\get_inbox_by_actor( $follower ); $inbox = \Activitypub\get_inbox_by_actor( $follower );
if ( ! $inbox || is_wp_error( $inbox ) ) { if ( ! $inbox || \is_wp_error( $inbox ) ) {
continue; continue;
} }
// init array if empty // init array if empty
@ -207,12 +207,12 @@ function get_identifier_settings( $user_id ) {
<tbody> <tbody>
<tr> <tr>
<th scope="row"> <th scope="row">
<label><?php esc_html_e( 'Profile identifier', 'activitypub' ); ?></label> <label><?php \esc_html_e( 'Profile identifier', 'activitypub' ); ?></label>
</th> </th>
<td> <td>
<p><code><?php echo esc_html( \Activitypub\get_webfinger_resource( $user_id ) ); ?></code> or <code><?php echo esc_url( get_author_posts_url( $user_id ) ); ?></code></p> <p><code><?php echo \esc_html( \Activitypub\get_webfinger_resource( $user_id ) ); ?></code> or <code><?php echo \esc_url( \get_author_posts_url( $user_id ) ); ?></code></p>
<?php // translators: the webfinger resource ?> <?php // translators: the webfinger resource ?>
<p class="description"><?php printf( esc_html__( 'Try to follow "@%s" in the Mastodon/Friendica search field.', 'activitypub' ), esc_html( \Activitypub\get_webfinger_resource( $user_id ) ) ); ?></p> <p class="description"><?php printf( \esc_html__( 'Try to follow "@%s" in the Mastodon/Friendica search field.', 'activitypub' ), \esc_html( \Activitypub\get_webfinger_resource( $user_id ) ) ); ?></p>
</td> </td>
</tr> </tr>
</tbody> </tbody>
@ -233,5 +233,5 @@ function get_followers( $user_id ) {
function count_followers( $user_id ) { function count_followers( $user_id ) {
$followers = \Activitypub\get_followers( $user_id ); $followers = \Activitypub\get_followers( $user_id );
return count( $followers ); return \count( $followers );
} }

View file

@ -13,14 +13,14 @@ class Followers {
* Initialize the class, registering WordPress hooks * Initialize the class, registering WordPress hooks
*/ */
public static function init() { public static function init() {
add_action( 'rest_api_init', array( '\Activitypub\Rest\Followers', 'register_routes' ) ); \add_action( 'rest_api_init', array( '\Activitypub\Rest\Followers', 'register_routes' ) );
} }
/** /**
* Register routes * Register routes
*/ */
public static function register_routes() { public static function register_routes() {
register_rest_route( \register_rest_route(
'activitypub/1.0', '/users/(?P<id>\d+)/followers', array( 'activitypub/1.0', '/users/(?P<id>\d+)/followers', array(
array( array(
'methods' => \WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
@ -40,35 +40,33 @@ class Followers {
*/ */
public static function get( $request ) { public static function get( $request ) {
$user_id = $request->get_param( 'id' ); $user_id = $request->get_param( 'id' );
$user = get_user_by( 'ID', $user_id ); $user = \get_user_by( 'ID', $user_id );
if ( ! $user ) { if ( ! $user ) {
return new \WP_Error( 'rest_invalid_param', __( 'User not found', 'activitypub' ), array( return new \WP_Error( 'rest_invalid_param', \__( 'User not found', 'activitypub' ), array(
'status' => 404, 'status' => 404,
'params' => array( 'params' => array(
'user_id' => __( 'User not found', 'activitypub' ), 'user_id' => \__( 'User not found', 'activitypub' ),
), ),
) ); ) );
} }
$page = $request->get_param( 'page', 0 );
/* /*
* Action triggerd prior to the ActivityPub profile being created and sent to the client * Action triggerd prior to the ActivityPub profile being created and sent to the client
*/ */
do_action( 'activitypub_outbox_pre' ); \do_action( 'activitypub_outbox_pre' );
$json = new \stdClass(); $json = new \stdClass();
$json->{'@context'} = \Activitypub\get_context(); $json->{'@context'} = \Activitypub\get_context();
$json->partOf = get_rest_url( null, "/activitypub/1.0/users/$user_id/followers" ); // phpcs:ignore $json->partOf = \get_rest_url( null, "/activitypub/1.0/users/$user_id/followers" ); // phpcs:ignore
$json->totalItems = \Activitypub\count_followers( $user_id ); // phpcs:ignore $json->totalItems = \Activitypub\count_followers( $user_id ); // phpcs:ignore
$json->orderedItems = \Activitypub\Db\Followers::get_followers( $user_id ); // phpcs:ignore $json->orderedItems = \Activitypub\Db\Followers::get_followers( $user_id ); // phpcs:ignore
$json->first = $json->partOf; // phpcs:ignore $json->first = $json->partOf; // phpcs:ignore
$json->first = get_rest_url( null, "/activitypub/1.0/users/$user_id/followers" ); $json->first = \get_rest_url( null, "/activitypub/1.0/users/$user_id/followers" );
$response = new \WP_REST_Response( $json, 200 ); $response = new \WP_REST_Response( $json, 200 );
$response->header( 'Content-Type', 'application/activity+json' ); $response->header( 'Content-Type', 'application/activity+json' );

View file

@ -13,14 +13,14 @@ class Following {
* Initialize the class, registering WordPress hooks * Initialize the class, registering WordPress hooks
*/ */
public static function init() { public static function init() {
add_action( 'rest_api_init', array( '\Activitypub\Rest\Following', 'register_routes' ) ); \add_action( 'rest_api_init', array( '\Activitypub\Rest\Following', 'register_routes' ) );
} }
/** /**
* Register routes * Register routes
*/ */
public static function register_routes() { public static function register_routes() {
register_rest_route( \register_rest_route(
'activitypub/1.0', '/users/(?P<id>\d+)/following', array( 'activitypub/1.0', '/users/(?P<id>\d+)/following', array(
array( array(
'methods' => \WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
@ -40,35 +40,33 @@ class Following {
*/ */
public static function get( $request ) { public static function get( $request ) {
$user_id = $request->get_param( 'id' ); $user_id = $request->get_param( 'id' );
$user = get_user_by( 'ID', $user_id ); $user = \get_user_by( 'ID', $user_id );
if ( ! $user ) { if ( ! $user ) {
return new \WP_Error( 'rest_invalid_param', __( 'User not found', 'activitypub' ), array( return new \WP_Error( 'rest_invalid_param', \__( 'User not found', 'activitypub' ), array(
'status' => 404, 'status' => 404,
'params' => array( 'params' => array(
'user_id' => __( 'User not found', 'activitypub' ), 'user_id' => \__( 'User not found', 'activitypub' ),
), ),
) ); ) );
} }
$page = $request->get_param( 'page', 0 );
/* /*
* Action triggerd prior to the ActivityPub profile being created and sent to the client * Action triggerd prior to the ActivityPub profile being created and sent to the client
*/ */
do_action( 'activitypub_outbox_pre' ); \do_action( 'activitypub_outbox_pre' );
$json = new \stdClass(); $json = new \stdClass();
$json->{'@context'} = \Activitypub\get_context(); $json->{'@context'} = \Activitypub\get_context();
$json->partOf = get_rest_url( null, "/activitypub/1.0/users/$user_id/following" ); // phpcs:ignore $json->partOf = \get_rest_url( null, "/activitypub/1.0/users/$user_id/following" ); // phpcs:ignore
$json->totalItems = 0; // phpcs:ignore $json->totalItems = 0; // phpcs:ignore
$json->orderedItems = array(); // phpcs:ignore $json->orderedItems = array(); // phpcs:ignore
$json->first = $json->partOf; // phpcs:ignore $json->first = $json->partOf; // phpcs:ignore
$json->first = get_rest_url( null, "/activitypub/1.0/users/$user_id/following" ); $json->first = \get_rest_url( null, "/activitypub/1.0/users/$user_id/following" );
$response = new \WP_REST_Response( $json, 200 ); $response = new \WP_REST_Response( $json, 200 );
$response->header( 'Content-Type', 'application/activity+json' ); $response->header( 'Content-Type', 'application/activity+json' );

View file

@ -13,20 +13,20 @@ class Inbox {
* Initialize the class, registering WordPress hooks * Initialize the class, registering WordPress hooks
*/ */
public static function init() { public static function init() {
add_action( 'rest_api_init', array( '\Activitypub\Rest\Inbox', 'register_routes' ) ); \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_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_follow', array( '\Activitypub\Rest\Inbox', 'handle_follow' ), 10, 2 );
add_action( 'activitypub_inbox_unfollow', array( '\Activitypub\Rest\Inbox', 'handle_unfollow' ), 10, 2 ); \add_action( 'activitypub_inbox_unfollow', 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_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_announce', array( '\Activitypub\Rest\Inbox', 'handle_reaction' ), 10, 2 );
add_action( 'activitypub_inbox_create', array( '\Activitypub\Rest\Inbox', 'handle_create' ), 10, 2 ); \add_action( 'activitypub_inbox_create', array( '\Activitypub\Rest\Inbox', 'handle_create' ), 10, 2 );
} }
/** /**
* Register routes * Register routes
*/ */
public static function register_routes() { public static function register_routes() {
register_rest_route( \register_rest_route(
'activitypub/1.0', '/inbox', array( 'activitypub/1.0', '/inbox', array(
array( array(
'methods' => \WP_REST_Server::EDITABLE, 'methods' => \WP_REST_Server::EDITABLE,
@ -35,7 +35,7 @@ class Inbox {
) )
); );
register_rest_route( \register_rest_route(
'activitypub/1.0', '/users/(?P<id>\d+)/inbox', array( 'activitypub/1.0', '/users/(?P<id>\d+)/inbox', array(
array( array(
'methods' => \WP_REST_Server::EDITABLE, 'methods' => \WP_REST_Server::EDITABLE,
@ -57,7 +57,7 @@ class Inbox {
* @return true * @return true
*/ */
public static function serve_request( $served, $result, $request, $server ) { public static function serve_request( $served, $result, $request, $server ) {
if ( '/activitypub' !== substr( $request->get_route(), 0, 12 ) ) { if ( '/activitypub' !== \substr( $request->get_route(), 0, 12 ) ) {
return $served; return $served;
} }
@ -87,21 +87,21 @@ class Inbox {
*/ */
public static function user_inbox( $request ) { public static function user_inbox( $request ) {
$author_id = $request->get_param( 'id' ); $author_id = $request->get_param( 'id' );
$author = get_user_by( 'ID', $author_id ); $author = \get_user_by( 'ID', $author_id );
$data = json_decode( $request->get_body(), true ); $data = \json_decode( $request->get_body(), true );
$type = 'create'; $type = 'create';
if ( ! empty( $data['type'] ) ) { if ( ! empty( $data['type'] ) ) {
$type = strtolower( $data['type'] ); $type = \strtolower( $data['type'] );
} }
if ( ! is_array( $data ) || ! array_key_exists( 'type', $data ) ) { 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', $data, $author_id, $type );
do_action( "activitypub_inbox_{$type}", $data, $author_id ); \do_action( "activitypub_inbox_{$type}", $data, $author_id );
return new \WP_REST_Response( array(), 202 ); return new \WP_REST_Response( array(), 202 );
} }
@ -115,7 +115,7 @@ class Inbox {
*/ */
public static function global_inbox( $request ) { public static function global_inbox( $request ) {
// Create the response object // 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 ) );
} }
/** /**
@ -145,8 +145,8 @@ class Inbox {
* @param int $user_id The id of the local blog-user * @param int $user_id The id of the local blog-user
*/ */
public static function handle_follow( $object, $user_id ) { public static function handle_follow( $object, $user_id ) {
if ( ! array_key_exists( 'actor', $object ) ) { 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' ) );
} }
// save follower // save follower
@ -158,9 +158,9 @@ class Inbox {
// send "Accept" activity // 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_object( $object );
$activity->set_actor( get_author_posts_url( $user_id ) ); $activity->set_actor( \get_author_posts_url( $user_id ) );
$activity->set_to( $object['actor'] ); $activity->set_to( $object['actor'] );
$activity->set_id( get_author_posts_url( $user_id ) . '#follow' . preg_replace( '~^https?://~', '', $object['actor'] ) ); $activity->set_id( \get_author_posts_url( $user_id ) . '#follow' . \preg_replace( '~^https?://~', '', $object['actor'] ) );
$activity = $activity->to_simple_json(); $activity = $activity->to_simple_json();
@ -174,8 +174,8 @@ class Inbox {
* @param int $user_id The id of the local blog-user * @param int $user_id The id of the local blog-user
*/ */
public static function handle_unfollow( $object, $user_id ) { public static function handle_unfollow( $object, $user_id ) {
if ( ! array_key_exists( 'actor', $object ) ) { 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' ) );
} }
\Activitypub\Db\Followers::remove_follower( $object['actor'], $user_id ); \Activitypub\Db\Followers::remove_follower( $object['actor'], $user_id );
@ -188,34 +188,34 @@ class Inbox {
* @param int $user_id The id of the local blog-user * @param int $user_id The id of the local blog-user
*/ */
public static function handle_reaction( $object, $user_id ) { public static function handle_reaction( $object, $user_id ) {
if ( ! array_key_exists( 'actor', $object ) ) { 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' ) );
} }
$meta = \Activitypub\get_remote_metadata_by_actor( $object['actor'] ); $meta = \Activitypub\get_remote_metadata_by_actor( $object['actor'] );
$commentdata = array( $commentdata = array(
'comment_post_ID' => url_to_postid( $object['object'] ), 'comment_post_ID' => \url_to_postid( $object['object'] ),
'comment_author' => esc_attr( $meta['name'] ), 'comment_author' => \esc_attr( $meta['name'] ),
'comment_author_email' => '', 'comment_author_email' => '',
'comment_author_url' => esc_url_raw( $object['actor'] ), 'comment_author_url' => \esc_url_raw( $object['actor'] ),
'comment_content' => esc_url_raw( $object['actor'] ), 'comment_content' => \esc_url_raw( $object['actor'] ),
'comment_type' => esc_attr( strtolower( $object['type'] ) ), 'comment_type' => \esc_attr( strtolower( $object['type'] ) ),
'comment_parent' => 0, 'comment_parent' => 0,
'comment_meta' => array( 'comment_meta' => array(
'source_url' => esc_url_raw( $object['id'] ), 'source_url' => \esc_url_raw( $object['id'] ),
'avatar_url' => esc_url_raw( $meta['icon']['url'] ), 'avatar_url' => \esc_url_raw( $meta['icon']['url'] ),
'protocol' => 'activitypub', 'protocol' => 'activitypub',
), ),
); );
// disable flood control // disable flood control
remove_action( 'check_comment_flood', 'check_comment_flood_db', 10 ); \remove_action( 'check_comment_flood', 'check_comment_flood_db', 10 );
$state = wp_new_comment( $commentdata, true ); $state = \wp_new_comment( $commentdata, true );
// re-add flood control // re-add flood control
add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 4 ); \add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 4 );
} }
/** /**
@ -225,33 +225,33 @@ class Inbox {
* @param int $user_id The id of the local blog-user * @param int $user_id The id of the local blog-user
*/ */
public static function handle_create( $object, $user_id ) { public static function handle_create( $object, $user_id ) {
if ( ! array_key_exists( 'actor', $object ) ) { 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' ) );
} }
$meta = \Activitypub\get_remote_metadata_by_actor( $object['actor'] ); $meta = \Activitypub\get_remote_metadata_by_actor( $object['actor'] );
$commentdata = array( $commentdata = array(
'comment_post_ID' => url_to_postid( $object['object']['inReplyTo'] ), 'comment_post_ID' => \url_to_postid( $object['object']['inReplyTo'] ),
'comment_author' => esc_attr( $meta['name'] ), 'comment_author' => \esc_attr( $meta['name'] ),
'comment_author_url' => esc_url_raw( $object['actor'] ), 'comment_author_url' => \esc_url_raw( $object['actor'] ),
'comment_content' => wp_filter_kses( $object['object']['content'] ), 'comment_content' => \wp_filter_kses( $object['object']['content'] ),
'comment_type' => '', 'comment_type' => '',
'comment_author_email' => '', 'comment_author_email' => '',
'comment_parent' => 0, 'comment_parent' => 0,
'comment_meta' => array( 'comment_meta' => array(
'source_url' => esc_url_raw( $object['object']['url'] ), 'source_url' => \esc_url_raw( $object['object']['url'] ),
'avatar_url' => esc_url_raw( $meta['icon']['url'] ), 'avatar_url' => \esc_url_raw( $meta['icon']['url'] ),
'protocol' => 'activitypub', 'protocol' => 'activitypub',
), ),
); );
// disable flood control // disable flood control
remove_action( 'check_comment_flood', 'check_comment_flood_db', 10 ); \remove_action( 'check_comment_flood', 'check_comment_flood_db', 10 );
$state = wp_new_comment( $commentdata, true ); $state = \wp_new_comment( $commentdata, true );
// re-add flood control // re-add flood control
add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 4 ); \add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 4 );
} }
} }

View file

@ -13,16 +13,16 @@ class Nodeinfo {
* Initialize the class, registering WordPress hooks * Initialize the class, registering WordPress hooks
*/ */
public static function init() { public static function init() {
add_action( 'rest_api_init', array( '\Activitypub\Rest\Nodeinfo', 'register_routes' ) ); \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( 'nodeinfo_data', array( '\Activitypub\Rest\Nodeinfo', 'add_nodeinfo_discovery' ), 10, 2 );
add_filter( 'nodeinfo2_data', array( '\Activitypub\Rest\Nodeinfo', 'add_nodeinfo2_discovery' ), 10 ); \add_filter( 'nodeinfo2_data', array( '\Activitypub\Rest\Nodeinfo', 'add_nodeinfo2_discovery' ), 10 );
} }
/** /**
* Register routes * Register routes
*/ */
public static function register_routes() { public static function register_routes() {
register_rest_route( \register_rest_route(
'activitypub/1.0', '/nodeinfo/discovery', array( 'activitypub/1.0', '/nodeinfo/discovery', array(
array( array(
'methods' => \WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
@ -31,7 +31,7 @@ class Nodeinfo {
) )
); );
register_rest_route( \register_rest_route(
'activitypub/1.0', '/nodeinfo', array( 'activitypub/1.0', '/nodeinfo', array(
array( array(
'methods' => \WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
@ -40,7 +40,7 @@ class Nodeinfo {
) )
); );
register_rest_route( \register_rest_route(
'activitypub/1.0', '/nodeinfo2', array( 'activitypub/1.0', '/nodeinfo2', array(
array( array(
'methods' => \WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
@ -63,55 +63,12 @@ class Nodeinfo {
$nodeinfo['version'] = '2.0'; $nodeinfo['version'] = '2.0';
$nodeinfo['software'] = array( $nodeinfo['software'] = array(
'name' => 'wordpress', 'name' => 'wordpress',
'version' => get_bloginfo( 'version' ), 'version' => \get_bloginfo( 'version' ),
); );
$users = count_users(); $users = \count_users();
$posts = wp_count_posts(); $posts = \wp_count_posts();
$comments = wp_count_comments(); $comments = \wp_count_comments();
$nodeinfo['usage'] = array(
'users' => array(
'total' => (int) $users['total_users'],
),
'localPosts' => (int) $posts->publish,
'localComments' => (int) $comments->approved,
);
$nodeinfo['openRegistrations'] = false;
$nodeinfo['protocols'] = array( 'activitypub' );
$nodeinfo['services'] = array(
'inbound' => array(),
'outbound' => array(),
);
$nodeinfo['metadata'] = new \stdClass();
return new \WP_REST_Response( $nodeinfo, 200 );
}
/**
* Render NodeInfo file
*
* @param WP_REST_Request $request
*
* @return WP_REST_Response
*/
public static function nodeinfo2( $request ) {
$nodeinfo = array();
$nodeinfo['version'] = '1.0';
$nodeinfo['software'] = array(
'baseUrl' => home_url( '/' ),
'name' => get_bloginfo( 'name' ),
'software' => 'wordpress',
'version' => get_bloginfo( 'version' ),
);
$users = count_users();
$posts = wp_count_posts();
$comments = wp_count_comments();
$nodeinfo['usage'] = array( $nodeinfo['usage'] = array(
'users' => array( 'users' => array(
@ -130,7 +87,52 @@ class Nodeinfo {
); );
$nodeinfo['metadata'] = array( $nodeinfo['metadata'] = array(
'email' => get_option( 'admin_email' ), 'email' => \get_option( 'admin_email' ),
);
return new \WP_REST_Response( $nodeinfo, 200 );
}
/**
* Render NodeInfo file
*
* @param WP_REST_Request $request
*
* @return WP_REST_Response
*/
public static function nodeinfo2( $request ) {
$nodeinfo = array();
$nodeinfo['version'] = '1.0';
$nodeinfo['software'] = array(
'baseUrl' => home_url( '/' ),
'name' => \get_bloginfo( 'name' ),
'software' => 'wordpress',
'version' => \get_bloginfo( 'version' ),
);
$users = \count_users();
$posts = \wp_count_posts();
$comments = \wp_count_comments();
$nodeinfo['usage'] = array(
'users' => array(
'total' => (int) $users['total_users'],
),
'localPosts' => (int) $posts->publish,
'localComments' => (int) $comments->approved,
);
$nodeinfo['openRegistrations'] = false;
$nodeinfo['protocols'] = array( 'activitypub' );
$nodeinfo['services'] = array(
'inbound' => array(),
'outbound' => array(),
);
$nodeinfo['metadata'] = array(
'email' => \get_option( 'admin_email' ),
); );
return new \WP_REST_Response( $nodeinfo, 200 ); return new \WP_REST_Response( $nodeinfo, 200 );
@ -148,7 +150,7 @@ class Nodeinfo {
$discovery['links'] = array( $discovery['links'] = array(
array( array(
'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0', 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0',
'href' => get_rest_url( null, 'activitypub/1.0/nodeinfo' ), 'href' => \get_rest_url( null, 'activitypub/1.0/nodeinfo' ),
), ),
); );

View file

@ -13,7 +13,7 @@ class Ostatus {
* Register routes * Register routes
*/ */
public static function register_routes() { public static function register_routes() {
register_rest_route( \register_rest_route(
'activitypub/1.0', '/ostatus/remote-follow', array( 'activitypub/1.0', '/ostatus/remote-follow', array(
array( array(
'methods' => \WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,

View file

@ -13,14 +13,14 @@ class Outbox {
* Initialize the class, registering WordPress hooks * Initialize the class, registering WordPress hooks
*/ */
public static function init() { public static function init() {
add_action( 'rest_api_init', array( '\Activitypub\Rest\Outbox', 'register_routes' ) ); \add_action( 'rest_api_init', array( '\Activitypub\Rest\Outbox', 'register_routes' ) );
} }
/** /**
* Register routes * Register routes
*/ */
public static function register_routes() { public static function register_routes() {
register_rest_route( \register_rest_route(
'activitypub/1.0', '/users/(?P<id>\d+)/outbox', array( 'activitypub/1.0', '/users/(?P<id>\d+)/outbox', array(
array( array(
'methods' => \WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
@ -39,13 +39,13 @@ class Outbox {
*/ */
public static function user_outbox( $request ) { public static function user_outbox( $request ) {
$user_id = $request->get_param( 'id' ); $user_id = $request->get_param( 'id' );
$author = get_user_by( 'ID', $user_id ); $author = \get_user_by( 'ID', $user_id );
if ( ! $author ) { if ( ! $author ) {
return new \WP_Error( 'rest_invalid_param', __( 'User not found', 'activitypub' ), array( return new \WP_Error( 'rest_invalid_param', __( 'User not found', 'activitypub' ), array(
'status' => 404, 'status' => 404,
'params' => array( 'params' => array(
'user_id' => __( 'User not found', 'activitypub' ), 'user_id' => \__( 'User not found', 'activitypub' ),
), ),
) ); ) );
} }
@ -55,31 +55,31 @@ class Outbox {
/* /*
* Action triggerd prior to the ActivityPub profile being created and sent to the client * Action triggerd prior to the ActivityPub profile being created and sent to the client
*/ */
do_action( 'activitypub_outbox_pre' ); \do_action( 'activitypub_outbox_pre' );
$json = new \stdClass(); $json = new \stdClass();
$json->{'@context'} = \Activitypub\get_context(); $json->{'@context'} = \Activitypub\get_context();
$json->id = home_url( add_query_arg( null, null ) ); $json->id = \home_url( \add_query_arg( null, null ) );
$json->generator = 'http://wordpress.org/?v=' . get_bloginfo_rss( 'version' ); $json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' );
$json->actor = get_author_posts_url( $user_id ); $json->actor = \get_author_posts_url( $user_id );
$json->type = 'OrderedCollectionPage'; $json->type = 'OrderedCollectionPage';
$json->partOf = get_rest_url( null, "/activitypub/1.0/users/$user_id/outbox" ); // phpcs:ignore $json->partOf = \get_rest_url( null, "/activitypub/1.0/users/$user_id/outbox" ); // phpcs:ignore
$count_posts = wp_count_posts(); $count_posts = \wp_count_posts();
$json->totalItems = intval( $count_posts->publish ); // phpcs:ignore $json->totalItems = \intval( $count_posts->publish ); // phpcs:ignore
$posts = get_posts( array( $posts = \get_posts( array(
'posts_per_page' => 10, 'posts_per_page' => 10,
'author' => $user_id, 'author' => $user_id,
'offset' => $page * 10, 'offset' => $page * 10,
) ); ) );
$json->first = add_query_arg( 'page', 0, $json->partOf ); // phpcs:ignore $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 $json->last = \add_query_arg( 'page', ( \ceil ( $json->totalItems / 10 ) ) - 1, $json->partOf ); // phpcs:ignore
if ( ( ceil ( $json->totalItems / 10 ) ) - 1 > $page ) { // phpcs:ignore if ( ( ceil ( $json->totalItems / 10 ) ) - 1 > $page ) { // phpcs:ignore
$json->next = add_query_arg( 'page', ++$page, $json->partOf ); // phpcs:ignore $json->next = \add_query_arg( 'page', ++$page, $json->partOf ); // phpcs:ignore
} }
foreach ( $posts as $post ) { foreach ( $posts as $post ) {
@ -90,12 +90,12 @@ class Outbox {
} }
// filter output // filter output
$json = apply_filters( 'activitypub_outbox_array', $json ); $json = \apply_filters( 'activitypub_outbox_array', $json );
/* /*
* Action triggerd after the ActivityPub profile has been created and sent to the client * Action triggerd after the ActivityPub profile has been created and sent to the client
*/ */
do_action( 'activitypub_outbox_post' ); \do_action( 'activitypub_outbox_post' );
$response = new \WP_REST_Response( $json, 200 ); $response = new \WP_REST_Response( $json, 200 );

View file

@ -13,15 +13,15 @@ class Webfinger {
* Initialize the class, registering WordPress hooks * Initialize the class, registering WordPress hooks
*/ */
public static function init() { public static function init() {
add_action( 'rest_api_init', array( '\Activitypub\Rest\Webfinger', 'register_routes' ) ); \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( 'webfinger_user_data', array( '\Activitypub\Rest\Webfinger', 'add_webfinger_discovery' ), 10, 3 );
} }
/** /**
* Register routes * Register routes
*/ */
public static function register_routes() { public static function register_routes() {
register_rest_route( \register_rest_route(
'activitypub/1.0', '/webfinger', array( 'activitypub/1.0', '/webfinger', array(
array( array(
'methods' => \WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
@ -42,40 +42,40 @@ class Webfinger {
$resource = $request->get_param( 'resource' ); $resource = $request->get_param( 'resource' );
$matches = array(); $matches = array();
$matched = preg_match( '/^acct:([^@]+)@(.+)$/', $resource, $matches ); $matched = \preg_match( '/^acct:([^@]+)@(.+)$/', $resource, $matches );
if ( ! $matched ) { if ( ! $matched ) {
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_identifier = $matches[1]; $resource_identifier = $matches[1];
$resource_host = $matches[2]; $resource_host = $matches[2];
if ( wp_parse_url( home_url( '/' ), PHP_URL_HOST ) !== $resource_host ) { 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 ) ); $user = \get_user_by( 'login', \esc_sql( $resource_identifier ) );
if ( ! $user ) { 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( $json = array(
'subject' => $resource, 'subject' => $resource,
'aliases' => array( 'aliases' => array(
get_author_posts_url( $user->ID ), \get_author_posts_url( $user->ID ),
), ),
'links' => array( 'links' => array(
array( array(
'rel' => 'self', 'rel' => 'self',
'type' => 'application/activity+json', 'type' => 'application/activity+json',
'href' => get_author_posts_url( $user->ID ), 'href' => \get_author_posts_url( $user->ID ),
), ),
array( array(
'rel' => 'http://webfinger.net/rel/profile-page', 'rel' => 'http://webfinger.net/rel/profile-page',
'type' => 'text/html', 'type' => 'text/html',
'href' => get_author_posts_url( $user->ID ), 'href' => \get_author_posts_url( $user->ID ),
), ),
), ),
); );
@ -111,7 +111,7 @@ class Webfinger {
$array['links'][] = array( $array['links'][] = array(
'rel' => 'self', 'rel' => 'self',
'type' => 'application/activity+json', 'type' => 'application/activity+json',
'href' => get_author_posts_url( $user->ID ), 'href' => \get_author_posts_url( $user->ID ),
); );
return $array; return $array;

View file

@ -8,7 +8,7 @@ if ( ! class_exists( '\WP_List_Table' ) ) {
class Followers_List extends \WP_List_Table { class Followers_List extends \WP_List_Table {
public function get_columns() { public function get_columns() {
return array( return array(
'identifier' => __( 'Identifier', 'activitypub' ), 'identifier' => \__( 'Identifier', 'activitypub' ),
); );
} }
@ -25,8 +25,8 @@ class Followers_List extends \WP_List_Table {
$this->items = array(); $this->items = array();
foreach ( \Activitypub\Db\Followers::get_followers( get_current_user_id() ) as $follower ) { foreach ( \Activitypub\Db\Followers::get_followers( \get_current_user_id() ) as $follower ) {
$this->items[]['identifier'] = esc_attr( $follower ); $this->items[]['identifier'] = \esc_attr( $follower );
} }
} }

View file

@ -4,8 +4,9 @@
<file>./activitypub.php</file> <file>./activitypub.php</file>
<file>./includes/</file> <file>./includes/</file>
<config name="minimum_supported_wp_version" value="4.7"/> <config name="minimum_supported_wp_version" value="4.7"/>
<exclude-pattern>*/includes/*\.(inc|css|js|svg)</exclude-pattern> <exclude-pattern>*\.(inc|css|js|svg)</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern> <exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/node_modules/*</exclude-pattern>
<rule ref="PHPCompatibility"/> <rule ref="PHPCompatibility"/>
<config name="testVersion" value="5.6-"/> <config name="testVersion" value="5.6-"/>
<rule ref="WordPress-Core"> <rule ref="WordPress-Core">

View file

@ -19,6 +19,7 @@ class Test_Activitypub_Hashtag extends WP_UnitTestCase {
array( '#test', '#test' ), array( '#test', '#test' ),
array( 'hallo #test test', 'hallo #test test' ), array( 'hallo #test test', 'hallo #test test' ),
array( 'hallo #object test', 'hallo <a rel="tag" class="u-tag u-category" href="' . $link . '">#object</a> test' ), array( 'hallo #object test', 'hallo <a rel="tag" class="u-tag u-category" href="' . $link . '">#object</a> test' ),
array( '#object test', '<a rel="tag" class="u-tag u-category" href="' . $link . '">#object</a> test' ),
array( 'hallo <a href="http://test.test/#object">test</a> test', 'hallo <a href="http://test.test/#object">test</a> test' ), array( 'hallo <a href="http://test.test/#object">test</a> test', 'hallo <a href="http://test.test/#object">test</a> test' ),
array( 'hallo <a href="http://test.test/#object">#test</a> test', 'hallo <a href="http://test.test/#object">#test</a> test' ), array( 'hallo <a href="http://test.test/#object">#test</a> test', 'hallo <a href="http://test.test/#object">#test</a> test' ),
array( '<div>hallo #object test</div>', '<div>hallo <a rel="tag" class="u-tag u-category" href="' . $link . '">#object</a> test</div>' ), array( '<div>hallo #object test</div>', '<div>hallo <a rel="tag" class="u-tag u-category" href="' . $link . '">#object</a> test</div>' ),