Report (empty when nothing was done). */ public static function maybe_migrate(): array { if ( '' !== (string) get_option( self::DONE_OPTION, '' ) ) { return array(); } return self::migrate(); } /** * Perform the migration. * * @return array */ public static function migrate(): array { global $wpdb; $report = array( 'options' => 0, 'tables' => array(), 'dir' => false, 'paths' => 0, ); // 1. Options: every restoreproof_ becomes brverify_. $like = $wpdb->esc_like( 'restoreproof_' ) . '%'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $names = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s", $like ) ); foreach ( is_array( $names ) ? $names : array() as $old ) { $old = (string) $old; $new = 'brverify_' . substr( $old, strlen( 'restoreproof_' ) ); $val = get_option( $old ); if ( false === get_option( $new ) ) { if ( 'brverify_settings' === $new && is_array( $val ) ) { $val['working_dir_rel'] = self::NEW_DIR; if ( ! empty( $val['delete_artifacts_on_uninstall'] ) ) { $val['delete_data_on_uninstall'] = true; } unset( $val['delete_artifacts_on_uninstall'], $val['pro_cta_dismissed'] ); } add_option( $new, $val, '', false ); ++$report['options']; } delete_option( $old ); } // Old transients. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", $wpdb->esc_like( '_transient_restoreproof_' ) . '%', $wpdb->esc_like( '_transient_timeout_restoreproof_' ) . '%' ) ); // 2. Tables: rename in place (rows preserved). An empty freshly created new table is replaced. foreach ( array( 'jobs', 'logs', 'artifacts' ) as $t ) { $old = $wpdb->prefix . 'restoreproof_' . $t; $new = $wpdb->prefix . 'brverify_' . $t; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $has_old = ( $old === $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $old ) ) ) ); if ( ! $has_old ) { continue; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $has_new = ( $new === $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $new ) ) ) ); if ( $has_new ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $rows = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$new}" ); if ( $rows > 0 ) { $report['tables'][ $t ] = 'both_exist_old_kept'; continue; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $wpdb->query( "DROP TABLE {$new}" ); } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $ok = $wpdb->query( "RENAME TABLE {$old} TO {$new}" ); $report['tables'][ $t ] = false === $ok ? 'rename_failed' : 'renamed'; } // 3. Working directory + stored paths. // The protected working directory lives in wp-content (not the plugin folder); the pre-release one sits next to it. $new_dir = Working_Dir::legacy_root(); // Relocated to uploads afterwards by Working_Dir::maybe_relocate(). $old_dir = trailingslashit( dirname( $new_dir ) ) . self::OLD_DIR; if ( is_dir( $old_dir ) && ! is_link( $old_dir ) ) { $new_empty = ! is_dir( $new_dir ) || self::dir_is_empty( $new_dir ); if ( $new_empty ) { if ( is_dir( $new_dir ) ) { @rmdir( $new_dir ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir, WordPress.PHP.NoSilencedErrors.Discouraged } // phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename, WordPress.PHP.NoSilencedErrors.Discouraged $report['dir'] = @rename( $old_dir, $new_dir ); } } if ( ! empty( $report['dir'] ) ) { $cols = array( 'jobs' => array( 'cursor_json', 'config_json', 'result_json', 'error_message' ), 'logs' => array( 'context_json', 'message' ), 'artifacts' => array( 'path', 'remote_ref', 'meta_json' ), ); foreach ( $cols as $t => $list ) { $table = $wpdb->prefix . 'brverify_' . $t; foreach ( $list as $col ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $n = $wpdb->query( $wpdb->prepare( "UPDATE {$table} SET {$col} = REPLACE({$col}, %s, %s) WHERE {$col} LIKE %s", self::OLD_DIR, self::NEW_DIR, '%' . $wpdb->esc_like( self::OLD_DIR ) . '%' ) ); $report['paths'] += is_int( $n ) ? $n : 0; } } } // 4. Capability. $role = get_role( 'administrator' ); if ( $role ) { $role->add_cap( 'manage_brverify' ); $role->remove_cap( 'manage_restoreproof' ); } // 5. Old cron events. $cron = _get_cron_array(); if ( is_array( $cron ) ) { foreach ( $cron as $events ) { foreach ( is_array( $events ) ? array_keys( $events ) : array() as $hook ) { if ( 0 === strpos( (string) $hook, 'restoreproof_' ) ) { wp_clear_scheduled_hook( (string) $hook ); } } } } update_option( self::DONE_OPTION, '1', true ); wp_cache_flush(); return $report; } /** * Whether a directory has no entries. * * @param string $dir Directory. */ private static function dir_is_empty( string $dir ): bool { $items = @scandir( $dir ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged return is_array( $items ) && 2 === count( $items ); } }