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 / inc / functions / query.php

query.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 2.0.12, at inc/functions/query.php

227 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 * Query functions.
4 *
5 * @package ContentControl
6 */
7
8 namespace ContentControl;
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Get the main query.
14 *
15 * @return \WP_Query|null
16 */
17 function get_main_wp_query() {
18 global $wp_the_query;
19
20 if ( ! is_null( $wp_the_query ) ) {
21 /**
22 * WP Query object.
23 *
24 * @var \WP_Query $wp_the_query
25 */
26 return $wp_the_query;
27 }
28
29 return null;
30 }
31
32 /**
33 * Get the current wp query.
34 *
35 * Helper that returns the current query object, reguardless of if
36 * it's the main query or not.
37 *
38 * @return \WP_Query|null
39 */
40 function get_current_wp_query() {
41 global $wp_query;
42
43 if ( ! is_null( $wp_query ) ) {
44 /**
45 * WP Query object.
46 *
47 * @var \WP_Query $wp_query
48 */
49 return $wp_query;
50 }
51
52 return get_main_wp_query();
53 }
54
55 /**
56 * Get the current query.
57 *
58 * @param \WP_Query|null $query Query object.
59 *
60 * @return \WP_Query|null
61 */
62 function get_query( $query = null ) {
63 /**
64 * Current query object.
65 *
66 * @var null|\WP_Query $cc_current_query
67 */
68 global $cc_current_query;
69
70 if ( is_null( $query ) ) {
71 if ( isset( $cc_current_query ) ) {
72 /**
73 * WP Query object.
74 *
75 * @var \WP_Query $query
76 */
77 $query = $cc_current_query;
78 } else {
79 $query = get_current_wp_query();
80 }
81 }
82
83 return $query;
84 }
85
86 /**
87 * Set the current query context.
88 *
89 * @param string $context 'main', 'main/posts', 'posts', 'main/blocks', 'blocks`.
90 *
91 * @return void
92 */
93 function override_query_context( $context ) {
94 global $cc_current_query_context;
95 $cc_current_query_context = $context;
96 }
97
98 /**
99 * Reset the current query context.
100 *
101 * @return void
102 */
103 function reset_query_context() {
104 global $cc_current_query_context;
105 unset( $cc_current_query_context );
106 }
107
108 /**
109 * Get or set the current rule (globaly accessible).
110 *
111 * 'main', 'main/posts', 'posts', 'main/blocks', 'blocks`
112 *
113 * Rules can work differently depending on the context they are being checked in.
114 * This context allows us to handle the main query differently to other queries,
115 * and blocks. It further allows us to handle blocks in several unique ways per
116 * rule.
117 *
118 * 1. Main query is checked in the template_redirect action.
119 * 2. Main query posts are checked in the the_posts filter & $wp_query->is_main_query().
120 * 3. Alternate query posts are checked in the_posts or pre_get_posts & ! $wp_query->is_main_query().
121 * 4. Blocks are checked in the content_control/should_hide_block filter.
122 *
123 * @param \WP_Query|null $query Query object.
124 *
125 * @return string 'main', 'main/posts', 'posts', 'main/blocks', 'blocks`.
126 */
127 function current_query_context( $query = null ) {
128 global $cc_current_query_context;
129
130 if ( isset( $cc_current_query_context ) ) {
131 return $cc_current_query_context;
132 }
133
134 $query = get_query( $query );
135 $is_main = $query->is_main_query();
136
137 // Blocks in the main page or other locations.
138 if ( doing_filter( 'content_control/should_hide_block' ) ) {
139 return $is_main ? 'main/blocks' : 'blocks';
140 }
141
142 // Main query (page/psst/home/search/archive etc) (template_redirect).
143 if ( $is_main && doing_action( 'template_redirect' ) ) {
144 return 'main';
145 }
146
147 if ( doing_filter( 'pre_get_posts' ) || doing_filter( 'the_posts' ) ) {
148 return $is_main ? 'main/posts' : 'posts';
149 }
150
151 // Default to posts.
152 return 'posts';
153 }
154
155 /**
156 * Set the current rule (globaly accessible).
157 *
158 * Because we check posts in `the_posts`, we can't trust the global $wp_query
159 * has been set yet, so we need to manage global state ourselves.
160 *
161 * @param \WP_Query|null $query WP_Query object.
162 *
163 * @return void
164 */
165 function set_rules_query( $query ) {
166 /**
167 * Current query object.
168 *
169 * @var null|\WP_Query $cc_current_query
170 */
171 global $cc_current_query;
172 $cc_current_query = $query;
173 }
174
175 /**
176 * Check and overload global post if needed.
177 *
178 * This has no effect when checking global queries ($post_id = null).
179 *
180 * @param int|\WP_Post|null $post_id Post ID.
181 *
182 * @return bool
183 */
184 function setup_post( $post_id = null ) {
185 global $post, $content_control_last_post;
186
187 if ( ! isset( $content_control_last_post ) ) {
188 $content_control_last_post = [];
189 }
190
191 if ( is_null( $post_id ) ) {
192 return false;
193 }
194
195 $current_post_id = isset( $post ) ? $post->ID : null;
196
197 $overload_post =
198 ( is_object( $post_id ) && $post_id->ID !== $current_post_id ) ||
199 ( is_int( $post_id ) && $post_id !== $current_post_id );
200
201 if ( $overload_post ) {
202 $content_control_last_post[] = isset( $post ) ? $post : $current_post_id;
203 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
204 $post = get_post( $post_id );
205 setup_postdata( $post );
206 }
207
208 return $overload_post;
209 }
210
211 /**
212 * Check and clear global post if needed.
213 *
214 * @return void
215 */
216 function clear_post() {
217 global $post, $content_control_last_post;
218 if ( ! empty( $content_control_last_post ) ) {
219 // Get the last post ID from the array.
220 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
221 $post = array_pop( $content_control_last_post );
222
223 // Reset global post object.
224 setup_postdata( $post );
225 }
226 }
227