PluginProbe
Depicter — Popup & Slider Builder / 1.6.2
Depicter — Popup & Slider Builder v1.6.2
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Controllers / Ajax / EditorAjaxController.php

EditorAjaxController.php in Depicter — Popup & Slider Builder 1.6.2, at app/src/Controllers/Ajax/EditorAjaxController.php

474 lines 13.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Controllers\Ajax;
3
4
5 use Averta\Core\Controllers\RestMethodsInterface;
6 use Averta\Core\Utility\Arr;
7 use Averta\Core\Utility\Data;
8 use Averta\WordPress\Utility\JSON;
9 use Depicter;
10 use Depicter\Editor\EditorLocalization;
11 use Depicter\GuzzleHttp\Exception\ConnectException;
12 use Depicter\GuzzleHttp\Exception\GuzzleException;
13 use Depicter\Utility\Sanitize;
14 use Depicter\Utility\Http;
15 use Exception;
16 use GuzzleHttp\Psr7\UploadedFile;
17 use Psr\Http\Message\ResponseInterface;
18 use WPEmerge\Requests\RequestInterface;
19 use Psr\Http\Message\UploadedFileInterface;
20
21 //use Depicter\Editor\EditorData;
22
23 class EditorAjaxController implements RestMethodsInterface
24 {
25
26 /**
27 * Retrieves Lists of all entries. (GET)
28 *
29 * @param RequestInterface $request
30 * @param string $view
31 *
32 * @return string
33 */
34 public function index( RequestInterface $request, $view ){
35 return "";
36 }
37
38 /**
39 * Adds a new entry. (POST)
40 *
41 * @param RequestInterface $request
42 * @param string $view
43 *
44 * @return ResponseInterface
45 * @throws Exception
46 * @throws GuzzleException
47 */
48 public function store( RequestInterface $request, $view ){
49 // fields to update
50 $properties = [];
51
52 // Get editor data
53 $editor = $request->body( 'editor' );
54 if( ! empty( $editor ) && JSON::isJson( $editor ) ){
55 $properties['editor'] = $editor;
56 }
57
58 // Get document ID
59 $id = Sanitize::int( $request->body( 'ID' ) );
60
61 if( empty( $id ) ){
62 return Depicter::json( [ 'errors' => [ 'Document "ID" is required.' ], ] )->withStatus( 400 );
63 }
64
65 // Get document name
66 $name = Sanitize::textfield( $request->body( 'name' ) );
67
68 if( ! empty( $name ) ){
69 $properties['name'] = $name;
70 }
71
72 // Get document slug
73 $slug = Sanitize::slug( $request->body( 'slug' ) );
74
75 if( ! empty( $slug ) ){
76 $properties['slug'] = $slug;
77 }
78
79 // Get document status
80 $status = Sanitize::textfield( $request->body( 'status' ) ) ?? 'draft';
81 $properties['status'] = $status === 'published' ? 'publish' : $status;
82
83
84 try{
85 if( $properties['status'] == 'publish' ){
86
87 if ( \Depicter::auth()->isPaid() && ! \Depicter::auth()->verifyActivation() ) {
88 throw new Exception( esc_html__( 'License is not valid.', 'depicter' ) );
89 }
90
91 if( function_exists( 'get_filesystem_method' ) && get_filesystem_method() != 'direct' ){
92 throw new Exception( esc_html__( 'Media files cannot be published due to lack of proper file permissions for uploads directory.', 'depicter' ) );
93 }
94
95 $editorRawData = $properties['editor'] ?? Depicter::document()->getEditorRawData( $id );
96
97 // Download media if document published
98 \Depicter::media()->importDocumentAssets( $editorRawData );
99 }
100
101 $result = Depicter::documentRepository()->saveEditorData( $id, $properties );
102
103 if( false === $result ){
104 return Depicter::json( [ 'errors' => [ 'Document does not exist.', $result ] ] )->withStatus( 404 );
105 }
106
107 // \Depicter::action()->do( 'depicter/editor/after/store', $id, $properties, $result );
108 do_action( 'depicter/editor/after/store', $id, $properties, $result );
109
110 $this->setDocumentPoster( $request, $id );
111
112 return Depicter::json( [ 'hits' => [ 'status' => $status,
113 'modifiedAt' => $result['modifiedAt'],
114 'publishedAt' => $result['publishedAt'] ] ] )->withStatus( 200 );
115 } catch( Exception $exception ){
116 $error = Http::getErrorExceptionResponse( $exception );
117
118 return \Depicter::json([
119 'errors' => $error
120 ])->withStatus( $error['statusCode'] );
121 }
122
123 }
124
125
126 /**
127 * Adds a new entry. (POST)
128 *
129 * @param RequestInterface $request
130 * @param string $view
131 *
132 * @return ResponseInterface
133 * @throws Exception
134 */
135 public function create( RequestInterface $request, $view ){
136 $document = Depicter::documentRepository()->create();
137
138 return Depicter::json( [ 'hits' => $document ] )
139 ->withHeader( 'X-Message', 'New document created successfully' )
140 ->withHeader( 'X-Document-ID', $document->getID() )
141 ->withStatus( 200 );
142 }
143
144 /**
145 * Displays an entry. (GET)
146 *
147 * @param RequestInterface $request
148 * @param string $view
149 *
150 * @return ResponseInterface
151 */
152 public function show( RequestInterface $request, $view ){
153 if( ! $documentId = Sanitize::int( $request->query( 'ID' ) ) ){
154 return Depicter::json( [ 'errors' => [ 'Document ID is required.' ], ] )->withStatus( 400 );
155 }
156
157 if( ! $document = Depicter::documentRepository()->findById( $documentId ) ){
158 return Depicter::json( [ 'errors' => [ 'Document ID not found.', $documentId ] ] )
159 ->withHeader( 'X-Document-ID', $documentId )
160 ->withStatus( 404 );
161 }
162
163 return Depicter::json( [ 'hits' => Arr::camelizeKeys( $document->getApiProperties(), '_' ) ] )
164 ->withHeader( 'X-Document-ID', $documentId )
165 ->withStatus( 200 );
166 }
167
168
169 /**
170 * Removes an entry. (DELETE)
171 *
172 * @param RequestInterface $request
173 * @param string $view
174 *
175 * @return ResponseInterface
176 * @throws Exception
177 */
178 public function destroy( RequestInterface $request, $view ){
179 // Get document ID
180 $id = Sanitize::textfield( $request->body( 'ID' ) );
181
182 if( empty( $id ) ){
183 return Depicter::json( [ 'errors' => [ 'Document ID is required.' ] ] )->withStatus( 400 );
184 }
185
186 $ids = explode( ',', $id );
187 $result = [];
188
189 foreach( $ids as $_id ){
190 $_id = Sanitize::int( $_id );
191 $isDeleted = Depicter::documentRepository()->delete( $_id );
192
193 if( $isDeleted ){
194 $result['hits'][] = $_id;
195 // \Depicter::action()->do( 'depicter/editor/after/delete', $id, $properties, $result );
196 do_action( 'depicter/editor/after/delete', $_id );
197 } else {
198 $result['errors'][] = sprintf( "Document '%s' does not exist.", $_id );
199 }
200 }
201
202 return Depicter::json( $result )->withStatus( 200 );
203 }
204
205 /**
206 * Renames a document name.
207 *
208 * @param RequestInterface $request
209 * @param string $view
210 *
211 * @return ResponseInterface
212 * @throws Exception
213 */
214 public function checkSlug( RequestInterface $request, $view ){
215 // Get document ID
216 $slug = Sanitize::textfield( $request->body( 'slug' ) );
217
218 if( is_null( $slug ) ){
219 return Depicter::json( [ 'errors' => [ 'Document slug is required.' ], ] )->withStatus( 400 );
220 }
221
222 $result = Depicter::documentRepository()->checkSlug( $slug );
223
224 if( $result ){
225 return Depicter::json( [ "errors" => [ "Taken!" ] ] )
226 ->withHeader( 'X-Taken-Slug', $slug )
227 ->withHeader( 'X-Available-Slug', Depicter::documentRepository()->makeSlug() )
228 ->withStatus( 423 );
229 }
230
231 return Depicter::json( [ 'hits' => $slug ] )->withStatus( 200 );
232 }
233
234 /**
235 * Retrieves editor and dashboard localized texts
236 *
237 * @param RequestInterface $request
238 * @param $view
239 *
240 * @return ResponseInterface
241 */
242 public function getLocalization( RequestInterface $request, $view ){
243 return Depicter::json(
244 EditorLocalization::getTranslateList()
245 )->withHeader('Access-Control-Allow-Origin' , '*')->withStatus( 200 );
246 }
247
248 /**
249 * Renders a document markup.
250 *
251 * @param RequestInterface $request
252 * @param string $view
253 *
254 * @return ResponseInterface
255 * @throws Exception
256 */
257 public function render( RequestInterface $request, $view ){
258 if( ! $documentId = Sanitize::int( $request->query( 'ID' ) ) ){
259 return Depicter::json( [ 'errors' => [ 'Document ID is required.' ], ] )->withStatus( 400 );
260 }
261
262 $args = [];
263 if ( Data::isBool( $request->query( 'addImportant' ) ) ) {
264 $args['addImportant'] = Sanitize::textfield( $request->query( 'addImportant' ) );
265 }
266
267 return Depicter::output( Depicter::front()->render()->document( $documentId, $args ) );
268 }
269
270 /**
271 * Outputs markup to preview a document
272 *
273 * @param RequestInterface $request
274 * @param $view
275 *
276 * @return ResponseInterface
277 */
278 public function preview( RequestInterface $request, $view ){
279 $gutenberg = $request->query( 'gutenberg', '' );
280 $documentId = Sanitize::int( $request->query( 'ID' ) );
281 if( ! $documentId && empty( $gutenberg ) ){
282 return Depicter::json( [ 'errors' => [ 'Document ID is required.' ], ] )->withStatus( 400 );
283 }
284
285 $status = Sanitize::textfield( $request->query( 'status' ) );
286
287 $previewArgs = [ 'status' => ! empty( $status ) ? $status : 'auto',
288 'start' => Sanitize::int( $request->query( 'startSection' ) ),
289 'gutenberg' => !empty( $gutenberg ) ];
290
291 return Depicter::output( Depicter::front()->preview()->document( $documentId, $previewArgs ) );
292 }
293
294 /**
295 * Retrieves object of document editor data
296 *
297 * @param RequestInterface $request
298 * @param string $view
299 *
300 * @return ResponseInterface
301 */
302 public function getEditorData( RequestInterface $request, $view ){
303 if( ! $documentId = Sanitize::int( $request->query( 'ID' ) ) ){
304 return Depicter::json( [ 'errors' => [ 'Document ID is required.' ], ] )->withStatus( 400 );
305 }
306
307 $output = '';
308
309 try{
310 $output = Depicter::document()->getEditorData( $documentId );
311 } catch( Exception $exception ){
312 return Depicter::json( [ 'errors' => [ $exception->getMessage() ] ] )
313 ->withHeader( 'X-Document-ID', $documentId )
314 ->withStatus( 404 );
315 }
316
317 return Depicter::json( $output );
318 }
319
320
321 /**
322 * Reverts a document to previous snapshots
323 *
324 * @param RequestInterface $request
325 * @param string $view
326 *
327 * @return ResponseInterface
328 */
329 public function revert( RequestInterface $request, $view ){
330 if( ! $documentId = Sanitize::int( $request->body( 'ID' ) ) ){
331 return Depicter::json( [ 'errors' => [ 'Document ID is required.' ], ] )->withStatus( 400 );
332 }
333 $to = Sanitize::textfield( $request->body( 'to' ) );
334
335 try{
336 $output = Depicter::document()->repository()->revert( $documentId, $to );
337 return Depicter::json( $output );
338
339 } catch( Exception $exception ){
340 return Depicter::json( [ 'errors' => [ $exception->getMessage() ] ] )
341 ->withHeader( 'X-Document-ID', $documentId )
342 ->withStatus( 404 );
343 }
344 }
345
346 /**
347 * Updates an entry. (PUT/PATCH)
348 *
349 * @param RequestInterface $request
350 * @param string $view
351 *
352 * @return ResponseInterface
353 */
354 public function update( RequestInterface $request, $view ){
355 return Depicter::json( [ 'hits' => [] ] )->withStatus( 200 );
356 }
357
358 /**
359 * Upload cover photo of slider
360 *
361 * @param RequestInterface $request
362 * @param int $id
363 *
364 * @return bool
365 */
366 public function setDocumentPoster( RequestInterface $request, int $id ){
367 $uploadedFiles = $request->getUploadedFiles();
368
369 if( empty( $uploadedFiles['previewImage'] ) || empty( $id ) ){
370 return false;
371 }
372 $previewImages = $uploadedFiles['previewImage'];
373
374 if ( ! is_array( $previewImages ) ) {
375 /* @var $previewImages UploadedFileInterface */
376 if( $previewImages->getError() || ! $previewImages->getSize() ){
377 return false;
378 }
379 return $this->uploadPreviewImage( $previewImages, $id );
380
381 } else {
382 /* @var $previewImages UploadedFileInterface[] */
383 foreach( $previewImages as $key => $image ) {
384 if( empty( $image ) || $image->getError() || ! $image->getSize() ){
385 continue;
386 }
387 if( $key === 0 ){
388 $this->uploadPreviewImage( $image, $id );
389 $this->uploadPreviewImage( $image, $id . '-1' );
390 } else {
391 $fileName = $id . '-' . ($key + 1);
392 $this->uploadPreviewImage( $image, $fileName );
393 }
394 }
395
396 return true;
397 }
398 }
399
400 /**
401 * Upload preview image to depicter upload folder
402 *
403 * @param UploadedFileInterface $imageFile
404 * @param string $fileName
405 *
406 * @return bool
407 */
408 public function uploadPreviewImage( UploadedFileInterface $imageFile, string $fileName ): bool{
409 $mediaType = $imageFile->getClientMediaType();
410
411 if( false === strpos( $mediaType, 'image/' ) ){
412 // Not an image file
413 return false;
414 }
415
416 return Depicter::storage()->filesystem()->write( Depicter::storage()->getPluginUploadsDirectory() . '/preview-images/' . $fileName . '.png', (string) $imageFile->getStream() );
417 }
418
419 /**
420 * Upload multiple document posters
421 *
422 * @param RequestInterface $request
423 * @param $view
424 *
425 * @return ResponseInterface
426 */
427 public function uploadDocumentPosters( RequestInterface $request, $view ) {
428
429 $documentId = Sanitize::textfield( $request->body('ID', '' ) );
430
431 try {
432 $coverPhoto = $request->getUploadedFiles();
433
434 if( empty( $documentId ) ){
435 throw new \Exception(__( 'Document ID is required.', 'depicter' ), 400 );
436 }
437 if( empty( $coverPhoto ) ){
438 throw new \Exception(__( 'Cover image not found.', 'depicter' ), 400 );
439 }
440 if ( ! $this->setDocumentPoster( $request, $documentId ) ) {
441 throw new \Exception(__( 'Error occurred! Cannot upload cover photos.', 'depicter' ), 400 );
442 }
443
444 } catch ( \Exception $exception ) {
445 return Depicter::json([
446 'errors' => [ $exception->getMessage() ]
447 ])->withStatus( $exception->getCode() );
448 };
449
450 return Depicter::json([ 'success' => true, "message" => "Cover images uploaded successfully." ])->withStatus( 200 );
451 }
452
453 /**
454 * Get Document Status
455 *
456 * @param RequestInterface $request
457 * @param string $view
458 *
459 * @return ResponseInterface
460 */
461 public function getDocumentStatus( RequestInterface $request, $view ) {
462 $documentId = absint( $request->query( 'ID', 0 ) );
463 try {
464 return Depicter::json([
465 'status' => Depicter::documentRepository()->getStatus( $documentId )
466 ])->withStatus(200);
467 } catch ( \Exception $exception ) {
468 return Depicter::json([
469 'errors' => [ $exception->getMessage() ]
470 ])->withStatus( 400 );
471 };
472 }
473 }
474