| 1 |
<?php |
| 2 |
|
| 3 |
namespace Bookit\Classes\Database; |
| 4 |
|
| 5 |
use Bookit\Classes\Vendor\DatabaseModel; |
| 6 |
|
| 7 |
class Customers extends DatabaseModel { |
| 8 |
|
| 9 |
/** |
| 10 |
* Create Table |
| 11 |
*/ |
| 12 |
public static function create_table() { |
| 13 |
global $wpdb; |
| 14 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 15 |
$table_name = self::_table(); |
| 16 |
$primary_key = self::$primary_key; |
| 17 |
|
| 18 |
$sql = "CREATE TABLE {$table_name} ( |
| 19 |
id INT UNSIGNED NOT NULL AUTO_INCREMENT, |
| 20 |
wp_user_id BIGINT(20), |
| 21 |
full_name VARCHAR(255) NOT NULL, |
| 22 |
email VARCHAR(255) NOT NULL, |
| 23 |
phone VARCHAR(255), |
| 24 |
PRIMARY KEY ({$primary_key}), |
| 25 |
INDEX `idx_wp_user_id` (`wp_user_id`) |
| 26 |
) {$wpdb->get_charset_collate()};"; |
| 27 |
|
| 28 |
maybe_create_table( $table_name, $sql ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get an unlinked customer whose submitted contact details all match. |
| 33 |
* |
| 34 |
* @since 2.6.0.2 |
| 35 |
* |
| 36 |
* @param string $full_name Submitted customer full name. |
| 37 |
* @param string $email Submitted customer email. |
| 38 |
* @param string $phone Submitted customer phone. |
| 39 |
* |
| 40 |
* @return object|null |
| 41 |
*/ |
| 42 |
public static function get_by_contact( $full_name, $email, $phone ) { |
| 43 |
global $wpdb; |
| 44 |
|
| 45 |
return $wpdb->get_row( |
| 46 |
$wpdb->prepare( |
| 47 |
'SELECT * FROM %i WHERE `full_name` = %s AND `email` = %s AND `phone` = %s AND ( `wp_user_id` IS NULL OR `wp_user_id` = 0 ) LIMIT 1', |
| 48 |
self::_table(), |
| 49 |
$full_name, |
| 50 |
$email, |
| 51 |
$phone |
| 52 |
) |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Delete Customer |
| 58 |
* Set customer appointments status = delete |
| 59 |
* Update customer appointments notes |
| 60 |
*/ |
| 61 |
public static function deleteCustomer( $id ) { |
| 62 |
global $wpdb; |
| 63 |
|
| 64 |
$wpdb->query( 'START TRANSACTION' ); |
| 65 |
|
| 66 |
$customer = self::get( 'id', $id ); |
| 67 |
$customer->delete_date = gmdate( 'Y-m-d H:i:s' ); |
| 68 |
|
| 69 |
$customerAppointments = Appointments::customer_appointments( $id ); |
| 70 |
|
| 71 |
foreach ( $customerAppointments as $appointment ) { |
| 72 |
$notes = unserialize( $appointment->notes ); |
| 73 |
$notes['delete_customer'] = $customer; |
| 74 |
|
| 75 |
$wpdb->update( |
| 76 |
Appointments::_table(), |
| 77 |
array( |
| 78 |
'notes' => serialize( $notes ), |
| 79 |
'status' => Appointments::$delete, |
| 80 |
), |
| 81 |
array( 'id' => $appointment->id ) |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
$sql = sprintf( 'DELETE FROM `%s` WHERE id = %d', esc_sql( self::_table() ), absint( $id ) ); |
| 86 |
$wpdb->query( $wpdb->prepare( $sql ) ); |
| 87 |
|
| 88 |
$wpdb->query( 'COMMIT' ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Save WP user if not exist |
| 93 |
* |
| 94 |
* @since 2.6.0 Let WordPress hash the password on insert so the account can authenticate later. |
| 95 |
*/ |
| 96 |
public static function save_or_get_wp_user( $data ) { |
| 97 |
$is_exist_user = get_user_by( 'email', sanitize_email( $data['email'] ) ); |
| 98 |
if ( $is_exist_user ) { |
| 99 |
return $is_exist_user->data->ID; |
| 100 |
} |
| 101 |
|
| 102 |
$user_data = array( |
| 103 |
'user_login' => sanitize_user( $data['email'] ), |
| 104 |
'user_pass' => $data['password'], |
| 105 |
'first_name' => sanitize_text_field( $data['full_name'] ), |
| 106 |
'last_name' => '', |
| 107 |
'user_email' => sanitize_email( $data['email'] ), |
| 108 |
); |
| 109 |
|
| 110 |
if ( empty( $user_data['first_name'] ) ) { |
| 111 |
$user_data['first_name'] = $user_data['user_login']; |
| 112 |
} |
| 113 |
|
| 114 |
if ( array_key_exists( 'role', $data ) && get_role( sanitize_text_field( $data['role'] ) ) != null ) { |
| 115 |
$user_data['role'] = sanitize_text_field( $data['role'] ); |
| 116 |
} |
| 117 |
|
| 118 |
return wp_insert_user( $user_data ); |
| 119 |
} |
| 120 |
} |
| 121 |
|