| 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 |
// phpcs:disable PSR1.Files.SideEffects |
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
use IvyForms\Common\Exceptions\ForbiddenException; |
| 16 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 17 |
use IvyForms\Controllers\Controller; |
| 18 |
use IvyForms\Services\Changelog\ChangelogService; |
| 19 |
use IvyForms\Services\Settings\SettingsService; |
| 20 |
use WP_REST_Request; |
| 21 |
use WP_REST_Response; |
| 22 |
|
| 23 |
/** |
| 24 |
* Class GetChangelogController |
| 25 |
* |
| 26 |
* @package IvyForms\Controllers\Changelog |
| 27 |
*/ |
| 28 |
class GetChangelogController extends Controller |
| 29 |
{ |
| 30 |
private ChangelogService $changelogService; |
| 31 |
private SettingsService $settingsService; |
| 32 |
|
| 33 |
public function __construct( |
| 34 |
ChangelogService $changelogService, |
| 35 |
SettingsService $settingsService |
| 36 |
) { |
| 37 |
$this->changelogService = $changelogService; |
| 38 |
$this->settingsService = $settingsService; |
| 39 |
} |
| 40 |
/** |
| 41 |
* Get changelog data with translated strings |
| 42 |
* |
| 43 |
* @param WP_REST_Request $data |
| 44 |
* |
| 45 |
* @return WP_REST_Response |
| 46 |
* |
| 47 |
* @throws ForbiddenException |
| 48 |
*/ |
| 49 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 50 |
{ |
| 51 |
// Verify the nonce |
| 52 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 53 |
|
| 54 |
$changelogData = $this->changelogService->getChangelogData(); |
| 55 |
|
| 56 |
$oldVersion = $this->settingsService->getSetting('general', 'changelog_version'); |
| 57 |
$shouldShow = $oldVersion !== IVYFORMS_VERSION; |
| 58 |
|
| 59 |
if ($shouldShow) { |
| 60 |
$this->settingsService->setSetting('general', 'changelog_version', IVYFORMS_VERSION); |
| 61 |
} |
| 62 |
|
| 63 |
$responseData = [ |
| 64 |
'oldVersion' => $oldVersion, |
| 65 |
'version' => IVYFORMS_VERSION, |
| 66 |
'release_date' => $changelogData['release_date'], |
| 67 |
'features' => $changelogData['features'], |
| 68 |
'improvements' => $changelogData['improvements'], |
| 69 |
'bugfixes' => $changelogData['bugfixes'], |
| 70 |
'shouldShow' => $shouldShow, |
| 71 |
]; |
| 72 |
|
| 73 |
/** |
| 74 |
* Filter changelog response data to allow Pro plugin to modify version, release date, etc. |
| 75 |
* |
| 76 |
* @param array $responseData The response data array |
| 77 |
* @param array $changelogData The full changelog data from service |
| 78 |
* @param SettingsService $settingsService The settings service instance |
| 79 |
* @return array Modified response data |
| 80 |
*/ |
| 81 |
$responseData = apply_filters( |
| 82 |
'ivyforms/changelog/response_data', |
| 83 |
$responseData, |
| 84 |
$changelogData, |
| 85 |
$this->settingsService |
| 86 |
); |
| 87 |
|
| 88 |
return new WP_REST_Response([ |
| 89 |
'message' => __('Changelog retrieved successfully', 'ivyforms'), |
| 90 |
'data' => $responseData |
| 91 |
], 200); |
| 92 |
} |
| 93 |
} |
| 94 |
|