PluginProbe
Depicter — Popup & Slider Builder / 1.3.8
Depicter — Popup & Slider Builder v1.3.8
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 1.3.8, at app/src/Controllers/Ajax/CuratedAPIAjaxController.php

253 lines 7.3 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\GuzzleHttp\Exception\GuzzleException;
6 use Depicter\Services\AssetsAPIService;
7 use Depicter\Utility\Http;
8 use Depicter\Utility\Sanitize;
9 use Psr\Http\Message\RequestInterface;
10 use Psr\Http\Message\ResponseInterface;
11
12
13 class CuratedAPIAjaxController
14 {
15
16 /**
17 * Search Elements
18 *
19 * @param RequestInterface $request
20 * @param string $view
21 *
22 * @return ResponseInterface
23 * @throws GuzzleException
24 */
25 public function searchElements( RequestInterface $request, $view )
26 {
27 $page = !empty( $request->query('page' ) ) ? Sanitize::int( $request->query('page') ) : 1;
28 $perpage = !empty( $request->query('perpage' ) ) ? Sanitize::int( $request->query('perpage') ) : 20;
29 $category = !empty( $request->query('category') ) ? Sanitize::textfield( $request->query('category') ) : '';
30 $search = !empty( $request->query('s' ) ) ? Sanitize::textfield( $request->query('s') ) : '';
31
32 $options = [
33 'page' => $page,
34 'perpage' => $perpage,
35 'category' => $category,
36 's' => $search
37 ];
38
39 try {
40 return \Depicter::json( AssetsAPIService::searchElements( $options ) );
41 } catch ( \Exception $exception ) {
42
43 $error = Http::getErrorExceptionResponse( $exception );
44
45 return \Depicter::json([
46 'errors' => $error
47 ])->withStatus( $error['statusCode'] );
48 }
49
50 }
51
52 /**
53 * search Document Templates
54 *
55 * @param RequestInterface $request
56 * @param string $view
57 *
58 * @return ResponseInterface
59 * @throws GuzzleException
60 */
61 public function searchDocumentTemplates( RequestInterface $request, $view )
62 {
63 $page = !empty( $request->query('page') ) ? Sanitize::int( $request->query('page') ) : 1;
64 $perpage = !empty( $request->query('perpage') ) ? Sanitize::int( $request->query('perpage') ) : 20;
65 $category = !empty( $request->query('category') ) ? Sanitize::textfield( $request->query('category') ) : '';
66 $search = !empty( $request->query('s') ) ? Sanitize::textfield( $request->query('s') ) : '';
67
68 $options = [
69 'page' => $page,
70 'perpage' => $perpage,
71 'category' => $category,
72 's' => $search
73 ];
74
75 try {
76 return \Depicter::json( AssetsAPIService::searchDocumentTemplates( $options ) );
77
78 } catch ( \Exception $exception ) {
79 $error = Http::getErrorExceptionResponse( $exception );
80
81 return \Depicter::json([
82 'errors' => $error
83 ])->withStatus( $error['statusCode'] );
84 }
85
86 }
87
88 public function getDocumentTemplateCategories( RequestInterface $request, $view )
89 {
90 $category = !empty( $request->query('category') ) ? Sanitize::textfield( $request->query('category') ) : '';
91
92 $options = [ 'category' => $category ];
93
94 try {
95 return \Depicter::json( AssetsAPIService::getDocumentTemplateCategories( $options ) );
96 } catch ( \Exception $exception ) {
97 $error = Http::getErrorExceptionResponse( $exception );
98
99 return \Depicter::json([
100 'errors' => $error
101 ])->withStatus( $error['statusCode'] );
102 }
103 }
104
105 /**
106 * Imports a template
107 *
108 * @param RequestInterface $request
109 * @param $view
110 *
111 * @return ResponseInterface
112 * @throws GuzzleException
113 */
114 public function importDocumentTemplate( RequestInterface $request, $view ) {
115 $templateID = !empty( $request->query('ID') ) ? Sanitize::textfield( $request->query('ID') ) : '';
116 try {
117 $result = AssetsAPIService::getDocumentTemplateData( $templateID );
118 if ( !empty( $result->hits ) ) {
119 $editorData = JSON::encode( $result->hits );
120 $editorData = preg_replace( '/"activeBreakpoint":".+?"/', '"activeBreakpoint":"default"', $editorData );
121 $document = \Depicter::documentRepository()->create();
122
123 $updateData = ['content' => $editorData ];
124 if ( !empty( $result->title ) ) {
125 $updateData['name'] = $result->title . ' ' . $document->getID();
126 }
127 if ( !empty( $result->image ) ) {
128 $previewImage = file_get_contents( $result->image );
129 \Depicter::storage()->filesystem()->write( \Depicter::documentRepository()->getPreviewImagePath( $document->getID() ) , $previewImage );
130 }
131
132 \Depicter::documentRepository()->update( $document->getID(), $updateData );
133 \Depicter::media()->importDocumentAssets( $editorData );
134
135 return \Depicter::json([
136 'hits' => [
137 'documentID' => $document->getID()
138 ]
139 ]);
140
141 } else {
142 // Return the error message received from server
143 return \Depicter::json( $result );
144 }
145
146 } catch ( \Exception $exception ) {
147
148 $error = Http::getErrorExceptionResponse( $exception );
149
150 if( $error['group'] == 'RequestException' ){
151 $error['message'] = __( 'Cannot reach the server for downloading images.', 'depicter' );
152 $error['statusCode'] = 421;
153 }
154
155 return \Depicter::json([
156 'errors' => $error
157 ])->withStatus( $error['statusCode'] );
158 }
159 }
160
161 /**
162 * @param RequestInterface $request
163 * @param $view
164 *
165 * @return ResponseInterface
166 * @throws GuzzleException
167 */
168 public function previewDocumentTemplate( RequestInterface $request, $view ) {
169 $templateID = !empty( $request->query('ID') ) ? Sanitize::textfield( $request->query('ID') ) : '';
170 try {
171 $result = AssetsAPIService::previewDocumentTemplate( $templateID );
172 return \Depicter::output( $result );
173 } catch ( \Exception $exception ) {
174 $error = Http::getErrorExceptionResponse( $exception );
175
176 return \Depicter::json([
177 'errors' => $error
178 ])->withStatus( $error['statusCode'] );
179 }
180 }
181
182 /**
183 * Search in Animations
184 *
185 * @param RequestInterface $request
186 * @param string $view
187 *
188 * @return ResponseInterface
189 * @throws GuzzleException
190 */
191 public function searchAnimations( RequestInterface $request, $view ){
192
193 $page = !empty( $request->query('page') ) ? Sanitize::int( $request->query('page') ) : 1;
194 $perpage = !empty( $request->query('perpage') ) ? Sanitize::int( $request->query('perpage') ) : 20;
195 $phase = !empty( $request->query('phase') ) ? Sanitize::textfield( $request->query('phase') ) : '';
196 $search = !empty( $request->query('s' ) ) ? Sanitize::textfield( $request->query('s') ) : '';
197 $category = !empty( $request->query('category' ) ) ? Sanitize::textfield( $request->query('category') ) : '';
198
199 $options = [
200 'page' => $page,
201 'perpage' => $perpage,
202 'phase' => $phase,
203 's' => $search,
204 'category' => $category
205 ];
206
207
208 try {
209 return \Depicter::json( AssetsAPIService::searchAnimations( $options ) );
210
211 } catch ( \Exception $exception ) {
212 $error = Http::getErrorExceptionResponse( $exception );
213
214 return \Depicter::json([
215 'errors' => $error
216 ])->withStatus( $error['statusCode'] );
217 }
218
219 }
220
221 /**
222 * Get list of animation categories
223 *
224 * @param RequestInterface $request
225 * @param $view
226 *
227 * @return ResponseInterface
228 * @throws GuzzleException
229 */
230 public function getAnimationsCategories( RequestInterface $request, $view )
231 {
232
233 $phase = !empty( $request->query('phase') ) ? Sanitize::textfield( $request->query('phase') ) : '';
234 $options = [ 'phase' => $phase ];
235
236 if( !empty( $request->query('elementType') ) ){
237 $options['elementType'] = Sanitize::textfield( $request->query('elementType') );
238 }
239
240 try {
241 return \Depicter::json( AssetsAPIService::getAnimationsCategories( $options ) );
242
243 } catch ( \Exception $exception ) {
244 $error = Http::getErrorExceptionResponse( $exception );
245
246 return \Depicter::json([
247 'errors' => $error
248 ])->withStatus( $error['statusCode'] );
249 }
250 }
251
252 }
253