PluginProbe ʕ •ᴥ•ʔ
FAPI Member / 2.2.24
FAPI Member v2.2.24
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 / Utils / DisplayHelper.php
fapi-member / src / Utils Last commit date
AlertProvider.php 11 months ago ApiClient.php 11 months ago DateTimeHelper.php 11 months ago DateTimes.php 11 months ago DateTimesImmutable.php 11 months ago Debugger.php 11 months ago DisplayHelper.php 11 months ago EmailHelper.php 11 months ago PostTypeHelper.php 11 months ago Random.php 11 months ago SecurityValidator.php 11 months ago ShortcodeSubstitutor.php 11 months ago TemplateProvider.php 11 months ago functions.php 11 months ago
DisplayHelper.php
73 lines
1 <?php declare(strict_types = 1);
2
3 namespace FapiMember\Utils;
4
5 final class DisplayHelper
6 {
7
8 public static function shouldContentBeRendered(
9 string|bool $hasSectionOrLevel,
10 array|string|null $fapiSectionAndLevels,
11 $wpUserId = null,
12 ): bool
13 {
14 if (!in_array($hasSectionOrLevel, ['1', '0', true, false], true)) {
15 return true;
16 }
17
18 if (!isset($fapiSectionAndLevels)) {
19 return true;
20 }
21
22 if (is_string($fapiSectionAndLevels)) {
23 $sectionAndLevels = json_decode($fapiSectionAndLevels, true);
24 } elseif (is_array($fapiSectionAndLevels)) {
25 $sectionAndLevels = $fapiSectionAndLevels;
26 } else {
27 return true;
28 }
29
30 if ($sectionAndLevels === [] || $sectionAndLevels === null) {
31 return true;
32 }
33
34 $sectionAndLevels = array_map(
35 static function ($item) {
36 return (int) $item;
37 },
38 $sectionAndLevels
39 );
40
41 $hasMemberSectionOrLevel = (bool) $hasSectionOrLevel;
42 $userId = isset($wpUserId) && $wpUserId ? $wpUserId : get_current_user_id();
43
44 global $membershipRepository;
45 $memberships = $membershipRepository->getAllByUserId($userId);
46
47 if ($hasMemberSectionOrLevel === true) {
48 foreach ($memberships as $membership) {
49 if (in_array($membership->getLevelId(), $sectionAndLevels, true)) {
50 return true;
51 }
52 }
53
54 return false;
55 }
56
57 if ($memberships === []) {
58 return true;
59 }
60
61 foreach ($sectionAndLevels as $sectionAndLevel) {
62 foreach ($memberships as $membership) {
63 if ($membership->getLevelId() === $sectionAndLevel) {
64 return false;
65 }
66 }
67 }
68
69 return true;
70 }
71
72 }
73