| 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 |
* @param string $content Content of post being checked. |
| 46 |
* |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public function filter_the_content_if_restricted( $content ) { |
| 50 |
$filter_name = 'content_control/restricted_post_content'; |
| 51 |
|
| 52 |
// Ensure we don't get into an infinite loop. |
| 53 |
if ( doing_filter( $filter_name ) || doing_filter( 'get_the_excerpt' ) ) { |
| 54 |
return $content; |
| 55 |
} |
| 56 |
|
| 57 |
// If this isn't a post type that can be restricted, bail. |
| 58 |
if ( protection_is_disabled() ) { |
| 59 |
return $content; |
| 60 |
} |
| 61 |
|
| 62 |
if ( ! content_is_restricted() ) { |
| 63 |
return $content; |
| 64 |
} |
| 65 |
|
| 66 |
$restriction = get_applicable_restriction(); |
| 67 |
|
| 68 |
// If this is a replacement page, bail. |
| 69 |
if ( |
| 70 |
( 'replace' === $restriction->protection_method && 'page' === $restriction->replacement_type && is_page( $restriction->replacement_page ) ) || |
| 71 |
( 'replace_archive_page' === $restriction->archive_handling && is_page( $restriction->archive_replacement_page ) ) |
| 72 |
) { |
| 73 |
return $content; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Filter the message to display when a post is restricted. |
| 78 |
* |
| 79 |
* @param string $message Message to display. |
| 80 |
* @param object $restriction Restriction object. |
| 81 |
* |
| 82 |
* @return string |
| 83 |
*/ |
| 84 |
return apply_filters( |
| 85 |
$filter_name, |
| 86 |
$restriction->get_message(), |
| 87 |
$restriction |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Filter post excerpt when needed. |
| 93 |
* |
| 94 |
* @param string $post_excerpt The post excerpt. |
| 95 |
* @param WP_Post $post Post object. |
| 96 |
* |
| 97 |
* @return string |
| 98 |
*/ |
| 99 |
public function filter_the_excerpt_if_restricted( $post_excerpt, $post ) { |
| 100 |
$filter_name = 'content_control/restricted_post_excerpt'; |
| 101 |
|
| 102 |
if ( doing_filter( $filter_name ) ) { |
| 103 |
return $post_excerpt; |
| 104 |
} |
| 105 |
|
| 106 |
// If this isn't a post type that can be restricted, bail. |
| 107 |
if ( protection_is_disabled() ) { |
| 108 |
return $post_excerpt; |
| 109 |
} |
| 110 |
|
| 111 |
if ( ! content_is_restricted( $post->ID ) ) { |
| 112 |
return $post_excerpt; |
| 113 |
} |
| 114 |
|
| 115 |
$restriction = get_applicable_restriction(); |
| 116 |
|
| 117 |
/** |
| 118 |
* Filter the excerpt to display when a post is restricted. |
| 119 |
* |
| 120 |
* @param string $message Message to display. |
| 121 |
* @param object $restriction Restriction object. |
| 122 |
* |
| 123 |
* @return string |
| 124 |
*/ |
| 125 |
return apply_filters( |
| 126 |
$filter_name, |
| 127 |
$restriction->get_message(), |
| 128 |
$restriction |
| 129 |
); |
| 130 |
} |
| 131 |
} |
| 132 |
|