| 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 |
* Delete Customer |
| 33 |
* Set customer appointments status = delete |
| 34 |
* Update customer appointments notes |
| 35 |
*/ |
| 36 |
public static function deleteCustomer( $id ) { |
| 37 |
global $wpdb; |
| 38 |
|
| 39 |
$wpdb->query( 'START TRANSACTION' ); |
| 40 |
|
| 41 |
$customer = self::get( 'id', $id ); |
| 42 |
$customer->delete_date = gmdate( 'Y-m-d H:i:s' ); |
| 43 |
|
| 44 |
$customerAppointments = Appointments::customer_appointments( $id ); |
| 45 |
|
| 46 |
foreach ( $customerAppointments as $appointment ) { |
| 47 |
$notes = unserialize( $appointment->notes ); |
| 48 |
$notes['delete_customer'] = $customer; |
| 49 |
|
| 50 |
$wpdb->update( |
| 51 |
Appointments::_table(), |
| 52 |
array( |
| 53 |
'notes' => serialize( $notes ), |
| 54 |
'status' => Appointments::$delete, |
| 55 |
), |
| 56 |
array( 'id' => $appointment->id ) |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
$sql = sprintf( 'DELETE FROM `%s` WHERE id = %d', esc_sql( self::_table() ), absint( $id ) ); |
| 61 |
$wpdb->query( $wpdb->prepare( $sql ) ); |
| 62 |
|
| 63 |
$wpdb->query( 'COMMIT' ); |
| 64 |
} |
| 65 |
|
| 66 |
/** Save WP user if not exist **/ |
| 67 |
public static function save_or_get_wp_user( $data ) { |
| 68 |
$is_exist_user = get_user_by( 'email', sanitize_email( $data['email'] ) ); |
| 69 |
if ( $is_exist_user ) { |
| 70 |
return $is_exist_user->data->ID; |
| 71 |
} |
| 72 |
|
| 73 |
$user_data = array( |
| 74 |
'user_login' => sanitize_user( $data['email'] ), |
| 75 |
'user_pass' => wp_hash_password( $data['password'] ), |
| 76 |
'first_name' => sanitize_text_field( $data['full_name'] ), |
| 77 |
'last_name' => '', |
| 78 |
'user_email' => sanitize_email( $data['email'] ), |
| 79 |
); |
| 80 |
|
| 81 |
if ( empty( $user_data['first_name'] ) ) { |
| 82 |
$user_data['first_name'] = $user_data['user_login']; |
| 83 |
} |
| 84 |
|
| 85 |
if ( array_key_exists( 'role', $data ) && get_role( sanitize_text_field( $data['role'] ) ) != null ) { |
| 86 |
$user_data['role'] = sanitize_text_field( $data['role'] ); |
| 87 |
} |
| 88 |
|
| 89 |
return wp_insert_user( $user_data ); |
| 90 |
} |
| 91 |
} |
| 92 |
|