Use new code style
This commit is contained in:
@ -11,9 +11,9 @@
|
||||
|
||||
namespace WP_Ultimo\Managers;
|
||||
|
||||
use \WP_Ultimo\Managers\Base_Manager;
|
||||
use \WP_Ultimo\Models\Customer;
|
||||
use \WP_Ultimo\Database\Memberships\Membership_Status;
|
||||
use WP_Ultimo\Managers\Base_Manager;
|
||||
use WP_Ultimo\Models\Customer;
|
||||
use WP_Ultimo\Database\Memberships\Membership_Status;
|
||||
|
||||
// Exit if accessed directly
|
||||
defined('ABSPATH') || exit;
|
||||
@ -25,7 +25,9 @@ defined('ABSPATH') || exit;
|
||||
*/
|
||||
class Customer_Manager extends Base_Manager {
|
||||
|
||||
use \WP_Ultimo\Apis\Rest_Api, \WP_Ultimo\Apis\WP_CLI, \WP_Ultimo\Traits\Singleton;
|
||||
use \WP_Ultimo\Apis\Rest_Api;
|
||||
use \WP_Ultimo\Apis\WP_CLI;
|
||||
use \WP_Ultimo\Traits\Singleton;
|
||||
|
||||
/**
|
||||
* The manager slug.
|
||||
@ -55,11 +57,16 @@ class Customer_Manager extends Base_Manager {
|
||||
|
||||
$this->enable_wp_cli();
|
||||
|
||||
add_action('init', function () {
|
||||
Event_Manager::register_model_events( 'customer',
|
||||
__( 'Customer', 'wp-ultimo' ),
|
||||
array( 'created', 'updated' ) );
|
||||
});
|
||||
add_action(
|
||||
'init',
|
||||
function () {
|
||||
Event_Manager::register_model_events(
|
||||
'customer',
|
||||
__('Customer', 'wp-ultimo'),
|
||||
array('created', 'updated')
|
||||
);
|
||||
}
|
||||
);
|
||||
add_action('wp_login', array($this, 'log_ip_and_last_login'), 10, 2);
|
||||
|
||||
add_filter('heartbeat_send', array($this, 'on_heartbeat_send'));
|
||||
@ -71,8 +78,7 @@ class Customer_Manager extends Base_Manager {
|
||||
add_action('wu_maybe_create_customer', array($this, 'maybe_add_to_main_site'), 10, 2);
|
||||
|
||||
add_action('wp_ajax_wu_resend_verification_email', array($this, 'handle_resend_verification_email'));
|
||||
|
||||
} // end init;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the resend verification email ajax action.
|
||||
@ -82,31 +88,26 @@ class Customer_Manager extends Base_Manager {
|
||||
*/
|
||||
public function handle_resend_verification_email() {
|
||||
|
||||
if (!check_ajax_referer('wu_resend_verification_email_nonce', false, false)) {
|
||||
|
||||
if ( ! check_ajax_referer('wu_resend_verification_email_nonce', false, false)) {
|
||||
wp_send_json_error(new \WP_Error('not-allowed', __('Error: you are not allowed to perform this action.', 'wp-ultimo')));
|
||||
|
||||
exit;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$customer = wu_get_current_customer();
|
||||
|
||||
if (!$customer) {
|
||||
|
||||
if ( ! $customer) {
|
||||
wp_send_json_error(new \WP_Error('customer-not-found', __('Error: customer not found.', 'wp-ultimo')));
|
||||
|
||||
exit;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$customer->send_verification_email();
|
||||
|
||||
wp_send_json_success();
|
||||
|
||||
exit;
|
||||
|
||||
} // end handle_resend_verification_email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle heartbeat response sent.
|
||||
@ -121,8 +122,7 @@ class Customer_Manager extends Base_Manager {
|
||||
$this->log_ip_and_last_login(wp_get_current_user());
|
||||
|
||||
return $response;
|
||||
|
||||
} // end on_heartbeat_send;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the IP address and last_login date onto the user.
|
||||
@ -134,29 +134,22 @@ class Customer_Manager extends Base_Manager {
|
||||
*/
|
||||
public function log_ip_and_last_login($user) {
|
||||
|
||||
if (!is_a($user, '\WP_User')) {
|
||||
|
||||
if ( ! is_a($user, '\WP_User')) {
|
||||
$user = get_user_by('login', $user);
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
if (!$user) {
|
||||
|
||||
if ( ! $user) {
|
||||
return;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$customer = wu_get_customer_by_user_id($user->ID);
|
||||
|
||||
if (!$customer) {
|
||||
|
||||
if ( ! $customer) {
|
||||
return;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$customer->update_last_login();
|
||||
|
||||
} // end log_ip_and_last_login;
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the change in customer verification status to take action when needed.
|
||||
@ -171,20 +164,15 @@ class Customer_Manager extends Base_Manager {
|
||||
public function transition_customer_email_verification($old_status, $new_status, $customer_id) {
|
||||
|
||||
if ($new_status !== 'pending') {
|
||||
|
||||
return;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$customer = wu_get_customer($customer_id);
|
||||
|
||||
if ($customer) {
|
||||
|
||||
$customer->send_verification_email();
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end transition_customer_email_verification;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies a customer by checking the email key.
|
||||
@ -200,72 +188,58 @@ class Customer_Manager extends Base_Manager {
|
||||
|
||||
$email_verify_key = wu_request('email-verification-key');
|
||||
|
||||
if (!$email_verify_key) {
|
||||
|
||||
if ( ! $email_verify_key) {
|
||||
return;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$customer_hash = wu_request('customer');
|
||||
|
||||
$customer_to_verify = wu_get_customer_by_hash($customer_hash);
|
||||
|
||||
if (!is_user_logged_in()) {
|
||||
|
||||
if ( ! is_user_logged_in()) {
|
||||
wp_die(
|
||||
sprintf(
|
||||
/* translators: the placeholder is the login URL */
|
||||
__('You must be authenticated in order to verify your email address. <a href=%s>Click here</a> to access your account.', 'wp-ultimo'),
|
||||
wp_login_url(add_query_arg(
|
||||
array(
|
||||
'email-verification-key' => $email_verify_key,
|
||||
'customer' => $customer_hash,
|
||||
wp_login_url(
|
||||
add_query_arg(
|
||||
array(
|
||||
'email-verification-key' => $email_verify_key,
|
||||
'customer' => $customer_hash,
|
||||
)
|
||||
)
|
||||
))
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
if (!$customer_to_verify) {
|
||||
|
||||
if ( ! $customer_to_verify) {
|
||||
wp_die(__('Invalid verification key.', 'wp-ultimo'));
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$current_customer = wu_get_current_customer();
|
||||
|
||||
if (!$current_customer) {
|
||||
|
||||
if ( ! $current_customer) {
|
||||
wp_die(__('Invalid verification key.', 'wp-ultimo'));
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
if ($current_customer->get_id() !== $customer_to_verify->get_id()) {
|
||||
|
||||
wp_die(__('Invalid verification key.', 'wp-ultimo'));
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
if ($customer_to_verify->get_email_verification() !== 'pending') {
|
||||
|
||||
wp_die(__('Invalid verification key.', 'wp-ultimo'));
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$key = $customer_to_verify->get_verification_key();
|
||||
|
||||
if (!$key) {
|
||||
|
||||
if ( ! $key) {
|
||||
wp_die(__('Invalid verification key.', 'wp-ultimo'));
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
if ($key !== $email_verify_key) {
|
||||
|
||||
wp_die(__('Invalid verification key.', 'wp-ultimo'));
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
/*
|
||||
* Uff! If we got here, we can verify the customer.
|
||||
@ -285,52 +259,44 @@ class Customer_Manager extends Base_Manager {
|
||||
* which one to manage.
|
||||
*/
|
||||
if (count($memberships) === 1) {
|
||||
|
||||
$membership = current($memberships);
|
||||
|
||||
/*
|
||||
* Only publish pending memberships
|
||||
*/
|
||||
if ($membership->get_status() === Membership_Status::PENDING) {
|
||||
|
||||
$membership->publish_pending_site_async();
|
||||
|
||||
if ($membership->get_date_trial_end() <= gmdate('Y-m-d 23:59:59')) {
|
||||
|
||||
$membership->set_status(Membership_Status::ACTIVE);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$membership->save();
|
||||
|
||||
} elseif ($membership->get_status() === Membership_Status::TRIALING) {
|
||||
|
||||
$membership->publish_pending_site_async();
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$payments = $membership->get_payments();
|
||||
|
||||
if ($payments) {
|
||||
|
||||
$redirect_url = add_query_arg(array(
|
||||
'payment' => $payments[0]->get_hash(),
|
||||
'status' => 'done',
|
||||
), wu_get_registration_url());
|
||||
$redirect_url = add_query_arg(
|
||||
array(
|
||||
'payment' => $payments[0]->get_hash(),
|
||||
'status' => 'done',
|
||||
),
|
||||
wu_get_registration_url()
|
||||
);
|
||||
|
||||
wp_redirect($redirect_url);
|
||||
|
||||
exit;
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
}
|
||||
|
||||
wp_redirect(get_admin_url($customer_to_verify->get_primary_site_id()));
|
||||
|
||||
exit;
|
||||
|
||||
} // end maybe_verify_email_address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe adds the customer to the main site.
|
||||
@ -343,24 +309,18 @@ class Customer_Manager extends Base_Manager {
|
||||
*/
|
||||
public function maybe_add_to_main_site($customer, $checkout) {
|
||||
|
||||
if (!wu_get_setting('add_users_to_main_site')) {
|
||||
|
||||
if ( ! wu_get_setting('add_users_to_main_site')) {
|
||||
return;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$user_id = $customer->get_user_id();
|
||||
|
||||
$is_already_user = is_user_member_of_blog($user_id, wu_get_main_site_id());
|
||||
|
||||
if ($is_already_user === false) {
|
||||
|
||||
$role = wu_get_setting('main_site_default_role', 'subscriber');
|
||||
|
||||
add_user_to_blog(wu_get_main_site_id(), $user_id, $role);
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end maybe_add_to_main_site;
|
||||
|
||||
} // end class Customer_Manager;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user