PluginProbe
ActivityPub / 3.2.2
ActivityPub v3.2.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
← All changes | includes/class-migration.php +160 -20 2.0.03.2.2 View file →
@@ -1,9 +1,9 @@
1 1 <?php
2 2 namespace Activitypub;
3 3
4 4 use Activitypub\Activitypub;
5 -use Activitypub\Model\Blog_User;
5 +use Activitypub\Model\Blog;
6 6 use Activitypub\Collection\Followers;
7 7
8 8 /**
9 9 * ActivityPub Migration Class
@@ -14,9 +14,11 @@
14 14 /**
15 15 * Initialize the class, registering WordPress hooks
16 16 */
17 17 public static function init() {
18 - \add_action( 'activitypub_schedule_migration', array( self::class, 'maybe_migrate' ) );
18 + \add_action( 'activitypub_migrate', array( self::class, 'async_migration' ) );
19 +
20 + self::maybe_migrate();
19 21 }
20 22
21 23 /**
22 24 * Get the target version.
@@ -107,17 +109,33 @@
107 109 self::lock();
108 110
109 111 $version_from_db = self::get_version();
110 112
113 + // check for inital migration
114 + if ( ! $version_from_db ) {
115 + self::add_default_settings();
116 + $version_from_db = self::get_target_version();
117 + }
118 +
119 + // schedule the async migration
120 + if ( ! \wp_next_scheduled( 'activitypub_migrate', $version_from_db ) ) {
121 + \wp_schedule_single_event( \time(), 'activitypub_migrate', array( $version_from_db ) );
122 + }
111 123 if ( version_compare( $version_from_db, '0.17.0', '<' ) ) {
112 124 self::migrate_from_0_16();
113 125 }
114 - if ( version_compare( $version_from_db, '1.0.0', '<' ) ) {
115 - self::migrate_from_0_17();
116 - }
117 126 if ( version_compare( $version_from_db, '1.3.0', '<' ) ) {
118 127 self::migrate_from_1_2_0();
119 128 }
129 + if ( version_compare( $version_from_db, '2.1.0', '<' ) ) {
130 + self::migrate_from_2_0_0();
131 + }
132 + if ( version_compare( $version_from_db, '2.3.0', '<' ) ) {
133 + self::migrate_from_2_2_0();
134 + }
135 + if ( version_compare( $version_from_db, '3.0.0', '<' ) ) {
136 + self::migrate_from_2_6_0();
137 + }
120 138
121 139 update_option( 'activitypub_db_version', self::get_target_version() );
122 140
123 141 self::unlock();
@@ -123,25 +141,16 @@
123 141 self::unlock();
124 142 }
125 143
126 144 /**
127 - * Updates the DB-schema of the followers-list
145 + * Asynchronously migrates the database structure.
128 146 *
129 - * @return void
147 + * @param string $version_from_db The version from which to migrate.
130 148 */
131 - private static function migrate_from_0_17() {
132 - // migrate followers
133 - foreach ( get_users( array( 'fields' => 'ID' ) ) as $user_id ) {
134 - $followers = get_user_meta( $user_id, 'activitypub_followers', true );
135 -
136 - if ( $followers ) {
137 - foreach ( $followers as $actor ) {
138 - Followers::add_follower( $user_id, $actor );
139 - }
140 - }
149 + public static function async_migration( $version_from_db ) {
150 + if ( version_compare( $version_from_db, '1.0.0', '<' ) ) {
151 + self::migrate_from_0_17();
141 152 }
142 -
143 - Activitypub::flush_rewrite_rules();
144 153 }
145 154
146 155 /**
147 156 * Updates the custom template to use shortcodes instead of the deprecated templates.
@@ -180,14 +189,34 @@
180 189 }
181 190 }
182 191
183 192 /**
193 + * Updates the DB-schema of the followers-list
194 + *
195 + * @return void
196 + */
197 + public static function migrate_from_0_17() {
198 + // migrate followers
199 + foreach ( get_users( array( 'fields' => 'ID' ) ) as $user_id ) {
200 + $followers = get_user_meta( $user_id, 'activitypub_followers', true );
201 +
202 + if ( $followers ) {
203 + foreach ( $followers as $actor ) {
204 + Followers::add_follower( $user_id, $actor );
205 + }
206 + }
207 + }
208 +
209 + Activitypub::flush_rewrite_rules();
210 + }
211 +
212 + /**
184 213 * Clear the cache after updating to 1.3.0
185 214 *
186 215 * @return void
187 216 */
188 217 private static function migrate_from_1_2_0() {
189 - $user_ids = get_users(
218 + $user_ids = \get_users(
190 219 array(
191 220 'fields' => 'ID',
192 221 'capability__in' => array( 'publish_posts' ),
193 222 )
@@ -195,6 +224,117 @@
195 224
196 225 foreach ( $user_ids as $user_id ) {
197 226 wp_cache_delete( sprintf( Followers::CACHE_KEY_INBOXES, $user_id ), 'activitypub' );
198 227 }
228 + }
229 +
230 + /**
231 + * Unschedule Hooks after updating to 2.0.0
232 + *
233 + * @return void
234 + */
235 + private static function migrate_from_2_0_0() {
236 + wp_clear_scheduled_hook( 'activitypub_send_post_activity' );
237 + wp_clear_scheduled_hook( 'activitypub_send_update_activity' );
238 + wp_clear_scheduled_hook( 'activitypub_send_delete_activity' );
239 +
240 + wp_unschedule_hook( 'activitypub_send_post_activity' );
241 + wp_unschedule_hook( 'activitypub_send_update_activity' );
242 + wp_unschedule_hook( 'activitypub_send_delete_activity' );
243 +
244 + $object_type = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE );
245 + if ( 'article' === $object_type ) {
246 + \update_option( 'activitypub_object_type', 'wordpress-post-format' );
247 + }
248 + }
249 +
250 + /**
251 + * Add the ActivityPub capability to all users that can publish posts
252 + * Delete old meta to store followers
253 + *
254 + * @return void
255 + */
256 + private static function migrate_from_2_2_0() {
257 + // add the ActivityPub capability to all users that can publish posts
258 + self::add_activitypub_capability();
259 + }
260 +
261 + /**
262 + * Rename DB fields
263 + *
264 + * @return void
265 + */
266 + private static function migrate_from_2_6_0() {
267 + wp_cache_flush();
268 +
269 + self::update_usermeta_key( 'activitypub_user_description', 'activitypub_description' );
270 +
271 + self::update_options_key( 'activitypub_blog_user_description', 'activitypub_blog_description' );
272 + self::update_options_key( 'activitypub_blog_user_identifier', 'activitypub_blog_identifier' );
273 + }
274 +
275 + /**
276 + * Set the defaults needed for the plugin to work
277 + *
278 + * * Add the ActivityPub capability to all users that can publish posts
279 + *
280 + * @return void
281 + */
282 + public static function add_default_settings() {
283 + self::add_activitypub_capability();
284 + }
285 +
286 + /**
287 + * Add the ActivityPub capability to all users that can publish posts
288 + *
289 + * @return void
290 + */
291 + private static function add_activitypub_capability() {
292 + // get all WP_User objects that can publish posts
293 + $users = \get_users(
294 + array(
295 + 'capability__in' => array( 'publish_posts' ),
296 + )
297 + );
298 +
299 + // add ActivityPub capability to all users that can publish posts
300 + foreach ( $users as $user ) {
301 + $user->add_cap( 'activitypub' );
302 + }
303 + }
304 +
305 + /**
306 + * Rename meta keys.
307 + *
308 + * @param string $old The old commentmeta key
309 + * @param string $new The new commentmeta key
310 + */
311 + private static function update_usermeta_key( $old, $new ) { // phpcs:ignore
312 + global $wpdb;
313 +
314 + $wpdb->update( // phpcs:ignore
315 + $wpdb->usermeta,
316 + array( 'meta_key' => $new ), // phpcs:ignore
317 + array( 'meta_key' => $old ), // phpcs:ignore
318 + array( '%s' ),
319 + array( '%s' )
320 + );
321 + }
322 +
323 + /**
324 + * Rename option keys.
325 + *
326 + * @param string $old The old option key
327 + * @param string $new The new option key
328 + */
329 + private static function update_options_key( $old, $new ) { // phpcs:ignore
330 + global $wpdb;
331 +
332 + $wpdb->update( // phpcs:ignore
333 + $wpdb->options,
334 + array( 'option_name' => $new ), // phpcs:ignore
335 + array( 'option_name' => $old ), // phpcs:ignore
336 + array( '%s' ),
337 + array( '%s' )
338 + );
199 339 }
200 340 }