Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails
WCEmailTemplateSelectiveApplier::undo
Restore the pre-apply snapshot for a post. Single-step undo only: the snapshot meta is consumed (deleted) on success, so a second undo without an intervening apply returns 410 Gone.
Method of the class: WCEmailTemplateSelectiveApplier{}
No Hooks.
Returns
Arrayrestored_content and status ('restored').
Usage
$result = WCEmailTemplateSelectiveApplier::undo( $post_id, $revision_id );
- $post_id(int) (required)
- The
woo_emailpost ID. - $revision_id(string) (required)
- The UUID returned by the prior
apply_selectively()call.
Changelog
| Since 10.9.0 | Introduced. |
WCEmailTemplateSelectiveApplier::undo() WCEmailTemplateSelectiveApplier::undo code WC 10.9.1
public static function undo( int $post_id, string $revision_id ) {
$post = get_post( $post_id );
if ( ! $post instanceof \WP_Post || Integration::EMAIL_POST_TYPE !== $post->post_type ) {
return new \WP_Error(
'post_not_found',
sprintf(
/* translators: %d: post ID */
__( 'No woo_email post found for ID %d.', 'woocommerce' ),
$post_id
),
array( 'status' => 404 )
);
}
$snapshot = get_post_meta( $post_id, self::SNAPSHOT_META_KEY, true );
if ( ! is_array( $snapshot ) || ! isset( $snapshot['revision_id'], $snapshot['content'] ) ) {
return new \WP_Error(
'undo_unavailable',
__( 'No pre-apply snapshot is available for this post.', 'woocommerce' ),
array( 'status' => 410 )
);
}
if ( (string) $snapshot['revision_id'] !== $revision_id ) {
return new \WP_Error(
'undo_unavailable',
__( 'The supplied revision ID does not match the latest snapshot for this post.', 'woocommerce' ),
array( 'status' => 410 )
);
}
$restored_content = (string) $snapshot['content'];
self::$is_applying = true;
try {
$updated = wp_update_post(
array(
'ID' => $post_id,
'post_content' => $restored_content,
),
true
);
if ( is_wp_error( $updated ) ) {
return $updated;
}
// Restore every meta apply stamped. `restore_meta_from_snapshot`
// no-ops on keys missing from older snapshot formats.
self::restore_meta_from_snapshot(
$post_id,
$snapshot,
'last_core_render',
WCEmailTemplateDivergenceDetector::LAST_CORE_RENDER_META_KEY
);
self::restore_meta_from_snapshot(
$post_id,
$snapshot,
'version',
WCEmailTemplateDivergenceDetector::VERSION_META_KEY
);
self::restore_meta_from_snapshot(
$post_id,
$snapshot,
'source_hash',
WCEmailTemplateDivergenceDetector::SOURCE_HASH_META_KEY
);
self::restore_meta_from_snapshot(
$post_id,
$snapshot,
'last_synced_at',
WCEmailTemplateDivergenceDetector::LAST_SYNCED_AT_META_KEY
);
// The snapshot's prior_status was correct at snapshot time, but
// the world may have moved since (core released, canonical
// changed). Ask the classifier for the truth against current
// state instead of stamping a stale value.
WCEmailTemplateDivergenceDetector::reclassify( $post_id );
delete_post_meta( $post_id, self::SNAPSHOT_META_KEY );
} finally {
self::$is_applying = false;
}//end try
WCEmailTemplateChangeSummary::reset_cache();
return array(
'restored_content' => $restored_content,
'status' => 'restored',
);
}