PluginProbe
User Access Manager / 2.2.14
User Access Manager v2.2.14
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / Controller / Frontend / ShortCodeController.php

ShortCodeController.php in User Access Manager 2.2.14, at src/Controller/Frontend/ShortCodeController.php

142 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ShortCodeController.php
4 *
5 * The ShortCodeController class file.
6 *
7 * PHP versions 5
8 *
9 * @author Alexander Schneider <alexanderschneider85@gmail.com>
10 * @copyright 2008-2017 Alexander Schneider
11 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 * @version SVN: $id$
13 * @link http://wordpress.org/extend/plugins/user-access-manager/
14 */
15
16 declare(strict_types=1);
17
18 namespace UserAccessManager\Controller\Frontend;
19
20 use UserAccessManager\Config\WordpressConfig;
21 use UserAccessManager\Controller\Controller;
22 use UserAccessManager\UserGroup\UserGroupHandler;
23 use UserAccessManager\UserGroup\UserGroupTypeException;
24 use UserAccessManager\Wrapper\Php;
25 use UserAccessManager\Wrapper\Wordpress;
26
27 /**
28 * Class ShortCodeController
29 *
30 * @package UserAccessManager\Controller\Frontend
31 */
32 class ShortCodeController extends Controller
33 {
34 use LoginControllerTrait;
35
36 /**
37 * @var UserGroupHandler
38 */
39 protected $userGroupHandler;
40
41 /**
42 * ShortCodeController constructor.
43 * @param Php $php
44 * @param Wordpress $wordpress
45 * @param WordpressConfig $wordpressConfig
46 * @param UserGroupHandler $userGroupHandler
47 */
48 public function __construct(
49 Php $php,
50 Wordpress $wordpress,
51 WordpressConfig $wordpressConfig,
52 UserGroupHandler $userGroupHandler
53 ) {
54 parent::__construct($php, $wordpress, $wordpressConfig);
55 $this->userGroupHandler = $userGroupHandler;
56 }
57
58 /**
59 * @return Wordpress
60 */
61 protected function getWordpress(): Wordpress
62 {
63 return $this->wordpress;
64 }
65
66 /**
67 * Returns the login bar.
68 * @return string
69 */
70 public function getLoginFormHtml(): string
71 {
72 $loginForm = '';
73
74 if ($this->wordpress->isUserLoggedIn() === false) {
75 $loginForm = $this->getIncludeContents('LoginForm.php');
76 }
77
78 return $this->wordpress->applyFilters('uam_login_form', $loginForm);
79 }
80
81 /**
82 * Handles the login form short code.
83 * @return string
84 */
85 public function loginFormShortCode(): string
86 {
87 return $this->getLoginFormHtml();
88 }
89
90 /**
91 * Handles the public short code.
92 * @param string|array $attributes
93 * @param string $content
94 * @return string
95 */
96 public function publicShortCode($attributes, $content = ''): string
97 {
98 return ($this->wordpress->isUserLoggedIn() === false) ? $this->wordpress->doShortCode($content) : '';
99 }
100
101 /**
102 * Returns the user group map from the short code attribute.
103 * @param array $attributes
104 * @return array
105 */
106 private function getUserGroupsMapFromAttributes(array $attributes): array
107 {
108 $userGroups = (isset($attributes['group']) === true) ? explode(',', $attributes['group']) : [];
109 return (array) array_flip(array_map('trim', $userGroups));
110 }
111
112 /**
113 * Handles the private short code.
114 * @param array $attributes
115 * @param string $content
116 * @return string
117 * @throws UserGroupTypeException
118 */
119 public function privateShortCode(array $attributes, $content = ''): string
120 {
121 if ($this->wordpress->isUserLoggedIn() === true) {
122 $userGroupMap = $this->getUserGroupsMapFromAttributes($attributes);
123
124 if ($userGroupMap === []) {
125 return $this->wordpress->doShortCode($content);
126 }
127
128 $userUserGroups = $this->userGroupHandler->getUserGroupsForUser();
129
130 foreach ($userUserGroups as $userGroup) {
131 if (isset($userGroupMap[$userGroup->getId()])
132 || isset($userGroupMap[$userGroup->getName()])
133 ) {
134 return $this->wordpress->doShortCode($content);
135 }
136 }
137 }
138
139 return '';
140 }
141 }
142