self::ACTION_RESTORE, 'artifact_id' => $artifact_id, ), admin_url( 'admin-post.php' ) ), self::nonce_action( $artifact_id ) ); } /** * Dismiss URL. */ public static function dismiss_url(): string { return wp_nonce_url( add_query_arg( array( 'action' => self::ACTION_DISMISS ), admin_url( 'admin-post.php' ) ), self::ACTION_DISMISS ); } /** * Build the view-model for the current record (testable without output). * * @param array|null $rec Record (null → stored). * @return array{show:bool,level:string,status:string,text:string,artifact_id:int,can_restore:bool,notes:list} */ public static function view_model( ?array $rec = null ): array { $rec = null === $rec ? Pre_Update::last_record() : $rec; $out = array( 'show' => false, 'level' => 'info', 'status' => '', 'text' => '', 'artifact_id' => 0, 'can_restore' => false, 'notes' => array(), ); if ( empty( $rec ) || ! empty( $rec['seen'] ) ) { return $out; } $age = time() - (int) ( $rec['time'] ?? 0 ); if ( $age > self::MAX_AGE ) { return $out; } $status = (string) ( $rec['status'] ?? '' ); $items = isset( $rec['items'] ) && is_array( $rec['items'] ) ? $rec['items'] : array(); $parts = array(); foreach ( $items as $it ) { if ( ! is_array( $it ) ) { continue; } $label = (string) ( $it['slug'] ?? '' ); if ( ! empty( $it['from'] ) || ! empty( $it['to'] ) ) { $label .= ' (' . (string) ( $it['from'] ?: '?' ) . ' → ' . (string) ( $it['to'] ?: '?' ) . ')'; } if ( '' !== trim( $label ) ) { $parts[] = $label; } } $what = empty( $parts ) ? __( 'an update', '007aj-backup-restore-verifier' ) : implode( ', ', array_slice( $parts, 0, 8 ) ) . ( count( $parts ) > 8 ? '…' : '' ); $artifact_id = (int) ( $rec['artifact_id'] ?? 0 ); $out['show'] = true; $out['status'] = $status; $out['notes'] = isset( $rec['notes'] ) && is_array( $rec['notes'] ) ? array_values( array_map( 'strval', $rec['notes'] ) ) : array(); switch ( $status ) { case 'success': $out['level'] = 'success'; $out['text'] = sprintf( /* translators: %s: updated items */ __( '007AJ Backup Restore Verifier took a pre-update backup before updating %s. If something looks wrong you can roll back.', '007aj-backup-restore-verifier' ), $what ); $out['artifact_id'] = $artifact_id; break; case 'skipped': $out['level'] = 'info'; $out['text'] = sprintf( /* translators: 1: updated items, 2: reason */ __( '007AJ Backup Restore Verifier skipped a new pre-update backup before updating %1$s. %2$s', '007aj-backup-restore-verifier' ), $what, (string) ( $rec['message'] ?? '' ) ); $out['artifact_id'] = 'recent' === (string) ( $rec['reason'] ?? '' ) ? $artifact_id : 0; break; case 'timeout': $out['level'] = 'warning'; $out['text'] = sprintf( /* translators: 1: updated items, 2: message */ __( '007AJ Backup Restore Verifier’s pre-update backup for %1$s was still running when the update proceeded. %2$s', '007aj-backup-restore-verifier' ), $what, (string) ( $rec['message'] ?? '' ) ); break; default: $out['level'] = 'error'; $out['text'] = sprintf( /* translators: 1: updated items, 2: error */ __( '007AJ Backup Restore Verifier’s pre-update backup FAILED for %1$s. %2$s', '007aj-backup-restore-verifier' ), $what, (string) ( $rec['message'] ?? '' ) ); if ( ! empty( $rec['blocked'] ) ) { $out['text'] .= ' ' . __( 'The update was blocked because “block update if backup fails” is enabled.', '007aj-backup-restore-verifier' ); } else { $out['text'] .= ' ' . __( 'The update continued without a fresh backup.', '007aj-backup-restore-verifier' ); } break; } if ( $out['artifact_id'] > 0 ) { $repo = new Job_Repository(); $art = $repo->get_artifact( $out['artifact_id'] ); $out['can_restore'] = null !== $art && '' !== (string) $art['path'] && is_file( (string) $art['path'] ); } return $out; } /** * Guideline 11: the one-time result notice appears only on this plugin's own * screens and on the update-related core screens (Dashboard → Updates, Plugins, * Themes), never across the whole admin. It is dismissible and shown once per update. */ public static function is_relevant_screen(): bool { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || ! isset( $screen->id ) ) { return false; } $id = (string) $screen->id; if ( in_array( $id, array( 'update-core', 'plugins', 'themes' ), true ) ) { return true; } return false !== strpos( $id, Admin::MENU_SLUG ); } /** * Render the notice. */ public function render(): void { if ( ! current_user_can( 'manage_brverify' ) || ! self::is_relevant_screen() ) { return; } $vm = self::view_model(); if ( ! $vm['show'] ) { return; } $class = 'notice notice-' . ( in_array( $vm['level'], array( 'success', 'warning', 'error', 'info' ), true ) ? $vm['level'] : 'info' ); echo '

' . esc_html__( '007AJ Backup Restore Verifier', '007aj-backup-restore-verifier' ) . ': ' . esc_html( $vm['text'] ) . '

'; foreach ( $vm['notes'] as $note ) { echo '

' . esc_html( $note ) . '

'; } echo '

'; if ( $vm['can_restore'] ) { wp_enqueue_script( 'brverify-notice', BRVERIFY_URL . 'assets/js/notice.js', array(), BRVERIFY_VERSION, true ); echo '' . esc_html__( 'Restore this backup', '007aj-backup-restore-verifier' ) . ' '; } echo '' . esc_html__( 'Dismiss', '007aj-backup-restore-verifier' ) . '

'; } /** * Capability + nonce check, then queue a Phase C restore for the artifact. * * Split from the redirecting handler so it is unit-testable. Dies (wp_die) on a * capability or nonce failure — nothing is queued in that case. * * @param int $artifact_id Artifact id. * @return array{ok:bool,job_id?:int,error?:string} */ public function process_restore_request( int $artifact_id ): array { if ( ! current_user_can( 'manage_brverify' ) ) { wp_die( esc_html__( 'You do not have permission to restore backups.', '007aj-backup-restore-verifier' ), '', array( 'response' => 403 ) ); } check_admin_referer( self::nonce_action( $artifact_id ) ); if ( $artifact_id <= 0 ) { return array( 'ok' => false, 'error' => __( 'Missing artifact.', '007aj-backup-restore-verifier' ), ); } $runner = new Job_Runner(); $queued = $runner->queue_restore( array( 'artifact_id' => $artifact_id, 'components' => array( 'database' => true, 'plugins' => true, 'themes' => true, 'uploads' => true, ), 'trigger' => 'pre_update_notice', ) ); if ( empty( $queued['ok'] ) ) { return $queued; } $rec = Pre_Update::last_record(); $rec['seen'] = true; update_option( Pre_Update::OPTION_LAST, $rec, false ); return $queued; } /** * admin-post handler: queue the restore, start it, hand off to the 007AJ Backup Restore Verifier screen. */ public function handle_restore(): void { $artifact_id = isset( $_GET['artifact_id'] ) ? absint( wp_unslash( $_GET['artifact_id'] ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- verified in process_restore_request(). $res = $this->process_restore_request( $artifact_id ); if ( empty( $res['ok'] ) ) { wp_die( esc_html( (string) ( $res['error'] ?? __( 'Unable to start restore.', '007aj-backup-restore-verifier' ) ) ), '', array( 'response' => 400, 'back_link' => true, ) ); } ( new Job_Runner() )->tick( (int) $res['job_id'] ); // Start; the 007AJ Backup Restore Verifier screen resumes ticking via data-active-job. wp_safe_redirect( add_query_arg( array( 'page' => Admin::MENU_SLUG ), admin_url( 'admin.php' ) ) ); exit; } /** * Mark the notice seen. */ public function handle_dismiss(): void { if ( ! current_user_can( 'manage_brverify' ) ) { wp_die( esc_html__( 'You do not have permission.', '007aj-backup-restore-verifier' ), '', array( 'response' => 403 ) ); } check_admin_referer( self::ACTION_DISMISS ); $rec = Pre_Update::last_record(); $rec['seen'] = true; update_option( Pre_Update::OPTION_LAST, $rec, false ); $back = wp_get_referer(); wp_safe_redirect( $back ? $back : admin_url() ); exit; } }