GlobalStyleRequest.php
1 week ago
PageDataRequest.php
1 week ago
PageRequest.php
1 week ago
PageSettingsRequest.php
1 week ago
PageUpdateRequest.php
1 week ago
PopupRequest.php
1 week ago
RenameStagingVersionRequest.php
1 week ago
StagingVersionRequest.php
1 week ago
TogglePageSymbolRequest.php
1 month ago
PageRequest.php
78 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App\Http\Requests\Page; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | use Kirki\App\Constants\PostTypes; |
| 8 | use Kirki\App\Constants\UtilityPageType; |
| 9 | use Kirki\App\Services\UtilityPageService; |
| 10 | use Kirki\Framework\Http\Request; |
| 11 | use Kirki\Framework\Sanitizer; |
| 12 | |
| 13 | use function Kirki\App\ensure_array; |
| 14 | |
| 15 | class PageRequest extends Request |
| 16 | { |
| 17 | protected function prepare_for_validation() |
| 18 | { |
| 19 | $this->merge([ |
| 20 | 'blocks' => ensure_array($this->blocks), |
| 21 | 'conditions' => ensure_array($this->conditions), |
| 22 | 'custom_template' => ensure_array($this->custom_template), |
| 23 | ]); |
| 24 | } |
| 25 | |
| 26 | public function rules() |
| 27 | { |
| 28 | return [ |
| 29 | 'post_title' => 'required|string', |
| 30 | 'post_type' => 'required|string', |
| 31 | 'blocks' => 'nullable|array', |
| 32 | 'conditions' => 'nullable|array', |
| 33 | 'collection_type' => 'nullable|string', |
| 34 | 'utility_page_type' => [ |
| 35 | 'nullable', |
| 36 | 'string', |
| 37 | 'in:' . UtilityPageType::join(), |
| 38 | function ($value) { |
| 39 | if ($this->post_type === PostTypes::UTILITY) { |
| 40 | if (empty($value)) { |
| 41 | return __("Utility page type is required.", 'kirki'); |
| 42 | } |
| 43 | |
| 44 | $exists = UtilityPageService::create()->exists($value); |
| 45 | |
| 46 | if ($exists) { |
| 47 | /** translators: %s: Utility page type */ |
| 48 | return sprintf(__("Utility page of type %s already exists."), $value); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return true; |
| 53 | } |
| 54 | ], |
| 55 | 'custom_template' => 'nullable|array', |
| 56 | 'custom_template.url' => 'nullable|url', |
| 57 | 'content_manager_collection_id' => 'nullable|integer', |
| 58 | 'content_manager_page_kind' => 'nullable|string|in:index,details', |
| 59 | ]; |
| 60 | } |
| 61 | |
| 62 | public function filters() |
| 63 | { |
| 64 | return [ |
| 65 | 'post_title' => Sanitizer::TEXT, |
| 66 | 'post_type' => Sanitizer::TEXT, |
| 67 | 'blocks' => Sanitizer::ARRAY, |
| 68 | 'conditions' => Sanitizer::ARRAY, |
| 69 | 'collection_type' => Sanitizer::TEXT, |
| 70 | 'utility_page_type' => Sanitizer::TEXT, |
| 71 | 'custom_template' => Sanitizer::ARRAY, |
| 72 | 'custom_template.url' => Sanitizer::URL, |
| 73 | 'content_manager_collection_id' => Sanitizer::INT, |
| 74 | 'content_manager_page_kind' => Sanitizer::TEXT, |
| 75 | ]; |
| 76 | } |
| 77 | } |
| 78 |