Skip to content

Commit

Permalink
Update wizard text, improve validation of migration for templates/pages
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0ttkclark committed Nov 17, 2024
1 parent caa4ea7 commit c445492
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 39 deletions.
123 changes: 85 additions & 38 deletions components/Migrate-PHP/Migrate-PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,17 @@ public function init() {

/**
* Enqueue styles
*
* @since 2.0.0
*/
public function admin_assets() {
wp_enqueue_style( 'pods-wizard' );
}

/**
* Show the Admin
* Get the list of objects that need migration.
*
* @param $options
* @param $component
* @return array{pod_templates:Template[],pod_pages:Page[]} The list of objects that need migration.
*/
public function admin( $options, $component ) {
$method = 'migrate';

public function get_objects_that_need_migration(): array {
$pod_templates = [];
$pod_pages = [];

Expand Down Expand Up @@ -91,6 +86,32 @@ static function( $object ) {
);
}

// Rekey the objects by ID.
$pod_templates = array_combine( array_map( static function( $object ) {
return $object->get_id();
}, $pod_templates ), $pod_templates );

$pod_pages = array_combine( array_map( static function( $object ) {
return $object->get_id();
}, $pod_pages ), $pod_pages );

return compact( 'pod_templates', 'pod_pages' );
}

/**
* Show the Admin
*
* @param $options
* @param $component
*/
public function admin( $options, $component ) {
$method = 'migrate';

[
'pod_templates' => $pod_templates,
'pod_pages' => $pod_pages,
] = $this->get_objects_that_need_migration();

// ajax_migrate
pods_view( __DIR__ . '/ui/wizard.php', compact( array_keys( get_defined_vars() ) ) );
}
Expand All @@ -103,68 +124,93 @@ static function( $object ) {
public function ajax_migrate( $params ) {
WP_Filesystem();

[
'pod_templates' => $pod_templates_available_to_migrate,
'pod_pages' => $pod_pages_available_to_migrate,
] = $this->get_objects_that_need_migration();

$objects_can_be_migrated = ! empty( $pod_templates_available_to_migrate ) || ! empty( $pod_pages_available_to_migrate );

$cleanup = 1 === (int) pods_v( 'cleanup', $params, 0 );

$pod_templates = [];
$pod_pages = [];

if ( isset( $params->templates ) && ! empty( $params->templates ) ) {
foreach ( $params->templates as $object_id => $checked ) {
if ( true === (boolean) $checked ) {
$pod_templates[] = $object_id;
}
$pod_templates_selected = (array) pods_v( 'templates', $params, [] );
$pod_templates_selected = array_filter( $pod_templates_selected );

$pod_pages_selected = (array) pods_v( 'pages', $params, [] );
$pod_pages_selected = array_filter( $pod_pages_selected );

$has_objects_to_migrate = ! empty( $pod_templates_selected ) || ! empty( $pod_pages_selected );

foreach ( $pod_templates_selected as $object_id => $checked ) {
if ( true === (boolean) $checked && isset( $pod_templates_available_to_migrate[ (int) $object_id ] ) ) {
$pod_templates[] = $object_id;
}
}

if ( isset( $params->pages ) && ! empty( $params->pages ) ) {
foreach ( $params->pages as $object_id => $checked ) {
if ( true === (boolean) $checked ) {
$pod_pages[] = $object_id;
}
foreach ( $pod_pages_selected as $object_id => $checked ) {
if ( true === (boolean) $checked && isset( $pod_pages_available_to_migrate[ (int) $object_id ] ) ) {
$pod_pages[] = $object_id;
}
}

$pod_templates_file_paths = [];
$pod_pages_file_paths = [];

$migrated = false;

foreach ( $pod_templates as $object_id ) {
$migrated = true;

$pod_templates_file_paths[] = $this->migrate_template( $object_id, $cleanup );
}

foreach ( $pod_pages as $object_id ) {
$migrated = true;

$pod_pages_file_paths[] = $this->migrate_page( $object_id, $cleanup );
}

$content = '<div class="pods-wizard-content">' . "\n";

$content .= '<p>' . esc_html__( 'Migration Complete! The following paths were saved:', 'pods' ) . '</p>' . "\n";
if ( ! $has_objects_to_migrate ) {
$content .= '<p>' . esc_html__( 'No Pod Templates or Pod Pages were selected.', 'pods' ) . '</p>' . "\n";
} elseif ( ! $objects_can_be_migrated ) {
$content .= '<p>' . esc_html__( 'The selected Pod Templates or Pod Pages are not available for migration. They no longer contain PHP.', 'pods' ) . '</p>' . "\n";
} elseif ( ! $migrated ) {
$content .= '<p>' . esc_html__( 'The selected Pod Templates or Pod Pages were not successfully migrated.', 'pods' ) . '</p>' . "\n";
} else {
$content .= '<p>' . esc_html__( 'Migration Complete! The following paths were saved:', 'pods' ) . '</p>' . "\n";

if ( ! empty( $pod_templates_file_paths ) ) {
$content .= '<h4>' . esc_html__( 'Pod Templates saved', 'pods' ) . '</h4>' . "\n";
$content .= '<ul class="normal">' . "\n";

if ( ! empty( $pod_templates_file_paths ) ) {
$content .= '<h4>' . esc_html__( 'Pod Templates saved', 'pods' ) . '</h4>' . "\n";
$content .= '<ul class="normal">' . "\n";
foreach ( $pod_templates_file_paths as $file_path ) {
$content .= '<li>' . esc_html( $file_path ) . '</li>' . "\n";
}

foreach ( $pod_templates_file_paths as $file_path ) {
$content .= '<li>' . esc_html( $file_path ) . '</li>' . "\n";
$content .= '</ul>' . "\n";
}

$content .= '</ul>' . "\n";
}
if ( ! empty( $pod_pages_file_paths ) ) {
$content .= '<h4>' . esc_html__( 'Pod Pages saved', 'pods' ) . '</h4>' . "\n";
$content .= '<ul class="normal">' . "\n";

if ( ! empty( $pod_pages_file_paths ) ) {
$content .= '<h4>' . esc_html__( 'Pod Pages saved', 'pods' ) . '</h4>' . "\n";
$content .= '<ul class="normal">' . "\n";
foreach ( $pod_pages_file_paths as $file_path ) {
$content .= '<li>' . esc_html( $file_path ) . '</li>' . "\n";
}

foreach ( $pod_pages_file_paths as $file_path ) {
$content .= '<li>' . esc_html( $file_path ) . '</li>' . "\n";
$content .= '</ul>' . "\n";
}

$content .= '</ul>' . "\n";
}

if ( $cleanup ) {
$content .= '<p>' . esc_html__( 'The Pod Page(s) and/or Pod Template(s) were cleaned up and will now load directly from the theme files.', 'pods' ) . '</p>' . "\n";
} else {
$content .= '<p>' . esc_html__( 'The Pod Page(s) and/or Pod Template(s) were not modified. You will need to empty the content on them before they will load from the theme files.', 'pods' ) . '</p>' . "\n";
if ( $cleanup ) {
$content .= '<p>' . esc_html__( 'The Pod Page(s) and/or Pod Template(s) were cleaned up and will now load directly from the theme files.', 'pods' ) . '</p>' . "\n";
} else {
$content .= '<p>' . esc_html__( 'The Pod Page(s) and/or Pod Template(s) were not modified. You will need to empty the content on them before they will load from the theme files.', 'pods' ) . '</p>' . "\n";
}
}

return $content;
Expand Down Expand Up @@ -259,6 +305,7 @@ private function migrate_template( $object_id, bool $cleanup ) {
if ( $cleanup ) {
$api->save_template( [
'id' => $object->get_id(),
'name' => $object->get_label(),
'code' => '',
] );
}
Expand Down
3 changes: 2 additions & 1 deletion components/Migrate-PHP/ui/wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
</div>
<div id="pods-wizard-panel-2" class="pods-wizard-panel pods-wizard-option-content">
<div class="pods-wizard-content">
<p><?php esc_html_e( 'Choose below which Pod Page(s) and/or Pod Template(s) you want to migrate into theme files', 'pods' ); ?></p>
<p><?php esc_html_e( 'The objects listed have been determined to currently contain PHP that needs to be migrated. Choose below which Pod Page(s) and/or Pod Template(s) you want to migrate into theme files', 'pods' ); ?></p>
<p><?php esc_html_e( 'If you have chosen to clear the content in this migration, when you refresh this migration screen the migrated objects will no longer be shown.', 'pods' ); ?></p>

<p>
<a href="#toggle" class="button pods-wizard-toggle-all"
Expand Down

0 comments on commit c445492

Please sign in to comment.