← All changes
|
backend/src/Controllers/Changelog/GetChangelogController.php
+97
-12
0.9
→
1.5
View file →
| @@ -6,13 +6,8 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace IvyForms\Controllers\Changelog; |
| 9 | 9 | |
| 10 | -// phpcs:disable PSR1.Files.SideEffects | |
| 11 | -if (!defined('ABSPATH')) { | |
| 12 | - exit; // Exit if accessed directly | |
| 13 | -} | |
| 14 | - | |
| 15 | 10 | use IvyForms\Common\Exceptions\ForbiddenException; |
| 16 | 11 | use IvyForms\Common\Sanitizer\Sanitizer; |
| 17 | 12 | use IvyForms\Controllers\Controller; |
| 18 | 13 | use IvyForms\Services\Changelog\ChangelogService; |
| @@ -18,8 +13,9 @@ | ||
| 18 | 13 | use IvyForms\Services\Changelog\ChangelogService; |
| 19 | 14 | use IvyForms\Services\Settings\SettingsService; |
| 20 | 15 | use WP_REST_Request; |
| 21 | 16 | use WP_REST_Response; |
| 17 | +use IvyForms\Services\API\IvyFormsAPI; | |
| 22 | 18 | |
| 23 | 19 | /** |
| 24 | 20 | * Class GetChangelogController |
| 25 | 21 | * |
| @@ -51,24 +47,44 @@ | ||
| 51 | 47 | // Verify the nonce |
| 52 | 48 | Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 53 | 49 | |
| 54 | 50 | $changelogData = $this->changelogService->getChangelogData(); |
| 51 | + /** @var array<string,mixed> $changelogData */ | |
| 55 | 52 | |
| 56 | - $oldVersion = $this->settingsService->getSetting('general', 'changelog_version'); | |
| 57 | - $shouldShow = $oldVersion !== IVYFORMS_VERSION; | |
| 53 | + // Get stored versions | |
| 54 | + $oldLiteVersion = $this->settingsService->getSetting('general', 'changelog_lite_version'); | |
| 55 | + $oldProVersion = $this->settingsService->getSetting('general', 'changelog_pro_version'); | |
| 58 | 56 | |
| 57 | + // Check if Pro plugin is active | |
| 58 | + // Use IvyFormsAPI::isProPluginActive() instead of relying on a settings flag | |
| 59 | + // which may not be initialized or reliable. | |
| 60 | + $isProActive = IvyFormsAPI::isProPluginActive(); | |
| 61 | + | |
| 62 | + // Extract the decision logic to a helper and avoid deep nesting/else clauses | |
| 63 | + $flags = $this->determineChangelogFlags($changelogData, $oldLiteVersion, $oldProVersion, $isProActive); | |
| 64 | + $shouldShow = $flags['shouldShow']; | |
| 65 | + $showLiteChangelog = $flags['showLiteChangelog']; | |
| 66 | + $showProChangelog = $flags['showProChangelog']; | |
| 67 | + | |
| 68 | + // Update stored versions if showing changelog | |
| 59 | 69 | if ($shouldShow) { |
| 60 | - $this->settingsService->setSetting('general', 'changelog_version', IVYFORMS_VERSION); | |
| 70 | + $this->settingsService->setSetting('general', 'changelog_lite_version', IVYFORMS_VERSION); | |
| 71 | + if ($isProActive && !empty($changelogData['pro_version'])) { | |
| 72 | + $this->settingsService->setSetting('general', 'changelog_pro_version', $changelogData['pro_version']); | |
| 73 | + } | |
| 61 | 74 | } |
| 62 | 75 | |
| 63 | 76 | $responseData = [ |
| 64 | - 'oldVersion' => $oldVersion, | |
| 77 | + 'oldLiteVersion' => $oldLiteVersion, | |
| 78 | + 'oldProVersion' => $oldProVersion, | |
| 65 | 79 | 'version' => IVYFORMS_VERSION, |
| 66 | 80 | 'release_date' => $changelogData['release_date'], |
| 67 | - 'features' => $changelogData['features'], | |
| 68 | - 'improvements' => $changelogData['improvements'], | |
| 69 | - 'bugfixes' => $changelogData['bugfixes'], | |
| 81 | + 'features' => $showLiteChangelog ? $changelogData['features'] : [], | |
| 82 | + 'improvements' => $showLiteChangelog ? $changelogData['improvements'] : [], | |
| 83 | + 'bugfixes' => $showLiteChangelog ? $changelogData['bugfixes'] : [], | |
| 70 | 84 | 'shouldShow' => $shouldShow, |
| 85 | + 'showProChangelog' => $showProChangelog, | |
| 86 | + 'isProActive' => $isProActive, | |
| 71 | 87 | ]; |
| 72 | 88 | |
| 73 | 89 | /** |
| 74 | 90 | * Filter changelog response data to allow Pro plugin to modify version, release date, etc. |
| @@ -88,6 +104,75 @@ | ||
| 88 | 104 | return new WP_REST_Response([ |
| 89 | 105 | 'message' => __('Changelog retrieved successfully', 'ivyforms'), |
| 90 | 106 | 'data' => $responseData |
| 91 | 107 | ], 200); |
| 108 | + } | |
| 109 | + | |
| 110 | + /** | |
| 111 | + * Determine which changelog(s) should be shown. | |
| 112 | + * Extracted to reduce complexity and improve testability. | |
| 113 | + * | |
| 114 | + * @param array<string,mixed> $changelogData | |
| 115 | + * @param string|null $oldLiteVersion | |
| 116 | + * @param string|null $oldProVersion | |
| 117 | + * @param bool $isProActive | |
| 118 | + * @return array{shouldShow: bool, showLiteChangelog: bool, showProChangelog: bool} | |
| 119 | + */ | |
| 120 | + private function determineChangelogFlags( | |
| 121 | + array $changelogData, | |
| 122 | + ?string $oldLiteVersion, | |
| 123 | + ?string $oldProVersion, | |
| 124 | + bool $isProActive | |
| 125 | + ): array { | |
| 126 | + $shouldShow = false; | |
| 127 | + $showLiteChangelog = false; | |
| 128 | + $showProChangelog = false; | |
| 129 | + | |
| 130 | + $liteUpdated = $oldLiteVersion !== IVYFORMS_VERSION; | |
| 131 | + | |
| 132 | + $proUpdated = false; | |
| 133 | + // Use defensive checks for optional pro_version key that may be added by Pro plugin | |
| 134 | + if ($isProActive && isset($changelogData['pro_version']) && $changelogData['pro_version'] !== '') { | |
| 135 | + $proUpdated = $oldProVersion !== (string) $changelogData['pro_version']; | |
| 136 | + } | |
| 137 | + | |
| 138 | + if (!$isProActive) { | |
| 139 | + // Only lite is active | |
| 140 | + if ($liteUpdated) { | |
| 141 | + $shouldShow = true; | |
| 142 | + $showLiteChangelog = true; | |
| 143 | + } | |
| 144 | + | |
| 145 | + return [ | |
| 146 | + 'shouldShow' => $shouldShow, | |
| 147 | + 'showLiteChangelog' => $showLiteChangelog, | |
| 148 | + 'showProChangelog' => $showProChangelog, | |
| 149 | + ]; | |
| 150 | + } | |
| 151 | + | |
| 152 | + // Both lite and pro are active | |
| 153 | + if ($proUpdated) { | |
| 154 | + // Pro was updated (or both were updated) - show merged changelog | |
| 155 | + $shouldShow = true; | |
| 156 | + $showLiteChangelog = true; | |
| 157 | + $showProChangelog = true; | |
| 158 | + | |
| 159 | + return [ | |
| 160 | + 'shouldShow' => $shouldShow, | |
| 161 | + 'showLiteChangelog' => $showLiteChangelog, | |
| 162 | + 'showProChangelog' => $showProChangelog, | |
| 163 | + ]; | |
| 164 | + } | |
| 165 | + | |
| 166 | + if ($liteUpdated) { | |
| 167 | + // Only lite was updated | |
| 168 | + $shouldShow = true; | |
| 169 | + $showLiteChangelog = true; | |
| 170 | + } | |
| 171 | + | |
| 172 | + return [ | |
| 173 | + 'shouldShow' => $shouldShow, | |
| 174 | + 'showLiteChangelog' => $showLiteChangelog, | |
| 175 | + 'showProChangelog' => $showProChangelog, | |
| 176 | + ]; | |
| 92 | 177 | } |
| 93 | 178 | } |