Use new code style

This commit is contained in:
David Stone
2025-02-07 19:02:33 -07:00
parent 0181024ae1
commit 8433379d90
672 changed files with 37107 additions and 45249 deletions

View File

@ -9,7 +9,7 @@
// Exit if accessed directly
defined('ABSPATH') || exit;
use \WP_Ultimo\Models\Customer;
use WP_Ultimo\Models\Customer;
/**
* Returns a customer.
@ -22,8 +22,7 @@ use \WP_Ultimo\Models\Customer;
function wu_get_customer($customer_id) {
return \WP_Ultimo\Models\Customer::get_by_id($customer_id);
} // end wu_get_customer;
}
/**
* Returns a single customer defined by a particular column and value.
*
@ -36,8 +35,7 @@ function wu_get_customer($customer_id) {
function wu_get_customer_by($column, $value) {
return \WP_Ultimo\Models\Customer::get_by($column, $value);
} // end wu_get_customer_by;
}
/**
* Gets a customer based on the hash.
*
@ -49,8 +47,7 @@ function wu_get_customer_by($column, $value) {
function wu_get_customer_by_hash($hash) {
return \WP_Ultimo\Models\Customer::get_by_hash($hash);
} // end wu_get_customer_by_hash;
}
/**
* Queries customers.
@ -62,24 +59,22 @@ function wu_get_customer_by_hash($hash) {
*/
function wu_get_customers($query = array()) {
if (!empty($query['search'])) {
$user_ids = get_users(array(
'blog_id' => 0,
'number' => -1,
'search' => '*' . $query['search'] . '*',
'fields' => 'ids',
));
if (!empty($user_ids)) {
if ( ! empty($query['search'])) {
$user_ids = get_users(
array(
'blog_id' => 0,
'number' => -1,
'search' => '*' . $query['search'] . '*',
'fields' => 'ids',
)
);
if ( ! empty($user_ids)) {
$query['user_id__in'] = $user_ids;
unset($query['search']);
} // end if;
} // end if;
}
}
/*
* Force search limit to customers only.
@ -87,8 +82,7 @@ function wu_get_customers($query = array()) {
$query['type'] = 'customer';
return \WP_Ultimo\Models\Customer::query($query);
} // end wu_get_customers;
}
/**
* Returns a customer based on user_id.
*
@ -100,8 +94,7 @@ function wu_get_customers($query = array()) {
function wu_get_customer_by_user_id($user_id) {
return \WP_Ultimo\Models\Customer::get_by('user_id', $user_id);
} // end wu_get_customer_by_user_id;
}
/**
* Returns the current customer.
*
@ -111,8 +104,7 @@ function wu_get_customer_by_user_id($user_id) {
function wu_get_current_customer() {
return wu_get_customer_by_user_id(get_current_user_id());
} // end wu_get_current_customer;
}
/**
* Creates a new customer.
*
@ -126,82 +118,70 @@ function wu_get_current_customer() {
*/
function wu_create_customer($customer_data) {
$customer_data = wp_parse_args($customer_data, array(
'user_id' => false,
'email' => false,
'username' => false,
'password' => false,
'vip' => false,
'ip' => false,
'email_verification' => 'none',
'meta' => array(),
'date_registered' => wu_get_current_time('mysql', true),
'date_modified' => wu_get_current_time('mysql', true),
'last_login' => wu_get_current_time('mysql', true),
'signup_form' => 'by-admin',
'billing_address' => array(),
));
$customer_data = wp_parse_args(
$customer_data,
array(
'user_id' => false,
'email' => false,
'username' => false,
'password' => false,
'vip' => false,
'ip' => false,
'email_verification' => 'none',
'meta' => array(),
'date_registered' => wu_get_current_time('mysql', true),
'date_modified' => wu_get_current_time('mysql', true),
'last_login' => wu_get_current_time('mysql', true),
'signup_form' => 'by-admin',
'billing_address' => array(),
)
);
$user = get_user_by('email', $customer_data['email']);
if (!$user) {
if ( ! $user) {
$user = get_user_by('ID', $customer_data['user_id']);
}
} // end if;
if (!$user) {
if ( ! $user) {
if ($customer_data['password']) {
$user_id = wpmu_create_user($customer_data['username'], $customer_data['password'], $customer_data['email']);
} else {
$user_id = register_new_user($customer_data['username'], $customer_data['email']);
} // end if;
}
if (is_wp_error($user_id)) {
return $user_id;
} // end if;
}
if ($user_id === false) {
return new \WP_Error('user', __('We were not able to create a new user with the provided username and email address combination.', 'wp-ultimo'), $customer_data);
} // end if;
}
} else {
$user_id = $user->ID;
}
} // end if;
if (!get_userdata($user_id)) {
if ( ! get_userdata($user_id)) {
return new \WP_Error('user_id', __('We were not able to find a user with the given user_id.', 'wp-ultimo'), $customer_data);
}
} // end if;
$customer = new Customer(array(
'user_id' => $user_id,
'email_verification' => $customer_data['email_verification'],
'meta' => $customer_data['meta'],
'date_registered' => $customer_data['date_registered'],
'signup_form' => $customer_data['signup_form'],
'billing_address' => $customer_data['billing_address'],
'last_login' => $customer_data['last_login'],
'type' => 'customer',
));
$customer = new Customer(
array(
'user_id' => $user_id,
'email_verification' => $customer_data['email_verification'],
'meta' => $customer_data['meta'],
'date_registered' => $customer_data['date_registered'],
'signup_form' => $customer_data['signup_form'],
'billing_address' => $customer_data['billing_address'],
'last_login' => $customer_data['last_login'],
'type' => 'customer',
)
);
$saved = $customer->save();
return is_wp_error($saved) ? $saved : $customer;
} // end wu_create_customer;
}
/**
* Get the customer meta types available.
@ -223,20 +203,16 @@ function wu_get_customer_meta_types($form_slugs = false) {
);
if (is_array($form_slugs)) {
$query['slug__in'] = (array) $form_slugs;
} // end if;
}
$forms = wu_get_checkout_forms($query);
foreach ($forms as $form) {
$customer_meta_fields = $form->get_all_meta_fields('customer_meta');
foreach ($customer_meta_fields as $customer_meta_field) {
$meta_keys[$customer_meta_field['id']] = array(
$meta_keys[ $customer_meta_field['id'] ] = array(
'type' => $customer_meta_field['type'],
'title' => $customer_meta_field['name'],
'description' => wu_get_isset($customer_meta_field, 'description', ''),
@ -249,14 +225,11 @@ function wu_get_customer_meta_types($form_slugs = false) {
'value' => '',
'exists' => false,
);
} // end foreach;
} // end foreach;
}
}
return $meta_keys;
} // end wu_get_customer_meta_types;
}
/**
* Returns all the meta data keys present on a customer.
@ -275,38 +248,33 @@ function wu_get_all_customer_meta($customer_id, $include_unset = true) {
$customer = wu_get_customer($customer_id);
if ($include_unset) {
$form_slugs = $customer ? array($customer->get_signup_form()) : false;
$all_meta = array_merge($all_meta, wu_get_customer_meta_types($form_slugs));
}
} // end if;
if (!$customer) {
if ( ! $customer) {
return $all_meta;
} // end if;
}
$meta_keys = $customer->get_meta('wu_custom_meta_keys');
if ($meta_keys) {
$meta_keys = array_map(
function ($item) {
$meta_keys = array_map(function($item) {
$item['exists'] = true;
$item['exists'] = true;
return $item;
}, $meta_keys);
return $item;
},
$meta_keys
);
$all_meta = wu_array_merge_recursive_distinct($all_meta, $meta_keys);
} // end if;
}
return $all_meta;
} // end wu_get_all_customer_meta;
}
/**
* Returns a customer meta.
@ -323,15 +291,12 @@ function wu_get_customer_meta($customer_id, $meta_key, $default = false, $single
$customer = wu_get_customer($customer_id);
if (!$customer) {
if ( ! $customer) {
return $default;
} // end if;
}
return $customer->get_meta($meta_key, $default, $single);
} // end wu_get_customer_meta;
}
/**
* Updates a customer meta.
@ -353,30 +318,28 @@ function wu_update_customer_meta($customer_id, $key, $value, $type = null, $titl
$customer = wu_get_customer($customer_id);
if (!$customer) {
if ( ! $customer) {
return false;
} // end if;
}
if ($type) {
$custom_keys = $customer->get_meta('wu_custom_meta_keys', array());
$custom_keys = array_merge($custom_keys, array(
$key => array(
'type' => $type,
'title' => $title,
),
));
$custom_keys = array_merge(
$custom_keys,
array(
$key => array(
'type' => $type,
'title' => $title,
),
)
);
$customer->update_meta('wu_custom_meta_keys', $custom_keys);
} // end if;
}
return $customer->update_meta($key, $value);
} // end wu_update_customer_meta;
}
/**
* Deletes a customer meta with a custom type field.
@ -391,25 +354,20 @@ function wu_delete_customer_meta($customer_id, $meta_key) {
$customer = wu_get_customer($customer_id);
if (!$customer) {
if ( ! $customer) {
return false;
} // end if;
}
$custom_keys = $customer->get_meta('wu_custom_meta_keys', array());
if (isset($custom_keys[$meta_key])) {
unset($custom_keys[$meta_key]);
if (isset($custom_keys[ $meta_key ])) {
unset($custom_keys[ $meta_key ]);
$customer->update_meta('wu_custom_meta_keys', $custom_keys);
} // end if;
}
return (bool) $customer->delete_meta($meta_key);
} // end wu_delete_customer_meta;
}
/**
* Searches for a existing gateway customer among the customer memberships.
@ -422,16 +380,17 @@ function wu_delete_customer_meta($customer_id, $meta_key) {
*/
function wu_get_customer_gateway_id($customer_id, $allowed_gateways = array()) {
$memberships = wu_get_memberships(array(
'customer_id' => absint($customer_id),
'gateway__in' => $allowed_gateways,
'number' => 1,
'gateway_customer_id__not_in' => array(''),
));
$memberships = wu_get_memberships(
array(
'customer_id' => absint($customer_id),
'gateway__in' => $allowed_gateways,
'number' => 1,
'gateway_customer_id__not_in' => array(''),
)
);
return !empty($memberships) ? $memberships[0]->get_gateway_customer_id() : '';
} // end wu_get_customer_gateway_id;
return ! empty($memberships) ? $memberships[0]->get_gateway_customer_id() : '';
}
/**
* Create a unique username for a new customer based on the email address passed.
@ -452,23 +411,18 @@ function wu_username_from_email($email, $new_user_args = array(), $suffix = '')
$username_parts = array();
if (isset($new_user_args['first_name'])) {
$username_parts[] = sanitize_user($new_user_args['first_name'], true);
} // end if;
}
if (isset($new_user_args['last_name'])) {
$username_parts[] = sanitize_user($new_user_args['last_name'], true);
} // end if;
}
// Remove empty parts.
$username_parts = array_filter($username_parts);
// If there are no parts, e.g. name had unicode chars, or was not provided, fallback to email.
if (empty($username_parts)) {
$email_parts = explode('@', $email);
$email_username = $email_parts[0];
@ -485,26 +439,21 @@ function wu_username_from_email($email, $new_user_args = array(), $suffix = '')
// Get the domain part, minus the dot.
$email_username = strtok($email_parts[1], '.');
} // end if;
}
$username_parts[] = sanitize_user($email_username, true);
} // end if;
}
$username = strtolower(implode('', $username_parts));
if ($suffix) {
$username .= $suffix;
} // end if;
}
$illegal_logins = (array) apply_filters('illegal_user_logins', array()); // phpcs:ignore
// Stop illegal logins and generate a new random username.
if (in_array(strtolower($username), array_map('strtolower', $illegal_logins), true)) {
$new_args = array();
/**
@ -525,17 +474,15 @@ function wu_username_from_email($email, $new_user_args = array(), $suffix = '')
);
return wu_username_from_email($email, $new_args, $suffix);
} // end if;
}
if (username_exists($username)) {
// Generate something unique to append to the username in case of a conflict with another user.
$suffix = '-' . zeroise(wp_rand(0, 9999), 4);
return wu_username_from_email( $email, $new_user_args, $suffix );
} // end if;
return wu_username_from_email($email, $new_user_args, $suffix);
}
/**
* Filter new customer username.
@ -547,5 +494,4 @@ function wu_username_from_email($email, $new_user_args = array(), $suffix = '')
* @param string $suffix Append string to username to make it unique.
*/
return apply_filters('wu_username_from_email', $username, $email, $new_user_args, $suffix);
} // end wu_username_from_email;
}