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.1.2 #86

Merged
merged 16 commits into from
Jun 24, 2019
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ branches:
- develop
- release-v0.1.0
- release-v0.1.1
- release-v0.1.2

cache:
apt: true
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "wp-graphql/wp-graphql-woocommerce",
"description": "WooCommerce bindings for wp-graphql",
"version": "0.1.1",
"version": "0.1.2",
"type": "wordpress-plugin",
"keywords": [
"wordpress",
Expand Down
71 changes: 71 additions & 0 deletions includes/class-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use WPGraphQL\Extensions\WooCommerce\Data\Factory;
use WPGraphQL\Extensions\WooCommerce\Data\Loader\WC_Customer_Loader;
use WPGraphQL\Extensions\WooCommerce\Data\Loader\WC_Post_Crud_Loader;
use WPGraphQL\Extensions\WooCommerce\Utils\QL_Session_Handler;

/**
* Class Filters
Expand All @@ -34,12 +35,24 @@ class Filters {
*/
private static $post_crud_loader;

/**
* Stores instance session header name.
*
* @var string
*/
private static $session_header;

/**
* Register filters
*/
public static function load() {
// Registers WooCommerce taxonomies.
add_filter( 'register_taxonomy_args', array( __CLASS__, 'register_taxonomy_args' ), 10, 2 );

// Add data-loaders to AppContext.
add_filter( 'graphql_data_loaders', array( __CLASS__, 'graphql_data_loaders' ), 10, 2 );

// Filter core connection resolutions.
add_filter(
'graphql_post_object_connection_query_args',
array( __CLASS__, 'graphql_post_object_connection_query_args' ),
Expand All @@ -52,6 +65,13 @@ public static function load() {
10,
5
);

// Setup QL session handler.
self::$session_header = apply_filters( 'woocommerce_session_header_name', 'woocommerce-session' );
add_filter( 'woocommerce_cookie', array( __CLASS__, 'woocommerce_cookie' ) );
add_filter( 'woocommerce_session_handler', array( __CLASS__, 'init_ql_session_handler' ) );
add_filter( 'graphql_response_headers_to_send', array( __CLASS__, 'add_session_header_to_expose_headers' ) );
add_filter( 'graphql_access_control_allow_headers', array( __CLASS__, 'add_session_header_to_allow_headers' ) );
}

/**
Expand Down Expand Up @@ -182,4 +202,55 @@ public static function graphql_post_object_connection_query_args( $query_args, $
public static function graphql_term_object_connection_query_args( $query_args, $source, $args, $context, $info ) {
return WC_Terms_Connection_Resolver::get_query_args( $query_args, $source, $args, $context, $info );
}

/**
* Filters WooCommerce cookie key to be used as a HTTP Header on GraphQL HTTP requests
*
* @param string $cookie WooCommerce cookie key.
*
* @return string
*/
public static function woocommerce_cookie( $cookie ) {
return self::$session_header;
}

/**
* Filters WooCommerce session handler class on GraphQL HTTP requests
*
* @param string $session_class Classname of the current session handler class.
*
* @return string
*/
public static function init_ql_session_handler( $session_class ) {
return QL_Session_Handler::class;
}

/**
* Append session header to the exposed headers in GraphQL responses
*
* @param array $headers GraphQL responser headers.
*
* @return array
*/
public static function add_session_header_to_expose_headers( $headers ) {
if ( empty( $headers['Access-Control-Expose-Headers'] ) ) {
$headers['Access-Control-Expose-Headers'] = apply_filters( 'woocommerce_cookie', self::$session_header );
} else {
$headers['Access-Control-Expose-Headers'] .= ', ' . apply_filters( 'woocommerce_cookie', self::$session_header );
}

return $headers;
}

/**
* Append the session header to the allowed headers in GraphQL responses
*
* @param array $allowed_headers The existing allowed headers.
*
* @return array
*/
public static function add_session_header_to_allow_headers( array $allowed_headers ) {
$allowed_headers[] = self::$session_header;
return $allowed_headers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function resolve( $source, array $args, AppContext $context, ResolveInfo
$nodes[] = ! empty( $edge['node'] ) ? $edge['node'] : null;
}
}
$connection['nodes'] = ! empty( $nodes ) ? $nodes : null;
return ! empty( $items ) ? $connection : null;
$connection['nodes'] = ! empty( $nodes ) ? $nodes : array();
return $connection;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,30 @@
use GraphQL\Type\Definition\ResolveInfo;
use GraphQLRelay\Relay;
use WPGraphQL\AppContext;
use WPGraphQL\Extensions\WooCommerce\Model\Product;

/**
* Class Product_Attribute_Connection_Resolver
*/
class Product_Attribute_Connection_Resolver {
/**
* Builds Product attribute items
*
* @param array $attributes Array of WC_Product_Attributes instances.
* @param Product $source Parent product model.
*
* @return array
*/
private function get_items( $attributes, $source ) {
$items = array();
foreach ( $attributes as $attribute_name => $data ) {
$data->_relay_id = base64_encode( $attribute_name . '||' . $source->ID . '||' . $data->get_id() );
$items[] = $data;
}

return $items;
}

/**
* Creates connection
*
Expand All @@ -27,13 +46,7 @@ class Product_Attribute_Connection_Resolver {
* @param ResolveInfo $info - ResolveInfo object.
*/
public function resolve( $source, array $args, AppContext $context, ResolveInfo $info ) {
// @codingStandardsIgnoreStart
if ( 'defaultAttributes' === $info->fieldName ) {
// @codingStandardsIgnoreEnd
$attributes = $source->default_attributes;
} else {
$attributes = $source->attributes;
}
$attributes = $this->get_items( $source->attributes, $source );

$connection = Relay::connectionFromArray( $attributes, $args );
$nodes = array();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public function sanitize_input_fields( array $where_args ) {
if ( in_array( $where_args['attribute'], \wc_get_attribute_taxonomy_names(), true ) ) {
$tax_query[] = array(
'taxonomy' => $where_args['attribute'],
'field' => 'term_id',
'field' => 'slug',
'terms' => $where_args['attributeTerm'],
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,31 @@ class Variation_Attribute_Connection_Resolver {
/**
* Returns data array from WC_Product_Attribute ArrayAccess object.
*
* @param WC_Product_Attribute $attrs - WC_Product_Attribute object.
* @param WC_Product_Attribute $attrs - WC_Product_Attribute object.
* @param string $variation_id - ProductVariation Relay ID.
*
* @return array
*/
public function to_data_array( $attrs = array() ) {
public function to_data_array( $attrs = array(), $parent_id = 0 ) {
$attributes = array();
if ( array( '0' ) !== $attrs ) {
foreach ( $attrs as $name => $value ) {
$term = \get_term_by( 'slug', $value, $name );
if ( empty( $term ) ) {
$attributes[] = array(
'id' => 0,
'name' => $name,
'value' => $value,
// ID create for caching only, not object retrieval.
'id' => base64_encode( $parent_id . '||' . $name . '||' . $value ),
'attributeId' => 0,
'name' => $name,
'value' => $value,
);
} else {
$attributes[] = array(
'id' => $term->term_id,
'name' => $term->taxonomy,
'value' => $term->name,
// ID create for caching only, not object retrieval.
'id' => base64_encode( $parent_id . '||' . $name . '||' . $value ),
'attributeId' => $term->term_id,
'name' => $term->taxonomy,
'value' => $term->name,
);
}
}
Expand All @@ -60,9 +66,9 @@ public function to_data_array( $attrs = array() ) {
*/
public function resolve( $source, array $args, AppContext $context, ResolveInfo $info ) {
if ( is_a( $source, Product::class ) ) {
$attributes = $this->to_data_array( $source->default_attributes );
$attributes = $this->to_data_array( $source->default_attributes, $source->ID );
} else {
$attributes = $this->to_data_array( $source->attributes );
$attributes = $this->to_data_array( $source->attributes, $source->ID );
}

$connection = Relay::connectionFromArray( $attributes, $args );
Expand Down
2 changes: 1 addition & 1 deletion includes/model/class-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ protected function init() {
return array( '0' );
},
'attributes' => function() {
return ! empty( $this->data->get_attributes() ) ? array_values( $this->data->get_attributes() ) : array( '0' );
return ! empty( $this->data->get_attributes() ) ? $this->data->get_attributes() : array( '0' );
},
'default_attributes' => function() {
return ! empty( $this->data->get_default_attributes() ) ? $this->data->get_default_attributes() : array( '0' );
Expand Down
9 changes: 8 additions & 1 deletion includes/type/object/class-product-attribute-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ public static function register() {
array(
'description' => __( 'A product attribute object', 'wp-graphql-woocommerce' ),
'fields' => array(
'id' => array(
'type' => array( 'non_null' => 'ID' ),
'description' => __( 'Attribute Global ID', 'wp-graphql-woocommerce' ),
'resolve' => function ( $attribute ) {
return ! empty( $attribute->_relay_id ) ? $attribute->_relay_id : null;
},
),
'attributeId' => array(
'type' => array( 'non_null' => 'Int' ),
'description' => __( 'Attribute ID', 'wp-graphql-woocommerce' ),
'resolve' => function ( $attribute ) {
return ! empty( $attribute->get_id() ) ? $attribute->get_id() : null;
return ! is_null( $attribute->get_id() ) ? $attribute->get_id() : null;
},
),
'name' => array(
Expand Down
15 changes: 11 additions & 4 deletions includes/type/object/class-variation-attribute-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,28 @@ public static function register() {
array(
'description' => __( 'A product variation attribute object', 'wp-graphql-woocommerce' ),
'fields' => array(
'id' => array(
'type' => 'Int',
'id' => array(
'type' => array( 'non_null' => 'ID' ),
'description' => __( 'The Id of the order. Equivalent to WP_Post->ID', 'wp-graphql-woocommerce' ),
'resolve' => function ( $source ) {
return isset( $source['id'] ) ? $source['id'] : null;
},
),
'name' => array(
'attributeId' => array(
'type' => 'Int',
'description' => __( 'The Id of the order. Equivalent to WP_Post->ID', 'wp-graphql-woocommerce' ),
'resolve' => function ( $source ) {
return isset( $source['attributeId'] ) ? $source['attributeId'] : null;
},
),
'name' => array(
'type' => 'String',
'description' => __( 'Name of attribute', 'wp-graphql-woocommerce' ),
'resolve' => function ( $source ) {
return isset( $source['name'] ) ? $source['name'] : null;
},
),
'value' => array(
'value' => array(
'type' => 'String',
'description' => __( 'Selected value of attribute', 'wp-graphql-woocommerce' ),
'resolve' => function ( $source ) {
Expand Down
Loading