| 1 |
<?php |
| 2 |
|
| 3 |
namespace Activitypub; |
| 4 |
|
| 5 |
use Activitypub\Collection\Users; |
| 6 |
use Activitypub\Collection\Followers; |
| 7 |
use Activitypub\Transformer\Post; |
| 8 |
|
| 9 |
/** |
| 10 |
* ActivityPub Scheduler Class |
| 11 |
* |
| 12 |
* @author Matthias Pfefferle |
| 13 |
*/ |
| 14 |
class Scheduler { |
| 15 |
/** |
| 16 |
* Initialize the class, registering WordPress hooks |
| 17 |
*/ |
| 18 |
public static function init() { |
| 19 |
\add_action( 'transition_post_status', array( self::class, 'schedule_post_activity' ), 33, 3 ); |
| 20 |
|
| 21 |
\add_action( 'activitypub_update_followers', array( self::class, 'update_followers' ) ); |
| 22 |
\add_action( 'activitypub_cleanup_followers', array( self::class, 'cleanup_followers' ) ); |
| 23 |
|
| 24 |
\add_action( 'admin_init', array( self::class, 'schedule_migration' ) ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Schedule all ActivityPub schedules. |
| 29 |
* |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
public static function register_schedules() { |
| 33 |
if ( ! \wp_next_scheduled( 'activitypub_update_followers' ) ) { |
| 34 |
\wp_schedule_event( time(), 'hourly', 'activitypub_update_followers' ); |
| 35 |
} |
| 36 |
|
| 37 |
if ( ! \wp_next_scheduled( 'activitypub_cleanup_followers' ) ) { |
| 38 |
\wp_schedule_event( time(), 'daily', 'activitypub_cleanup_followers' ); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Unscedule all ActivityPub schedules. |
| 44 |
* |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
public static function deregister_schedules() { |
| 48 |
wp_unschedule_hook( 'activitypub_update_followers' ); |
| 49 |
wp_unschedule_hook( 'activitypub_cleanup_followers' ); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
/** |
| 54 |
* Schedule Activities. |
| 55 |
* |
| 56 |
* @param string $new_status New post status. |
| 57 |
* @param string $old_status Old post status. |
| 58 |
* @param WP_Post $post Post object. |
| 59 |
*/ |
| 60 |
public static function schedule_post_activity( $new_status, $old_status, $post ) { |
| 61 |
// Do not send activities if post is password protected. |
| 62 |
if ( \post_password_required( $post ) ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
// Check if post-type supports ActivityPub. |
| 67 |
$post_types = \get_post_types_by_support( 'activitypub' ); |
| 68 |
if ( ! \in_array( $post->post_type, $post_types, true ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
$type = false; |
| 73 |
|
| 74 |
if ( 'publish' === $new_status && 'publish' !== $old_status ) { |
| 75 |
$type = 'Create'; |
| 76 |
} elseif ( 'publish' === $new_status ) { |
| 77 |
$type = 'Update'; |
| 78 |
} elseif ( 'trash' === $new_status ) { |
| 79 |
$type = 'Delete'; |
| 80 |
} |
| 81 |
|
| 82 |
if ( ! $type ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
\wp_schedule_single_event( |
| 87 |
\time(), |
| 88 |
'activitypub_send_activity', |
| 89 |
array( $post, $type ) |
| 90 |
); |
| 91 |
|
| 92 |
\wp_schedule_single_event( |
| 93 |
\time(), |
| 94 |
sprintf( |
| 95 |
'activitypub_send_%s_activity', |
| 96 |
\strtolower( $type ) |
| 97 |
), |
| 98 |
array( $post ) |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Update followers |
| 104 |
* |
| 105 |
* @return void |
| 106 |
*/ |
| 107 |
public static function update_followers() { |
| 108 |
$number = 5; |
| 109 |
|
| 110 |
if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) { |
| 111 |
$number = 50; |
| 112 |
} |
| 113 |
|
| 114 |
$followers = Followers::get_outdated_followers( $number ); |
| 115 |
|
| 116 |
foreach ( $followers as $follower ) { |
| 117 |
$meta = get_remote_metadata_by_actor( $follower->get_url(), false ); |
| 118 |
|
| 119 |
if ( empty( $meta ) || ! is_array( $meta ) || is_wp_error( $meta ) ) { |
| 120 |
Followers::add_error( $follower->get__id(), $meta ); |
| 121 |
} else { |
| 122 |
$follower->from_array( $meta ); |
| 123 |
$follower->update(); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Cleanup followers |
| 130 |
* |
| 131 |
* @return void |
| 132 |
*/ |
| 133 |
public static function cleanup_followers() { |
| 134 |
$number = 5; |
| 135 |
|
| 136 |
if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) { |
| 137 |
$number = 50; |
| 138 |
} |
| 139 |
|
| 140 |
$followers = Followers::get_faulty_followers( $number ); |
| 141 |
|
| 142 |
foreach ( $followers as $follower ) { |
| 143 |
$meta = get_remote_metadata_by_actor( $follower->get_url(), false ); |
| 144 |
|
| 145 |
if ( is_tombstone( $meta ) ) { |
| 146 |
$follower->delete(); |
| 147 |
} elseif ( empty( $meta ) || ! is_array( $meta ) || is_wp_error( $meta ) ) { |
| 148 |
if ( $follower->count_errors() >= 5 ) { |
| 149 |
$follower->delete(); |
| 150 |
} else { |
| 151 |
Followers::add_error( $follower->get__id(), $meta ); |
| 152 |
} |
| 153 |
} else { |
| 154 |
$follower->reset_errors(); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Schedule migration if DB-Version is not up to date. |
| 161 |
* |
| 162 |
* @return void |
| 163 |
*/ |
| 164 |
public static function schedule_migration() { |
| 165 |
if ( ! \wp_next_scheduled( 'activitypub_schedule_migration' ) && ! Migration::is_latest_version() ) { |
| 166 |
\wp_schedule_single_event( \time(), 'activitypub_schedule_migration' ); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|