From 3afeb8878443856b1c9b7641760ddc8a7fd08ef1 Mon Sep 17 00:00:00 2001 From: Dharmesh Patel Date: Thu, 8 Aug 2024 11:44:00 +0530 Subject: [PATCH] Use oauth url from the server side. --- includes/class-mailchimp-admin.php | 34 +++++++++++++++++++++++++++++ js/admin.js | 7 +++--- mailchimp.php | 35 ------------------------------ 3 files changed, 37 insertions(+), 39 deletions(-) diff --git a/includes/class-mailchimp-admin.php b/includes/class-mailchimp-admin.php index dc3a201..f0e7d4d 100644 --- a/includes/class-mailchimp-admin.php +++ b/includes/class-mailchimp-admin.php @@ -32,6 +32,8 @@ public function init() { add_action( 'admin_notices', array( $this, 'admin_notices' ) ); add_action( 'wp_ajax_mailchimp_sf_oauth_start', array( $this, 'start_oauth_process' ) ); add_action( 'wp_ajax_mailchimp_sf_oauth_finish', array( $this, 'finish_oauth_process' ) ); + + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_page_scripts' ) ); } @@ -233,4 +235,36 @@ public function sanitize_data( $data ) { return is_scalar( $data ) ? sanitize_text_field( $data ) : $data; } } + + /** + * Enqueue scripts/styles for the Mailchimp admin page + * + * @param string $hook_suffix The current admin page. + * @return void + */ + public function enqueue_admin_page_scripts( $hook_suffix ) { + if ( 'toplevel_page_mailchimp_sf_options' !== $hook_suffix ) { + return; + } + + wp_enqueue_style( 'mailchimp_sf_admin_css', MCSF_URL . 'css/admin.css', array( 'wp-jquery-ui-dialog' ), true ); + wp_enqueue_script( 'showMe', MCSF_URL . 'js/hidecss.js', array( 'jquery' ), MCSF_VER, true ); + wp_enqueue_script( 'mailchimp_sf_admin', MCSF_URL . 'js/admin.js', array( 'jquery', 'jquery-ui-dialog' ), MCSF_VER, true ); + + wp_localize_script( + 'mailchimp_sf_admin', + 'mailchimp_sf_admin_params', + array( + 'ajax_url' => esc_url( admin_url( 'admin-ajax.php' ) ), + 'oauth_url' => esc_url( $this->oauth_url ), + 'oauth_start_nonce' => wp_create_nonce( 'mailchimp_sf_oauth_start_nonce' ), + 'oauth_finish_nonce' => wp_create_nonce( 'mailchimp_sf_oauth_finish_nonce' ), + 'oauth_window_name' => esc_html__( 'Mailchimp For WordPress OAuth', 'mailchimp' ), + 'generic_error' => esc_html__( 'An error occurred. Please try again.', 'mailchimp' ), + 'modal_title' => esc_html__( 'Login Popup is blocked!', 'mailchimp' ), + 'modal_button_try_again' => esc_html__( 'Try again', 'mailchimp' ), + 'modal_button_cancel' => esc_html__( 'No, cancel!', 'mailchimp' ), + ) + ); + } } diff --git a/js/admin.js b/js/admin.js index b33e9da..6e24563 100644 --- a/js/admin.js +++ b/js/admin.js @@ -1,7 +1,6 @@ /* eslint-disable prefer-template, no-console */ (function ($) { const params = window.mailchimp_sf_admin_params || {}; - const oauthBaseUrl = 'https://woocommerce.mailchimpapp.com'; const spinner = '.mailchimp-sf-oauth-connect-wrapper .spinner'; const errorSelector = '.mailchimp-sf-oauth-section .oauth-error'; @@ -11,7 +10,7 @@ * @param {string} token - Token from the Oauth service. */ function openMailchimpOauthPopup(token) { - const startUrl = oauthBaseUrl + '/auth/start/' + token; + const startUrl = params.oauth_url + '/auth/start/' + token; const width = 800; const height = 600; const screenSizes = window.screen || { width: 1024, height: 768 }; @@ -27,7 +26,7 @@ ', left=' + left + ', domain=' + - oauthBaseUrl.replace('https://', ''); + params.oauth_url.replace('https://', ''); // Open Mailchimp OAuth popup. const popup = window.open(startUrl, params.oauth_window_name, windowOptions); @@ -67,7 +66,7 @@ window.clearInterval(oauthInterval); // Check status of OAuth connection. - const statusUrl = oauthBaseUrl + '/api/status/' + token; + const statusUrl = params.oauth_url + '/api/status/' + token; $.post(statusUrl, function (statusData) { if (statusData && statusData.status === 'accepted') { const finishData = { diff --git a/mailchimp.php b/mailchimp.php index de7d340..166ae43 100644 --- a/mailchimp.php +++ b/mailchimp.php @@ -140,41 +140,6 @@ function mailchimp_sf_load_resources() { } } - -/** - * Loads resources for the Mailchimp admin page - * - * @param string $hook_suffix The current admin page. - * @return void - */ -function mailchimp_admin_page_scripts( $hook_suffix ) { - if ( 'toplevel_page_mailchimp_sf_options' !== $hook_suffix ) { - return; - } - - wp_enqueue_style( 'mailchimp_sf_admin_css', MCSF_URL . 'css/admin.css', array( 'wp-jquery-ui-dialog' ), true ); - wp_enqueue_script( 'showMe', MCSF_URL . 'js/hidecss.js', array( 'jquery' ), MCSF_VER, true ); - wp_enqueue_script( 'mailchimp_sf_admin', MCSF_URL . 'js/admin.js', array( 'jquery', 'jquery-ui-dialog' ), MCSF_VER, true ); - - wp_localize_script( - 'mailchimp_sf_admin', - 'mailchimp_sf_admin_params', - array( - 'ajax_url' => esc_url( admin_url( 'admin-ajax.php' ) ), - 'oauth_start_nonce' => wp_create_nonce( 'mailchimp_sf_oauth_start_nonce' ), - 'oauth_finish_nonce' => wp_create_nonce( 'mailchimp_sf_oauth_finish_nonce' ), - 'oauth_window_name' => esc_html__( 'Mailchimp For WordPress OAuth', 'mailchimp' ), - 'generic_error' => esc_html__( 'An error occurred. Please try again.', 'mailchimp' ), - 'modal_title' => esc_html__( 'Login Popup is blocked!', 'mailchimp' ), - 'modal_button_try_again' => esc_html__( 'Try again', 'mailchimp' ), - 'modal_button_cancel' => esc_html__( 'No, cancel!', 'mailchimp' ), - ) - ); -} - -add_action( 'admin_enqueue_scripts', 'mailchimp_admin_page_scripts', 10, 1 ); - - /** * Loads jQuery Datepicker for the date-pick class **/