| 1 |
<?php |
| 2 |
/** |
| 3 |
* Actor scheduler class file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Scheduler; |
| 9 |
|
| 10 |
use Activitypub\Activity\Activity; |
| 11 |
use Activitypub\Collection\Actors; |
| 12 |
use Activitypub\Collection\Extra_Fields; |
| 13 |
use Activitypub\Collection\Outbox; |
| 14 |
|
| 15 |
use function Activitypub\add_to_outbox; |
| 16 |
use function Activitypub\is_user_type_disabled; |
| 17 |
|
| 18 |
/** |
| 19 |
* Post scheduler class. |
| 20 |
*/ |
| 21 |
class Actor { |
| 22 |
/** |
| 23 |
* Initialize the class, registering WordPress hooks. |
| 24 |
*/ |
| 25 |
public static function init() { |
| 26 |
// Profile updates for blog options. |
| 27 |
if ( ! is_user_type_disabled( 'blog' ) ) { |
| 28 |
\add_action( 'update_option_site_icon', array( self::class, 'blog_user_update' ) ); |
| 29 |
\add_action( 'update_option_blogdescription', array( self::class, 'blog_user_update' ) ); |
| 30 |
\add_action( 'update_option_blogname', array( self::class, 'blog_user_update' ) ); |
| 31 |
\add_action( 'add_option_activitypub_header_image', array( self::class, 'blog_user_update' ) ); |
| 32 |
\add_action( 'update_option_activitypub_header_image', array( self::class, 'blog_user_update' ) ); |
| 33 |
\add_action( 'add_option_activitypub_blog_identifier', array( self::class, 'blog_user_update' ) ); |
| 34 |
\add_action( 'update_option_activitypub_blog_identifier', array( self::class, 'blog_user_update' ) ); |
| 35 |
\add_action( 'add_option_activitypub_blog_description', array( self::class, 'blog_user_update' ) ); |
| 36 |
\add_action( 'update_option_activitypub_blog_description', array( self::class, 'blog_user_update' ) ); |
| 37 |
\add_filter( 'pre_set_theme_mod_custom_logo', array( self::class, 'blog_user_update' ) ); |
| 38 |
\add_filter( 'pre_set_theme_mod_header_image', array( self::class, 'blog_user_update' ) ); |
| 39 |
} |
| 40 |
|
| 41 |
// Profile updates for user options. |
| 42 |
if ( ! is_user_type_disabled( 'user' ) ) { |
| 43 |
\add_action( 'profile_update', array( self::class, 'user_update' ) ); |
| 44 |
\add_action( 'added_user_meta', array( self::class, 'user_meta_update' ), 10, 3 ); |
| 45 |
\add_action( 'updated_user_meta', array( self::class, 'user_meta_update' ), 10, 3 ); |
| 46 |
// @todo figure out a feasible way of updating the header image since it's not unique to any user. |
| 47 |
} |
| 48 |
|
| 49 |
\add_action( 'add_option_activitypub_actor_mode', array( self::class, 'blog_user_update' ) ); |
| 50 |
\add_action( 'update_option_activitypub_actor_mode', array( self::class, 'blog_user_update' ) ); |
| 51 |
|
| 52 |
// The Starter Kit policy is part of every actor profile. |
| 53 |
\add_action( 'add_option_activitypub_default_feature_policy', array( self::class, 'schedule_all_profile_updates' ) ); |
| 54 |
\add_action( 'update_option_activitypub_default_feature_policy', array( self::class, 'schedule_all_profile_updates' ) ); |
| 55 |
|
| 56 |
\add_action( 'transition_post_status', array( self::class, 'schedule_post_activity' ), 33, 3 ); |
| 57 |
|
| 58 |
\add_action( 'post_stuck', array( self::class, 'sticky_post_update' ) ); |
| 59 |
\add_action( 'post_unstuck', array( self::class, 'sticky_post_update' ) ); |
| 60 |
|
| 61 |
// User deletion handling. |
| 62 |
\add_action( 'delete_user', array( self::class, 'schedule_user_delete' ), 10, 3 ); |
| 63 |
\add_filter( 'post_types_to_delete_with_user', array( self::class, 'post_types_to_delete_with_user' ) ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Send a profile update when relevant user meta is updated. |
| 68 |
* |
| 69 |
* @param int $meta_id Meta ID being updated. |
| 70 |
* @param int $user_id User ID being updated. |
| 71 |
* @param string $meta_key Meta key being updated. |
| 72 |
*/ |
| 73 |
public static function user_meta_update( $meta_id, $user_id, $meta_key ) { |
| 74 |
// Don't bother if the user can't publish. |
| 75 |
if ( ! \user_can( $user_id, 'activitypub' ) ) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
$blog_prefix = $GLOBALS['wpdb']->get_blog_prefix(); |
| 80 |
|
| 81 |
// The user meta fields that affect a profile. |
| 82 |
$fields = array( |
| 83 |
$blog_prefix . 'activitypub_description', |
| 84 |
$blog_prefix . 'activitypub_header_image', |
| 85 |
$blog_prefix . 'activitypub_icon', |
| 86 |
'description', |
| 87 |
'display_name', |
| 88 |
'user_url', |
| 89 |
); |
| 90 |
|
| 91 |
if ( \in_array( $meta_key, $fields, true ) ) { |
| 92 |
self::schedule_profile_update( $user_id ); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Send a profile update when a user is updated. |
| 98 |
* |
| 99 |
* @param int $user_id User ID being updated. |
| 100 |
*/ |
| 101 |
public static function user_update( $user_id ) { |
| 102 |
// Don't bother if the user can't publish. |
| 103 |
if ( ! \user_can( $user_id, 'activitypub' ) ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
self::schedule_profile_update( $user_id ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Theme mods only have a dynamic filter so we fudge it like this. |
| 112 |
* |
| 113 |
* @param mixed $value Optional. The value to be updated. Default null. |
| 114 |
* |
| 115 |
* @return mixed |
| 116 |
*/ |
| 117 |
public static function blog_user_update( $value = null ) { |
| 118 |
self::schedule_profile_update( Actors::BLOG_USER_ID ); |
| 119 |
return $value; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Schedule Activities. |
| 124 |
* |
| 125 |
* @param string $new_status New post status. |
| 126 |
* @param string $old_status Old post status. |
| 127 |
* @param \WP_Post $post Post object. |
| 128 |
*/ |
| 129 |
public static function schedule_post_activity( $new_status, $old_status, $post ) { |
| 130 |
if ( $post instanceof \WP_Post ) { |
| 131 |
if ( Extra_Fields::USER_POST_TYPE === $post->post_type ) { |
| 132 |
self::schedule_profile_update( $post->post_author ); |
| 133 |
} elseif ( Extra_Fields::BLOG_POST_TYPE === $post->post_type ) { |
| 134 |
self::schedule_profile_update( Actors::BLOG_USER_ID ); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Send profile updates for all local actors. |
| 141 |
* |
| 142 |
* Used when a site-wide setting that is part of every actor profile |
| 143 |
* changes, like the Starter Kit policy (FEP-7aa9 `canFeature`), so |
| 144 |
* followers of the blog actor and of every author receive the change. |
| 145 |
* |
| 146 |
* @since 9.0.1 |
| 147 |
*/ |
| 148 |
public static function schedule_all_profile_updates() { |
| 149 |
foreach ( Actors::get_all_ids() as $user_id ) { |
| 150 |
self::schedule_profile_update( $user_id ); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Send a profile update to all followers. Gets hooked into all relevant options/meta etc. |
| 156 |
* |
| 157 |
* @param int $user_id The user ID to update (Could be 0 for Blog-User). |
| 158 |
*/ |
| 159 |
public static function schedule_profile_update( $user_id ) { |
| 160 |
if ( \defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
$actor = Actors::get_by_id( $user_id ); |
| 165 |
|
| 166 |
if ( ! $actor || \is_wp_error( $actor ) ) { |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
$actor->set_updated( \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, \time() ) ); |
| 171 |
|
| 172 |
add_to_outbox( $actor, 'Update', $user_id ); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Send a profile update when a post's sticky status changes. |
| 177 |
* |
| 178 |
* @param int $post_id The post ID. |
| 179 |
*/ |
| 180 |
public static function sticky_post_update( $post_id ) { |
| 181 |
$post = \get_post( $post_id ); |
| 182 |
|
| 183 |
if ( ! $post ) { |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
self::schedule_profile_update( $post->post_author ); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Schedule a Delete activity when a user is deleted. |
| 192 |
* |
| 193 |
* @param int $user_id The user ID being deleted. |
| 194 |
*/ |
| 195 |
public static function schedule_user_delete( $user_id ) { |
| 196 |
// Get the actor before deletion to ensure we have the data. |
| 197 |
$actor = Actors::get_by_id( $user_id ); |
| 198 |
if ( \is_wp_error( $actor ) ) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
$activity = new Activity(); |
| 203 |
$activity->set_actor( $actor->get_id() ); |
| 204 |
$activity->set_object( $actor->get_id() ); |
| 205 |
$activity->set_type( 'Delete' ); |
| 206 |
|
| 207 |
add_to_outbox( $activity, null, $user_id ); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Remove outbox from post types to delete with user. |
| 212 |
* |
| 213 |
* Outbox items should not be deleted with the user, because we |
| 214 |
* need to federate the `Delete` Activities. |
| 215 |
* |
| 216 |
* @param array $post_types The post types to delete with user. |
| 217 |
* |
| 218 |
* @return array The post types to delete with user without outbox. |
| 219 |
*/ |
| 220 |
public static function post_types_to_delete_with_user( $post_types ) { |
| 221 |
return \array_diff( $post_types, array( Outbox::POST_TYPE ) ); |
| 222 |
} |
| 223 |
} |
| 224 |
|