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