Skip to content

Commit

Permalink
Merge branch 'trunk' into epic/ras-acc
Browse files Browse the repository at this point in the history
  • Loading branch information
laurelfulford committed Oct 23, 2024
2 parents 4181410 + 57d83fc commit 61d9089
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ public function get_send_lists( $args = [], $to_array = false ) {
'provider' => $this->service,
'type' => 'sublist',
'id' => $segment['id'],
'parent' => $args['parent'] ?? null,
'parent_id' => $args['parent_id'] ?? null,
'name' => $segment_name,
'entity_type' => 'segment',
'count' => $segment['subscriber_count'] ?? null,
Expand Down Expand Up @@ -790,19 +790,24 @@ public function retrieve( $post_id, $skip_sync = false ) {
$campaign_id = get_post_meta( $post_id, 'ac_campaign_id', true );
$send_list_id = get_post_meta( $post_id, 'send_list_id', true );
$send_sublist_id = get_post_meta( $post_id, 'send_sublist_id', true );
$newsletter_data = [
'campaign' => true, // Satisfy the JS API.
'campaign_id' => $campaign_id,
'supports_multiple_test_recipients' => true,
];

// Handle legacy send-to meta.
if ( ! $send_list_id ) {
$legacy_list_id = get_post_meta( $post_id, 'ac_list_id', true );
if ( $legacy_list_id ) {
$newsletter_data['list_id'] = $legacy_list_id;
$newsletter_data['send_list_id'] = $legacy_list_id;
$send_list_id = $legacy_list_id;
}
}
if ( ! $send_sublist_id ) {
$legacy_sublist_id = get_post_meta( $post_id, 'ac_segment_id', true );
if ( $legacy_sublist_id ) {
$newsletter_data['sublist_id'] = $legacy_sublist_id;
$newsletter_data['send_sublist_id'] = $legacy_sublist_id;
$send_sublist_id = $legacy_sublist_id;
}
}
Expand All @@ -816,6 +821,7 @@ public function retrieve( $post_id, $skip_sync = false ) {
if ( is_wp_error( $send_lists ) ) {
throw new Exception( wp_kses_post( $send_lists->get_error_message() ) );
}
$newsletter_data['lists'] = $send_lists;
$send_sublists = $send_list_id || $send_sublist_id ?
$this->get_send_lists(
[
Expand All @@ -829,13 +835,7 @@ public function retrieve( $post_id, $skip_sync = false ) {
if ( is_wp_error( $send_sublists ) ) {
throw new Exception( wp_kses_post( $send_sublists->get_error_message() ) );
}
$newsletter_data = [
'campaign' => true, // Satisfy the JS API.
'campaign_id' => $campaign_id,
'supports_multiple_test_recipients' => true,
'lists' => $send_lists,
'sublists' => $send_sublists,
];
$newsletter_data['sublists'] = $send_sublists;

if ( $campaign_id ) {
$newsletter_data['link'] = sprintf(
Expand Down Expand Up @@ -1066,7 +1066,7 @@ public function sync( $post ) {
// Retrieve and store campaign data.
$data = $this->retrieve( $post->ID, true );
if ( is_wp_error( $data ) ) {
set_transient( $transient_name, __( 'Error syncing with ESP. ', 'newspack-newsletters' ) . $data->get_error_message(), 45 );
set_transient( $transient_name, __( 'ActiveCampaign sync error: ', 'newspack-newsletters' ) . $data->get_error_message(), 45 );
return $data;
} else {
$data = array_merge( $data, $sync_data );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ public function get_segments() {
$segments = array_map(
function ( $item ) {
return [
'id' => $item->SegmentID, // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
'name' => $item->Title, // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
'parent' => $item->ListID, // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
'id' => $item->SegmentID, // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
'name' => $item->Title, // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
'parent_id' => $item->ListID, // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
];
},
$segments->response
Expand Down Expand Up @@ -400,7 +400,7 @@ public function retrieve( $post_id ) {
if ( ! $send_list_id ) {
$legacy_list_id = get_post_meta( $post_id, 'cm_list_id', true ) ?? get_post_meta( $post_id, 'cm_segment_id', true );
if ( $legacy_list_id ) {
$newsletter_data['list_id'] = $legacy_list_id;
$newsletter_data['send_list_id'] = $legacy_list_id;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function __construct() {
add_action( 'rest_api_init', [ $this->controller, 'register_routes' ] );
}
add_action( 'pre_post_update', [ $this, 'pre_post_update' ], 10, 2 );
add_action( 'save_post', [ $this, 'save_post' ], 10, 2 );
add_action( 'transition_post_status', [ $this, 'transition_post_status' ], 10, 3 );
add_action( 'updated_post_meta', [ $this, 'updated_post_meta' ], 10, 4 );
add_action( 'wp_insert_post', [ $this, 'insert_post' ], 10, 3 );
Expand Down Expand Up @@ -144,6 +145,22 @@ public function pre_post_update( $post_id, $data ) {
}
}

/**
* Delete layout defaults meta after saving the post.
* We don't want layout defaults overwriting saved values unless the layout has just been set.
*
* @param int $post_id The ID of the post being saved.
* @return void
*/
public function save_post( $post_id ) {
$post_type = get_post_type( $post_id );
if ( Newspack_Newsletters::NEWSPACK_NEWSLETTERS_CPT !== $post_type ) {
return;
}

delete_post_meta( $post_id, 'stringifiedCampaignDefaults' );
}

/**
* Handle post status transition for scheduled newsletters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ public function sync( $post ) {

return $campaign_result;
} catch ( Exception $e ) {
set_transient( $transient_name, __( 'Error syncing with ESP. ', 'newspack-newsletters' ) . $e->getMessage(), 45 );
set_transient( $transient_name, 'Constant Contact campaign sync error: ' . wp_specialchars_decode( $e->getMessage(), ENT_QUOTES ), 45 );
return new WP_Error( 'newspack_newsletters_constant_contact_error', $e->getMessage() );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private static function get_cache_date_key( $list_id = 'lists' ) {
* @param string $list_id The List ID.
* @return boolean
*/
private static function is_cache_expired( $list_id = null ) {
private static function is_cache_expired( $list_id = 'lists' ) {
$cache_date = get_option( self::get_cache_date_key( $list_id ) );
return $cache_date && ( time() - $cache_date ) > 20 * MINUTE_IN_SECONDS;
}
Expand Down Expand Up @@ -525,7 +525,7 @@ private static function refresh_cached_data( $list_id ) {
*/
public static function handle_cron() {
Newspack_Newsletters_Logger::log( 'Mailchimp cache: Handling cron request to refresh cache' );
$lists = self::get_lists();
$lists = self::fetch_lists(); // Force a cache refresh.

foreach ( $lists as $list ) {
Newspack_Newsletters_Logger::log( 'Mailchimp cache: Dispatching request to refresh cache for list ' . $list['id'] );
Expand Down Expand Up @@ -586,7 +586,7 @@ public static function fetch_segment( $segment_id, $list_id ) {
}
$response = ( self::get_mc_instance() )->validate(
$mc->get(
"lists/$list_id/segment/$segment_id",
"lists/$list_id/segments/$segment_id",
[
'fields' => 'id,name,member_count,type,options,list_id',
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Newspack\Newsletters\Send_Lists;
use Newspack\Newsletters\Send_List;

use function cli\err;

/**
* Main Newspack Newsletters Class.
*/
Expand Down Expand Up @@ -560,11 +562,11 @@ public function retrieve( $post_id ) {
$newsletter_data['senderEmail'] = $campaign_info['senderEmail']; // If campaign has different sender info set, update ours.
}
if ( $list_id && $list_id !== $send_list_id ) {
$newsletter_data['list_id'] = $list_id; // If campaign has a different list selected, update ours.
$send_list_id = $list_id;
$newsletter_data['send_list_id'] = $list_id; // If campaign has a different list selected, update ours.
$send_list_id = $list_id;

if ( ! empty( $campaign_info['sublist_id'] ) && $campaign_info['sublist_id'] !== $send_sublist_id ) {
$newsletter_data['sublist_id'] = $campaign_info['sublist_id']; // If campaign has a different sublist selected, update ours.
$newsletter_data['send_sublist_id'] = $campaign_info['sublist_id']; // If campaign has a different sublist selected, update ours.
$send_sublist_id = $campaign_info['sublist_id'];
}
}
Expand Down Expand Up @@ -725,7 +727,7 @@ public function get_send_lists( $args = [], $to_array = false ) {
'id' => $interest['id'],
'name' => $interest['name'],
'entity_type' => $entity_type,
'parent' => $interest['list_id'],
'parent_id' => $interest['list_id'],
'count' => $interest['subscriber_count'],
];
if ( $admin_url && $audience['web_id'] ) {
Expand All @@ -749,7 +751,7 @@ public function get_send_lists( $args = [], $to_array = false ) {
'id' => $tag['id'],
'name' => $tag['name'],
'entity_type' => $entity_type,
'parent' => $tag['list_id'],
'parent_id' => $tag['list_id'],
'count' => $tag['member_count'],
];
if ( $admin_url && $audience['web_id'] ) {
Expand All @@ -770,7 +772,7 @@ public function get_send_lists( $args = [], $to_array = false ) {
'id' => $segment['id'],
'name' => $segment['name'],
'entity_type' => $entity_type,
'parent' => $segment['list_id'],
'parent_id' => $segment['list_id'],
'count' => $segment['member_count'],
];
if ( $admin_url && $audience['web_id'] ) {
Expand Down Expand Up @@ -1044,12 +1046,12 @@ public function get_sync_payload( $post ) {
$sublist = $this->get_send_lists(
[
'ids' => [ $send_sublist_id ],
'limit' => 1,
'limit' => 1000,
'parent_id' => $send_list_id,
'type' => 'sublist',
]
);
if ( ! empty( $sublist[0]->get_entity_type() ) ) {
if ( ! empty( $sublist ) && ! empty( $sublist[0]->get_entity_type() ) ) {
$sublist_type = $sublist[0]->get_entity_type();
switch ( $sublist_type ) {
case 'group':
Expand Down Expand Up @@ -1091,6 +1093,8 @@ public function get_sync_payload( $post ) {
break;
}
}
} else {
$payload['recipients']['segment_opts'] = (object) [];
}
}

Expand Down Expand Up @@ -1146,13 +1150,11 @@ public function sync( $post ) {

if ( $mc_campaign_id ) {
$campaign_result = $this->validate(
$mc->patch( "campaigns/$mc_campaign_id", $payload ),
__( 'Error updating existing campaign draft.', 'newspack_newsletters' )
$mc->patch( "campaigns/$mc_campaign_id", $payload )
);
} else {
$campaign_result = $this->validate(
$mc->post( 'campaigns', $payload ),
__( 'Error creating campaign.', 'newspack_newsletters' )
$mc->post( 'campaigns', $payload )
);
$mc_campaign_id = $campaign_result['id'];
update_post_meta( $post->ID, 'mc_campaign_id', $mc_campaign_id );
Expand All @@ -1177,7 +1179,7 @@ public function sync( $post ) {
'content_result' => $content_result,
];
} catch ( Exception $e ) {
set_transient( $transient_name, 'Mailchimp: ' . $e->getMessage(), 45 );
set_transient( $transient_name, 'Mailchimp campaign sync error: ' . wp_specialchars_decode( $e->getMessage(), ENT_QUOTES ), 45 );
return new WP_Error( 'newspack_newsletters_mailchimp_error', $e->getMessage() );
}
}
Expand Down
2 changes: 1 addition & 1 deletion includes/tracking/class-click.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static function process_link( $url, $original_url, $post ) {
* @return void
*/
public static function track_click( $newsletter_id, $email_address, $url ) {
if ( ! $newsletter_id || ! $email_address ) {
if ( ! $newsletter_id || ! $email_address || ! Admin::is_tracking_click_enabled() ) {
return;
}

Expand Down
5 changes: 5 additions & 0 deletions includes/tracking/class-pixel.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ public static function get_pixel_url( $post_id ) {
* @return void
*/
public static function track_seen( $newsletter_id, $tracking_id, $email_address ) {

if ( ! Admin::is_tracking_pixel_enabled() ) {
return;
}

$newsletter_tracking_id = \get_post_meta( $newsletter_id, 'tracking_id', true );

// Bail if tracking ID mismatch.
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
"devDependencies": {
"@rushstack/eslint-patch": "^1.10.4",
"@wordpress/browserslist-config": "^6.9.0",
"@wordpress/browserslist-config": "^6.10.0",
"eslint": "^8.57.0",
"lint-staged": "^15.2.10",
"newspack-scripts": "^5.5.2",
Expand Down
31 changes: 18 additions & 13 deletions src/editor/mjml/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { refreshEmailHtml } from '../../newsletter-editor/utils';
/**
* Internal dependencies
*/
import { fetchNewsletterData, fetchSyncErrors } from '../../newsletter-editor/store';
import { fetchNewsletterData, fetchSyncErrors, updateNewsletterDataError } from '../../newsletter-editor/store';
import { getServiceProvider } from '../../service-providers';

/**
Expand Down Expand Up @@ -83,6 +83,10 @@ function MJML() {

// After the post is successfully saved, refresh the email HTML.
const wasSaving = usePrevProp( isSaving );
const { name: serviceProviderName } = getServiceProvider();
const { supported_esps: supportedESPs } = newspack_email_editor_data || [];
const isSupportedESP = serviceProviderName && 'manual' !== serviceProviderName && supportedESPs?.includes( serviceProviderName );

useEffect( () => {
if (
wasSaving &&
Expand All @@ -103,24 +107,25 @@ function MJML() {
method: 'POST',
path: `/wp/v2/${ postType }/${ postId }`,
} );
} ).then( () => {
// Rehydrate ESP newsletter data after completing sync.
if ( isSupportedESP ) {
return fetchNewsletterData( postId );
}
return true;
} ).then ( () => {
// Check for sync errors after refreshing the HTML.
if ( isSupportedESP ) {
return fetchSyncErrors( postId );
}
return true;
} )
.catch( e => {
console.warn( e ); // eslint-disable-line no-console
updateNewsletterDataError( e );
} )
.finally( () => {
unlockPostSaving( 'newspack-newsletters-refresh-html' );
setIsRefreshingHTML( false );

const { name: serviceProviderName } = getServiceProvider();
const { supported_esps: supportedESPs } = newspack_email_editor_data || [];
const isSupportedESP = serviceProviderName && 'manual' !== serviceProviderName && supportedESPs?.includes( serviceProviderName );
if ( isSupportedESP ) {
// Rehydrate ESP newsletter data after completing sync.
fetchNewsletterData( postId );

// Check for sync errors after refreshing the HTML.
fetchSyncErrors( postId );
}
} );
}
}, [ isSaving, isAutosaving ] );
Expand Down
4 changes: 2 additions & 2 deletions src/newsletter-editor/sidebar/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const Autocomplete = ( {
{ selectedInfo?.hasOwnProperty( 'count' )
? ' • ' +
sprintf(
// Translators: If available, show a contact count alongside the selected item's type. %d is the number of contacts in the item.
_n( '%d contact', '%d contacts', selectedInfo.count, 'newspack-newsletters' ),
// Translators: If available, show a contact count alongside the selected item's type. %s is the number of contacts in the item.
_n( '%s contact', '%s contacts', selectedInfo.count, 'newspack-newsletters' ),
selectedInfo.count.toLocaleString()
)
: '' }
Expand Down
Loading

0 comments on commit 61d9089

Please sign in to comment.