PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.0.12
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.0.12
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 / PostContent.php

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

157 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Frontend post content 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\content_is_restricted;
14 use function ContentControl\protection_is_disabled;
15 use function ContentControl\get_applicable_restriction;
16
17 defined( 'ABSPATH' ) || exit;
18
19 /**
20 * Class for handling global restrictions of the post contents.
21 *
22 * @package ContentControl
23 */
24 class PostContent extends Controller {
25
26 /**
27 * Initiate functionality.
28 */
29 public function init() {
30 add_filter( 'the_content', [ $this, 'filter_the_content_if_restricted' ], 1000 );
31 add_filter( 'get_the_excerpt', [ $this, 'filter_the_excerpt_if_restricted' ], 1000, 2 );
32
33 // phpcs:disable Squiz.PHP.CommentedOutCode.Found, Squiz.Commenting.InlineComment.InvalidEndChar -- These are for future use.
34 // add_filter( 'the_title', [ $this, 'filter_the_title_if_restricted'], 1000, 2 );
35 // add_filter( 'get_the_excerpt', [ $this, 'filter_the_excerpt_if_restricted' ], 1000, 2 );
36 // add_filter( 'post_class', [ $this, 'filter_post_class_if_restricted' ], 1000, 3 );
37 // add_filter( 'post_password_required', [ $this, 'require_password_if_restricted' ], 1000, 2 );
38 // add_filter( 'the_password_form', [ $this, 'filter_password_form_if_restricted' ], 1000, 2 );
39 // phpcs:enable Squiz.PHP.CommentedOutCode.Found, Squiz.Commenting.InlineComment.InvalidEndChar
40 }
41
42 /**
43 * Filter post content when needed.
44 *
45 * NOTE: If we got this far with restricted content, this is the last attempt to protect
46 * it. This serves as the default fallback protection method if all others fail.
47 *
48 * @param string $content Content of post being checked.
49 *
50 * @return string
51 */
52 public function filter_the_content_if_restricted( $content ) {
53 $filter_name = 'content_control/restricted_post_content';
54
55 // Ensure we don't get into an infinite loop.
56 if ( doing_filter( $filter_name ) || doing_filter( 'get_the_excerpt' ) ) {
57 return $content;
58 }
59
60 // If this isn't a post type that can be restricted, bail.
61 if ( protection_is_disabled() ) {
62 return $content;
63 }
64
65 if ( ! content_is_restricted() ) {
66 return $content;
67 }
68
69 $restriction = get_applicable_restriction();
70
71 if ( false === $restriction ) {
72 return $content;
73 }
74
75 // If this is a replacement page, bail.
76 if (
77 ( 'replace' === $restriction->protection_method && 'page' === $restriction->replacement_type && is_page( $restriction->replacement_page ) ) ||
78 ( 'replace_archive_page' === $restriction->archive_handling && is_page( $restriction->archive_replacement_page ) )
79 ) {
80 return $content;
81 }
82
83 $message = \ContentControl\get_default_denial_message();
84
85 /**
86 * If the restriction has a custom message, use it.
87 *
88 * We could check $restriction->replacement_type, but we need a safe default for
89 * all cases. Further we do content filtering for all sub queries and currently
90 * don't offer a way to override the message for those.
91 *
92 * In this way currently users can change to content replacement, set the override
93 * message, then change back to page replacement and the override message will still
94 * be used for the post in sub queries.
95 */
96 if ( $restriction->override_message ) {
97 $message = $restriction->get_message();
98 }
99
100 /**
101 * Filter the message to display when a post is restricted.
102 *
103 * @param string $message Message to display.
104 * @param object $restriction Restriction object.
105 *
106 * @return string
107 */
108 return apply_filters(
109 $filter_name,
110 // If the default message is empty, show a generic message.
111 ! empty( $message ) ? $message : __( 'This content is restricted.', 'content-control' ),
112 $restriction
113 );
114 }
115
116 /**
117 * Filter post excerpt when needed.
118 *
119 * @param string $post_excerpt The post excerpt.
120 * @param \WP_Post $post Post object.
121 *
122 * @return string
123 */
124 public function filter_the_excerpt_if_restricted( $post_excerpt, $post = null ) {
125 $filter_name = 'content_control/restricted_post_excerpt';
126
127 if ( doing_filter( $filter_name ) ) {
128 return $post_excerpt;
129 }
130
131 // If this isn't a post type that can be restricted, bail.
132 if ( protection_is_disabled() ) {
133 return $post_excerpt;
134 }
135
136 if ( ! content_is_restricted( $post->ID ) ) {
137 return $post_excerpt;
138 }
139
140 $restriction = get_applicable_restriction();
141
142 /**
143 * Filter the excerpt to display when a post is restricted.
144 *
145 * @param string $message Message to display.
146 * @param object $restriction Restriction object.
147 *
148 * @return string
149 */
150 return apply_filters(
151 $filter_name,
152 $restriction->get_message(),
153 $restriction
154 );
155 }
156 }
157