Skip to content

Commit

Permalink
Merge branch 'develop' into enhancement/add-composer-autoload
Browse files Browse the repository at this point in the history
  • Loading branch information
dkotter committed Jan 28, 2025
2 parents 6a238a2 + 8d10e68 commit 0c92026
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 113 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/wordpress-plugin-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ jobs:
tag:
name: New release
runs-on: ubuntu-latest
if: ${{ !github.event.release.prerelease }} # Skip job if it is a pre-release

steps:
- name: Checkout code
Expand Down Expand Up @@ -44,12 +45,11 @@ jobs:
SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }}
SLUG: mailchimp

- name: Upload release asset
uses: actions/[email protected]
- name: Attach the wordpress.org plugin files to the Github release
uses: softprops/action-gh-release@v2
with:
files: ${{ steps.deploy.outputs.zip-path }}
body: |
This release contains the latest updates for the WordPress plugin.
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ${{ steps.deploy.outputs.zip-path }}
asset_name: mailchimp.zip
asset_content_type: application/zip
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ tests/cypress/reports
tests/cypress/downloads

mailchimp.zip

# IDE
.vscode
11 changes: 11 additions & 0 deletions includes/blocks/mailchimp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ import Icon from './icon';

registerBlockType(metadata, {
icon: Icon,
transforms: {
from: [
{
type: 'shortcode',
tag: 'mailchimpsf_form',
attributes: {
// No attributes, but attributes property is required
},
},
],
},
edit: BlockEdit,
save: () => null,
});
192 changes: 92 additions & 100 deletions mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ function () {
*/
function mailchimp_sf_plugin_init() {

// Remove Sopresto check. If user does not have API key, make them authenticate.

if ( get_option( 'mc_list_id' ) && get_option( 'mc_merge_field_migrate' ) !== '1' && mailchimp_sf_get_api() !== false ) {
mailchimp_sf_update_merge_fields();
}
Expand Down Expand Up @@ -247,7 +245,7 @@ function mailchimp_sf_request_handler() {
}

// erase auth information
$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' );
$options = array( 'mc_api_key', 'mailchimp_sf_access_token', 'mc_datacenter', 'mailchimp_sf_auth_error', 'mailchimp_sf_waiting_for_login' );
mailchimp_sf_delete_options( $options );
break;
case 'change_form_settings':
Expand Down Expand Up @@ -291,58 +289,6 @@ function mailchimp_sf_request_handler() {
}
add_action( 'init', 'mailchimp_sf_request_handler' );

/**
* Migrate Sopresto
*
* @return void
*/
function mailchimp_sf_migrate_sopresto() {
$sopresto = get_option( 'mc_sopresto_secret_key' );
if ( ! $sopresto ) {
return;
}

// Talk to Sopresto, make exchange, delete old sopresto things.
$body = array(
'public_key' => get_option( 'mc_sopresto_public_key' ),
'hash' => sha1( get_option( 'mc_sopresto_public_key' ) . get_option( 'mc_sopresto_secret_key' ) ),
);

$url = 'https://sopresto.socialize-this.com/mailchimp/exchange';
$args = array(
'method' => 'POST',
'timeout' => 500,
'redirection' => 5,
'httpversion' => '1.0',
'user-agent' => 'Mailchimp WordPress Plugin/' . get_bloginfo( 'url' ),
'body' => $body,
);

// post to sopresto
$key = wp_remote_post( $url, $args );
if ( ! is_wp_error( $key ) && 200 === $key['response']['code'] ) {
$key = json_decode( $key['body'] );
try {
$api = new MailChimp_API( $key->response );
} catch ( Exception $e ) {
$msg = '<strong class="mc_error_msg">' . $e->getMessage() . '</strong>';
mailchimp_sf_global_msg( $msg );
return;
}

$verify = mailchimp_sf_verify_key( $api );

// something went wrong with the key that we had
if ( is_wp_error( $verify ) ) {
return;
}

delete_option( 'mc_sopresto_public_key' );
delete_option( 'mc_sopresto_secret_key' );
delete_option( 'mc_sopresto_user' );
}
}

/**
* Update merge fields
*
Expand Down Expand Up @@ -421,13 +367,13 @@ function mailchimp_sf_needs_upgrade() {

/**
* Deletes all Mailchimp options
*
* TODO: The options names should be moved to a config file
* or to a class dedicated to options
**/
function mailchimp_sf_delete_setup() {
$options = array(
'mc_user_id',
'mc_sopresto_user',
'mc_sopresto_public_key',
'mc_sopresto_secret_key',
'mc_use_javascript',
'mc_use_datepicker',
'mc_use_unsub_link',
Expand Down Expand Up @@ -934,14 +880,18 @@ function mailchimp_sf_signup_submit() {
$url = 'lists/' . $list_id . '/members/' . md5( strtolower( $email ) );
$status = mailchimp_sf_check_status( $url );

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

// TODO: If get_option( 'mc_update_existing' ) && 'unsubscribed' === $status then
// make an API request to fetch Mailchimp hosted sign up form and display to user

$body = mailchimp_sf_subscribe_body( $merge, $igs, $email_type, $email, $status, get_option( 'mc_double_optin' ) );
$retval = $api->post( $url, $body, 'PUT' );

Expand Down Expand Up @@ -970,40 +920,42 @@ function mailchimp_sf_signup_submit() {
* Cleans up merge fields and interests to make them
* API 3.0-friendly.
*
* @param [type] $merge Merge fields
* @param [type] $igs Interest groups
* @param string $email_type Email type
* @param string $email Email
* @param string $status Status
* @param bool $double_optin Whether this is double optin
* @param [type] $merge Merge fields
* @param [type] $igs Interest groups
* @param string $email_type Email type
* @param string $email Email
* @param string|false $status Status The subscription status ('subscribed', 'unsubscribed', 'pending', etc.) or false if an error occurred.
* @param string $double_optin Whether double opt-in is enabled. "1" for enabled and "" for disabled.
* @return stdClass
*/
function mailchimp_sf_subscribe_body( $merge, $igs, $email_type, $email, $status, $double_optin ) {
$body = new stdClass();
$body->email_address = $email;
$body->email_type = $email_type;
$body->merge_fields = $merge;

if ( ! empty( $igs ) ) {
$body->interests = $igs;
}

if ( 'subscribed' !== $status ) {
// single opt-in that covers new subscribers
if ( false === ! $status && $double_optin ) {
$body->status = 'subscribed';
} else {
// anyone else
$body->status = 'pending';
}
// Early return for already subscribed users
if ( 'subscribed' === $status ) {
return $body;
}

// Subscribe the email immediately unless double opt-in is enabled
// "unsubscribed" and "subscribed" existing emails have been excluded at this stage
// "pending" emails should follow double opt-in rules
$body->status = $double_optin ? 'pending' : 'subscribed';

return $body;
}

/**
* Check status.
* Check the status of a subscriber in the list.
*
* @param string $endpoint Endpoint.
* @return string
* @param string $endpoint API endpoint to check the status.
* @return string|false The subscription status ('subscribed', 'unsubscribed', 'pending', etc.) or false if the API returned 404 or an error occurred.
*/
function mailchimp_sf_check_status( $endpoint ) {
$endpoint .= '?fields=status';
Expand Down Expand Up @@ -1033,32 +985,72 @@ function mailchimp_sf_merge_submit( $mv ) {

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

// Handle phone number logic
if ( isset( $mv_var['options']['phone_format'] ) && 'phone' === $mv_var['type'] && 'US' === $mv_var['options']['phone_format'] ) {
$opt_val = mailchimp_sf_merge_validate_phone( $opt_val, $mv_var );
if ( is_wp_error( $opt_val ) ) {
return $opt_val;
}
} elseif ( is_array( $opt_val ) && 'address' === $mv_var['type'] ) { // Handle address logic
$validate = mailchimp_sf_merge_validate_address( $opt_val, $mv_var );
if ( is_wp_error( $validate ) ) {
return $validate;
}
switch ( $mv_var['type'] ) {
/**
* US Phone validation
*
* - Merge field is phone
* - Merge field is "included" in the Mailchimp admin options
* - Phone format is set in Mailchimp account
* - Phone format is US in Mailchimp account
*/
case 'phone':
if (
'on' === get_option( $opt )
&& isset( $mv_var['options']['phone_format'] )
&& 'US' === $mv_var['options']['phone_format']
) {
$opt_val = mailchimp_sf_merge_validate_phone( $opt_val, $mv_var );
if ( is_wp_error( $opt_val ) ) {
return $opt_val;
}
}
break;

if ( $validate ) {
$merge->$tag = $validate;
}
continue;
/**
* Address validation
*
* - Merge field is address
* - Merge field is "included" in the Mailchimp admin options
* - Merge field is an array (address contains multiple <input> elements)
*/
case 'address':
if ( 'on' === get_option( $opt ) && is_array( $opt_val ) ) {
$validate = mailchimp_sf_merge_validate_address( $opt_val, $mv_var );
if ( is_wp_error( $validate ) ) {
return $validate;
}

} elseif ( is_array( $opt_val ) ) {
$keys = array_keys( $opt_val );
$val = new stdClass();
foreach ( $keys as $key ) {
$val->$key = $opt_val[ $key ];
}
$opt_val = $val;
if ( $validate ) {
$merge->$tag = $validate;
}
}
break;

/**
* Handle generic array values
*
* Not sure what this does or is for
*
* - Merge field is an array, not specifically phone or address
*/
default:
if ( is_array( $opt_val ) ) {
$keys = array_keys( $opt_val );
$val = new stdClass();
foreach ( $keys as $key ) {
$val->$key = $opt_val[ $key ];
}
$opt_val = $val;
}
break;
}

/**
* Required fields
*
* If the field is required and empty, return an error
*/
if ( 'Y' === $mv_var['required'] && trim( $opt_val ) === '' ) {
/* translators: %s: field name */
$message = sprintf( esc_html__( 'You must fill in %s.', 'mailchimp' ), esc_html( $mv_var['name'] ) );
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

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

12 changes: 12 additions & 0 deletions tests/cypress/e2e/connect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ describe('Admin can connect to "Mailchimp" Account', () => {
cy.get('#mailchimp_sf_oauth_connect').click();
cy.wait(6000);

// Accept cookie consent popup window (if present)
cy.popup().then(($popup) => {
const acceptButtonSelector = '#onetrust-accept-btn-handler';

// Check if the accept button is visible and click it
if ($popup.find(acceptButtonSelector).length > 0 && $popup.find(acceptButtonSelector).is(':visible')) {
$popup.find(acceptButtonSelector).click();
} else {
cy.log('Cookie consent popup not found or not visible.');
}
});

cy.popup()
.find('input#username')
.clear()
Expand Down
5 changes: 2 additions & 3 deletions views/setup_page.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

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

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

0 comments on commit 0c92026

Please sign in to comment.