Skip to content

Commit

Permalink
Merge pull request #972 from CodeProKid/bug/961/many-queries-from-mod…
Browse files Browse the repository at this point in the history
…el-layer

Allow developers to short circuit potentially expensive is_private ch…
  • Loading branch information
jasonbahl committed Jan 22, 2021
2 parents 3f2a6c5 + 1df226e commit f15bbb5
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,32 @@ public function get_visibility() {
*/
$protected_cap = apply_filters( 'graphql_restricted_data_cap', $this->restricted_cap, $this->get_model_name(), $this->data, $this->visibility, $this->owner, $this->current_user );

/**
* Filter to short circuit default is_private check for the model. This is expensive in some cases so
* this filter lets you prevent this from running by returning a true or false value.
*
* @param string $model_name Name of the model the filter is currently being executed in
* @param mixed $data The un-modeled incoming data
* @param string|null $visibility The visibility that has currently been set for the data at this point
* @param null|int $owner The user ID for the owner of this piece of data
* @param WP_User $current_user The current user for the session
*
* @return bool|null
*/
$pre_is_private = apply_filters( 'graphql_pre_model_data_is_private', null, $this->get_model_name(), $this->data, $this->visibility, $this->owner, $this->current_user );

// If 3rd party code has not filtered this, use the Models default logic to determine
// whether the model should be considered private
if ( null !== $pre_is_private ) {
$is_private = $pre_is_private;
} else {
$is_private = $this->is_private();
}

/**
* Filter to determine if the data should be considered private or not
*
* @param boolean $is_private Whether the model is private
* @param string $model_name Name of the model the filter is currently being executed in
* @param mixed $data The un-modeled incoming data
* @param string|null $visibility The visibility that has currently been set for the data at this point
Expand All @@ -233,7 +256,7 @@ public function get_visibility() {
*
* @return bool
*/
$is_private = apply_filters( 'graphql_data_is_private', $this->is_private(), $this->get_model_name(), $this->data, $this->visibility, $this->owner, $this->current_user );
$is_private = apply_filters( 'graphql_data_is_private', $is_private, $this->get_model_name(), $this->data, $this->visibility, $this->owner, $this->current_user );

if ( true === $is_private ) {
$this->visibility = 'private';
Expand Down

0 comments on commit f15bbb5

Please sign in to comment.