*/ private $pairs = array(); /** @var SerializedReplacer|null */ private $replacer = null; public static function fromJson( string $json ): self { $decoded = '' === $json ? array() : json_decode( $json, true ); return self::fromArray( is_array( $decoded ) ? $decoded : array() ); } /** * @param array $raw */ public static function fromArray( array $raw ): self { $options = new self(); $options->exclude_revisions = ! empty( $raw['exclude_revisions'] ); $options->exclude_spam = ! empty( $raw['exclude_spam'] ); $options->exclude_inactive_themes = ! empty( $raw['exclude_inactive_themes'] ); $options->exclude_media = ! empty( $raw['exclude_media'] ); // `replace` is the name jobs saved before `find_replace` used; they must still resume. foreach ( (array) ( $raw['find_replace'] ?? $raw['replace'] ?? array() ) as $row ) { if ( count( $options->pairs ) >= self::MAX_PAIRS || ! is_array( $row ) ) { continue; } $find = isset( $row['find'] ) && is_string( $row['find'] ) ? $row['find'] : ''; $replace = isset( $row['replace'] ) && is_string( $row['replace'] ) ? $row['replace'] : ''; // An empty "find" matches everywhere and a no-op pair does nothing; both are dropped. if ( '' === $find || $find === $replace || strlen( $find ) > self::MAX_LENGTH || strlen( $replace ) > self::MAX_LENGTH || false !== strpos( $find . $replace, "\0" ) ) { continue; } $options->pairs[] = array( 'find' => $find, 'replace' => $replace, ); } return $options; } /** * @return array */ public function toArray(): array { return array( 'exclude_revisions' => $this->exclude_revisions, 'exclude_spam' => $this->exclude_spam, 'exclude_inactive_themes' => $this->exclude_inactive_themes, 'exclude_media' => $this->exclude_media, 'find_replace' => $this->pairs, ); } public function excludeMedia(): bool { return $this->exclude_media; } public function excludeInactiveThemes(): bool { return $this->exclude_inactive_themes; } private function replacer(): SerializedReplacer { if ( null === $this->replacer ) { $map = array(); foreach ( $this->pairs as $pair ) { $map[ $pair['find'] ] = $pair['replace']; } $this->replacer = new SerializedReplacer( $map ); } return $this->replacer; } /** * SQL condition rows of $table must satisfy to be exported, or '' for all rows. * Written for the unaliased table so it can be appended to a plain SELECT. */ public function rowFilter( string $table ): string { global $wpdb; $conditions = array(); if ( $this->exclude_revisions ) { if ( $table === $wpdb->posts ) { $conditions[] = "`post_type` <> 'revision'"; } elseif ( $table === $wpdb->postmeta ) { $conditions[] = 'NOT EXISTS (SELECT 1 FROM ' . Sql::ident( $wpdb->posts ) . ' p WHERE p.`ID` = ' . Sql::ident( $table ) . ".`post_id` AND p.`post_type` = 'revision')"; } } if ( $this->exclude_spam ) { if ( $table === $wpdb->comments ) { $conditions[] = "`comment_approved` <> 'spam'"; } elseif ( $table === $wpdb->commentmeta ) { $conditions[] = 'NOT EXISTS (SELECT 1 FROM ' . Sql::ident( $wpdb->comments ) . ' c WHERE c.`comment_ID` = ' . Sql::ident( $table ) . ".`comment_id` AND c.`comment_approved` = 'spam')"; } } return implode( ' AND ', $conditions ); } /** * Applies the find/replace pairs to one database value, or returns it unchanged. * Only text columns are touched, serialized data stays valid, and a post's GUID * (its permanent identifier, not a link) is left alone. */ public function transform( string $table, string $column, string $type, string $value ): string { global $wpdb; if ( ! $this->hasReplacements() || ! Sql::isTextType( $type ) ) { return $value; } if ( 'guid' === $column && $table === $wpdb->posts ) { return $value; } return $this->replacer()->replace( $value ); } public function hasReplacements(): bool { return (bool) $this->pairs; } public function hasRowFilters(): bool { return $this->exclude_revisions || $this->exclude_spam; } }