register_importers();
}
/**
* Discovers and registers all available importer classes.
* This makes the system extensible for third-party importers.
*
* @return void
*/
private function register_importers(): void {
$core_importers = [
NativeJSONImporter::class,
AdvancedAdsImporter::class,
];
// Allow third-party plugins to add their own importer classes.
$all_importer_classes = apply_filters( 'adpresso_importer_classes', $core_importers );
foreach ( $all_importer_classes as $importer_class ) {
if ( class_exists( $importer_class ) && is_subclass_of( $importer_class, AbstractImporter::class ) ) {
$importer = new $importer_class();
$this->importers[ $importer->get_id() ] = $importer;
} else {
RuntimeDebugger::add_doing_it_wrong_entry(
sprintf( 'Importer %s was incorrectly implemented.', esc_html( $importer_class ) )
);
}
}
}
/**
* Renders the export UI for the Tools page.
* This method is called by the `Tools` screen class.
*
* @return void
*/
public function render_export_ui(): void {
$export_options = [
'ads' => __( 'Ads', 'adpresso' ),
'groups' => __( 'Groups', 'adpresso' ),
'placements' => __( 'Placements', 'adpresso' ),
'settings' => __( 'Plugin Settings', 'adpresso' ),
];
?>
scan();
?>
' . esc_html__( 'Import History', 'adpresso' ) . '';
$history = ImportRollback::get_import_history();
if ( empty( $history ) ) {
echo '' . esc_html__( 'No imports have been performed yet.', 'adpresso' ) . '
';
return;
}
?>
|
|
|
|
| imported_at ); ?> |
summary_message ); ?> |
importer_id ); ?> |
user_id )->display_name ?? 'N/A' ); ?> |
status === 'active' ) : ?>
session_key ),
// 'adpresso_rollback_' . $log_entry->session_key
// );
// ?>
getKey( 'adpresso_action' );
if ( $action === '' ) {
return;
}
if ( ! current_user_can( adpresso_get_capability( 'use_tools' ) ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to perform this action.', 'adpresso' ) );
}
if ( $action === 'export_data' ) {
if ( ! isset( $_POST['adpresso_export_nonce_field'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['adpresso_export_nonce_field'] ) ), 'adpresso_export_nonce' ) ) {
wp_die( 'Security check failed.' );
}
$content_to_export = [];
if ( isset( $_POST['export_content'] ) && is_array( $_POST['export_content'] ) ) {
$selected_items = array_filter( wp_unslash( $_POST['export_content'] ) );
$content_to_export = array_map( 'sanitize_key', array_keys( $selected_items ) );
}
if ( empty( $content_to_export ) ) {
add_action( 'admin_notices', function() {
echo '' . esc_html__( 'Please select at least one content type to export.', 'adpresso' ) . '
';
} );
return;
}
$exporter = new JSONExporter();
$exporter->set_content_to_export( $content_to_export );
$exporter->download_export_file();
}
if ( 'convert_a2_blocks' === $action ) {
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['adpresso_convert_a2_blocks_nonce'] ?? '' ) ), 'adpresso_convert_a2_blocks' ) ) {
wp_die( 'Security check failed.' );
}
$result = ( new A2BlockConverter() )->convert();
$message = sprintf(
/* translators: 1: number of converted blocks, 2: number of posts/templates, 3: number of block widgets. */
__( 'Converted %1$d Advanced Ads blocks in %2$d posts or templates and %3$d widgets.', 'adpresso' ),
$result['converted'],
$result['posts'],
$result['widgets']
);
if ( $result['remaining'] > 0 ) {
/* translators: %d: number of posts still to convert. */
$message .= ' ' . sprintf( __( '%d more posts are left, run the conversion again.', 'adpresso' ), $result['remaining'] );
}
set_transient(
'adpresso_import_messages',
[
[
'type' => 'success',
'message' => $message,
],
],
60
);
// Shown once on the tools page (transient above), the last result is kept in the Notification Center.
NotificationManager::get()->register(
'a2-block-conversion',
$message,
[
'type' => 'success',
'location' => 'dashboard',
]
);
wp_safe_redirect( admin_url( 'admin.php?page=adpresso-tools&tab=migration' ) );
exit;
}
if ( $action === 'import_data' ) {
if ( ! isset( $_POST['adpresso_import_nonce_field'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['adpresso_import_nonce_field'] ) ), 'adpresso_import_nonce' ) ) {
wp_die( 'Security check failed.' );
}
$importer_id = Params::post()->getKey( 'adpresso_importer_id' );
$importer = $this->importers[ $importer_id ] ?? null;
if ( $importer ) {
$messages = $importer->import();
set_transient( 'adpresso_import_messages', $messages, 60 );
// Shown once on the tools page (transient above). The last result per message is kept in the
// Notification Center; a fixed slug replaces it instead of adding one notice per import.
foreach ( $messages as $index => $message ) {
$slug = ! empty( $message['slug'] ) ? $message['slug'] : 'import-notice-' . $index;
NotificationManager::get()->register(
$importer->get_id() . '-' . $slug,
$message['message'],
[
'type' => $message['type'] ?? 'info',
'location' => 'dashboard',
]
);
}
} else {
set_transient( 'adpresso_import_messages', [ [ 'type' => 'error', 'message' => __( 'Selected importer not found or inactive.', 'adpresso' ) ] ], 60 );
}
// Redirect back to the importer's own tab to prevent form re-submission.
$tab = $importer && $importer->is_migration() ? '&tab=migration' : '';
wp_safe_redirect( admin_url( 'admin.php?page=adpresso-tools' . $tab ) );
exit;
}
}
/**
* Outputs a small inline script to clear the React sessionStorage cache if an import was just performed.
*
* @return void
*/
public function clear_frontend_cache_on_success(): void {
// Check if we come from the import page and if the import was successful.
$messages = get_transient( 'adpresso_import_messages' );
if ( ! empty( $messages ) ) {
?>