← All changes
|
app/src/Controllers/Ajax/EditorAjaxController.php
+114
-16
1.3.0
→
1.9.2
View file →
| @@ -10,11 +10,14 @@ | ||
| 10 | 10 | use Depicter\Editor\EditorLocalization; |
| 11 | 11 | use Depicter\GuzzleHttp\Exception\ConnectException; |
| 12 | 12 | use Depicter\GuzzleHttp\Exception\GuzzleException; |
| 13 | 13 | use Depicter\Utility\Sanitize; |
| 14 | +use Depicter\Utility\Http; | |
| 14 | 15 | use Exception; |
| 16 | +use GuzzleHttp\Psr7\UploadedFile; | |
| 15 | 17 | use Psr\Http\Message\ResponseInterface; |
| 16 | 18 | use WPEmerge\Requests\RequestInterface; |
| 19 | +use Psr\Http\Message\UploadedFileInterface; | |
| 17 | 20 | |
| 18 | 21 | //use Depicter\Editor\EditorData; |
| 19 | 22 | |
| 20 | 23 | class EditorAjaxController implements RestMethodsInterface |
| @@ -79,8 +82,13 @@ | ||
| 79 | 82 | |
| 80 | 83 | |
| 81 | 84 | try{ |
| 82 | 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 | + | |
| 83 | 91 | if( function_exists( 'get_filesystem_method' ) && get_filesystem_method() != 'direct' ){ |
| 84 | 92 | throw new Exception( esc_html__( 'Media files cannot be published due to lack of proper file permissions for uploads directory.', 'depicter' ) ); |
| 85 | 93 | } |
| 86 | 94 | |
| @@ -103,17 +111,14 @@ | ||
| 103 | 111 | |
| 104 | 112 | return Depicter::json( [ 'hits' => [ 'status' => $status, |
| 105 | 113 | 'modifiedAt' => $result['modifiedAt'], |
| 106 | 114 | 'publishedAt' => $result['publishedAt'] ] ] )->withStatus( 200 ); |
| 107 | - } catch( ConnectException $exception ){ | |
| 108 | - return Depicter::json([ | |
| 109 | - 'errors' => [ | |
| 110 | - 'Cannot reach the server for downloading images.', | |
| 111 | - $exception->getMessage() | |
| 112 | - ] | |
| 113 | - ])->withStatus(421); | |
| 114 | 115 | } catch( Exception $exception ){ |
| 115 | - return Depicter::json([ 'errors' => [ $exception->getMessage() ] ]); | |
| 116 | + $error = Http::getErrorExceptionResponse( $exception ); | |
| 117 | + | |
| 118 | + return \Depicter::json([ | |
| 119 | + 'errors' => $error | |
| 120 | + ])->withStatus( $error['statusCode'] ); | |
| 116 | 121 | } |
| 117 | 122 | |
| 118 | 123 | } |
| 119 | 124 | |
| @@ -270,9 +275,11 @@ | ||
| 270 | 275 | * |
| 271 | 276 | * @return ResponseInterface |
| 272 | 277 | */ |
| 273 | 278 | public function preview( RequestInterface $request, $view ){ |
| 274 | - if( ! $documentId = Sanitize::int( $request->query( 'ID' ) ) ){ | |
| 279 | + $gutenberg = $request->query( 'gutenberg', '' ); | |
| 280 | + $documentId = Sanitize::int( $request->query( 'ID' ) ); | |
| 281 | + if( ! $documentId && empty( $gutenberg ) ){ | |
| 275 | 282 | return Depicter::json( [ 'errors' => [ 'Document ID is required.' ], ] )->withStatus( 400 ); |
| 276 | 283 | } |
| 277 | 284 | |
| 278 | 285 | $status = Sanitize::textfield( $request->query( 'status' ) ); |
| @@ -277,9 +284,10 @@ | ||
| 277 | 284 | |
| 278 | 285 | $status = Sanitize::textfield( $request->query( 'status' ) ); |
| 279 | 286 | |
| 280 | 287 | $previewArgs = [ 'status' => ! empty( $status ) ? $status : 'auto', |
| 281 | - 'start' => Sanitize::int( $request->query( 'startSection' ) ) ]; | |
| 288 | + 'start' => Sanitize::int( $request->query( 'startSection' ) ), | |
| 289 | + 'gutenberg' => !empty( $gutenberg ) ]; | |
| 282 | 290 | |
| 283 | 291 | return Depicter::output( Depicter::front()->preview()->document( $documentId, $previewArgs ) ); |
| 284 | 292 | } |
| 285 | 293 | |
| @@ -354,22 +362,112 @@ | ||
| 354 | 362 | * @param int $id |
| 355 | 363 | * |
| 356 | 364 | * @return bool |
| 357 | 365 | */ |
| 358 | - public function setDocumentPoster( RequestInterface $request, $id ){ | |
| 359 | - $coverPhoto = $request->files( 'previewImage' ); | |
| 360 | - if( empty( $coverPhoto ) || ! $coverPhoto->getSize() ){ | |
| 366 | + public function setDocumentPoster( RequestInterface $request, int $id ){ | |
| 367 | + $uploadedFiles = $request->getUploadedFiles(); | |
| 368 | + | |
| 369 | + if( empty( $uploadedFiles['previewImage'] ) || empty( $id ) ){ | |
| 361 | 370 | return false; |
| 362 | 371 | } |
| 372 | + $previewImages = $uploadedFiles['previewImage']; | |
| 363 | 373 | |
| 364 | - $mediaType = $coverPhoto->getClientMediaType(); | |
| 365 | - $extension = str_replace( 'image/', '', $mediaType ); | |
| 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 ); | |
| 366 | 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 | + | |
| 367 | 411 | if( false === strpos( $mediaType, 'image/' ) ){ |
| 368 | 412 | // Not an image file |
| 369 | 413 | return false; |
| 370 | 414 | } |
| 371 | 415 | |
| 372 | - return Depicter::storage()->filesystem()->write( Depicter::storage()->getPluginUploadsDirectory() . '/preview-images/' . $id . '.' . $extension, (string) $coverPhoto->getStream() ); | |
| 416 | + return Depicter::storage()->filesystem()->write( Depicter::storage()->getPluginUploadsDirectory() . '/preview-images/' . $fileName . '.png', (string) $imageFile->getStream() ); | |
| 373 | 417 | } |
| 374 | 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 | + } | |
| 375 | 473 | } |