PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
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 / CuratedAPIAjaxController.php

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

352 lines 10.8 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 use Averta\WordPress\Utility\JSON;
5 use Depicter\Document\Mapper;
6 use Depicter\GuzzleHttp\Exception\GuzzleException;
7 use Depicter\Services\AssetsAPIService;
8 use Depicter\Utility\Http;
9 use Depicter\Utility\Sanitize;
10 use Psr\Http\Message\RequestInterface;
11 use Psr\Http\Message\ResponseInterface;
12
13
14 class CuratedAPIAjaxController
15 {
16
17 /**
18 * Search Elements
19 *
20 * @param RequestInterface $request
21 * @param string $view
22 *
23 * @return ResponseInterface
24 * @throws GuzzleException
25 */
26 public function searchElements( RequestInterface $request, $view )
27 {
28 $page = !empty( $request->query('page' ) ) ? Sanitize::int( $request->query('page') ) : 1;
29 $perpage = !empty( $request->query('perpage' ) ) ? Sanitize::int( $request->query('perpage') ) : 20;
30 $category = !empty( $request->query('category') ) ? Sanitize::textfield( $request->query('category') ) : '';
31 $search = !empty( $request->query('s' ) ) ? Sanitize::textfield( $request->query('s') ) : '';
32
33 $options = [
34 'page' => $page,
35 'perpage' => $perpage,
36 'category' => $category,
37 's' => $search
38 ];
39
40 try {
41 return \Depicter::json( AssetsAPIService::searchElements( $options ) );
42 } catch ( \Exception $exception ) {
43
44 $error = Http::getErrorExceptionResponse( $exception );
45
46 return \Depicter::json([
47 'errors' => $error
48 ])->withStatus( $error['statusCode'] );
49 }
50
51 }
52
53 /**
54 * search Document Templates
55 *
56 * @param RequestInterface $request
57 * @param string $view
58 *
59 * @return ResponseInterface
60 * @throws GuzzleException
61 */
62 public function searchDocumentTemplates( RequestInterface $request, $view )
63 {
64 $page = !empty( $request->query('page') ) ? Sanitize::int( $request->query('page') ) : 1;
65 $perpage = !empty( $request->query('perpage') ) ? Sanitize::int( $request->query('perpage') ) : 20;
66 $category = !empty( $request->query('category') ) ? Sanitize::textfield( $request->query('category') ) : '';
67 $search = !empty( $request->query('s') ) ? Sanitize::textfield( $request->query('s') ) : '';
68 $from = !empty( $request->query('from') ) ? Sanitize::textfield( $request->query('from') ) : 'website';
69 $directory= !empty( $request->query('directory') ) ? Sanitize::textfield( $request->query('directory') ) : 2;
70
71 $options = [
72 'page' => $page,
73 'perpage' => $perpage,
74 'category' => $category,
75 's' => $search,
76 'from' => $from,
77 'directory' => $directory
78 ];
79
80 try {
81 $result = AssetsAPIService::searchDocumentTemplates( $options );
82 if ( $directory == 4 ) {
83 shuffle( $result['hits'] );
84 }
85
86 return \Depicter::json( $result );
87
88 } catch ( \Exception $exception ) {
89 $error = Http::getErrorExceptionResponse( $exception );
90
91 return \Depicter::json([
92 'errors' => $error
93 ])->withStatus( $error['statusCode'] );
94 }
95
96 }
97
98 /**
99 * search Document Templates
100 *
101 * @param RequestInterface $request
102 * @param string $view
103 *
104 * @return ResponseInterface
105 * @throws GuzzleException
106 */
107 public function searchDocumentTemplatesV2( RequestInterface $request, $view )
108 {
109 $page = !empty( $request->query('page') ) ? Sanitize::int( $request->query('page') ) : 1;
110 $perpage = !empty( $request->query('perpage') ) ? Sanitize::int( $request->query('perpage') ) : 20;
111 $group = !empty( $request->query('group') ) ? Sanitize::textfield( $request->query('group') ) : '';
112 $search = !empty( $request->query('s') ) ? Sanitize::textfield( $request->query('s') ) : '';
113 $from = !empty( $request->query('from') ) ? Sanitize::textfield( $request->query('from') ) : 'website';
114 $directory= !empty( $request->query('directory') ) ? Sanitize::textfield( $request->query('directory') ) : 2;
115 $flush = !empty( $request->query('flush') );
116
117 $options = [
118 'page' => $page,
119 'perpage' => $perpage,
120 'group' => $group,
121 's' => $search,
122 'from' => $from,
123 'flush' => $flush,
124 'directory' => $directory
125 ];
126
127 try {
128 return \Depicter::json( AssetsAPIService::searchDocumentTemplatesV2( $options ) );
129
130 } catch ( \Exception $exception ) {
131 $error = Http::getErrorExceptionResponse( $exception );
132
133 return \Depicter::json([
134 'errors' => $error
135 ])->withStatus( $error['statusCode'] );
136 }
137
138 }
139
140 public function getDocumentTemplateCategories( RequestInterface $request, $view )
141 {
142 $category = !empty( $request->query('category') ) ? Sanitize::textfield( $request->query('category') ) : '';
143 $directory = !empty( $request->query('directory') ) ? Sanitize::textfield( $request->query('directory') ) : 2;
144
145 $options = [
146 'category' => $category,
147 'directory' => $directory
148 ];
149
150 try {
151 return \Depicter::json( AssetsAPIService::getDocumentTemplateCategories( $options ) );
152 } catch ( \Exception $exception ) {
153 $error = Http::getErrorExceptionResponse( $exception );
154
155 return \Depicter::json([
156 'errors' => $error
157 ])->withStatus( $error['statusCode'] );
158 }
159 }
160
161 public function getDocumentTemplateGroups( RequestInterface $request, $view )
162 {
163 $group = !empty( $request->query('group') ) ? Sanitize::textfield( $request->query('group') ) : '';
164 $directory = !empty( $request->query('directory') ) ? Sanitize::textfield( $request->query('directory') ) : 2;
165 $featured = !empty( $request->query('featured') );
166
167 $options = [
168 'group' => $group,
169 'featured' => $featured,
170 'directory' => $directory
171 ];
172
173 try {
174 return \Depicter::json( AssetsAPIService::getDocumentTemplateGroups( $options ) );
175 } catch ( \Exception $exception ) {
176 $error = Http::getErrorExceptionResponse( $exception );
177
178 return \Depicter::json([
179 'errors' => $error
180 ])->withStatus( $error['statusCode'] );
181 }
182 }
183
184 /**
185 * Imports a template
186 *
187 * @param RequestInterface $request
188 * @param $view
189 *
190 * @return ResponseInterface
191 * @throws GuzzleException
192 */
193 public function importDocumentTemplate( RequestInterface $request, $view ) {
194 $templateID = !empty( $request->query('ID') ) ? Sanitize::textfield( $request->query('ID') ) : '';
195 $endpointVersion = !empty( $request->query('v') ) ? Sanitize::int( $request->query('v') ) : 1;
196 $directory = !empty( $request->query('directory') ) ? Sanitize::textfield( $request->query('directory') ) : 2;
197
198 try {
199 $result = AssetsAPIService::getDocumentTemplateData( $templateID, [ 'v' => $endpointVersion, 'directory' => $directory ] );
200
201 if ( !empty( $result->errors ) ) {
202 return Http::getErrorJson( $result->errors );
203
204 } elseif ( !empty( $result->hits ) ) {
205 $editorData = JSON::encode( $result->hits );
206 $editorData = preg_replace( '/"activeBreakpoint":".+?"/', '"activeBreakpoint":"default"', $editorData );
207 $document = \Depicter::documentRepository()->create();
208
209 $updateData = ['content' => $editorData ];
210 if ( !empty( $result->title ) ) {
211 $updateData['name'] = $result->title . ' ' . $document->getID();
212 }
213
214 if ( !empty( $result->type ) ) {
215 $updateData['type'] = $result->type;
216 }
217 if ( !empty( $result->image ) ) {
218 $imageRequest = wp_remote_get( $result->image );
219 $previewImage = wp_remote_retrieve_body( $imageRequest );
220 \Depicter::storage()->filesystem()->write( \Depicter::documentRepository()->getPreviewImagePath( $document->getID() ) , $previewImage );
221 }
222
223 \Depicter::documentRepository()->update( $document->getID(), $updateData );
224 \Depicter::media()->importDocumentAssets( $editorData );
225
226 if ( \Depicter::options()->get('use_google_fonts', 'on') === 'save_locally' ) {
227 $documentModel = \Depicter::document()->getModel( $document->getID() )->prepare();
228 \Depicter::googleFontsService()->download( $documentModel->getFontsLink() );
229 }
230
231 return \Depicter::json([
232 'hits' => [
233 'documentID' => $document->getID()
234 ]
235 ]);
236
237 } else {
238 // Return the error message received from server
239 return \Depicter::json( $result );
240 }
241
242 } catch ( \Exception $exception ) {
243
244 $error = Http::getErrorExceptionResponse( $exception );
245
246 return \Depicter::json([
247 'errors' => $error
248 ])->withStatus( $error['statusCode'] );
249 }
250 }
251
252 /**
253 * @param RequestInterface $request
254 * @param $view
255 *
256 * @return ResponseInterface
257 * @throws GuzzleException
258 */
259 public function previewDocumentTemplate( RequestInterface $request, $view ) {
260 $templateID = !empty( $request->query('ID') ) ? Sanitize::textfield( $request->query('ID') ) : '';
261 $directory = !empty( $request->query('directory') ) ? Sanitize::textfield( $request->query('directory') ) : 2;
262
263 try {
264 $result = AssetsAPIService::previewDocumentTemplate( $templateID, $directory );
265 return \Depicter::output( $result );
266 } catch ( \Exception $exception ) {
267 $error = Http::getErrorExceptionResponse( $exception );
268
269 return \Depicter::json([
270 'errors' => $error
271 ])->withStatus( $error['statusCode'] );
272 }
273 }
274
275 /**
276 * Search in Animations
277 *
278 * @param RequestInterface $request
279 * @param string $view
280 *
281 * @return ResponseInterface
282 * @throws GuzzleException
283 */
284 public function searchAnimations( RequestInterface $request, $view ){
285
286 $page = !empty( $request->query('page') ) ? Sanitize::int( $request->query('page') ) : 1;
287 $perpage = !empty( $request->query('perpage') ) ? Sanitize::int( $request->query('perpage') ) : 20;
288 $phase = !empty( $request->query('phase') ) ? Sanitize::textfield( $request->query('phase') ) : '';
289 $search = !empty( $request->query('s' ) ) ? Sanitize::textfield( $request->query('s') ) : '';
290 $category = !empty( $request->query('category' ) ) ? Sanitize::textfield( $request->query('category') ) : '';
291
292 $options = [
293 'page' => $page,
294 'perpage' => $perpage,
295 'phase' => $phase,
296 's' => $search,
297 'category' => $category
298 ];
299
300
301 try {
302 return \Depicter::json( AssetsAPIService::searchAnimations( $options ) );
303
304 } catch ( \Exception $exception ) {
305 $error = Http::getErrorExceptionResponse( $exception );
306
307 return \Depicter::json([
308 'errors' => $error
309 ])->withStatus( $error['statusCode'] );
310 }
311
312 }
313
314 /**
315 * Get list of animation categories
316 *
317 * @param RequestInterface $request
318 * @param $view
319 *
320 * @return ResponseInterface
321 * @throws GuzzleException
322 */
323 public function getAnimationsCategories( RequestInterface $request, $view )
324 {
325
326 $phase = !empty( $request->query('phase') ) ? Sanitize::textfield( $request->query('phase') ) : '';
327 $options = [ 'phase' => $phase ];
328
329 if( !empty( $request->query('elementType') ) ){
330 $options['elementType'] = Sanitize::textfield( $request->query('elementType') );
331 }
332
333 try {
334 $result = AssetsAPIService::getAnimationsCategories( $options );
335
336 if ( !empty( $result['errors'] ) ) {
337 return Http::getErrorJson( $result['errors'] );
338 } else {
339 return \Depicter::json( $result );
340 }
341
342 } catch ( \Exception $exception ) {
343 $error = Http::getErrorExceptionResponse( $exception );
344
345 return \Depicter::json([
346 'errors' => $error
347 ])->withStatus( $error['statusCode'] );
348 }
349 }
350
351 }
352