, offset:int, total:int, remaining:int} */ public static function import( string $content, string $format, array $options ): array { global $wpdb; $dry_run = ! empty( $options['dry_run'] ); $overwrite = ! empty( $options['overwrite'] ); $table = Database::redirects(); $result = array( 'created' => 0, 'updated' => 0, 'skipped' => 0, 'errors' => array(), 'remaining' => 0 ); $rows = FileImport::rows( $content, 'json' === $format ? 'json' : 'csv' ); $total = count( $rows ); $offset = max( 0, (int) ( $options['offset'] ?? 0 ) ); $slice = array_slice( $rows, $offset, self::MAX_PER_REQUEST, true ); $processed = 0; foreach ( $slice as $index => $row ) { ++$processed; $number = $index + 2; // Header is row 1, as the spreadsheet counts. $raw = trim( (string) ( $row['source'] ?? $row['from'] ?? $row['url'] ?? '' ) ); $target = trim( (string) ( $row['target'] ?? $row['to'] ?? $row['destination'] ?? '' ) ); $type = (int) ( $row['type'] ?? $row['code'] ?? $row['status_code'] ?? 301 ); $type = in_array( $type, self::TYPES, true ) ? $type : 301; if ( '' === $raw ) { $result['errors'][] = array( 'row' => $number, 'source' => '', 'reason' => __( 'no source', 'ai-rank-suite' ) ); continue; } $source = RedirectsModule::normalize( $raw ); if ( '/' === $source ) { $result['errors'][] = array( 'row' => $number, 'source' => $raw, 'reason' => __( 'refusing to redirect the home page', 'ai-rank-suite' ) ); continue; } if ( '' === $target && 410 !== $type ) { $result['errors'][] = array( 'row' => $number, 'source' => $raw, 'reason' => __( 'no target', 'ai-rank-suite' ) ); continue; } if ( '' !== $target && self::same_place( $source, $target ) ) { $result['errors'][] = array( 'row' => $number, 'source' => $raw, 'reason' => __( 'target is the source — that is a redirect loop', 'ai-rank-suite' ) ); continue; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $existing = $wpdb->get_row( $wpdb->prepare( "SELECT id, target, type FROM {$table} WHERE source = %s", $source ), ARRAY_A ); if ( $existing && ! $overwrite ) { ++$result['skipped']; continue; } if ( $existing && (string) $existing['target'] === $target && (int) $existing['type'] === $type ) { ++$result['skipped']; continue; } if ( $dry_run ) { $existing ? ++$result['updated'] : ++$result['created']; continue; } $data = array( 'source' => mb_substr( $source, 0, 191 ), 'target' => $target, 'type' => $type, 'status' => 'active', ); if ( $existing ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->update( $table, $data, array( 'id' => (int) $existing['id'] ) ); ++$result['updated']; } else { $data['created_at'] = current_time( 'mysql', true ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->insert( $table, $data ); ++$result['created']; } } if ( ! $dry_run && ( $result['created'] > 0 || $result['updated'] > 0 ) ) { RedirectsModule::forget(); } // Where the next request should pick up, and how much is left. Zero // remaining is the only signal the screen may read as "done". $result['offset'] = $offset + $processed; $result['total'] = $total; $result['remaining'] = max( 0, $total - $result['offset'] ); return $result; } /** * Every rule as a CSV, hits included — the column that tells an agency * which of three thousand rules are still doing work. */ public static function export(): string { global $wpdb; $table = Database::redirects(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $rows = $wpdb->get_results( $wpdb->prepare( "SELECT source, target, type, status, hits, last_hit, created_at FROM %i ORDER BY id ASC", $table ), ARRAY_A ); $lines = array( 'source,target,type,status,hits,last_hit,created_at' ); foreach ( (array) $rows as $row ) { $lines[] = implode( ',', array_map( array( self::class, 'cell' ), array( (string) $row['source'], (string) $row['target'], (string) (int) $row['type'], (string) $row['status'], (string) (int) $row['hits'], (string) ( $row['last_hit'] ?? '' ), (string) $row['created_at'], ) ) ); } return implode( "\r\n", $lines ) . "\r\n"; } /** * One CSV cell. A value that begins with =, +, - or @ is a formula to * Excel and Google Sheets — a redirect target typed by an attacker would * run when the customer opens the export. */ private static function cell( string $value ): string { $value = (string) preg_replace( '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $value ); if ( preg_match( '/^[=+\-@\t\r]/', $value ) ) { $value = "'" . $value; } return '"' . str_replace( '"', '""', $value ) . '"'; } /** Does this target point back at the source path? */ private static function same_place( string $source, string $target ): bool { $target_path = (string) wp_parse_url( $target, PHP_URL_PATH ); if ( '' === $target_path ) { return false; } $host = (string) wp_parse_url( $target, PHP_URL_HOST ); if ( '' !== $host && strtolower( $host ) !== strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ) ) { return false; } return RedirectsModule::normalize( $target_path ) === $source; } }