PluginProbe
ActivityPub / 1.0.0
ActivityPub v1.0.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 / class-scheduler.php

class-scheduler.php in ActivityPub 1.0.0, at includes/class-scheduler.php

158 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $followers = Followers::get_outdated_followers();
109
110 foreach ( $followers as $follower ) {
111 $meta = get_remote_metadata_by_actor( $follower->get_url(), true );
112
113 if ( empty( $meta ) || ! is_array( $meta ) || is_wp_error( $meta ) ) {
114 Followers::add_error( $follower->get__id(), $meta );
115 } else {
116 $follower->from_array( $meta );
117 $follower->update();
118 }
119 }
120 }
121
122 /**
123 * Cleanup followers
124 *
125 * @return void
126 */
127 public static function cleanup_followers() {
128 $followers = Followers::get_faulty_followers();
129
130 foreach ( $followers as $follower ) {
131 $meta = get_remote_metadata_by_actor( $follower->get_url(), true );
132
133 if ( is_tombstone( $meta ) ) {
134 $follower->delete();
135 } elseif ( empty( $meta ) || ! is_array( $meta ) || is_wp_error( $meta ) ) {
136 if ( $follower->count_errors() >= 5 ) {
137 $follower->delete();
138 } else {
139 Followers::add_error( $follower->get__id(), $meta );
140 }
141 } else {
142 $follower->reset_errors();
143 }
144 }
145 }
146
147 /**
148 * Schedule migration if DB-Version is not up to date.
149 *
150 * @return void
151 */
152 public static function schedule_migration() {
153 if ( ! \wp_next_scheduled( 'activitypub_schedule_migration' ) && ! Migration::is_latest_version() ) {
154 \wp_schedule_single_event( \time(), 'activitypub_schedule_migration' );
155 }
156 }
157 }
158