post_meta = $post_meta; $this->term_meta = $term_meta; } public function preview( bool $overwrite ): ImportReport { $report = $this->run( true, $overwrite ); $report->dry_run = true; return $report; } public function import( bool $overwrite ): ImportReport { return $this->run( false, $overwrite ); } abstract protected function run( bool $dry, bool $overwrite ): ImportReport; /** * Imports one string field into post meta. Empty source values are * ignored entirely (not even counted); a non-empty existing target value * is only replaced when $overwrite is set. */ protected function import_post_field( ImportReport $report, bool $dry, bool $overwrite, int $post_id, string $target_key, string $value ): void { $value = trim( $value ); if ( '' === $value ) { return; } $existing = (string) $this->post_meta->get( $post_id, $target_key ); if ( '' !== $existing && ! $overwrite ) { ++$report->skipped_fields; return; } if ( ! $dry ) { $this->post_meta->update( $post_id, $target_key, $value ); } ++$report->imported_fields; $report->add_post_field( $post_id, $target_key, $value ); } /** * Imports one boolean flag (robots index/follow) into post meta. Unlike * string fields, "already set" means the meta row EXISTS — a stored * `false` is a deliberate choice that must not be overwritten silently. */ protected function import_post_flag( ImportReport $report, bool $dry, bool $overwrite, int $post_id, string $target_key, bool $value ): void { if ( $this->post_meta->has( $post_id, $target_key ) && ! $overwrite ) { ++$report->skipped_fields; return; } if ( ! $dry ) { $this->post_meta->update( $post_id, $target_key, $value ); } ++$report->imported_fields; $report->add_post_field( $post_id, $target_key, $value ? __( 'yes', 'acez-seo' ) : __( 'no', 'acez-seo' ) ); } protected function import_term_field( ImportReport $report, bool $dry, bool $overwrite, int $term_id, string $target_key, string $value ): void { $value = trim( $value ); if ( '' === $value ) { return; } $existing = (string) $this->term_meta->get( $term_id, $target_key ); if ( '' !== $existing && ! $overwrite ) { ++$report->skipped_fields; return; } if ( ! $dry ) { $this->term_meta->update( $term_id, $target_key, $value ); } ++$report->imported_fields; $report->add_term_field( $term_id, $target_key, $value ); } /** * Source plugins mostly store OG images as URLs; we store an attachment * ID. Returns 0 when the URL doesn't belong to this site's media library * (external images can't be mapped — skipped rather than guessed). */ protected function attachment_id_from_url( string $url ): int { if ( '' === trim( $url ) ) { return 0; } return (int) attachment_url_to_postid( $url ); } protected function import_post_og_image( ImportReport $report, bool $dry, bool $overwrite, int $post_id, int $attachment_id ): void { if ( $attachment_id <= 0 ) { return; } $existing = (int) $this->post_meta->get( $post_id, \AcezSeo\Data\MetaFields::OG_IMAGE_ID ); if ( $existing > 0 && ! $overwrite ) { ++$report->skipped_fields; return; } if ( ! $dry ) { $this->post_meta->update( $post_id, \AcezSeo\Data\MetaFields::OG_IMAGE_ID, $attachment_id ); } ++$report->imported_fields; $image_title = (string) get_the_title( $attachment_id ); $report->add_post_field( $post_id, \AcezSeo\Data\MetaFields::OG_IMAGE_ID, '' !== $image_title ? $image_title : sprintf( '#%d', $attachment_id ) ); } }