getVar($db->prepare( "SELECT `id` FROM `#__daytidy_customers` WHERE `email` = %s", $email )); // User Already Exists if ($existing) return (int) $existing; $now = current_time('Y-m-d H:i:s'); $db->insert('daytidy_customers', [ 'first_name' => $first_name, 'last_name' => $last_name, 'email' => $email, 'created_at' => $now, 'updated_at' => $now, ], [ '%s', '%s', '%s', '%s', '%s', ]); $user_id = $db->insertId(); // Ensure that DayTidy does not create users with an ID lower than 1 // million to prevent any conflicts with existing WordPress users. if ($user_id < 1000000) { $new_id = $user_id + 1000000; $db->update('daytidy_customers', [ 'id' => $new_id, ], [ 'id' => $user_id, ], [ '%d', ], [ '%d', ]); $user_id = $new_id; } } // WordPress Registration else { $username = $email; if (isset($args['username']) && trim($args['username'])) $username = $args['username']; if (isset($args['password']) && trim($args['password'])) { $password = $args['password']; $user_id = wp_create_user($username, $password, $email); } else $user_id = register_new_user($username, $email); if (is_wp_error($user_id) || absint($user_id) <= 0) return 0; $user_id = absint($user_id); $user = new stdClass(); $user->ID = $user_id; $user->first_name = $first_name; $user->last_name = $last_name; $updated = wp_update_user($user); if (is_wp_error($updated)) { if (!function_exists('wp_delete_user')) require_once ABSPATH . 'wp-admin/includes/user.php'; wp_delete_user($user_id); return 0; } update_user_meta($user_id, 'nickname', trim($first_name . ' ' . $last_name)); // DayTidy customer accounts must never receive a privileged role. $wp_user = new WP_User($user_id); $wp_user->set_role('subscriber'); } return (int) $user_id; } /** * @param int $id * @return false|stdClass|WP_User|null */ public static function get(int $id) { $user = self::wp($id); if (!$user) $user = self::daytidy($id); return $user; } /** * @param int $id * @return stdClass|null */ private static function daytidy(int $id): ?stdClass { // DB $db = new Db(); $data = $db->getRow($db->prepare( "SELECT * FROM `#__daytidy_customers` WHERE `id` = %d", $id )); if (!$data) return null; $user = new stdClass(); $user->ID = $data->id; $user->first_name = stripslashes($data->first_name); $user->last_name = stripslashes($data->last_name); $user->display_name = trim(stripslashes($data->first_name) . ' ' . stripslashes($data->last_name)); $user->email = $data->email; $user->user_email = $data->email; $user->user_registered = $data->created_at; $user->data = $user; return $user; } /** * @param int $id * @return false|WP_User */ private static function wp(int $id) { return get_userdata($id); } /** * @param int $id * @return false|stdClass|WP_User|null */ public function booking(int $id) { // User ID $daytidy_customer_id = get_post_meta($id, 'daytidy_customer_id', true); // DayTidy Customer if (trim((string) $daytidy_customer_id) && is_numeric($daytidy_customer_id) && (int) $daytidy_customer_id >= 1000000) { $customer = $this->daytidy((int) $daytidy_customer_id); if ($customer) return $customer; return $this->bookingMetaCustomer($id, (int) $daytidy_customer_id); } // Booking $booking = get_post($id); // WordPress User return isset($booking->post_author) ? $this->wp((int) $booking->post_author) : null; } private function bookingMetaCustomer(int $booking_id, int $customer_id): stdClass { $first_name = (string) get_post_meta($booking_id, 'daytidy_first_name', true); $last_name = (string) get_post_meta($booking_id, 'daytidy_last_name', true); $email = (string) get_post_meta($booking_id, 'daytidy_email', true); $user = new stdClass(); $user->ID = $customer_id; $user->first_name = $first_name; $user->last_name = $last_name; $user->display_name = trim($first_name . ' ' . $last_name); $user->email = $email; $user->user_email = $email; $user->data = $user; return $user; } }