Skip to content

Commit

Permalink
PHPCS fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdharmesh committed Aug 21, 2024
1 parent 3ec4ec7 commit 676365c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 63 deletions.
32 changes: 17 additions & 15 deletions includes/admin/templates/create-account-page.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php
/**
* Create account page template
*
* @package Mailchimp
*/

$admin_email = get_option( 'admin_email' );
$user = get_user_by( 'email', $admin_email );

if ( empty( $user ) ) {
$user_id = get_current_user_id();
$user_id = get_current_user_id();
$user = get_user_by( 'id', $user_id );
}

Expand All @@ -17,12 +19,12 @@
$api = mailchimp_sf_get_api();

if ( ! empty( $api ) ) {
$profile = $api->get('');
$profile = $api->get( '' );
$email = isset( $profile['email'] ) ? $profile['email'] : $email;
}
?>
<div class="mailchimp-sf-create-account">
<input type="hidden" name="signup_initiated" value="<?php echo esc_attr(!!$signup_initiated); ?>" />
<input type="hidden" name="signup_initiated" value="<?php echo esc_attr( (bool) $signup_initiated ); ?>" />
<div class="mailchimp-sf-create-account__header flex items-center">
<div class="flex items-center">
<svg width="44" height="44" viewBox="0 0 44 44" fill="none" xmlns="http://www.w3.org/2000/svg">
Expand All @@ -48,33 +50,33 @@
</svg>
</div>
<div class="">
<h3><?php esc_html_e( 'Mailchimp List Subscribe Form', 'mailchimp' ) ?></h3>
<h3><?php esc_html_e( 'Mailchimp List Subscribe Form', 'mailchimp' ); ?></h3>
<div class="flex items-center wizard-steps">
<div class="current"><?php echo esc_html__( 'Sign up', 'mailchimp' ) ?></div>
<div class="current"><?php echo esc_html__( 'Sign up', 'mailchimp' ); ?></div>
<span class="chevron">
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M9.05715 8.00005L6.19522 5.13812L7.13803 4.19531L10.9428 8.00005L7.13803 11.8048L6.19522 10.862L9.05715 8.00005Z" fill="#241C15" fill-opacity="0.3"/>
</svg>
</span>
<div class="deselected"><?php echo esc_html__( 'Activate account', 'mailchimp' ) ?></div>
<div class="deselected"><?php echo esc_html__( 'Activate account', 'mailchimp' ); ?></div>
<span class="chevron">
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M9.05715 8.00005L6.19522 5.13812L7.13803 4.19531L10.9428 8.00005L7.13803 11.8048L6.19522 10.862L9.05715 8.00005Z" fill="#241C15" fill-opacity="0.3"/>
</svg>
</span>
<div class="deselected"><?php echo esc_html__( 'Choose plan', 'mailchimp' ) ?></div>
<div class="deselected"><?php echo esc_html__( 'Choose plan', 'mailchimp' ); ?></div>
</div>
</div>
</div>
<div class="mailchimp-sf-create-account__body">
<div class="mailchimp-sf-create-account__body-inner">
<form class="mailchimp-sf-activate-account <?php echo esc_attr( ( $signup_initiated ) ? 'hidden' : '' ); ?>">
<div id="mailchimp-sf-profile-details" class="mailchimp-sf-create-account-step">
<div class="title"><?php esc_html_e( 'Confirm your information', 'mailchimp' ) ?></div>
<div class="title"><?php esc_html_e( 'Confirm your information', 'mailchimp' ); ?></div>
<div class="general-error">
<p class="error-message"></p>
</div>
<div class="subtitle"><?php esc_html_e( 'Profile details', 'mailchimp' ) ?></div>
<div class="subtitle"><?php esc_html_e( 'Profile details', 'mailchimp' ); ?></div>
<div class="mailchimp-sf-form-wrapper">
<fieldset>
<input id="org" name="org" type="hidden" value="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>">
Expand All @@ -83,14 +85,14 @@
<label for="first_name">
<span> <?php esc_html_e( 'First name', 'mailchimp' ); ?></span>
</label>
<input required type="text" id="first_name" name="first_name" value="<?php echo esc_attr( isset($user->first_name) ? $user->first_name : '' ); ?>"/>
<input required type="text" id="first_name" name="first_name" value="<?php echo esc_attr( isset( $user->first_name ) ? $user->first_name : '' ); ?>"/>
<p id="mailchimp-sf-first_name-error" class="error-field"></p>
</div>
<div class="box box-half">
<label for="last_name">
<span> <?php esc_html_e( 'Last name', 'mailchimp' ); ?></span>
</label>
<input required type="text" id="last_name" name="last_name" value="<?php echo esc_attr( isset($user->last_name) ? $user->last_name : '' ); ?>"/>
<input required type="text" id="last_name" name="last_name" value="<?php echo esc_attr( isset( $user->last_name ) ? $user->last_name : '' ); ?>"/>
<p id="mailchimp-sf-last_name-error" class="error-field"></p>
</div>
</div>
Expand All @@ -110,7 +112,7 @@
<span> <?php esc_html_e( 'Phone number', 'mailchimp' ); ?></span>
<span>Optional</span>
</label>
<input type="text" id="phone_number" name="phone_number" value="<?php echo esc_attr( isset($user->billing_phone) ? $user->billing_phone : '' ); ?>"/>
<input type="text" id="phone_number" name="phone_number" value="<?php echo esc_attr( isset( $user->billing_phone ) ? $user->billing_phone : '' ); ?>"/>
</div>
</div>

Expand All @@ -119,7 +121,7 @@
<label for="email">
<span> <?php esc_html_e( 'Email', 'mailchimp' ); ?></span>
</label>
<input required type="email" id="email" name="email" value="<?php echo esc_attr( isset($user->user_email) ? $user->user_email : '' ) ?>"/>
<input required type="email" id="email" name="email" value="<?php echo esc_attr( isset( $user->user_email ) ? $user->user_email : '' ); ?>"/>
<p id="mailchimp-sf-email-error" class="error-field"></p>

</div>
Expand All @@ -138,7 +140,7 @@
</div>

<div id="mailchimp-sf-business-address" class="mailchimp-sf-create-account-step">
<div class="subtitle"><?php echo esc_html__( 'Business Address', 'mailchimp' ) ?></div>
<div class="subtitle"><?php echo esc_html__( 'Business Address', 'mailchimp' ); ?></div>

<div class="mailchimp-sf-form-wrapper">
<fieldset>
Expand Down Expand Up @@ -212,7 +214,7 @@
<?php
foreach ( $timezones as $timezone ) {
?>
<option value="<?php echo esc_attr( $timezone['zone'] ); ?>" <?php selected( $timezone['zone'] === $selected_timezone, true ) ?>>
<option value="<?php echo esc_attr( $timezone['zone'] ); ?>" <?php selected( $timezone['zone'] === $selected_timezone, true ); ?>>
<?php echo esc_html( $timezone['diff_from_GMT'] . ' - ' . $timezone['zone'] ); ?>
</option>
<?php
Expand Down
102 changes: 55 additions & 47 deletions includes/class-mailchimp-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,26 +169,30 @@ public function mailchimp_create_account() {
wp_send_json_error( array( 'message' => esc_html__( 'You do not have permission to perform this action.', 'mailchimp' ) ) );
}

$data = isset( $_POST['data'] ) ? $this->sanitize_data( wp_unslash( $_POST['data'] ) ) : array();
$data = isset( $_POST['data'] ) ? $this->sanitize_data( wp_unslash( $_POST['data'] ) ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Data is sanitized in the sanitize_data method.
if ( empty( $data ) ) {
wp_send_json_error( array( 'message' => esc_html__( 'No data provided.', 'mailchimp' ) ) );
}

// Get the IP address.
if ( $_SERVER['REMOTE_ADDR'] == '::1' || $_SERVER['REMOTE_ADDR'] == '127.0.0.1' ) {
if ( isset( $_SERVER['REMOTE_ADDR'] ) && ( '::1' === $_SERVER['REMOTE_ADDR'] || '127.0.0.1' === $_SERVER['REMOTE_ADDR'] ) ) {
$data['ip_address'] = '127.0.0.1';
} elseif ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
$data['ip_address'] = sanitize_text_field( wp_unslash( $_SERVER['HTTP_CLIENT_IP'] ) );
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
$data['ip_address'] = sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) );
} else {
$data['ip_address'] = isset( $_SERVER['HTTP_CLIENT_IP'] ) ? $_SERVER['HTTP_CLIENT_IP'] : ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'] );
$data['ip_address'] = sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) );
}

$request_data = array(
'headers' => array(
'Content-type' => 'application/json',
'Accept' => 'application/json'
),
'body' => json_encode( $data ),
'timeout' => 30,
);
$request_data = array(
'headers' => array(
'Content-type' => 'application/json',
'Accept' => 'application/json',
),
'body' => wp_json_encode( $data ),
'timeout' => 30,
);

$response = wp_remote_post( $this->oauth_url . '/api/signup/', $request_data );
// Return the error if there is one.
Expand All @@ -198,7 +202,7 @@ public function mailchimp_create_account() {

$response_body = json_decode( $response['body'] );
if ( 200 === $response['response']['code'] && true === $response_body->success ) {
$result = json_decode( $response['body'], true );
$result = json_decode( $response['body'], true );

// Verify and save the token.
$verify = $this->verify_and_save_oauth_token( $result['data']['oauth_token'], $result['data']['dc'] );
Expand All @@ -210,21 +214,22 @@ public function mailchimp_create_account() {
update_option( 'mailchimp_sf_waiting_for_login', 'waiting' );
wp_send_json_success( true );

} elseif ( $response['response']['code'] == 404 ) {
} elseif ( 404 === $response['response']['code'] ) {
wp_send_json_error( array( 'success' => false ) );

} else {
$username = preg_replace( '/[^A-Za-z0-9\-\@\.]/', '', $_POST['data']['username'] );
$suggestion = wp_remote_get( $this->oauth_url . '/api/usernames/suggestions/' . $username );
$suggested_username = json_decode( $suggestion['body'] )->data;
wp_send_json_error(
array(
'success' => false,
'suggest_login' => true,
'suggested_username' => $suggested_username,
)
);
}
} else {
$username = isset( $_POST['data']['username'] ) ? sanitize_email( wp_unslash( $_POST['data']['username'] ) ) : '';
$username = preg_replace( '/[^A-Za-z0-9\-\@\.]/', '', $username );
$suggestion = wp_remote_get( $this->oauth_url . '/api/usernames/suggestions/' . $username );
$suggested_username = json_decode( $suggestion['body'] )->data;
wp_send_json_error(
array(
'success' => false,
'suggest_login' => true,
'suggested_username' => $suggested_username,
)
);
}
}

/**
Expand Down Expand Up @@ -252,14 +257,16 @@ public function check_login_session() {
}

$logged_in = ( ! empty( $profile['last_login'] ) );
if ( $logged_in ) {
if ( $logged_in ) {
delete_option( 'mailchimp_sf_waiting_for_login' );
}
wp_send_json_success(array(
'success' => true,
'logged_in' => $logged_in,
'redirect' => admin_url('admin.php?page=mailchimp_sf_options')
));
}
wp_send_json_success(
array(
'success' => true,
'logged_in' => $logged_in,
'redirect' => admin_url( 'admin.php?page=mailchimp_sf_options' ),
)
);
} else {
wp_send_json_error( array( 'success' => false ) );
}
Expand Down Expand Up @@ -411,10 +418,11 @@ public function enqueue_admin_page_scripts( $hook_suffix ) {

$data['create_account_nonce'] = wp_create_nonce( 'mailchimp_sf_create_account_nonce' );
$data['check_login_session_nonce'] = wp_create_nonce( 'mailchimp_sf_check_login_session_nonce' );
$data['required_error'] = esc_html__( '%s can\'t be blank.', 'mailchimp' );
$data['invalid_email_error'] = esc_html__( 'Insert correct email.', 'mailchimp' );
$data['confirm_email_match'] = esc_html__( 'Email confirmation must match confirmation email.', 'mailchimp' );
$data['confirm_email_match2'] = esc_html__( 'Email confirmation must match the field above.', 'mailchimp' );
/* translators: %s is field name. */
$data['required_error'] = esc_html__( '%s can\'t be blank.', 'mailchimp' );
$data['invalid_email_error'] = esc_html__( 'Insert correct email.', 'mailchimp' );
$data['confirm_email_match'] = esc_html__( 'Email confirmation must match confirmation email.', 'mailchimp' );
$data['confirm_email_match2'] = esc_html__( 'Email confirmation must match the field above.', 'mailchimp' );
}

wp_localize_script(
Expand Down Expand Up @@ -470,37 +478,37 @@ public function create_account_page() {
*
* @return array
*/
function get_timezones() {
private function get_timezones() {
$zones_array = array();
$timestamp = time();
$current = date_default_timezone_get();

foreach( timezone_identifiers_list() as $key => $zone ) {
date_default_timezone_set( $zone );
$zones_array[$key]['zone'] = $zone;
$zones_array[$key]['diff_from_GMT'] = 'UTC/GMT ' . date( 'P', $timestamp );
foreach ( timezone_identifiers_list() as $key => $zone ) {
date_default_timezone_set( $zone ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
$zones_array[ $key ]['zone'] = $zone;
$zones_array[ $key ]['diff_from_GMT'] = 'UTC/GMT ' . date( 'P', $timestamp ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
}

date_default_timezone_set( $current );
date_default_timezone_set( $current ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set

return $zones_array;
}

/**
* Get the current timezone from wordpress settings.
* Get the current timezone from WordPress settings.
*
* @return mixed|string|void
*/
function get_current_timezone() {
private function get_current_timezone() {
// Get timezone data from options.
$timezone_string = get_option( 'timezone_string' );
$offset = get_option( 'gmt_offset' );

$signal = ($offset <=> 0 ) < 0 ? "-" : "+";
$offset = sprintf('%1s%02d:%02d', $signal, abs((int) $offset), abs(fmod($offset, 1) * 60));
$signal = ( $offset <=> 0 ) < 0 ? '-' : '+';
$offset = sprintf( '%1s%02d:%02d', $signal, abs( (int) $offset ), abs( fmod( $offset, 1 ) * 60 ) );

$timezone = $offset;
if ($timezone_string) {
if ( $timezone_string ) {
$timezone = $timezone_string;
}

Expand Down
2 changes: 1 addition & 1 deletion views/setup_page.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<div class="mailchimp-sf-oauth-connect-wrapper">
<span class="spinner"></span>
<button class="button button-primary" id="mailchimp_sf_oauth_connect"><?php esc_html_e( 'Connect Account', 'mailchimp' ); ?></button>
<a class="button button-secondary" id="mailchimp_sf_create_account_button" href="<?php echo esc_url( admin_url('admin.php?page=mailchimp_sf_create_account') ); ?>"><?php esc_html_e( 'Create account', 'mailchimp' ); ?></a>
<a class="button button-secondary" id="mailchimp_sf_create_account_button" href="<?php echo esc_url( admin_url( 'admin.php?page=mailchimp_sf_create_account' ) ); ?>"><?php esc_html_e( 'Create account', 'mailchimp' ); ?></a>
</div>
</td>
</tr>
Expand Down

0 comments on commit 676365c

Please sign in to comment.