Skip to content

Commit

Permalink
Use oauth url from the server side.
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdharmesh committed Aug 8, 2024
1 parent 1198ebb commit 3afeb88
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 39 deletions.
34 changes: 34 additions & 0 deletions includes/class-mailchimp-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}


Expand Down Expand Up @@ -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' ),
)
);
}
}
7 changes: 3 additions & 4 deletions js/admin.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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 };
Expand All @@ -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);
Expand Down Expand Up @@ -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 = {
Expand Down
35 changes: 0 additions & 35 deletions mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
**/
Expand Down

0 comments on commit 3afeb88

Please sign in to comment.