Skip to content

Commit 0c92026

Browse files
committed
Merge branch 'develop' into enhancement/add-composer-autoload
2 parents 6a238a2 + 8d10e68 commit 0c92026

File tree

7 files changed

+130
-113
lines changed

7 files changed

+130
-113
lines changed

.github/workflows/wordpress-plugin-deploy.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ jobs:
88
tag:
99
name: New release
1010
runs-on: ubuntu-latest
11+
if: ${{ !github.event.release.prerelease }} # Skip job if it is a pre-release
1112

1213
steps:
1314
- name: Checkout code
@@ -44,12 +45,11 @@ jobs:
4445
SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }}
4546
SLUG: mailchimp
4647

47-
- name: Upload release asset
48-
uses: actions/upload-release-asset@v1.0.2
48+
- name: Attach the wordpress.org plugin files to the Github release
49+
uses: softprops/action-gh-release@v2
50+
with:
51+
files: ${{ steps.deploy.outputs.zip-path }}
52+
body: |
53+
This release contains the latest updates for the WordPress plugin.
4954
env:
5055
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
51-
with:
52-
upload_url: ${{ github.event.release.upload_url }}
53-
asset_path: ${{ steps.deploy.outputs.zip-path }}
54-
asset_name: mailchimp.zip
55-
asset_content_type: application/zip

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ tests/cypress/reports
1414
tests/cypress/downloads
1515

1616
mailchimp.zip
17+
18+
# IDE
19+
.vscode

includes/blocks/mailchimp/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ import Icon from './icon';
66

77
registerBlockType(metadata, {
88
icon: Icon,
9+
transforms: {
10+
from: [
11+
{
12+
type: 'shortcode',
13+
tag: 'mailchimpsf_form',
14+
attributes: {
15+
// No attributes, but attributes property is required
16+
},
17+
},
18+
],
19+
},
920
edit: BlockEdit,
1021
save: () => null,
1122
});

mailchimp.php

Lines changed: 92 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ function () {
105105
*/
106106
function mailchimp_sf_plugin_init() {
107107

108-
// Remove Sopresto check. If user does not have API key, make them authenticate.
109-
110108
if ( get_option( 'mc_list_id' ) && get_option( 'mc_merge_field_migrate' ) !== '1' && mailchimp_sf_get_api() !== false ) {
111109
mailchimp_sf_update_merge_fields();
112110
}
@@ -247,7 +245,7 @@ function mailchimp_sf_request_handler() {
247245
}
248246

249247
// erase auth information
250-
$options = array( 'mc_api_key', 'mailchimp_sf_access_token', 'mc_datacenter', 'mailchimp_sf_auth_error', 'mailchimp_sf_waiting_for_login', 'mc_sopresto_user', 'mc_sopresto_public_key', 'mc_sopresto_secret_key' );
248+
$options = array( 'mc_api_key', 'mailchimp_sf_access_token', 'mc_datacenter', 'mailchimp_sf_auth_error', 'mailchimp_sf_waiting_for_login' );
251249
mailchimp_sf_delete_options( $options );
252250
break;
253251
case 'change_form_settings':
@@ -291,58 +289,6 @@ function mailchimp_sf_request_handler() {
291289
}
292290
add_action( 'init', 'mailchimp_sf_request_handler' );
293291

294-
/**
295-
* Migrate Sopresto
296-
*
297-
* @return void
298-
*/
299-
function mailchimp_sf_migrate_sopresto() {
300-
$sopresto = get_option( 'mc_sopresto_secret_key' );
301-
if ( ! $sopresto ) {
302-
return;
303-
}
304-
305-
// Talk to Sopresto, make exchange, delete old sopresto things.
306-
$body = array(
307-
'public_key' => get_option( 'mc_sopresto_public_key' ),
308-
'hash' => sha1( get_option( 'mc_sopresto_public_key' ) . get_option( 'mc_sopresto_secret_key' ) ),
309-
);
310-
311-
$url = 'https://sopresto.socialize-this.com/mailchimp/exchange';
312-
$args = array(
313-
'method' => 'POST',
314-
'timeout' => 500,
315-
'redirection' => 5,
316-
'httpversion' => '1.0',
317-
'user-agent' => 'Mailchimp WordPress Plugin/' . get_bloginfo( 'url' ),
318-
'body' => $body,
319-
);
320-
321-
// post to sopresto
322-
$key = wp_remote_post( $url, $args );
323-
if ( ! is_wp_error( $key ) && 200 === $key['response']['code'] ) {
324-
$key = json_decode( $key['body'] );
325-
try {
326-
$api = new MailChimp_API( $key->response );
327-
} catch ( Exception $e ) {
328-
$msg = '<strong class="mc_error_msg">' . $e->getMessage() . '</strong>';
329-
mailchimp_sf_global_msg( $msg );
330-
return;
331-
}
332-
333-
$verify = mailchimp_sf_verify_key( $api );
334-
335-
// something went wrong with the key that we had
336-
if ( is_wp_error( $verify ) ) {
337-
return;
338-
}
339-
340-
delete_option( 'mc_sopresto_public_key' );
341-
delete_option( 'mc_sopresto_secret_key' );
342-
delete_option( 'mc_sopresto_user' );
343-
}
344-
}
345-
346292
/**
347293
* Update merge fields
348294
*
@@ -421,13 +367,13 @@ function mailchimp_sf_needs_upgrade() {
421367

422368
/**
423369
* Deletes all Mailchimp options
370+
*
371+
* TODO: The options names should be moved to a config file
372+
* or to a class dedicated to options
424373
**/
425374
function mailchimp_sf_delete_setup() {
426375
$options = array(
427376
'mc_user_id',
428-
'mc_sopresto_user',
429-
'mc_sopresto_public_key',
430-
'mc_sopresto_secret_key',
431377
'mc_use_javascript',
432378
'mc_use_datepicker',
433379
'mc_use_unsub_link',
@@ -934,14 +880,18 @@ function mailchimp_sf_signup_submit() {
934880
$url = 'lists/' . $list_id . '/members/' . md5( strtolower( $email ) );
935881
$status = mailchimp_sf_check_status( $url );
936882

937-
// If update existing is turned off and the subscriber exists, error out.
938-
if ( get_option( 'mc_update_existing' ) === false && 'subscribed' === $status ) {
939-
$msg = esc_html__( 'This email address is already subscribed to the list.', 'mailchimp' );
883+
// If update existing is turned off and the subscriber is not new, error out.
884+
$is_new_subscriber = false === $status;
885+
if ( ! get_option( 'mc_update_existing' ) && ! $is_new_subscriber ) {
886+
$msg = esc_html__( 'This email address has already been subscribed to this list.', 'mailchimp' );
940887
$error = new WP_Error( 'mailchimp-update-existing', $msg );
941888
mailchimp_sf_global_msg( '<strong class="mc_error_msg">' . $msg . '</strong>' );
942889
return false;
943890
}
944891

892+
// TODO: If get_option( 'mc_update_existing' ) && 'unsubscribed' === $status then
893+
// make an API request to fetch Mailchimp hosted sign up form and display to user
894+
945895
$body = mailchimp_sf_subscribe_body( $merge, $igs, $email_type, $email, $status, get_option( 'mc_double_optin' ) );
946896
$retval = $api->post( $url, $body, 'PUT' );
947897

@@ -970,40 +920,42 @@ function mailchimp_sf_signup_submit() {
970920
* Cleans up merge fields and interests to make them
971921
* API 3.0-friendly.
972922
*
973-
* @param [type] $merge Merge fields
974-
* @param [type] $igs Interest groups
975-
* @param string $email_type Email type
976-
* @param string $email Email
977-
* @param string $status Status
978-
* @param bool $double_optin Whether this is double optin
923+
* @param [type] $merge Merge fields
924+
* @param [type] $igs Interest groups
925+
* @param string $email_type Email type
926+
* @param string $email Email
927+
* @param string|false $status Status The subscription status ('subscribed', 'unsubscribed', 'pending', etc.) or false if an error occurred.
928+
* @param string $double_optin Whether double opt-in is enabled. "1" for enabled and "" for disabled.
979929
* @return stdClass
980930
*/
981931
function mailchimp_sf_subscribe_body( $merge, $igs, $email_type, $email, $status, $double_optin ) {
982932
$body = new stdClass();
983933
$body->email_address = $email;
984934
$body->email_type = $email_type;
985935
$body->merge_fields = $merge;
936+
986937
if ( ! empty( $igs ) ) {
987938
$body->interests = $igs;
988939
}
989940

990-
if ( 'subscribed' !== $status ) {
991-
// single opt-in that covers new subscribers
992-
if ( false === ! $status && $double_optin ) {
993-
$body->status = 'subscribed';
994-
} else {
995-
// anyone else
996-
$body->status = 'pending';
997-
}
941+
// Early return for already subscribed users
942+
if ( 'subscribed' === $status ) {
943+
return $body;
998944
}
945+
946+
// Subscribe the email immediately unless double opt-in is enabled
947+
// "unsubscribed" and "subscribed" existing emails have been excluded at this stage
948+
// "pending" emails should follow double opt-in rules
949+
$body->status = $double_optin ? 'pending' : 'subscribed';
950+
999951
return $body;
1000952
}
1001953

1002954
/**
1003-
* Check status.
955+
* Check the status of a subscriber in the list.
1004956
*
1005-
* @param string $endpoint Endpoint.
1006-
* @return string
957+
* @param string $endpoint API endpoint to check the status.
958+
* @return string|false The subscription status ('subscribed', 'unsubscribed', 'pending', etc.) or false if the API returned 404 or an error occurred.
1007959
*/
1008960
function mailchimp_sf_check_status( $endpoint ) {
1009961
$endpoint .= '?fields=status';
@@ -1033,32 +985,72 @@ function mailchimp_sf_merge_submit( $mv ) {
1033985

1034986
$opt_val = isset( $_POST[ $opt ] ) ? map_deep( stripslashes_deep( $_POST[ $opt ] ), 'sanitize_text_field' ) : '';
1035987

1036-
// Handle phone number logic
1037-
if ( isset( $mv_var['options']['phone_format'] ) && 'phone' === $mv_var['type'] && 'US' === $mv_var['options']['phone_format'] ) {
1038-
$opt_val = mailchimp_sf_merge_validate_phone( $opt_val, $mv_var );
1039-
if ( is_wp_error( $opt_val ) ) {
1040-
return $opt_val;
1041-
}
1042-
} elseif ( is_array( $opt_val ) && 'address' === $mv_var['type'] ) { // Handle address logic
1043-
$validate = mailchimp_sf_merge_validate_address( $opt_val, $mv_var );
1044-
if ( is_wp_error( $validate ) ) {
1045-
return $validate;
1046-
}
988+
switch ( $mv_var['type'] ) {
989+
/**
990+
* US Phone validation
991+
*
992+
* - Merge field is phone
993+
* - Merge field is "included" in the Mailchimp admin options
994+
* - Phone format is set in Mailchimp account
995+
* - Phone format is US in Mailchimp account
996+
*/
997+
case 'phone':
998+
if (
999+
'on' === get_option( $opt )
1000+
&& isset( $mv_var['options']['phone_format'] )
1001+
&& 'US' === $mv_var['options']['phone_format']
1002+
) {
1003+
$opt_val = mailchimp_sf_merge_validate_phone( $opt_val, $mv_var );
1004+
if ( is_wp_error( $opt_val ) ) {
1005+
return $opt_val;
1006+
}
1007+
}
1008+
break;
10471009

1048-
if ( $validate ) {
1049-
$merge->$tag = $validate;
1050-
}
1051-
continue;
1010+
/**
1011+
* Address validation
1012+
*
1013+
* - Merge field is address
1014+
* - Merge field is "included" in the Mailchimp admin options
1015+
* - Merge field is an array (address contains multiple <input> elements)
1016+
*/
1017+
case 'address':
1018+
if ( 'on' === get_option( $opt ) && is_array( $opt_val ) ) {
1019+
$validate = mailchimp_sf_merge_validate_address( $opt_val, $mv_var );
1020+
if ( is_wp_error( $validate ) ) {
1021+
return $validate;
1022+
}
10521023

1053-
} elseif ( is_array( $opt_val ) ) {
1054-
$keys = array_keys( $opt_val );
1055-
$val = new stdClass();
1056-
foreach ( $keys as $key ) {
1057-
$val->$key = $opt_val[ $key ];
1058-
}
1059-
$opt_val = $val;
1024+
if ( $validate ) {
1025+
$merge->$tag = $validate;
1026+
}
1027+
}
1028+
break;
1029+
1030+
/**
1031+
* Handle generic array values
1032+
*
1033+
* Not sure what this does or is for
1034+
*
1035+
* - Merge field is an array, not specifically phone or address
1036+
*/
1037+
default:
1038+
if ( is_array( $opt_val ) ) {
1039+
$keys = array_keys( $opt_val );
1040+
$val = new stdClass();
1041+
foreach ( $keys as $key ) {
1042+
$val->$key = $opt_val[ $key ];
1043+
}
1044+
$opt_val = $val;
1045+
}
1046+
break;
10601047
}
10611048

1049+
/**
1050+
* Required fields
1051+
*
1052+
* If the field is required and empty, return an error
1053+
*/
10621054
if ( 'Y' === $mv_var['required'] && trim( $opt_val ) === '' ) {
10631055
/* translators: %s: field name */
10641056
$message = sprintf( esc_html__( 'You must fill in %s.', 'mailchimp' ), esc_html( $mv_var['name'] ) );

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/cypress/e2e/connect.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ describe('Admin can connect to "Mailchimp" Account', () => {
2323
cy.get('#mailchimp_sf_oauth_connect').click();
2424
cy.wait(6000);
2525

26+
// Accept cookie consent popup window (if present)
27+
cy.popup().then(($popup) => {
28+
const acceptButtonSelector = '#onetrust-accept-btn-handler';
29+
30+
// Check if the accept button is visible and click it
31+
if ($popup.find(acceptButtonSelector).length > 0 && $popup.find(acceptButtonSelector).is(':visible')) {
32+
$popup.find(acceptButtonSelector).click();
33+
} else {
34+
cy.log('Cookie consent popup not found or not visible.');
35+
}
36+
});
37+
2638
cy.popup()
2739
.find('input#username')
2840
.clear()

views/setup_page.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77

88
$user = get_option( 'mc_user' );
9-
/* TODO MC SOPRESTO USER INFO */
109

1110
// If we have an API Key, see if we need to change the lists and its options
1211
mailchimp_sf_change_list_if_necessary();
@@ -300,10 +299,10 @@ function ( $ele ) {
300299
<tr valign="top">
301300
<td><?php echo esc_html( $mv_var['name'] ); ?></td>
302301
<td><?php echo esc_html( $mv_var['tag'] ); ?></td>
303-
<td><?php echo esc_html( ( 1 === $mv_var['required'] ) ? 'Y' : 'N' ); ?></td>
302+
<td><?php echo esc_html( ( 1 === intval( $mv_var['required'] ) ) ? 'Y' : 'N' ); ?></td>
304303
<td>
305304
<?php
306-
if ( ! $mv_var['required'] ) {
305+
if ( ! $mv_var['required'] && $mv_var['public'] ) {
307306
$opt = 'mc_mv_' . $mv_var['tag'];
308307
?>
309308
<label class="screen-reader-text" for="<?php echo esc_attr( $opt ); ?>">

0 commit comments

Comments
 (0)