mirror of
https://github.com/wp-graphql/wp-graphql-woocommerce.git
synced 2026-08-14 12:53:44 +02:00
* fix: address WordPress.org plugin review feedback - Prefix the session transaction queue transient with the plugin's graphql_woocommerce_ namespace instead of the generic "woo_" word, to avoid collisions (Plugin Directory: prefix data storage). - Declare the WooCommerce dependency via the "Requires Plugins: woocommerce" plugin header. - Bump README.txt "Tested up to" to 7.0. - Ship composer.json in the distributed plugin (drop it, and composer.lock, from composer archive excludes) so the build is reproducible/reviewable. * chore: rename plugin to "GraphQL for eCommerce" for trademark compliance The WordPress.org plugin review flagged the display name/slug for beginning with the "WPGraphQL" trademark (and the "WooGraphQL" portmanteau of the WooCommerce mark), which can imply official affiliation. - Display name (plugin header + readme title) -> "GraphQL for eCommerce". - Slug/text domain -> "graphql-for-ecommerce" (header, all i18n string literals, and the PHPCS WordPress.WP.I18n text_domain config). - Update user-facing notices/errors that named the old plugin. "WooGraphQL" remains the project's informal nickname (repo, docs, community), just not in the WordPress.org directory's official name/slug. Internal file names and GitHub URLs are unchanged. * fix: keep test-only dev deps out of the committed composer.json The committed manifest mirrors develop (lint/stan dev deps only); CI adds the test suite deps at runtime via `composer installTestEnv`. A previous commit captured the installTestEnv-modified composer.json, desyncing it from composer.lock and breaking `composer install` in CI. * chore: regenerate composer.lock (refresh dev dependencies) Regenerate the lock from the manifest so it is in sync (fixes the CI `composer install` failure) and refresh dependencies in the process — firebase/php-jwt v7.0.4 -> v7.1.0 plus 11 others, with vendor-prefixed re-strauss'd to match. Full wpunit suite passes against the updated deps (305 tests, 835 assertions). * chore: rename text domain in createdVia/attribution strings from #1018 #1018 (createdVia + order attribution) merged into develop after the rename commit was authored, so its new i18n strings still used the old 'wp-graphql-woocommerce' text domain. Update them to 'graphql-for-ecommerce' to match the rename.
252 lines
6.7 KiB
PHP
252 lines
6.7 KiB
PHP
<?php
|
|
/**
|
|
* Factory
|
|
*
|
|
* This class serves as a factory for all the resolvers of queries and mutations.
|
|
*
|
|
* @package WPGraphQL\WooCommerce\Data
|
|
* @since 0.0.1
|
|
*/
|
|
|
|
namespace WPGraphQL\WooCommerce\Data;
|
|
|
|
use GraphQL\Error\UserError;
|
|
use WPGraphQL\AppContext;
|
|
use WPGraphQL\WooCommerce\Model\Coupon;
|
|
use WPGraphQL\WooCommerce\Model\Customer;
|
|
use WPGraphQL\WooCommerce\Model\Order;
|
|
use WPGraphQL\WooCommerce\Model\Order_Item;
|
|
use WPGraphQL\WooCommerce\Model\Product;
|
|
use WPGraphQL\WooCommerce\Model\Product_Variation;
|
|
use WPGraphQL\WooCommerce\Model\Shipping_Method;
|
|
use WPGraphQL\WooCommerce\Model\Tax_Rate;
|
|
use function WC;
|
|
|
|
/**
|
|
* Class Factory
|
|
*/
|
|
class Factory {
|
|
/**
|
|
* Returns the current woocommerce customer object tied to the current session.
|
|
*
|
|
* @return \WPGraphQL\WooCommerce\Model\Customer
|
|
* @access public
|
|
*/
|
|
public static function resolve_session_customer() {
|
|
return new Customer();
|
|
}
|
|
|
|
/**
|
|
* Returns the Customer store object for the provided user ID
|
|
*
|
|
* @param int $id - user ID of the customer being retrieved.
|
|
* @param \WPGraphQL\AppContext $context - AppContext object.
|
|
*
|
|
* @return null|\GraphQL\Deferred object
|
|
* @access public
|
|
*/
|
|
public static function resolve_customer( $id, AppContext $context ) {
|
|
if ( 0 === absint( $id ) ) {
|
|
return null;
|
|
}
|
|
|
|
return $context->get_loader( 'wc_customer' )->load_deferred( absint( $id ) );
|
|
}
|
|
|
|
/**
|
|
* Returns the WooCommerce CRUD object for the post ID
|
|
*
|
|
* @param int $id - post ID of the crud object being retrieved.
|
|
* @param \WPGraphQL\AppContext $context - AppContext object.
|
|
*
|
|
* @return null|\GraphQL\Deferred object
|
|
* @access public
|
|
*/
|
|
public static function resolve_crud_object( $id, AppContext $context ) {
|
|
if ( 0 === absint( $id ) ) {
|
|
return null;
|
|
}
|
|
|
|
return $context->get_loader( 'wc_post' )->load_deferred( absint( $id ) );
|
|
}
|
|
|
|
/**
|
|
* Returns the order item Model for the order item.
|
|
*
|
|
* @param \WC_Order_Item $item - order item crud object instance.
|
|
*
|
|
* @return \WPGraphQL\WooCommerce\Model\Order_Item
|
|
* @access public
|
|
* @throws \GraphQL\Error\UserError Invalid object.
|
|
*/
|
|
public static function resolve_order_item( $item ) {
|
|
/**
|
|
* If $id is an instance of WC_Order_Item
|
|
*/
|
|
if ( is_a( $item, \WC_Order_Item::class ) ) {
|
|
return new Order_Item( $item );
|
|
} else {
|
|
throw new UserError( __( 'Object provided to order item resolver is an invalid type', 'graphql-for-ecommerce' ) );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the tax rate Model for the tax rate ID.
|
|
*
|
|
* @param int $id - Tax rate ID.
|
|
* @param \WPGraphQL\AppContext $context - AppContext object.
|
|
*
|
|
* @return null|\GraphQL\Deferred object
|
|
*/
|
|
public static function resolve_tax_rate( $id, AppContext $context ) {
|
|
if ( 0 === absint( $id ) ) {
|
|
return null;
|
|
}
|
|
|
|
return $context->get_loader( 'tax_rate' )->load_deferred( absint( $id ) );
|
|
}
|
|
|
|
/**
|
|
* Returns the shipping method Model for the shipping method ID.
|
|
*
|
|
* @param int $id - Shipping method ID.
|
|
*
|
|
* @return \WPGraphQL\WooCommerce\Model\Shipping_Method
|
|
* @access public
|
|
* @throws \GraphQL\Error\UserError Invalid object.
|
|
*/
|
|
public static function resolve_shipping_method( $id ) {
|
|
$wc_shipping = \WC_Shipping::instance();
|
|
$methods = $wc_shipping->get_shipping_methods();
|
|
if ( empty( $methods[ $id ] ) ) {
|
|
throw new UserError(
|
|
/* translators: shipping method ID */
|
|
sprintf( __( 'No Shipping Method assigned to ID %s was found ', 'graphql-for-ecommerce' ), $id )
|
|
);
|
|
}
|
|
|
|
$method = $methods[ $id ];
|
|
|
|
return new Shipping_Method( $method );
|
|
}
|
|
|
|
/**
|
|
* Resolves the WooCommerce cart instance.
|
|
*
|
|
* @return \WC_Cart
|
|
*/
|
|
public static function resolve_cart() {
|
|
return WC()->cart;
|
|
}
|
|
|
|
/**
|
|
* Resolves a cart item by key.
|
|
*
|
|
* @param string $key Cart item key.
|
|
* @param \WPGraphQL\AppContext $context AppContext object.
|
|
*
|
|
* @return null|\GraphQL\Deferred object
|
|
*/
|
|
public static function resolve_cart_item( $key, AppContext $context ) {
|
|
if ( empty( $key ) ) {
|
|
return null;
|
|
}
|
|
|
|
return $context->get_loader( 'cart_item' )->load_deferred( $key );
|
|
}
|
|
|
|
/**
|
|
* Resolves a downloadable item by ID.
|
|
*
|
|
* @param int $id Downloadable item ID.
|
|
* @param \WPGraphQL\AppContext $context AppContext object.
|
|
*
|
|
* @return null|\GraphQL\Deferred object
|
|
*/
|
|
public static function resolve_downloadable_item( $id, AppContext $context ) {
|
|
if ( 0 === absint( $id ) ) {
|
|
return null;
|
|
}
|
|
|
|
return $context->get_loader( 'downloadable_item' )->load_deferred( absint( $id ) );
|
|
}
|
|
|
|
/**
|
|
* Resolves Relay node for some WPGraphQL for WooCommerce types.
|
|
*
|
|
* @param mixed $node Node object.
|
|
* @param int $id Object unique ID.
|
|
* @param string $type Node type.
|
|
* @param \WPGraphQL\AppContext $context AppContext instance.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public static function resolve_node( $node, $id, $type, $context ) {
|
|
switch ( $type ) {
|
|
case 'customer':
|
|
$node = self::resolve_customer( $id, $context );
|
|
break;
|
|
case 'shop_coupon':
|
|
case 'shop_order':
|
|
case 'shop_order_refund':
|
|
case 'product':
|
|
case 'product_variation':
|
|
$node = self::resolve_crud_object( $id, $context );
|
|
break;
|
|
case 'shipping_method':
|
|
$node = self::resolve_shipping_method( $id );
|
|
break;
|
|
case 'tax_rate':
|
|
$node = self::resolve_tax_rate( $id, $context );
|
|
break;
|
|
}
|
|
|
|
return $node;
|
|
}
|
|
|
|
/**
|
|
* Resolves Relay node type for some WPGraphQL for WooCommerce types.
|
|
*
|
|
* @param string|null $type Node type.
|
|
* @param mixed $node Node object.
|
|
*
|
|
* @return string|null
|
|
*/
|
|
public static function resolve_node_type( $type, $node ) {
|
|
switch ( true ) {
|
|
case is_a( $node, Coupon::class ):
|
|
$type = 'Coupon';
|
|
break;
|
|
case is_a( $node, Customer::class ):
|
|
$type = 'Customer';
|
|
break;
|
|
case is_a( $node, Order::class ):
|
|
$type = $node->get_type() === 'shop_order_refund' ? 'Refund' : 'Order';
|
|
break;
|
|
case is_a( $node, Product::class ) && 'simple' === $node->get_type():
|
|
$type = 'SimpleProduct';
|
|
break;
|
|
case is_a( $node, Product::class ) && 'variable' === $node->get_type():
|
|
$type = 'VariableProduct';
|
|
break;
|
|
case is_a( $node, Product::class ) && 'external' === $node->get_type():
|
|
$type = 'ExternalProduct';
|
|
break;
|
|
case is_a( $node, Product::class ) && 'grouped' === $node->get_type():
|
|
$type = 'GroupProduct';
|
|
break;
|
|
case is_a( $node, Product_Variation::class ):
|
|
$type = 'ProductVariation';
|
|
break;
|
|
case is_a( $node, Shipping_Method::class ):
|
|
$type = 'ShippingMethod';
|
|
break;
|
|
case is_a( $node, Tax_Rate::class ):
|
|
$type = 'TaxRate';
|
|
break;
|
|
}//end switch
|
|
|
|
return $type;
|
|
}
|
|
}
|