Files

96 lines
2.0 KiB
PHP
Raw Permalink Normal View History

2019-04-12 18:28:08 -04:00
<?php
/**
* ConnectionResolver - Shipping_Method_Connection_Resolver
*
* Resolves connections to ShippingMethod
*
2019-10-25 19:13:36 -04:00
* @package WPGraphQL\WooCommerce\Data\Connection
2019-04-12 18:28:08 -04:00
* @since 0.0.2
*/
2019-10-25 19:13:36 -04:00
namespace WPGraphQL\WooCommerce\Data\Connection;
2019-04-12 18:28:08 -04:00
use WPGraphQL\Data\Connection\AbstractConnectionResolver;
use WPGraphQL\WooCommerce\Model\Shipping_Method;
use WPGraphQL\WooCommerce\Model\Shipping_Zone;
2019-04-12 18:28:08 -04:00
/**
* Class Shipping_Method_Connection_Resolver
*/
class Shipping_Method_Connection_Resolver extends AbstractConnectionResolver {
2019-04-12 18:28:08 -04:00
/**
* Return the name of the loader to be used with the connection resolver
2019-04-12 18:28:08 -04:00
*
* @return string
2019-04-12 18:28:08 -04:00
*/
public function get_loader_name() {
return 'shipping_method';
}
/**
* Confirms if shipping methods should be retrieved.
*
* @return bool
*/
public function should_execute() {
if ( ! wc_rest_check_manager_permissions( 'shipping_methods', 'read' ) ) {
graphql_debug( __( 'Permission denied.', 'graphql-for-ecommerce' ) );
return false;
}
return true;
}
/**
* Creates filters for shipping methods.
*
* @return array|void
*/
public function get_query_args() {
// TODO: Implement get_query_args() method.
return [];
}
/**
* Executes query
*
* @return int[]
*/
public function get_query() {
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->get_loader()->prime( $method->id, new Shipping_Method( $method ) );
}
2019-04-12 18:28:08 -04:00
// Get shipping method IDs.
$methods = wp_list_pluck( array_values( $methods ), 'id' );
2019-04-12 18:28:08 -04:00
return $methods;
2019-04-12 18:28:08 -04:00
}
/**
* Return an array of items from the query
*
* @return array
*/
public function get_ids_from_query() {
return ! empty( $this->query ) ? $this->query : [];
}
/**
* Validates offset.
*
* @param mixed $offset Decoded query cursor.
*
* @return bool
*/
public function is_valid_offset( $offset ) {
return is_string( $offset );
}
2019-04-12 18:28:08 -04:00
}