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 / Repository / PageRepository.php
fapi-member / src / Repository Last commit date
EmailRepository.php 11 months ago LevelOrderRepository.php 11 months ago LevelRepository.php 11 months ago MemberActivityRepository.php 11 months ago MembershipChangeRepository.php 11 months ago MembershipRepository.php 11 months ago PageRepository.php 11 months ago Repository.php 11 months ago SettingsRepository.php 11 months ago UserRepository.php 11 months ago
PageRepository.php
231 lines
1 <?php
2
3 namespace FapiMember\Repository;
4
5 use FapiMember\Container\Container;
6 use FapiMember\Library\Nette\Utils\Json;
7 use FapiMember\Model\Enums\Keys\MetaKey;
8 use FapiMember\Model\Enums\Keys\OptionKey;
9 use FapiMember\Model\Page;
10 use FapiMember\Utils\PostTypeHelper;
11 use WP_Post;
12
13 class PageRepository extends Repository
14 {
15 private SettingsRepository $settingsRepository;
16
17 public function __construct()
18 {
19 $this->settingsRepository = Container::get(SettingsRepository::class);
20 $this->key = MetaKey::PAGES;
21 }
22
23 public function removeServicePage(int $levelId, string $pageType): void
24 {
25 $this->deleteTermMeta($levelId, $this->getPageTemplateKey($pageType));
26 }
27
28 public function updateServicePage(int $levelId, string $pageType, int|null $page): void
29 {
30 $this->updateTermMeta(
31 $levelId,
32 $this->getPageTemplateKey($pageType),
33 $page,
34 );
35 }
36
37 /**
38 * @return array<Page>
39 */
40 public function getAllPages($includingCpt = false): array
41 {
42 $posts = get_posts(
43 array(
44 'post_type' => PostTypeHelper::getSupportedPostTypes(),
45 'post_status' => array( 'publish' ),
46 'numberposts' => -1,
47 'orderby' => 'page_title',
48 'order' => 'ASC',
49 )
50 );
51
52 $cpts = [];
53
54 if ($includingCpt) {
55 $cpts = PostTypeHelper::getSupportedPostTypes(true);
56 }
57
58 return $this->postsToPages($posts, $cpts);
59 }
60
61 /**
62 * @param array<WP_Post> $posts
63 * @return array<Page>
64 */
65 private function postsToPages(array $posts, array $cpts): array
66 {
67 $pages = [];
68
69 foreach ($posts as $post) {
70 $pages[] = new Page([
71 'id' => $post->ID,
72 'title' => $post->post_title,
73 'type' => $post->post_type,
74 'url' => wp_make_link_relative(get_permalink($post->ID)),
75 ]);
76 }
77
78 foreach ($cpts as $cpt) {
79 $pages[] = new Page([
80 'id' => $cpt,
81 'title' => $cpt,
82 'type' => 'cpt',
83 'url' => '',
84 ]);
85 }
86
87 return $pages;
88 }
89
90 /**
91 * @return array<int, string>
92 */
93 public function getPageIdsByLevelId(int $levelId): array
94 {
95 $pages = $this->getTermMeta($levelId, $this->key);
96 $pages = (empty($pages)) ? [] : array_values(json_decode($pages, true));
97
98 $cptsByLevels = get_option(OptionKey::POST_TYPES, array());
99
100 if (!isset($cptsByLevels[$levelId])) {
101 return $pages;
102 }
103
104 foreach ($cptsByLevels[$levelId] as $cpt) {
105 $pages[] = $cpt;
106 }
107
108 return $pages;
109 }
110
111 public function getPageTemplateKey(string $type): string
112 {
113 return sprintf('fapi_page_%s', $type);
114 }
115
116 public function addPages(int $levelId, array $newPageIds): array
117 {
118 $old = $this->getTermMeta($levelId, $this->key);
119 $old = (empty( $old )) ? null : json_decode($old, true);
120
121 $all = ($old === null) ? $newPageIds : array_merge( $old, $newPageIds);
122
123 return $this->updatePagesForLevel($levelId, $all);
124 }
125
126 public function updatePagesForLevel(int $levelId, array $pagesData): array
127 {
128 $pagesData = array_values(array_unique($pagesData));
129 $pages = [];
130 $cpts = [];
131
132 foreach ($pagesData as $pageData) {
133 if (gettype($pageData) === 'string') {
134 $cpts[] = $pageData;
135 } elseif (gettype($pageData) === 'integer') {
136 $pages[] = $pageData;
137 }
138 }
139
140 $this->updateTermMeta($levelId, $this->key, json_encode($pages));
141
142 $allStoredPostTypes = get_option(OptionKey::POST_TYPES, array());
143 $allStoredPostTypes[$levelId] = $cpts;
144 update_option(OptionKey::POST_TYPES, $allStoredPostTypes);
145
146 return Json::decode($this->getTermMeta($levelId, $this->key));
147 }
148
149 public function getPageUrlById(int|null $pageId): string|null
150 {
151 if ($pageId === null) {
152 return null;
153 }
154
155 $page = get_post($pageId);
156
157 if ($page === null) {
158 return null;
159 }
160
161 $link = get_permalink($page);
162
163 if ($link === false) {
164 return null;
165 }
166
167 return $link;
168 }
169
170
171 public function getNoAccessPageId(int $levelId): int|null
172 {
173 return $this->pageIdToIntOrNull(
174 $this->getTermMeta($levelId, MetaKey::NO_ACCESS_PAGE),
175 );
176 }
177
178 public function getLoginPageId(int $levelId): int|null
179 {
180 return $this->pageIdToIntOrNull(
181 $this->getTermMeta($levelId, MetaKey::LOGIN_PAGE),
182 );
183 }
184
185 public function getAfterLoginPageId(int $levelId): int|null
186 {
187 return $this->pageIdToIntOrNull(
188 $this->getTermMeta($levelId, MetaKey::AFTER_LOGIN_PAGE),
189 );
190 }
191
192 public function getCommonLoginPageId(): int|null
193 {
194 return $this->settingsRepository->getSettings()?->getLoginPageId();
195 }
196
197 public function getCommonDashboardPageId(): int|null
198 {
199 return $this->settingsRepository->getSettings()?->getDashboardPageId();
200 }
201
202 public function getTimedUnlockNoAccessPageId(): int|null
203 {
204 return $this->settingsRepository->getSettings()?->getTimeLockedPageId();
205 }
206
207 public function getLockedPageIdsByLevelId(int $levelId): array
208 {
209 $pages = json_decode($this->getTermMeta(
210 $levelId,
211 $this->key,
212 ));
213
214 if ($pages === null) {
215 return [];
216 }
217
218 return $pages;
219 }
220
221 private function pageIdToIntOrNull(mixed $pageId): int|null
222 {
223 if ($pageId === null || $pageId === '') {
224 return null;
225 }
226
227 return (int) $pageId;
228 }
229
230 }
231