Skip to content

Commit 8e7fcc5

Browse files
authored
Merge pull request #85 from mailchimp/enhancement/update-success-error-messages-to-wp-admin-notices
Enhancement/update success error messages to wp admin notices
2 parents 1468478 + fc788c9 commit 8e7fcc5

File tree

8 files changed

+168
-74
lines changed

8 files changed

+168
-74
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
"Mailchimp\\WordPress\\": "src/"
2424
},
2525
"classmap": [
26-
"includes"
26+
"includes/"
27+
],
28+
"files": [
29+
"includes/admin/admin-notices.php"
2730
]
2831
},
2932
"config": {

includes/admin/admin-notices.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Admin notices.
4+
*
5+
* @package Mailchimp
6+
*/
7+
8+
namespace Mailchimp\WordPress\Includes\Admin;
9+
10+
/**
11+
* Display success admin notice.
12+
*
13+
* NOTE: WordPress localization i18n functionality should be done
14+
* on string literals outside of this function in order to work
15+
* correctly.
16+
*
17+
* For more info read here: https://salferrarello.com/why-__-needs-a-hardcoded-string-in-wordpress/
18+
*
19+
* @since 1.7.0
20+
* @param string $msg The message to display.
21+
* @return void
22+
*/
23+
function admin_notice_success( string $msg ) {
24+
?>
25+
<div class="notice notice-success is-dismissible">
26+
<p>
27+
<?php
28+
echo wp_kses(
29+
$msg,
30+
array(
31+
'a' => array(
32+
'href' => array(),
33+
'title' => array(),
34+
'target' => array(),
35+
),
36+
'strong' => array(),
37+
'em' => array(),
38+
'br' => array(),
39+
)
40+
);
41+
?>
42+
</p>
43+
</div>
44+
<?php
45+
}
46+
47+
/**
48+
* Display error admin notice.
49+
*
50+
* NOTE: WordPress localization i18n functionality should be done
51+
* on string literals outside of this function in order to work
52+
* correctly.
53+
*
54+
* For more info read here: https://salferrarello.com/why-__-needs-a-hardcoded-string-in-wordpress/
55+
*
56+
* @since 1.7.0
57+
* @param string $msg The message to display.
58+
* @return void
59+
*/
60+
function admin_notice_error( string $msg ) {
61+
?>
62+
<div class="notice notice-error">
63+
<p>
64+
<?php
65+
echo wp_kses(
66+
$msg,
67+
array(
68+
'a' => array(
69+
'href' => array(),
70+
'title' => array(),
71+
'target' => array(),
72+
),
73+
'strong' => array(),
74+
'em' => array(),
75+
)
76+
);
77+
?>
78+
</p>
79+
</div>
80+
<?php
81+
}

includes/class-mailchimp-admin.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
/**
33
* Class responsible for admin side functionalities.
44
*
5+
* The long term plan is to break up admin functionality into smaller, more focused
6+
* files to improve maintainability. This could also include:
7+
* - Moving OAuth related code to oauth.php
8+
* - Moving account creation code to account.php
9+
* - Moving settings page code to settings.php
10+
* - Moving notices code to notices.php (already done)
11+
* This will help avoid having too much code in a single file and make the codebase more modular.
12+
*
513
* @package Mailchimp
614
*/
715

mailchimp.php

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ function () {
6464
return;
6565
}
6666

67+
use function Mailchimp\WordPress\Includes\Admin\{admin_notice_error, admin_notice_success};
68+
6769
// Version constant for easy CSS refreshes
6870
define( 'MCSF_VER', '1.6.3' );
6971

@@ -291,7 +293,7 @@ function mailchimp_sf_request_handler() {
291293
if ( ! headers_sent() ) { // just in case...
292294
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT', true, 200 );
293295
}
294-
echo wp_kses_post( mailchimp_sf_global_msg() );
296+
echo wp_kses_post( mailchimp_sf_frontend_msg() );
295297
exit;
296298
}
297299
}
@@ -411,12 +413,17 @@ function mailchimp_sf_delete_setup() {
411413
}
412414

413415
/**
414-
* Gets or sets a global message based on parameter passed to it
416+
* Gets or sets a frontend message based on parameter passed to it
417+
*
418+
* Used to convey error messages to the user outside of the WP Admin
419+
*
420+
* On the plugin settings page, WP admin notices are used exclusively
421+
* instead of the frontend message.
415422
*
416423
* @param mixed $msg Message
417424
* @return string/bool depending on get/set
418425
*/
419-
function mailchimp_sf_global_msg( $msg = null ) {
426+
function mailchimp_sf_frontend_msg( $msg = null ) {
420427
global $mcsf_msgs;
421428

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

444+
/**
445+
* Gets or sets a frontend message based on parameter passed to it
446+
*
447+
* TODO: Deprecate this function in favor of mailchimp_sf_frontend_msg()
448+
*
449+
* @param mixed $msg Message
450+
* @return string/bool depending on get/set
451+
*/
452+
function mailchimp_sf_global_msg( $msg = null ) {
453+
return mailchimp_sf_frontend_msg( $msg );
454+
}
455+
437456
/**
438457
* Sets the default options for the option form
439458
*
@@ -464,44 +483,44 @@ function mailchimp_sf_save_general_form_settings() {
464483
/*Enable double optin toggle*/
465484
if ( isset( $_POST['mc_double_optin'] ) ) {
466485
update_option( 'mc_double_optin', true );
467-
$msg = '<p class="success_msg">' . esc_html__( 'Double opt-in turned On!', 'mailchimp' ) . '</p>';
468-
mailchimp_sf_global_msg( $msg );
486+
$msg = esc_html__( 'Double opt-in turned On!', 'mailchimp' );
487+
admin_notice_success( $msg );
469488
} elseif ( get_option( 'mc_double_optin' ) !== false ) {
470489
update_option( 'mc_double_optin', false );
471-
$msg = '<p class="success_msg">' . esc_html__( 'Double opt-in turned Off!', 'mailchimp' ) . '</p>';
472-
mailchimp_sf_global_msg( $msg );
490+
$msg = esc_html__( 'Double opt-in turned Off!', 'mailchimp' );
491+
admin_notice_success( $msg );
473492
}
474493

475494
/* NUKE the CSS! */
476495
if ( isset( $_POST['mc_nuke_all_styles'] ) ) {
477496
update_option( 'mc_nuke_all_styles', true );
478-
$msg = '<p class="success_msg">' . esc_html__( 'Mailchimp CSS turned Off!', 'mailchimp' ) . '</p>';
479-
mailchimp_sf_global_msg( $msg );
497+
$msg = esc_html__( 'Mailchimp CSS turned Off!', 'mailchimp' );
498+
admin_notice_success( $msg );
480499
} elseif ( get_option( 'mc_nuke_all_styles' ) !== false ) {
481500
update_option( 'mc_nuke_all_styles', false );
482-
$msg = '<p class="success_msg">' . esc_html__( 'Mailchimp CSS turned On!', 'mailchimp' ) . '</p>';
483-
mailchimp_sf_global_msg( $msg );
501+
$msg = esc_html__( 'Mailchimp CSS turned On!', 'mailchimp' );
502+
admin_notice_success( $msg );
484503
}
485504

486505
/* Update existing */
487506
if ( isset( $_POST['mc_update_existing'] ) ) {
488507
update_option( 'mc_update_existing', true );
489-
$msg = '<p class="success_msg">' . esc_html__( 'Update existing subscribers turned On!' ) . '</p>';
490-
mailchimp_sf_global_msg( $msg );
508+
$msg = esc_html__( 'Update existing subscribers turned On!' );
509+
admin_notice_success( $msg );
491510
} elseif ( get_option( 'mc_update_existing' ) !== false ) {
492511
update_option( 'mc_update_existing', false );
493-
$msg = '<p class="success_msg">' . esc_html__( 'Update existing subscribers turned Off!' ) . '</p>';
494-
mailchimp_sf_global_msg( $msg );
512+
$msg = esc_html__( 'Update existing subscribers turned Off!' );
513+
admin_notice_success( $msg );
495514
}
496515

497516
if ( isset( $_POST['mc_use_unsub_link'] ) ) {
498517
update_option( 'mc_use_unsub_link', 'on' );
499-
$msg = '<p class="success_msg">' . esc_html__( 'Unsubscribe link turned On!', 'mailchimp' ) . '</p>';
500-
mailchimp_sf_global_msg( $msg );
518+
$msg = esc_html__( 'Unsubscribe link turned On!', 'mailchimp' );
519+
admin_notice_success( $msg );
501520
} elseif ( get_option( 'mc_use_unsub_link' ) !== 'off' ) {
502521
update_option( 'mc_use_unsub_link', 'off' );
503-
$msg = '<p class="success_msg">' . esc_html__( 'Unsubscribe link turned Off!', 'mailchimp' ) . '</p>';
504-
mailchimp_sf_global_msg( $msg );
522+
$msg = esc_html__( 'Unsubscribe link turned Off!', 'mailchimp' );
523+
admin_notice_success( $msg );
505524
}
506525

507526
$content = isset( $_POST['mc_header_content'] ) ? wp_kses_post( wp_unslash( $_POST['mc_header_content'] ) ) : '';
@@ -558,8 +577,8 @@ function mailchimp_sf_save_general_form_settings() {
558577
}
559578
}
560579

561-
$msg = '<p class="success_msg">' . esc_html__( 'Successfully Updated your List Subscribe Form Settings!', 'mailchimp' ) . '</p>';
562-
mailchimp_sf_global_msg( $msg );
580+
$msg = esc_html__( 'Successfully Updated your List Subscribe Form Settings!', 'mailchimp' );
581+
admin_notice_success( $msg );
563582
}
564583

565584
/**
@@ -571,8 +590,8 @@ function mailchimp_sf_change_list_if_necessary() {
571590
}
572591

573592
if ( empty( $_POST['mc_list_id'] ) ) {
574-
$msg = '<p class="error_msg">' . esc_html__( 'Please choose a valid list', 'mailchimp' ) . '</p>';
575-
mailchimp_sf_global_msg( $msg );
593+
$msg = esc_html__( 'Please choose a valid list', 'mailchimp' );
594+
admin_notice_error( $msg );
576595
return;
577596
}
578597

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

642-
$msg = '<p class="success_msg">' .
643-
sprintf(
644-
/* translators: %s: count (number) */
645-
__( '<b>Success!</b> Loaded and saved the info for %d Merge Variables', 'mailchimp' ) . $igs_text,
646-
count( $mv )
647-
) . ' ' .
648-
esc_html__( 'from your list' ) . ' "' . $list_name . '"<br/><br/>' .
649-
esc_html__( 'Now you should either Turn On the Mailchimp Widget or change your options below, then turn it on.', 'mailchimp' ) . '</p>';
650-
651-
mailchimp_sf_global_msg( $msg );
661+
$msg = sprintf(
662+
/* translators: %s: count (number) */
663+
__( '<b>Success!</b> Loaded and saved the info for %d Merge Variables', 'mailchimp' ) . $igs_text,
664+
count( $mv )
665+
) . ' ' .
666+
esc_html__( 'from your list' ) . ' "' . $list_name . '"<br/><br/>' .
667+
esc_html__( 'Now you should either Turn On the Mailchimp Widget or change your options below, then turn it on.', 'mailchimp' );
668+
669+
admin_notice_success( $msg );
652670
}
653671

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

813831
return false;
814832
}
@@ -849,7 +867,7 @@ function mailchimp_sf_signup_submit() {
849867
]
850868
)
851869
);
852-
mailchimp_sf_global_msg( $error );
870+
mailchimp_sf_frontend_msg( $error );
853871
return false;
854872
}
855873

@@ -861,7 +879,7 @@ function mailchimp_sf_signup_submit() {
861879
if ( ! get_option( 'mc_update_existing' ) && ! $is_new_subscriber ) {
862880
$msg = esc_html__( 'This email address has already been subscribed to this list.', 'mailchimp' );
863881
$error = new WP_Error( 'mailchimp-update-existing', $msg );
864-
mailchimp_sf_global_msg( '<strong class="mc_error_msg">' . $msg . '</strong>' );
882+
mailchimp_sf_frontend_msg( '<strong class="mc_error_msg">' . $msg . '</strong>' );
865883
return false;
866884
}
867885

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

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

889-
// Set our global message
890-
mailchimp_sf_global_msg( $msg );
907+
// Set our front end success message
908+
mailchimp_sf_frontend_msg( $msg );
891909

892910
return true;
893911
}

mailchimp_widget.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ function mailchimp_sf_signup_form( $args = array() ) {
184184

185185
<div class="mc_form_inside">
186186

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

191191
<?php

tests/cypress/e2e/admin.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('Admin can login and make sure plugin is activated', () => {
3232
cy.get('#mailchimp-sf-create-activate-account').should('be.visible');
3333
});
3434

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

3838
// Submit form without filling any data.

tests/cypress/e2e/settings.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('Admin can update plugin settings', () => {
1313
cy.get('.mc-h2').contains('Your Lists');
1414
cy.get('#mc_list_id').select('10up');
1515
cy.get('input[value="Update List"]').click();
16-
cy.get('#mc-message .success_msg b').contains('Success!');
16+
cy.get('#mailchimp-sf-settings-page .notice.notice-success p').contains('Success!');
1717
});
1818

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

0 commit comments

Comments
 (0)