batch, 'customers', $index ); $plan = Plan::customer( $random, $ctx->from, $ctx->to ); $first = (string) $random->pick( $ctx->words['first_names'] ); $last = (string) $random->pick( $ctx->words['last_names'] ); $local = Words::ascii( $first ) . '.' . Words::ascii( $last ) . '.' . $random->int( 1000, 9999 ); $plan['first_name'] = $first; $plan['last_name'] = $last; $plan['email'] = $local . '@' . $ctx->domain; $plan['address'] = self::address( $random, $ctx, ! empty( $plan['base_country'] ) ); $plan['shipping'] = ! empty( $plan['ship_elsewhere'] ) ? self::address( $random, $ctx, true ) : $plan['address']; $plan['company'] = ! empty( $plan['company'] ) ? (string) $random->pick( $ctx->words['companies'] ) : ''; /** This filter is documented in src/Modules/TestData/Make/Products.php */ $plan = (array) apply_filters( 'apwc_test_data_item', $plan, 'customer', $index, $ctx->batch ); // The email must stay on the test domain, whatever a filter did. $email = sanitize_email( (string) ( $plan['email'] ?? '' ) ); if ( ! $email || ! str_ends_with( strtolower( $email ), '@' . $ctx->domain ) ) { $email = $local . '@' . $ctx->domain; } $existing = email_exists( $email ); if ( $existing ) { // A resumed step made it already; a real account is never touched. if ( (string) get_user_meta( (int) $existing, Marker::user_key(), true ) !== $ctx->batch ) { return 0; } if ( ! Marker::verified( 'user', (int) $existing, $ctx->batch ) ) { Marker::sign( 'user', (int) $existing, $ctx->batch ); } return (int) $existing; } $login = sanitize_user( strstr( $email, '@', true ), true ); if ( '' === $login || username_exists( $login ) ) { $login = substr( $login, 0, 40 ) . '.' . $ctx->batch . $index; } try { $user_id = wp_insert_user( array( 'user_login' => $login, 'user_email' => $email, 'user_pass' => wp_generate_password( 32, true, true ), 'first_name' => sanitize_text_field( (string) ( $plan['first_name'] ?? $first ) ), 'last_name' => sanitize_text_field( (string) ( $plan['last_name'] ?? $last ) ), 'display_name' => sanitize_text_field( trim( ( $plan['first_name'] ?? $first ) . ' ' . ( $plan['last_name'] ?? $last ) ) ), 'role' => 'customer', 'user_registered' => gmdate( 'Y-m-d H:i:s', (int) ( $plan['registered'] ?? $ctx->to ) ), 'meta_input' => array( Marker::user_key() => $ctx->batch ), ) ); } catch ( \Throwable $e ) { // Inserted before the error (a plugin on `user_register`, say): sign it, so Clean up // can remove it. $made = get_user_by( 'email', $email ); if ( $made && (string) get_user_meta( (int) $made->ID, Marker::user_key(), true ) === $ctx->batch ) { Marker::sign( 'user', (int) $made->ID, $ctx->batch ); } throw $e; } if ( is_wp_error( $user_id ) || ! $user_id ) { return 0; } $user_id = (int) $user_id; Marker::sign( 'user', $user_id, $ctx->batch ); try { $customer = new \WC_Customer( $user_id ); $billing = (array) $plan['address']; $shipping = (array) $plan['shipping']; $customer->set_billing_first_name( (string) $plan['first_name'] ); $customer->set_billing_last_name( (string) $plan['last_name'] ); $customer->set_billing_company( (string) $plan['company'] ); $customer->set_billing_email( $email ); $customer->set_billing_phone( (string) ( $billing['phone'] ?? '' ) ); $customer->set_billing_address_1( (string) ( $billing['address_1'] ?? '' ) ); $customer->set_billing_city( (string) ( $billing['city'] ?? '' ) ); $customer->set_billing_state( (string) ( $billing['state'] ?? '' ) ); $customer->set_billing_postcode( (string) ( $billing['postcode'] ?? '' ) ); $customer->set_billing_country( (string) ( $billing['country'] ?? '' ) ); $customer->set_shipping_first_name( (string) $plan['first_name'] ); $customer->set_shipping_last_name( (string) $plan['last_name'] ); $customer->set_shipping_address_1( (string) ( $shipping['address_1'] ?? '' ) ); $customer->set_shipping_city( (string) ( $shipping['city'] ?? '' ) ); $customer->set_shipping_state( (string) ( $shipping['state'] ?? '' ) ); $customer->set_shipping_postcode( (string) ( $shipping['postcode'] ?? '' ) ); $customer->set_shipping_country( (string) ( $shipping['country'] ?? '' ) ); $customer->save(); } catch ( \Exception $e ) { unset( $e ); // The account exists and is marked; the address is optional. } $ctx->created( 'customer', $user_id ); return $user_id; } /** * A fictional address: the store's base country, or another allowed one. * * @since 1.0.0 * @param Random $random Generator. * @param Context $ctx Context. * @param bool $base Use the base country. * @return array */ public static function address( Random $random, Context $ctx, bool $base ): array { $countries = function_exists( 'WC' ) && WC()->countries ? WC()->countries : null; $country = $countries ? (string) $countries->get_base_country() : 'US'; if ( ! $base && $countries ) { $allowed = array_keys( (array) $countries->get_allowed_countries() ); $country = $allowed ? (string) $random->pick( $allowed ) : $country; } $country = '' === $country ? 'US' : $country; $states = $countries ? $countries->get_states( $country ) : array(); $state = is_array( $states ) && $states ? (string) $random->pick( array_keys( $states ) ) : ''; return array( 'address_1' => $random->int( 1, 999 ) . ' ' . $random->pick( $ctx->words['streets'] ), 'city' => (string) $random->pick( $ctx->words['cities'] ), 'state' => $state, 'postcode' => Words::postcode( $random, $country ), 'country' => $country, 'phone' => Words::phone( $random, $country ), ); } }