PluginProbe
ActivityPub / 5.7.0
ActivityPub v5.7.0
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 5.7.0, at includes/scheduler/class-actor.php

147 lines 4.8 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\Collection\Actors;
11 use Activitypub\Collection\Extra_Fields;
12
13 use function Activitypub\add_to_outbox;
14 use function Activitypub\is_user_type_disabled;
15
16 /**
17 * Post scheduler class.
18 */
19 class Actor {
20 /**
21 * Initialize the class, registering WordPress hooks.
22 */
23 public static function init() {
24 // Profile updates for blog options.
25 if ( ! is_user_type_disabled( 'blog' ) ) {
26 \add_action( 'update_option_site_icon', array( self::class, 'blog_user_update' ) );
27 \add_action( 'update_option_blogdescription', array( self::class, 'blog_user_update' ) );
28 \add_action( 'update_option_blogname', array( self::class, 'blog_user_update' ) );
29 \add_action( 'add_option_activitypub_header_image', array( self::class, 'blog_user_update' ) );
30 \add_action( 'update_option_activitypub_header_image', array( self::class, 'blog_user_update' ) );
31 \add_action( 'add_option_activitypub_blog_identifier', array( self::class, 'blog_user_update' ) );
32 \add_action( 'update_option_activitypub_blog_identifier', array( self::class, 'blog_user_update' ) );
33 \add_action( 'add_option_activitypub_blog_description', array( self::class, 'blog_user_update' ) );
34 \add_action( 'update_option_activitypub_blog_description', array( self::class, 'blog_user_update' ) );
35 \add_filter( 'pre_set_theme_mod_custom_logo', array( self::class, 'blog_user_update' ) );
36 \add_filter( 'pre_set_theme_mod_header_image', array( self::class, 'blog_user_update' ) );
37 }
38
39 // Profile updates for user options.
40 if ( ! is_user_type_disabled( 'user' ) ) {
41 \add_action( 'profile_update', array( self::class, 'user_update' ) );
42 \add_action( 'added_user_meta', array( self::class, 'user_meta_update' ), 10, 3 );
43 \add_action( 'updated_user_meta', array( self::class, 'user_meta_update' ), 10, 3 );
44 // @todo figure out a feasible way of updating the header image since it's not unique to any user.
45 }
46
47 \add_action( 'add_option_activitypub_actor_mode', array( self::class, 'blog_user_update' ) );
48 \add_action( 'update_option_activitypub_actor_mode', array( self::class, 'blog_user_update' ) );
49
50 \add_action( 'transition_post_status', array( self::class, 'schedule_post_activity' ), 33, 3 );
51 }
52
53 /**
54 * Send a profile update when relevant user meta is updated.
55 *
56 * @param int $meta_id Meta ID being updated.
57 * @param int $user_id User ID being updated.
58 * @param string $meta_key Meta key being updated.
59 */
60 public static function user_meta_update( $meta_id, $user_id, $meta_key ) {
61 // Don't bother if the user can't publish.
62 if ( ! \user_can( $user_id, 'activitypub' ) ) {
63 return;
64 }
65
66 $blog_prefix = $GLOBALS['wpdb']->get_blog_prefix();
67
68 // The user meta fields that affect a profile.
69 $fields = array(
70 $blog_prefix . 'activitypub_description',
71 $blog_prefix . 'activitypub_header_image',
72 $blog_prefix . 'activitypub_icon',
73 'description',
74 'display_name',
75 'user_url',
76 );
77
78 if ( in_array( $meta_key, $fields, true ) ) {
79 self::schedule_profile_update( $user_id );
80 }
81 }
82
83 /**
84 * Send a profile update when a user is updated.
85 *
86 * @param int $user_id User ID being updated.
87 */
88 public static function user_update( $user_id ) {
89 // Don't bother if the user can't publish.
90 if ( ! \user_can( $user_id, 'activitypub' ) ) {
91 return;
92 }
93
94 self::schedule_profile_update( $user_id );
95 }
96
97 /**
98 * Theme mods only have a dynamic filter so we fudge it like this.
99 *
100 * @param mixed $value Optional. The value to be updated. Default null.
101 *
102 * @return mixed
103 */
104 public static function blog_user_update( $value = null ) {
105 self::schedule_profile_update( Actors::BLOG_USER_ID );
106 return $value;
107 }
108
109 /**
110 * Schedule Activities.
111 *
112 * @param string $new_status New post status.
113 * @param string $old_status Old post status.
114 * @param \WP_Post $post Post object.
115 */
116 public static function schedule_post_activity( $new_status, $old_status, $post ) {
117 if ( $post instanceof \WP_Post ) {
118 if ( Extra_Fields::USER_POST_TYPE === $post->post_type ) {
119 self::schedule_profile_update( $post->post_author );
120 } elseif ( Extra_Fields::BLOG_POST_TYPE === $post->post_type ) {
121 self::schedule_profile_update( Actors::BLOG_USER_ID );
122 }
123 }
124 }
125
126 /**
127 * Send a profile update to all followers. Gets hooked into all relevant options/meta etc.
128 *
129 * @param int $user_id The user ID to update (Could be 0 for Blog-User).
130 */
131 public static function schedule_profile_update( $user_id ) {
132 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
133 return;
134 }
135
136 $actor = Actors::get_by_id( $user_id );
137
138 if ( ! $actor || \is_wp_error( $actor ) ) {
139 return;
140 }
141
142 $actor->set_updated( gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, time() ) );
143
144 add_to_outbox( $actor, 'Update', $user_id );
145 }
146 }
147