ui = $ui; $this->importers = $importers; } public function slug(): string { return 'tools'; } public function label(): string { return __( 'Tools', 'acez-seo' ); } public function icon(): string { return 'tools'; } public function enqueue(): void { } public function handles_form_key( string $raw_tab ): bool { return 'tools_import' === sanitize_key( $raw_tab ); } /** @return array */ public function save( string $raw_tab ): array { // Defence in depth: SettingsPage::handle_save() verifies this nonce // before dispatch; re-checking keeps every entry point independently safe. check_admin_referer( FormControls::NONCE_ACTION ); $source = isset( $_POST['import_source'] ) ? sanitize_key( wp_unslash( $_POST['import_source'] ) ) : ''; $importer = $this->importers->get( $source ); if ( null !== $importer && $importer->is_available() ) { $overwrite = isset( $_POST['import_overwrite'] ); $dry_run = isset( $_POST['import_preview'] ); $report = $dry_run ? $importer->preview( $overwrite ) : $importer->import( $overwrite ); set_transient( 'acez_seo_import_report_' . get_current_user_id(), $report->to_array(), 60 ); } return array( 'tab' => 'tools' ); } public function render(): void { echo '

' . esc_html__( 'Administrative tools for the plugin.', 'acez-seo' ) . '

'; $report = $this->pull_import_report(); if ( null !== $report ) { $this->render_import_report_notice( $report ); } $this->ui->card_open( __( 'Migrate SEO Data', 'acez-seo' ), 'tools', '', 'tools-migrate' ); echo '

' . esc_html__( 'Imports SEO data from another plugin (per-post titles, descriptions, canonical, robots, OG image, plus archive titles/descriptions where the source stores them). Source data is only read — never modified or deleted, so you can always go back and compare.', 'acez-seo' ) . wp_kses( HelpIcon::render( 'migrate_seo' ), KsesTags::help_icon() ) . '

'; $available = $this->importers->available(); if ( array() === $available ) { echo '
' . esc_html__( 'No data from any supported SEO plugin (Slim SEO, Yoast SEO, All in One SEO) was detected in the database.', 'acez-seo' ) . '
'; $this->ui->card_close(); return; } $this->ui->form_open( 'tools_import' ); echo ''; echo ''; echo ''; echo '
' . esc_html__( 'Data source', 'acez-seo' ) . ''; $first = true; foreach ( $available as $importer ) { printf( '', esc_attr( $importer->get_id() ), checked( $first, true, false ), esc_html( $importer->get_label() ) ); $first = false; } echo '
' . esc_html__( 'Overwriting', 'acez-seo' ) . ''; printf( '', esc_html__( 'Overwrite values already set in Acez SEO AI (by default only empty fields are filled in)', 'acez-seo' ) ); echo '
'; echo '

'; submit_button( __( 'Preview (saves nothing)', 'acez-seo' ), 'secondary', 'import_preview', false ); echo ' '; submit_button( __( 'Import', 'acez-seo' ), 'primary', 'import_run', false ); echo '

'; $this->ui->form_close(); $this->ui->card_close(); if ( null !== $report && $report->dry_run ) { $this->render_import_report_details( $report ); } } private function pull_import_report(): ?ImportReport { $transient_key = 'acez_seo_import_report_' . get_current_user_id(); $stored = get_transient( $transient_key ); if ( ! is_array( $stored ) ) { return null; } delete_transient( $transient_key ); return ImportReport::from_array( $stored ); } private function render_import_report_notice( ImportReport $report ): void { printf( '

%2$s %3$s

', $report->dry_run ? 'notice-info' : 'notice-success', esc_html( $report->dry_run ? __( 'Import preview:', 'acez-seo' ) : __( 'Import finished.', 'acez-seo' ) ), esc_html( $report->to_summary() ) ); } /** * Dry-run detail list rendered BELOW the Migrate SEO Data card: every * post/term the import would touch, with the fields that would be filled. */ private function render_import_report_details( ImportReport $report ): void { if ( array() === $report->post_fields && array() === $report->term_fields ) { return; } $field_labels = array( MetaFields::TITLE => __( 'SEO title', 'acez-seo' ), MetaFields::DESCRIPTION => __( 'Meta description', 'acez-seo' ), MetaFields::CANONICAL => __( 'Canonical', 'acez-seo' ), MetaFields::ROBOTS_INDEX => __( 'Robots: index', 'acez-seo' ), MetaFields::ROBOTS_FOLLOW => __( 'Robots: follow', 'acez-seo' ), MetaFields::OG_IMAGE_ID => __( 'OG image', 'acez-seo' ), TermMetaFields::TITLE => __( 'SEO title', 'acez-seo' ), TermMetaFields::DESCRIPTION => __( 'Meta description', 'acez-seo' ), ); $this->ui->card_open( __( 'Items to be imported (preview)', 'acez-seo' ), 'tools', '', 'tools-import-preview' ); echo '

' . esc_html__( 'The items that will be filled in after clicking "Import" — along with the fields and values that will actually be imported under the current overwrite setting. The link opens the edit screen, where you can compare the current state with what is coming.', 'acez-seo' ) . '

'; if ( array() !== $report->post_fields ) { echo '

' . esc_html__( 'Posts and pages', 'acez-seo' ) . '

'; echo ''; if ( $report->more_posts > 0 ) { printf( '

%s

', esc_html( sprintf( /* translators: %d: number of additional posts not listed. */ __( '…and %d more posts/pages (the list is truncated).', 'acez-seo' ), $report->more_posts ) ) ); } } if ( array() !== $report->term_fields ) { echo '

' . esc_html__( 'Taxonomy terms', 'acez-seo' ) . '

'; echo ''; if ( $report->more_terms > 0 ) { printf( '

%s

', esc_html( sprintf( /* translators: %d: number of additional terms not listed. */ __( '…and %d more terms (the list is truncated).', 'acez-seo' ), $report->more_terms ) ) ); } } $this->ui->card_close(); } /** * Formats "field: „incoming value”" segments for the dry-run detail list. * * @param array $fields target key => truncated source value * @param array $field_labels */ private function field_list_labels( array $fields, array $field_labels ): string { $parts = array(); foreach ( $fields as $key => $value ) { $label = $field_labels[ $key ] ?? (string) $key; $parts[] = '' !== $value ? sprintf( '%1$s: „%2$s”', $label, $value ) : $label; } return implode( ' · ', $parts ); } }