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