new dispatcher class
This commit is contained in:
parent
a0ed2df6f9
commit
f80f6a791f
3 changed files with 87 additions and 45 deletions
|
@ -26,11 +26,14 @@ function init() {
|
||||||
require_once dirname( __FILE__ ) . '/includes/db/class-followers.php';
|
require_once dirname( __FILE__ ) . '/includes/db/class-followers.php';
|
||||||
require_once dirname( __FILE__ ) . '/includes/functions.php';
|
require_once dirname( __FILE__ ) . '/includes/functions.php';
|
||||||
|
|
||||||
|
require_once dirname( __FILE__ ) . '/includes/class-activity-dispatcher.php';
|
||||||
|
\Activitypub\Activity_Dispatcher::init();
|
||||||
|
|
||||||
require_once dirname( __FILE__ ) . '/includes/class-post.php';
|
require_once dirname( __FILE__ ) . '/includes/class-post.php';
|
||||||
\Activitypub\Post::init();
|
\Activitypub\Post::init();
|
||||||
|
|
||||||
require_once dirname( __FILE__ ) . '/includes/class-activitypub.php';
|
require_once dirname( __FILE__ ) . '/includes/class-activitypub.php';
|
||||||
\Activitypub\ActivityPub::init();
|
\Activitypub\Activitypub::init();
|
||||||
|
|
||||||
// Configure the REST API route
|
// Configure the REST API route
|
||||||
require_once dirname( __FILE__ ) . '/includes/rest/class-outbox.php';
|
require_once dirname( __FILE__ ) . '/includes/rest/class-outbox.php';
|
||||||
|
|
83
includes/class-activity-dispatcher.php
Normal file
83
includes/class-activity-dispatcher.php
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
<?php
|
||||||
|
namespace Activitypub;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ActivityPub Activity_Dispatcher Class
|
||||||
|
*
|
||||||
|
* @author Matthias Pfefferle
|
||||||
|
*
|
||||||
|
* @see https://www.w3.org/TR/activitypub/
|
||||||
|
*/
|
||||||
|
class Activity_Dispatcher {
|
||||||
|
/**
|
||||||
|
* Initialize the class, registering WordPress hooks
|
||||||
|
*/
|
||||||
|
public static function init() {
|
||||||
|
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_delete_activity', array( '\Activitypub\Activity_Dispatcher', 'send_delete_activity' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send "create" activities
|
||||||
|
*
|
||||||
|
* @param int $post_id
|
||||||
|
*/
|
||||||
|
public static function send_post_activity( $post_id ) {
|
||||||
|
$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_activity->from_post( $activitypub_post->to_array() );
|
||||||
|
|
||||||
|
foreach ( \Activitypub\get_follower_inboxes( $user_id ) as $inbox => $to ) {
|
||||||
|
$activitypub_activity->set_to( $to );
|
||||||
|
$activity = $activitypub_activity->to_json(); // phpcs:ignore
|
||||||
|
|
||||||
|
\Activitypub\safe_remote_post( $inbox, $activity, $user_id );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send "update" activities
|
||||||
|
*
|
||||||
|
* @param int $post_id
|
||||||
|
*/
|
||||||
|
public static function send_update_activity( $post_id ) {
|
||||||
|
$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_activity->from_post( $activitypub_post->to_array() );
|
||||||
|
|
||||||
|
foreach ( \Activitypub\get_follower_inboxes( $user_id ) as $inbox => $to ) {
|
||||||
|
$activitypub_activity->set_to( $to );
|
||||||
|
$activity = $activitypub_activity->to_json(); // phpcs:ignore
|
||||||
|
|
||||||
|
\Activitypub\safe_remote_post( $inbox, $activity, $user_id );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send "delete" activities
|
||||||
|
*
|
||||||
|
* @param int $post_id
|
||||||
|
*/
|
||||||
|
public static function send_delete_activity( $post_id ) {
|
||||||
|
$post = get_post( $post_id );
|
||||||
|
$user_id = $post->post_author;
|
||||||
|
|
||||||
|
$activitypub_post = new \Activitypub\Post( $post );
|
||||||
|
$activitypub_activity = new \Activitypub\Activity( 'Delete', \Activitypub\Activity::TYPE_FULL );
|
||||||
|
$activitypub_activity->from_post( $activitypub_post->to_array() );
|
||||||
|
|
||||||
|
foreach ( \Activitypub\get_follower_inboxes( $user_id ) as $inbox => $to ) {
|
||||||
|
$activitypub_activity->set_to( $to );
|
||||||
|
$activity = $activitypub_activity->to_json(); // phpcs:ignore
|
||||||
|
|
||||||
|
\Activitypub\safe_remote_post( $inbox, $activity, $user_id );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -14,8 +14,6 @@ class Outbox {
|
||||||
*/
|
*/
|
||||||
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' ) );
|
||||||
add_action( 'activitypub_send_post_activity', array( '\Activitypub\Rest\Outbox', 'send_post_activity' ) );
|
|
||||||
add_action( 'activitypub_send_update_activity', array( '\Activitypub\Rest\Outbox', 'send_update_activity' ) );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -125,46 +123,4 @@ class Outbox {
|
||||||
|
|
||||||
return $params;
|
return $params;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Send "create" activities
|
|
||||||
*
|
|
||||||
* @param int $post_id
|
|
||||||
*/
|
|
||||||
public static function send_post_activity( $post_id ) {
|
|
||||||
$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_activity->from_post( $activitypub_post->to_array() );
|
|
||||||
|
|
||||||
foreach ( \Activitypub\get_follower_inboxes( $user_id ) as $inbox => $to ) {
|
|
||||||
$activitypub_activity->set_to( $to );
|
|
||||||
$activity = $activitypub_activity->to_json(); // phpcs:ignore
|
|
||||||
|
|
||||||
\Activitypub\safe_remote_post( $inbox, $activity, $user_id );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Send "update" activities
|
|
||||||
*
|
|
||||||
* @param int $post_id
|
|
||||||
*/
|
|
||||||
public static function send_update_activity( $post_id ) {
|
|
||||||
$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_activity->from_post( $activitypub_post->to_array() );
|
|
||||||
|
|
||||||
foreach ( \Activitypub\get_follower_inboxes( $user_id ) as $inbox => $to ) {
|
|
||||||
$activitypub_activity->set_to( $to );
|
|
||||||
$activity = $activitypub_activity->to_json(); // phpcs:ignore
|
|
||||||
|
|
||||||
\Activitypub\safe_remote_post( $inbox, $activity, $user_id );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue