PluginProbe ʕ •ᴥ•ʔ
Advanced Access Manager – Access Governance for WordPress / 6.9.2
Advanced Access Manager – Access Governance for WordPress v6.9.2
6.8.4 6.8.5 6.9.0 6.9.1 6.9.10 6.9.11 6.9.12 6.9.13 6.9.14 6.9.15 6.9.16 6.9.17 6.9.18 6.9.19 6.9.2 6.9.20 6.9.21 6.9.22 6.9.23 6.9.24 6.9.25 6.9.26 6.9.27 6.9.28 6.9.29 6.9.3 6.9.30 6.9.31 6.9.32 6.9.33 6.9.34 6.9.35 6.9.36 6.9.37 6.9.38 6.9.39 6.9.4 6.9.41 6.9.42 6.9.43 6.9.44 6.9.45 6.9.46 6.9.47 6.9.48 6.9.49 6.9.5 6.9.51 6.9.6 6.9.7 6.9.8 6.9.9 7.0.0 7.0.0-alpha.6 7.0.0-alpha.7 7.0.0-beta.1 7.0.0-rc1 7.0.0-rc2 7.0.0-rc3 7.0.1 7.0.10 7.0.11 7.0.2 7.0.3 7.0.4 7.0.5 7.0.6 7.0.7 7.0.8 7.0.9 7.1.0 7.1.1 trunk 3.0 4.0 4.0.1 4.1 4.2 4.3 4.4 4.4.1 4.5 4.6 4.6.1 4.6.2 4.7 4.7.1 4.7.2 4.7.5 4.7.6 4.8 4.8.1 4.9 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.5.1 4.9.5.2 5.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1 5.1.1 5.10 5.11 5.2 5.2.1 5.2.5 5.2.6 5.2.7 5.3 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.4 5.4.1 5.4.2 5.4.3 5.4.3.1 5.4.3.2 5.5 5.5.1 5.5.2 5.6 5.6.1 5.6.1.1 5.7 5.7.1 5.7.2 5.7.3 5.8 5.8.1 5.8.2 5.8.3 5.9 5.9.1 5.9.1.1 5.9.2 5.9.2.1 5.9.3 5.9.4 5.9.5 5.9.6 5.9.6.1 5.9.6.2 5.9.6.3 5.9.7 5.9.7.1 5.9.7.2 5.9.7.3 5.9.8 5.9.8.1 5.9.9 5.9.9.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.2.0 6.2.1 6.2.2 6.3.0 6.3.1 6.3.2 6.3.3 6.4.0 6.4.1 6.4.2 6.4.3 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.6.0 6.6.1 6.6.2 6.6.3 6.6.4 6.7.0 6.7.1 6.7.2 6.7.3 6.7.4 6.7.5 6.7.6 6.7.7 6.7.8 6.7.9 6.8.0 6.8.1 6.8.2 6.8.3
advanced-access-manager / application / Shortcode / Handler / Content.php
advanced-access-manager / application / Shortcode / Handler Last commit date
Content.php 3 years ago LoginForm.php 3 years ago LoginRedirect.php 3 years ago
Content.php
235 lines
1 <?php
2
3 /**
4 * ======================================================================
5 * LICENSE: This file is subject to the terms and conditions defined in *
6 * file 'license.txt', which is part of this source code package. *
7 * ======================================================================
8 */
9
10 /**
11 * AAM shortcode handler for content visibility
12 *
13 * @since 6.5.0 https://github.com/aamplugin/advanced-access-manager/issues/96
14 * @since 6.0.0 Initial implementation of the class
15 *
16 * @package AAM
17 * @version 6.5.0
18 */
19 class AAM_Shortcode_Handler_Content
20 implements AAM_Core_Contract_ShortcodeInterface
21 {
22
23 /**
24 * Shortcode arguments
25 *
26 * @var array
27 *
28 * @access protected
29 * @version 6.0.0
30 */
31 protected $args;
32
33 /**
34 * Wrapped by shortcode content
35 *
36 * @var string
37 *
38 * @access protected
39 * @version 6.0.0
40 */
41 protected $content;
42
43 /**
44 * Initialize shortcode decorator
45 *
46 * Expecting attributes in $args are:
47 * "hide" => comma-separated list of role, caps, user IDs to hide content
48 * "show" => comma-separated list of role, caps, user IDs to show content
49 * "limit" => comma-separated list of role, caps, user IDs to limit content
50 * "message" => message to show if "limit" is defined
51 * "callback" => callback function that returns message if "limit" is defined
52 *
53 * @param array $args
54 * @param string $content
55 *
56 * @return void
57 *
58 * @access public
59 * @version 6.0.0
60 */
61 public function __construct($args, $content)
62 {
63 $this->args = $args;
64 $this->content = do_shortcode($content);
65 }
66
67 /**
68 * Process shortcode
69 *
70 * @return string
71 *
72 * @since 6.5.0 https://github.com/aamplugin/advanced-access-manager/issues/96
73 * @since 6.0.0 Initial implementation of the method
74 *
75 * @access public
76 * @version 6.5.0
77 */
78 public function run()
79 {
80 // Prepare list of subjects
81 if (get_current_user_id()) {
82 $roles = array_merge(AAM::getUser()->roles);
83
84 // Build the list of assigned capabilities
85 $caps = array();
86 foreach(AAM::getUser()->allcaps as $key => $effect) {
87 if (!empty($effect)) {
88 $caps[] = $key;
89 }
90 }
91
92 if (AAM::api()->getConfig('core.settings.multiSubject', false)) {
93 $parts = array_merge(array((string) AAM::getUser()->ID), $roles);
94 } else {
95 $parts = array((string) AAM::getUser()->ID, array_shift($roles));
96 }
97
98 $parts = array_merge($parts, $caps);
99 } else {
100 $parts = array('visitor');
101 }
102
103 $show = $this->getAccess('show');
104 $limit = $this->getAccess('limit');
105 $hide = $this->getAccess('hide');
106 $msg = $this->getMessage();
107
108 if (!empty($this->args['callback'])) {
109 $content = call_user_func($this->args['callback'], $this);
110 } else {
111 $content = $this->content;
112
113 // #1. Check if content is restricted for current user
114 if (in_array('all', $hide, true) || $this->check($parts, $hide)) {
115 $content = '';
116 }
117
118 // #2. Check if content is limited for current user
119 if (in_array('all', $limit, true) || $this->check($parts, $limit)) {
120 $content = do_shortcode($msg);
121 }
122
123 // #3. Check if content is allowed for current user
124 if ($this->check($parts, $show)) {
125 $content = $this->content;
126 }
127 }
128
129 return $content;
130 }
131
132 /**
133 * Check if visibility condition is matched
134 *
135 * @param mixed $subject
136 * @param array $conditions
137 *
138 * @return boolean
139 *
140 * @access protected
141 * @version 6.0.0
142 */
143 protected function check($subject, $conditions)
144 {
145 $match = false;
146 $auth = get_current_user_id();
147
148 foreach ($conditions as $condition) {
149 if (($condition === 'authenticated') && $auth) {
150 $match = true;
151 } else if (preg_match('/^[\d*-]+\.[\d*-]+[\d\.*-]*[\d\.*-]*$/', $condition)) {
152 $match = $this->checkIP(
153 $condition, AAM_Core_Request::server('REMOTE_ADDR')
154 );
155 } else {
156 $match = in_array($condition, $subject, true);
157 }
158
159 if ($match) {
160 break;
161 }
162 }
163
164 return $match;
165 }
166
167 /**
168 * Check user IP for match
169 *
170 * @param string $ip
171 * @param string $userIp
172 *
173 * @return boolean
174 *
175 * @since 6.3.0 Fixed potential bug https://github.com/aamplugin/advanced-access-manager/issues/38
176 * @since 6.0.0 Initial implementation of the method
177 *
178 * @access protected
179 * @version 6.3.0
180 */
181 protected function checkIP($ip, $userIp)
182 {
183 $match = true;
184
185 $ipSplit = preg_split('/[\.:]/', $ip);
186 $uipSplit = preg_split('/[\.:]/', $userIp);
187
188 foreach ($ipSplit as $i => $group) {
189 if (strpos($group, '-') !== false) { //range
190 $parts = explode('-', $group);
191
192 if ($uipSplit[$i] < $parts[0] || $uipSplit[$i] > $parts[1]) {
193 $match = false;
194 break;
195 }
196 } elseif ($group !== '*') {
197 if ($group !== $uipSplit[$i]) {
198 $match = false;
199 break;
200 }
201 }
202 }
203
204 return $match;
205 }
206
207 /**
208 * Get access preference by type
209 *
210 * @return array
211 *
212 * @access public
213 * @version 6.0.0
214 */
215 public function getAccess($type)
216 {
217 $access = (isset($this->args[$type]) ? $this->args[$type] : null);
218
219 return array_map('trim', explode(',', $access));
220 }
221
222 /**
223 * Get replacement message
224 *
225 * @return string|null
226 *
227 * @access public
228 * @version 6.0.0
229 */
230 public function getMessage()
231 {
232 return isset($this->args['message']) ? $this->args['message'] : null;
233 }
234
235 }