Skip to content

Commit

Permalink
(feat): validate passed query params before initiating WP_Query
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike van den Hoek committed Jan 4, 2024
1 parent 7ed52d9 commit 6028ddd
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 42 deletions.
97 changes: 67 additions & 30 deletions src/Base/Repositories/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
namespace OWC\PDC\Base\Repositories;

use Closure;
use WP_Post;
use WP_Query;
use OWC\PDC\Base\Exceptions\PropertyNotExistsException;
use OWC\PDC\Base\Support\CreatesFields;
use OWC\PDC\Base\Support\Traits\QueryHelpers;
use OWC\PDC\Base\Exceptions\PropertyNotExistsException;
use WP_Post;
use WP_Query;

/**
* PDC item object with default quering and methods.
Expand Down Expand Up @@ -98,35 +98,29 @@ public function __construct()

/**
* Get all the items from the database.
*
* @return array
*/
public function all(): array
{
$args = array_merge($this->queryArgs, [
'post_type' => [$this->posttype],
]);

$this->query = new WP_Query($args);
$this->query = new WP_Query($this->cleanParams($args));

return array_map([$this, 'transform'], $this->getQuery()->posts);
}

/**
* Find a particular pdc item by ID.
*
* @param int $id
*
* @return array
*/
public function find(int $id)
public function find(int $id): ?array
{
$args = array_merge($this->queryArgs, [
'p' => $id,
'p' => $id,
'post_type' => [$this->posttype],
]);

$this->query = new WP_Query($args);
$this->query = new WP_Query($this->cleanParams($args));

if (empty($this->getQuery()->posts)) {
return null;
Expand All @@ -137,19 +131,15 @@ public function find(int $id)

/**
* Find a particular pdc item by slug.
*
* @param string $slug
*
* @return array|null
*/
public function findBySlug(string $slug)
public function findBySlug(string $slug): ?array
{
$args = array_merge($this->queryArgs, [
'name' => $slug,
'name' => $slug,
'post_type' => [$this->posttype],
]);

$this->query = new WP_Query($args);
$this->query = new WP_Query($this->cleanParams($args));

if (empty($this->getQuery()->posts)) {
return null;
Expand All @@ -158,6 +148,53 @@ public function findBySlug(string $slug)
return $this->transform(reset($this->getQuery()->posts));
}

protected function cleanParams(array $args): array
{
$args = $this->validatePostStatusParam($args);
$args = $this->cleanWronglyNestedQueryParams($args, 'tax_query');
$args = $this->cleanWronglyNestedQueryParams($args, 'meta_query');

return $args;
}

protected function validatePostStatusParam(array $args): array
{
if (empty($args['post_status'])) {
return $args;
}

if (! is_string($args['post_status']) && ! is_array($args['post_status'])) {
unset($args['post_status']);

return $args;
}

if (is_string($args['post_status'])) {
$args['post_status'] = [$args['post_status']];
}

if (! \is_user_logged_in()) {
$args['post_status'] = ['publish'];
}

return $args;
}

protected function cleanWronglyNestedQueryParams(array $args, string $key): array
{
if (empty($args[$key]) || ! is_array($args[$key])) {
return $args;
}

foreach ($args[$key] as &$query) {
if (is_array($query) && ! empty($query[0])) {
$query = call_user_func_array('array_merge', $query);
}
}

return $args;
}

/**
* Get the WP_Query object.
*
Expand Down Expand Up @@ -223,8 +260,8 @@ public function addField(string $key, CreatesFields $creator)
public static function addGlobalField(string $key, CreatesFields $creator, Closure $conditional = null)
{
static::$globalFields[] = [
'key' => $key,
'creator' => $creator,
'key' => $key,
'creator' => $creator,
'conditional' => $conditional,
];
}
Expand Down Expand Up @@ -265,15 +302,15 @@ public function transform(WP_Post $post)
}

$data = [
'id' => $post->ID,
'title' => $post->post_title,
'slug' => $post->post_name,
'content' => $this->isAllowed($post) ? apply_filters('the_content', $post->post_content) : "",
'excerpt' => $this->isAllowed($post) ? $post->post_excerpt : "",
'date' => $post->post_date,
'slug' => $post->post_name,
'id' => $post->ID,
'title' => $post->post_title,
'slug' => $post->post_name,
'content' => $this->isAllowed($post) ? apply_filters('the_content', $post->post_content) : "",
'excerpt' => $this->isAllowed($post) ? $post->post_excerpt : "",
'date' => $post->post_date,
'slug' => $post->post_name,
'post_status' => $post->post_status,
'protected' => ! $this->isAllowed($post)
'protected' => ! $this->isAllowed($post),
];

$data = $this->assignFields($data, $post);
Expand Down
35 changes: 27 additions & 8 deletions src/Base/RestAPI/Controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

namespace OWC\PDC\Base\RestAPI\Controllers;

use OWC\PDC\Base\Foundation\Plugin;
use WP_Query;
use WP_REST_Request;
use OWC\PDC\Base\Foundation\Plugin;

/**
* Controller which handels general quering, such as pagination.
Expand Down Expand Up @@ -39,14 +39,14 @@ protected function addPaginator(array $data, WP_Query $query): array
$page = 0 == $page ? 1 : $page;

return array_merge([
'data' => $data
'data' => $data,
], [
'pagination' => [
'total_count' => (int) $query->found_posts,
'total_pages' => $query->max_num_pages,
'total_count' => (int) $query->found_posts,
'total_pages' => $query->max_num_pages,
'current_page' => $page,
'limit' => $query->get('posts_per_page')
]
'limit' => $query->get('posts_per_page'),
],
]);
}

Expand All @@ -55,10 +55,29 @@ protected function addPaginator(array $data, WP_Query $query): array
*/
protected function getPaginatorParams(WP_REST_Request $request, int $limit = 10): array
{
return array_merge($request->get_params(), [
$params = array_merge($request->get_params(), [
'posts_per_page' => $request->get_param('limit') ?: $limit,
'paged' => $request->get_param('page') ?: 0
'paged' => $request->get_param('page') ?: 0,
]);

return $this->validateQueryParams($params);
}

protected function validateQueryParams(array $params): array
{
$allowedQueryParams = [
'include-connected',
'tax_query',
'meta_query',
'posts_per_page',
'paged',
'post_type',
'post_status',
];

return array_filter($params, function ($param) use ($allowedQueryParams) {
return in_array($param, $allowedQueryParams);
}, ARRAY_FILTER_USE_KEY);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Base/RestAPI/SharedFields/ItemsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

namespace OWC\PDC\Base\RestAPI\SharedFields;

use WP_Post;
use OWC\PDC\Base\Support\Traits\QueryHelpers;
use OWC\PDC\Base\Support\Traits\CheckPluginActive;
use OWC\PDC\Base\RestAPI\ItemFields\ConnectedField;
use OWC\PDC\Base\Support\Traits\CheckPluginActive;
use OWC\PDC\Base\Support\Traits\QueryHelpers;
use WP_Post;

/**
* Adds connected fields to item in API.
Expand Down Expand Up @@ -44,7 +44,7 @@ protected function extraQueryArgs(string $type): array
}

$query['connected_query'] = [
'post_status' => ['publish', 'draft'],
'post_status' => ['publish', 'draft'], // Draft only for logged in users?
];

return $query;
Expand Down

0 comments on commit 6028ddd

Please sign in to comment.