PluginProbe
WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation / trunk
WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation vtrunk
1.4.48 1.4.47 1.4.46 1.4.45 1.4.44 1.4.43 1.4.42 1.4.41 1.4.40 1.4.39 1.4.38 1.4.37 1.4.36 1.1.2 1.2.0 1.2.1 1.2.2 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 All 64 releases
optin / includes / utils / class-templates.php

class-templates.php in WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation trunk, at includes/utils/class-templates.php

353 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore
2
3 namespace OPTN\Includes\Utils;
4
5 /**
6 * Template class
7 */
8 class Templates {
9
10 const TEMPLATE_URL = 'https://apps.wowoptin.com/wp-json/otmp/v1/templates';
11 const PRESET_URL = 'https://apps.wowoptin.com/wp-json/otmp/v1/presets';
12 const RECIPE_URL = 'https://apps.wowoptin.com/wp-json/otmp/v1/recipes';
13
14 /**
15 * Get metadata of all templates
16 *
17 * @return array|null
18 */
19 public static function get_templates() {
20 $key = get_option( 'optn_temp_metadata_key', '' );
21
22 $cached_data = self::get_from_cache( 'templates' );
23
24 $key = ! empty( $cached_data ) ? $key : '';
25
26 $url = add_query_arg(
27 array(
28 'key' => $key,
29 'is_dev' => defined( 'OPTN_DEV_MODE' ) && OPTN_DEV_MODE ? '1' : '0',
30 'version' => defined( 'OPTN_VERSION' ) ? OPTN_VERSION : '0.0.0',
31 ),
32 self::TEMPLATE_URL
33 );
34
35 $response = wp_remote_get(
36 $url,
37 array(
38 'timeout' => 30,
39 )
40 );
41
42 if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
43 Utils::log_to_file( $response );
44 return null;
45 }
46
47 $data = json_decode( $response['body'] );
48
49 if ( isset( $data->data ) && 'valid' === $data->data ) {
50 return $cached_data;
51 }
52
53 if ( ! isset( $data->data ) || ! isset( $data->key ) ) {
54 return null;
55 }
56
57 update_option( 'optn_temp_metadata_key', $data->key );
58 self::store_in_cache( 'templates', $data->data );
59
60 return $data->data;
61 }
62
63 /**
64 * Get metadata of all element presets
65 *
66 * @return array|null
67 */
68 public static function get_presets() {
69 $key = get_option( 'optn_temp_preset_key', '' );
70
71 $cached_data = self::get_from_cache( 'presets' );
72
73 $key = ! empty( $cached_data ) ? $key : '';
74
75 $url = add_query_arg(
76 array(
77 'key' => $key,
78 'is_dev' => defined( 'OPTN_DEV_MODE' ) && OPTN_DEV_MODE ? '1' : '0',
79 'version' => defined( 'OPTN_VERSION' ) ? OPTN_VERSION : '0.0.0',
80 ),
81 self::PRESET_URL
82 );
83
84 $response = wp_remote_get(
85 $url,
86 array(
87 'timeout' => 30,
88 )
89 );
90
91 if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
92 Utils::log_to_file( $response );
93 return null;
94 }
95
96 $data = json_decode( $response['body'] );
97
98 if ( isset( $data->data ) && 'valid' === $data->data ) {
99 return $cached_data;
100 }
101
102 if ( ! isset( $data->data ) || ! isset( $data->key ) ) {
103 return null;
104 }
105
106 update_option( 'optn_temp_preset_key', $data->key );
107 self::store_in_cache( 'presets', $data->data );
108
109 return $data->data;
110 }
111
112 /**
113 * Get Template post
114 *
115 * @param int $id template id.
116 * @return array|null
117 */
118 public static function fetch_template_post( $id ) {
119
120 $cache_data = Cache::get( 'optn_template_post' );
121
122 if ( ! empty( $cache_data ) && isset( $cache_data[ $id ] ) ) {
123 return $cache_data[ $id ];
124 }
125
126 $url = add_query_arg(
127 array(
128 'license' => Utils::get_license_key(),
129 'is_dev' => defined( 'OPTN_DEV_MODE' ) && OPTN_DEV_MODE ? '1' : '0',
130 'version' => defined( 'OPTN_VERSION' ) ? OPTN_VERSION : '0.0.0',
131 ),
132 self::TEMPLATE_URL . '/preview/' . $id
133 );
134
135 $response = wp_remote_get(
136 $url,
137 array(
138 'timeout' => 15,
139 )
140 );
141
142 if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
143 Utils::log_to_file( $response );
144
145 $body = wp_remote_retrieve_body( $response );
146 $body = json_decode( $body, true );
147 if ( isset( $body['message'] ) && 'Update required' === $body['message'] ) {
148 return 'update_required';
149 }
150
151 return null;
152 }
153
154 $data = json_decode( $response['body'] );
155
156 if ( ! empty( $data ) ) {
157 Cache::set( 'optn_template_post', array( $id => $data ), 3 );
158 }
159
160 return $data;
161 }
162
163
164 /**
165 * Get template data by ID
166 *
167 * @param int $id template id.
168 * @return array|null
169 */
170 public static function fetch_single_template( $id ) {
171
172 $url = add_query_arg(
173 array(
174 'license' => Utils::get_license_key(),
175 'is_dev' => defined( 'OPTN_DEV_MODE' ) && OPTN_DEV_MODE ? '1' : '0',
176 'version' => defined( 'OPTN_VERSION' ) ? OPTN_VERSION : '0.0.0',
177 ),
178 self::TEMPLATE_URL . '/' . $id
179 );
180
181 $response = wp_remote_get(
182 $url,
183 array(
184 'timeout' => 30,
185 )
186 );
187
188 if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
189 Utils::log_to_file( $response );
190
191 $body = wp_remote_retrieve_body( $response );
192 $body = json_decode( $body, true );
193 if ( isset( $body['message'] ) && 'Update required' === $body['message'] ) {
194 return 'update_required';
195 }
196
197 return null;
198 }
199
200 $data = json_decode( $response['body'] );
201
202 return $data;
203 }
204
205
206 /**
207 * Get recipe data by ID
208 *
209 * @param int $id template id.
210 * @return array|null
211 */
212 public static function fetch_single_recipe( $id ) {
213
214 $url = add_query_arg(
215 array(
216 'license' => Utils::get_license_key(),
217 'is_dev' => defined( 'OPTN_DEV_MODE' ) && OPTN_DEV_MODE ? '1' : '0',
218 'version' => defined( 'OPTN_VERSION' ) ? OPTN_VERSION : '0.0.0',
219 ),
220 self::RECIPE_URL . '/' . $id
221 );
222
223 $response = wp_remote_get(
224 $url,
225 array(
226 'timeout' => 30,
227 )
228 );
229
230 if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
231 Utils::log_to_file( $response );
232
233 $body = wp_remote_retrieve_body( $response );
234 $body = json_decode( $body, true );
235 if ( isset( $body['message'] ) && 'Update required' === $body['message'] ) {
236 return 'update_required';
237 }
238
239 return null;
240 }
241
242 $data = json_decode( wp_remote_retrieve_body( $response ) );
243
244 return $data;
245 }
246
247 /**
248 * Get Preset data by ID
249 *
250 * @param int $id preset id.
251 * @return array|null
252 */
253 public static function fetch_single_preset( $id ) {
254
255 $url = add_query_arg(
256 array(
257 'license' => Utils::get_license_key(),
258 'is_dev' => defined( 'OPTN_DEV_MODE' ) && OPTN_DEV_MODE ? '1' : '0',
259 'version' => defined( 'OPTN_VERSION' ) ? OPTN_VERSION : '0.0.0',
260 ),
261 self::PRESET_URL . '/' . $id
262 );
263
264 $response = wp_remote_get(
265 $url,
266 array(
267 'timeout' => 30,
268 )
269 );
270
271 if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
272 Utils::log_to_file( $response );
273
274 $body = wp_remote_retrieve_body( $response );
275 $body = json_decode( $body, true );
276 if ( isset( $body['message'] ) && 'Update required' === $body['message'] ) {
277 return 'update_required';
278 }
279
280 return null;
281 }
282
283 $data = json_decode( $response['body'] );
284
285 return $data;
286 }
287
288 /**
289 * Get Cache root dir
290 *
291 * @return string
292 */
293 private static function get_path() {
294 $upload_dir = wp_upload_dir();
295 return trailingslashit( $upload_dir['basedir'] ) . 'optn';
296 }
297
298 /**
299 * Get value from cache
300 *
301 * @param string $key key.
302 * @return string|null
303 */
304 private static function get_from_cache( $key ) {
305 $path = self::get_path() . "/{$key}.json";
306
307 if ( file_exists( $path ) ) {
308 return wp_json_file_decode( $path );
309 }
310
311 return null;
312 }
313
314 /**
315 * Delete value from cache
316 *
317 * @param string $key key.
318 * @return void
319 */
320 private static function delete_cache( $key ) {
321 $path = self::get_path() . "/{$key}.json";
322 wp_delete_file( $path );
323 }
324
325 /**
326 * Store value in cache
327 *
328 * @param string $key key.
329 * @param string $content content.
330 * @return void
331 */
332 private static function store_in_cache( $key, $content ) {
333 if ( ! function_exists( 'wp_filesystem' ) ) {
334 require_once ABSPATH . 'wp-admin/includes/file.php';
335 }
336
337 global $wp_filesystem;
338 WP_Filesystem();
339
340 $dir = self::get_path();
341
342 if ( ! $wp_filesystem->is_dir( $dir ) ) {
343 $wp_filesystem->mkdir( $dir );
344 }
345
346 $path = trailingslashit( $dir ) . "{$key}.json";
347
348 if ( ! $wp_filesystem->put_contents( $path, wp_json_encode( $content ), FS_CHMOD_FILE ) ) {
349 Utils::log( 'Failed to write to the cache file: ' . $path );
350 }
351 }
352 }
353