Content restriction plugin 08
July 6, 2015
Planning the content restriction framework next sprint to handle mixed queries.
Edge case?
I've decided to base the restriction system on taxonomies to leverage the hierarchical nature of those and the way those are supported in the WordPress query system.
The framework allows for some post types, not all, to be restricted and to define what restriction should be used to restrict which post type; the problem arises when one single query
- queries for restricted and unrestricted post types
- queries for post types that are restricted using different taxonomies
To make an example given the post
,page
and notice
post types, given the post
page is restricted with the user_role
taxonomy and the notice
post type is restricted with the user_country
taxonomy then two possible "edge cases" would be
$posts = (new WP_Query('post_type' => ['post', 'page']))->get_posts();
and
$posts = (new WP_Query('post_type' => ['post', 'notice']))->get_posts();
Post not in
The solution to handle both cases I've found is hooking into the pre_get_posts
filter to manipulate the post__not_in
query var.
In terms of queries the solution comes with a cost paid in sub-queries that I'm willing to test
if querying for restricted post types or querying for a mix of restricted and unrestricted post types
for each restricted post type in the queried post types
get the inaccessible IDs for the post type using a tax query using the taxonomies restricting the post type
set the post__not_in query var on the query to prevent inaccessible posts from being returned in the query
else if querying for restricted post types only and if all of the post types are restricted using the same taxonomies
add a taxonomy query to the query
let the query run
Reworking much of the code
I will start reworking the code to accommodate for this solution and see what will come out.