AlertProvider.php
1 year ago
ApiClient.php
1 day ago
DateTimeHelper.php
1 year ago
DateTimes.php
2 years ago
DateTimesImmutable.php
2 years ago
Debugger.php
1 year ago
DisplayHelper.php
1 year ago
EmailHelper.php
1 year ago
PostTypeHelper.php
2 years ago
Random.php
4 years ago
SecurityValidator.php
2 years ago
ShortcodeSubstitutor.php
2 months ago
TemplateProvider.php
2 years ago
functions.php
3 years ago
TemplateProvider.php
146 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FapiMember\Utils; |
| 4 | |
| 5 | use FapiMember\Container\Container; |
| 6 | use FapiMember\FapiMemberPlugin; |
| 7 | use FapiMember\Model\Enums\UserPermission; |
| 8 | use FapiMember\Service\ApiService; |
| 9 | |
| 10 | class TemplateProvider |
| 11 | { |
| 12 | private FapiMemberPlugin $fapiMemberPlugin; |
| 13 | private ApiService $apiService; |
| 14 | |
| 15 | public function __construct() |
| 16 | { |
| 17 | $this->fapiMemberPlugin = Container::get(FapiMemberPlugin::class); |
| 18 | $this->apiService = Container::get(ApiService::class); |
| 19 | } |
| 20 | |
| 21 | public function displayCurrentTemplate(): void |
| 22 | { |
| 23 | if (!current_user_can(UserPermission::REQUIRED_CAPABILITY)) { |
| 24 | wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); |
| 25 | } |
| 26 | |
| 27 | $subpage = $this->findSubpage(); |
| 28 | |
| 29 | $this->{sprintf( 'show%s', ucfirst( $subpage ))}(); |
| 30 | } |
| 31 | |
| 32 | public function findSubpage(): string |
| 33 | { |
| 34 | $subpage = (isset($_GET['subpage'])) ? $this->sanitizeSubpage($_GET['subpage']) : null; |
| 35 | |
| 36 | if (!$subpage) { |
| 37 | return 'index'; |
| 38 | } |
| 39 | |
| 40 | return $subpage; |
| 41 | } |
| 42 | |
| 43 | protected function sanitizeSubpage($subpage): string|null |
| 44 | { |
| 45 | if (!is_string( $subpage ) || $subpage === '') { |
| 46 | return null; |
| 47 | } |
| 48 | if (!method_exists( $this, sprintf( 'show%s', ucfirst( $subpage )))) { |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | return $subpage; |
| 53 | } |
| 54 | |
| 55 | protected function showSettingsSectionNew(): void |
| 56 | { |
| 57 | $this->showTemplate('settingsSectionNew'); |
| 58 | } |
| 59 | |
| 60 | protected function showSettingsLevelNew(): void |
| 61 | { |
| 62 | $this->showTemplate('settingsLevelNew'); |
| 63 | } |
| 64 | |
| 65 | protected function showSettingsContentSelect(): void |
| 66 | { |
| 67 | $this->showTemplate('settingsContentSelect'); |
| 68 | } |
| 69 | |
| 70 | protected function showSettingsContentRemove(): void |
| 71 | { |
| 72 | $this->showTemplate('settingsContentRemove'); |
| 73 | } |
| 74 | |
| 75 | protected function showSettingsContentAdd(): void |
| 76 | { |
| 77 | $this->showTemplate('settingsContentAdd'); |
| 78 | } |
| 79 | |
| 80 | protected function showConnection(): void |
| 81 | { |
| 82 | $this->showTemplate('connection'); |
| 83 | } |
| 84 | |
| 85 | protected function showSettingsEmails(): void |
| 86 | { |
| 87 | $this->showTemplate('settingsEmails'); |
| 88 | } |
| 89 | |
| 90 | protected function showSettingsElements(): void |
| 91 | { |
| 92 | $this->showTemplate('settingsElements'); |
| 93 | } |
| 94 | |
| 95 | protected function showSettingsSettings(): void |
| 96 | { |
| 97 | $this->showTemplate('settingsSettings'); |
| 98 | } |
| 99 | |
| 100 | protected function showSettingsPages(): void |
| 101 | { |
| 102 | $this->showTemplate('settingsPages'); |
| 103 | } |
| 104 | |
| 105 | protected function showMemberList(): void |
| 106 | { |
| 107 | $this->showTemplate('memberList'); |
| 108 | } |
| 109 | |
| 110 | protected function showTest(): void |
| 111 | { |
| 112 | if (!$this->fapiMemberPlugin->isDevelopment()) { |
| 113 | wp_die('This path is only allowed in development.'); |
| 114 | } |
| 115 | |
| 116 | $this->showTemplate('test'); |
| 117 | } |
| 118 | |
| 119 | protected function showSettingsUnlocking(): void |
| 120 | { |
| 121 | $this->showTemplate('settingsUnlocking'); |
| 122 | } |
| 123 | |
| 124 | protected function showIndex(): void |
| 125 | { |
| 126 | if (!$this->apiService->areApiCredentialsSet()) { |
| 127 | $this->showTemplate('connection'); |
| 128 | } |
| 129 | |
| 130 | $this->showTemplate('index'); |
| 131 | } |
| 132 | |
| 133 | protected function showTemplate($name): void |
| 134 | { |
| 135 | $areApiCredentialsSet = $this->apiService->areApiCredentialsSet(); |
| 136 | $subpage = $this->findSubpage(); |
| 137 | |
| 138 | $path = sprintf('%s/../../templates/%s.php', __DIR__, $name); |
| 139 | |
| 140 | if (file_exists($path)) { |
| 141 | include $path; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | } |
| 146 |