PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.6.1
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.6.1
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.6.1, at classes/Controllers/Frontend/Restrictions/MainQuery.php

195 lines 5.7 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 $method = $restriction->get_setting( 'protectionMethod' );
91
92 switch ( $method ) {
93 case 'redirect':
94 redirect( $restriction->get_setting( 'redirectType' ), $restriction->get_setting( 'redirectUrl' ) );
95 return;
96
97 case 'replace':
98 if ( 'page' === $restriction->get_setting( 'replacementType' ) ) {
99 set_query_to_page( $restriction->get_setting( 'replacementPage' ) );
100 return;
101 }
102 }
103 }
104 }
105
106 /**
107 * Handle restrictions on the main query posts.
108 *
109 * NOTE: This is only for redirecting or replacing archives and
110 * should not be used to filter or hide post contents.
111 *
112 * @return void
113 */
114 public function check_main_query_posts() {
115 $query = get_main_wp_query();
116
117 if ( query_can_be_ignored( $query ) ) {
118 return;
119 }
120
121 // Ensure rules are checked in the correct context.
122 override_query_context( 'main/posts' );
123
124 // Get restrictions for the queried posts.
125 $post_restrictions = get_restriction_matches_for_queried_posts( $query );
126
127 // Reset query context.
128 reset_query_context();
129
130 if ( ! $post_restrictions ) {
131 return;
132 }
133
134 // Only the highest priority restriction is needed with redirect or replace handling.
135 $restriction_match = false;
136
137 // Find the highest priority restriction with redirect or replace handling.
138 foreach ( $post_restrictions as $post_restriction ) {
139 /**
140 * Restriction object.
141 *
142 * @var \ContentControl\Models\Restriction
143 */
144 $restriction = $post_restriction['restriction'];
145
146 if ( 'redirect' === $restriction->get_setting( 'archiveHandling' ) || 'replace_archive_page' === $restriction->get_setting( 'archiveHandling' ) ) {
147 $restriction_match = $post_restriction;
148 break;
149 }
150 }
151
152 if ( false === $restriction_match ) {
153 return;
154 }
155
156 /**
157 * Restriction object.
158 *
159 * @var \ContentControl\Models\Restriction
160 */
161 $restriction = $restriction_match['restriction'];
162 $post_ids = $restriction_match['post_ids'];
163
164 /**
165 * Use this filter to prevent a post from being restricted, or to handle it yourself.
166 *
167 * @param null|mixed $pre Whether to prevent the post from being restricted.
168 * @param null|\ContentControl\Models\Restriction $restriction Restriction object.
169 * @param int[] $post_id Post ID.
170 *
171 * @return null|mixed
172 */
173 if ( null !== apply_filters( 'content_control/pre_restrict_main_query_post', null, $restriction, $post_ids ) ) {
174 return;
175 }
176
177 /**
178 * Fires when a post is restricted, but before the restriction is handled.
179 *
180 * @param \ContentControl\Models\Restriction $restriction Restriction object.
181 * @param int[] $post_id Post ID.
182 */
183 do_action( 'content_control/restrict_main_query_post', $restriction, $post_ids );
184
185 switch ( $restriction->get_setting( 'archiveHandling' ) ) {
186 case 'replace_archive_page':
187 set_query_to_page( $restriction->get_setting( 'archiveReplacementPage' ) );
188 break;
189 case 'redirect':
190 redirect( $restriction->get_setting( 'archiveRedirectType' ), $restriction->get_setting( 'archiveRedirectUrl' ) );
191 break;
192 }
193 }
194 }
195