PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.6.4
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.6.4
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / classes / Controllers / Frontend / Restrictions / QueryTerms.php

QueryTerms.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 2.6.4, at classes/Controllers/Frontend/Restrictions/QueryTerms.php

161 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Frontend query post restrictions.
4 *
5 * @copyright (c) 2023, Code Atlantic LLC.
6 * @package ContentControl
7 */
8
9 namespace ContentControl\Controllers\Frontend\Restrictions;
10
11 use ContentControl\Base\Controller;
12
13 use function ContentControl\query_can_be_ignored;
14 use function ContentControl\protection_is_disabled;
15 use function ContentControl\get_restriction_matches_for_queried_terms;
16 use function ContentControl\is_rest;
17
18 defined( 'ABSPATH' ) || exit;
19
20 /**
21 * Class for handling global restrictions of the query posts.
22 *
23 * @package ContentControl
24 */
25 class QueryTerms extends Controller {
26
27 /**
28 * Initiate functionality.
29 *
30 * @return void
31 */
32 public function init() {
33 // We delay this until functions.php is loaded, so that users can use the content_control/query_filter_init_hook filter.
34 // The assumption is that most code should be registered by init 999999, so we'll use that as the default.
35 add_action( 'init', [ $this, 'register_hooks' ], 999999 );
36 }
37
38 /**
39 * Register hooks.
40 *
41 * @return void
42 */
43 public function register_hooks() {
44 /**
45 * Use this filter to change the hook used to add query post filtering.
46 *
47 * This only applies to alternate queries, not the main query, and is used for removing
48 * posts from the query that are restricted.
49 *
50 * - Register earlier for more restriction coverage.
51 * - Register later for more compatibility with other plugins that late register post types.
52 *
53 * @param null|string $init_hook The hook to use to add the query post filtering.
54 * @return null|string The hook to use, should be: wp_loaded, or maybe even parse_query or wp (if you know what you're doing).
55 */
56 $init_hook = apply_filters( 'content_control/query_filter_init_hook', null );
57
58 /**
59 * Use this filter to change the priority used to add query post filtering.
60 *
61 * @param int $init_priority The priority to use to add the query post filtering.
62 * @return int The priority to use. Default: 999.
63 */
64 $init_priority = apply_filters( 'content_control/query_filter_init_priority', 999 );
65
66 if ( is_null( $init_hook ) || ! did_action( $init_hook ) ) {
67 // If the user has not specified a hook, we'll use the default (now).
68 $this->enable_query_filtering();
69 return;
70 }
71
72 add_action( (string) $init_hook, [ $this, 'enable_query_filtering' ], (int) $init_priority );
73 }
74
75 /**
76 * Late hooks.
77 *
78 * @return void
79 */
80 public function enable_query_filtering() {
81 add_filter( 'get_terms', [ $this, 'restrict_query_terms' ], 10, 4 );
82 }
83
84 /**
85 * Handle restricted content appropriately.
86 *
87 * NOTE. This is only for filtering terms, and should not
88 * be used to redirect or replace the entire page.
89 *
90 * @param \WP_Term[] $terms Array of terms to filter.
91 * @param string $taxonomy The taxonomy.
92 * @param array<string,mixed> $query_vars Array of query vars.
93 * @param \WP_Term_Query $query The WP_Query instance (passed by reference).
94 *
95 * @return \WP_Term[]
96 */
97 public function restrict_query_terms( $terms, $taxonomy, $query_vars, $query ) {
98 if ( query_can_be_ignored( $query ) ) {
99 return $terms;
100 }
101
102 if ( protection_is_disabled() ) {
103 return $terms;
104 }
105
106 $term_restrictions = get_restriction_matches_for_queried_terms( $query );
107
108 if ( false === $term_restrictions ) {
109 return $terms;
110 }
111
112 // If we have restrictions on the queried terms, handle them top down.
113 foreach ( $term_restrictions as $match ) {
114 $term_id = $match['term_ids'];
115 $restriction = $match['restriction'];
116
117 /**
118 * Use this filter to prevent a term from being restricted, or to handle it yourself.
119 *
120 * @param null|mixed $pre Whether to prevent the term from being restricted.
121 * @param null|\ContentControl\Models\Restriction $restriction Restriction object.
122 * @param int[] $term_id Term ID.
123 * @return null|mixed
124 */
125 if ( null !== apply_filters( 'content_control/pre_restrict_archive_term', null, $restriction, $term_id ) ) {
126 continue;
127 }
128
129 /**
130 * Fires when a term is restricted, but before the restriction is handled.
131 *
132 * @param \ContentControl\Models\Restriction $restriction Restriction object.
133 * @param int[] $term_id Perm ID.
134 */
135 do_action( 'content_control/restrict_archive_term', $restriction, $term_id );
136
137 $handling = $restriction->get_setting( 'additionalQueryHandling' );
138
139 // If this is a search query and showInSearch is false, hide the post.
140 if ( $query->query_vars['search'] && false === $restriction->get_setting( 'showInSearch' ) ) {
141 $handling = 'hide';
142 }
143
144 switch ( $handling ) {
145 case 'hide':
146 foreach ( $terms as $key => $term ) {
147 if ( in_array( $term->term_id, $term_id, true ) ) {
148 unset( $terms[ $key ] );
149 }
150 }
151
152 // Reset term indexes.
153 $query->terms = array_values( $terms );
154 break;
155 }
156 }
157
158 return $terms;
159 }
160 }
161