> post_id => target meta key => truncated source value */ public array $post_fields = array(); /** @var array> term_id => target meta key => truncated source value */ public array $term_fields = array(); public int $more_posts = 0; public int $more_terms = 0; public function add_note( string $note ): void { $this->notes[] = $note; } public function add_post_field( int $post_id, string $target_key, string $value = '' ): void { if ( ! isset( $this->post_fields[ $post_id ] ) && count( $this->post_fields ) >= self::MAX_ITEMS ) { ++$this->more_posts; return; } $this->post_fields[ $post_id ][ $target_key ] = self::truncate_value( $value ); } public function add_term_field( int $term_id, string $target_key, string $value = '' ): void { if ( ! isset( $this->term_fields[ $term_id ] ) && count( $this->term_fields ) >= self::MAX_ITEMS ) { ++$this->more_terms; return; } $this->term_fields[ $term_id ][ $target_key ] = self::truncate_value( $value ); } /** * Values are shown in the dry-run preview list; the report travels * through a transient, so long descriptions get clipped. */ private static function truncate_value( string $value ): string { $value = trim( $value ); return mb_strlen( $value ) > 100 ? mb_substr( $value, 0, 99 ) . '…' : $value; } /** * @return array */ public function to_array(): array { return array( 'posts' => $this->posts, 'terms' => $this->terms, 'imported_fields' => $this->imported_fields, 'skipped_fields' => $this->skipped_fields, 'dry_run' => $this->dry_run, 'notes' => $this->notes, 'post_fields' => $this->post_fields, 'term_fields' => $this->term_fields, 'more_posts' => $this->more_posts, 'more_terms' => $this->more_terms, ); } /** * @param array $data */ public static function from_array( array $data ): self { $report = new self(); $report->posts = (int) ( $data['posts'] ?? 0 ); $report->terms = (int) ( $data['terms'] ?? 0 ); $report->imported_fields = (int) ( $data['imported_fields'] ?? 0 ); $report->skipped_fields = (int) ( $data['skipped_fields'] ?? 0 ); $report->dry_run = (bool) ( $data['dry_run'] ?? false ); $report->notes = array_map( 'strval', (array) ( $data['notes'] ?? array() ) ); $report->more_posts = (int) ( $data['more_posts'] ?? 0 ); $report->more_terms = (int) ( $data['more_terms'] ?? 0 ); foreach ( (array) ( $data['post_fields'] ?? array() ) as $post_id => $fields ) { $report->post_fields[ (int) $post_id ] = array_map( 'strval', (array) $fields ); } foreach ( (array) ( $data['term_fields'] ?? array() ) as $term_id => $fields ) { $report->term_fields[ (int) $term_id ] = array_map( 'strval', (array) $fields ); } return $report; } public function to_summary(): string { $summary = sprintf( /* translators: 1: number of posts, 2: number of terms, 3: imported field count, 4: skipped field count */ __( 'Posts/pages: %1$d, taxonomy terms: %2$d, imported fields: %3$d, skipped fields: %4$d.', 'acez-seo' ), $this->posts, $this->terms, $this->imported_fields, $this->skipped_fields ); if ( $this->dry_run ) { $summary = __( '[Preview — nothing saved] ', 'acez-seo' ) . $summary; } if ( array() !== $this->notes ) { $summary .= ' ' . implode( ' ', $this->notes ); } return $summary; } }