| 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( 'updated_user_meta', array( self::class, 'user_meta_update' ), 10, 3 ); |
| 43 |
// @todo figure out a feasible way of updating the header image since it's not unique to any user. |
| 44 |
} |
| 45 |
|
| 46 |
\add_action( 'add_option_activitypub_actor_mode', array( self::class, 'blog_user_update' ) ); |
| 47 |
\add_action( 'update_option_activitypub_actor_mode', array( self::class, 'blog_user_update' ) ); |
| 48 |
|
| 49 |
\add_action( 'transition_post_status', array( self::class, 'schedule_post_activity' ), 33, 3 ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Send a profile update when relevant user meta is updated. |
| 54 |
* |
| 55 |
* @param int $meta_id Meta ID being updated. |
| 56 |
* @param int $user_id User ID being updated. |
| 57 |
* @param string $meta_key Meta key being updated. |
| 58 |
*/ |
| 59 |
public static function user_meta_update( $meta_id, $user_id, $meta_key ) { |
| 60 |
// Don't bother if the user can't publish. |
| 61 |
if ( ! \user_can( $user_id, 'activitypub' ) ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
// The user meta fields that affect a profile. |
| 66 |
$fields = array( |
| 67 |
'activitypub_description', |
| 68 |
'activitypub_header_image', |
| 69 |
'description', |
| 70 |
'user_url', |
| 71 |
'display_name', |
| 72 |
); |
| 73 |
|
| 74 |
if ( in_array( $meta_key, $fields, true ) ) { |
| 75 |
self::schedule_profile_update( $user_id ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Send a profile update when a user is updated. |
| 81 |
* |
| 82 |
* @param int $user_id User ID being updated. |
| 83 |
*/ |
| 84 |
public static function user_update( $user_id ) { |
| 85 |
// Don't bother if the user can't publish. |
| 86 |
if ( ! \user_can( $user_id, 'activitypub' ) ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
self::schedule_profile_update( $user_id ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Theme mods only have a dynamic filter so we fudge it like this. |
| 95 |
* |
| 96 |
* @param mixed $value Optional. The value to be updated. Default null. |
| 97 |
* |
| 98 |
* @return mixed |
| 99 |
*/ |
| 100 |
public static function blog_user_update( $value = null ) { |
| 101 |
self::schedule_profile_update( Actors::BLOG_USER_ID ); |
| 102 |
return $value; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Schedule Activities. |
| 107 |
* |
| 108 |
* @param string $new_status New post status. |
| 109 |
* @param string $old_status Old post status. |
| 110 |
* @param \WP_Post $post Post object. |
| 111 |
*/ |
| 112 |
public static function schedule_post_activity( $new_status, $old_status, $post ) { |
| 113 |
if ( Extra_Fields::USER_POST_TYPE === $post->post_type ) { |
| 114 |
self::schedule_profile_update( $post->post_author ); |
| 115 |
} elseif ( Extra_Fields::BLOG_POST_TYPE === $post->post_type ) { |
| 116 |
self::schedule_profile_update( Actors::BLOG_USER_ID ); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Send a profile update to all followers. Gets hooked into all relevant options/meta etc. |
| 122 |
* |
| 123 |
* @param int $user_id The user ID to update (Could be 0 for Blog-User). |
| 124 |
*/ |
| 125 |
public static function schedule_profile_update( $user_id ) { |
| 126 |
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
add_to_outbox( Actors::get_by_id( $user_id ), 'Update', $user_id ); |
| 131 |
} |
| 132 |
} |
| 133 |
|