| 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 Exception; |
| 15 |
use Psr\Http\Message\ResponseInterface; |
| 16 |
use WPEmerge\Requests\RequestInterface; |
| 17 |
|
| 18 |
//use Depicter\Editor\EditorData; |
| 19 |
|
| 20 |
class EditorAjaxController implements RestMethodsInterface |
| 21 |
{ |
| 22 |
|
| 23 |
/** |
| 24 |
* Retrieves Lists of all entries. (GET) |
| 25 |
* |
| 26 |
* @param RequestInterface $request |
| 27 |
* @param string $view |
| 28 |
* |
| 29 |
* @return string |
| 30 |
*/ |
| 31 |
public function index( RequestInterface $request, $view ){ |
| 32 |
return ""; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Adds a new entry. (POST) |
| 37 |
* |
| 38 |
* @param RequestInterface $request |
| 39 |
* @param string $view |
| 40 |
* |
| 41 |
* @return ResponseInterface |
| 42 |
* @throws Exception |
| 43 |
* @throws GuzzleException |
| 44 |
*/ |
| 45 |
public function store( RequestInterface $request, $view ){ |
| 46 |
// fields to update |
| 47 |
$properties = []; |
| 48 |
|
| 49 |
// Get editor data |
| 50 |
$editor = $request->body( 'editor' ); |
| 51 |
if( ! empty( $editor ) && JSON::isJson( $editor ) ){ |
| 52 |
$properties['editor'] = $editor; |
| 53 |
} |
| 54 |
|
| 55 |
// Get document ID |
| 56 |
$id = Sanitize::int( $request->body( 'ID' ) ); |
| 57 |
|
| 58 |
if( empty( $id ) ){ |
| 59 |
return Depicter::json( [ 'errors' => [ 'Document "ID" is required.' ], ] )->withStatus( 400 ); |
| 60 |
} |
| 61 |
|
| 62 |
// Get document name |
| 63 |
$name = Sanitize::textfield( $request->body( 'name' ) ); |
| 64 |
|
| 65 |
if( ! empty( $name ) ){ |
| 66 |
$properties['name'] = $name; |
| 67 |
} |
| 68 |
|
| 69 |
// Get document slug |
| 70 |
$slug = Sanitize::slug( $request->body( 'slug' ) ); |
| 71 |
|
| 72 |
if( ! empty( $slug ) ){ |
| 73 |
$properties['slug'] = $slug; |
| 74 |
} |
| 75 |
|
| 76 |
// Get document status |
| 77 |
$status = Sanitize::textfield( $request->body( 'status' ) ) ?? 'draft'; |
| 78 |
$properties['status'] = $status === 'published' ? 'publish' : $status; |
| 79 |
|
| 80 |
|
| 81 |
try{ |
| 82 |
if( $properties['status'] == 'publish' ){ |
| 83 |
if( function_exists( 'get_filesystem_method' ) && get_filesystem_method() != 'direct' ){ |
| 84 |
throw new Exception( esc_html__( 'Media files cannot be published due to lack of proper file permissions for uploads directory.', 'depicter' ) ); |
| 85 |
} |
| 86 |
|
| 87 |
$editorRawData = $properties['editor'] ?? Depicter::document()->getEditorRawData( $id ); |
| 88 |
|
| 89 |
// Download media if document published |
| 90 |
\Depicter::media()->importDocumentAssets( $editorRawData ); |
| 91 |
} |
| 92 |
|
| 93 |
$result = Depicter::documentRepository()->saveEditorData( $id, $properties ); |
| 94 |
|
| 95 |
if( false === $result ){ |
| 96 |
return Depicter::json( [ 'errors' => [ 'Document does not exist.', $result ] ] )->withStatus( 404 ); |
| 97 |
} |
| 98 |
|
| 99 |
// \Depicter::action()->do( 'depicter/editor/after/store', $id, $properties, $result ); |
| 100 |
do_action( 'depicter/editor/after/store', $id, $properties, $result ); |
| 101 |
|
| 102 |
$this->setDocumentPoster( $request, $id ); |
| 103 |
|
| 104 |
return Depicter::json( [ 'hits' => [ 'status' => $status, |
| 105 |
'modifiedAt' => $result['modifiedAt'], |
| 106 |
'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 |
} catch( Exception $exception ){ |
| 115 |
return Depicter::json([ 'errors' => [ $exception->getMessage() ] ]); |
| 116 |
} |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
/** |
| 122 |
* Adds a new entry. (POST) |
| 123 |
* |
| 124 |
* @param RequestInterface $request |
| 125 |
* @param string $view |
| 126 |
* |
| 127 |
* @return ResponseInterface |
| 128 |
* @throws Exception |
| 129 |
*/ |
| 130 |
public function create( RequestInterface $request, $view ){ |
| 131 |
$document = Depicter::documentRepository()->create(); |
| 132 |
|
| 133 |
return Depicter::json( [ 'hits' => $document ] ) |
| 134 |
->withHeader( 'X-Message', 'New document created successfully' ) |
| 135 |
->withHeader( 'X-Document-ID', $document->getID() ) |
| 136 |
->withStatus( 200 ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Displays an entry. (GET) |
| 141 |
* |
| 142 |
* @param RequestInterface $request |
| 143 |
* @param string $view |
| 144 |
* |
| 145 |
* @return ResponseInterface |
| 146 |
*/ |
| 147 |
public function show( RequestInterface $request, $view ){ |
| 148 |
if( ! $documentId = Sanitize::int( $request->query( 'ID' ) ) ){ |
| 149 |
return Depicter::json( [ 'errors' => [ 'Document ID is required.' ], ] )->withStatus( 400 ); |
| 150 |
} |
| 151 |
|
| 152 |
if( ! $document = Depicter::documentRepository()->findById( $documentId ) ){ |
| 153 |
return Depicter::json( [ 'errors' => [ 'Document ID not found.', $documentId ] ] ) |
| 154 |
->withHeader( 'X-Document-ID', $documentId ) |
| 155 |
->withStatus( 404 ); |
| 156 |
} |
| 157 |
|
| 158 |
return Depicter::json( [ 'hits' => Arr::camelizeKeys( $document->getApiProperties(), '_' ) ] ) |
| 159 |
->withHeader( 'X-Document-ID', $documentId ) |
| 160 |
->withStatus( 200 ); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
/** |
| 165 |
* Removes an entry. (DELETE) |
| 166 |
* |
| 167 |
* @param RequestInterface $request |
| 168 |
* @param string $view |
| 169 |
* |
| 170 |
* @return ResponseInterface |
| 171 |
* @throws Exception |
| 172 |
*/ |
| 173 |
public function destroy( RequestInterface $request, $view ){ |
| 174 |
// Get document ID |
| 175 |
$id = Sanitize::textfield( $request->body( 'ID' ) ); |
| 176 |
|
| 177 |
if( empty( $id ) ){ |
| 178 |
return Depicter::json( [ 'errors' => [ 'Document ID is required.' ] ] )->withStatus( 400 ); |
| 179 |
} |
| 180 |
|
| 181 |
$ids = explode( ',', $id ); |
| 182 |
$result = []; |
| 183 |
|
| 184 |
foreach( $ids as $_id ){ |
| 185 |
$_id = Sanitize::int( $_id ); |
| 186 |
$isDeleted = Depicter::documentRepository()->delete( $_id ); |
| 187 |
|
| 188 |
if( $isDeleted ){ |
| 189 |
$result['hits'][] = $_id; |
| 190 |
// \Depicter::action()->do( 'depicter/editor/after/delete', $id, $properties, $result ); |
| 191 |
do_action( 'depicter/editor/after/delete', $_id ); |
| 192 |
} else { |
| 193 |
$result['errors'][] = sprintf( "Document '%s' does not exist.", $_id ); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
return Depicter::json( $result )->withStatus( 200 ); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Renames a document name. |
| 202 |
* |
| 203 |
* @param RequestInterface $request |
| 204 |
* @param string $view |
| 205 |
* |
| 206 |
* @return ResponseInterface |
| 207 |
* @throws Exception |
| 208 |
*/ |
| 209 |
public function checkSlug( RequestInterface $request, $view ){ |
| 210 |
// Get document ID |
| 211 |
$slug = Sanitize::textfield( $request->body( 'slug' ) ); |
| 212 |
|
| 213 |
if( is_null( $slug ) ){ |
| 214 |
return Depicter::json( [ 'errors' => [ 'Document slug is required.' ], ] )->withStatus( 400 ); |
| 215 |
} |
| 216 |
|
| 217 |
$result = Depicter::documentRepository()->checkSlug( $slug ); |
| 218 |
|
| 219 |
if( $result ){ |
| 220 |
return Depicter::json( [ "errors" => [ "Taken!" ] ] ) |
| 221 |
->withHeader( 'X-Taken-Slug', $slug ) |
| 222 |
->withHeader( 'X-Available-Slug', Depicter::documentRepository()->makeSlug() ) |
| 223 |
->withStatus( 423 ); |
| 224 |
} |
| 225 |
|
| 226 |
return Depicter::json( [ 'hits' => $slug ] )->withStatus( 200 ); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Retrieves editor and dashboard localized texts |
| 231 |
* |
| 232 |
* @param RequestInterface $request |
| 233 |
* @param $view |
| 234 |
* |
| 235 |
* @return ResponseInterface |
| 236 |
*/ |
| 237 |
public function getLocalization( RequestInterface $request, $view ){ |
| 238 |
return Depicter::json( |
| 239 |
EditorLocalization::getTranslateList() |
| 240 |
)->withHeader('Access-Control-Allow-Origin' , '*')->withStatus( 200 ); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Renders a document markup. |
| 245 |
* |
| 246 |
* @param RequestInterface $request |
| 247 |
* @param string $view |
| 248 |
* |
| 249 |
* @return ResponseInterface |
| 250 |
* @throws Exception |
| 251 |
*/ |
| 252 |
public function render( RequestInterface $request, $view ){ |
| 253 |
if( ! $documentId = Sanitize::int( $request->query( 'ID' ) ) ){ |
| 254 |
return Depicter::json( [ 'errors' => [ 'Document ID is required.' ], ] )->withStatus( 400 ); |
| 255 |
} |
| 256 |
|
| 257 |
$args = []; |
| 258 |
if ( Data::isBool( $request->query( 'addImportant' ) ) ) { |
| 259 |
$args['addImportant'] = Sanitize::textfield( $request->query( 'addImportant' ) ); |
| 260 |
} |
| 261 |
|
| 262 |
return Depicter::output( Depicter::front()->render()->document( $documentId, $args ) ); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Outputs markup to preview a document |
| 267 |
* |
| 268 |
* @param RequestInterface $request |
| 269 |
* @param $view |
| 270 |
* |
| 271 |
* @return ResponseInterface |
| 272 |
*/ |
| 273 |
public function preview( RequestInterface $request, $view ){ |
| 274 |
if( ! $documentId = Sanitize::int( $request->query( 'ID' ) ) ){ |
| 275 |
return Depicter::json( [ 'errors' => [ 'Document ID is required.' ], ] )->withStatus( 400 ); |
| 276 |
} |
| 277 |
|
| 278 |
$status = Sanitize::textfield( $request->query( 'status' ) ); |
| 279 |
|
| 280 |
$previewArgs = [ 'status' => ! empty( $status ) ? $status : 'auto', |
| 281 |
'start' => Sanitize::int( $request->query( 'startSection' ) ) ]; |
| 282 |
|
| 283 |
return Depicter::output( Depicter::front()->preview()->document( $documentId, $previewArgs ) ); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Retrieves object of document editor data |
| 288 |
* |
| 289 |
* @param RequestInterface $request |
| 290 |
* @param string $view |
| 291 |
* |
| 292 |
* @return ResponseInterface |
| 293 |
*/ |
| 294 |
public function getEditorData( RequestInterface $request, $view ){ |
| 295 |
if( ! $documentId = Sanitize::int( $request->query( 'ID' ) ) ){ |
| 296 |
return Depicter::json( [ 'errors' => [ 'Document ID is required.' ], ] )->withStatus( 400 ); |
| 297 |
} |
| 298 |
|
| 299 |
$output = ''; |
| 300 |
|
| 301 |
try{ |
| 302 |
$output = Depicter::document()->getEditorData( $documentId ); |
| 303 |
} catch( Exception $exception ){ |
| 304 |
return Depicter::json( [ 'errors' => [ $exception->getMessage() ] ] ) |
| 305 |
->withHeader( 'X-Document-ID', $documentId ) |
| 306 |
->withStatus( 404 ); |
| 307 |
} |
| 308 |
|
| 309 |
return Depicter::json( $output ); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
/** |
| 314 |
* Reverts a document to previous snapshots |
| 315 |
* |
| 316 |
* @param RequestInterface $request |
| 317 |
* @param string $view |
| 318 |
* |
| 319 |
* @return ResponseInterface |
| 320 |
*/ |
| 321 |
public function revert( RequestInterface $request, $view ){ |
| 322 |
if( ! $documentId = Sanitize::int( $request->body( 'ID' ) ) ){ |
| 323 |
return Depicter::json( [ 'errors' => [ 'Document ID is required.' ], ] )->withStatus( 400 ); |
| 324 |
} |
| 325 |
$to = Sanitize::textfield( $request->body( 'to' ) ); |
| 326 |
|
| 327 |
try{ |
| 328 |
$output = Depicter::document()->repository()->revert( $documentId, $to ); |
| 329 |
return Depicter::json( $output ); |
| 330 |
|
| 331 |
} catch( Exception $exception ){ |
| 332 |
return Depicter::json( [ 'errors' => [ $exception->getMessage() ] ] ) |
| 333 |
->withHeader( 'X-Document-ID', $documentId ) |
| 334 |
->withStatus( 404 ); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Updates an entry. (PUT/PATCH) |
| 340 |
* |
| 341 |
* @param RequestInterface $request |
| 342 |
* @param string $view |
| 343 |
* |
| 344 |
* @return ResponseInterface |
| 345 |
*/ |
| 346 |
public function update( RequestInterface $request, $view ){ |
| 347 |
return Depicter::json( [ 'hits' => [] ] )->withStatus( 200 ); |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Upload cover photo of slider |
| 352 |
* |
| 353 |
* @param RequestInterface $request |
| 354 |
* @param int $id |
| 355 |
* |
| 356 |
* @return bool |
| 357 |
*/ |
| 358 |
public function setDocumentPoster( RequestInterface $request, $id ){ |
| 359 |
$coverPhoto = $request->files( 'previewImage' ); |
| 360 |
if( empty( $coverPhoto ) || ! $coverPhoto->getSize() ){ |
| 361 |
return false; |
| 362 |
} |
| 363 |
|
| 364 |
$mediaType = $coverPhoto->getClientMediaType(); |
| 365 |
$extension = str_replace( 'image/', '', $mediaType ); |
| 366 |
|
| 367 |
if( false === strpos( $mediaType, 'image/' ) ){ |
| 368 |
// Not an image file |
| 369 |
return false; |
| 370 |
} |
| 371 |
|
| 372 |
return Depicter::storage()->filesystem()->write( Depicter::storage()->getPluginUploadsDirectory() . '/preview-images/' . $id . '.' . $extension, (string) $coverPhoto->getStream() ); |
| 373 |
} |
| 374 |
|
| 375 |
} |
| 376 |
|