Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve status when emails are re-created #6902

Draft
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions includes/internal/emails/class-email-seeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Sensei\Internal\Emails\Email_Seeder_Data;
use Sensei\Internal\Emails\Email_Repository;
use WP_Query;

if ( ! defined( 'ABSPATH' ) ) {
exit;
Expand Down Expand Up @@ -93,6 +94,24 @@ public function create_email( string $identifier, bool $force = false ): bool {
return false;
}

// Preserve existing email status when re-creating.
$query = new WP_Query(
[
'post_type' => Email_Post_Type::POST_TYPE,
'post_status' => 'any',
'meta_key' => '_sensei_email_identifier', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => $identifier, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
]
);

if ( ! empty( $query->posts ) ) {
if ( 'publish' === $query->posts[0]->post_status ) {
$disabled = false;
} else {
$disabled = true;
}
}

$this->email_repository->delete( $identifier );
}

Expand All @@ -117,9 +136,11 @@ public function create_email( string $identifier, bool $force = false ): bool {
}

$description = $email_data['description'] ?? '';
$is_pro = $email_data['is_pro'] ?? false;

$is_pro = $email_data['is_pro'] ?? false;
$disabled = $email_data['disabled'] ?? false;
if ( ! isset( $disabled ) ) {
$disabled = $email_data['disabled'] ?? false;
}

$email_id = $this->email_repository->create( $identifier, $types, $subject, $description, $content, $is_pro, $disabled );

Expand Down
8 changes: 4 additions & 4 deletions includes/internal/emails/class-recreate-emails-tool.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function get_id() {
* @return string
*/
public function get_name() {
return __( 'Recreate Emails', 'sensei-lms' );
return __( 'Re-create Emails', 'sensei-lms' );
}

/**
Expand All @@ -88,7 +88,7 @@ public function get_name() {
*/
public function get_description() {
return __(
'Recreate all emails. Existing customizations will be lost.',
'Re-create all emails. Existing customizations will be lost.',
'sensei-lms'
);
}
Expand All @@ -101,8 +101,8 @@ public function process() {
$result = $this->seeder->create_all( true );

$message = $result
? __( 'Emails were recreated successfully.', 'sensei-lms' )
: __( 'There were errors while recreating emails.', 'sensei-lms' );
? __( 'Emails were re-created successfully.', 'sensei-lms' )
: __( 'There were errors while re-creating emails.', 'sensei-lms' );
$this->tools->add_user_message( $message, ! $result );
}

Expand Down