| 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 |
| 61 |
*/ |
| 62 |
function get_query( $query = null ) { |
| 63 |
global $cc_current_query; |
| 64 |
|
| 65 |
if ( is_null( $query ) ) { |
| 66 |
if ( isset( $cc_current_query ) && ! is_null( $cc_current_query ) ) { |
| 67 |
/** |
| 68 |
* WP Query object. |
| 69 |
* |
| 70 |
* @var \WP_Query $query |
| 71 |
*/ |
| 72 |
$query = $cc_current_query; |
| 73 |
} else { |
| 74 |
$query = get_current_wp_query(); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return $query; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Set the current query context. |
| 83 |
* |
| 84 |
* @param string $context 'main', 'main/posts', 'posts', 'main/blocks', 'blocks`. |
| 85 |
*/ |
| 86 |
function override_query_context( $context ) { |
| 87 |
global $cc_current_query_context; |
| 88 |
$cc_current_query_context = $context; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Reset the current query context. |
| 93 |
*/ |
| 94 |
function reset_query_context() { |
| 95 |
global $cc_current_query_context; |
| 96 |
unset( $cc_current_query_context ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get or set the current rule (globaly accessible). |
| 101 |
* |
| 102 |
* 'main', 'main/posts', 'posts', 'main/blocks', 'blocks` |
| 103 |
* |
| 104 |
* Rules can work differently depending on the context they are being checked in. |
| 105 |
* This context allows us to handle the main query differently to other queries, |
| 106 |
* and blocks. It further allows us to handle blocks in several unique ways per |
| 107 |
* rule. |
| 108 |
* |
| 109 |
* 1. Main query is checked in the template_redirect action. |
| 110 |
* 2. Main query posts are checked in the the_posts filter & $wp_query->is_main_query(). |
| 111 |
* 3. Alternate query posts are checked in the_posts or pre_get_posts & ! $wp_query->is_main_query(). |
| 112 |
* 4. Blocks are checked in the content_control/should_hide_block filter. |
| 113 |
* |
| 114 |
* @param \WP_Query|null $query Query object. |
| 115 |
* |
| 116 |
* @return string 'main', 'main/posts', 'posts', 'main/blocks', 'blocks`. |
| 117 |
*/ |
| 118 |
function current_query_context( $query = null ) { |
| 119 |
global $cc_current_query_context; |
| 120 |
|
| 121 |
if ( isset( $cc_current_query_context ) ) { |
| 122 |
return $cc_current_query_context; |
| 123 |
} |
| 124 |
|
| 125 |
$query = get_query( $query ); |
| 126 |
$is_main = $query->is_main_query(); |
| 127 |
|
| 128 |
// Blocks in the main page or other locations. |
| 129 |
if ( doing_filter( 'content_control/should_hide_block' ) ) { |
| 130 |
return $is_main ? 'main/blocks' : 'blocks'; |
| 131 |
} |
| 132 |
|
| 133 |
// Main query (page/psst/home/search/archive etc) (template_redirect). |
| 134 |
if ( $is_main && doing_action( 'template_redirect' ) ) { |
| 135 |
return 'main'; |
| 136 |
} |
| 137 |
|
| 138 |
if ( doing_filter( 'pre_get_posts' ) || doing_filter( 'the_posts' ) ) { |
| 139 |
return $is_main ? 'main/posts' : 'posts'; |
| 140 |
} |
| 141 |
|
| 142 |
// Default to posts. |
| 143 |
return 'posts'; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Set the current rule (globaly accessible). |
| 148 |
* |
| 149 |
* Because we check posts in `the_posts`, we can't trust the global $wp_query |
| 150 |
* has been set yet, so we need to manage global state ourselves. |
| 151 |
* |
| 152 |
* @param string $query WP_Query object. |
| 153 |
* |
| 154 |
* @return void |
| 155 |
*/ |
| 156 |
function set_rules_query( $query ) { |
| 157 |
global $cc_current_query; |
| 158 |
$cc_current_query = $query; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Check and overload global post if needed. |
| 163 |
* |
| 164 |
* This has no effect when checking global queries ($post_id = null). |
| 165 |
* |
| 166 |
* @param int|null $post_id Post ID. |
| 167 |
* |
| 168 |
* @return bool |
| 169 |
*/ |
| 170 |
function setup_post( $post_id = null ) { |
| 171 |
global $post; |
| 172 |
|
| 173 |
if ( is_null( $post_id ) ) { |
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
$current_post_id = isset( $post ) ? $post->ID : null; |
| 178 |
|
| 179 |
$overload_post = isset( $post_id ) && ( |
| 180 |
( is_object( $post_id ) && $post_id->ID !== $current_post_id ) || |
| 181 |
( is_int( $post_id ) && $post_id !== $current_post_id ) ); |
| 182 |
|
| 183 |
if ( $overload_post ) { |
| 184 |
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 185 |
$post = get_post( $post_id ); |
| 186 |
setup_postdata( $post ); |
| 187 |
} |
| 188 |
|
| 189 |
return $overload_post; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Check and clear global post if needed. |
| 194 |
* |
| 195 |
* @param bool $overload_post Whether post was overloaded. |
| 196 |
* |
| 197 |
* @return void |
| 198 |
*/ |
| 199 |
function clear_post( $overload_post = false ) { |
| 200 |
if ( $overload_post ) { |
| 201 |
// Reset global post object. |
| 202 |
wp_reset_postdata(); |
| 203 |
} |
| 204 |
} |
| 205 |
|