Files
wp-graphql-woocommerce/includes/data/connection/class-shipping-method-connection-resolver.php
T
a7714d0cd2 Updates to be compatible with WPGraphQL v1.6.1 (#537)
* fix: Huge refactor across schema and unit tests

* - add shipping_method loader in class-core-schema-filters.php

* - Update class-wc-db-loader.php to respect the new SHIPPING_METHOD loader with a `load_shipping_method_from_id()` function

* - Convert Shipping_Method_Connection_Resolver to extend AbstractConnectionResolver

* - Replace resolver for Shipping Methods connection. Remove use of `resolveNode` and replace the resolver with the new connection class `get_connection()` call

* chore: Factory class refactored.

* - update the function that registers Product Types to the Schema to use the value from the `get_enabled_product_types()` as the Type name and the key as the fieldName

* chore: PaymentGatewayQueriesTest refactored.

* chore: WPCS compliance met

* chore: Syntax error fixed.

Co-authored-by: Jason Bahl <jasonbahl@mac.com>
2021-08-11 14:26:09 -04:00

123 lines
2.5 KiB
PHP

<?php
/**
* ConnectionResolver - Shipping_Method_Connection_Resolver
*
* Resolves connections to ShippingMethod
*
* @package WPGraphQL\WooCommerce\Data\Connection
* @since 0.0.2
*/
namespace WPGraphQL\WooCommerce\Data\Connection;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQLRelay\Relay;
use WPGraphQL\AppContext;
use WPGraphQL\Data\Connection\AbstractConnectionResolver;
/**
* Class Shipping_Method_Connection_Resolver
*/
class Shipping_Method_Connection_Resolver extends AbstractConnectionResolver {
/**
* Return the name of the loader to be used with the connection resolver
*
* @return string
*/
public function get_loader_name() {
return 'shipping_method';
}
/**
* Confirms if shipping methods should be retrieved.
*
* @return bool
*/
public function should_execute() {
return true;
}
/**
* Creates filters for shipping methods.
*
* @return array|void
*/
public function get_query_args() {
// TODO: Implement get_query_args() method.
}
/**
* Executes query
*
* @return array|mixed|string[]
*/
public function get_query() {
// TODO: Implement get_query() method.
$wc_shipping = \WC_Shipping::instance();
$methods = $wc_shipping->get_shipping_methods();
// Get shipping method IDs.
$methods = array_map(
function( $item ) {
return $item->id;
},
array_values( $methods )
);
return $methods;
}
/**
* Return an array of items from the query
*
* @return array|mixed
*/
public function get_ids() {
return ! empty( $this->query ) ? $this->query : array();
}
/**
* Validates offset.
*
* @param mixed $offset Decoded query cursor.
*
* @return bool
*/
public function is_valid_offset( $offset ) {
return is_string( $offset );
}
/**
* Get_offset
*
* This returns the offset to be used in the $query_args based on the $args passed to the
* GraphQL query.
*
* @return int|mixed
*/
public function get_offset() {
/**
* Defaults
*/
$offset = 0;
/**
* Get the $after offset
*/
if ( ! empty( $this->args['after'] ) ) {
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
$offset = substr( base64_decode( $this->args['after'] ), strlen( 'arrayconnection:' ) );
} elseif ( ! empty( $this->args['before'] ) ) {
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
$offset = substr( base64_decode( $this->args['before'] ), strlen( 'arrayconnection:' ) );
}
/**
* Return the higher of the two values
*/
return $offset;
}
}