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
|
|
|
|
2021-08-11 14:26:09 -04:00
|
|
|
use WPGraphQL\Data\Connection\AbstractConnectionResolver;
|
2024-05-17 00:03:10 -04:00
|
|
|
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
|
|
|
|
|
*/
|
2021-08-11 14:26:09 -04:00
|
|
|
class Shipping_Method_Connection_Resolver extends AbstractConnectionResolver {
|
2019-04-12 18:28:08 -04:00
|
|
|
/**
|
2021-08-11 14:26:09 -04:00
|
|
|
* Return the name of the loader to be used with the connection resolver
|
2019-04-12 18:28:08 -04:00
|
|
|
*
|
2021-08-11 14:26:09 -04:00
|
|
|
* @return string
|
2019-04-12 18:28:08 -04:00
|
|
|
*/
|
2021-08-11 14:26:09 -04:00
|
|
|
public function get_loader_name() {
|
|
|
|
|
return 'shipping_method';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Confirms if shipping methods should be retrieved.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function should_execute() {
|
2024-05-17 00:03:10 -04:00
|
|
|
if ( ! wc_rest_check_manager_permissions( 'shipping_methods', 'read' ) ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
graphql_debug( __( 'Permission denied.', 'graphql-for-ecommerce' ) );
|
2024-05-17 00:03:10 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
2021-08-11 14:26:09 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates filters for shipping methods.
|
|
|
|
|
*
|
|
|
|
|
* @return array|void
|
|
|
|
|
*/
|
|
|
|
|
public function get_query_args() {
|
|
|
|
|
// TODO: Implement get_query_args() method.
|
2022-08-26 14:53:36 -04:00
|
|
|
return [];
|
2021-08-11 14:26:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Executes query
|
|
|
|
|
*
|
2024-05-17 00:03:10 -04:00
|
|
|
* @return int[]
|
2021-08-11 14:26:09 -04:00
|
|
|
*/
|
|
|
|
|
public function get_query() {
|
2024-05-17 00:03:10 -04:00
|
|
|
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 ) {
|
2024-08-06 18:07:37 -04:00
|
|
|
$this->get_loader()->prime( $method->id, new Shipping_Method( $method ) );
|
2024-05-17 00:03:10 -04:00
|
|
|
}
|
2019-04-12 18:28:08 -04:00
|
|
|
|
|
|
|
|
// Get shipping method IDs.
|
2024-05-17 00:03:10 -04:00
|
|
|
$methods = wp_list_pluck( array_values( $methods ), 'id' );
|
2019-04-12 18:28:08 -04:00
|
|
|
|
2021-08-11 14:26:09 -04:00
|
|
|
return $methods;
|
2019-04-12 18:28:08 -04:00
|
|
|
}
|
2021-08-11 14:26:09 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return an array of items from the query
|
|
|
|
|
*
|
2024-05-17 00:03:10 -04:00
|
|
|
* @return array
|
2021-08-11 14:26:09 -04:00
|
|
|
*/
|
2024-05-17 00:03:10 -04:00
|
|
|
public function get_ids_from_query() {
|
2022-08-26 14:53:36 -04:00
|
|
|
return ! empty( $this->query ) ? $this->query : [];
|
2021-08-11 14:26:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
}
|