Skip to content

Commit

Permalink
feat: Queries and mutations for shipping zones, tax classes, and tax …
Browse files Browse the repository at this point in the history
…rates. (#856)

* fix: General bugfixes and improvements

* devops: New mutations and types tested and compliance with Linter and PHPStan

* chore: hooks added to the mutation resolvers

* feat: permission checks added

* chore: Linter and compliance met

* chore: Linter and compliance met
  • Loading branch information
kidunot89 authored May 17, 2024
1 parent 6028061 commit f686e74
Show file tree
Hide file tree
Showing 49 changed files with 4,602 additions and 115 deletions.
4 changes: 4 additions & 0 deletions includes/class-core-schema-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,16 @@ public static function graphql_data_loaders( $loaders, $context ) {
$loaders['cart_item'] = &$cart_item_loader;
$downloadable_item_loader = new WC_Db_Loader( $context, 'DOWNLOADABLE_ITEM' );
$loaders['downloadable_item'] = &$downloadable_item_loader;
$tax_class_loader = new WC_Db_Loader( $context, 'TAX_CLASS' );
$loaders['tax_class'] = &$tax_class_loader;
$tax_rate_loader = new WC_Db_Loader( $context, 'TAX_RATE' );
$loaders['tax_rate'] = &$tax_rate_loader;
$order_item_loader = new WC_Db_Loader( $context, 'ORDER_ITEM' );
$loaders['order_item'] = &$order_item_loader;
$shipping_item_loader = new WC_Db_Loader( $context, 'SHIPPING_METHOD' );
$loaders['shipping_method'] = &$shipping_item_loader;
$shipping_zone_loader = new WC_Db_Loader( $context, 'SHIPPING_ZONE' );
$loaders['shipping_zone'] = &$shipping_zone_loader;
return $loaders;
}

Expand Down
23 changes: 23 additions & 0 deletions includes/class-type-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public function init() {
Type\WPEnum\Product_Attribute_Enum::register();
Type\WPEnum\Attribute_Operator_Enum::register();
Type\WPEnum\Currency_Enum::register();
Type\WPEnum\Shipping_Location_Type_Enum::register();
Type\WPEnum\WC_Setting_Type_Enum::register();

/**
* InputObjects.
Expand All @@ -67,6 +69,8 @@ public function init() {
Type\WPInputObject\Collection_Stats_Where_Args::register();
Type\WPInputObject\Product_Attribute_Filter_Input::register();
Type\WPInputObject\Product_Attribute_Query_Input::register();
Type\WPInputObject\Shipping_Location_Input::register();
Type\WPInputObject\WC_Setting_Input::register();

/**
* Interfaces.
Expand Down Expand Up @@ -112,6 +116,10 @@ public function init() {
Type\WPObject\Payment_Token_Types::register();
Type\WPObject\Country_State_Type::register();
Type\WPObject\Collection_Stats_Type::register();
Type\WPObject\Shipping_Zone_Type::register();
Type\WPObject\Shipping_Location_Type::register();
Type\WPObject\Tax_Class_Type::register();
Type\WPObject\WC_Setting_Type::register();

/**
* Object fields.
Expand Down Expand Up @@ -145,6 +153,8 @@ public function init() {
Connection\Tax_Rates::register_connections();
Connection\Shipping_Methods::register_connections();
Connection\Payment_Gateways::register_connections();
Connection\Shipping_Zones::register_connections();
Connection\Tax_Classes::register_connections();

/**
* Mutations.
Expand Down Expand Up @@ -175,6 +185,19 @@ public function init() {
Mutation\Coupon_Delete::register_mutation();
Mutation\Payment_Method_Delete::register_mutation();
Mutation\Payment_Method_Set_Default::register_mutation();
Mutation\Shipping_Zone_Create::register_mutation();
Mutation\Shipping_Zone_Delete::register_mutation();
Mutation\Shipping_Zone_Locations_Clear::register_mutation();
Mutation\Shipping_Zone_Locations_Update::register_mutation();
Mutation\Shipping_Zone_Method_Add::register_mutation();
Mutation\Shipping_Zone_Method_Remove::register_mutation();
Mutation\Shipping_Zone_Method_Update::register_mutation();
Mutation\Shipping_Zone_Update::register_mutation();
Mutation\Tax_Class_Create::register_mutation();
Mutation\Tax_Class_Delete::register_mutation();
Mutation\Tax_Rate_Create::register_mutation();
Mutation\Tax_Rate_Delete::register_mutation();
Mutation\Tax_Rate_Update::register_mutation();
Mutation\Update_Session::register_mutation();
}
}
30 changes: 29 additions & 1 deletion includes/class-wp-graphql-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ private function includes() {
require $include_directory_path . 'model/class-order.php';
require $include_directory_path . 'model/class-order-item.php';
require $include_directory_path . 'model/class-shipping-method.php';
require $include_directory_path . 'model/class-shipping-zone.php';
require $include_directory_path . 'model/class-tax-rate.php';

// Include data loaders class files.
Expand All @@ -190,13 +191,15 @@ private function includes() {
require $include_directory_path . 'data/connection/class-order-item-connection-resolver.php';
require $include_directory_path . 'data/connection/class-payment-gateway-connection-resolver.php';
require $include_directory_path . 'data/connection/class-product-attribute-connection-resolver.php';
require $include_directory_path . 'data/connection/class-product-connection-resolver.php';
require $include_directory_path . 'data/connection/class-shipping-method-connection-resolver.php';
require $include_directory_path . 'data/connection/class-shipping-zone-connection-resolver.php';
require $include_directory_path . 'data/connection/class-tax-class-connection-resolver.php';
require $include_directory_path . 'data/connection/class-tax-rate-connection-resolver.php';
require $include_directory_path . 'data/connection/class-variation-attribute-connection-resolver.php';

// Include deprecated resolver trait/class files.
require $include_directory_path . 'data/connection/class-coupon-connection-resolver.php';
require $include_directory_path . 'data/connection/class-product-connection-resolver.php';
require $include_directory_path . 'data/connection/class-customer-connection-resolver.php';

// Include mutation processor class files.
Expand All @@ -205,6 +208,8 @@ private function includes() {
require $include_directory_path . 'data/mutation/class-coupon-mutation.php';
require $include_directory_path . 'data/mutation/class-customer-mutation.php';
require $include_directory_path . 'data/mutation/class-order-mutation.php';
require $include_directory_path . 'data/mutation/class-shipping-mutation.php';
require $include_directory_path . 'data/mutation/class-settings-mutation.php';

// Include factory class file.
require $include_directory_path . 'data/class-factory.php';
Expand Down Expand Up @@ -239,6 +244,8 @@ private function includes() {
require $include_directory_path . 'type/enum/class-attribute-operator-enum.php';
require $include_directory_path . 'type/enum/class-product-attribute-enum.php';
require $include_directory_path . 'type/enum/class-currency-enum.php';
require $include_directory_path . 'type/enum/class-shipping-location-type-enum.php';
require $include_directory_path . 'type/enum/class-wc-setting-type-enum.php';

// Include interface type class files.
require $include_directory_path . 'type/interface/class-attribute.php';
Expand Down Expand Up @@ -282,6 +289,10 @@ private function includes() {
require $include_directory_path . 'type/object/class-payment-token-types.php';
require $include_directory_path . 'type/object/class-country-state-type.php';
require $include_directory_path . 'type/object/class-collection-stats-type.php';
require $include_directory_path . 'type/object/class-shipping-location-type.php';
require $include_directory_path . 'type/object/class-shipping-zone-type.php';
require $include_directory_path . 'type/object/class-tax-class-type.php';
require $include_directory_path . 'type/object/class-wc-setting-type.php';

// Include input type class files.
require $include_directory_path . 'type/input/class-cart-item-input.php';
Expand All @@ -301,6 +312,8 @@ private function includes() {
require $include_directory_path . 'type/input/class-collection-stats-where-args.php';
require $include_directory_path . 'type/input/class-product-attribute-filter-input.php';
require $include_directory_path . 'type/input/class-product-attribute-query-input.php';
require $include_directory_path . 'type/input/class-shipping-location-input.php';
require $include_directory_path . 'type/input/class-wc-setting-input.php';

// Include mutation type class files.
require $include_directory_path . 'mutation/class-cart-add-fee.php';
Expand Down Expand Up @@ -329,6 +342,19 @@ private function includes() {
require $include_directory_path . 'mutation/class-review-update.php';
require $include_directory_path . 'mutation/class-payment-method-delete.php';
require $include_directory_path . 'mutation/class-payment-method-set-default.php';
require $include_directory_path . 'mutation/class-shipping-zone-create.php';
require $include_directory_path . 'mutation/class-shipping-zone-delete.php';
require $include_directory_path . 'mutation/class-shipping-zone-locations-clear.php';
require $include_directory_path . 'mutation/class-shipping-zone-locations-update.php';
require $include_directory_path . 'mutation/class-shipping-zone-method-add.php';
require $include_directory_path . 'mutation/class-shipping-zone-method-remove.php';
require $include_directory_path . 'mutation/class-shipping-zone-method-update.php';
require $include_directory_path . 'mutation/class-shipping-zone-update.php';
require $include_directory_path . 'mutation/class-tax-class-create.php';
require $include_directory_path . 'mutation/class-tax-class-delete.php';
require $include_directory_path . 'mutation/class-tax-rate-create.php';
require $include_directory_path . 'mutation/class-tax-rate-delete.php';
require $include_directory_path . 'mutation/class-tax-rate-update.php';
require $include_directory_path . 'mutation/class-update-session.php';

// Include connection class/function files.
Expand All @@ -342,6 +368,8 @@ private function includes() {
require $include_directory_path . 'connection/class-product-attributes.php';
require $include_directory_path . 'connection/class-products.php';
require $include_directory_path . 'connection/class-shipping-methods.php';
require $include_directory_path . 'connection/class-shipping-zones.php';
require $include_directory_path . 'connection/class-tax-classes.php';
require $include_directory_path . 'connection/class-tax-rates.php';
require $include_directory_path . 'connection/class-wc-terms.php';

Expand Down
63 changes: 63 additions & 0 deletions includes/connection/class-shipping-zones.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Connection - Shipping_Zones
*
* Registers connections to ShippingZone
*
* @package WPGraphQL\WooCommerce\Connection
* @since TBD
*/

namespace WPGraphQL\WooCommerce\Connection;

use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\WooCommerce\Data\Connection\Shipping_Zone_Connection_Resolver;

/**
* Class - Shipping_Zones
*/
class Shipping_Zones {
/**
* Registers the various connections from other Types to ShippingZone
*
* @return void
*/
public static function register_connections() {
// From RootQuery.
register_graphql_connection( self::get_connection_config() );
}

/**
* Given an array of $args, this returns the connection config, merging the provided args
* with the defaults.
*
* @param array $args - Connection configuration.
* @return array
*/
public static function get_connection_config( $args = [] ): array {
return array_merge(
[
'fromType' => 'RootQuery',
'toType' => 'ShippingZone',
'fromFieldName' => 'shippingZones',
'connectionArgs' => [],
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
$resolver = new Shipping_Zone_Connection_Resolver( $source, $args, $context, $info );

return $resolver->get_connection();
},
],
$args
);
}

/**
* Returns array of where args.
*
* @return array
*/
public static function get_connection_args(): array {
return [];
}
}
63 changes: 63 additions & 0 deletions includes/connection/class-tax-classes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Connection - Tax_Classes
*
* Registers connections to TaxClass
*
* @package WPGraphQL\WooCommerce\Connection
* @since TBD
*/

namespace WPGraphQL\WooCommerce\Connection;

use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\WooCommerce\Data\Connection\Tax_Class_Connection_Resolver;

/**
* Class - TaxClass
*/
class Tax_Classes {
/**
* Registers the various connections from other Types to TaxClass
*
* @return void
*/
public static function register_connections() {
// From RootQuery.
register_graphql_connection( self::get_connection_config() );
}

/**
* Given an array of $args, this returns the connection config, merging the provided args
* with the defaults.
*
* @param array $args - Connection configuration.
* @return array
*/
public static function get_connection_config( $args = [] ): array {
return array_merge(
[
'fromType' => 'RootQuery',
'toType' => 'TaxClass',
'fromFieldName' => 'taxClasses',
'connectionArgs' => [],
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
$resolver = new Tax_Class_Connection_Resolver( $source, $args, $context, $info );

return $resolver->get_connection();
},
],
$args
);
}

/**
* Returns array of where args.
*
* @return array
*/
public static function get_connection_args(): array {
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
namespace WPGraphQL\WooCommerce\Data\Connection;

use WPGraphQL\Data\Connection\AbstractConnectionResolver;
use WPGraphQL\WooCommerce\Model\Shipping_Method;
use WPGraphQL\WooCommerce\Model\Shipping_Zone;

/**
* Class Shipping_Method_Connection_Resolver
Expand All @@ -31,6 +33,10 @@ public function get_loader_name() {
* @return bool
*/
public function should_execute() {
if ( ! wc_rest_check_manager_permissions( 'shipping_methods', 'read' ) ) {
graphql_debug( __( 'Permission denied.', 'wp-graphql-woocommerce' ) );
return false;
}
return true;
}

Expand All @@ -47,30 +53,32 @@ public function get_query_args() {
/**
* Executes query
*
* @return array|mixed|string[]
* @return int[]
*/
public function get_query() {
// TODO: Implement get_query() method.
$wc_shipping = \WC_Shipping::instance();
$methods = $wc_shipping->get_shipping_methods();
if ( $this->source instanceof Shipping_Zone ) {
$methods = $this->source->methods;
} else {
$wc_shipping = \WC_Shipping::instance();
$methods = $wc_shipping->get_shipping_methods();
}

foreach ( $methods as $method ) {
$this->loader->prime( $method->id, new Shipping_Method( $method ) );
}

// Get shipping method IDs.
$methods = array_map(
static function ( $item ) {
return $item->id;
},
array_values( $methods )
);
$methods = wp_list_pluck( array_values( $methods ), 'id' );

return $methods;
}

/**
* Return an array of items from the query
*
* @return array|mixed
* @return array
*/
public function get_ids() {
public function get_ids_from_query() {
return ! empty( $this->query ) ? $this->query : [];
}

Expand Down
Loading

0 comments on commit f686e74

Please sign in to comment.