PluginProbe
ActivityPub / 7.8.2
ActivityPub v7.8.2
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / scheduler / class-actor.php

class-actor.php in ActivityPub 7.8.2, at includes/scheduler/class-actor.php

205 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 \add_action( 'transition_post_status', array( self::class, 'schedule_post_activity' ), 33, 3 );
53
54 \add_action( 'post_stuck', array( self::class, 'sticky_post_update' ) );
55 \add_action( 'post_unstuck', array( self::class, 'sticky_post_update' ) );
56
57 // User deletion handling.
58 \add_action( 'delete_user', array( self::class, 'schedule_user_delete' ), 10, 3 );
59 \add_filter( 'post_types_to_delete_with_user', array( self::class, 'post_types_to_delete_with_user' ) );
60 }
61
62 /**
63 * Send a profile update when relevant user meta is updated.
64 *
65 * @param int $meta_id Meta ID being updated.
66 * @param int $user_id User ID being updated.
67 * @param string $meta_key Meta key being updated.
68 */
69 public static function user_meta_update( $meta_id, $user_id, $meta_key ) {
70 // Don't bother if the user can't publish.
71 if ( ! \user_can( $user_id, 'activitypub' ) ) {
72 return;
73 }
74
75 $blog_prefix = $GLOBALS['wpdb']->get_blog_prefix();
76
77 // The user meta fields that affect a profile.
78 $fields = array(
79 $blog_prefix . 'activitypub_description',
80 $blog_prefix . 'activitypub_header_image',
81 $blog_prefix . 'activitypub_icon',
82 'description',
83 'display_name',
84 'user_url',
85 );
86
87 if ( in_array( $meta_key, $fields, true ) ) {
88 self::schedule_profile_update( $user_id );
89 }
90 }
91
92 /**
93 * Send a profile update when a user is updated.
94 *
95 * @param int $user_id User ID being updated.
96 */
97 public static function user_update( $user_id ) {
98 // Don't bother if the user can't publish.
99 if ( ! \user_can( $user_id, 'activitypub' ) ) {
100 return;
101 }
102
103 self::schedule_profile_update( $user_id );
104 }
105
106 /**
107 * Theme mods only have a dynamic filter so we fudge it like this.
108 *
109 * @param mixed $value Optional. The value to be updated. Default null.
110 *
111 * @return mixed
112 */
113 public static function blog_user_update( $value = null ) {
114 self::schedule_profile_update( Actors::BLOG_USER_ID );
115 return $value;
116 }
117
118 /**
119 * Schedule Activities.
120 *
121 * @param string $new_status New post status.
122 * @param string $old_status Old post status.
123 * @param \WP_Post $post Post object.
124 */
125 public static function schedule_post_activity( $new_status, $old_status, $post ) {
126 if ( $post instanceof \WP_Post ) {
127 if ( Extra_Fields::USER_POST_TYPE === $post->post_type ) {
128 self::schedule_profile_update( $post->post_author );
129 } elseif ( Extra_Fields::BLOG_POST_TYPE === $post->post_type ) {
130 self::schedule_profile_update( Actors::BLOG_USER_ID );
131 }
132 }
133 }
134
135 /**
136 * Send a profile update to all followers. Gets hooked into all relevant options/meta etc.
137 *
138 * @param int $user_id The user ID to update (Could be 0 for Blog-User).
139 */
140 public static function schedule_profile_update( $user_id ) {
141 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
142 return;
143 }
144
145 $actor = Actors::get_by_id( $user_id );
146
147 if ( ! $actor || \is_wp_error( $actor ) ) {
148 return;
149 }
150
151 $actor->set_updated( gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, time() ) );
152
153 add_to_outbox( $actor, 'Update', $user_id );
154 }
155
156 /**
157 * Detect sticky posts update.
158 *
159 * @param int $post_id The post ID.
160 */
161 public static function sticky_post_update( $post_id ) {
162 $post = \get_post( $post_id );
163
164 if ( ! $post ) {
165 return;
166 }
167
168 self::schedule_profile_update( $post->post_author );
169 }
170
171 /**
172 * Schedule a Delete activity when a user is deleted.
173 *
174 * @param int $user_id The user ID being deleted.
175 */
176 public static function schedule_user_delete( $user_id ) {
177 // Get the actor before deletion to ensure we have the data.
178 $actor = Actors::get_by_id( $user_id );
179 if ( \is_wp_error( $actor ) ) {
180 return;
181 }
182
183 $activity = new Activity();
184 $activity->set_actor( $actor->get_id() );
185 $activity->set_object( $actor->get_id() );
186 $activity->set_type( 'Delete' );
187
188 add_to_outbox( $activity, null, $user_id );
189 }
190
191 /**
192 * Remove outbox from post types to delete with user.
193 *
194 * Outbox items should not be deleted with the user, because we
195 * need to federate the `Delete` Activities.
196 *
197 * @param array $post_types The post types to delete with user.
198 *
199 * @return array The post types to delete with user without outbox.
200 */
201 public static function post_types_to_delete_with_user( $post_types ) {
202 return \array_diff( $post_types, array( Outbox::POST_TYPE ) );
203 }
204 }
205