← All changes
|
classes/Controllers/Frontend/Restrictions/QueryPosts.php
+65
-12
2.0.4
→
2.6.1
View file →
| @@ -24,10 +24,60 @@ | ||
| 24 | 24 | class QueryPosts extends Controller { |
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * Initiate functionality. |
| 28 | + * | |
| 29 | + * @return void | |
| 28 | 30 | */ |
| 29 | 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() { | |
| 30 | 80 | add_filter( 'the_posts', [ $this, 'restrict_query_posts' ], 10, 2 ); |
| 31 | 81 | } |
| 32 | 82 | |
| 33 | 83 | /** |
| @@ -35,19 +85,19 @@ | ||
| 35 | 85 | * |
| 36 | 86 | * NOTE. This is only for filtering posts, and should not |
| 37 | 87 | * be used to redirect or replace the entire page. |
| 38 | 88 | * |
| 39 | - * @param WP_Post[] $posts Array of post objects. | |
| 40 | - * @param \WP_Query $query The WP_Query instance (passed by reference). | |
| 89 | + * @param \WP_Post[] $posts Array of post objects. | |
| 90 | + * @param \WP_Query $query The WP_Query instance (passed by reference). | |
| 41 | 91 | * |
| 42 | - * @return WP_Post[] | |
| 92 | + * @return \WP_Post[] | |
| 43 | 93 | */ |
| 44 | 94 | public function restrict_query_posts( $posts, $query ) { |
| 45 | - if ( protection_is_disabled() ) { | |
| 95 | + if ( query_can_be_ignored( $query ) ) { | |
| 46 | 96 | return $posts; |
| 47 | 97 | } |
| 48 | 98 | |
| 49 | - if ( query_can_be_ignored( $query ) ) { | |
| 99 | + if ( protection_is_disabled() ) { | |
| 50 | 100 | return $posts; |
| 51 | 101 | } |
| 52 | 102 | |
| 53 | 103 | $post_restrictions = get_restriction_matches_for_queried_posts( $query ); |
| @@ -60,18 +110,14 @@ | ||
| 60 | 110 | foreach ( $post_restrictions as $match ) { |
| 61 | 111 | $post_id = $match['post_ids']; |
| 62 | 112 | $restriction = $match['restriction']; |
| 63 | 113 | |
| 64 | - if ( is_int( $post_id ) ) { | |
| 65 | - $post_id = [ $post_id ]; | |
| 66 | - } | |
| 67 | - | |
| 68 | 114 | /** |
| 69 | 115 | * Use this filter to prevent a post from being restricted, or to handle it yourself. |
| 70 | 116 | * |
| 71 | - * @param null $pre Whether to prevent the post from being restricted. | |
| 117 | + * @param null|mixed $pre Whether to prevent the post from being restricted. | |
| 72 | 118 | * @param null|\ContentControl\Models\Restriction $restriction Restriction object. |
| 73 | - * @param int[] $post_id Post ID. | |
| 119 | + * @param int[] $post_id Post ID. | |
| 74 | 120 | * @return null|mixed |
| 75 | 121 | */ |
| 76 | 122 | if ( null !== apply_filters( 'content_control/pre_restrict_archive_post', null, $restriction, $post_id ) ) { |
| 77 | 123 | continue; |
| @@ -84,9 +130,16 @@ | ||
| 84 | 130 | * @param int[] $post_id Post ID. |
| 85 | 131 | */ |
| 86 | 132 | do_action( 'content_control/restrict_archive_post', $restriction, $post_id ); |
| 87 | 133 | |
| 88 | - switch ( $restriction->archive_handling ) { | |
| 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 ) { | |
| 89 | 142 | case 'filter_post_content': |
| 90 | 143 | // Filter the title/excerpt/contents of the restricted items. |
| 91 | 144 | break; |
| 92 | 145 | case 'hide': |