| @@ -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,14 +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(); | |
| 126 | + if ( version_compare( $version_from_db, '1.3.0', '<' ) ) { | |
| 127 | + self::migrate_from_1_2_0(); | |
| 116 | 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 | + } | |
| 117 | 138 | |
| 118 | 139 | update_option( 'activitypub_db_version', self::get_target_version() ); |
| 119 | 140 | |
| 120 | 141 | self::unlock(); |
| @@ -120,25 +141,16 @@ | ||
| 120 | 141 | self::unlock(); |
| 121 | 142 | } |
| 122 | 143 | |
| 123 | 144 | /** |
| 124 | - * Updates the DB-schema of the followers-list | |
| 145 | + * Asynchronously migrates the database structure. | |
| 125 | 146 | * |
| 126 | - * @return void | |
| 147 | + * @param string $version_from_db The version from which to migrate. | |
| 127 | 148 | */ |
| 128 | - private static function migrate_from_0_17() { | |
| 129 | - // migrate followers | |
| 130 | - foreach ( get_users( array( 'fields' => 'ID' ) ) as $user_id ) { | |
| 131 | - $followers = get_user_meta( $user_id, 'activitypub_followers', true ); | |
| 132 | - | |
| 133 | - if ( $followers ) { | |
| 134 | - foreach ( $followers as $actor ) { | |
| 135 | - Followers::add_follower( $user_id, $actor ); | |
| 136 | - } | |
| 137 | - } | |
| 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(); | |
| 138 | 152 | } |
| 139 | - | |
| 140 | - Activitypub::flush_rewrite_rules(); | |
| 141 | 153 | } |
| 142 | 154 | |
| 143 | 155 | /** |
| 144 | 156 | * Updates the custom template to use shortcodes instead of the deprecated templates. |
| @@ -174,6 +186,155 @@ | ||
| 174 | 186 | // Store the new template if required. |
| 175 | 187 | if ( $content !== $old_content || $need_update ) { |
| 176 | 188 | \update_option( 'activitypub_custom_post_content', $content ); |
| 177 | 189 | } |
| 190 | + } | |
| 191 | + | |
| 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 | + /** | |
| 213 | + * Clear the cache after updating to 1.3.0 | |
| 214 | + * | |
| 215 | + * @return void | |
| 216 | + */ | |
| 217 | + private static function migrate_from_1_2_0() { | |
| 218 | + $user_ids = \get_users( | |
| 219 | + array( | |
| 220 | + 'fields' => 'ID', | |
| 221 | + 'capability__in' => array( 'publish_posts' ), | |
| 222 | + ) | |
| 223 | + ); | |
| 224 | + | |
| 225 | + foreach ( $user_ids as $user_id ) { | |
| 226 | + wp_cache_delete( sprintf( Followers::CACHE_KEY_INBOXES, $user_id ), 'activitypub' ); | |
| 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 | + ); | |
| 178 | 339 | } |
| 179 | 340 | } |