| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Ajax; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
use StoreEngine\Classes\AbstractAjaxHandler; |
| 10 |
use StoreEngine\Classes\AccountMover; |
| 11 |
use StoreEngine\Classes\Customer; |
| 12 |
use StoreEngine\Utils\Helper; |
| 13 |
|
| 14 |
class Customers extends AbstractAjaxHandler { |
| 15 |
public function __construct() { |
| 16 |
$this->actions = [ |
| 17 |
'create_new_customer' => [ |
| 18 |
'capability' => 'manage_options', |
| 19 |
'callback' => [ $this, 'create_new_customer' ], |
| 20 |
'fields' => [ |
| 21 |
'first_name' => 'string', |
| 22 |
'last_name' => 'string', |
| 23 |
'email' => 'email', |
| 24 |
'password' => 'string', |
| 25 |
'address_1' => 'string', |
| 26 |
'address_2' => 'string', |
| 27 |
'city' => 'string', |
| 28 |
'state' => 'string', |
| 29 |
'postcode' => 'string', |
| 30 |
'country' => 'string', |
| 31 |
'phone' => 'string', |
| 32 |
], |
| 33 |
], |
| 34 |
'top_customers' => [ |
| 35 |
'capability' => 'manage_options', |
| 36 |
'callback' => [ $this, 'top_customers' ], |
| 37 |
], |
| 38 |
'preview_account_move' => [ |
| 39 |
'capability' => 'manage_options', |
| 40 |
'callback' => [ $this, 'preview_account_move' ], |
| 41 |
'fields' => [ |
| 42 |
'from' => 'absint', |
| 43 |
'to' => 'absint', |
| 44 |
], |
| 45 |
], |
| 46 |
'move_account' => [ |
| 47 |
'capability' => 'manage_options', |
| 48 |
'callback' => [ $this, 'move_account' ], |
| 49 |
'fields' => [ |
| 50 |
'from' => 'absint', |
| 51 |
'to' => 'absint', |
| 52 |
], |
| 53 |
], |
| 54 |
]; |
| 55 |
} |
| 56 |
|
| 57 |
public function preview_account_move( $payload ) { |
| 58 |
$mover = new AccountMover( (int) $payload['from'], (int) $payload['to'] ); |
| 59 |
$preview = $mover->preview(); |
| 60 |
|
| 61 |
if ( is_wp_error( $preview ) ) { |
| 62 |
wp_send_json_error( $preview->get_error_message() ); |
| 63 |
} |
| 64 |
|
| 65 |
wp_send_json_success( [ |
| 66 |
'counts' => $preview, |
| 67 |
'total' => array_sum( $preview ), |
| 68 |
] ); |
| 69 |
} |
| 70 |
|
| 71 |
public function move_account( $payload ) { |
| 72 |
$mover = new AccountMover( (int) $payload['from'], (int) $payload['to'] ); |
| 73 |
$result = $mover->move(); |
| 74 |
|
| 75 |
if ( is_wp_error( $result ) ) { |
| 76 |
wp_send_json_error( $result->get_error_message() ); |
| 77 |
} |
| 78 |
|
| 79 |
wp_send_json_success( [ |
| 80 |
'message' => esc_html__( 'Account data moved successfully.', 'storeengine' ), |
| 81 |
'result' => $result, |
| 82 |
] ); |
| 83 |
} |
| 84 |
|
| 85 |
public function create_new_customer( $payload ) { |
| 86 |
if ( empty( $payload['email'] ) || empty( $payload['password'] ) ) { |
| 87 |
wp_send_json_error( esc_html__( 'Email and Password are required', 'storeengine' ) ); |
| 88 |
} |
| 89 |
|
| 90 |
if ( ! is_email( $payload['email'] ) ) { |
| 91 |
wp_send_json_error( esc_html__( 'Invalid email', 'storeengine' ) ); |
| 92 |
} |
| 93 |
|
| 94 |
$customer = new Customer(); |
| 95 |
$customer->set_first_name( $payload['first_name'] ); |
| 96 |
$customer->set_last_name( $payload['last_name'] ); |
| 97 |
$customer->set_display_name( $payload['first_name'] ); |
| 98 |
$customer->set_email( $payload['email'] ); |
| 99 |
$customer->set_password( $payload['password'] ); |
| 100 |
$customer = $customer->save(); |
| 101 |
|
| 102 |
if ( is_wp_error( $customer ) ) { |
| 103 |
wp_send_json_error( $customer->get_error_message() ); |
| 104 |
} |
| 105 |
|
| 106 |
wp_send_json_success( [ |
| 107 |
'id' => $customer->get_id(), |
| 108 |
'user_login' => $customer->get_username(), |
| 109 |
'user_nicename' => $customer->get_nicename(), |
| 110 |
'user_email' => $customer->get_email(), |
| 111 |
'user_url' => $customer->get_url(), |
| 112 |
'user_registered' => $customer->get_user_registered() ? $customer->get_user_registered()->date( 'Y-m-d H:i:s' ) : null, |
| 113 |
'display_name' => $customer->get_display_name(), |
| 114 |
'first_name' => $customer->get_first_name(), |
| 115 |
'last_name' => $customer->get_last_name(), |
| 116 |
] ); |
| 117 |
} |
| 118 |
|
| 119 |
public function top_customers() { |
| 120 |
$customers = Helper::get_top_customers(); |
| 121 |
$top_customers = []; |
| 122 |
|
| 123 |
foreach ( $customers as $customer ) { |
| 124 |
$top_customers[] = [ |
| 125 |
'id' => $customer->get_id(), |
| 126 |
'avatar' => get_avatar_url( $customer->get_email(), [ 'size' => 50 ] ), |
| 127 |
'name' => ( ! empty( $customer->get_first_name() ) && ! empty( $customer->get_last_name() ) ) ? $customer->get_first_name() . ' ' . $customer->get_last_name() : null, |
| 128 |
'billing_name' => $customer->get_billing_first_name() . ' ' . $customer->get_billing_last_name(), |
| 129 |
'total_spent' => $customer->get_total_spent(), |
| 130 |
]; |
| 131 |
} |
| 132 |
|
| 133 |
wp_send_json_success( $top_customers ); |
| 134 |
} |
| 135 |
|
| 136 |
} |
| 137 |
|