From 190f3229946fcd89fd51e50868ccc33181a12b83 Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Sat, 11 Jan 2020 15:39:04 -0500 Subject: [PATCH] new field cap check set. --- includes/model/class-product-variation.php | 53 +++++---- includes/model/class-product.php | 111 ++++++++++-------- .../_support/Helper/crud-helpers/product.php | 9 +- 3 files changed, 101 insertions(+), 72 deletions(-) diff --git a/includes/model/class-product-variation.php b/includes/model/class-product-variation.php index e93f48cd9..83b3bf6fb 100644 --- a/includes/model/class-product-variation.php +++ b/includes/model/class-product-variation.php @@ -98,34 +98,16 @@ protected function init() { ? \wc_graphql_price( $this->data->get_price() ) : null; }, - 'priceRaw' => array( - 'callback' => function() { - return ! empty( $this->data->get_price() ) ? $this->data->get_price() : null; - }, - 'capability' => $this->post_type_object->cap->edit_posts, - ), - 'salePrice' => function() { - return ! empty( $this->data->get_sale_price() ) - ? \wc_graphql_price( $this->data->get_sale_price() ) - : null; - }, - 'salePriceRaw' => array( - 'callback' => function() { - return ! empty( $this->data->get_sale_price() ) ? $this->data->get_sale_price() : null; - }, - 'capability' => $this->post_type_object->cap->edit_posts, - ), 'regularPrice' => function() { return ! empty( $this->data->get_regular_price() ) ? \wc_graphql_price( $this->data->get_regular_price() ) : null; }, - 'regularPriceRaw' => array( - 'callback' => function() { - return ! empty( $this->data->get_regular_price() ) ? $this->data->get_regular_price() : null; - }, - 'capability' => $this->post_type_object->cap->edit_posts, - ), + 'salePrice' => function() { + return ! empty( $this->data->get_sale_price() ) + ? \wc_graphql_price( $this->data->get_sale_price() ) + : null; + }, 'dateOnSaleFrom' => function() { return ! empty( $this->data->get_date_on_sale_from() ) ? $this->data->get_date_on_sale_from() : null; }, @@ -204,10 +186,33 @@ protected function init() { 'type' => function() { return ! empty( $this->data->get_type() ) ? $this->data->get_type() : null; }, + + /** + * Editor/Shop Manager only fields + */ + 'priceRaw' => array( + 'callback' => function() { + return ! empty( $this->data->get_price() ) ? $this->data->get_price() : null; + }, + 'capability' => $this->post_type_object->cap->edit_posts, + ), + 'regularPriceRaw' => array( + 'callback' => function() { + return ! empty( $this->data->get_regular_price() ) ? $this->data->get_regular_price() : null; + }, + 'capability' => $this->post_type_object->cap->edit_posts, + ), + 'salePriceRaw' => array( + 'callback' => function() { + return ! empty( $this->data->get_sale_price() ) ? $this->data->get_sale_price() : null; + }, + 'capability' => $this->post_type_object->cap->edit_posts, + ), + /** * Connection resolvers fields * - * These field resolvers are used in connection resolvers to define WP_Query argument + * These field resolvers are used in connection resolvers to define WP_Query argument. * Note: underscore naming style is used as a quick identifier */ 'parent_id' => function() { diff --git a/includes/model/class-product.php b/includes/model/class-product.php index e1df65dcb..64ef9aa6f 100644 --- a/includes/model/class-product.php +++ b/includes/model/class-product.php @@ -181,20 +181,11 @@ protected function init() { 'featured' => function() { return ! is_null( $this->data->get_featured() ) ? $this->data->get_featured() : null; }, - 'catalogVisibility' => function() { - return ! empty( $this->data->get_catalog_visibility() ) ? $this->data->get_catalog_visibility() : null; - }, 'description' => function() { return ! empty( $this->data->get_description() ) ? apply_filters( 'the_content', $this->data->get_description() ) : null; }, - 'descriptionRaw' => array( - 'callback' => function() { - return ! empty( $this->data->get_description() ) ? $this->data->get_description() : null; - }, - 'capability' => $this->post_type_object->cap->edit_posts, - ), 'shortDescription' => function() { $short_description = ! empty( $this->data->get_short_description() ) ? apply_filters( @@ -205,12 +196,6 @@ protected function init() { : null; return apply_filters( 'the_excerpt', $short_description ); }, - 'shortDescriptionRaw' => array( - 'callback' => function() { - return ! empty( $this->data->get_short_description() ) ? $this->data->get_short_description() : null; - }, - 'capability' => $this->post_type_object->cap->edit_posts, - ), 'sku' => function() { return ! empty( $this->data->get_sku() ) ? $this->data->get_sku() : null; }, @@ -220,9 +205,6 @@ protected function init() { 'dateOnSaleTo' => function() { return ! empty( $this->data->get_date_on_sale_to() ) ? $this->data->get_date_on_sale_to() : null; }, - 'totalSales' => function() { - return ! is_null( $this->data->get_total_sales() ) ? $this->data->get_total_sales() : null; - }, 'reviewsAllowed' => function() { return ! empty( $this->data->get_reviews_allowed() ) ? $this->data->get_reviews_allowed() : null; }, @@ -244,6 +226,35 @@ protected function init() { 'purchasable' => function () { return ! is_null( $this->data->is_purchasable() ) ? $this->data->is_purchasable() : null; }, + + /** + * Editor/Shop Manager only fields + */ + 'descriptionRaw' => array( + 'callback' => function() { + return ! empty( $this->data->get_description() ) ? $this->data->get_description() : null; + }, + 'capability' => $this->post_type_object->cap->edit_posts, + ), + 'shortDescriptionRaw' => array( + 'callback' => function() { + return ! empty( $this->data->get_short_description() ) ? $this->data->get_short_description() : null; + }, + 'capability' => $this->post_type_object->cap->edit_posts, + ), + 'catalogVisibility' => array( + 'callback' => function() { + return ! empty( $this->data->get_catalog_visibility() ) ? $this->data->get_catalog_visibility() : null; + }, + 'capability' => $this->post_type_object->cap->edit_posts, + ), + 'totalSales' => array( + 'callback' => function() { + return ! is_null( $this->data->get_total_sales() ) ? $this->data->get_total_sales() : null; + }, + 'capability' => $this->post_type_object->cap->edit_posts, + ), + /** * Connection resolvers fields * @@ -286,45 +297,49 @@ protected function init() { ); if ( 'grouped' !== $this->data->get_type() ) { - $shared_fields['price'] = function() { + $shared_fields['price'] = function() { return ! empty( $this->data->get_price() ) ? \wc_graphql_price( $this->data->get_price() ) : null; }; + $shared_fields['regularPrice'] = function() { + return ! empty( $this->data->get_regular_price() ) + ? \wc_graphql_price( $this->data->get_regular_price() ) + : null; + }; + $shared_fields['salePrice'] = function() { + return ! empty( $this->data->get_sale_price() ) + ? \wc_graphql_price( $this->data->get_sale_price() ) + : null; + }; + $shared_fields['taxStatus'] = function() { + return ! empty( $this->data->get_tax_status() ) ? $this->data->get_tax_status() : null; + }; + $shared_fields['taxClass'] = function() { + return ! is_null( $this->data->get_tax_class() ) ? $this->data->get_tax_class() : ''; + }; + + /** + * Editor/Shop Manager only fields + */ $shared_fields['priceRaw'] = array( 'callback' => function() { return ! empty( $this->data->get_price() ) ? $this->data->get_price() : null; }, 'capability' => $this->post_type_object->cap->edit_posts, ); - $shared_fields['regularPrice'] = function() { - return ! empty( $this->data->get_regular_price() ) - ? \wc_graphql_price( $this->data->get_regular_price() ) - : null; - }; $shared_fields['regularPriceRaw'] = array( 'callback' => function() { return ! empty( $this->data->get_regular_price() ) ? $this->data->get_regular_price() : null; }, 'capability' => $this->post_type_object->cap->edit_posts, ); - $shared_fields['salePrice'] = function() { - return ! empty( $this->data->get_sale_price() ) - ? \wc_graphql_price( $this->data->get_sale_price() ) - : null; - }; $shared_fields['salePriceRaw'] = array( 'callback' => function() { return ! empty( $this->data->get_sale_price() ) ? $this->data->get_sale_price() : null; }, 'capability' => $this->post_type_object->cap->edit_posts, ); - $shared_fields['taxStatus'] = function() { - return ! empty( $this->data->get_tax_status() ) ? $this->data->get_tax_status() : null; - }; - $shared_fields['taxClass'] = function() { - return ! is_null( $this->data->get_tax_class() ) ? $this->data->get_tax_class() : ''; - }; } if ( 'simple' === $this->data->get_type() || 'variable' === $this->data->get_type() ) { @@ -400,35 +415,39 @@ protected function init() { 'price' => function() { return $this->get_variation_price(); }, + 'regularPrice' => function() { + return $this->get_variation_price( 'regular' ); + }, + 'salePrice' => function() { + return $this->get_variation_price( 'sale' ); + }, + 'variation_ids' => function() { + return ! empty( $this->data->get_children() ) + ? array_map( 'absint', $this->data->get_children() ) + : array( '0' ); + }, + + /** + * Editor/Shop Manager only fields + */ 'priceRaw' => array( 'callback' => function() { return $this->get_variation_price( '', true ); }, 'capability' => $this->post_type_object->cap->edit_posts, ), - 'regularPrice' => function() { - return $this->get_variation_price( 'regular' ); - }, 'regularPriceRaw' => array( 'callback' => function() { return $this->get_variation_price( 'regular', true ); }, 'capability' => $this->post_type_object->cap->edit_posts, ), - 'salePrice' => function() { - return $this->get_variation_price( 'sale' ); - }, 'salePriceRaw' => array( 'callback' => function() { return $this->get_variation_price( 'sale', true ); }, 'capability' => $this->post_type_object->cap->edit_posts, ), - 'variation_ids' => function() { - return ! empty( $this->data->get_children() ) - ? array_map( 'absint', $this->data->get_children() ) - : array( '0' ); - }, ); break; case 'external': diff --git a/tests/_support/Helper/crud-helpers/product.php b/tests/_support/Helper/crud-helpers/product.php index dd3389c74..7465267ac 100644 --- a/tests/_support/Helper/crud-helpers/product.php +++ b/tests/_support/Helper/crud-helpers/product.php @@ -300,6 +300,11 @@ public function create_download( $id = 0 ) { public function print_query( $id, $raw = false ) { $data = wc_get_product( $id ); + $is_shop_manager = false; + $user = wp_get_current_user(); + if ( $user && in_array( 'shop_manager', (array) $user->roles ) ) { + $is_shop_manager = true; + } return array( 'id' => $this->to_relay_id( $id ), @@ -310,7 +315,6 @@ public function print_query( $id, $raw = false ) { 'modified' => $data->get_date_modified()->__toString(), 'status' => $data->get_status(), 'featured' => $data->get_featured(), - 'catalogVisibility' => strtoupper( $data->get_catalog_visibility() ), 'description' => ! empty( $data->get_description() ) ? $raw ? $data->get_description() @@ -336,7 +340,6 @@ public function print_query( $id, $raw = false ) { : null, 'dateOnSaleFrom' => $data->get_date_on_sale_from(), 'dateOnSaleTo' => $data->get_date_on_sale_to(), - 'totalSales' => $data->get_total_sales(), 'taxStatus' => strtoupper( $data->get_tax_status() ), 'taxClass' => ! empty( $data->get_tax_class() ) ? $data->get_tax_class() @@ -367,6 +370,8 @@ public function print_query( $id, $raw = false ) { 'shippingRequired' => $data->needs_shipping(), 'shippingTaxable' => $data->is_shipping_taxable(), 'link' => get_post_permalink( $id ), + 'totalSales' => $is_shop_manager ? $data->get_total_sales() : null, + 'catalogVisibility' => $is_shop_manager ? strtoupper( $data->get_catalog_visibility() ) :null, ); }