PluginProbe
Depicter — Popup & Slider Builder / 1.5.2
Depicter — Popup & Slider Builder v1.5.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.5.2, at app/src/Controllers/Ajax/EditorAjaxController.php

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