Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release v0.4.1 #220

Merged
merged 2 commits into from
Jan 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Requires PHP: 5.6
Requires WooCommerce: 3.0.0
Requires WPGraphQL: 0.6.0+
Works with WPGraphQL-JWT-Authentication: 0.4.0+
Stable tag: 0.4.0
Stable tag: 0.4.1
License: GPL-3
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Maintained at: https://github.com/wp-graphql/wp-graphql-woocommerce
Expand Down
1 change: 1 addition & 0 deletions includes/class-type-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function init( \WPGraphQL\Registry\TypeRegistry $type_registry ) {
\WPGraphQL\WooCommerce\Type\WPEnum\Post_Type_Orderby_Enum::register();
\WPGraphQL\WooCommerce\Type\WPEnum\Products_Orderby_Enum::register();
\WPGraphQL\WooCommerce\Type\WPEnum\Orders_Orderby_Enum::register();
\WPGraphQL\WooCommerce\Type\WPEnum\Id_Type_Enums::register();

// InputObjects.
\WPGraphQL\WooCommerce\Type\WPInputObject\Customer_Address_Input::register();
Expand Down
2 changes: 1 addition & 1 deletion includes/mutation/class-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static function get_output_fields() {
return is_user_logged_in() ? new Customer( get_current_user_id() ) : null;
},
),
'result' => array(
'result' => array(
'type' => 'String',
'resolve' => function( $payload ) {
return $payload['result'];
Expand Down
143 changes: 143 additions & 0 deletions includes/type/enum/class-id-type-enums.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php
/**
* Register *IdTypeEnum
*
* @package \WPGraphQL\WooCommerce\Type\WPEnum
* @since 0.0.1
*/

namespace WPGraphQL\WooCommerce\Type\WPEnum;

/**
* Class - Id_Type_Enums
*/
class Id_Type_Enums {

/**
* Register the Enum used for setting the field to identify WC crud objects by
*
* @access public
* @return void
*/
public static function register() {
register_graphql_enum_type(
'CouponIdTypeEnum',
array(
'description' => __( 'The Type of Identifier used to fetch a single Coupon. Default is ID.', 'wp-graphql' ),
'values' => array(
'id' => self::get_value( 'id' ),
'database_id' => self::get_value( 'database_id' ),
'code' => array(
'name' => 'CODE',
'value' => 'code',
'description' => __( 'Coupon code.', 'wp-graphql-woocommerce' ),
),
),
)
);

register_graphql_enum_type(
'OrderIdTypeEnum',
array(
'description' => __( 'The Type of Identifier used to fetch a single Order. Default is ID.', 'wp-graphql' ),
'values' => array(
'id' => self::get_value( 'id' ),
'database_id' => self::get_value( 'database_id' ),
'order_number' => array(
'name' => 'ORDER_NUMBER',
'value' => 'order_number',
'description' => __( 'Order number.', 'wp-graphql-woocommerce' ),
),
),
)
);

register_graphql_enum_type(
'ProductIdTypeEnum',
array(
'description' => __( 'The Type of Identifier used to fetch a single Product. Default is ID.', 'wp-graphql' ),
'values' => array(
'id' => self::get_value( 'id' ),
'database_id' => self::get_value( 'database_id' ),
'slug' => self::get_value( 'slug' ),
'sku' => array(
'name' => 'SKU',
'value' => 'sku',
'description' => __( 'Unique store identifier for product.', 'wp-graphql-woocommerce' ),
),
),
)
);

register_graphql_enum_type(
'ProductVariationIdTypeEnum',
array(
'description' => __( 'The Type of Identifier used to fetch a single ProductVariation. Default is ID.', 'wp-graphql' ),
'values' => array(
'id' => self::get_value( 'id' ),
'database_id' => self::get_value( 'database_id' ),
),
)
);

register_graphql_enum_type(
'RefundIdTypeEnum',
array(
'description' => __( 'The Type of Identifier used to fetch a single Refund. Default is ID.', 'wp-graphql' ),
'values' => array(
'id' => self::get_value( 'id' ),
'database_id' => self::get_value( 'database_id' ),
),
)
);

register_graphql_enum_type(
'ShippingMethodIdTypeEnum',
array(
'description' => __( 'The Type of Identifier used to fetch a single Shipping Method. Default is ID.', 'wp-graphql' ),
'values' => array(
'id' => self::get_value( 'id' ),
'database_id' => self::get_value( 'database_id' ),
),
)
);
}

/**
* Returns Enum Value definition.
*
* @param string $value Enumeration value being retrieved.
* @return array
*/
private static function get_value( $value ) {
switch ( $value ) {
case 'slug':
return array(
'name' => 'SLUG',
'value' => 'slug',
'description' => __(
'Identify a resource by the slug. Available to non-hierarchcial Types where the slug is a unique identifier.',
'wp-graphql-woocommerce'
),
);
case 'id':
return array(
'name' => 'ID',
'value' => 'global_id',
'description' => __( 'Identify a resource by the (hashed) Global ID.', 'wp-graphql-woocommerce' ),
);
case 'database_id':
return array(
'name' => 'DATABASE_ID',
'value' => 'database_id',
'description' => __( 'Identify a resource by the Database ID.', 'wp-graphql-woocommerce' ),
);
case 'uri':
return array(
'name' => 'URI',
'value' => 'uri',
'description' => __( 'Identify a resource by the URI.', 'wp-graphql-woocommerce' ),
);
}
}
}
2 changes: 1 addition & 1 deletion includes/type/interface/class-product-attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static function get_fields() {
return ! empty( $attribute->get_name() ) ? $attribute->get_name() : null;
},
),
'options' => array(
'options' => array(
'type' => array( 'list_of' => 'String' ),
'description' => __( 'Attribute options', 'wp-graphql-woocommerce' ),
'resolve' => function ( $attribute ) {
Expand Down
103 changes: 73 additions & 30 deletions includes/type/interface/class-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,48 +55,91 @@ public static function register_interface( &$type_registry ) {
'type' => 'Product',
'description' => __( 'A product object', 'wp-graphql-woocommerce' ),
'args' => array(
'id' => array(
'type' => array( 'non_null' => 'ID' ),
'id' => array(
'type' => array( 'non_null' => 'ID' ),
'description' => __( 'The ID for identifying the product', 'wp-graphql-woocommerce' ),
),
'idType' => array(
'type' => 'ProductIdTypeEnum',
'description' => __( 'Type of ID being used identify product', 'wp-graphql-woocommerce' ),
),
),
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
$id_components = Relay::fromGlobalId( $args['id'] );
if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
throw new UserError( __( 'The ID input is invalid', 'wp-graphql-woocommerce' ) );
$id = isset( $args['id'] ) ? $args['id'] : null;
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';

$product_id = null;
switch ( $id_type ) {
case 'sku':
$product_id = \wc_get_product_id_by_sku( $id );
break;
case 'slug':
$post = get_page_by_path( $id, OBJECT, 'product' );
$product_id = ! empty( $post ) ? absint( $post->ID ) : 0;
break;
case 'database_id':
$product_id = absint( $id );
break;
case 'global_id':
default:
$id_components = Relay::fromGlobalId( $id );
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
throw new UserError( __( 'The "global ID" is invalid', 'wp-graphql-woocommerce' ) );
}
$product_id = absint( $id_components['id'] );
break;
}

if ( empty( $product_id ) ) {
/* translators: %1$s: ID type, %2$s: ID value */
throw new UserError( sprintf( __( 'No product ID was found corresponding to the %1$s: %2$s' ), $id_type, $id ) );
} elseif ( get_post( $product_id )->post_type !== 'product' ) {
/* translators: %1$s: ID type, %2$s: ID value */
throw new UserError( sprintf( __( 'No product exists with the %1$s: %2$s' ), $id_type, $id ) );
}
$product_id = absint( $id_components['id'] );
return Factory::resolve_crud_object( $product_id, $context );

$product = Factory::resolve_crud_object( $product_id, $context );

return $product;
},
)
);

$post_by_args = array(
'id' => array(
'type' => 'ID',
'description' => __( 'Get the product by its global ID', 'wp-graphql-woocommerce' ),
),
'productId' => array(
'type' => 'Int',
'description' => __( 'Get the product by its database ID', 'wp-graphql-woocommerce' ),
),
'slug' => array(
'type' => 'String',
'description' => __( 'Get the product by its slug', 'wp-graphql-woocommerce' ),
),
'sku' => array(
'type' => 'String',
'description' => __( 'Get the product by its sku', 'wp-graphql-woocommerce' ),
),
);

/**
* DEPRECATED
*
* Will be removed in v0.5.x.
*/
register_graphql_field(
'RootQuery',
'productBy',
array(
'type' => 'Product',
'description' => __( 'A product object', 'wp-graphql-woocommerce' ),
'args' => $post_by_args,
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
'type' => 'Product',
'isDeprecated' => true,
'deprecationReason' => __(
'This query has been deprecation, and will be removed in v0.5.x. Please use "product(id: value, idType: DATABASE_ID|SLUG|SKU)" instead',
'wp-graphql-woocommerce'
),
'description' => __( 'A product object', 'wp-graphql-woocommerce' ),
'args' => array(
'id' => array(
'type' => 'ID',
'description' => __( 'Get the product by its global ID', 'wp-graphql-woocommerce' ),
),
'productId' => array(
'type' => 'Int',
'description' => __( 'Get the product by its database ID', 'wp-graphql-woocommerce' ),
),
'slug' => array(
'type' => 'String',
'description' => __( 'Get the product by its slug', 'wp-graphql-woocommerce' ),
),
'sku' => array(
'type' => 'String',
'description' => __( 'Get the product by its sku', 'wp-graphql-woocommerce' ),
),
),
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
$product_id = 0;
$id_type = '';
if ( ! empty( $args['id'] ) ) {
Expand Down
Loading