Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b6c7b0b
Add WP admin notices to plugin settings page
MaxwellGarceau Dec 19, 2024
8382b91
Move admin-notices to separate file
MaxwellGarceau Dec 19, 2024
135094c
Add wp_kses to allow links for admin notices
MaxwellGarceau Dec 19, 2024
b6cf2a7
Use admin notices for MC list errors
MaxwellGarceau Dec 19, 2024
9c02369
Remove global message output from settings page
MaxwellGarceau Dec 19, 2024
d30bfa3
Rename global message to frontend message
MaxwellGarceau Dec 19, 2024
85c6632
Fix typo
MaxwellGarceau Dec 19, 2024
222e6db
Remove void return type to maintain compatibility with php 7.0
MaxwellGarceau Dec 19, 2024
90b0289
Update comment
MaxwellGarceau Dec 19, 2024
8db2531
Update success message selectors in settings.test.js
MaxwellGarceau Dec 19, 2024
9652226
Namespace admin notice functions
MaxwellGarceau Dec 26, 2024
65ecf2b
Add autoloading via composer PSR-4
MaxwellGarceau Dec 26, 2024
d6bd14b
Fix lint errors
MaxwellGarceau Dec 26, 2024
868a777
Update build workflows to install composer dependencies
MaxwellGarceau Dec 26, 2024
1e5a9fa
Revert autoload commits
MaxwellGarceau Dec 26, 2024
7fce632
Change todo note to info note
MaxwellGarceau Dec 26, 2024
7aeb042
Fix lint issue
MaxwellGarceau Dec 26, 2024
fe3b7ba
Load admin functions with autoload
MaxwellGarceau Feb 3, 2025
8d48faf
Add back the build steps for composer autoload
MaxwellGarceau Feb 3, 2025
da89938
Add "mailchimp_sf_global_msg" back to fix fatal error in other PR. (T…
iamdharmesh Apr 4, 2025
7d78013
Fix PHPCS
iamdharmesh Apr 4, 2025
e9b6465
Formatting fix for the admin notice message.
iamdharmesh Apr 7, 2025
b9603ed
Merge branch 'develop' of github.com:mailchimp/wordpress into enhance…
iamdharmesh Apr 7, 2025
fc788c9
Merge branch 'develop' of github.com:mailchimp/wordpress into enhance…
iamdharmesh Apr 7, 2025
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
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
"Mailchimp\\WordPress\\": "src/"
},
"classmap": [
"includes"
"includes/"
],
"files": [
"includes/admin/admin-notices.php"
]
},
"config": {
Expand Down
81 changes: 81 additions & 0 deletions includes/admin/admin-notices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Admin notices.
*
* @package Mailchimp
*/

namespace Mailchimp\WordPress\Includes\Admin;

/**
* Display success admin notice.
*
* NOTE: WordPress localization i18n functionality should be done
* on string literals outside of this function in order to work
* correctly.
*
* For more info read here: https://salferrarello.com/why-__-needs-a-hardcoded-string-in-wordpress/
*
* @since 1.7.0
* @param string $msg The message to display.
* @return void
*/
function admin_notice_success( string $msg ) {
?>
<div class="notice notice-success is-dismissible">
<p>
<?php
echo wp_kses(
$msg,
array(
'a' => array(
'href' => array(),
'title' => array(),
'target' => array(),
),
'strong' => array(),
'em' => array(),
'br' => array(),
)
);
?>
</p>
</div>
<?php
}

/**
* Display error admin notice.
*
* NOTE: WordPress localization i18n functionality should be done
* on string literals outside of this function in order to work
* correctly.
*
* For more info read here: https://salferrarello.com/why-__-needs-a-hardcoded-string-in-wordpress/
*
* @since 1.7.0
* @param string $msg The message to display.
* @return void
*/
function admin_notice_error( string $msg ) {
?>
<div class="notice notice-error">
<p>
<?php
echo wp_kses(
$msg,
array(
'a' => array(
'href' => array(),
'title' => array(),
'target' => array(),
),
'strong' => array(),
'em' => array(),
)
);
?>
</p>
</div>
<?php
}
8 changes: 8 additions & 0 deletions includes/class-mailchimp-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
/**
* Class responsible for admin side functionalities.
*
* The long term plan is to break up admin functionality into smaller, more focused
* files to improve maintainability. This could also include:
* - Moving OAuth related code to oauth.php
* - Moving account creation code to account.php
* - Moving settings page code to settings.php
* - Moving notices code to notices.php (already done)
* This will help avoid having too much code in a single file and make the codebase more modular.
*
* @package Mailchimp
*/

Expand Down
96 changes: 57 additions & 39 deletions mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ function () {
return;
}

use function Mailchimp\WordPress\Includes\Admin\{admin_notice_error, admin_notice_success};

// Version constant for easy CSS refreshes
define( 'MCSF_VER', '1.6.3' );

Expand Down Expand Up @@ -291,7 +293,7 @@ function mailchimp_sf_request_handler() {
if ( ! headers_sent() ) { // just in case...
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT', true, 200 );
}
echo wp_kses_post( mailchimp_sf_global_msg() );
echo wp_kses_post( mailchimp_sf_frontend_msg() );
exit;
}
}
Expand Down Expand Up @@ -411,12 +413,17 @@ function mailchimp_sf_delete_setup() {
}

/**
* Gets or sets a global message based on parameter passed to it
* Gets or sets a frontend message based on parameter passed to it
*
* Used to convey error messages to the user outside of the WP Admin
*
* On the plugin settings page, WP admin notices are used exclusively
* instead of the frontend message.
*
* @param mixed $msg Message
* @return string/bool depending on get/set
*/
function mailchimp_sf_global_msg( $msg = null ) {
function mailchimp_sf_frontend_msg( $msg = null ) {
global $mcsf_msgs;

// Make sure we're formed properly
Expand All @@ -434,6 +441,18 @@ function mailchimp_sf_global_msg( $msg = null ) {
return true;
}

/**
* Gets or sets a frontend message based on parameter passed to it
*
* TODO: Deprecate this function in favor of mailchimp_sf_frontend_msg()
*
* @param mixed $msg Message
* @return string/bool depending on get/set
*/
function mailchimp_sf_global_msg( $msg = null ) {
return mailchimp_sf_frontend_msg( $msg );
}

/**
* Sets the default options for the option form
*
Expand Down Expand Up @@ -464,44 +483,44 @@ function mailchimp_sf_save_general_form_settings() {
/*Enable double optin toggle*/
if ( isset( $_POST['mc_double_optin'] ) ) {
update_option( 'mc_double_optin', true );
$msg = '<p class="success_msg">' . esc_html__( 'Double opt-in turned On!', 'mailchimp' ) . '</p>';
mailchimp_sf_global_msg( $msg );
$msg = esc_html__( 'Double opt-in turned On!', 'mailchimp' );
admin_notice_success( $msg );
} elseif ( get_option( 'mc_double_optin' ) !== false ) {
update_option( 'mc_double_optin', false );
$msg = '<p class="success_msg">' . esc_html__( 'Double opt-in turned Off!', 'mailchimp' ) . '</p>';
mailchimp_sf_global_msg( $msg );
$msg = esc_html__( 'Double opt-in turned Off!', 'mailchimp' );
admin_notice_success( $msg );
}

/* NUKE the CSS! */
if ( isset( $_POST['mc_nuke_all_styles'] ) ) {
update_option( 'mc_nuke_all_styles', true );
$msg = '<p class="success_msg">' . esc_html__( 'Mailchimp CSS turned Off!', 'mailchimp' ) . '</p>';
mailchimp_sf_global_msg( $msg );
$msg = esc_html__( 'Mailchimp CSS turned Off!', 'mailchimp' );
admin_notice_success( $msg );
} elseif ( get_option( 'mc_nuke_all_styles' ) !== false ) {
update_option( 'mc_nuke_all_styles', false );
$msg = '<p class="success_msg">' . esc_html__( 'Mailchimp CSS turned On!', 'mailchimp' ) . '</p>';
mailchimp_sf_global_msg( $msg );
$msg = esc_html__( 'Mailchimp CSS turned On!', 'mailchimp' );
admin_notice_success( $msg );
}

/* Update existing */
if ( isset( $_POST['mc_update_existing'] ) ) {
update_option( 'mc_update_existing', true );
$msg = '<p class="success_msg">' . esc_html__( 'Update existing subscribers turned On!' ) . '</p>';
mailchimp_sf_global_msg( $msg );
$msg = esc_html__( 'Update existing subscribers turned On!' );
admin_notice_success( $msg );
} elseif ( get_option( 'mc_update_existing' ) !== false ) {
update_option( 'mc_update_existing', false );
$msg = '<p class="success_msg">' . esc_html__( 'Update existing subscribers turned Off!' ) . '</p>';
mailchimp_sf_global_msg( $msg );
$msg = esc_html__( 'Update existing subscribers turned Off!' );
admin_notice_success( $msg );
}

if ( isset( $_POST['mc_use_unsub_link'] ) ) {
update_option( 'mc_use_unsub_link', 'on' );
$msg = '<p class="success_msg">' . esc_html__( 'Unsubscribe link turned On!', 'mailchimp' ) . '</p>';
mailchimp_sf_global_msg( $msg );
$msg = esc_html__( 'Unsubscribe link turned On!', 'mailchimp' );
admin_notice_success( $msg );
} elseif ( get_option( 'mc_use_unsub_link' ) !== 'off' ) {
update_option( 'mc_use_unsub_link', 'off' );
$msg = '<p class="success_msg">' . esc_html__( 'Unsubscribe link turned Off!', 'mailchimp' ) . '</p>';
mailchimp_sf_global_msg( $msg );
$msg = esc_html__( 'Unsubscribe link turned Off!', 'mailchimp' );
admin_notice_success( $msg );
}

$content = isset( $_POST['mc_header_content'] ) ? wp_kses_post( wp_unslash( $_POST['mc_header_content'] ) ) : '';
Expand Down Expand Up @@ -558,8 +577,8 @@ function mailchimp_sf_save_general_form_settings() {
}
}

$msg = '<p class="success_msg">' . esc_html__( 'Successfully Updated your List Subscribe Form Settings!', 'mailchimp' ) . '</p>';
mailchimp_sf_global_msg( $msg );
$msg = esc_html__( 'Successfully Updated your List Subscribe Form Settings!', 'mailchimp' );
admin_notice_success( $msg );
}

/**
Expand All @@ -571,8 +590,8 @@ function mailchimp_sf_change_list_if_necessary() {
}

if ( empty( $_POST['mc_list_id'] ) ) {
$msg = '<p class="error_msg">' . esc_html__( 'Please choose a valid list', 'mailchimp' ) . '</p>';
mailchimp_sf_global_msg( $msg );
$msg = esc_html__( 'Please choose a valid list', 'mailchimp' );
admin_notice_error( $msg );
return;
}

Expand Down Expand Up @@ -639,16 +658,15 @@ function mailchimp_sf_change_list_if_necessary() {
$igs_text .= sprintf( esc_html__( 'and %s Sets of Interest Groups', 'mailchimp' ), count( $igs ) );
}

$msg = '<p class="success_msg">' .
sprintf(
/* translators: %s: count (number) */
__( '<b>Success!</b> Loaded and saved the info for %d Merge Variables', 'mailchimp' ) . $igs_text,
count( $mv )
) . ' ' .
esc_html__( 'from your list' ) . ' "' . $list_name . '"<br/><br/>' .
esc_html__( 'Now you should either Turn On the Mailchimp Widget or change your options below, then turn it on.', 'mailchimp' ) . '</p>';

mailchimp_sf_global_msg( $msg );
$msg = sprintf(
/* translators: %s: count (number) */
__( '<b>Success!</b> Loaded and saved the info for %d Merge Variables', 'mailchimp' ) . $igs_text,
count( $mv )
) . ' ' .
esc_html__( 'from your list' ) . ' "' . $list_name . '"<br/><br/>' .
esc_html__( 'Now you should either Turn On the Mailchimp Widget or change your options below, then turn it on.', 'mailchimp' );

admin_notice_success( $msg );
}

// Update the lists option.
Expand Down Expand Up @@ -808,7 +826,7 @@ function mailchimp_sf_signup_submit() {
// Catch errors and fail early.
if ( is_wp_error( $merge ) ) {
$msg = '<strong class="mc_error_msg">' . $merge->get_error_message() . '</strong>';
mailchimp_sf_global_msg( $msg );
mailchimp_sf_frontend_msg( $msg );

return false;
}
Expand Down Expand Up @@ -849,7 +867,7 @@ function mailchimp_sf_signup_submit() {
]
)
);
mailchimp_sf_global_msg( $error );
mailchimp_sf_frontend_msg( $error );
return false;
}

Expand All @@ -861,7 +879,7 @@ function mailchimp_sf_signup_submit() {
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>' );
mailchimp_sf_frontend_msg( '<strong class="mc_error_msg">' . $msg . '</strong>' );
return false;
}

Expand All @@ -874,7 +892,7 @@ function mailchimp_sf_signup_submit() {
// If we have errors, then show them
if ( is_wp_error( $retval ) ) {
$msg = '<strong class="mc_error_msg">' . $retval->get_error_message() . '</strong>';
mailchimp_sf_global_msg( $msg );
mailchimp_sf_frontend_msg( $msg );
return false;
}

Expand All @@ -886,8 +904,8 @@ function mailchimp_sf_signup_submit() {
$msg = "<strong class='mc_success_msg'>{$esc}</strong>";
}

// Set our global message
mailchimp_sf_global_msg( $msg );
// Set our front end success message
mailchimp_sf_frontend_msg( $msg );

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions mailchimp_widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ function mailchimp_sf_signup_form( $args = array() ) {

<div class="mc_form_inside">

<div class="update mc_message_wrapper" id="mc_message">
<?php echo wp_kses_post( mailchimp_sf_global_msg() ); ?>
<div class="updated mc_message_wrapper" id="mc_message">
<?php echo wp_kses_post( mailchimp_sf_frontend_msg() ); ?>
</div><!-- /mc_message -->

<?php
Expand Down
2 changes: 1 addition & 1 deletion tests/cypress/e2e/admin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Admin can login and make sure plugin is activated', () => {
cy.get('#mailchimp-sf-create-activate-account').should('be.visible');
});

it("Admin shouldn't able to submit create account form with invalid data", () => {
it("Admin shouldn't be able to submit create account form with invalid data", () => {
cy.visit('/wp-admin/admin.php?page=mailchimp_sf_create_account');

// Submit form without filling any data.
Expand Down
2 changes: 1 addition & 1 deletion tests/cypress/e2e/settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Admin can update plugin settings', () => {
cy.get('.mc-h2').contains('Your Lists');
cy.get('#mc_list_id').select('10up');
cy.get('input[value="Update List"]').click();
cy.get('#mc-message .success_msg b').contains('Success!');
cy.get('#mailchimp-sf-settings-page .notice.notice-success p').contains('Success!');
});

it('Admin can create a Signup form using the shortcode', () => {
Expand Down
Loading