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

187 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 public function restrict_main_query() {
49 if ( ! \is_main_query() || protection_is_disabled() ) {
50 return;
51 }
52
53 $this->check_main_query();
54 $this->check_main_query_posts();
55 }
56
57 /**
58 * Handle restrictions on the main query.
59 *
60 * NOTE: This is only for redirecting or replacing archives and
61 * should not be used to filter or hide post contents.
62 */
63 public function check_main_query() {
64 // Bail if we didn't match any restrictions.
65 if ( content_is_restricted() ) {
66 $restriction = get_applicable_restriction();
67
68 /**
69 * Use this filter to prevent a post from being restricted, or to handle it yourself.
70 *
71 * @param null $pre Whether to prevent the post from being restricted.
72 * @param null|\ContentControl\Models\Restriction $restriction Restriction object.
73 * @return null|mixed
74 */
75 if ( null !== apply_filters( 'content_control/pre_restrict_main_query', null, $restriction ) ) {
76 return;
77 }
78
79 /**
80 * Fires when a post is restricted, but before the restriction is handled.
81 *
82 * @param \ContentControl\Models\Restriction $restriction Restriction object.
83 */
84 do_action( 'content_control/restrict_main_query', $restriction );
85
86 switch ( $restriction->protection_method ) {
87 case 'redirect':
88 redirect( $restriction->redirect_type, $restriction->redirect_url );
89 return;
90
91 case 'replace':
92 if ( 'page' === $restriction->replacement_type ) {
93 set_query_to_page( $restriction->replacement_page );
94 return;
95 }
96 }
97 }
98 }
99
100 /**
101 * Handle restrictions on the main query posts.
102 *
103 * NOTE: This is only for redirecting or replacing archives and
104 * should not be used to filter or hide post contents.
105 */
106 public function check_main_query_posts() {
107 $query = get_main_wp_query();
108
109 if ( query_can_be_ignored( $query ) ) {
110 return;
111 }
112
113 // Ensure rules are checked in the correct context.
114 override_query_context( 'main/posts' );
115
116 // Get restrictions for the queried posts.
117 $post_restrictions = get_restriction_matches_for_queried_posts( $query );
118
119 // Reset query context.
120 reset_query_context();
121
122 if ( ! $post_restrictions ) {
123 return;
124 }
125
126 // Only the highest priority restriction is needed with redirect or replace handling.
127 $restriction_match = false;
128
129 // Find the highest priority restriction with redirect or replace handling.
130 foreach ( $post_restrictions as $post_restriction ) {
131 /**
132 * Restriction object.
133 *
134 * @var \ContentControl\Models\Restriction
135 */
136 $restriction = $post_restriction['restriction'];
137
138 if ( 'redirect' === $restriction->archive_handling || 'replace_archive_page' === $restriction->archive_handling ) {
139 $restriction_match = $post_restriction;
140 break;
141 }
142 }
143
144 /**
145 * Restriction object.
146 *
147 * @var \ContentControl\Models\Restriction
148 */
149 $restriction = $restriction_match['restriction'];
150 $post_ids = $restriction_match['post_ids'];
151
152 if ( is_int( $post_ids ) ) {
153 $post_ids = [ $post_ids ];
154 }
155
156 /**
157 * Use this filter to prevent a post from being restricted, or to handle it yourself.
158 *
159 * @param null $pre Whether to prevent the post from being restricted.
160 * @param null|\ContentControl\Models\Restriction $restriction Restriction object.
161 * @param int[] $post_id Post ID.
162 *
163 * @return null|mixed
164 */
165 if ( null !== apply_filters( 'content_control/pre_restrict_main_query_post', null, $restriction, $post_ids ) ) {
166 return;
167 }
168
169 /**
170 * Fires when a post is restricted, but before the restriction is handled.
171 *
172 * @param \ContentControl\Models\Restriction $restriction Restriction object.
173 * @param int[] $post_id Post ID.
174 */
175 do_action( 'content_control/restrict_main_query_post', $restriction, $post_ids );
176
177 switch ( $restriction->archive_handling ) {
178 case 'replace_archive_page':
179 set_query_to_page( $restriction->archive_replacement_page );
180 break;
181 case 'redirect':
182 redirect( $restriction->archive_redirect_type, $restriction->archive_redirect_url );
183 break;
184 }
185 }
186 }
187