Content restriction plugin 06

A content restriction usage issue.

The problem

While developing, and all the while testing, the content restriction framework plugin I’ve run into a common issue that I had, but, not taken into consideration.
Given this posts in the database where the page post type is restricted using the restricting_tax taxonomy and each page has one term of that taxonomy applied

  • post 1
  • post 2
  • post 3
  • page 1 - term_1
  • page 2 - term_2
  • page 3 - term_1

queries for the post post type would yield all the posts as expected

// will return 3 posts
$posts = (new WP_Query(['post_type' => 'post']))->get_posts();

queries for the page post type, given a user that has access to the term_1 term of the restricting_tax taxonomy would return 2 posts

// will return 2 pages
$pages = (new WP_Query(['post_type' => 'page']))->get_posts();

but querying for both the post types will return just 2 pages

// will only return 2 pages
$pages_and_posts = (new WP_Query(['post_type' => ['page', 'page']))->get_posts();

That’s how the tax query works. Under the hood a WP_Query like this one is being made

$pages_and_posts =  (new WP_Query(
        [
            'post_type' => ['post', 'page'],
            'tax_query' => [
                [
                'taxonomy' => 'restricting_tax',
                'fields' => 'slugs',
                'terms' => ['term_1']
                'operator' => 'IN'
                ]
            ]
        ]
    ));

The restricting_tax taxonomy does not apply to the post post type and hence no post of the post type will be returned; in plain english the taxonomy query states:

Return any post of the post and page type that has a term from the restricting_tax taxonomy applied AND that has the restricting_tax term_1 applied.

A partial solution

I’ve implemented a first solution to the problem discriminating queries for one or more restricted post types from queries where restricted and unrestricted post types are mixed.
In essence a separate query is launched to add the inaccessible post IDs to the post__not_in query parameter. While the solution works it will fail when more than one restricting taxonomy is registered and different post types are restricted using different taxonomies: while separate queries seem to be the way to go I’m also taking the possibility of switching to post meta entirely to store the restriction details.
The meta query would allow for queries that would spell, in plain english, like:

Return any post of the post and page type that either has a meta with the key restricting_meta applied AND the meta_1 meta value OR for that has no restricting_meta meta key applied at all.

While it seems to be a bargain the hierarchical power of taxonomies would be lost in the process so that’s a solution that might require some thinking.