Skip to content

Commit 74d4612

Browse files
committed
Syntax error in "order" type fixed. Related tests updated.
1 parent 80a83b0 commit 74d4612

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

includes/model/class-order.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,4 @@ protected function init() {
267267

268268
parent::prepare_fields();
269269
}
270-
271-
/**
272-
* Determines if the data object should be considered private
273-
*
274-
* @access public
275-
* @return bool
276-
*/
277-
protected function is_private() {
278-
if ( current_user_can( $this->post_type_object->cap->edit_others_posts ) ) {
279-
return true;
280-
}
281-
282-
return $this->owner_matches_current_user();
283-
}
284270
}

includes/type/object/class-order-type.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,26 @@ public static function register() {
366366
throw new UserError( sprintf( __( 'No order exists with the %1$s: %2$s' ), $id_type, $product_id ) );
367367
}
368368

369-
$order = Factory::resolve_crud_object( $order_id, $context );
369+
// Check if user authorized to view order.
370+
$post_type = get_post_type_object( 'shop_order' );
371+
$is_authorized = current_user_can( $post_type->cap->edit_others_posts );
372+
if ( get_current_user_id() ) {
373+
$orders = wc_get_orders(
374+
array(
375+
'type' => 'shop_order',
376+
'post__in' => array( $order_id ),
377+
'customer_id' => get_current_user_id(),
378+
'no_rows_found' => true,
379+
'return' => 'ids',
380+
)
381+
);
382+
383+
if ( in_array( $order_id, $orders, true ) ) {
384+
$is_authorized = true;
385+
}
386+
}
387+
388+
$order = $is_authorized ? Factory::resolve_crud_object( $order_id, $context ) : null;
370389

371390
return $order;
372391
},

tests/wpunit/MetaDataQueriesTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,9 @@ public function testOrderMetaDataQueries() {
462462
}
463463
}
464464
';
465+
466+
// Must be an "shop_manager" or "admin" to query orders not owned by the user.
467+
wp_set_current_user( $this->shop_manager );
465468

466469
/**
467470
* Assertion One

tests/wpunit/OrderQueriesTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ public function testOrderQuery() {
110110
/**
111111
* Assertion One
112112
*
113-
* tests query as customer
113+
* tests query as customer, should return "null" because the customer isn't authorized.
114114
*/
115115
wp_set_current_user( $this->customer );
116116
$variables = array( 'id' => $id );
117117
$actual = graphql( array( 'query' => $query, 'variables' => $variables ) );
118-
$expected = array( 'data' => array( 'order' => $this->order_helper->print_restricted_query( $this->order ) ) );
118+
$expected = array( 'data' => array( 'order' => null ) );
119119

120120
// use --debug flag to view.
121121
codecept_debug( $actual );
@@ -152,6 +152,9 @@ public function testOrderQueryAndArgs() {
152152
}
153153
';
154154

155+
// Must be an "shop_manager" or "admin" to query orders not owned by the user.
156+
wp_set_current_user( $this->shop_manager );
157+
155158
/**
156159
* Assertion One
157160
*

0 commit comments

Comments
 (0)