| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See COPYING.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Controllers\Changelog; |
| 9 |
|
| 10 |
use IvyForms\Common\Exceptions\ForbiddenException; |
| 11 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 12 |
use IvyForms\Controllers\Controller; |
| 13 |
use IvyForms\Services\Changelog\ChangelogService; |
| 14 |
use IvyForms\Services\Settings\SettingsService; |
| 15 |
use WP_REST_Request; |
| 16 |
use WP_REST_Response; |
| 17 |
use IvyForms\Services\API\IvyFormsAPI; |
| 18 |
|
| 19 |
/** |
| 20 |
* Class GetChangelogController |
| 21 |
* |
| 22 |
* @package IvyForms\Controllers\Changelog |
| 23 |
*/ |
| 24 |
class GetChangelogController extends Controller |
| 25 |
{ |
| 26 |
private ChangelogService $changelogService; |
| 27 |
private SettingsService $settingsService; |
| 28 |
|
| 29 |
public function __construct( |
| 30 |
ChangelogService $changelogService, |
| 31 |
SettingsService $settingsService |
| 32 |
) { |
| 33 |
$this->changelogService = $changelogService; |
| 34 |
$this->settingsService = $settingsService; |
| 35 |
} |
| 36 |
/** |
| 37 |
* Get changelog data with translated strings |
| 38 |
* |
| 39 |
* @param WP_REST_Request $data |
| 40 |
* |
| 41 |
* @return WP_REST_Response |
| 42 |
* |
| 43 |
* @throws ForbiddenException |
| 44 |
*/ |
| 45 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 46 |
{ |
| 47 |
// Verify the nonce |
| 48 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 49 |
|
| 50 |
$changelogData = $this->changelogService->getChangelogData(); |
| 51 |
/** @var array<string,mixed> $changelogData */ |
| 52 |
|
| 53 |
// Get stored versions |
| 54 |
$oldLiteVersion = $this->settingsService->getSetting('general', 'changelog_lite_version'); |
| 55 |
$oldProVersion = $this->settingsService->getSetting('general', 'changelog_pro_version'); |
| 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 |
| 69 |
if ($shouldShow) { |
| 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 |
} |
| 74 |
} |
| 75 |
|
| 76 |
$responseData = [ |
| 77 |
'oldLiteVersion' => $oldLiteVersion, |
| 78 |
'oldProVersion' => $oldProVersion, |
| 79 |
'version' => IVYFORMS_VERSION, |
| 80 |
'release_date' => $changelogData['release_date'], |
| 81 |
'features' => $showLiteChangelog ? $changelogData['features'] : [], |
| 82 |
'improvements' => $showLiteChangelog ? $changelogData['improvements'] : [], |
| 83 |
'bugfixes' => $showLiteChangelog ? $changelogData['bugfixes'] : [], |
| 84 |
'shouldShow' => $shouldShow, |
| 85 |
'showProChangelog' => $showProChangelog, |
| 86 |
'isProActive' => $isProActive, |
| 87 |
]; |
| 88 |
|
| 89 |
/** |
| 90 |
* Filter changelog response data to allow Pro plugin to modify version, release date, etc. |
| 91 |
* |
| 92 |
* @param array $responseData The response data array |
| 93 |
* @param array $changelogData The full changelog data from service |
| 94 |
* @param SettingsService $settingsService The settings service instance |
| 95 |
* @return array Modified response data |
| 96 |
*/ |
| 97 |
$responseData = apply_filters( |
| 98 |
'ivyforms/changelog/response_data', |
| 99 |
$responseData, |
| 100 |
$changelogData, |
| 101 |
$this->settingsService |
| 102 |
); |
| 103 |
|
| 104 |
return new WP_REST_Response([ |
| 105 |
'message' => __('Changelog retrieved successfully', 'ivyforms'), |
| 106 |
'data' => $responseData |
| 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 |
]; |
| 177 |
} |
| 178 |
} |
| 179 |
|