diff --git a/includes/admin/admin-notices.php b/includes/admin/admin-notices.php new file mode 100644 index 0000000..6307c52 --- /dev/null +++ b/includes/admin/admin-notices.php @@ -0,0 +1,80 @@ + +
+

+ array( + 'href' => array(), + 'title' => array(), + 'target' => array(), + ), + 'strong' => array(), + 'em' => array(), + ) + ); + ?> +

+
+ +
+

+ array( + 'href' => array(), + 'title' => array(), + 'target' => array(), + ), + 'strong' => array(), + 'em' => array(), + ) + ); + ?> +

+
+ require_admin_utils(); + } + + /** + * Require admin utils. + * + * Currently includes admin notices from a single file. 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. + * + * @since 1.7.0 + */ + private function require_admin_utils() { + include_once MCSF_DIR . 'includes/admin/admin-notices.php'; } /** diff --git a/mailchimp.php b/mailchimp.php index f9870e5..3b79b99 100644 --- a/mailchimp.php +++ b/mailchimp.php @@ -34,6 +34,8 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +use function Mailchimp\WordPress\Includes\Admin\{admin_notice_error, admin_notice_success}; + // Version constant for easy CSS refreshes define( 'MCSF_VER', '1.6.2' ); @@ -253,7 +255,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; } } @@ -296,7 +298,7 @@ function mailchimp_sf_migrate_sopresto() { $api = new MailChimp_API( $key->response ); } catch ( Exception $e ) { $msg = '' . $e->getMessage() . ''; - mailchimp_sf_global_msg( $msg ); + mailchimp_sf_frontend_msg( $msg ); return; } @@ -427,12 +429,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 @@ -486,65 +493,65 @@ function mailchimp_sf_save_general_form_settings() { // IF NOT DEV MODE if ( isset( $_POST['mc_use_javascript'] ) ) { update_option( 'mc_use_javascript', 'on' ); - $msg = '

' . esc_html__( 'Fancy Javascript submission turned On!', 'mailchimp' ) . '

'; - mailchimp_sf_global_msg( $msg ); + $msg = esc_html__( 'Fancy Javascript submission turned On!', 'mailchimp' ); + admin_notice_success( $msg ); } elseif ( get_option( 'mc_use_javascript' ) !== 'off' ) { update_option( 'mc_use_javascript', 'off' ); - $msg = '

' . esc_html__( 'Fancy Javascript submission turned Off!', 'mailchimp' ) . '

'; - mailchimp_sf_global_msg( $msg ); + $msg = esc_html__( 'Fancy Javascript submission turned Off!', 'mailchimp' ); + admin_notice_success( $msg ); } if ( isset( $_POST['mc_use_datepicker'] ) ) { update_option( 'mc_use_datepicker', 'on' ); - $msg = '

' . esc_html__( 'Datepicker turned On!', 'mailchimp' ) . '

'; - mailchimp_sf_global_msg( $msg ); + $msg = esc_html__( 'Datepicker turned On!', 'mailchimp' ); + admin_notice_success( $msg ); } elseif ( get_option( 'mc_use_datepicker' ) !== 'off' ) { update_option( 'mc_use_datepicker', 'off' ); - $msg = '

' . esc_html__( 'Datepicker turned Off!', 'mailchimp' ) . '

'; - mailchimp_sf_global_msg( $msg ); + $msg = esc_html__( 'Datepicker turned Off!', 'mailchimp' ); + admin_notice_success( $msg ); } /*Enable double optin toggle*/ if ( isset( $_POST['mc_double_optin'] ) ) { update_option( 'mc_double_optin', true ); - $msg = '

' . esc_html__( 'Double opt-in turned On!', 'mailchimp' ) . '

'; - 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 = '

' . esc_html__( 'Double opt-in turned Off!', 'mailchimp' ) . '

'; - 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 = '

' . esc_html__( 'Mailchimp CSS turned Off!', 'mailchimp' ) . '

'; - 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 = '

' . esc_html__( 'Mailchimp CSS turned On!', 'mailchimp' ) . '

'; - 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 = '

' . esc_html__( 'Update existing subscribers turned On!' ) . '

'; - 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 = '

' . esc_html__( 'Update existing subscribers turned Off!' ) . '

'; - 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 = '

' . esc_html__( 'Unsubscribe link turned On!', 'mailchimp' ) . '

'; - 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 = '

' . esc_html__( 'Unsubscribe link turned Off!', 'mailchimp' ) . '

'; - 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'] ) ) : ''; @@ -601,8 +608,8 @@ function mailchimp_sf_save_general_form_settings() { } } - $msg = '

' . esc_html__( 'Successfully Updated your List Subscribe Form Settings!', 'mailchimp' ) . '

'; - mailchimp_sf_global_msg( $msg ); + $msg = esc_html__( 'Successfully Updated your List Subscribe Form Settings!', 'mailchimp' ); + admin_notice_success( $msg ); } /** @@ -614,8 +621,8 @@ function mailchimp_sf_change_list_if_necessary() { } if ( empty( $_POST['mc_list_id'] ) ) { - $msg = '

' . esc_html__( 'Please choose a valid list', 'mailchimp' ) . '

'; - mailchimp_sf_global_msg( $msg ); + $msg = esc_html__( 'Please choose a valid list', 'mailchimp' ); + admin_notice_error( $msg ); return; } @@ -673,16 +680,15 @@ function mailchimp_sf_change_list_if_necessary() { $igs_text .= sprintf( esc_html__( 'and %s Sets of Interest Groups', 'mailchimp' ), count( $igs ) ); } - $msg = '

' . - sprintf( - /* translators: %s: count (number) */ - __( 'Success! Loaded and saved the info for %d Merge Variables', 'mailchimp' ) . $igs_text, - count( $mv ) - ) . ' ' . - esc_html__( 'from your list' ) . ' "' . $list_name . '"

' . - esc_html__( 'Now you should either Turn On the Mailchimp Widget or change your options below, then turn it on.', 'mailchimp' ) . '

'; - - mailchimp_sf_global_msg( $msg ); + $msg = sprintf( + /* translators: %s: count (number) */ + __( 'Success! Loaded and saved the info for %d Merge Variables', 'mailchimp' ) . $igs_text, + count( $mv ) + ) . ' ' . + esc_html__( 'from your list' ) . ' "' . $list_name . '"

' . + 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 ); } } } @@ -856,7 +862,7 @@ function mailchimp_sf_signup_submit() { // Catch errors and fail early. if ( is_wp_error( $merge ) ) { $msg = '' . $merge->get_error_message() . ''; - mailchimp_sf_global_msg( $msg ); + mailchimp_sf_frontend_msg( $msg ); return false; } @@ -897,7 +903,7 @@ function mailchimp_sf_signup_submit() { ] ) ); - mailchimp_sf_global_msg( $error ); + mailchimp_sf_frontend_msg( $error ); return false; } @@ -908,7 +914,7 @@ function mailchimp_sf_signup_submit() { if ( get_option( 'mc_update_existing' ) === false && 'subscribed' === $status ) { $msg = esc_html__( 'This email address is already subscribed to the list.', 'mailchimp' ); $error = new WP_Error( 'mailchimp-update-existing', $msg ); - mailchimp_sf_global_msg( '' . $msg . '' ); + mailchimp_sf_frontend_msg( '' . $msg . '' ); return false; } @@ -918,7 +924,7 @@ function mailchimp_sf_signup_submit() { // If we have errors, then show them if ( is_wp_error( $retval ) ) { $msg = '' . $retval->get_error_message() . ''; - mailchimp_sf_global_msg( $msg ); + mailchimp_sf_frontend_msg( $msg ); return false; } @@ -930,8 +936,8 @@ function mailchimp_sf_signup_submit() { $msg = "{$esc}"; } - // Set our global message - mailchimp_sf_global_msg( $msg ); + // Set our front end success message + mailchimp_sf_frontend_msg( $msg ); return true; } diff --git a/mailchimp_widget.php b/mailchimp_widget.php index d2d129c..a5266e4 100644 --- a/mailchimp_widget.php +++ b/mailchimp_widget.php @@ -184,7 +184,7 @@ function mailchimp_sf_signup_form( $args = array() ) {
- +
{ 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. diff --git a/tests/cypress/e2e/settings.test.js b/tests/cypress/e2e/settings.test.js index 0466b10..8399234 100644 --- a/tests/cypress/e2e/settings.test.js +++ b/tests/cypress/e2e/settings.test.js @@ -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', () => { diff --git a/views/setup_page.php b/views/setup_page.php index d1f1fcd..2a3cfc7 100644 --- a/views/setup_page.php +++ b/views/setup_page.php @@ -5,6 +5,8 @@ * @package Mailchimp */ +use function Mailchimp\WordPress\Includes\Admin\admin_notice_error; + $user = get_option( 'mc_user' ); /* TODO MC SOPRESTO USER INFO */ @@ -15,14 +17,6 @@ ?>

- -
-

:

@@ -54,29 +48,19 @@ // we *could* support paging, but few users have that many lists (and shouldn't) $lists = $api->get( 'lists', 100, array( 'fields' => 'lists.id,lists.name,lists.email_type_option' ) ); if ( is_wp_error( $lists ) ) { - ?> -
- get_error_message() ) - ); - ?> -
- get_error_message() ) + ); + admin_notice_error( $msg ); } elseif ( isset( $lists['lists'] ) && count( $lists['lists'] ) === 0 ) { - ?> -
- Mailchimp" - ); - ?> -
- Mailchimp" + ); + admin_notice_error( $msg ); } else { $lists = $lists['lists']; $option = get_option( 'mc_list_id' );