PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.0.12
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.0.12
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 / MainQuery.php

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

193 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Frontend main query 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\redirect;
14 use function ContentControl\get_main_wp_query;
15 use function ContentControl\set_query_to_page;
16 use function ContentControl\reset_query_context;
17 use function ContentControl\query_can_be_ignored;
18 use function ContentControl\content_is_restricted;
19 use function ContentControl\override_query_context;
20 use function ContentControl\protection_is_disabled;
21 use function ContentControl\get_applicable_restriction;
22 use function ContentControl\get_restriction_matches_for_queried_posts;
23
24 defined( 'ABSPATH' ) || exit;
25
26 /**
27 * Class for handling global restrictions of the Main Query.
28 *
29 * @package ContentControl
30 */
31 class MainQuery extends Controller {
32
33 /**
34 * Initiate functionality.
35 */
36 public function init() {
37 // This can be done no later than template_redirect, and no sooner than send_headers (when conditional tags are available).
38 // Can be done on send_headers, posts_selection, or wp as well.
39 add_action( 'template_redirect', [ $this, 'restrict_main_query' ], 10 );
40 }
41
42 /**
43 * Handle a restriction on the main query.
44 *
45 * NOTE: This is only for redirecting or replacing pages and
46 * should not be used to filter or hide post contents.
47 *
48 * @return void
49 */
50 public function restrict_main_query() {
51 if ( ! \is_main_query() || protection_is_disabled() ) {
52 return;
53 }
54
55 $this->check_main_query();
56 $this->check_main_query_posts();
57 }
58
59 /**
60 * Handle restrictions on the main query.
61 *
62 * NOTE: This is only for redirecting or replacing archives and
63 * should not be used to filter or hide post contents.
64 *
65 * @return void
66 */
67 public function check_main_query() {
68 // Bail if we didn't match any restrictions.
69 if ( content_is_restricted() ) {
70 $restriction = get_applicable_restriction();
71
72 /**
73 * Use this filter to prevent a post from being restricted, or to handle it yourself.
74 *
75 * @param null|mixed $pre Whether to prevent the post from being restricted.
76 * @param null|\ContentControl\Models\Restriction $restriction Restriction object.
77 * @return null|mixed
78 */
79 if ( null !== apply_filters( 'content_control/pre_restrict_main_query', null, $restriction ) ) {
80 return;
81 }
82
83 /**
84 * Fires when a post is restricted, but before the restriction is handled.
85 *
86 * @param \ContentControl\Models\Restriction $restriction Restriction object.
87 */
88 do_action( 'content_control/restrict_main_query', $restriction );
89
90 switch ( $restriction->protection_method ) {
91 case 'redirect':
92 redirect( $restriction->redirect_type, $restriction->redirect_url );
93 return;
94
95 case 'replace':
96 if ( 'page' === $restriction->replacement_type ) {
97 set_query_to_page( $restriction->replacement_page );
98 return;
99 }
100 }
101 }
102 }
103
104 /**
105 * Handle restrictions on the main query posts.
106 *
107 * NOTE: This is only for redirecting or replacing archives and
108 * should not be used to filter or hide post contents.
109 *
110 * @return void
111 */
112 public function check_main_query_posts() {
113 $query = get_main_wp_query();
114
115 if ( query_can_be_ignored( $query ) ) {
116 return;
117 }
118
119 // Ensure rules are checked in the correct context.
120 override_query_context( 'main/posts' );
121
122 // Get restrictions for the queried posts.
123 $post_restrictions = get_restriction_matches_for_queried_posts( $query );
124
125 // Reset query context.
126 reset_query_context();
127
128 if ( ! $post_restrictions ) {
129 return;
130 }
131
132 // Only the highest priority restriction is needed with redirect or replace handling.
133 $restriction_match = false;
134
135 // Find the highest priority restriction with redirect or replace handling.
136 foreach ( $post_restrictions as $post_restriction ) {
137 /**
138 * Restriction object.
139 *
140 * @var \ContentControl\Models\Restriction
141 */
142 $restriction = $post_restriction['restriction'];
143
144 if ( 'redirect' === $restriction->archive_handling || 'replace_archive_page' === $restriction->archive_handling ) {
145 $restriction_match = $post_restriction;
146 break;
147 }
148 }
149
150 if ( false === $restriction_match ) {
151 return;
152 }
153
154 /**
155 * Restriction object.
156 *
157 * @var \ContentControl\Models\Restriction
158 */
159 $restriction = $restriction_match['restriction'];
160 $post_ids = $restriction_match['post_ids'];
161
162 /**
163 * Use this filter to prevent a post from being restricted, or to handle it yourself.
164 *
165 * @param null|mixed $pre Whether to prevent the post from being restricted.
166 * @param null|\ContentControl\Models\Restriction $restriction Restriction object.
167 * @param int[] $post_id Post ID.
168 *
169 * @return null|mixed
170 */
171 if ( null !== apply_filters( 'content_control/pre_restrict_main_query_post', null, $restriction, $post_ids ) ) {
172 return;
173 }
174
175 /**
176 * Fires when a post is restricted, but before the restriction is handled.
177 *
178 * @param \ContentControl\Models\Restriction $restriction Restriction object.
179 * @param int[] $post_id Post ID.
180 */
181 do_action( 'content_control/restrict_main_query_post', $restriction, $post_ids );
182
183 switch ( $restriction->archive_handling ) {
184 case 'replace_archive_page':
185 set_query_to_page( $restriction->archive_replacement_page );
186 break;
187 case 'redirect':
188 redirect( $restriction->archive_redirect_type, $restriction->archive_redirect_url );
189 break;
190 }
191 }
192 }
193