Skip to content

Commit

Permalink
Transporter: Added variable transformers+cleanup. See sybrew/the-seo-…
Browse files Browse the repository at this point in the history
  • Loading branch information
sybrew committed Jul 10, 2022
1 parent c442993 commit afa19ca
Show file tree
Hide file tree
Showing 11 changed files with 1,395 additions and 70 deletions.
65 changes: 64 additions & 1 deletion extensions/free/transport/trunk/inc/classes/admin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ private function construct() {
'twitter_title',
'twitter_description',
'og_image',
'article_type',
// 'article_type', TODO
'primary_term',
],
'transform' => [ /* "Transformed fields cannot be recovered without a backup" */
'title',
Expand All @@ -148,6 +149,7 @@ private function construct() {
'og_description',
'twitter_title',
'twitter_description',
'canonical_url',
],
],
'termmeta' => [
Expand Down Expand Up @@ -178,6 +180,67 @@ private function construct() {
],
],
],
'Temp_The_SEO_Framework' => [ // For moving data back/forth
'title' => 'Temp The SEO Framework',
'importers' => [
'settings' => false, // Let's keep this at false, for now. Perhaps we want to move the homepage stuff, but that's tricky.
'postmeta' => [
'supports' => [
'title',
'description',
'canonical_url',
'redirect',
'noindex',
'nofollow',
'noarchive',
'og_title',
'og_description',
'twitter_title',
'twitter_description',
'og_image',
// 'article_type', TODO
'primary_term',
],
// 'transform' => [ /* "Transformed fields cannot be recovered without a backup" */
// 'title',
// 'description',
// 'noindex',
// 'nofollow',
// 'noarchive',
// 'og_title',
// 'og_description',
// 'twitter_title',
// 'twitter_description',
// ],
],
// 'termmeta' => [
// 'supports' => [
// 'title',
// 'description',
// 'canonical_url',
// 'noindex',
// 'nofollow',
// 'noarchive',
// 'og_title',
// 'og_description',
// 'twitter_title',
// 'twitter_description',
// 'og_image',
// ],
// 'transform' => [ /* "Transformed fields cannot be recovered without a backup" */
// 'title',
// 'description',
// 'noindex',
// 'nofollow',
// 'noarchive',
// 'og_title',
// 'og_description',
// 'twitter_title',
// 'twitter_description',
// ],
// ],
],
],
// TODO
// 'SEO_By_Rank_Math' => [
// 'title' => 'Rank Math SEO',
Expand Down
15 changes: 8 additions & 7 deletions extensions/free/transport/trunk/inc/classes/handler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ public function _import( $supported_importers ) {
// Convert 90 posts per second, 5400 per minute, 27_000 per 5. Some have 100_000+... welp, they can automatically retry.
$timeout = 5 * MINUTE_IN_SECONDS;

// var_dump()
$this->release_transport_lock();
// var_dump() debug
// $this->release_transport_lock();

if ( ! $this->lock_transport( $timeout ) )
$this->_halt_server( [
Expand Down Expand Up @@ -182,7 +182,7 @@ public function _import( $supported_importers ) {
\esc_html(
sprintf(
/* translators: 1 = post number, 2 = totalposts, 3 = post ID */
\__( 'Handling post %1$d of %2$d. (ID: %3$d)', 'the-seo-framework-extension-manager' ),
\__( 'Processing post %1$d of %2$d. (ID: %3$d)', 'the-seo-framework-extension-manager' ),
$post_iterator,
$total_posts,
$post_id
Expand Down Expand Up @@ -218,7 +218,7 @@ public function _import( $supported_importers ) {
\esc_html__( 'Data imported succesfully.', 'the-seo-framework-extension-manager' )
);
}
$succeeded++;
$succeeded += (int) $results['updated'];
}

resultsDelete:;
Expand All @@ -233,7 +233,7 @@ public function _import( $supported_importers ) {
$store->store(
\esc_html__( 'Deleted useless data successfully.', 'the-seo-framework-extension-manager' )
);
$deleted++;
$deleted += (int) $results['deleted'];
}
}

Expand Down Expand Up @@ -304,7 +304,7 @@ public function _import( $supported_importers ) {
\esc_html__( 'Data imported succesfully.', 'the-seo-framework-extension-manager' )
);
}
$succeeded++;
$succeeded += (int) $results['updated'];
}
if ( $actions['delete'] ) {
// In case anyone asks: "useless" data is '' or null.
Expand All @@ -316,7 +316,7 @@ public function _import( $supported_importers ) {
$store->store(
\esc_html__( 'Deleted useless data successfully.', 'the-seo-framework-extension-manager' )
);
$deleted++;
$deleted += (int) $results['deleted'];
}
}
break;
Expand Down Expand Up @@ -385,6 +385,7 @@ public function _import( $supported_importers ) {
}
break;
case 'debug':
// phpcs:ignore, WordPress.PHP.DevelopmentFunctions -- Exactly.
$store->store( \esc_html( print_r( $data, true ) ) );
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ final public function import() {

foreach ( $this->conversion_sets as $conversion_set ) :
prepare: {
[ $transfer_from, $transfer_to, $transformer, $sanitizer, $transmuter ] = array_pad( $conversion_set, 4, null );
[ $transfer_from, $transfer_to, $transformer, $sanitizer, $transmuter ] = array_pad( $conversion_set, 5, null );

[ $from_table, $from_index ] = array_pad( $transfer_from ?? [], 2, null );
[ $to_table, $to_index ] = array_pad( $transfer_to ?? [], 2, null );
Expand All @@ -128,8 +128,8 @@ final public function import() {

getlist: {
// Sanity is a virtue.
$from_table = \esc_sql( $from_table ) ?: $_globals_postmeta;
$to_table = \esc_sql( $to_table ) ?: $_globals_postmeta;
$from_table = \esc_sql( $from_table ) ?: ( $from_index ? $_globals_postmeta : null );
$to_table = \esc_sql( $to_table ) ?: ( $to_index ? $_globals_postmeta : null );

if ( $is_deletion_only ) {
yield 'nowDeleting' => [ [ $from_table, $from_index ] ];
Expand Down Expand Up @@ -198,10 +198,12 @@ final public function import() {
'transform' => $has_transformer,
// If the data goes nowhere there's no need to delete nor transport.
'transport' => ! $identical_index,
'delete' => ! $identical_index,
// With no old handle, we must delete manually. TODO fixme?
'delete' => $from_index && ! $identical_index,
'sanitize' => $has_sanitizer,
'sanitized' => false,
];
$cleanup = [];

$existing_value = null; // Value in place by new plugin.
$transport_value = null; // Value from old plugin.
Expand Down Expand Up @@ -234,6 +236,11 @@ final public function import() {
}
}

// If existing new data exists, don't overwrite with old.
// Updating is still tried if $results['transformed'] is true.
if ( isset( $existing_value ) )
$actions['transport'] = false;

if ( $has_transmuter_from ) {
$transport_value = \call_user_func_array(
$transmuter['from'][1],
Expand All @@ -247,6 +254,7 @@ final public function import() {
],
&$actions,
&$results,
&$cleanup,
]
);
} else {
Expand All @@ -259,9 +267,7 @@ final public function import() {
) );
if ( WP_DEBUG && $wpdb->last_error ) throw new \Exception( $wpdb->last_error );
} else {
// If new data exists, don't overwrite with old.
$actions['transport'] = false;
// If new data exists and index is the same, still try to transform. Otherwise, forgo.
// If new data exists and index is the same, still try to transform (update). Otherwise, forgo.
$actions['transform'] = $actions['transform'] && $identical_index;
}
}
Expand All @@ -276,8 +282,7 @@ final public function import() {
[
$set_value,
$post_id,
[ $from_table, $from_index ],
[ $to_table, $to_index ],
'post',
]
);

Expand Down Expand Up @@ -313,17 +318,37 @@ final public function import() {
],
&$actions,
&$results,
$cleanup,
]
);
} else {
if ( 1984 == $post_id )
yield 'debug' =>
[

[
'transport_value' => $transport_value,
'post_id' => $post_id,
'from_data' => $transmuter['from_data'] ?? null,
'from' => [ $from_table, $from_index ],
'existing_value' => $existing_value,
'has_transmuter_to' => $has_transmuter_to,
'set_value' => $set_value,
],
$actions,
$results,
$cleanup,
];

// $actions and $results are passed by reference.
$this->transmute(
$set_value,
$post_id,
[ $from_table, $from_index ],
[ $to_table, $to_index ],
$actions,
$results
$results,
$cleanup
);
}

Expand Down Expand Up @@ -353,9 +378,10 @@ final public function import() {
* @param ?string[] $transfer_to The table+index to transfer to.
* @param array $actions The actions for and after transmuation, passed by reference.
* @param array $results The results before and after transmuation, passed by reference.
* @param ?array $cleanup The extraneous database indexes to clean up.
* @throws \Exception On database error when WP_DEBUG is enabled.
*/
protected function transmute( $set_value, $post_id, $transfer_from, $transfer_to, &$actions, &$results ) {
protected function transmute( $set_value, $post_id, $transfer_from, $transfer_to, &$actions, &$results, &$cleanup = null ) {
global $wpdb;

[ $from_table, $from_index ] = $transfer_from;
Expand All @@ -364,7 +390,7 @@ protected function transmute( $set_value, $post_id, $transfer_from, $transfer_to
if ( ! $to_index ) goto delete;

if ( $actions['transport'] ) {
if ( $to_table === $from_table ) {
if ( $from_index && $to_table === $from_table ) {
if ( $results['transformed'] ) {
$results['updated'] = $wpdb->update(
$to_table,
Expand Down Expand Up @@ -418,14 +444,25 @@ protected function transmute( $set_value, $post_id, $transfer_from, $transfer_to
}

delete: if ( $actions['delete'] ) {
$results['deleted'] = $wpdb->delete(
$results['deleted'] += $from_index ? $wpdb->delete(
$from_table,
[
'post_id' => $post_id,
'meta_key' => $from_index,
]
);
) : false;
if ( WP_DEBUG && $wpdb->last_error ) throw new \Exception( $wpdb->last_error );
}

// This is also "deleting", but then willfully.
cleanup: foreach ( (array) $cleanup as [ $_from_table, $_from_index ] ) {
$results['deleted'] += $wpdb->delete(
$_from_table,
[
'post_id' => $post_id,
'meta_key' => $_from_index,
]
);
}
}
}
Loading

0 comments on commit afa19ca

Please sign in to comment.