PluginProbe
Restrict User Access – Ultimate Membership & Content Protection / 2.8
Restrict User Access – Ultimate Membership & Content Protection v2.8
trunk 1.3 2.0 2.1.3 2.2.3 2.3.2 2.4 2.4.1 2.4.2 2.4.3 2.5 2.6 2.6.1 2.7 2.7.1 2.8 2.8.1
restrict-user-access / src / Module / ContentMode.php

ContentMode.php in Restrict User Access – Ultimate Membership & Content Protection 2.8, at src/Module/ContentMode.php

115 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace RestrictUserAccess\Module;
4
5 use RestrictUserAccess\Hook\HookService;
6 use RestrictUserAccess\Hook\HookSubscriberInterface;
7 use RestrictUserAccess\Repository\SettingRepositoryInterface;
8
9 /**
10 * Class ContentMode
11 *
12 * @author Joachim Jensen <joachim@dev.institute>
13 * @license https://www.gnu.org/licenses/gpl-3.0.html
14 */
15 class ContentMode implements HookSubscriberInterface
16 {
17 private $handled_content_ids = [];
18
19 /** @var SettingRepositoryInterface */
20 private $settingRepository;
21
22 public function __construct(
23 SettingRepositoryInterface $settingRepository
24 ) {
25 $this->settingRepository = $settingRepository;
26 }
27
28 public function subscribe(HookService $service)
29 {
30 if (is_admin()) {
31 return;
32 }
33
34 $service->add_action(
35 'wp_head',
36 [$this, 'init']
37 );
38 $service->add_filter(
39 'rest_api_init',
40 [$this, 'init_rest']
41 );
42 }
43
44 public function init()
45 {
46 if (!$this->is_enabled()) {
47 return;
48 }
49 if (is_singular()) {
50 return;
51 }
52 $this->add_content_filters();
53 }
54
55 /**
56 * @param \WP_Rest_Server $wp_rest_server
57 * @return void
58 */
59 public function init_rest(\WP_Rest_Server $wp_rest_server)
60 {
61 if (!$this->is_enabled()) {
62 return;
63 }
64 if (current_user_can('edit_posts')) {
65 return;
66 }
67 $this->add_content_filters();
68 }
69
70 /**
71 * @param string $content
72 * @return string
73 */
74 public function restrict_the_content($content)
75 {
76 $id = get_the_ID();
77 if (isset($this->handled_content_ids[$id])) {
78 return $content;
79 }
80
81 $this->handled_content_ids[$id] = true;
82
83 //bail if password is required, wp will show input form
84 if (post_password_required()) {
85 return $content;
86 }
87 //bail if is $more_link_text is used, already showing teaser
88 if (preg_match('/href=".+#more-[0-9]*"/', $content)) {
89 return $content;
90 }
91 return get_the_excerpt();
92 }
93
94 private function is_enabled()
95 {
96 return $this->settingRepository->get_int('rua_list_content_mode') !== 0;
97 }
98
99 private function add_content_filters()
100 {
101 $option_value = $this->settingRepository->get_int('rua_list_content_mode');
102 switch ($option_value) {
103 case 1:
104 add_filter('the_content', [$this, 'restrict_the_content']);
105 return;
106 case 2:
107 add_filter('the_content', '__return_empty_string');
108 add_filter('the_excerpt', '__return_empty_string');
109 return;
110 default:
111 return;
112 }
113 }
114 }
115