Skip to content

Commit a605f55

Browse files
authored
Merge pull request #187 from kidunot89/feature/better-guest-customer-support
Better guest customer support
2 parents 78eff70 + 42f9a21 commit a605f55

File tree

13 files changed

+85
-53
lines changed

13 files changed

+85
-53
lines changed

.env.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ TESTS_ENVS=tests/_envs
2828

2929
CORE_BRANCH=develop
3030
JWT_AUTH_BRANCH=master
31-
SKIP_PCOV_CLOBBER_CLEANUP=false
31+
SKIP_TEST_CLEANUP=0

.travis.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ matrix:
3434
env: PHP_VERSION=7.2 WP_VERSION=5.3
3535
- php: 7.2
3636
env: PHP_VERSION=7.2 WP_VERSION=4.9
37+
- php: 7.1
38+
env: PHP_VERSION=7.1 WP_VERSION=5.3
39+
- php: 7.1
40+
env: PHP_VERSION=7.1 WP_VERSION=4.9
3741
- php: 7.0
38-
env: PHP_VERSION=7.0 WP_VERSION=5.0
42+
env: PHP_VERSION=7.0 WP_VERSION=5.0 COVERAGE=1
3943
- php: 7.0
4044
env: PHP_VERSION=7.0 WP_VERSION=4.9
4145
- php: 5.6
4246
env: PHP_VERSION=5.6 WP_VERSION=5.0
4347
- php: 5.6
4448
env: PHP_VERSION=5.6 WP_VERSION=4.9
45-
- php: 7.1
46-
env: PHP_VERSION=7.1 WP_VERSION=5.3
47-
- php: 7.1
48-
env: PHP_VERSION=7.1 WP_VERSION=4.9 COVERAGE=1
4949
allow_failures:
5050
- env: PHP_VERSION=5.6 WP_VERSION=5.0
5151
- env: PHP_VERSION=5.6 WP_VERSION=4.9

Dockerfile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,15 @@ RUN if [ "$PHP_VERSION" != "5.6" ] && [ "$PHP_VERSION" != "7.0" ]; then \
3131
rm -f /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
3232
echo "pcov.enabled=1" >> /usr/local/etc/php/php.ini ;\
3333
elif [ "$PHP_VERSION" == "5.6" ]; then \
34-
yes | pecl install xdebug-2.5.5; \
34+
yes | pecl install xdebug-2.5.5 \
35+
&& echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
36+
&& echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini \
37+
&& echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini; \
3538
else \
36-
yes | pecl install xdebug; \
39+
yes | pecl install xdebug \
40+
&& echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
41+
&& echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini \
42+
&& echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini; \
3743
fi
3844

3945
# Install composer

bin/testing-entrypoint.sh

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ dockerize \
4444
-timeout 1m
4545

4646
# Download c3 for testing.
47-
if [ ! -f "${PROJECT_DIR}/c3.php" ]; then
48-
echo 'Downloading c3.php'
49-
curl -L 'https://raw.github.com/Codeception/c3/2.0/c3.php' > "${PROJECT_DIR}/c3.php"
47+
if [ ! -f "$PROJECT_DIR/c3.php" ]; then
48+
echo "Downloading Codeception's c3.php"
49+
curl -L 'https://raw.github.com/Codeception/c3/2.0/c3.php' > "$PROJECT_DIR/c3.php"
5050
fi
5151

5252
# Install dependencies
@@ -68,14 +68,20 @@ chmod 777 ${TESTS_OUTPUT}
6868
# Run tests
6969
run_tests ${SUITES}
7070

71+
# Remove c3.php
72+
if [ -f "$PROJECT_DIR/c3.php" ] && [ "$SKIP_TESTS_CLEANUP" != "1" ]; then
73+
echo "Removing Codeception's c3.php"
74+
rm -rf "$PROJECT_DIR/c3.php"
75+
fi
76+
7177
# Clean coverage.xml and clean up PCOV configurations.
7278
if [ -f "${TESTS_OUTPUT}/coverage.xml" ] && [[ "$COVERAGE" == "1" ]]; then
7379
echo 'Cleaning coverage.xml for deployment'.
7480
pattern="$PROJECT_DIR/"
7581
sed -i "s~$pattern~~g" "$TESTS_OUTPUT"/coverage.xml
7682

7783
# Remove pcov/clobber
78-
if version_gt $PHP_VERSION 7.0 && [ ${SKIP_PCOV_CLOBBER_CLEANUP} != "true" ]; then
84+
if version_gt $PHP_VERSION 7.0 && [ "$SKIP_TESTS_CLEANUP" != "1" ]; then
7985
echo 'Removing pcov/clobber.'
8086
vendor/bin/pcov unclobber
8187
COMPOSER_MEMORY_LIMIT=-1 composer remove --dev pcov/clobber

codeception.dist.yml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@ settings:
1414
coverage:
1515
enabled: true
1616
remote: false
17-
work_dir: '%WP_ROOT_FOLDER%/wp-content/plugins/wp-graphql-woocommerce'
1817
c3_url: '%WP_URL%/wp-content/plugins/wp-graphql-woocommerce/wp-graphql-woocommerce.php'
19-
whitelist:
20-
include:
18+
include:
19+
- includes/*
20+
exclude:
2121
- wp-graphql-woocommerce.php
2222
- access-functions.php
2323
- class-inflect.php
24-
- includes/*.php
25-
remote_context_options:
26-
http:
27-
timeout: 90
24+
- vendor/*
25+
show_only_summary: false
2826
extensions:
2927
enabled:
3028
- Codeception\Extension\RunFailed
@@ -56,12 +54,9 @@ modules:
5654
adminUsername: '%ADMIN_USERNAME%'
5755
adminPassword: '%ADMIN_PASSWORD%'
5856
adminPath: '/wp-admin'
59-
curl:
60-
CURLOPT_TIMEOUT: 60000
6157
REST:
6258
depends: WPBrowser
6359
url: '%WP_URL%'
64-
timeout: 90
6560
WPFilesystem:
6661
wpRootFolder: '%WP_ROOT_FOLDER%'
6762
plugins: '/wp-content/plugins'

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ services:
4949
- './.log/testing:/var/log/apache2'
5050
env_file: .env.dist
5151
environment:
52+
XDEBUG_CONFIG: remote_host=host.docker.internal remote_port=9000 remote_enable=1
5253
WPGRAPHQL_WOOCOMMERCE_AUTOLOAD: 1
5354
DB_HOST: app_db
5455
WP_URL: 'http://localhost'

includes/data/class-factory.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,24 @@
2929
use WPGraphQL\WooCommerce\Data\Connection\Payment_Gateway_Connection_Resolver;
3030
use WPGraphQL\WooCommerce\Model\Order_Item;
3131
use WPGraphQL\WooCommerce\Model\Product;
32+
use WPGraphQL\WooCommerce\Model\Customer;
3233
use WPGraphQL\WooCommerce\Model\Tax_Rate;
3334
use WPGraphQL\WooCommerce\Model\Shipping_Method;
3435

3536
/**
3637
* Class Factory
3738
*/
3839
class Factory {
40+
/**
41+
* Returns the current woocommerce customer object tied to the current session.
42+
*
43+
* @return Customer
44+
* @access public
45+
*/
46+
public static function resolve_session_customer() {
47+
return new Customer();
48+
}
49+
3950
/**
4051
* Returns the Customer store object for the provided user ID
4152
*

includes/model/class-customer.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class Customer extends Model {
2525
* @access public
2626
* @return void
2727
*/
28-
public function __construct( $id ) {
29-
$this->data = new \WC_Customer( $id );
28+
public function __construct( $id = 'session' ) {
29+
$this->data = 'session' === $id ? \WC()->customer : new \WC_Customer( $id );
3030
$allowed_restricted_fields = array(
3131
'isRestricted',
3232
'isPrivate',
@@ -36,7 +36,7 @@ public function __construct( $id ) {
3636
'displayName',
3737
);
3838

39-
$restricted_cap = apply_filters( 'customer_restricted_cap', 'list_users' );
39+
$restricted_cap = apply_filters( 'customer_restricted_cap', 'session' === $id ? '' : 'list_users' );
4040

4141
parent::__construct( $restricted_cap, $allowed_restricted_fields, $id );
4242
}
@@ -62,10 +62,12 @@ protected function init() {
6262
if ( empty( $this->fields ) ) {
6363
$this->fields = array(
6464
'ID' => function() {
65-
return $this->data->get_id();
65+
return ( ! empty( $this->data->get_id() ) ) ? $this->data->get_id() : \WC()->session->_customer_id;
6666
},
6767
'id' => function() {
68-
return ( ! empty( $this->data ) ) ? Relay::toGlobalId( 'customer', $this->data->get_id() ) : null;
68+
return ( ! empty( $this->data->get_id() ) )
69+
? Relay::toGlobalId( 'customer', $this->data->get_id() )
70+
: 'new_customer';
6971
},
7072
'customerId' => function() {
7173
return ( ! empty( $this->data->get_id() ) ) ? $this->data->get_id() : null;

includes/mutation/class-customer-update.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use WPGraphQL\WooCommerce\Data\Mutation\Customer_Mutation;
1515
use WPGraphQL\WooCommerce\Model\Customer;
1616
use WPGraphQL\Model\User;
17+
use WPGraphQL\Mutation\UserCreate;
1718
use WPGraphQL\Mutation\UserUpdate;
1819

1920
/**
@@ -41,8 +42,12 @@ public static function register_mutation() {
4142
*/
4243
public static function get_input_fields() {
4344
$input_fields = array_merge(
44-
UserUpdate::get_input_fields(),
45+
UserCreate::get_input_fields(),
4546
array(
47+
'id' => array(
48+
'type' => 'ID',
49+
'description' => __( 'The ID of the user', 'wp-graphql' ),
50+
),
4651
'billing' => array(
4752
'type' => 'CustomerAddressInput',
4853
'description' => __( 'Customer billing information', 'wp-graphql-woocommerce' ),
@@ -84,21 +89,26 @@ public static function get_output_fields() {
8489
*/
8590
public static function mutate_and_get_payload() {
8691
return function( $input, AppContext $context, ResolveInfo $info ) {
87-
// Get closure from "UserRegister::mutate_and_get_payload".
88-
$update_user = UserUpdate::mutate_and_get_payload();
92+
$session_only = empty( $input['id'] );
93+
$payload = null;
94+
95+
if ( ! $session_only ) {
96+
// Get closure from "UserRegister::mutate_and_get_payload".
97+
$update_user = UserUpdate::mutate_and_get_payload();
8998

90-
// Update customer with core UserUpdate closure.
91-
$payload = $update_user( $input, $context, $info );
99+
// Update customer with core UserUpdate closure.
100+
$payload = $update_user( $input, $context, $info );
92101

93-
if ( empty( $payload ) ) {
94-
throw new UserError( __( 'Failed to update customer.', 'wp-graphql-woocommerce' ) );
102+
if ( empty( $payload ) ) {
103+
throw new UserError( __( 'Failed to update customer.', 'wp-graphql-woocommerce' ) );
104+
}
95105
}
96106

97107
// Map all of the args from GQL to WC friendly.
98108
$customer_args = Customer_Mutation::prepare_customer_props( $input, 'update' );
99109

100110
// Create customer object.
101-
$customer = new \WC_Customer( $payload['id'] );
111+
$customer = ! $session_only ? new \WC_Customer( $payload['id'] ) : \WC()->customer;
102112

103113
// Set billing address.
104114
if ( ! empty( $customer_args['billing'] ) ) {
@@ -128,7 +138,7 @@ public static function mutate_and_get_payload() {
128138
$customer->save();
129139

130140
// Return payload.
131-
return $payload;
141+
return ! empty( $payload ) ? $payload : array( 'id' => 'session' );
132142
};
133143
}
134144
}

includes/mutation/class-order-create.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static function get_input_fields() {
5050
'description' => __( 'Currency the order was created with, in ISO format.', 'wp-graphql-woocommerce' ),
5151
),
5252
'customerId' => array(
53-
'type' => array( 'non_null' => 'Int' ),
53+
'type' => 'Int',
5454
'description' => __( 'Order customer ID', 'wp-graphql-woocommerce' ),
5555
),
5656
'customerNote' => array(
@@ -154,10 +154,8 @@ public static function mutate_and_get_payload() {
154154
// Make sure gateways are loaded so hooks from gateways fire on save/create.
155155
WC()->payment_gateways();
156156

157-
// Validate customer ID.
158-
if ( empty( $input['customerId'] ) ) {
159-
throw new UserError( __( 'No customer ID provided.', 'wp-graphql-woocommerce' ) );
160-
} elseif ( ! Order_Mutation::validate_customer( $input ) ) {
157+
// Validate customer ID, if set.
158+
if ( ! empty( $input['customerId'] ) && ! Order_Mutation::validate_customer( $input ) ) {
161159
throw new UserError( __( 'Customer ID is invalid.', 'wp-graphql-woocommerce' ) );
162160
}
163161

0 commit comments

Comments
 (0)