type, array( 'edit', 'import' ), true ) ) { return new \WP_Error( 'invalid_job', __( 'Only slug edit and import jobs can be rolled back.', 'slugh' ) ); } if ( ! in_array( $target->status, array( 'completed', 'completed_with_errors', 'partially_rolled_back', 'paused' ), true ) ) { return new \WP_Error( 'invalid_job', __( 'Only finished or paused jobs can be rolled back.', 'slugh' ) ); } $running = JobRepository::query( array( 'type' => 'rollback', 'status' => array( 'confirmed', 'running', 'paused' ), ) ); foreach ( $running['items'] as $job ) { if ( (int) $job->parent_job_id === (int) $target_job_id ) { /* translators: %d: rollback job ID */ return new \WP_Error( 'rollback_in_progress', sprintf( __( 'Rollback job #%d for this job is still in progress.', 'slugh' ), $job->id ) ); } } $applied = JobRepository::get_items( $target_job_id, array( 'result' => 'applied' ) ); $direct = array(); foreach ( $applied as $item ) { if ( 'direct' === $item->kind && empty( $item->message['rolled_back_by'] ) && ( null === $item_ids || in_array( $item->id, array_map( 'absint', $item_ids ), true ) ) ) { $direct[ $item->id ] = $item; } } if ( empty( $direct ) ) { return new \WP_Error( 'nothing_to_rollback', __( 'There are no applied items left to roll back.', 'slugh' ) ); } $rollback_id = JobRepository::create( 'rollback', 'confirmed', array( 'phase' => 'apply', 'ownership_mode' => Settings::get( 'redirect_ownership_mode' ), 'trial_mode' => (bool) Settings::get( 'trial_mode', false ), 'report' => array(), 'verify_offset' => 0, ), $target_job_id ); foreach ( $direct as $item ) { self::add_rollback_item( $rollback_id, $item ); } // Descendant items whose parent item is rolled back. foreach ( $applied as $item ) { if ( 'descendant' === $item->kind && empty( $item->message['rolled_back_by'] ) && isset( $item->message['parent_item'] ) && ( null === $item_ids || self::parent_in_set( $item, $direct, $target_job_id ) ) ) { self::add_rollback_item( $rollback_id, $item ); } } JobRepository::update( $rollback_id, array( 'confirmed_at' => current_time( 'mysql', true ), 'counts' => JobRunner::summarize( $rollback_id ), ) ); AuditLogger::log( 'rollback_create', 'job', $target_job_id, null, array( 'rollback_job_id' => $rollback_id, 'items' => count( $direct ), ), $rollback_id ); return $rollback_id; } /** * Whether a descendant item's parent (or any ancestor item) is part of the rollback set. * * @param object $item Descendant item. * @param array $direct Direct items being rolled back, keyed by item ID. * @param int $target_job_id Target job. * @return bool */ private static function parent_in_set( $item, array $direct, $target_job_id ) { if ( isset( $direct[ (int) $item->message['parent_item'] ] ) ) { return true; } // The descendant may have been added for another direct ancestor in the same job. $ancestors = get_post_ancestors( $item->post_id ); foreach ( $direct as $direct_item ) { if ( in_array( (int) $direct_item->post_id, array_map( 'intval', $ancestors ), true ) && (int) $direct_item->job_id === (int) $target_job_id ) { return true; } } return false; } /** * Add one rollback item mirroring a target item. * * @param int $rollback_id Rollback job. * @param object $item Target item. * @return void */ private static function add_rollback_item( $rollback_id, $item ) { JobRepository::add_item( $rollback_id, array( 'post_id' => $item->post_id, 'kind' => $item->kind, 'post_type' => $item->post_type, 'parent_id' => $item->parent_id, 'old_post_name' => (string) $item->actual_post_name, 'requested_post_name' => $item->old_post_name, 'old_permalink' => (string) $item->actual_permalink, 'expected_permalink' => $item->old_permalink, 'validation_state' => 'ok', 'result' => 'pending', 'sort_order' => 'descendant' === $item->kind ? JobRunner::DESCENDANT_ORDER + DescendantPropagator::depth( $item->post_id ) : DescendantPropagator::depth( $item->post_id ), 'message' => array( 'target_item' => $item->id, 'snapshot' => isset( $item->message['snapshot'] ) ? array( 'post_status' => isset( $item->message['snapshot']['post_status'] ) ? $item->message['snapshot']['post_status'] : '' ) : array(), ), ) ); } /** * Process one rollback item. * * @param object $job Rollback job. * @param object $item Rollback item. * @return string Result. */ public static function process_item( $job, $item ) { $target_item = JobRepository::get_item( isset( $item->message['target_item'] ) ? (int) $item->message['target_item'] : 0 ); if ( ! $target_item ) { return JobRunner::finish_item( $item, 'failed', array( 'reason' => 'target_missing' ) ); } clean_post_cache( $item->post_id ); $post = get_post( $item->post_id ); if ( ! $post ) { return JobRunner::finish_item( $item, 'skipped', array( 'reason' => 'not_found' ) ); } return 'descendant' === $item->kind ? self::process_descendant( $job, $item, $target_item, $post ) : self::process_direct( $job, $item, $target_item, $post ); } /** * Restore a direct item's slug. * * @param object $job Rollback job. * @param object $item Rollback item. * @param object $target_item Target item. * @param \WP_Post $post Post. * @return string Result. */ private static function process_direct( $job, $item, $target_item, $post ) { $current_link = (string) get_permalink( $post ); // The record must still be exactly as the job left it. if ( $post->post_name !== $item->old_post_name || Normalizer::normalize_path( $current_link ) !== Normalizer::normalize_path( $item->old_permalink ) ) { return JobRunner::finish_item( $item, 'skipped', array( 'reason' => 'changed_since_job', 'detail' => sprintf( /* translators: 1: slug the job set, 2: current slug */ __( 'The slug changed after the job (job set "%1$s", now "%2$s").', 'slugh' ), rawurldecode( $item->old_post_name ), rawurldecode( $post->post_name ) ), ) ); } // The original slug must still be free. $original = $item->requested_post_name; $unique = wp_unique_post_slug( $original, $post->ID, 'publish', $post->post_type, $post->post_parent ); if ( $unique !== $original ) { return JobRunner::finish_item( $item, 'skipped', array( 'reason' => 'original_slug_taken', 'detail' => __( 'Another record now uses the original slug.', 'slugh' ), ) ); } $update = SlugUpdater::update( $post->ID, $original, $job->id ); if ( is_wp_error( $update ) ) { return JobRunner::finish_item( $item, 'failed', array( 'reason' => $update->get_error_code(), 'detail' => $update->get_error_message(), ) ); } $columns = array( 'actual_post_name' => $update['actual_slug'], 'actual_permalink' => $update['actual_permalink'], 'hash_after' => $update['hash_after'], 'hash_before' => $update['hash_before'], ); if ( ! empty( $update['changed_fields'] ) ) { JobRunner::finish_item( $item, 'integrity_changed', array( 'changed_fields' => $update['changed_fields'] ), $columns ); return 'integrity_changed'; } $notes = array(); if ( $update['actual_slug'] !== $original ) { $notes['restore_mismatch'] = $update['actual_slug']; } $redirect = self::undo_redirects( $job, $target_item ); $reverse = self::reverse_redirect( $job, $item, $update['actual_permalink'], $post->post_status ); if ( $reverse ) { JobRunner::record_redirect( $redirect, $reverse ); } CacheManager::purge_urls( array( $item->old_permalink, $update['actual_permalink'] ), $post->ID ); AuditLogger::log( 'rollback_item', 'post', $post->ID, array( 'slug' => $item->old_post_name ), array( 'slug' => $update['actual_slug'] ), $job->id ); JobRepository::merge_item_message( $target_item, array( 'rolled_back_by' => $job->id ) ); $notes['redirects'] = $redirect; $columns['redirect_ids'] = $redirect['ids']; return JobRunner::finish_item( $item, 'applied', $notes, $columns ); } /** * Roll back a descendant: its URL is restored by the parent; undo its redirects. * * @param object $job Rollback job. * @param object $item Rollback item. * @param object $target_item Target item. * @param \WP_Post $post Post. * @return string Result. */ private static function process_descendant( $job, $item, $target_item, $post ) { $current_link = (string) get_permalink( $post ); if ( Normalizer::normalize_path( $current_link ) !== Normalizer::normalize_path( $item->expected_permalink ) ) { return JobRunner::finish_item( $item, 'skipped', array( 'reason' => 'changed_since_job', 'detail' => __( 'The descendant URL did not return to its original value (its parent was not restored, or it changed after the job).', 'slugh' ), ), array( 'actual_permalink' => $current_link ) ); } $redirect = self::undo_redirects( $job, $target_item ); $reverse = self::reverse_redirect( $job, $item, $current_link, $post->post_status ); if ( $reverse ) { JobRunner::record_redirect( $redirect, $reverse ); } CacheManager::purge_urls( array( $item->old_permalink, $current_link ), $post->ID ); JobRepository::merge_item_message( $target_item, array( 'rolled_back_by' => $job->id ) ); return JobRunner::finish_item( $item, 'applied', array( 'redirects' => $redirect ), array( 'actual_post_name' => $post->post_name, 'actual_permalink' => $current_link, 'redirect_ids' => $redirect['ids'], ) ); } /** * Disable (never delete) the redirects the target item created; restore the ones it changed. * * @param object $job Rollback job. * @param object $target_item Target item. * @return array Redirect record for the rollback item. */ private static function undo_redirects( $job, $target_item ) { $record = array( 'ids' => array(), 'actions' => array(), 'flattened' => array(), 'previous' => array(), 'disabled' => array(), ); $original = isset( $target_item->message['redirects'] ) ? (array) $target_item->message['redirects'] : array(); $previous_by_id = array(); foreach ( isset( $original['previous'] ) ? (array) $original['previous'] : array() as $prev ) { $previous_by_id[ (int) $prev['id'] ] = $prev; } foreach ( isset( $original['actions'] ) ? (array) $original['actions'] : array() as $action ) { $id = (int) $action['id']; if ( ! $id ) { continue; } if ( in_array( $action['action'], array( 'created', 'reenabled' ), true ) ) { if ( true === RedirectRepository::set_enabled( $id, false, $job->id ) ) { $record['disabled'][] = $id; } } elseif ( 'replaced' === $action['action'] && isset( $previous_by_id[ $id ] ) ) { $prev = $previous_by_id[ $id ]; RedirectRepository::update( $id, array( 'destination' => $prev['destination'], 'status_code' => $prev['status_code'], 'match_mode' => $prev['match_mode'], 'enabled' => $prev['enabled'], ), $job->id ); } } // Chain flattening: point those redirects back at their previous destinations. foreach ( isset( $original['flattened'] ) ? (array) $original['flattened'] : array() as $flat ) { RedirectRepository::restore_destination( (int) $flat['id'], $flat['previous'], $job->id ); } // Redirects the job disabled because they sat on the new path: enable them again. foreach ( isset( $original['disabled'] ) ? (array) $original['disabled'] : array() as $id ) { RedirectRepository::set_enabled( (int) $id, true, $job->id ); } return $record; } /** * Reverse redirect from the abandoned job URL to the restored URL (setting, on by default). * * @param object $job Rollback job. * @param object $item Rollback item (old_permalink = abandoned job URL). * @param string $restored_url Restored permalink. * @param string $post_status Current status. * @return array|null create() result. */ private static function reverse_redirect( $job, $item, $restored_url, $post_status ) { $public = Settings::get( 'redirect_private_posts', false ) ? array( 'publish', 'private' ) : array( 'publish' ); if ( ! Settings::get( 'rollback_reverse_redirect', true ) || ! Settings::slugh_owns_redirects() || ! in_array( $post_status, $public, true ) ) { return null; } if ( Normalizer::normalize_path( $item->old_permalink ) === Normalizer::normalize_path( $restored_url ) ) { return null; } $created = RedirectRepository::create( array( 'source' => $item->old_permalink, 'destination' => $restored_url, 'status_code' => ! empty( $job->settings['trial_mode'] ) ? 302 : 301, 'origin' => 'rollback', 'post_id' => $item->post_id, 'job_id' => $job->id, 'on_conflict' => 'keep', ) ); return is_wp_error( $created ) ? null : $created; } /** * After a rollback job finishes, mark the target job rolled_back or partially_rolled_back. * * @param int $target_job_id Target job. * @return void */ public static function update_target_status( $target_job_id ) { $items = JobRepository::get_items( $target_job_id, array( 'result' => 'applied' ) ); $rolled_back = 0; foreach ( $items as $item ) { if ( ! empty( $item->message['rolled_back_by'] ) ) { ++$rolled_back; } } if ( 0 === $rolled_back ) { return; } JobRepository::update( $target_job_id, array( 'status' => count( $items ) === $rolled_back ? 'rolled_back' : 'partially_rolled_back' ) ); } }