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

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

179 lines 5.2 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 $this->enable_filters();
31 }
32
33 /**
34 * Enable filters.
35 *
36 * @return void
37 */
38 public function enable_filters() {
39 add_filter( 'the_content', [ $this, 'filter_the_content_if_restricted' ], 1000 );
40 add_filter( 'get_the_excerpt', [ $this, 'filter_the_excerpt_if_restricted' ], 1000, 2 );
41 // phpcs:disable Squiz.PHP.CommentedOutCode.Found, Squiz.Commenting.InlineComment.InvalidEndChar -- These are for future use.
42 // add_filter( 'the_title', [ $this, 'filter_the_title_if_restricted'], 1000, 2 );
43 // add_filter( 'get_the_excerpt', [ $this, 'filter_the_excerpt_if_restricted' ], 1000, 2 );
44 // add_filter( 'post_class', [ $this, 'filter_post_class_if_restricted' ], 1000, 3 );
45 // add_filter( 'post_password_required', [ $this, 'require_password_if_restricted' ], 1000, 2 );
46 // add_filter( 'the_password_form', [ $this, 'filter_password_form_if_restricted' ], 1000, 2 );
47 // phpcs:enable Squiz.PHP.CommentedOutCode.Found, Squiz.Commenting.InlineComment.InvalidEndChar
48 }
49
50 /**
51 * Disable filters.
52 *
53 * @return void
54 */
55 public function disable_filters() {
56 remove_filter( 'the_content', [ $this, 'filter_the_content_if_restricted' ], 1000 );
57 remove_filter( 'get_the_excerpt', [ $this, 'filter_the_excerpt_if_restricted' ], 1000 );
58 }
59
60 /**
61 * Filter post content when needed.
62 *
63 * NOTE: If we got this far with restricted content, this is the last attempt to protect
64 * it. This serves as the default fallback protection method if all others fail.
65 *
66 * @param string $content Content of post being checked.
67 *
68 * @return string
69 */
70 public function filter_the_content_if_restricted( $content ) {
71 $filter_name = 'content_control/restricted_post_content';
72
73 // If this isn't a post type that can be restricted, bail.
74 if ( protection_is_disabled() ) {
75 return $content;
76 }
77
78 if ( ! content_is_restricted() ) {
79 return $content;
80 }
81
82 // Ensure we don't get into an infinite loop.
83 if ( doing_filter( $filter_name ) || doing_filter( 'get_the_excerpt' ) ) {
84 return $content;
85 }
86
87 $restriction = get_applicable_restriction();
88
89 if ( false === $restriction ) {
90 return $content;
91 }
92
93 // If this is a replacement page, bail.
94 if (
95 ( 'replace' === $restriction->get_setting( 'protectionMethod' ) && 'page' === $restriction->get_setting( 'replacementType' ) && is_page( $restriction->get_setting( 'replacementPage' ) ) ) ||
96 ( 'replace_archive_page' === $restriction->get_setting( 'archiveHandling' ) && is_page( $restriction->get_setting( 'archiveReplacementPage' ) ) )
97 ) {
98 return $content;
99 }
100
101 $message = \ContentControl\get_default_denial_message();
102
103 /**
104 * If the restriction has a custom message, use it.
105 *
106 * We could check $restriction->replacement_type, but we need a safe default for
107 * all cases. Further we do content filtering for all sub queries and currently
108 * don't offer a way to override the message for those.
109 *
110 * In this way currently users can change to content replacement, set the override
111 * message, then change back to page replacement and the override message will still
112 * be used for the post in sub queries.
113 */
114 if ( $restriction->get_setting( 'overrideMessage' ) ) {
115 $message = $restriction->get_message();
116 }
117
118 /**
119 * Filter the message to display when a post is restricted.
120 *
121 * @param string $message Message to display.
122 * @param object $restriction Restriction object.
123 *
124 * @return string
125 */
126 return apply_filters(
127 $filter_name,
128 // If the default message is empty, show a generic message.
129 ! empty( $message ) ? $message : __( 'This content is restricted.', 'content-control' ),
130 $restriction
131 );
132 }
133
134 /**
135 * Filter post excerpt when needed.
136 *
137 * @param string $post_excerpt The post excerpt.
138 * @param \WP_Post $post Post object.
139 *
140 * @return string
141 */
142 public function filter_the_excerpt_if_restricted( $post_excerpt, $post = null ) {
143 $filter_name = 'content_control/restricted_post_excerpt';
144
145 // If this isn't a post type that can be restricted, bail.
146 if ( protection_is_disabled() ) {
147 return $post_excerpt;
148 }
149
150 if ( ! content_is_restricted( $post->ID ) ) {
151 return $post_excerpt;
152 }
153
154 if ( doing_filter( $filter_name ) ) {
155 return $post_excerpt;
156 }
157
158 $restriction = get_applicable_restriction();
159
160 if ( false === $restriction ) {
161 return $post_excerpt;
162 }
163
164 /**
165 * Filter the excerpt to display when a post is restricted.
166 *
167 * @param string $message Message to display.
168 * @param object $restriction Restriction object.
169 *
170 * @return string
171 */
172 return apply_filters(
173 $filter_name,
174 $restriction->get_message(),
175 $restriction
176 );
177 }
178 }
179