From e7cda74671b5a51c4f40ca6bbc369f659bb2a56b Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Mon, 3 Sep 2018 21:22:23 -0400 Subject: [PATCH] Create actors for users on plugin update/user registration --- inc/actors.php | 25 +++++++++++++++++++++++-- inc/api.php | 3 +-- inc/init.php | 6 ++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/inc/actors.php b/inc/actors.php index 603b2df..7e660c3 100644 --- a/inc/actors.php +++ b/inc/actors.php @@ -60,10 +60,31 @@ function create_actors_table() { " CREATE TABLE IF NOT EXISTS activitypub_actors( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, - slug VARCHAR(255) UNIQUE NOT NULL, - type VARCHAR(64) + slug VARCHAR(64) UNIQUE NOT NULL, + type VARCHAR(64) NOT NULL ); " ); } + +/* +For every user in the WP instance, create a new actor row for that user +if it doesn't already exist +*/ +function initialize_user_actors() { + global $wpdb; + $user_slugs = $wpdb->get_col( + "SELECT user_nicename FROM wp_users;" + ); + foreach ( $user_slugs as $user_slug ) { + create_actor_from_user( $user_slug ); + } +} + +function create_actor_from_user( $user_slug ) { + global $wpdb; + $wpdb->query( $wpdb->prepare( + "INSERT IGNORE INTO activitypub_actors(slug, type) VALUES(%s, 'user')", $user_slug + ) ); +} ?> diff --git a/inc/api.php b/inc/api.php index c5f4d21..8e369bb 100644 --- a/inc/api.php +++ b/inc/api.php @@ -8,8 +8,7 @@ require_once plugin_dir_path( __FILE__ ) . '/activities.php'; function get_actor( $request ) { $actor = $request['actor']; - $user = get_user_by( 'slug', $actor ); - return \actors\get_actor( $user ); + return \actors\get_actor_by_slug( $user ); } function handle_activity( $request ) { diff --git a/inc/init.php b/inc/init.php index 006b5eb..4f6decf 100644 --- a/inc/init.php +++ b/inc/init.php @@ -11,10 +11,16 @@ add_action( 'rest_api_init', function() { \api\register_routes(); } ); +add_action( 'user_register', function( $user_id ) { + $slug = get_the_author_meta( 'user_nicename', $user_id ); + \actors\create_actor_from_user( $slug ); +} ); + add_action( 'activitypub_init', function() { \activities\create_activities_table(); \objects\create_object_table(); \outbox\create_outbox_table(); \actors\create_actors_table(); + \actors\initialize_user_actors(); } ); ?>