PluginProbe ʕ •ᴥ•ʔ
FAPI Member / trunk
FAPI Member vtrunk
2.2.33 2.2.32 trunk 1.9.47 2.1.18 2.2.24 2.2.25 2.2.26 2.2.28 2.2.29 2.2.30 2.2.31
fapi-member / src / Service / AdminMenuService.php
fapi-member / src / Service Last commit date
AdminMenuService.php 2 years ago ApiService.php 1 day ago ElementService.php 7 months ago EmailService.php 1 day ago FormService.php 1 year ago LevelOrderService.php 1 year ago LevelService.php 1 year ago MembershipService.php 1 year ago RedirectService.php 1 year ago SanitizationService.php 2 years ago StatisticsService.php 1 year ago UserService.php 1 year ago
AdminMenuService.php
472 lines
1 <?php
2
3 namespace FapiMember\Service;
4
5 use FapiMember\Container\Container;
6 use FapiMember\FapiMemberPlugin;
7 use FapiMember\Model\Enums\Alert;
8 use FapiMember\Model\Enums\Format;
9 use FapiMember\Model\Enums\Keys\OptionKey;
10 use FapiMember\Model\Enums\Keys\SettingsKey;
11 use FapiMember\Model\Enums\PostValue;
12 use FapiMember\Model\Enums\SubPage;
13 use FapiMember\Model\Enums\Types\FormValueType;
14 use FapiMember\Model\Enums\UserPermission;
15 use FapiMember\Model\Membership;
16 use FapiMember\Repository\EmailRepository;
17 use FapiMember\Repository\LevelRepository;
18 use FapiMember\Repository\MembershipHistoryRepository;
19 use FapiMember\Repository\MembershipRepository;
20 use FapiMember\Repository\PageRepository;
21 use FapiMember\Repository\SettingsRepository;
22 use FapiMember\Repository\UserRepository;
23 use FapiMember\Utils\DateTimeHelper;
24
25 class AdminMenuService
26 {
27 private FormService $formService;
28 private RedirectService $redirectService;
29 private LevelService $levelService;
30 private LevelRepository $levelRepository;
31 private EmailRepository $emailRepository;
32 private MembershipService $membershipService;
33 private MembershipHistoryRepository $membershipHistoryRepository;
34 private ApiService $apiService;
35 private PageRepository $pageRepository;
36 private SettingsRepository $settingsRepository;
37 private UserRepository $userRepository;
38 private MembershipRepository $membershipRepository;
39
40 public function __construct() {
41 $this->formService = Container::get(FormService::class);
42 $this->redirectService = Container::get(RedirectService::class);
43 $this->levelService = Container::get(LevelService::class);
44 $this->levelRepository = Container::get(LevelRepository::class);
45 $this->emailRepository = Container::get(EmailRepository::class);
46 $this->membershipService = Container::get(MembershipService::class);
47 $this->membershipHistoryRepository = Container::get(MembershipHistoryRepository::class);
48 $this->apiService = Container::get(ApiService::class);
49 $this->pageRepository = Container::get(PageRepository::class);
50 $this->settingsRepository = Container::get(SettingsRepository::class);
51 $this->userRepository = Container::get(UserRepository::class);
52 $this->membershipRepository = Container::get(MembershipRepository::class);
53 }
54
55 public function handleUserProfileSave(int $userId): bool
56 {
57 if (
58 (empty($_POST['_wpnonce']) || ! wp_verify_nonce($_POST['_wpnonce'], 'update-user_' . $userId)) ||
59 !current_user_can(UserPermission::REQUIRED_CAPABILITY)
60 ) {
61 return false;
62 }
63
64 $membershipData = $this->formService->loadPostValue('Levels', FormValueType::USER_PROFILE_LEVELS);
65 $memberships = [];
66
67 foreach ($membershipData as $data) {
68 $data['user_id'] = $userId;
69 $membership = new Membership($data);
70 $this->membershipHistoryRepository->update($userId, $membership);
71 $memberships[] = $membership;
72 }
73
74 $this->membershipService->saveAll($userId, $memberships);
75 $this->membershipService->extendMembershipsToSections($userId);
76
77 return true;
78 }
79
80 public function handleNewSection(): void
81 {
82 $this->formService->verifyNonceAndCapability('new_section');
83
84 $name = $this->formService->loadPostValue('fapiMemberSectionName', FormValueType::ANY_STRING);
85
86 if ($name === null) {
87 $this->redirectService->redirect(SubPage::SECTION_NEW, Alert::SECTION_NAME_EMPTY);
88 }
89
90 $this->levelService->create($name);
91 $this->redirectService->redirect(SubPage::SECTION_NEW);
92 }
93
94 public function handleNewLevel(): void
95 {
96 $this->formService->verifyNonceAndCapability('new_level');
97
98 $name = $this->formService
99 ->loadPostValue('fapiMemberLevelName', FormValueType::ANY_STRING);
100 $parentId = $this->formService
101 ->loadPostValue('fapiMemberLevelParent', FormValueType::VALID_LEVEL_ID);
102
103 if ($name === null || $parentId === null) {
104 $this->redirectService->redirect(SubPage::LEVEL_NEW, Alert::LEVEL_NAME_OR_PARENT_EMPTY);
105 }
106
107 $parent = $this->levelRepository->getLevelById($parentId);
108
109 if ($parent === null) {
110 $this->redirectService->redirect(SubPage::LEVEL_NEW, Alert::SECTION_NOT_FOUND);
111 }
112
113 $this->levelService->create($name, $parentId);
114 $this->redirectService->redirect(SubPage::LEVEL_NEW);
115 }
116
117 public function handleRemoveLevel(): void
118 {
119 $this->formService->verifyNonceAndCapability('remove_level');
120
121 $id = $this->formService
122 ->loadPostValue('level_id', FormValueType::VALID_LEVEL_ID);
123
124 if ($id === null) {
125 $this->redirectService->redirect(SubPage::SECTION_NEW);
126 }
127
128 $this->levelRepository->remove($id);
129 $this->redirectService->redirect(SubPage::SECTION_NEW, Alert::REMOVE_LEVEL_SUCCESSFUL);
130 }
131
132 public function handleEditLevel(): void
133 {
134 $this->formService->verifyNonceAndCapability('edit_level');
135
136 $id = $this->formService
137 ->loadPostValue('level_id', FormValueType::VALID_LEVEL_ID);
138 $name = $this->formService
139 ->loadPostValue('name', FormValueType::ANY_STRING);
140
141 if ($id === null || $name === null) {
142 $this->redirectService->redirect(SubPage::SECTION_NEW, Alert::EDIT_LEVEL_NO_NAME);
143 }
144
145 $this->levelService->updateName($id, $name);
146 $this->redirectService->redirect(SubPage::SECTION_NEW, Alert::EDIT_LEVEL_SUCCESSFUL);
147 }
148
149 public function handleOrderLevel(): void
150 {
151 $this->formService->verifyNonceAndCapability('order_level');
152
153 $id = $this->formService
154 ->loadPostValue('id', FormValueType::VALID_LEVEL_ID);
155 $direction = $this->formService
156 ->loadPostValue('direction', FormValueType::VALID_DIRECTION);
157
158
159 if ($id === null || $direction === null) {
160 $this->redirectService->redirect(SubPage::SECTION_NEW, Alert::EDIT_LEVEL_NO_NAME);
161 }
162
163 $this->levelService->order($id, $direction);
164 $this->redirectService->redirect(SubPage::SECTION_NEW, Alert::EDIT_LEVEL_SUCCESSFUL);
165 }
166
167 public function handleApiCredentialsSubmit(): void
168 {
169 $this->formService->verifyNonceAndCapability('api_credentials_submit');
170
171 $apiEmail = $this->formService->loadPostValue(OptionKey::API_USER, FormValueType::ANY_STRING);
172 $apiKey = $this->formService->loadPostValue(OptionKey::API_KEY, FormValueType::ANY_STRING);
173
174 if ( $apiKey === null || $apiEmail === null ) {
175 $this->redirectService->redirect(SubPage::CONNECTION, Alert::API_FORM_EMPTY);
176 }
177
178 update_option(OptionKey::API_USER, $apiEmail);
179 update_option(OptionKey::API_KEY, $apiKey);
180
181 $credentials = json_decode(get_option(OptionKey::API_CREDENTIALS));
182
183 if (wp_list_filter( $credentials, ['username' => $apiEmail])
184 && wp_list_filter($credentials, ['token' => $apiKey])
185 ) {
186 $this->redirectService->redirect(SubPage::CONNECTION, Alert::API_FORM_CREDENTIALS_EXIST);
187 }
188
189 if (empty($credentials)) {
190 $credentials = [['username' => $apiEmail, 'token' => $apiKey]];
191 } elseif (count($credentials) < FapiMemberPlugin::CONNECTED_API_KEYS_LIMIT) {
192 $credentials[] = ['username' => $apiEmail, 'token' => $apiKey];
193 } else {
194 $this->redirectService->redirect(SubPage::CONNECTION, Alert::API_FORM_TOO_MANY_CREDENTIALS);
195 }
196
197 update_option(OptionKey::API_CREDENTIALS, json_encode($credentials));
198 $credentialsOk = $this->apiService->checkCredentials();
199 update_option(OptionKey::API_CHECKED, $credentialsOk);
200 $webUrl = rtrim(get_site_url(), '/' ) . '/';
201
202 foreach ($this->apiService->getApiClients() as $apiClient) {
203 $connection = $apiClient->getConnection();
204
205 if ($connection === null) {
206 $connection = $this->apiService->createConnection($webUrl, $apiClient);
207 $apiClient->setConnection($connection);
208 }
209 }
210
211 if ($credentialsOk) {
212 $this->redirectService->redirect(SubPage::CONNECTION, Alert::API_FORM_SUCCESS);
213 } else {
214 array_pop($credentials);
215 update_option(OptionKey::API_CREDENTIALS, json_encode($credentials));
216 update_option(
217 OptionKey::API_CHECKED,
218 $this->apiService->checkCredentials(),
219 );
220 $this->redirectService->redirect(SubPage::CONNECTION, Alert::API_FORM_ERROR);
221 }
222 }
223
224 public function handleApiCredentialsRemove(): void
225 {
226 $keyToRemove = $_POST['fapiRemoveCredentials'];
227 $credentials = json_decode(get_option(OptionKey::API_CREDENTIALS )) ?? [];
228
229 foreach ($credentials as $user => $credential) {
230 if ($credential->token === $keyToRemove) {
231 unset( $credentials[$user]);
232 }
233 }
234
235 if (empty($credentials)) {
236 update_option(OptionKey::API_CREDENTIALS, '');
237 } else {
238 update_option(OptionKey::API_CREDENTIALS, json_encode(array_values($credentials)));
239 }
240
241 $credentialsOk = $this->apiService->checkCredentials();
242 update_option(OptionKey::API_CHECKED, $credentialsOk);
243
244 $this->redirectService->redirect(SubPage::CONNECTION, Alert::API_FORM_CREDENTIALS_REMOVED);
245 }
246
247 public function handleUpdatePages(): void
248 {
249 $this->formService->verifyNonceAndCapability('add_pages');
250
251 $levelId = $this->formService->loadPostValue('level_id', FormValueType::VALID_LEVEL_ID);
252 $toAdd = $this->formService->loadPostValue('toAdd', FormValueType::VALID_PAGE_IDS);
253
254 if ($levelId === null || $toAdd === null) {
255 $this->redirectService->redirect(SubPage::SETTINGS_CONTENT_ADD, Alert::LEVEL_ID_OR_TO_ADD_EMPTY);
256 }
257
258 $level = $this->levelRepository->getLevelById($levelId);
259
260 if ($level === null ){
261 $this->redirectService->redirect(SubPage::SETTINGS_CONTENT_ADD, Alert::SECTION_NOT_FOUND);
262 }
263
264 $this->pageRepository->addPages($level->getId(), $toAdd);
265 $this->redirectService->redirect(SubPage::SETTINGS_CONTENT_REMOVE, null, ['level' => $levelId]);
266 }
267
268 public function handleRemovePages(): void
269 {
270 $this->formService->verifyNonceAndCapability( 'remove_pages' );
271
272 $levelId = $this->formService->loadPostValue('level_id', FormValueType::VALID_LEVEL_ID);
273 $pageIds = $this->formService->loadPostValue('selection', FormValueType::VALID_PAGE_IDS, []);
274 $cptSelection = $this->formService->loadPostValue('cpt_selection', FormValueType::STR_LIST, []);
275
276 if ($levelId === null ) {
277 $this->redirectService->redirect(SubPage::SETTINGS_CONTENT_REMOVE, Alert::LEVEL_ID_OR_TO_ADD_EMPTY);
278 }
279
280 $level = $this->levelRepository->getLevelById($levelId);
281
282 if ($level === null) {
283 $this->redirectService->redirect(SubPage::SETTINGS_CONTENT_REMOVE, Alert::SECTION_NOT_FOUND);
284 }
285
286 $this->pageRepository->removePages($levelId, $pageIds, $cptSelection);
287 $this->redirectService->redirect(SubPage::SETTINGS_CONTENT_ADD, null, array('level' => $levelId));
288 }
289
290 public function handleEditEmail(): void
291 {
292 $this->formService->verifyNonceAndCapability('edit_email');
293
294 $levelId = $this->formService
295 ->loadPostValue('level_id', FormValueType::VALID_LEVEL_ID);
296 $emailType = $this->formService
297 ->loadPostValue('email_type', FormValueType::VALID_EMAIL_TYPE);
298 $mailSubject = $this->formService
299 ->loadPostValue('mail_subject', FormValueType::ANY_STRING);
300 $mailBody = $this->formService
301 ->loadPostValue('mail_body', FormValueType::ANY_STRING);
302 $mailCheckboxChecked = $this->formService
303 ->loadPostValue('specify_level_emails', FormValueType::ANY_STRING);
304
305 if ($mailSubject === null || $mailBody === null || $mailCheckboxChecked === null) {
306 $this->emailRepository->remove($levelId, $emailType);
307
308 $this->redirectService
309 ->redirect(SubPage::SETTINGS_EMAILS, Alert::EDIT_MAILS_REMOVED, ['level' => $levelId]);
310 }
311
312 $this->emailRepository->update($levelId, $emailType, $mailSubject, $mailBody);
313
314 $this->redirectService
315 ->redirect(SubPage::SETTINGS_EMAILS, Alert::EDIT_MAILS_UPDATED, ['level' => $levelId]);
316 }
317
318 public function handleSetServicePage(): void
319 {
320 $this->formService->verifyNonceAndCapability( 'set_other_page' );
321
322 $levelId = $this->formService->loadPostValue('level_id', FormValueType::VALID_LEVEL_ID);
323 $pageType = $this->formService->loadPostValue('page_type', FormValueType::VALID_SERVICE_PAGE_TYPE);
324 $pageId = $this->formService->loadPostValue('page', FormValueType::VALID_PAGE_ID);
325
326 if ($pageId === null) {
327 $this->pageRepository->removeServicePage($levelId, $pageType);
328 $this->redirectService
329 ->redirect(SubPage::SETTINGS_PAGES, Alert::EDIT_OTHER_PAGES_REMOVED, ['level' => $levelId]);
330 }
331
332 $this->pageRepository->updateServicePage($levelId, $pageType, $pageId);
333 $this->redirectService
334 ->redirect(SubPage::SETTINGS_PAGES, Alert::EDIT_OTHER_PAGES_UPDATED, ['level' => $levelId]);
335 }
336
337 public function handleSetSettings(): void
338 {
339 $this->formService->verifyNonceAndCapability('set_settings');
340
341 $loginPageId = $this->formService
342 ->loadPostValue(SettingsKey::LOGIN_PAGE, FormValueType::VALID_PAGE_ID);
343 $dashboardPageId = $this->formService
344 ->loadPostValue(SettingsKey::DASHBOARD_PAGE, FormValueType::VALID_PAGE_ID);
345
346 if (
347 $loginPageId !== null && get_post($loginPageId) === null ||
348 $dashboardPageId !== null && get_post($dashboardPageId) === null
349 ) {
350 $this->redirectService
351 ->redirect(SubPage::SETTINGS_SETTINGS, Alert::SETTINGS_SETTINGS_NO_VALID_PAGE);
352 }
353
354 $this->settingsRepository->createSettingsIfNeeded();
355
356 $settings = $this->settingsRepository->getSettings();
357 $settings->setLoginPageId($loginPageId);
358 $settings->setDashboardPageId($dashboardPageId);
359
360 $this->settingsRepository->updateSettings($settings);
361 $this->redirectService->redirect(SubPage::SETTINGS_SETTINGS, Alert::SETTINGS_SETTINGS_UPDATED);
362 }
363
364 public function handleSetUnlocking(): void
365 {
366 $this->formService->verifyNonceAndCapability('set_section_unlocking');
367
368 if (isset($_POST[SettingsKey::TIME_LOCKED_PAGE])) {
369 $timeLockedPageId = $this->formService
370 ->loadPostValue(SettingsKey::TIME_LOCKED_PAGE, FormValueType::VALID_PAGE_ID);
371
372 if ($timeLockedPageId !== null && get_post($timeLockedPageId) === null) {
373 $this->redirectService
374 ->redirect(SubPage::SETTINGS_UNLOCKING, Alert::SETTINGS_SETTINGS_NO_VALID_PAGE);
375 }
376
377 $settings = $this->settingsRepository->getSettings();
378 $settings->setTimeLockedPageId($timeLockedPageId);
379 $this->settingsRepository->updateSettings($settings);
380
381 $this->redirectService->redirect(SubPage::SETTINGS_UNLOCKING, Alert::SETTINGS_SETTINGS_UPDATED);
382 }
383
384 $levelId = $this->formService->loadPostValue('level_id', FormValueType::VALID_LEVEL_ID);
385 $buttonUnlock = $this->formService->loadPostValue('button_unlock', FormValueType::CHECKBOX);
386 $timeUnlock = $this->formService->loadPostValue('time_unlock', FormValueType::ANY_STRING);
387 $daysToUnlock = $this->formService->loadPostValue('days_to_unlock', FormValueType::SINGLE_INT);
388 $dateUnlock = $this->formService->loadPostValue('unlock_date', FormValueType::ANY_STRING);
389
390 $this->levelRepository
391 ->updateSetUnlocking($levelId, $buttonUnlock, $timeUnlock, $daysToUnlock, $dateUnlock);
392
393 $this->redirectService
394 ->redirect(SubPage::SETTINGS_UNLOCKING, Alert::SETTINGS_SETTINGS_UPDATED, ['level' => $levelId]);
395 }
396
397 public function handleButtonLevelUnlock(): void
398 {
399 $this->formService->verifyNonce('button_level_unlock');
400
401 $levelId = $this->formService->loadPostValue('level', FormValueType::VALID_LEVEL_ID);
402 $pageId = $this->formService->loadPostValue('page', FormValueType::VALID_PAGE_ID);
403
404 $user = $this->userRepository->getCurrentUser();
405 $level = $this->levelRepository->getLevelById($levelId);
406
407 if ($level === null || $user === null || !$this->levelRepository->isButtonUnlock($levelId)) {
408 $this->redirectService->redirectToNoAccessPage($levelId);
409 }
410
411 $memberships = $this->membershipRepository->getActiveByUserId($user->getId());
412 $ownsParent = false;
413
414 foreach ($memberships as $membership) {
415 if ($membership->getLevelId() === $level->getParentId()){
416 $ownsParent = true;
417 break;
418 }
419 }
420
421 if ($ownsParent === false) {
422 $this->redirectService->redirectToNoAccessPage($levelId);
423 }
424
425 $this->membershipService->saveOne(new Membership([
426 'level_id' => $levelId,
427 'user_id' => $user->getId(),
428 'registered' => DateTimeHelper::getNow()->format(Format::DATE_TIME),
429 'is_unlimited' => true,
430 ]));
431
432 if ($pageId === null) {
433 $pageId = $level->getAfterLoginPageId()
434 ?? $this->pageRepository->getCommonDashboardPageId();
435
436 if ($pageId === null) {
437 wp_redirect(home_url());
438 }
439 }
440
441 wp_redirect(home_url() . '/?page_id=' . $pageId);
442 }
443
444 public function savePostMetadata($postId): void
445 {
446 $levelAndSectionIds = $this->formService->loadPostValue(PostValue::SECTIONS, FormValueType::VALID_LEVEL_IDS);
447
448 if ($levelAndSectionIds === null) {
449 return;
450 }
451
452 $allLevels = $this->levelRepository->getAllAsLevels();
453
454 foreach ($allLevels as $level) {
455 $pages = $this->pageRepository->getLockedPageIdsByLevelId($level->getId());
456
457 if (in_array($level->getId(), $levelAndSectionIds, true)) {
458 $pages[] = (int) $postId;
459 } else {
460 foreach ($pages as $key => $levelPostId) {
461 if ($levelPostId === $postId) {
462 unset($pages[$key]);
463 }
464 }
465 }
466
467 $this->pageRepository->updatePages($level->getId(), $pages);
468 }
469 }
470
471 }
472