From b6c7b0b1a2dde1f3319233e61f9cddc8e7469d18 Mon Sep 17 00:00:00 2001 From: Max Garceau Date: Thu, 19 Dec 2024 15:06:12 -0500 Subject: [PATCH 01/19] Add WP admin notices to plugin settings page --- mailchimp.php | 113 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 38 deletions(-) diff --git a/mailchimp.php b/mailchimp.php index 34464dc..da48e77 100644 --- a/mailchimp.php +++ b/mailchimp.php @@ -426,6 +426,44 @@ function mailchimp_sf_global_msg( $msg = null ) { return true; } +/** + * 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 + */ +function mailchimp_sf_admin_notice_success( string $msg ): void { + ?> +
+

+
+ +
+

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

'; - 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'] ) ) : ''; @@ -577,8 +615,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' ); + mailchimp_sf_admin_notice_success( $msg ); } /** @@ -590,8 +628,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' ); + mailchimp_sf_admin_notice_error( $msg ); return; } @@ -649,16 +687,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' ); + + mailchimp_sf_admin_notice_success( $msg ); } } } From 8382b91cc3f859bc8b98c5afb5e069ddb2645a79 Mon Sep 17 00:00:00 2001 From: Max Garceau Date: Thu, 19 Dec 2024 15:22:04 -0500 Subject: [PATCH 02/19] Move admin-notices to separate file Lint project and fix errors --- includes/admin/admin-notices.php | 48 +++++++++++++++++++++++++++ includes/class-mailchimp-admin.php | 20 +++++++++++ mailchimp.php | 53 +++++------------------------- 3 files changed, 77 insertions(+), 44 deletions(-) create mode 100644 includes/admin/admin-notices.php diff --git a/includes/admin/admin-notices.php b/includes/admin/admin-notices.php new file mode 100644 index 0000000..e4fedc0 --- /dev/null +++ b/includes/admin/admin-notices.php @@ -0,0 +1,48 @@ + +
+

+
+ +
+

+
+ 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 da48e77..088fad1 100644 --- a/mailchimp.php +++ b/mailchimp.php @@ -405,6 +405,9 @@ function mailchimp_sf_delete_setup() { /** * Gets or sets a global message based on parameter passed to it * + * On the plugin settings page, WP admin notices are displayed + * instead of the global message. + * * @param mixed $msg Message * @return string/bool depending on get/set */ @@ -426,44 +429,6 @@ function mailchimp_sf_global_msg( $msg = null ) { return true; } -/** - * 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 - */ -function mailchimp_sf_admin_notice_success( string $msg ): void { - ?> -
-

-
- -
-

-
- 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' ); + /* 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_admin_notice_success( $msg ); } From 135094c113f9f4b7ef9d1af5bd3bb570d46acbac Mon Sep 17 00:00:00 2001 From: Max Garceau Date: Thu, 19 Dec 2024 15:43:35 -0500 Subject: [PATCH 03/19] Add wp_kses to allow links for admin notices --- includes/admin/admin-notices.php | 34 ++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/includes/admin/admin-notices.php b/includes/admin/admin-notices.php index e4fedc0..d5ac0bf 100644 --- a/includes/admin/admin-notices.php +++ b/includes/admin/admin-notices.php @@ -21,7 +21,22 @@ function mailchimp_sf_admin_notice_success( string $msg ): void { ?>
-

+

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

-

+

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

Date: Thu, 19 Dec 2024 15:44:15 -0500 Subject: [PATCH 04/19] Use admin notices for MC list errors --- views/setup_page.php | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/views/setup_page.php b/views/setup_page.php index b2e64d4..539066f 100644 --- a/views/setup_page.php +++ b/views/setup_page.php @@ -53,29 +53,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() ) + ); + mailchimp_sf_admin_notice_error( $msg ); } elseif ( isset( $lists['lists'] ) && count( $lists['lists'] ) === 0 ) { - ?> -
- Mailchimp" - ); - ?> -
- Mailchimp" + ); + mailchimp_sf_admin_notice_error( $msg ); } else { $lists = $lists['lists']; $option = get_option( 'mc_list_id' ); From 9c023697f8418640ca7d29d8054b6fd613027864 Mon Sep 17 00:00:00 2001 From: Max Garceau Date: Thu, 19 Dec 2024 15:46:44 -0500 Subject: [PATCH 05/19] Remove global message output from settings page Only admin notices should be displayed here --- views/setup_page.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/views/setup_page.php b/views/setup_page.php index 539066f..524e38f 100644 --- a/views/setup_page.php +++ b/views/setup_page.php @@ -14,14 +14,6 @@ ?>

- -
-

:

From d30bfa38f342e3c506304ab9e3895f6daba3cdfb Mon Sep 17 00:00:00 2001 From: Max Garceau Date: Thu, 19 Dec 2024 15:49:51 -0500 Subject: [PATCH 06/19] Rename global message to frontend message This better explains the functions purpose and will prevent confusion in the future. Updated mailchimp_sf_frontend_msg comments to explain how WP admin notices are used in the WP admin --- mailchimp.php | 22 ++++++++++++---------- mailchimp_widget.php | 2 +- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/mailchimp.php b/mailchimp.php index 088fad1..8a20abc 100644 --- a/mailchimp.php +++ b/mailchimp.php @@ -281,7 +281,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; } } @@ -403,15 +403,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 * - * On the plugin settings page, WP admin notices are displayed - * instead of the global message. + * 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 @@ -834,7 +836,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; } @@ -875,7 +877,7 @@ function mailchimp_sf_signup_submit() { ] ) ); - mailchimp_sf_global_msg( $error ); + mailchimp_sf_frontend_msg( $error ); return false; } @@ -887,7 +889,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( '' . $msg . '' ); + mailchimp_sf_frontend_msg( '' . $msg . '' ); return false; } @@ -900,7 +902,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; } @@ -913,7 +915,7 @@ function mailchimp_sf_signup_submit() { } // Set our global message - mailchimp_sf_global_msg( $msg ); + 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() ) {
- +
Date: Thu, 19 Dec 2024 16:29:41 -0500 Subject: [PATCH 07/19] Fix typo --- tests/cypress/e2e/admin.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cypress/e2e/admin.test.js b/tests/cypress/e2e/admin.test.js index e7ba13c..0686749 100644 --- a/tests/cypress/e2e/admin.test.js +++ b/tests/cypress/e2e/admin.test.js @@ -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. From 222e6db74821c6f76baf31fded1c9e26e17db738 Mon Sep 17 00:00:00 2001 From: Max Garceau Date: Thu, 19 Dec 2024 16:31:37 -0500 Subject: [PATCH 08/19] Remove void return type to maintain compatibility with php 7.0 --- includes/admin/admin-notices.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/admin/admin-notices.php b/includes/admin/admin-notices.php index d5ac0bf..f325333 100644 --- a/includes/admin/admin-notices.php +++ b/includes/admin/admin-notices.php @@ -18,7 +18,7 @@ * @param string $msg The message to display. * @return void */ -function mailchimp_sf_admin_notice_success( string $msg ): void { +function mailchimp_sf_admin_notice_success( string $msg ) { ?>

@@ -54,7 +54,7 @@ function mailchimp_sf_admin_notice_success( string $msg ): void { * @param string $msg The message to display. * @return void */ -function mailchimp_sf_admin_notice_error( string $msg ): void { +function mailchimp_sf_admin_notice_error( string $msg ) { ?>

From 90b0289c8d26bd5fb452665d5066748f2582b02e Mon Sep 17 00:00:00 2001 From: Max Garceau Date: Thu, 19 Dec 2024 16:38:27 -0500 Subject: [PATCH 09/19] Update comment --- mailchimp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mailchimp.php b/mailchimp.php index 8a20abc..b89dce5 100644 --- a/mailchimp.php +++ b/mailchimp.php @@ -914,7 +914,7 @@ function mailchimp_sf_signup_submit() { $msg = "{$esc}"; } - // Set our global message + // Set our front end success message mailchimp_sf_frontend_msg( $msg ); return true; From 8db2531b15dbb15a57f2b1a78a0b399905033b2c Mon Sep 17 00:00:00 2001 From: Max Garceau Date: Thu, 19 Dec 2024 16:46:08 -0500 Subject: [PATCH 10/19] Update success message selectors in settings.test.js --- tests/cypress/e2e/settings.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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', () => { From 9652226e10fb06f93143df540c82a4955e162e2e Mon Sep 17 00:00:00 2001 From: MaxwellGarceau Date: Thu, 26 Dec 2024 14:05:40 -0500 Subject: [PATCH 11/19] Namespace admin notice functions --- includes/admin/admin-notices.php | 7 +++++-- mailchimp.php | 32 +++++++++++++++++--------------- views/setup_page.php | 6 ++++-- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/includes/admin/admin-notices.php b/includes/admin/admin-notices.php index f325333..b6a630d 100644 --- a/includes/admin/admin-notices.php +++ b/includes/admin/admin-notices.php @@ -1,4 +1,7 @@

@@ -54,7 +57,7 @@ function mailchimp_sf_admin_notice_success( string $msg ) { * @param string $msg The message to display. * @return void */ -function mailchimp_sf_admin_notice_error( string $msg ) { +function admin_notice_error( string $msg ) { ?>

diff --git a/mailchimp.php b/mailchimp.php index b89dce5..bc3af16 100644 --- a/mailchimp.php +++ b/mailchimp.php @@ -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' ); @@ -468,64 +470,64 @@ function mailchimp_sf_save_general_form_settings() { if ( isset( $_POST['mc_use_javascript'] ) ) { update_option( 'mc_use_javascript', 'on' ); $msg = esc_html__( 'Fancy Javascript submission turned On!', 'mailchimp' ); - mailchimp_sf_admin_notice_success( $msg ); + 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_admin_notice_success( $msg ); + 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_admin_notice_success( $msg ); + 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_admin_notice_success( $msg ); + 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_admin_notice_success( $msg ); + 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_admin_notice_success( $msg ); + 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_admin_notice_success( $msg ); + 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_admin_notice_success( $msg ); + 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_admin_notice_success( $msg ); + 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_admin_notice_success( $msg ); + 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_admin_notice_success( $msg ); + 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_admin_notice_success( $msg ); + admin_notice_success( $msg ); } $content = isset( $_POST['mc_header_content'] ) ? wp_kses_post( wp_unslash( $_POST['mc_header_content'] ) ) : ''; @@ -583,7 +585,7 @@ function mailchimp_sf_save_general_form_settings() { } $msg = esc_html__( 'Successfully Updated your List Subscribe Form Settings!', 'mailchimp' ); - mailchimp_sf_admin_notice_success( $msg ); + admin_notice_success( $msg ); } /** @@ -596,7 +598,7 @@ function mailchimp_sf_change_list_if_necessary() { if ( empty( $_POST['mc_list_id'] ) ) { $msg = esc_html__( 'Please choose a valid list', 'mailchimp' ); - mailchimp_sf_admin_notice_error( $msg ); + admin_notice_error( $msg ); return; } @@ -662,7 +664,7 @@ function mailchimp_sf_change_list_if_necessary() { 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_admin_notice_success( $msg ); + admin_notice_success( $msg ); } } } diff --git a/views/setup_page.php b/views/setup_page.php index 524e38f..c146261 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' ); // If we have an API Key, see if we need to change the lists and its options @@ -50,14 +52,14 @@ esc_html__( 'Uh-oh, we couldn\'t get your lists from Mailchimp! Error: %s', 'mailchimp' ), esc_html( $lists->get_error_message() ) ); - mailchimp_sf_admin_notice_error( $msg ); + admin_notice_error( $msg ); } elseif ( isset( $lists['lists'] ) && count( $lists['lists'] ) === 0 ) { $msg = sprintf( /* translators: %s: link to Mailchimp */ esc_html__( 'Uh-oh, you don\'t have any lists defined! Please visit %s, login, and setup a list before using this tool!', 'mailchimp' ), "Mailchimp" ); - mailchimp_sf_admin_notice_error( $msg ); + admin_notice_error( $msg ); } else { $lists = $lists['lists']; $option = get_option( 'mc_list_id' ); From 65ecf2bca31acf7cbf38018f716cbe334641976a Mon Sep 17 00:00:00 2001 From: MaxwellGarceau Date: Thu, 26 Dec 2024 14:13:50 -0500 Subject: [PATCH 12/19] Add autoloading via composer PSR-4 --- .github/workflows/build-release-zip.yml | 1 + composer.json | 3 +++ includes/class-mailchimp-admin.php | 14 ++++++++------ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build-release-zip.yml b/.github/workflows/build-release-zip.yml index 597a51b..73c7ca1 100644 --- a/.github/workflows/build-release-zip.yml +++ b/.github/workflows/build-release-zip.yml @@ -43,6 +43,7 @@ jobs: - name: Build plugin run: | npm run build + composer dump-autoload - name: Install SVN run: | diff --git a/composer.json b/composer.json index 9965490..f05059f 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,9 @@ }, "classmap": [ "includes" + ], + "files": [ + "includes/admin/admin-notices.php" ] }, "config": { diff --git a/includes/class-mailchimp-admin.php b/includes/class-mailchimp-admin.php index 40c2198..d164ff2 100644 --- a/includes/class-mailchimp-admin.php +++ b/includes/class-mailchimp-admin.php @@ -12,6 +12,14 @@ /** * Class Mailchimp_Admin + * + * 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.6.0 */ @@ -38,9 +46,6 @@ 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? - $this->require_admin_utils(); } /** @@ -56,9 +61,6 @@ public function init() { * * @since 1.7.0 */ - private function require_admin_utils() { - include_once MCSF_DIR . 'includes/admin/admin-notices.php'; - } /** * Start the OAuth process. From d6bd14b39c749d2157161bde782861279a0c1460 Mon Sep 17 00:00:00 2001 From: MaxwellGarceau Date: Thu, 26 Dec 2024 14:18:03 -0500 Subject: [PATCH 13/19] Fix lint errors --- includes/admin/admin-notices.php | 5 ++--- includes/class-mailchimp-admin.php | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/includes/admin/admin-notices.php b/includes/admin/admin-notices.php index b6a630d..6307c52 100644 --- a/includes/admin/admin-notices.php +++ b/includes/admin/admin-notices.php @@ -1,13 +1,12 @@ Date: Thu, 26 Dec 2024 14:49:00 -0500 Subject: [PATCH 14/19] Update build workflows to install composer dependencies --- .github/workflows/build-release-zip.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build-release-zip.yml b/.github/workflows/build-release-zip.yml index 73c7ca1..597a51b 100644 --- a/.github/workflows/build-release-zip.yml +++ b/.github/workflows/build-release-zip.yml @@ -43,7 +43,6 @@ jobs: - name: Build plugin run: | npm run build - composer dump-autoload - name: Install SVN run: | From 1e5a9faed0c7a891767d6960d98a874deb976f39 Mon Sep 17 00:00:00 2001 From: MaxwellGarceau Date: Thu, 26 Dec 2024 15:49:14 -0500 Subject: [PATCH 15/19] Revert autoload commits --- .github/workflows/build-release-zip.yml | 5 ----- .github/workflows/wordpress-plugin-deploy.yml | 5 ----- composer.json | 11 ----------- includes/admin/admin-notices.php | 5 +++-- includes/class-mailchimp-admin.php | 14 ++++++-------- 5 files changed, 9 insertions(+), 31 deletions(-) diff --git a/.github/workflows/build-release-zip.yml b/.github/workflows/build-release-zip.yml index 597a51b..99cc507 100644 --- a/.github/workflows/build-release-zip.yml +++ b/.github/workflows/build-release-zip.yml @@ -35,11 +35,6 @@ jobs: if: steps.cache-node-modules.outputs.cache-hit != 'true' run: npm ci --no-optional - - name: Install Composer dependencies and dump autoload - run: | - composer install --no-dev --optimize-autoloader - composer dump-autoload - - name: Build plugin run: | npm run build diff --git a/.github/workflows/wordpress-plugin-deploy.yml b/.github/workflows/wordpress-plugin-deploy.yml index 903301a..6f67692 100644 --- a/.github/workflows/wordpress-plugin-deploy.yml +++ b/.github/workflows/wordpress-plugin-deploy.yml @@ -20,11 +20,6 @@ jobs: node-version-file: '.nvmrc' cache: 'npm' - - name: Install Composer dependencies and dump autoload - run: | - composer install --no-dev --optimize-autoloader - composer dump-autoload - - name: Build run: | npm ci --no-optional diff --git a/composer.json b/composer.json index f05059f..6ab523d 100644 --- a/composer.json +++ b/composer.json @@ -18,17 +18,6 @@ "10up/phpcs-composer": "^3.0", "sirbrillig/phpcs-changed": "^2.11" }, - "autoload": { - "psr-4": { - "Mailchimp\\WordPress\\": "src/" - }, - "classmap": [ - "includes" - ], - "files": [ - "includes/admin/admin-notices.php" - ] - }, "config": { "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true diff --git a/includes/admin/admin-notices.php b/includes/admin/admin-notices.php index 6307c52..b6a630d 100644 --- a/includes/admin/admin-notices.php +++ b/includes/admin/admin-notices.php @@ -1,12 +1,13 @@ require_admin_utils(); } /** @@ -61,6 +56,9 @@ public function init() { * * @since 1.7.0 */ + private function require_admin_utils() { + include_once MCSF_DIR . 'includes/admin/admin-notices.php'; + } /** * Start the OAuth process. From 7fce632cd49ad689163ac96fb36f82444355afd8 Mon Sep 17 00:00:00 2001 From: MaxwellGarceau Date: Thu, 26 Dec 2024 15:52:57 -0500 Subject: [PATCH 16/19] Change todo note to info note --- includes/class-mailchimp-admin.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/includes/class-mailchimp-admin.php b/includes/class-mailchimp-admin.php index 40c2198..d75b9a1 100644 --- a/includes/class-mailchimp-admin.php +++ b/includes/class-mailchimp-admin.php @@ -39,7 +39,9 @@ public function init() { 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? + /** + * Load modularized admin functionality here + */ $this->require_admin_utils(); } From 7aeb0424c8bf5f654bbbaa66bf5aaefc4ecd82d4 Mon Sep 17 00:00:00 2001 From: MaxwellGarceau Date: Thu, 26 Dec 2024 16:00:00 -0500 Subject: [PATCH 17/19] Fix lint issue --- includes/admin/admin-notices.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/includes/admin/admin-notices.php b/includes/admin/admin-notices.php index b6a630d..6307c52 100644 --- a/includes/admin/admin-notices.php +++ b/includes/admin/admin-notices.php @@ -1,13 +1,12 @@ Date: Mon, 3 Feb 2025 11:09:23 -0600 Subject: [PATCH 18/19] Load admin functions with autoload Set up the structure for classmaps and autoload for future development. --- composer.json | 11 +++++++++++ includes/class-mailchimp-admin.php | 30 ++++++++---------------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/composer.json b/composer.json index 6ab523d..0da82f6 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,17 @@ "10up/phpcs-composer": "^3.0", "sirbrillig/phpcs-changed": "^2.11" }, + "autoload": { + "psr-4": { + "Mailchimp\\WordPress\\": "src/" + }, + "classmap": [ + "includes/" + ], + "files": [ + "includes/admin/admin-notices.php" + ] + }, "config": { "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true diff --git a/includes/class-mailchimp-admin.php b/includes/class-mailchimp-admin.php index d75b9a1..d69b9b8 100644 --- a/includes/class-mailchimp-admin.php +++ b/includes/class-mailchimp-admin.php @@ -1,6 +1,14 @@ 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'; } /** From 8d48fafa103dc903ba6a066690661cbd5a41878f Mon Sep 17 00:00:00 2001 From: MaxwellGarceau Date: Mon, 3 Feb 2025 11:18:34 -0600 Subject: [PATCH 19/19] Add back the build steps for composer autoload --- .github/workflows/build-release-zip.yml | 5 +++++ .github/workflows/wordpress-plugin-deploy.yml | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/.github/workflows/build-release-zip.yml b/.github/workflows/build-release-zip.yml index 99cc507..597a51b 100644 --- a/.github/workflows/build-release-zip.yml +++ b/.github/workflows/build-release-zip.yml @@ -35,6 +35,11 @@ jobs: if: steps.cache-node-modules.outputs.cache-hit != 'true' run: npm ci --no-optional + - name: Install Composer dependencies and dump autoload + run: | + composer install --no-dev --optimize-autoloader + composer dump-autoload + - name: Build plugin run: | npm run build diff --git a/.github/workflows/wordpress-plugin-deploy.yml b/.github/workflows/wordpress-plugin-deploy.yml index 6f67692..903301a 100644 --- a/.github/workflows/wordpress-plugin-deploy.yml +++ b/.github/workflows/wordpress-plugin-deploy.yml @@ -20,6 +20,11 @@ jobs: node-version-file: '.nvmrc' cache: 'npm' + - name: Install Composer dependencies and dump autoload + run: | + composer install --no-dev --optimize-autoloader + composer dump-autoload + - name: Build run: | npm ci --no-optional