PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.0.1
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.0.1
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 / classes / Controllers / Frontend / Restrictions / QueryPosts.php

QueryPosts.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 2.0.1, at classes/Controllers/Frontend/Restrictions/QueryPosts.php

110 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 public function init() {
30 add_filter( 'the_posts', [ $this, 'restrict_query_posts' ], 10, 2 );
31 }
32
33 /**
34 * Handle restricted content appropriately.
35 *
36 * NOTE. This is only for filtering posts, and should not
37 * be used to redirect or replace the entire page.
38 *
39 * @param WP_Post[] $posts Array of post objects.
40 * @param \WP_Query $query The WP_Query instance (passed by reference).
41 *
42 * @return WP_Post[]
43 */
44 public function restrict_query_posts( $posts, $query ) {
45 if ( protection_is_disabled() ) {
46 return $posts;
47 }
48
49 if ( query_can_be_ignored( $query ) ) {
50 return $posts;
51 }
52
53 $post_restrictions = get_restriction_matches_for_queried_posts( $query );
54
55 if ( false === $post_restrictions ) {
56 return $posts;
57 }
58
59 // If we have restrictions on the queried posts, handle them top down.
60 foreach ( $post_restrictions as $match ) {
61 $post_id = $match['post_ids'];
62 $restriction = $match['restriction'];
63
64 if ( is_int( $post_id ) ) {
65 $post_id = [ $post_id ];
66 }
67
68 /**
69 * Use this filter to prevent a post from being restricted, or to handle it yourself.
70 *
71 * @param null $pre Whether to prevent the post from being restricted.
72 * @param null|\ContentControl\Models\Restriction $restriction Restriction object.
73 * @param int[] $post_id Post ID.
74 * @return null|mixed
75 */
76 if ( null !== apply_filters( 'content_control/pre_restrict_archive_post', null, $restriction, $post_id ) ) {
77 continue;
78 }
79
80 /**
81 * Fires when a post is restricted, but before the restriction is handled.
82 *
83 * @param \ContentControl\Models\Restriction $restriction Restriction object.
84 * @param int[] $post_id Post ID.
85 */
86 do_action( 'content_control/restrict_archive_post', $restriction, $post_id );
87
88 switch ( $restriction->archive_handling ) {
89 case 'filter_post_content':
90 // Filter the title/excerpt/contents of the restricted items.
91 break;
92 case 'hide':
93 foreach ( $posts as $key => $post ) {
94 if ( in_array( $post->ID, $post_id, true ) ) {
95 unset( $posts[ $key ] );
96 }
97 }
98
99 // Update the query's post count.
100 $query->post_count = count( $posts );
101 // Reset post indexes.
102 $posts = array_values( $posts );
103 break;
104 }
105 }
106
107 return $posts;
108 }
109 }
110