Skip to content

Commit

Permalink
Handle cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0ttkclark committed Oct 1, 2024
1 parent 17972ea commit 977d854
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
32 changes: 28 additions & 4 deletions components/Migrate-PHP/Migrate-PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ static function( $object ) {
public function ajax_migrate( $params ) {
WP_Filesystem();

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

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

Expand All @@ -126,11 +128,11 @@ public function ajax_migrate( $params ) {
$pod_pages_file_paths = [];

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

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

$content = '<div class="pods-wizard-content">' . "\n";
Expand Down Expand Up @@ -159,6 +161,12 @@ public function ajax_migrate( $params ) {
$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";
}

return $content;
}

Expand Down Expand Up @@ -193,7 +201,7 @@ private function setup_file_path( $file_path ) {
}
}

private function migrate_template( $object_id ) {
private function migrate_template( $object_id, bool $cleanup ) {
/**
* @var $wp_filesystem WP_Filesystem_Base
*/
Expand Down Expand Up @@ -235,10 +243,17 @@ private function migrate_template( $object_id ) {
pods_error( sprintf( esc_html__( 'Unable to write to the file: %s', 'pods' ), $file_path ) );
}

if ( $cleanup ) {
$api->save_template( [
'id' => $object->get_id(),
'code' => '',
] );
}

return str_replace( ABSPATH, '', $file_path );
}

private function migrate_page( $object_id ) {
private function migrate_page( $object_id, bool $cleanup ) {
/**
* @var $wp_filesystem WP_Filesystem_Base
*/
Expand Down Expand Up @@ -298,6 +313,15 @@ private function migrate_page( $object_id ) {
pods_error( sprintf( esc_html__( 'Unable to write to the file: %s', 'pods' ), $file_path ) );
}

if ( $cleanup ) {
$api->save_page( [
'id' => $object->get_id(),
'name' => $object->get_label(),
'code' => '',
'precode' => '',
] );
}

return str_replace( ABSPATH, '', $file_path );
}

Expand Down
34 changes: 25 additions & 9 deletions components/Migrate-PHP/ui/wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<?php echo PodsForm::field( 'component', $component, 'hidden' ); ?>
<?php echo PodsForm::field( 'method', $method, 'hidden' ); ?>
<?php echo PodsForm::field( '_wpnonce', wp_create_nonce( 'pods-component-' . $component . '-' . $method ), 'hidden' ); ?>
<?php echo PodsForm::field( 'cleanup', 0, 'hidden', array( 'attributes' => array( 'id' => 'pods_cleanup' ) ) ); ?>

<h2 class="italicized"><?php esc_html_e( 'Migrate: Pod Page and Pod Template PHP into File-based templates', 'pods' ); ?></h2>

Expand Down Expand Up @@ -34,11 +35,22 @@
<div id="pods-wizard-options">
<div class="pods-wizard-options-list">
<div class="pods-wizard-option">
<a href="#pods-wizard" data-opt="0">
<a href="#pods-wizard-run" data-opt="0">
<div>
<h2><?php esc_html_e( 'Migrate PHP to files', 'pods' ); ?></h2>

<p><?php esc_html_e( 'This will migrate Pod Pages and Pod Templates that have PHP in them into theme files.', 'pods' ); ?></p>
<p><?php esc_html_e( 'This will migrate Pod Pages and Pod Templates that have PHP in them into files in your theme folder.', 'pods' ); ?></p>
</div>
<span>&#10095;</span>
</a>
</div>
<div class="pods-wizard-option">
<a href="#pods-wizard-run-clean" data-opt="1">
<div>
<h2><?php esc_html_e( 'Migrate PHP to files and clear the content in the DB', 'pods' ); ?></h2>

<p><?php esc_html_e( 'This will migrate Pod Pages and Pod Templates that have PHP in them into files in your theme folder, and then clear the content in the DB for those.', 'pods' ); ?></p>
<p><?php esc_html_e( 'Please be sure to backup your database before you run this tool.', 'pods' ); ?></p>
</div>
<span>&#10095;</span>
</a>
Expand Down Expand Up @@ -151,22 +163,22 @@
</div>
</div>
<?php endif; ?>
</div>

<span id="pods-wizard-result"></span>
<span id="pods-wizard-result"></span>

<div class="stuffbox hidden" id="pods-wizard-results">
<h3><?php esc_html_e( 'Migration Results', 'pods' ); ?></h3>
<div class="stuffbox hidden" id="pods-wizard-results">
<h3><?php esc_html_e( 'Migration Results', 'pods' ); ?></h3>

<div class="inside pods-manage-field pods-dependency">
<div class="pods-wizard-results"></div>
<div class="inside pods-manage-field pods-dependency">
<div class="pods-wizard-results"></div>
</div>
</div>
</div>

<div id="pods-wizard-actions" class="pods-wizard-button-interface">
<div id="pods-wizard-toolbar">
<button id="pods-wizard-start" class="button button-secondary"><?php esc_html_e( 'Start Over', 'pods' ); ?></button>
<button id="pods-wizard-next" class="button button-primary hidden" data-again="<?php esc_attr_e( 'Process Again', 'pods' ); ?>" data-next="<?php esc_attr_e( 'Continue', 'pods' ); ?>" data-finished="<?php esc_attr_e( 'Finished', 'pods' ); ?>" data-processing="<?php esc_attr_e( 'Processing', 'pods' ); ?>.."><?php esc_html_e( 'Continue', 'pods' ); ?></button>
<button id="pods-wizard-next" class="button button-primary" data-again="<?php esc_attr_e( 'Process Again', 'pods' ); ?>" data-next="<?php esc_attr_e( 'Continue', 'pods' ); ?>" data-finished="<?php esc_attr_e( 'Finished', 'pods' ); ?>" data-processing="<?php esc_attr_e( 'Processing', 'pods' ); ?>.."><?php esc_html_e( 'Continue', 'pods' ); ?></button>
</div>
<div id="pods-wizard-finished">

Expand All @@ -190,6 +202,10 @@
return true;
};

var pods_admin_option_select_callback = function ( $opt ) {
jQuery( '#pods_cleanup' ).val( $opt.data( 'opt' ) );
};

var pods_admin_submit_callback = function ( id ) {
jQuery( '#pods-wizard-next' ).css( 'cursor', 'pointer' );
jQuery( '#pods-wizard-next' ).prop( 'disabled', false );
Expand Down
Loading

0 comments on commit 977d854

Please sign in to comment.