AppsController.php
1 month ago
CollaborationCommentController.php
1 month ago
CollaborationController.php
1 week ago
CollectionController.php
1 week ago
CollectionItemController.php
1 month ago
EditorController.php
1 week ago
FormController.php
1 week ago
GlobalDataController.php
2 weeks ago
MediaController.php
1 month ago
PageController.php
1 week ago
PageSettingsController.php
1 week ago
PostController.php
1 week ago
UserController.php
1 week ago
PageController.php
316 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App\Http\Controllers\Api; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | use Exception; |
| 8 | use Kirki\App\Constants\PostTypes; |
| 9 | use Kirki\App\DTO\Page\EditorPagePayloadDTO; |
| 10 | use Kirki\App\DTO\Page\EditPageDTO; |
| 11 | use Kirki\App\DTO\Page\EditPopupDTO; |
| 12 | use Kirki\App\DTO\Page\PageFilterDTO; |
| 13 | use Kirki\App\DTO\Page\PagePayloadDTO; |
| 14 | use Kirki\App\DTO\Page\TogglePageSymbolDTO; |
| 15 | use Kirki\App\Http\Requests\CollectionItem\CollectionItemConditionRequest; |
| 16 | use Kirki\App\Http\Requests\Page\PageDataRequest; |
| 17 | use Kirki\App\Http\Requests\Page\PageRequest; |
| 18 | use Kirki\App\Http\Requests\Page\PageUpdateRequest; |
| 19 | use Kirki\App\Http\Requests\Page\PopupRequest; |
| 20 | use Kirki\App\Http\Requests\Page\RenameStagingVersionRequest; |
| 21 | use Kirki\App\Http\Requests\Page\StagingVersionRequest; |
| 22 | use Kirki\App\Http\Requests\Page\TogglePageSymbolRequest; |
| 23 | use Kirki\App\Models\Page as PageModel; |
| 24 | use Kirki\App\Models\Post as PostModel; |
| 25 | use Kirki\App\Resources\PageContentResource; |
| 26 | use Kirki\App\Resources\PageResource; |
| 27 | use Kirki\App\Services\PageService; |
| 28 | use Kirki\App\Supports\CollectionItem; |
| 29 | use Kirki\App\Supports\Facades\Page; |
| 30 | use Kirki\Framework\Http\Request; |
| 31 | use Kirki\Framework\Http\Response; |
| 32 | |
| 33 | use function Kirki\Framework\response; |
| 34 | |
| 35 | class PageController |
| 36 | { |
| 37 | /** @var PageService */ |
| 38 | protected $service; |
| 39 | |
| 40 | public function __construct(PageService $service) |
| 41 | { |
| 42 | $this->service = $service; |
| 43 | } |
| 44 | |
| 45 | public function save(PageRequest $request) |
| 46 | { |
| 47 | $payload = PagePayloadDTO::from_array($request->sanitized()); |
| 48 | |
| 49 | $page = $this->service->save($payload); |
| 50 | |
| 51 | return response()->json([ |
| 52 | 'data' => new PageResource($page), |
| 53 | 'message' => __('Page created successfully.', 'kirki'), |
| 54 | ], Response::OK); |
| 55 | } |
| 56 | |
| 57 | public function save_page_data(PageDataRequest $request, int $page_id, string $page_content_type) |
| 58 | { |
| 59 | $page = PageModel::exclude_trash()->find($page_id); |
| 60 | |
| 61 | if (empty($page)) { |
| 62 | throw new Exception(esc_html__('Page not found.', 'kirki'), Response::NOT_FOUND); |
| 63 | } |
| 64 | |
| 65 | $payload = EditorPagePayloadDTO::from_array($request->sanitized()); |
| 66 | $payload->page = $page; |
| 67 | |
| 68 | return response()->json([ |
| 69 | 'data' => [ |
| 70 | 'staging_version' => $this->service->save_page_data($payload, $page_content_type), |
| 71 | 'status' => __('Page data saved.', 'kirki'), |
| 72 | ], |
| 73 | 'message' => __('Page data saved.', 'kirki'), |
| 74 | ], Response::OK); |
| 75 | } |
| 76 | |
| 77 | public function get_all_staged_versions(Request $request) |
| 78 | { |
| 79 | $page = PageModel::find($request->int('page_id')); |
| 80 | |
| 81 | if (empty($page)) { |
| 82 | throw new Exception(esc_html__('Page not found.', 'kirki'), Response::NOT_FOUND); |
| 83 | } |
| 84 | |
| 85 | return response()->json([ |
| 86 | 'data' => Page::get_all_staged_versions($page->ID, false), |
| 87 | ], Response::OK); |
| 88 | } |
| 89 | |
| 90 | public function publish_staging_version(Request $request) |
| 91 | { |
| 92 | $page = PageModel::exclude_trash()->find($request->int('page_id')); |
| 93 | |
| 94 | if (empty($page)) { |
| 95 | throw new Exception(esc_html__('Page not found.', 'kirki'), Response::NOT_FOUND); |
| 96 | } |
| 97 | |
| 98 | return response()->json([ |
| 99 | 'data' => Page::publish_stage_version($page->ID), |
| 100 | 'message' => __('Staging version published successfully.', 'kirki'), |
| 101 | ], Response::OK); |
| 102 | } |
| 103 | |
| 104 | public function rename_staging_version(RenameStagingVersionRequest $request) |
| 105 | { |
| 106 | $page = PageModel::find($request->int('page_id')); |
| 107 | |
| 108 | if (empty($page)) { |
| 109 | throw new Exception(esc_html__('Page not found.', 'kirki'), Response::NOT_FOUND); |
| 110 | } |
| 111 | |
| 112 | return response()->json([ |
| 113 | 'data' => Page::rename_stage_version($page->ID, $request->int('version_id'), $request->string('name')), |
| 114 | 'message' => __('Staging version renamed successfully.', 'kirki'), |
| 115 | ], Response::OK); |
| 116 | } |
| 117 | |
| 118 | public function restore_staging_version(StagingVersionRequest $request) |
| 119 | { |
| 120 | $page = PageModel::find($request->int('page_id')); |
| 121 | |
| 122 | if (empty($page)) { |
| 123 | throw new Exception(esc_html__('Page not found.', 'kirki'), Response::NOT_FOUND); |
| 124 | } |
| 125 | |
| 126 | return response()->json([ |
| 127 | 'data' => [ |
| 128 | 'new_version' => Page::restore_stage_version($page->ID, $request->int('version_id')), |
| 129 | 'versions' => Page::get_all_staged_versions($page->ID, false), |
| 130 | ], |
| 131 | 'message' => __('Staging version renamed successfully.', 'kirki'), |
| 132 | ], Response::OK); |
| 133 | } |
| 134 | |
| 135 | public function remove_staging_version(StagingVersionRequest $request) |
| 136 | { |
| 137 | $page = PageModel::find($request->int('page_id')); |
| 138 | |
| 139 | if (empty($page)) { |
| 140 | throw new Exception(esc_html__('Page not found.', 'kirki'), Response::NOT_FOUND); |
| 141 | } |
| 142 | |
| 143 | return response()->json([ |
| 144 | 'data' => Page::remove_stage_version($page->ID, $request->int('version_id')), |
| 145 | 'message' => __('Staging version removed successfully.', 'kirki'), |
| 146 | ], Response::OK); |
| 147 | } |
| 148 | |
| 149 | public function update(PageUpdateRequest $request, int $page_id) |
| 150 | { |
| 151 | $page = PageModel::exclude_trash()->find($page_id); |
| 152 | |
| 153 | if (empty($page)) { |
| 154 | throw new Exception(esc_html__('Page not found.', 'kirki'), Response::NOT_FOUND); |
| 155 | } |
| 156 | |
| 157 | $payload = EditPageDTO::from_array($request->sanitized()); |
| 158 | |
| 159 | $page = $this->service->update($page, $payload); |
| 160 | |
| 161 | return response()->json([ |
| 162 | 'data' => new PageResource($page), |
| 163 | 'message' => __('Page updated successfully.', 'kirki'), |
| 164 | ], Response::OK); |
| 165 | } |
| 166 | |
| 167 | public function update_popup(PopupRequest $request, int $popup_id) |
| 168 | { |
| 169 | $popup = PageModel::exclude_trash()->find($popup_id); |
| 170 | |
| 171 | if (empty($popup) || $popup->post_type !== PostTypes::POPUP) { |
| 172 | throw new Exception(esc_html__('Popup not found.', 'kirki'), Response::NOT_FOUND); |
| 173 | } |
| 174 | |
| 175 | $payload = EditPopupDTO::from_array($request->sanitized()); |
| 176 | |
| 177 | $popup = $this->service->update_popup_data($popup, $payload); |
| 178 | |
| 179 | return response()->json([ |
| 180 | 'data' => new PageResource($popup), |
| 181 | 'message' => __('Popup updated successfully.', 'kirki'), |
| 182 | ], Response::OK); |
| 183 | } |
| 184 | |
| 185 | public function toggle_disabled_page_symbols(TogglePageSymbolRequest $request) |
| 186 | { |
| 187 | $page = PageModel::exclude_trash()->find($request->int('post_id')); |
| 188 | |
| 189 | if (empty($page)) { |
| 190 | throw new Exception(esc_html__('Page not found.', 'kirki'), Response::NOT_FOUND); |
| 191 | } |
| 192 | |
| 193 | $payload = TogglePageSymbolDTO::from_array($request->sanitized()); |
| 194 | |
| 195 | return response()->json([ |
| 196 | 'data' => [ |
| 197 | 'status' => __('Page data saved.', 'kirki'), |
| 198 | 'success' => $this->service->toggle_disabled_page_symbols($payload) |
| 199 | ], |
| 200 | 'message' => __('Symbol data updated.', 'kirki'), |
| 201 | ], Response::OK); |
| 202 | } |
| 203 | |
| 204 | public function duplicate(Request $request) |
| 205 | { |
| 206 | $page = PageModel::exclude_trash()->find($request->int('page_id')); |
| 207 | |
| 208 | if (empty($page)) { |
| 209 | throw new Exception(esc_html__('Page not found.', 'kirki'), Response::NOT_FOUND); |
| 210 | } |
| 211 | |
| 212 | return response()->json([ |
| 213 | 'data' => new PageResource($this->service->duplicate_page($page)), |
| 214 | 'message' => __('Unused style blocks removed.', 'kirki'), |
| 215 | ], Response::OK); |
| 216 | } |
| 217 | |
| 218 | public function delete(Request $request) |
| 219 | { |
| 220 | $page = PageModel::exclude_trash()->find($request->int('page_id')); |
| 221 | |
| 222 | if (empty($page)) { |
| 223 | throw new Exception(esc_html__('Page not found.', 'kirki'), Response::NOT_FOUND); |
| 224 | } |
| 225 | |
| 226 | $this->service->delete_page($page); |
| 227 | |
| 228 | return response()->json([ |
| 229 | 'data' => [ |
| 230 | 'status' => __('Page deleted.', 'kirki'), |
| 231 | ], |
| 232 | 'message' => __('Page deleted.', 'kirki'), |
| 233 | ]); |
| 234 | } |
| 235 | |
| 236 | public function paginated(Request $request) |
| 237 | { |
| 238 | $filter_dto = PageFilterDTO::from_array([ |
| 239 | 'query' => $request->string('query'), |
| 240 | 'current_page' => $request->int('page', 1), |
| 241 | 'limit' => $request->int('numberposts', 20), |
| 242 | 'post_types' => $request->array('post_types', []), |
| 243 | 'exclude_page_ids' => $request->array('exclude_post_ids', []), |
| 244 | ]); |
| 245 | |
| 246 | return response()->json([ |
| 247 | 'data' => PageResource::paginated($this->service->paginated($filter_dto)), |
| 248 | ]); |
| 249 | } |
| 250 | |
| 251 | public function page_panel_pages(Request $request) |
| 252 | { |
| 253 | $filter_dto = PageFilterDTO::from_array([ |
| 254 | 'query' => $request->string('query'), |
| 255 | 'current_page' => $request->int('page', 1), |
| 256 | 'limit' => $request->int('numberposts', 20), |
| 257 | 'post_types' => $request->array('post_types', [PostTypes::WP_PAGE]), |
| 258 | ]); |
| 259 | |
| 260 | return response()->json([ |
| 261 | 'data' => PageResource::paginated($this->service->paginated($filter_dto)), |
| 262 | ]); |
| 263 | } |
| 264 | |
| 265 | public function get_wp_single_post(Request $request) |
| 266 | { |
| 267 | $post = PostModel::exclude_trash()->find($request->int('post_id')); |
| 268 | |
| 269 | if (empty($post)) { |
| 270 | throw new Exception(esc_html__('Post not found.', 'kirki'), Response::NOT_FOUND); |
| 271 | } |
| 272 | |
| 273 | return response()->json([ |
| 274 | 'data' => $post, |
| 275 | ]); |
| 276 | } |
| 277 | |
| 278 | public function get_current_page(Request $request) |
| 279 | { |
| 280 | $page = PageModel::exclude_trash()->find($request->int('page_id')); |
| 281 | |
| 282 | if (empty($page)) { |
| 283 | throw new Exception(esc_html__('Page not found.', 'kirki'), Response::NOT_FOUND); |
| 284 | } |
| 285 | |
| 286 | return response()->json([ |
| 287 | 'data' => PageResource::make($page), |
| 288 | ]); |
| 289 | } |
| 290 | |
| 291 | public function get_data_list_for_template_edit_search_flyout(CollectionItemConditionRequest $request) |
| 292 | { |
| 293 | $query = $request->string('query', ''); |
| 294 | $conditions = $request->array('conditions', []); |
| 295 | |
| 296 | return response()->json([ |
| 297 | 'data' => CollectionItem::get_items_from_condition($conditions, $query)['data'] ?? [], |
| 298 | ]); |
| 299 | } |
| 300 | |
| 301 | public function get_page_content(Request $request) |
| 302 | { |
| 303 | $page = PageModel::find($request->int('page_id')); |
| 304 | |
| 305 | if (empty($page)) { |
| 306 | throw new Exception(esc_html__('Page not found.', 'kirki'), Response::NOT_FOUND); |
| 307 | } |
| 308 | |
| 309 | $stage_version = $request->int('stage_version') ?? false; |
| 310 | |
| 311 | return response()->json([ |
| 312 | 'data' => new PageContentResource($page, $stage_version), |
| 313 | ]); |
| 314 | } |
| 315 | } |
| 316 |