FrontendController.php
4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* FrontendController.php
*
* The FrontendController class file.
*
* PHP versions 5
*
* @author Alexander Schneider <alexanderschneider85@gmail.com>
* @copyright 2008-2017 Alexander Schneider
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
* @version SVN: $id$
* @link http://wordpress.org/extend/plugins/user-access-manager/
*/
declare(strict_types=1);
namespace UserAccessManager\Controller\Frontend;
use UserAccessManager\Access\AccessHandler;
use UserAccessManager\Config\MainConfig;
use UserAccessManager\Config\WordpressConfig;
use UserAccessManager\Controller\Controller;
use UserAccessManager\UserAccessManager;
use UserAccessManager\UserGroup\UserGroupTypeException;
use UserAccessManager\Wrapper\Php;
use UserAccessManager\Wrapper\Wordpress;
/**
* Class FrontendController
*
* @package UserAccessManager\Controller
*/
class FrontendController extends Controller
{
const HANDLE_STYLE_LOGIN_FORM = 'UserAccessManagerLoginForm';
/**
* @var MainConfig
*/
private $mainConfig;
/**
* @var AccessHandler
*/
private $accessHandler;
/**
* FrontendController constructor.
* @param Php $php
* @param Wordpress $wordpress
* @param WordpressConfig $wordpressConfig
* @param MainConfig $mainConfig
* @param AccessHandler $userHandler
*/
public function __construct(
Php $php,
Wordpress $wordpress,
WordpressConfig $wordpressConfig,
MainConfig $mainConfig,
AccessHandler $userHandler
) {
parent::__construct($php, $wordpress, $wordpressConfig);
$this->mainConfig = $mainConfig;
$this->accessHandler = $userHandler;
}
/**
* Functions for other content.
*/
/**
* Register all other styles.
*/
private function registerStylesAndScripts()
{
$urlPath = $this->wordpressConfig->getUrlPath();
$this->wordpress->registerStyle(
self::HANDLE_STYLE_LOGIN_FORM,
$urlPath.'assets/css/uamLoginForm.css',
[],
UserAccessManager::VERSION,
'screen'
);
}
/**
* The function for the wp_enqueue_scripts action.
*/
public function enqueueStylesAndScripts()
{
$this->registerStylesAndScripts();
$this->wordpress->enqueueStyle(self::HANDLE_STYLE_LOGIN_FORM);
}
/**
* The function for the get_ancestors filter.
* @param array $ancestors
* @param int|string $objectId
* @param string $objectType
* @return array
* @throws UserGroupTypeException
*/
public function showAncestors(array $ancestors, $objectId, string $objectType): array
{
if ($this->mainConfig->lockRecursive() === true
&& $this->accessHandler->checkObjectAccess($objectType, $objectId) === false
) {
return [];
}
foreach ($ancestors as $key => $ancestorId) {
if ($this->accessHandler->checkObjectAccess($objectType, $ancestorId) === false) {
unset($ancestors[$key]);
}
}
return $ancestors;
}
/*
* Functions for the redirection and files.
*/
/**
* Filter for Yoast SEO Plugin
* Hides the url from the site map if the user has no access
* @param string|array $url The url to check
* @param string $type The object type
* @param object $object The object
* @return false|string
* @throws UserGroupTypeException
*/
public function getWpSeoUrl($url, string $type, object $object)
{
return ($this->accessHandler->checkObjectAccess($type, $object->ID) === true) ? $url : false;
}
/*
* Elementor
*/
/**
* @param $content
* @return mixed
* @throws UserGroupTypeException
*/
public function getElementorContent($content)
{
$this->wordpress->removeAction('elementor/frontend/the_content', [$this, 'getElementorContent']);
$post = $this->wordpress->getCurrentPost();
if ($this->accessHandler->checkObjectAccess($post->post_type, $post->ID) === false) {
$content = htmlspecialchars_decode($this->mainConfig->getPostTypeContent($post->post_type));
}
return $content;
}
}