| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPGraphQL\Data\Connection; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class MenuConnectionResolver |
| 7 |
* |
| 8 |
* @package WPGraphQL\Data\Connection |
| 9 |
*/ |
| 10 |
class MenuConnectionResolver extends TermObjectConnectionResolver { |
| 11 |
|
| 12 |
/** |
| 13 |
* {@inheritDoc} |
| 14 |
* |
| 15 |
* @throws \Exception |
| 16 |
*/ |
| 17 |
protected function prepare_query_args( array $args ): array { |
| 18 |
$term_args = [ |
| 19 |
'hide_empty' => false, |
| 20 |
'include' => [], |
| 21 |
'taxonomy' => 'nav_menu', |
| 22 |
'fields' => 'ids', |
| 23 |
]; |
| 24 |
|
| 25 |
if ( ! empty( $args['where']['slug'] ) ) { |
| 26 |
$term_args['slug'] = $args['where']['slug']; |
| 27 |
$term_args['include'] = null; |
| 28 |
} |
| 29 |
|
| 30 |
$theme_locations = get_nav_menu_locations(); |
| 31 |
|
| 32 |
// If a location is specified in the args, use it |
| 33 |
if ( ! empty( $args['where']['location'] ) ) { |
| 34 |
// Exclude unset and non-existent locations |
| 35 |
$term_args['include'] = ! empty( $theme_locations[ $args['where']['location'] ] ) ? $theme_locations[ $args['where']['location'] ] : -1; |
| 36 |
// If the current user cannot edit theme options |
| 37 |
} elseif ( ! current_user_can( 'edit_theme_options' ) ) { |
| 38 |
$term_args['include'] = array_values( $theme_locations ); |
| 39 |
} |
| 40 |
|
| 41 |
if ( ! empty( $args['where']['id'] ) ) { |
| 42 |
$term_args['include'] = $args['where']['id']; |
| 43 |
} |
| 44 |
|
| 45 |
$query_args = parent::prepare_query_args( $args ); |
| 46 |
|
| 47 |
return array_merge( $query_args, $term_args ); |
| 48 |
} |
| 49 |
} |
| 50 |
|