Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: split AbstractConnectionResolver::get_args() and ::get_query_args() into ::prepare_*() methods #3124

Merged
merged 9 commits into from
May 8, 2024
319 changes: 222 additions & 97 deletions src/Data/Connection/AbstractConnectionResolver.php

Large diffs are not rendered by default.

44 changes: 20 additions & 24 deletions src/Data/Connection/CommentConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ class CommentConnectionResolver extends AbstractConnectionResolver {
*
* @throws \GraphQL\Error\UserError
*/
public function get_query_args() {

protected function prepare_query_args( array $args ): array {
justlevine marked this conversation as resolved.
Show resolved Hide resolved
/**
* Prepare for later use
*/
$last = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
$last = ! empty( $args['last'] ) ? $args['last'] : null;

$query_args = [];

Expand All @@ -56,18 +55,18 @@ public function get_query_args() {
$query_args['orderby'] = 'comment_date';

/**
* Take any of the $this->args that were part of the GraphQL query and map their
* Take any of the $args that were part of the GraphQL query and map their
* GraphQL names to the WP_Term_Query names to be used in the WP_Term_Query
*
* @since 0.0.5
*/
$input_fields = [];
if ( ! empty( $this->args['where'] ) ) {
$input_fields = $this->sanitize_input_fields( $this->args['where'] );
if ( ! empty( $args['where'] ) ) {
$input_fields = $this->sanitize_input_fields( $args['where'] );
}

/**
* Merge the default $query_args with the $this->args that were entered
* Merge the default $query_args with the $args that were entered
* in the query.
*
* @since 0.0.5
Expand Down Expand Up @@ -113,14 +112,14 @@ public function get_query_args() {
$query_args['graphql_before_cursor'] = $this->get_before_offset();

/**
* Pass the graphql $this->args to the WP_Query
* Pass the graphql $args to the WP_Query
*/
$query_args['graphql_args'] = $this->args;
$query_args['graphql_args'] = $args;

// encode the graphql args as a cache domain to ensure the
// graphql_args are used to identify different queries.
// see: https://core.trac.wordpress.org/ticket/35075
$encoded_args = wp_json_encode( $this->args );
$encoded_args = wp_json_encode( $args );
$query_args['cache_domain'] = ! empty( $encoded_args ) ? 'graphql:' . md5( $encoded_args ) : 'graphql';

/**
Expand All @@ -130,8 +129,7 @@ public function get_query_args() {
$query_args['fields'] = 'ids';

/**
* Filter the query_args that should be applied to the query. This filter is applied AFTER the input args from
* the GraphQL Query have been applied and has the potential to override the GraphQL Query Input Args.
* Filter the query_args that should be applied to the query.
*
* @param array<string,mixed> $query_args array of query_args being passed to the
* @param mixed $source source passed down from the resolve tree
Expand All @@ -141,7 +139,7 @@ public function get_query_args() {
*
* @since 0.0.6
*/
return apply_filters( 'graphql_comment_connection_query_args', $query_args, $this->source, $this->args, $this->context, $this->info );
return apply_filters( 'graphql_comment_connection_query_args', $query_args, $this->source, $args, $this->context, $this->info );
}

/**
Expand All @@ -151,7 +149,7 @@ public function get_query_args() {
* @throws \Exception
*/
public function get_query() {
return new WP_Comment_Query( $this->query_args );
return new WP_Comment_Query( $this->get_query_args() );
}

/**
Expand All @@ -169,21 +167,19 @@ public function get_ids_from_query() {
$ids = ! empty( $this->query->get_comments() ) ? $this->query->get_comments() : [];

// If we're going backwards, we need to reverse the array.
if ( ! empty( $this->args['last'] ) ) {
$args = $this->get_args();

if ( ! empty( $args['last'] ) ) {
$ids = array_reverse( $ids );
}

return $ids;
}

/**
* Filters the GraphQL args before they are used in get_query_args().
*
* @return array<string,mixed>
* {@inheritDoc}
*/
public function get_args(): array {
$args = $this->get_unfiltered_args();

protected function prepare_args( array $args ): array {
justlevine marked this conversation as resolved.
Show resolved Hide resolved
justlevine marked this conversation as resolved.
Show resolved Hide resolved
if ( ! empty( $args['where'] ) ) {
// Ensure all IDs are converted to database IDs.
foreach ( $args['where'] as $input_key => $input_value ) {
Expand Down Expand Up @@ -238,8 +234,8 @@ static function ( $id ) {
/**
* Filters the GraphQL args before they are used in get_query_args().
*
* @param array<string,mixed> $args The GraphQL args passed to the resolver.
* @param \WPGraphQL\Data\Connection\CommentConnectionResolver $connection_resolver Instance of the ConnectionResolver
* @param array<string,mixed> $args The GraphQL args passed to the resolver.
* @param self $resolver Instance of the ConnectionResolver
*
* @since 1.11.0
*/
Expand Down Expand Up @@ -298,7 +294,7 @@ public function sanitize_input_fields( array $args ) {
*
* @since 0.0.5
*/
$query_args = apply_filters( 'graphql_map_input_fields_to_wp_comment_query', $query_args, $args, $this->source, $this->args, $this->context, $this->info );
$query_args = apply_filters( 'graphql_map_input_fields_to_wp_comment_query', $query_args, $args, $this->source, $this->get_args(), $this->context, $this->info );

return ! empty( $query_args ) && is_array( $query_args ) ? $query_args : [];
}
Expand Down
13 changes: 7 additions & 6 deletions src/Data/Connection/ContentTypeConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function get_ids_from_query() {
/**
* {@inheritDoc}
*/
public function get_query_args() {
protected function prepare_query_args( array $args ): array {
// If any args are added to filter/sort the connection
return [];
}
Expand All @@ -46,15 +46,16 @@ public function get_query_args() {
* @return string[]
*/
public function get_query() {
if ( isset( $this->query_args['contentTypeNames'] ) && is_array( $this->query_args['contentTypeNames'] ) ) {
return $this->query_args['contentTypeNames'];
$query_args = $this->get_query_args();

if ( isset( $query_args['contentTypeNames'] ) && is_array( $query_args['contentTypeNames'] ) ) {
return $query_args['contentTypeNames'];
}

if ( isset( $this->query_args['name'] ) ) {
return [ $this->query_args['name'] ];
if ( isset( $query_args['name'] ) ) {
return [ $query_args['name'] ];
}

$query_args = $this->query_args;
return \WPGraphQL::get_allowed_post_types( 'names', $query_args );
}

Expand Down
2 changes: 1 addition & 1 deletion src/Data/Connection/EnqueuedScriptsConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function get_ids_from_query() {
/**
* {@inheritDoc}
*/
public function get_query_args() {
protected function prepare_query_args( array $args ): array {
// If any args are added to filter/sort the connection
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function get_ids_from_query() {
/**
* {@inheritDoc}
*/
public function get_query_args() {
protected function prepare_query_args( array $args ): array {
// If any args are added to filter/sort the connection
return [];
}
Expand Down
16 changes: 8 additions & 8 deletions src/Data/Connection/MenuConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,35 @@ class MenuConnectionResolver extends TermObjectConnectionResolver {
*
* @throws \Exception
*/
public function get_query_args() {
protected function prepare_query_args( array $args ): array {
$term_args = [
'hide_empty' => false,
'include' => [],
'taxonomy' => 'nav_menu',
'fields' => 'ids',
];

if ( ! empty( $this->args['where']['slug'] ) ) {
$term_args['slug'] = $this->args['where']['slug'];
if ( ! empty( $args['where']['slug'] ) ) {
$term_args['slug'] = $args['where']['slug'];
$term_args['include'] = null;
}

$theme_locations = get_nav_menu_locations();

// If a location is specified in the args, use it
if ( ! empty( $this->args['where']['location'] ) ) {
if ( ! empty( $args['where']['location'] ) ) {
// Exclude unset and non-existent locations
$term_args['include'] = ! empty( $theme_locations[ $this->args['where']['location'] ] ) ? $theme_locations[ $this->args['where']['location'] ] : -1;
$term_args['include'] = ! empty( $theme_locations[ $args['where']['location'] ] ) ? $theme_locations[ $args['where']['location'] ] : -1;
// If the current user cannot edit theme options
} elseif ( ! current_user_can( 'edit_theme_options' ) ) {
$term_args['include'] = array_values( $theme_locations );
}

if ( ! empty( $this->args['where']['id'] ) ) {
$term_args['include'] = $this->args['where']['id'];
if ( ! empty( $args['where']['id'] ) ) {
$term_args['include'] = $args['where']['id'];
}

$query_args = parent::get_query_args();
$query_args = parent::prepare_query_args( $args );

return array_merge( $query_args, $term_args );
}
Expand Down
22 changes: 10 additions & 12 deletions src/Data/Connection/MenuItemConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,35 @@ public function __construct( $source, array $args, AppContext $context, ResolveI
/**
* {@inheritDoc}
*/
public function get_query_args() {
protected function prepare_query_args( array $args ): array {
justlevine marked this conversation as resolved.
Show resolved Hide resolved
/**
* Prepare for later use
*/
$last = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
$last = ! empty( $args['last'] ) ? $args['last'] : null;

$menu_locations = get_theme_mod( 'nav_menu_locations' );

$query_args = parent::get_query_args();
$query_args = parent::prepare_query_args( $args );
$query_args['orderby'] = 'menu_order';
$query_args['order'] = isset( $last ) ? 'DESC' : 'ASC';

if ( isset( $this->args['where']['parentDatabaseId'] ) ) {
if ( isset( $args['where']['parentDatabaseId'] ) ) {
$query_args['meta_key'] = '_menu_item_menu_item_parent';
$query_args['meta_value'] = (int) $this->args['where']['parentDatabaseId']; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
$query_args['meta_value'] = (int) $args['where']['parentDatabaseId']; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
}

if ( ! empty( $this->args['where']['parentId'] ) || ( isset( $this->args['where']['parentId'] ) && 0 === (int) $this->args['where']['parentId'] ) ) {
if ( ! empty( $args['where']['parentId'] ) || ( isset( $args['where']['parentId'] ) && 0 === (int) $args['where']['parentId'] ) ) {
$query_args['meta_key'] = '_menu_item_menu_item_parent';
$query_args['meta_value'] = $this->args['where']['parentId']; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
$query_args['meta_value'] = $args['where']['parentId']; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
}

// Get unique list of location term IDs as the default limitation of locations to allow public queries for.
// Public queries should only be allowed to query for Menu Items assigned to a Menu Location.
$locations = is_array( $menu_locations ) && ! empty( $menu_locations ) ? array_unique( array_values( $menu_locations ) ) : [];

// If the location argument is set, set the argument to the input argument
if ( ! empty( $this->args['where']['location'] ) ) {
$locations = isset( $menu_locations[ $this->args['where']['location'] ] ) ? [ $menu_locations[ $this->args['where']['location'] ] ] : []; // We use an empty array to prevent fetching all media items if the location has no items assigned.
if ( ! empty( $args['where']['location'] ) ) {
$locations = isset( $menu_locations[ $args['where']['location'] ] ) ? [ $menu_locations[ $args['where']['location'] ] ] : []; // We use an empty array to prevent fetching all media items if the location has no items assigned.

} elseif ( current_user_can( 'edit_theme_options' ) ) {
// If the $locations are NOT set, let a user with proper capability query all menu items.
Expand Down Expand Up @@ -79,9 +79,7 @@ public function get_query_args() {
/**
* {@inheritDoc}
*/
public function get_args(): array {
$args = $this->get_unfiltered_args();

protected function prepare_args( array $args ): array {
justlevine marked this conversation as resolved.
Show resolved Hide resolved
if ( ! empty( $args['where'] ) ) {
// Ensure all IDs are converted to database IDs.
foreach ( $args['where'] as $input_key => $input_value ) {
Expand Down
21 changes: 12 additions & 9 deletions src/Data/Connection/PluginConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public function get_ids_from_query() {
/**
* {@inheritDoc}
*/
public function get_query_args() {
if ( ! empty( $this->args['where']['status'] ) ) {
$this->args['where']['stati'] = [ $this->args['where']['status'] ];
} elseif ( ! empty( $this->args['where']['stati'] ) && is_string( $this->args['where']['stati'] ) ) {
$this->args['where']['stati'] = [ $this->args['where']['stati'] ];
protected function prepare_query_args( array $args ): array {
if ( ! empty( $args['where']['status'] ) ) {
$args['where']['stati'] = [ $args['where']['status'] ];
} elseif ( ! empty( $args['where']['stati'] ) && is_string( $args['where']['stati'] ) ) {
$args['where']['stati'] = [ $args['where']['stati'] ];
}

return $this->args;
return $args;
}

/**
Expand All @@ -69,6 +69,9 @@ public function get_query() {
return [];
}

// Get the GraphQL args for later.
$query_args = $this->get_query_args();

// Holds the plugin names sorted by status. The other ` status => [ plugin_names ] ` will be added later.
$plugins_by_status = [
'mustuse' => array_flip( array_keys( $plugins['mustuse'] ) ),
Expand All @@ -81,7 +84,7 @@ public function get_query() {
$show_network_plugins = apply_filters( 'show_network_active_plugins', current_user_can( 'manage_network_plugins' ) );

// Store the plugin stati as array keys for performance.
$active_stati = ! empty( $this->args['where']['stati'] ) ? array_flip( $this->args['where']['stati'] ) : [];
$active_stati = ! empty( $query_args['where']['stati'] ) ? array_flip( $query_args['where']['stati'] ) : [];

// Get additional plugin info.
$upgradable_list = $can_update && isset( $active_stati['upgrade'] ) ? get_site_transient( 'update_plugins' ) : [];
Expand Down Expand Up @@ -196,9 +199,9 @@ public function get_query() {
// If plugins exist for the filter, flatten and return them. Otherwise, return the full list.
$filtered_plugins = ! empty( $filtered_plugins ) ? array_merge( [], ...$filtered_plugins ) : $plugins_by_status['all'];

if ( ! empty( $this->args['where']['search'] ) ) {
if ( ! empty( $query_args['where']['search'] ) ) {
// Filter by search args.
$s = sanitize_text_field( $this->args['where']['search'] );
$s = sanitize_text_field( $query_args['where']['search'] );
$matches = array_keys(
array_filter(
$all_plugins,
Expand Down