Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
picocodes committed Jan 22, 2024
1 parent e5f2d9b commit 83124f3
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 84 deletions.
3 changes: 3 additions & 0 deletions build/Emails/Admin/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,9 @@ public static function get_post( $edited_campaign ) {
// Store subtype in a separate meta key.
$args['meta_input'][ $edited_campaign->type . '_type' ] = $edited_campaign->get_sub_type();

// Convert campaign data to an object. See: https://core.trac.wordpress.org/ticket/60314
$args['meta_input']['campaign_data'] = (object) $args['meta_input']['campaign_data'];

$args['meta_input'] = array_filter( $args['meta_input'] );

$post_id = wp_insert_post( $args, false, false );
Expand Down
8 changes: 8 additions & 0 deletions build/Emails/Admin/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,14 @@ public static function prepare_edited_campaign( $query_args ) {
$campaign = &self::$edited_campaigns[ $cache_key ];

if ( $campaign->exists() ) {

// Maybe convert options to object.
$old = get_post_meta( $campaign->id, 'campaign_data', true );

if ( is_array( $old ) ) {
update_post_meta( $campaign->id, 'campaign_data', (object) $old );
}

return $campaign;
}

Expand Down
10 changes: 1 addition & 9 deletions build/Emails/Admin/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,6 @@ public function column_title( $item ) {
esc_html( $sub_types[ $item->get_sub_type() ]['description'] )
);

if ( $item_name !== $sub_types[ $item->get_sub_type() ]['label'] ) {
$title .= sprintf(
'<div><span class="noptin-strong">%s</span>: <span>%s</span></div>',
esc_html__( 'Type', 'newsletter-optin-box' ),
esc_html( $sub_types[ $item->get_sub_type() ]['label'] )
);
}

// Custom description.
$description = wp_kses_post( apply_filters( 'noptin_' . $item->type . '_table_about_' . $item->get_sub_type(), '', $item, $this ) );

Expand Down Expand Up @@ -250,7 +242,7 @@ public function column_title( $item ) {
if ( ! $item->sends_immediately() ) {

$title .= sprintf(
'<div class="noptin-strong noptin-text-success"><span>%s</span>: <span>%s</span></div>',
'<div><span class="noptin-strong">%s</span>: <span>%s</span></div>',
esc_html__( 'Delay', 'newsletter-optin-box' ),
esc_html( $item->get_sends_after() . ' ' . $item->get_sends_after_unit( true ) )
);
Expand Down
90 changes: 85 additions & 5 deletions build/Emails/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,18 @@ private function init( $id ) {
}

// Fetch campaign data.
$data = get_post_meta( $post->ID, 'campaign_data', true );
$resave = false;
$data = get_post_meta( $post->ID, 'campaign_data', true );
$resave = false;
$is_revision = wp_is_post_revision( $post->ID );

// If this is a revision and no data is found, try to fetch the parent data.
if ( $is_revision && empty( $data ) ) {
$parent = wp_get_post_parent_id( $post->ID );

if ( $parent ) {
$data = get_post_meta( $parent, 'campaign_data', true );
}
}

// If data is stdClass, convert it to an array.
if ( is_object( $data ) ) {
Expand Down Expand Up @@ -171,17 +181,59 @@ private function init( $id ) {
}

// Subject.
$resave_title = false;
if ( ! isset( $this->options['subject'] ) ) {

if ( ! empty( $this->options['custom_title'] ) ) {
$this->name = $this->options['custom_title'];
$resave_title = true;

unset( $this->options['custom_title'] );
}

$this->options['subject'] = $post->post_title;
$resave = true;
}

if ( $is_revision ) {
$resave = false;
$resave_title = false;
}

if ( $resave && 'auto-draft' !== $post->post_status ) {
update_post_meta( $post->ID, 'campaign_data', $this->options );
// https://core.trac.wordpress.org/ticket/60314.
update_post_meta( $post->ID, 'campaign_data', (object) $this->options );
}

$this->subject = $this->options['subject'];

// Check if content contains blocks.
if ( $resave && ! has_blocks( $this->content ) ) {
$this->content = noptin_email_wrap_blocks(
empty( $this->content ) ? '' : sprintf(
'<!-- wp:html -->%s<!-- /wp:html -->',
wpautop( $this->content )
),
$this->get( 'footer_text' ),
$this->get( 'heading' )
);

wp_update_post(
array(
'ID' => $post->ID,
'post_title' => $this->name,
'post_content' => $this->content,
)
);
} elseif ( $resave_title ) {
wp_update_post(
array(
'ID' => $post->ID,
'post_title' => $this->name,
)
);
}

// Add sub-type to options array.
$key = $this->type . '_type';
$this->options[ $key ] = get_post_meta( $post->ID, $key, true );
Expand Down Expand Up @@ -230,6 +282,29 @@ private function init_args( $args ) {
$this->options = array_merge( $this->options, $args );
}

/**
* Loads autosaved data for the email.
*
* @param \WP_Post $post
*/
public function load_autosave( $post ) {

// Fetch campaign data.
$data = get_post_meta( $post->ID, 'campaign_data', true );

// If data is stdClass, convert it to an array.
if ( is_object( $data ) ) {
$data = (array) $data;
}

if ( is_array( $data ) ) {
$this->options = array_merge( $this->options, $data );
}

$this->name = $post->post_title;
$this->content = $post->post_content;
}

/**
* Fetches an emaail.
*
Expand Down Expand Up @@ -481,11 +556,15 @@ public function get_subject() {
*/
public function get_content( $email_type = 'normal' ) {

if ( 'visual' === $email_type ) {
return $this->content;
}

if ( isset( $this->options[ 'content_' . $email_type ] ) ) {
return $this->options[ 'content_' . $email_type ];
}

return $this->content;
return '';
}

/**
Expand Down Expand Up @@ -815,7 +894,8 @@ public function save() {
'post_content' => $this->content,
'meta_input' => array(
'campaign_type' => $this->type,
'campaign_data' => array_merge(
// https://core.trac.wordpress.org/ticket/60314.
'campaign_data' => (object) array_merge(
$this->options,
array(
'subject' => $this->subject,
Expand Down
26 changes: 15 additions & 11 deletions build/Emails/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,11 @@ public static function register_post_types() {
'noptin-campaign',
'campaign_type',
array(
'single' => true,
'type' => 'string',
'default' => 'newsletter',
'show_in_rest' => true,
'revisions_enabled' => true,
'auth_callback' => function ( $allowed, $meta_key, $post_id ) {
'single' => true,
'type' => 'string',
'default' => 'newsletter',
'show_in_rest' => true,
'auth_callback' => function ( $allowed, $meta_key, $post_id ) {
return current_user_can( 'edit_post', $post_id );
},
)
Expand Down Expand Up @@ -331,15 +330,19 @@ public static function register_post_types() {
);

foreach ( self::$types as $type ) {

if ( ! $type->supports_timing ) {
continue;
}

register_post_meta(
'noptin-campaign',
$type->type . '_type',
array(
'single' => true,
'type' => 'string',
'show_in_rest' => true,
'revisions_enabled' => true,
'auth_callback' => function ( $allowed, $meta_key, $post_id ) {
'single' => true,
'type' => 'string',
'show_in_rest' => true,
'auth_callback' => function ( $allowed, $meta_key, $post_id ) {
return current_user_can( 'edit_post', $post_id );
},
)
Expand Down Expand Up @@ -383,6 +386,7 @@ private static function register_email_types() {
'new_campaign_label' => __( 'New Automated Email', 'newsletter-optin-box' ),
'click_to_add_first' => __( 'Click the button below to set-up your first automated email', 'newsletter-optin-box' ),
'supports_timing' => true,
'supports_sub_types' => true,
)
);
}
Expand Down
12 changes: 12 additions & 0 deletions build/Emails/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ public static function admin_preview( $template ) {
'uid' => get_current_user_id(),
);

// Check if we have an autosave.
$autosave = wp_get_post_autosave( get_the_ID() );

if ( ! empty( $autosave ) && is_preview() ) {
self::$campaign->load_autosave( $autosave );
}

// Maybe set plain text mode.
if ( 'plain_text' === self::$campaign->get_email_type() && ! headers_sent() ) {
header( 'Content-Type: text/plain' );
}

// Render the preview.
self::render();
}
Expand Down
9 changes: 9 additions & 0 deletions build/Emails/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class Type {
*/
public $supports_timing = false;

/**
* @var string Checks if this email supports sub_types.
*/
public $supports_sub_types = false;

/**
* @var string The email type label.
*/
Expand Down Expand Up @@ -96,6 +101,10 @@ public function __construct( $args ) {
*/
public function get_sub_types() {

if ( ! $this->supports_sub_types ) {
return array();
}

if ( null === $this->sub_types ) {
$this->sub_types = get_noptin_campaign_sub_types( $this->type );
}
Expand Down
2 changes: 1 addition & 1 deletion build/Emails/assets/css/style-view-campaigns.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/Emails/assets/js/email-editor.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('lodash', 'react', 'wp-api-fetch', 'wp-autop', 'wp-blob', 'wp-block-editor', 'wp-blocks', 'wp-commands', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-date', 'wp-deprecated', 'wp-dom', 'wp-editor', 'wp-element', 'wp-format-library', 'wp-hooks', 'wp-html-entities', 'wp-i18n', 'wp-keyboard-shortcuts', 'wp-keycodes', 'wp-media-utils', 'wp-notices', 'wp-plugins', 'wp-preferences', 'wp-primitives', 'wp-rich-text', 'wp-url'), 'version' => '2593dff14077598df5c4');
<?php return array('dependencies' => array('lodash', 'react', 'wp-api-fetch', 'wp-autop', 'wp-blob', 'wp-block-editor', 'wp-blocks', 'wp-commands', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-date', 'wp-deprecated', 'wp-dom', 'wp-editor', 'wp-element', 'wp-format-library', 'wp-hooks', 'wp-html-entities', 'wp-i18n', 'wp-keyboard-shortcuts', 'wp-keycodes', 'wp-media-utils', 'wp-notices', 'wp-plugins', 'wp-preferences', 'wp-primitives', 'wp-rich-text', 'wp-url'), 'version' => 'db8e81dd79fcd393efac');
Loading

0 comments on commit 83124f3

Please sign in to comment.