Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/update success error messages to wp admin notices #85

Open
wants to merge 18 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
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
78 changes: 78 additions & 0 deletions includes/admin/admin-notices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
MaxwellGarceau marked this conversation as resolved.
Show resolved Hide resolved
/**
* Admin notices.
*
* @package Mailchimp
*/

/**
* 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 mailchimp_sf_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(),
)
);
?>
</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 mailchimp_sf_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
}
20 changes: 20 additions & 0 deletions includes/class-mailchimp-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ public function init() {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_page_scripts' ) );
add_action( 'admin_menu', array( $this, 'add_create_account_page' ) );
add_filter( 'admin_footer_text', array( $this, 'admin_footer_text' ) );

// TODO: Should this load somewhere else?
MaxwellGarceau marked this conversation as resolved.
Show resolved Hide resolved
$this->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';
MaxwellGarceau marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
98 changes: 51 additions & 47 deletions mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,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() );
dkotter marked this conversation as resolved.
Show resolved Hide resolved
exit;
}
}
Expand Down Expand Up @@ -296,7 +296,7 @@ function mailchimp_sf_migrate_sopresto() {
$api = new MailChimp_API( $key->response );
} catch ( Exception $e ) {
$msg = '<strong class="mc_error_msg">' . $e->getMessage() . '</strong>';
mailchimp_sf_global_msg( $msg );
mailchimp_sf_frontend_msg( $msg );
MaxwellGarceau marked this conversation as resolved.
Show resolved Hide resolved
return;
}

Expand Down Expand Up @@ -427,12 +427,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 Down Expand Up @@ -486,65 +491,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 = '<p class="success_msg">' . esc_html__( 'Fancy Javascript submission turned On!', 'mailchimp' ) . '</p>';
mailchimp_sf_global_msg( $msg );
$msg = esc_html__( 'Fancy Javascript submission turned On!', 'mailchimp' );
mailchimp_sf_admin_notice_success( $msg );
} elseif ( get_option( 'mc_use_javascript' ) !== 'off' ) {
update_option( 'mc_use_javascript', 'off' );
$msg = '<p class="success_msg">' . esc_html__( 'Fancy Javascript submission turned Off!', 'mailchimp' ) . '</p>';
mailchimp_sf_global_msg( $msg );
$msg = esc_html__( 'Fancy Javascript submission turned Off!', 'mailchimp' );
mailchimp_sf_admin_notice_success( $msg );
}

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

/*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' );
mailchimp_sf_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' );
mailchimp_sf_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' );
mailchimp_sf_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' );
mailchimp_sf_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!' );
mailchimp_sf_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!' );
mailchimp_sf_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' );
mailchimp_sf_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' );
mailchimp_sf_admin_notice_success( $msg );
}

$content = isset( $_POST['mc_header_content'] ) ? wp_kses_post( wp_unslash( $_POST['mc_header_content'] ) ) : '';
Expand Down Expand Up @@ -601,8 +606,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' );
mailchimp_sf_admin_notice_success( $msg );
}

/**
Expand All @@ -614,8 +619,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' );
mailchimp_sf_admin_notice_error( $msg );
return;
}

Expand Down Expand Up @@ -673,16 +678,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' );

mailchimp_sf_admin_notice_success( $msg );
}
}
}
Expand Down Expand Up @@ -856,7 +860,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 @@ -897,7 +901,7 @@ function mailchimp_sf_signup_submit() {
]
)
);
mailchimp_sf_global_msg( $error );
mailchimp_sf_frontend_msg( $error );
return false;
}

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

Expand All @@ -918,7 +922,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 @@ -931,7 +935,7 @@ function mailchimp_sf_signup_submit() {
}

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

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion mailchimp_widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ function mailchimp_sf_signup_form( $args = array() ) {
<div class="mc_form_inside">

<div class="updated" id="mc_message">
<?php echo wp_kses_post( mailchimp_sf_global_msg() ); ?>
<?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
Loading
Loading