| 1 |
<?php |
| 2 |
namespace WPGraphQL\Data\Connection; |
| 3 |
|
| 4 |
/** |
| 5 |
* Class TaxonomyConnectionResolver |
| 6 |
* |
| 7 |
* @package WPGraphQL\Data\Connection |
| 8 |
* @extends \WPGraphQL\Data\Connection\AbstractConnectionResolver<string[]> |
| 9 |
*/ |
| 10 |
class TaxonomyConnectionResolver extends AbstractConnectionResolver { |
| 11 |
|
| 12 |
/** |
| 13 |
* {@inheritDoc} |
| 14 |
*/ |
| 15 |
public function get_ids_from_query() { |
| 16 |
/** |
| 17 |
* @todo This is for b/c. We can just use $this->get_query(). |
| 18 |
*/ |
| 19 |
$queried = isset( $this->query ) ? $this->query : $this->get_query(); |
| 20 |
|
| 21 |
$ids = []; |
| 22 |
|
| 23 |
if ( empty( $queried ) ) { |
| 24 |
return $ids; |
| 25 |
} |
| 26 |
|
| 27 |
foreach ( $queried as $item ) { |
| 28 |
$ids[] = $item; |
| 29 |
} |
| 30 |
|
| 31 |
return $ids; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* {@inheritDoc} |
| 36 |
*/ |
| 37 |
protected function prepare_query_args( array $args ): array { |
| 38 |
// If any args are added to filter/sort the connection. |
| 39 |
return []; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* {@inheritDoc} |
| 44 |
*/ |
| 45 |
protected function query( array $query_args ) { |
| 46 |
if ( isset( $query_args['name'] ) ) { |
| 47 |
return [ $query_args['name'] ]; |
| 48 |
} |
| 49 |
|
| 50 |
if ( isset( $query_args['in'] ) ) { |
| 51 |
return is_array( $query_args['in'] ) ? $query_args['in'] : [ $query_args['in'] ]; |
| 52 |
} |
| 53 |
|
| 54 |
return \WPGraphQL::get_allowed_taxonomies( 'names', $query_args ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* {@inheritDoc} |
| 59 |
*/ |
| 60 |
protected function loader_name(): string { |
| 61 |
return 'taxonomy'; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* {@inheritDoc} |
| 66 |
* |
| 67 |
* @param string $offset The offset (taxonomy name) to check. |
| 68 |
*/ |
| 69 |
public function is_valid_offset( $offset ) { |
| 70 |
return (bool) get_taxonomy( $offset ); |
| 71 |
} |
| 72 |
} |
| 73 |
|