| 1 |
<?php |
| 2 |
/** |
| 3 |
* Frontend query post 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\query_can_be_ignored; |
| 14 |
use function ContentControl\protection_is_disabled; |
| 15 |
use function ContentControl\get_restriction_matches_for_queried_posts; |
| 16 |
|
| 17 |
defined( 'ABSPATH' ) || exit; |
| 18 |
|
| 19 |
/** |
| 20 |
* Class for handling global restrictions of the query posts. |
| 21 |
* |
| 22 |
* @package ContentControl |
| 23 |
*/ |
| 24 |
class QueryPosts extends Controller { |
| 25 |
|
| 26 |
/** |
| 27 |
* Initiate functionality. |
| 28 |
* |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
public function init() { |
| 32 |
// We delay this until functions.php is loaded, so that users can use the content_control/query_filter_init_hook filter. |
| 33 |
// The assumption is that most code should be registered by init 999999, so we'll use that as the default. |
| 34 |
add_action( 'init', [ $this, 'register_hooks' ], 999999 ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Register hooks. |
| 39 |
* |
| 40 |
* @return void |
| 41 |
*/ |
| 42 |
public function register_hooks() { |
| 43 |
/** |
| 44 |
* Use this filter to change the hook used to add query post filtering. |
| 45 |
* |
| 46 |
* This only applies to alternate queries, not the main query, and is used for removing |
| 47 |
* posts from the query that are restricted. |
| 48 |
* |
| 49 |
* - Register earlier for more restriction coverage. |
| 50 |
* - Register later for more compatibility with other plugins that late register post types. |
| 51 |
* |
| 52 |
* @param null|string $init_hook The hook to use to add the query post filtering. |
| 53 |
* @return null|string The hook to use, should be: wp_loaded, or maybe even parse_query or wp (if you know what you're doing). |
| 54 |
*/ |
| 55 |
$init_hook = apply_filters( 'content_control/query_filter_init_hook', null ); |
| 56 |
|
| 57 |
/** |
| 58 |
* Use this filter to change the priority used to add query post filtering. |
| 59 |
* |
| 60 |
* @param int $init_priority The priority to use to add the query post filtering. |
| 61 |
* @return int The priority to use. Default: 999. |
| 62 |
*/ |
| 63 |
$init_priority = apply_filters( 'content_control/query_filter_init_priority', 999 ); |
| 64 |
|
| 65 |
if ( is_null( $init_hook ) || ! did_action( $init_hook ) ) { |
| 66 |
// If the user has not specified a hook, we'll use the default (now). |
| 67 |
$this->enable_query_filtering(); |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
add_action( (string) $init_hook, [ $this, 'enable_query_filtering' ], (int) $init_priority ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Late hooks. |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function enable_query_filtering() { |
| 80 |
add_filter( 'the_posts', [ $this, 'restrict_query_posts' ], 10, 2 ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Handle restricted content appropriately. |
| 85 |
* |
| 86 |
* NOTE. This is only for filtering posts, and should not |
| 87 |
* be used to redirect or replace the entire page. |
| 88 |
* |
| 89 |
* @param \WP_Post[] $posts Array of post objects. |
| 90 |
* @param \WP_Query $query The WP_Query instance (passed by reference). |
| 91 |
* |
| 92 |
* @return \WP_Post[] |
| 93 |
*/ |
| 94 |
public function restrict_query_posts( $posts, $query ) { |
| 95 |
if ( query_can_be_ignored( $query ) ) { |
| 96 |
return $posts; |
| 97 |
} |
| 98 |
|
| 99 |
if ( protection_is_disabled() ) { |
| 100 |
return $posts; |
| 101 |
} |
| 102 |
|
| 103 |
$post_restrictions = get_restriction_matches_for_queried_posts( $query ); |
| 104 |
|
| 105 |
if ( false === $post_restrictions ) { |
| 106 |
return $posts; |
| 107 |
} |
| 108 |
|
| 109 |
// If we have restrictions on the queried posts, handle them top down. |
| 110 |
foreach ( $post_restrictions as $match ) { |
| 111 |
$post_id = $match['post_ids']; |
| 112 |
$restriction = $match['restriction']; |
| 113 |
|
| 114 |
/** |
| 115 |
* Use this filter to prevent a post from being restricted, or to handle it yourself. |
| 116 |
* |
| 117 |
* @param null|mixed $pre Whether to prevent the post from being restricted. |
| 118 |
* @param null|\ContentControl\Models\Restriction $restriction Restriction object. |
| 119 |
* @param int[] $post_id Post ID. |
| 120 |
* @return null|mixed |
| 121 |
*/ |
| 122 |
if ( null !== apply_filters( 'content_control/pre_restrict_archive_post', null, $restriction, $post_id ) ) { |
| 123 |
continue; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Fires when a post is restricted, but before the restriction is handled. |
| 128 |
* |
| 129 |
* @param \ContentControl\Models\Restriction $restriction Restriction object. |
| 130 |
* @param int[] $post_id Post ID. |
| 131 |
*/ |
| 132 |
do_action( 'content_control/restrict_archive_post', $restriction, $post_id ); |
| 133 |
|
| 134 |
$handling = $query->is_main_query() ? $restriction->get_setting( 'archiveHandling' ) : $restriction->get_setting( 'additionalQueryHandling' ); |
| 135 |
|
| 136 |
// If this is a search query and showInSearch is false, hide the post. |
| 137 |
if ( $query->is_search() && false === $restriction->get_setting( 'showInSearch' ) ) { |
| 138 |
$handling = 'hide'; |
| 139 |
} |
| 140 |
|
| 141 |
switch ( $handling ) { |
| 142 |
case 'filter_post_content': |
| 143 |
// Filter the title/excerpt/contents of the restricted items. |
| 144 |
break; |
| 145 |
case 'hide': |
| 146 |
foreach ( $posts as $key => $post ) { |
| 147 |
if ( in_array( $post->ID, $post_id, true ) ) { |
| 148 |
unset( $posts[ $key ] ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
// Update the query's post count. |
| 153 |
$query->post_count = count( $posts ); |
| 154 |
// Reset post indexes. |
| 155 |
$posts = array_values( $posts ); |
| 156 |
break; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return $posts; |
| 161 |
} |
| 162 |
} |
| 163 |
|