| 1 |
<?php |
| 2 |
/** |
| 3 |
* Option functions. |
| 4 |
* |
| 5 |
* @package ContentControl |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace ContentControl; |
| 9 |
|
| 10 |
/** |
| 11 |
* Redirect to the appropriate location. |
| 12 |
* |
| 13 |
* @param string $type login|home|custom. |
| 14 |
* @param string $url Custom URL. |
| 15 |
* |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
function redirect( $type = 'login', $url = null ) { |
| 19 |
switch ( $type ) { |
| 20 |
case 'login': |
| 21 |
$url = wp_login_url( \ContentControl\get_current_page_url() ); |
| 22 |
break; |
| 23 |
|
| 24 |
case 'home': |
| 25 |
$url = home_url(); |
| 26 |
break; |
| 27 |
|
| 28 |
default: |
| 29 |
case 'custom': |
| 30 |
add_filter( 'allowed_redirect_hosts', function ( $hosts ) use ( $url ) { |
| 31 |
$hosts[] = wp_parse_url( $url, PHP_URL_HOST ); |
| 32 |
|
| 33 |
return $hosts; |
| 34 |
} ); |
| 35 |
} |
| 36 |
|
| 37 |
if ( $url ) { |
| 38 |
wp_safe_redirect( $url ); |
| 39 |
exit; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Set the query to the page with the specified ID. |
| 45 |
* |
| 46 |
* @param int $page_id Page ID. |
| 47 |
* @param \WP_Query $query Query object. |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
function set_query_to_page( $page_id, $query = null ) { |
| 51 |
if ( ! $page_id ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
if ( ! $query ) { |
| 56 |
$query = get_current_wp_query(); |
| 57 |
} |
| 58 |
|
| 59 |
// Create a new custom query for the specific page. |
| 60 |
$args = [ |
| 61 |
'page_id' => $page_id, |
| 62 |
'post_type' => 'page', |
| 63 |
'posts_per_page' => 1, |
| 64 |
// Used to bypass the restrictions and not add processing to the new query. |
| 65 |
'ignore_restrictions' => true, |
| 66 |
]; |
| 67 |
|
| 68 |
$custom_query = new \WP_Query( $args ); |
| 69 |
|
| 70 |
if ( ! $custom_query->have_posts() ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
$query->init(); // Reset the main query. |
| 75 |
$query->query_vars = $args; |
| 76 |
|
| 77 |
// phpcs:disable:Squiz.PHP.CommentedOutCode.Found |
| 78 |
// $query->queried_object = $custom_query->post; |
| 79 |
// $query->queried_object_id = $page_id; |
| 80 |
// $query->post = $custom_query->post; |
| 81 |
// $query->posts = $custom_query->posts; |
| 82 |
// $query->query = $custom_query->query; |
| 83 |
|
| 84 |
// // Since init, only override defaults as needed to emulate page. |
| 85 |
// $query->is_page = true; |
| 86 |
// $query->is_singular = true; |
| 87 |
// $query->found_posts = 1; |
| 88 |
// $query->post_count = 1; |
| 89 |
// $query->max_num_pages = 1; |
| 90 |
|
| 91 |
// // Suppress filters. Might not need this. |
| 92 |
// $query->set( 'suppress_filters', true ); |
| 93 |
// phpcs:enable:Squiz.PHP.CommentedOutCode.Found |
| 94 |
|
| 95 |
// Ensure all query vars are set. |
| 96 |
$query->get_posts(); |
| 97 |
|
| 98 |
// Reset the post data. |
| 99 |
$query->reset_postdata(); |
| 100 |
} |
| 101 |
|