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 | } |