Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Internal/Utilitie...
File: Users.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Automattic\WooCommerce\Internal\Utilities;
[2] Fix | Delete
[3] Fix | Delete
use Automattic\WooCommerce\Proxies\LegacyProxy;
[4] Fix | Delete
use WP_Error, WP_User;
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* Helper functions for working with users.
[8] Fix | Delete
*/
[9] Fix | Delete
class Users {
[10] Fix | Delete
/**
[11] Fix | Delete
* Indicates if the user qualifies as site administrator.
[12] Fix | Delete
*
[13] Fix | Delete
* In the context of multisite networks, this means that they must have the `manage_sites`
[14] Fix | Delete
* capability. In all other cases, they must have the `manage_options` capability.
[15] Fix | Delete
*
[16] Fix | Delete
* @param int $user_id Optional, used to specify a specific user (otherwise we look at the current user).
[17] Fix | Delete
*
[18] Fix | Delete
* @return bool
[19] Fix | Delete
*/
[20] Fix | Delete
public static function is_site_administrator( int $user_id = 0 ): bool {
[21] Fix | Delete
$user = 0 === $user_id ? wp_get_current_user() : get_user_by( 'id', $user_id );
[22] Fix | Delete
[23] Fix | Delete
if ( false === $user ) {
[24] Fix | Delete
return false;
[25] Fix | Delete
}
[26] Fix | Delete
[27] Fix | Delete
return is_multisite() ? $user->has_cap( 'manage_sites' ) : $user->has_cap( 'manage_options' );
[28] Fix | Delete
}
[29] Fix | Delete
[30] Fix | Delete
/**
[31] Fix | Delete
* Get a user from a valid user ID, but only if the active user is able to see them.
[32] Fix | Delete
*
[33] Fix | Delete
* In a multisite context, that may mean that they both must be members of the current blog, or else the active
[34] Fix | Delete
* user must either have special permissions (manage_network_users) or else a special legacy mode
[35] Fix | Delete
* (woocommerce_network_wide_customers) is enabled.
[36] Fix | Delete
*
[37] Fix | Delete
* @param int $user_id The ID of the desired user.
[38] Fix | Delete
* @param int|null $requesting_user_id The ID of the user making the request. Optional, defaults to the current user.
[39] Fix | Delete
*
[40] Fix | Delete
* @return WP_User|WP_Error
[41] Fix | Delete
*/
[42] Fix | Delete
public static function get_user_in_current_site( $user_id, ?int $requesting_user_id = null ) {
[43] Fix | Delete
// User ID is expected to be an integer. Cast it if we can (avoiding additional runtime warnings), else treat it as 0.
[44] Fix | Delete
$user_id = is_numeric( $user_id ) ? (int) $user_id : 0;
[45] Fix | Delete
[46] Fix | Delete
$legacy_proxy = wc_get_container()->get( LegacyProxy::class );
[47] Fix | Delete
$requesting_user_id = $requesting_user_id > 0 ? $requesting_user_id : wp_get_current_user()->ID;
[48] Fix | Delete
$error = new WP_Error( 'wc_user_invalid_id', __( 'Invalid user ID.', 'woocommerce' ) );
[49] Fix | Delete
[50] Fix | Delete
if ( $user_id <= 0 ) {
[51] Fix | Delete
return $error;
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
$user = get_userdata( $user_id );
[55] Fix | Delete
if ( ! $user instanceof WP_User || ! $user->exists() ) {
[56] Fix | Delete
return $error;
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
if (
[60] Fix | Delete
$legacy_proxy->call_function( 'is_multisite' )
[61] Fix | Delete
&& ! $legacy_proxy->call_function( 'is_user_member_of_blog', $user->ID )
[62] Fix | Delete
&& ! $legacy_proxy->call_function( 'user_can', $requesting_user_id, 'manage_network_users' )
[63] Fix | Delete
&& get_site_option( 'woocommerce_network_wide_customers', 'no' ) !== 'yes'
[64] Fix | Delete
) {
[65] Fix | Delete
return $error;
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
return $user;
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
/**
[72] Fix | Delete
* Check if the email is valid.
[73] Fix | Delete
*
[74] Fix | Delete
* @param int $order_id Order ID.
[75] Fix | Delete
* @param string $supplied_email Supplied email.
[76] Fix | Delete
* @param string $context Context in which we are checking the email.
[77] Fix | Delete
* @return bool
[78] Fix | Delete
*/
[79] Fix | Delete
public static function should_user_verify_order_email( $order_id, $supplied_email = null, $context = 'view' ) {
[80] Fix | Delete
$order = wc_get_order( $order_id );
[81] Fix | Delete
$billing_email = $order->get_billing_email();
[82] Fix | Delete
$customer_id = $order->get_customer_id();
[83] Fix | Delete
[84] Fix | Delete
// If we do not have a billing email for the order (could happen in the order is created manually, or if the
[85] Fix | Delete
// requirement for this has been removed from the checkout flow), email verification does not make sense.
[86] Fix | Delete
if ( empty( $billing_email ) ) {
[87] Fix | Delete
return false;
[88] Fix | Delete
}
[89] Fix | Delete
[90] Fix | Delete
// No verification step is needed if the user is logged in and is already associated with the order.
[91] Fix | Delete
if ( $customer_id && get_current_user_id() === $customer_id ) {
[92] Fix | Delete
return false;
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
/**
[96] Fix | Delete
* Controls the grace period within which we do not require any sort of email verification step before rendering
[97] Fix | Delete
* the 'order received' or 'order pay' pages.
[98] Fix | Delete
*
[99] Fix | Delete
* To eliminate the grace period, set to zero (or to a negative value). Note that this filter is not invoked
[100] Fix | Delete
* at all if email verification is deemed to be unnecessary (in other words, it cannot be used to force
[101] Fix | Delete
* verification in *all* cases).
[102] Fix | Delete
*
[103] Fix | Delete
* @since 8.0.0
[104] Fix | Delete
*
[105] Fix | Delete
* @param int $grace_period Time in seconds after an order is placed before email verification may be required.
[106] Fix | Delete
* @param WC_Order $this The order for which this grace period is being assessed.
[107] Fix | Delete
* @param string $context Indicates the context in which we might verify the email address. Typically 'order-pay' or 'order-received'.
[108] Fix | Delete
*/
[109] Fix | Delete
$verification_grace_period = (int) apply_filters( 'woocommerce_order_email_verification_grace_period', 10 * MINUTE_IN_SECONDS, $order, $context );
[110] Fix | Delete
$date_created = $order->get_date_created();
[111] Fix | Delete
[112] Fix | Delete
// We do not need to verify the email address if we are within the grace period immediately following order creation.
[113] Fix | Delete
if (
[114] Fix | Delete
is_a( $date_created, \WC_DateTime::class, true )
[115] Fix | Delete
&& time() - $date_created->getTimestamp() <= $verification_grace_period
[116] Fix | Delete
) {
[117] Fix | Delete
return false;
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
$session = wc()->session;
[121] Fix | Delete
$session_email = '';
[122] Fix | Delete
[123] Fix | Delete
if ( is_a( $session, \WC_Session::class ) ) {
[124] Fix | Delete
$customer = $session->get( 'customer' );
[125] Fix | Delete
$session_email = is_array( $customer ) && isset( $customer['email'] ) ? $customer['email'] : '';
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
// Email verification is required if the user cannot be identified, or if they supplied an email address but the nonce check failed.
[129] Fix | Delete
$can_view_orders = current_user_can( 'read_private_shop_orders' );
[130] Fix | Delete
$session_email_match = $session_email === $billing_email;
[131] Fix | Delete
$supplied_email_match = $supplied_email === $billing_email;
[132] Fix | Delete
[133] Fix | Delete
$email_verification_required = ! $session_email_match && ! $supplied_email_match && ! $can_view_orders;
[134] Fix | Delete
[135] Fix | Delete
/**
[136] Fix | Delete
* Provides an opportunity to override the (potential) requirement for shoppers to verify their email address
[137] Fix | Delete
* before we show information such as the order summary, or order payment page.
[138] Fix | Delete
*
[139] Fix | Delete
* Note that this hook is not always triggered, therefore it is (for example) unsuitable as a way of forcing
[140] Fix | Delete
* email verification across all order confirmation/order payment scenarios. Instead, the filter primarily
[141] Fix | Delete
* exists as a way to *remove* the email verification step.
[142] Fix | Delete
*
[143] Fix | Delete
* @since 7.9.0
[144] Fix | Delete
*
[145] Fix | Delete
* @param bool $email_verification_required If email verification is required.
[146] Fix | Delete
* @param WC_Order $order The relevant order.
[147] Fix | Delete
* @param string $context The context under which we are performing this check.
[148] Fix | Delete
*/
[149] Fix | Delete
return (bool) apply_filters( 'woocommerce_order_email_verification_required', $email_verification_required, $order, $context );
[150] Fix | Delete
}
[151] Fix | Delete
[152] Fix | Delete
/**
[153] Fix | Delete
* Site-specific method of retrieving the requested user meta.
[154] Fix | Delete
*
[155] Fix | Delete
* This is a multisite-aware wrapper around WordPress's own `get_user_meta()` function, and works by prefixing the
[156] Fix | Delete
* supplied meta key with a blog-specific meta key.
[157] Fix | Delete
*
[158] Fix | Delete
* @param int $user_id User ID.
[159] Fix | Delete
* @param string $key Optional. The meta key to retrieve. By default, returns data for all keys.
[160] Fix | Delete
* @param bool $single Optional. Whether to return a single value. This parameter has no effect if `$key` is not
[161] Fix | Delete
* specified. Default false.
[162] Fix | Delete
*
[163] Fix | Delete
* @return mixed An array of values if `$single` is false. The value of meta data field if `$single` is true.
[164] Fix | Delete
* False for an invalid `$user_id` (non-numeric, zero, or negative value). An empty string if a valid
[165] Fix | Delete
* but non-existing user ID is passed.
[166] Fix | Delete
*/
[167] Fix | Delete
public static function get_site_user_meta( int $user_id, string $key = '', bool $single = false ) {
[168] Fix | Delete
global $wpdb;
[169] Fix | Delete
$site_specific_key = $key . '_' . rtrim( $wpdb->get_blog_prefix( get_current_blog_id() ), '_' );
[170] Fix | Delete
return get_user_meta( $user_id, $site_specific_key, true );
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
/**
[174] Fix | Delete
* Site-specific means of updating user meta.
[175] Fix | Delete
*
[176] Fix | Delete
* This is a multisite-aware wrapper around WordPress's own `update_user_meta()` function, and works by prefixing
[177] Fix | Delete
* the supplied meta key with a blog-specific meta key.
[178] Fix | Delete
*
[179] Fix | Delete
* @param int $user_id User ID.
[180] Fix | Delete
* @param string $meta_key Metadata key.
[181] Fix | Delete
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
[182] Fix | Delete
* @param mixed $prev_value Optional. Previous value to check before updating. If specified, only update existing
[183] Fix | Delete
* metadata entries with this value. Otherwise, update all entries. Default empty.
[184] Fix | Delete
*
[185] Fix | Delete
* @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure or if the value
[186] Fix | Delete
* passed to the function is the same as the one that is already in the database.
[187] Fix | Delete
*/
[188] Fix | Delete
public static function update_site_user_meta( int $user_id, string $meta_key, $meta_value, $prev_value = '' ) {
[189] Fix | Delete
global $wpdb;
[190] Fix | Delete
$site_specific_key = $meta_key . '_' . rtrim( $wpdb->get_blog_prefix( get_current_blog_id() ), '_' );
[191] Fix | Delete
return update_user_meta( $user_id, $site_specific_key, $meta_value, $prev_value );
[192] Fix | Delete
}
[193] Fix | Delete
[194] Fix | Delete
/**
[195] Fix | Delete
* Site-specific means of deleting user meta.
[196] Fix | Delete
*
[197] Fix | Delete
* This is a multisite-aware wrapper around WordPress's own `delete_user_meta()` function, and works by prefixing
[198] Fix | Delete
* the supplied meta key with a blog-specific meta key.
[199] Fix | Delete
*
[200] Fix | Delete
* @param int $user_id User ID.
[201] Fix | Delete
* @param string $meta_key Metadata name.
[202] Fix | Delete
* @param mixed $meta_value Optional. Metadata value. If provided, rows will only be removed that match the value.
[203] Fix | Delete
* Must be serializable if non-scalar. Default empty.
[204] Fix | Delete
*
[205] Fix | Delete
* @return bool True on success, false on failure.
[206] Fix | Delete
* /
[207] Fix | Delete
*/
[208] Fix | Delete
public static function delete_site_user_meta( $user_id, $meta_key, $meta_value = '' ) {
[209] Fix | Delete
global $wpdb;
[210] Fix | Delete
$site_specific_key = $meta_key . '_' . rtrim( $wpdb->get_blog_prefix(), '_' );
[211] Fix | Delete
return delete_user_meta( $user_id, $site_specific_key, $meta_value );
[212] Fix | Delete
}
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function