PluginProbe
Templateberg – Gutenberg Templates, WordPress Themes Template Kits & WordPress Templates / 1.2.0
Templateberg – Gutenberg Templates, WordPress Themes Template Kits & WordPress Templates v1.2.0
1.2.0 2.0.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9
templateberg / includes / functions.php

functions.php in Templateberg – Gutenberg Templates, WordPress Themes Template Kits & WordPress Templates 1.2.0, at includes/functions.php

727 lines 22.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! function_exists( 'templateberg_is_edit_page' ) ) {
3 function templateberg_is_edit_page() {
4 // make sure we are on the backend
5 if ( ! is_admin() ) {
6 return false;
7 }
8 global $pagenow;
9 return in_array( $pagenow, array( 'post.php', 'post-new.php' ) );
10 }
11 }
12
13 if ( ! function_exists( 'templateberg_get_payment_info_html' ) ) {
14 function templateberg_get_payment_info_html() {
15 $payment_info = templateberg_connect()->get_purchase();
16 if ( is_array( $payment_info ) && ! empty( $payment_info ) ) {
17 ?>
18 <div class="tb-purchase__wrap">
19 <div class="tb-purchase__id">
20 <span class="tb-purchase__title"><?php echo esc_html__( 'ID', 'templateberg' ); ?></span>
21 </div>
22 <div class="tb-purchase__type">
23 <span class="tb-purchase__title"><?php echo esc_html__( 'Type', 'templateberg' ); ?></span>
24 </div>
25 <div class="tb-purchase__type">
26 <span class="tb-purchase__title"><?php echo esc_html__( 'Name', 'templateberg' ); ?></span>
27 </div>
28 <div class="tb-purchase__date">
29 <span class="tb-purchase__title"><?php echo esc_html__( 'Date', 'templateberg' ); ?></span>
30 </div>
31 <div class="tb-purchase__amount">
32 <span class="tb-purchase__title"><?php echo esc_html__( 'Amount', 'templateberg' ); ?></span>
33 </div>
34 <div class="tb-purchase__limit">
35 <span class="tb-purchase__title"><?php echo esc_html__( 'Limit', 'templateberg' ); ?></span>
36 </div>
37 <div class="tb-purchase__plan">
38 <span class="tb-purchase__title"><?php echo esc_html__( 'Plan', 'templateberg' ); ?></span>
39 </div>
40 <div class="tb-purchase__link">
41 <span class="tb-purchase__title"><?php echo esc_html__( 'Details', 'templateberg' ); ?></span>
42 </div>
43 </div>
44 <?php
45 foreach ( $payment_info as $pi ) {
46 ?>
47 <div class="tb-purchase__wrap">
48 <div class="tb-purchase__id">
49 <span class="tb-purchase__desc">
50 <?php echo isset( $pi['id'] ) ? esc_html( $pi['id'] ) : ''; ?>
51 </span>
52 </div>
53 <div class="tb-purchase__type">
54 <span class="tb-purchase__desc">
55 <?php echo isset( $pi['type'] ) ? esc_html( $pi['type'] ) : ''; ?>
56 </span>
57 </div>
58 <div class="tb-purchase__type">
59 <span class="tb-purchase__desc">
60 <?php echo isset( $pi['title'] ) ? wp_kses_post( $pi['title'] ) : ''; ?>
61 </span>
62 </div>
63 <div class="tb-purchase__date">
64 <span class="tb-purchase__desc">
65 <?php echo isset( $pi['date'] ) ? esc_html( $pi['date'] ) : ''; ?>
66 </span>
67 </div>
68 <div class="tb-purchase__amount">
69 <span class="tb-purchase__desc">
70 <?php echo isset( $pi['amount'] ) ? esc_html( $pi['amount'] ) : ''; ?>
71 </span>
72 </div>
73 <div class="tb-purchase__limit">
74 <span class="tb-purchase__desc">
75 <?php echo isset( $pi['limit'] ) ? esc_html( $pi['limit'] ) : ''; ?>
76 </span>
77 </div>
78 <div class="tb-purchase__plan">
79 <span class="tb-purchase__desc">
80 <?php echo isset( $pi['plan'] ) ? esc_html( $pi['plan'] ) : ''; ?>
81 </span>
82 </div>
83
84 <div class="tb-purchase__plan">
85 <span class="tb-purchase__desc">
86 <?php echo wp_kses_post( $pi['details'] ); ?>
87 </span>
88 </div>
89 </div>
90 <?php
91 }
92 } else {
93 ?>
94 <div class="tb-info__box">
95 <?php echo esc_html__( 'No payment information found', 'templateberg' ); ?>
96 </div>
97 <?php
98 }
99 }
100 }
101
102 if ( ! function_exists( 'templateberg_get_free_templates_html' ) ) {
103 function templateberg_get_free_templates_html() {
104 $free_templates = templateberg_connect()->get_free_templates();
105 if ( is_array( $free_templates ) && ! empty( $free_templates ) ) {
106 echo '<div class="tb-row">';
107 foreach ( $free_templates as $ft ) {
108 ?>
109 <div class="tb-col-3">
110 <img src="<?php echo esc_url( $ft['screenshot_url'] ); ?>" alt="<?php echo esc_attr( $ft['title'] ); ?>"/>
111 </div>
112 <?php
113 }
114 echo '</div>';
115 } else {
116 ?>
117 <div class="tb-info__box">
118 <?php echo esc_html__( 'No template information found', 'templateberg' ); ?>
119 </div>
120 <?php
121 }
122 }
123 }
124
125 /**
126 * check if Gutentor activated
127 */
128 if ( ! function_exists( 'templateburg_is_gutentor_active' ) ) {
129
130 function templateburg_is_gutentor_active() {
131 return class_exists( 'Gutentor' ) ? true : false;
132 }
133 }
134
135
136 /**
137 * check if Gutentor activated
138 */
139 if ( ! function_exists( 'templateburg_is_advanced_import_active' ) ) {
140
141 function templateburg_is_advanced_import_active() {
142 return class_exists( 'Advanced_Import' ) ? true : false;
143 }
144 }
145
146
147 /**
148 * Get Transients by prefix
149 *
150 * @param $prefix string Prefix of Transients
151 * without _transient_
152 *
153 * @return array|boolean Array of Transients or false if no Transients
154 */
155 if ( ! function_exists( 'templateberg_get_transients_by_prefix' ) ) {
156 function templateberg_get_transients_by_prefix( $prefix ) {
157
158 global $wpdb;
159
160 /*Add Prefix*/
161 $prefix = $wpdb->esc_like( '_transient_' . $prefix );
162
163 /*SQL*/
164 $sql = "SELECT `option_name` FROM $wpdb->options WHERE `option_name` LIKE '%s'";
165
166 /*Get transients*/
167 $transients = $wpdb->get_results(
168 $wpdb->prepare(
169 $sql,
170 $prefix . '%'
171 ),
172 ARRAY_A
173 );
174
175 /*Return*/
176 if ( $transients && ! is_wp_error( $transients ) ) {
177 return $transients;
178 }
179
180 /*No transients, return false*/
181 return false;
182 }
183 }
184
185 /**
186 * Delete Transients by prefix
187 *
188 * @param $prefix string Prefix of Transients
189 * without _transient_
190 *
191 * @return array|boolean Array of found Transients and deleted Transients options name
192 * or false if no Transients
193 */
194 if ( ! function_exists( 'templateberg_delete_transients_by_prefix' ) ) {
195
196 function templateberg_delete_transients_by_prefix( $prefix ) {
197
198 $transients = templateberg_get_transients_by_prefix( $prefix );
199 if ( ! $transients ) {
200 return false;
201 }
202
203 $deleted = array();
204
205 /*Loop through found transients*/
206 foreach ( $transients as $transient ) {
207 $deleted[] = $transient['option_name'];
208 delete_transient( str_replace( '_transient_', '', $transient['option_name'] ) );
209 }
210
211 /*
212 Return an array of total transients
213 and deleted transients option_name*/
214 return array(
215 'found' => count( $transients ),
216 'deleted' => $deleted,
217 );
218 }
219 }
220
221 /**
222 * Function create pagination
223 *
224 * @param [array] $attr
225 * @return String
226 */
227 if ( ! function_exists( 'templateberg_pagination' ) ) {
228 function templateberg_pagination( $paged = false, $max_num_pages = false ) {
229 $da_link = get_post_type_archive_link( 'download' );
230 $nextDisabled = ( $paged + 1 ) > $max_num_pages ? 'tb-list__page-disabled' : '';
231 $prevDisabled = ( $paged - 1 ) < 1 ? 'tb-list__page-disabled' : '';
232 $prevPage = ( $paged - 1 ) < 1 ? 1 : $paged - 1;
233 $nextPage = ( $paged + 1 ) > $max_num_pages ? $max_num_pages : $paged + 1;
234
235 $phtml = '';
236 $phtml .= '<div class="tb-list__navigation"><nav aria-label="Page navigation"><ul class="tb-list__pagination">';
237 $phtml .= '<li class="tb-list__page-item ' . $prevDisabled . '"><a class="tb-list__page-link" href="' . esc_url( $da_link ) . 'page/' . $prevPage . '" data-gpage="' . esc_html( $prevPage ) . '">' . esc_html__( 'Previous', 'templateberg' ) . '</a></li>';
238
239 if ( ! $paged ) {
240 $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
241 }
242 if ( ! $max_num_pages ) {
243 global $wp_query;
244 $max_num_pages = $wp_query->max_num_pages;
245 if ( ! $max_num_pages ) {
246 $max_num_pages = 1;
247 }
248 }
249 $mid_pages = $paged >= 3 ? array( $paged - 1, $paged, $paged + 1 ) : array( 1, 2, 3 );
250 if ( $max_num_pages > 1 ) {
251 if ( ! in_array( 1, $mid_pages ) ) {
252 $is_active = $paged == 1 ? ' tb-list__page-active' : '';
253 $phtml .= '<li class="tb-list__page-item ' . $is_active . '">
254 <a class="tb-list__page-link" href="' . esc_url( $da_link ) . 'page/1" data-gpage="1">' . __( '1', 'templateberg' ) . '</a>
255 </li>';
256 }
257 if ( $paged > 3 ) {
258 $phtml .= '<li class="tb-list__page-item tb-list__pagination-dots"><a class="tb-list__page-link" href="#">...</a></li>';
259 }
260 foreach ( $mid_pages as $i ) {
261 if ( $max_num_pages >= $i ) {
262 $is_active = $paged == $i ? ' tb-list__page-active' : '';
263 $phtml .= '<li class="tb-list__page-item' . $is_active . '">
264 <a class="tb-list__page-link" href="' . esc_url( $da_link ) . 'page/' . $i . '" data-gpage="' . $i . '">' . __( $i, 'templateberg' ) . '</a>
265 </li>';
266 }
267 }
268 if ( $max_num_pages > $paged + 1 ) {
269 if ( $max_num_pages > 3 ) {
270 $phtml .= '<li class="tb-list__page-item tb-list__pagination-dots"><a class="tb-list__page-link" href="#">...</a></li>';
271 }
272 if ( $max_num_pages > 3 ) {
273 $is_active = $paged == $max_num_pages ? ' tb-list__page-active' : '';
274 $phtml .= '<li class="tb-list__page-item ' . $is_active . '">
275 <a class="tb-list__page-link" href="' . esc_url( $da_link ) . 'page/' . $max_num_pages . '" data-gpage="' . $max_num_pages . '">' . __( $max_num_pages, 'templateberg' ) . '</a>
276 </li>';
277 }
278 }
279 }
280 $phtml .= ' <li class="tb-list__page-item ' . $nextDisabled . '"><a class="tb-list__page-link" href="' . esc_url( $da_link ) . 'page/' . $nextPage . '" data-gpage="' . esc_html( $nextPage ) . '">' . esc_html__( 'Next', 'templateberg' ) . '</a></li>';
281 $phtml .= '</ul></nav></div>';
282 return $phtml;
283 }
284
285 }
286
287 /**
288 * Function to get Current Theme Info
289 *
290 * @param [array] $attr
291 * @return array
292 */
293 if ( ! function_exists( 'templateberg_get_current_theme_info' ) ) {
294 function templateberg_get_current_theme_info() {
295 return array(
296 'template' => get_template(),
297 'stylesheet' => get_stylesheet(),
298 'author' => wp_get_theme()->get( 'Author' ),
299 );
300 }
301 }
302
303 /**
304 * Function to get Current Theme Button
305 *
306 * @param [array] $attr
307 * @return boolean
308 */
309 if ( ! function_exists( 'templateberg_is_current_theme_template_available' ) ) {
310 function templateberg_is_current_theme_template_available( $item ) {
311 $is_available = false;
312 if ( ! isset( $item['is_pro'] ) || ! $item['is_pro'] ) {
313 $is_available = true;
314 } elseif ( empty( templateberg_connect()->get_current_theme_purchase_templates_id() ) ) {
315 $is_available = false;
316 } elseif ( in_array( $item['id'], templateberg_connect()->get_current_theme_purchase_templates_id() ) ) {
317 $is_available = true;
318 }
319 return $is_available;
320 }
321 }
322
323 /**
324 * Function check if templates has set up
325 *
326 * @param [array] $attr
327 * @return boolean
328 */
329 if ( ! function_exists( 'templateberg_has_templates' ) ) {
330 function templateberg_has_templates( $type, $theme_slug = '' ) {
331 $is_available = false;
332
333 switch ( $type ) {
334 case 'current-theme':
335 $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
336 if ( $theme_slug ) {
337 $theme = $theme_slug;
338 } else {
339 $theme = get_stylesheet();
340 }
341 $themes_list = templateberg_get_current_theme_templates( $paged, $theme );
342 if ( $themes_list && 'nothing' !== $themes_list ) {
343 $is_available = true;
344 }
345 break;
346
347 case 'available-themes':
348 $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
349 $themes_list = templateberg_get_theme_templates( $paged );
350 if ( $themes_list ) {
351 $is_available = true;
352 }
353 break;
354
355 default:
356 $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
357 $templates_list = templateberg_get_gutenberg_templates( $paged );
358 if ( $templates_list ) {
359 $is_available = true;
360 }
361 break;
362 }
363
364 return $is_available;
365 }
366 }
367
368 /**
369 * Function set gutenberg templates
370 *
371 * @param [array] $attr
372 * @return boolean
373 */
374 if ( ! function_exists( 'templateberg_set_gutenberg_templates' ) ) {
375 function templateberg_set_gutenberg_templates( $templates_list, $paged = 1 ) {
376 $is_set = false;
377 $message = array();
378 if ( set_transient( 'templateberg_gt_' . $paged, $templates_list, WEEK_IN_SECONDS ) !== true ) {
379 global $wp_filesystem;
380 if ( ! $wp_filesystem ) {
381 require_once ABSPATH . 'wp-admin' . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'file.php';
382 }
383 $upload_dir = wp_upload_dir();
384 $dir = trailingslashit( $upload_dir['basedir'] ) . 'templateberg' . DIRECTORY_SEPARATOR;
385
386 WP_Filesystem();
387 if ( ! $wp_filesystem->is_dir( $dir ) ) {
388 $message[] = $dir . __( ' not exists', 'templateberg' );
389 if ( $wp_filesystem->mkdir( $dir ) ) {
390 $message[] = $dir . __( ' created', 'templateberg' );
391 } else {
392 $message[] = $dir . __( ' create permission issue', 'templateberg' );
393 }
394 } else {
395 $message[] = $dir . __( ' exists', 'templateberg' );
396 }
397 $templates_list = wp_json_encode( $templates_list );
398 if ( $wp_filesystem->put_contents( $dir . 'gt-' . $paged . '.json', $templates_list, 0644 ) ) {
399 $is_set = true;
400 $message[] = __( 'Successfully created file ', 'templateberg' ) . 'gt-' . $paged . '.json';
401 } else {
402 $message[] = __( 'Permission denied to create file ', 'templateberg' ) . 'gt-' . $paged . '.json';
403 }
404 } else {
405 $is_set = true;
406 }
407 return $is_set;
408 }
409 }
410
411 /**
412 * Function get gutenberg templates
413 *
414 * @param [array] $attr
415 * @return boolean
416 */
417 if ( ! function_exists( 'templateberg_get_gutenberg_templates' ) ) {
418 function templateberg_get_gutenberg_templates( $paged = 1 ) {
419 $templates_list = get_transient( 'templateberg_gt_' . $paged );
420 if ( ! $templates_list ) {
421 $upload_dir = wp_upload_dir();
422 $file_dir = $upload_dir['basedir'] . '/templateberg/' . 'gt-' . $paged . '.json';
423 if ( file_exists( $file_dir ) ) {
424 $file_url = $upload_dir['baseurl'] . '/templateberg/' . 'gt-' . $paged . '.json';
425 $body_args = array(
426 /*API version*/
427 'api_version' => TEMPLATEBERG_VERSION,
428 /*lang*/
429 'site_lang' => get_bloginfo( 'language' ),
430 );
431 $raw_json = wp_safe_remote_get(
432 $file_url,
433 array(
434 'timeout' => 100,
435 'body' => $body_args,
436 )
437 );
438 if ( ! is_wp_error( $raw_json ) ) {
439 $templates_list = json_decode( wp_remote_retrieve_body( $raw_json ), true );
440 } else {
441 $templates_list = false;
442 }
443 }
444 }
445 return $templates_list;
446 }
447 }
448
449 /**
450 * Function set editor templates
451 *
452 * @param [array] $templates_list
453 * @param [int] $paged
454 * @return boolean
455 */
456 if ( ! function_exists( 'templateberg_set_editor_templates' ) ) {
457 function templateberg_set_editor_templates( $templates_list, $paged = 1 ) {
458 $is_set = false;
459 $message = array();
460 ob_start();
461 $is_transient_saved = set_transient( 'templateberg_edt_' . $paged, $templates_list, WEEK_IN_SECONDS );
462 ob_clean();
463 if ( $is_transient_saved !== true ) {
464 global $wp_filesystem;
465 if ( ! $wp_filesystem ) {
466 require_once ABSPATH . 'wp-admin' . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'file.php';
467 }
468 $upload_dir = wp_upload_dir();
469 $dir = trailingslashit( $upload_dir['basedir'] ) . 'templateberg' . DIRECTORY_SEPARATOR;
470
471 WP_Filesystem();
472 if ( ! $wp_filesystem->is_dir( $dir ) ) {
473 $message[] = $dir . __( ' not exists', 'templateberg' );
474 if ( $wp_filesystem->mkdir( $dir ) ) {
475 $message[] = $dir . __( ' created', 'templateberg' );
476 } else {
477 $message[] = $dir . __( ' create permission issue', 'templateberg' );
478 }
479 } else {
480 $message[] = $dir . __( ' exists', 'templateberg' );
481 }
482 $templates_list = wp_json_encode( $templates_list );
483 if ( $wp_filesystem->put_contents( $dir . 'edt-' . $paged . '.json', $templates_list, 0644 ) ) {
484 $is_set = true;
485 $message[] = __( 'Successfully created file ', 'templateberg' ) . 'edt-' . $paged . '.json';
486 } else {
487 $message[] = __( 'Permission denied to create file ', 'templateberg' ) . 'edt-' . $paged . '.json';
488 }
489 } else {
490 $is_set = true;
491 }
492 return $is_set;
493 }
494 }
495
496 /**
497 * Function get editor templates
498 *
499 * @param [int] $paged
500 * @return array
501 */
502 if ( ! function_exists( 'templateberg_get_editor_templates' ) ) {
503 function templateberg_get_editor_templates( $paged = 1 ) {
504 $templates_list = get_transient( 'templateberg_edt_' . $paged );
505 if ( ! $templates_list ) {
506 $upload_dir = wp_upload_dir();
507 $file_dir = $upload_dir['basedir'] . '/templateberg/' . 'edt-' . $paged . '.json';
508 if ( file_exists( $file_dir ) ) {
509 $file_url = $upload_dir['baseurl'] . '/templateberg/' . 'edt-' . $paged . '.json';
510 $body_args = array(
511 /*API version*/
512 'api_version' => TEMPLATEBERG_VERSION,
513 /*lang*/
514 'site_lang' => get_bloginfo( 'language' ),
515 );
516 $raw_json = wp_safe_remote_get(
517 $file_url,
518 array(
519 'timeout' => 100,
520 'body' => $body_args,
521 )
522 );
523 if ( ! is_wp_error( $raw_json ) ) {
524 $templates_list = json_decode( wp_remote_retrieve_body( $raw_json ), true );
525 } else {
526 $templates_list = false;
527 }
528 }
529 }
530 return $templates_list;
531 }
532 }
533
534 /**
535 * Function set theme templates
536 *
537 * @param [array] $templates_list
538 * @param [int] $paged
539 * @return boolean
540 */
541 if ( ! function_exists( 'templateberg_set_theme_templates' ) ) {
542 function templateberg_set_theme_templates( $templates_list, $paged = 1 ) {
543 $is_set = false;
544 $message = array();
545 if ( set_transient( 'templateberg_tt_' . $paged, $templates_list, 'WEEK_IN_SECONDS' ) !== true ) {
546 global $wp_filesystem;
547 if ( ! $wp_filesystem ) {
548 require_once ABSPATH . 'wp-admin' . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'file.php';
549 }
550 $upload_dir = wp_upload_dir();
551 $dir = trailingslashit( $upload_dir['basedir'] ) . 'templateberg' . DIRECTORY_SEPARATOR;
552
553 WP_Filesystem();
554 if ( ! $wp_filesystem->is_dir( $dir ) ) {
555 $message[] = $dir . __( ' not exists', 'templateberg' );
556 if ( $wp_filesystem->mkdir( $dir ) ) {
557 $message[] = $dir . __( ' created', 'templateberg' );
558 } else {
559 $message[] = $dir . __( ' create permission issue', 'templateberg' );
560 }
561 } else {
562 $message[] = $dir . __( ' exists', 'templateberg' );
563 }
564 $templates_list = wp_json_encode( $templates_list );
565 if ( $wp_filesystem->put_contents( $dir . 'tt-' . $paged . '.json', $templates_list, 0644 ) ) {
566 $is_set = true;
567 $message[] = __( 'Successfully created file ', 'templateberg' ) . 'tt-' . $paged . '.json';
568 } else {
569 $message[] = __( 'Permission denied to create file ', 'templateberg' ) . 'tt-' . $paged . '.json';
570 }
571 } else {
572 $is_set = true;
573 }
574 return $is_set;
575 }
576 }
577
578 /**
579 * Function get editor templates
580 *
581 * @param [int] $paged
582 * @return array
583 */
584 if ( ! function_exists( 'templateberg_get_theme_templates' ) ) {
585 function templateberg_get_theme_templates( $paged = 1 ) {
586 $templates_list = get_transient( 'templateberg_tt_' . $paged );
587 if ( ! $templates_list ) {
588 $upload_dir = wp_upload_dir();
589 $file_dir = $upload_dir['basedir'] . '/templateberg/' . 'tt-' . $paged . '.json';
590 if ( file_exists( $file_dir ) ) {
591 $file_url = $upload_dir['baseurl'] . '/templateberg/' . 'tt-' . $paged . '.json';
592 $body_args = array(
593 /*API version*/
594 'api_version' => TEMPLATEBERG_VERSION,
595 /*lang*/
596 'site_lang' => get_bloginfo( 'language' ),
597 );
598 $raw_json = wp_safe_remote_get(
599 $file_url,
600 array(
601 'timeout' => 100,
602 'body' => $body_args,
603 )
604 );
605 if ( ! is_wp_error( $raw_json ) ) {
606 $templates_list = json_decode( wp_remote_retrieve_body( $raw_json ), true );
607 } else {
608 $templates_list = false;
609 }
610 }
611 }
612 return $templates_list;
613 }
614 }
615
616 /**
617 * Function set current theme templates
618 *
619 * @param [array] $templates_list
620 * @param [int] $paged
621 * @return boolean
622 */
623 if ( ! function_exists( 'templateberg_set_current_theme_templates' ) ) {
624 function templateberg_set_current_theme_templates( $templates_list, $paged = 1, $theme_slug = '' ) {
625 $is_set = false;
626 $message = array();
627 if ( $theme_slug ) {
628 $theme = $theme_slug;
629 } else {
630 $theme = get_stylesheet();
631 }
632 if ( set_transient( 'templateberg_ct_' . $theme . '_templates_' . $paged, $templates_list, 'WEEK_IN_SECONDS' ) !== true ) {
633 global $wp_filesystem;
634 if ( ! $wp_filesystem ) {
635 require_once ABSPATH . 'wp-admin' . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'file.php';
636 }
637 $upload_dir = wp_upload_dir();
638 $dir = trailingslashit( $upload_dir['basedir'] ) . 'templateberg' . DIRECTORY_SEPARATOR;
639
640 WP_Filesystem();
641 if ( ! $wp_filesystem->is_dir( $dir ) ) {
642 $message[] = $dir . __( ' not exists', 'templateberg' );
643 if ( $wp_filesystem->mkdir( $dir ) ) {
644 $message[] = $dir . __( ' created', 'templateberg' );
645 } else {
646 $message[] = $dir . __( ' create permission issue', 'templateberg' );
647 }
648 } else {
649 $message[] = $dir . __( ' exists', 'templateberg' );
650 }
651 $templates_list = wp_json_encode( $templates_list );
652 if ( $wp_filesystem->put_contents( $dir . 'ct-' . $theme . '-' . $paged . '.json', $templates_list, 0644 ) ) {
653 $is_set = true;
654 $message[] = __( 'Successfully created file ', 'templateberg' ) . 'ct-' . $theme . '-' . $paged . '.json';
655 } else {
656 $message[] = __( 'Permission denied to create file ', 'templateberg' ) . 'ct-' . $theme . '-' . $paged . '.json';
657 }
658 } else {
659 $is_set = true;
660 }
661 return $is_set;
662 }
663 }
664
665 /**
666 * Function get current theme templates
667 *
668 * @param [int] $paged
669 * @return array
670 */
671 if ( ! function_exists( 'templateberg_get_current_theme_templates' ) ) {
672 function templateberg_get_current_theme_templates( $paged = 1, $theme_slug = '' ) {
673 if ( $theme_slug ) {
674 $theme = $theme_slug;
675 } else {
676 $theme = get_stylesheet();
677 }
678
679 $templates_list = get_transient( 'templateberg_ct_' . $theme . '_templates_' . $paged );
680 if ( ! $templates_list ) {
681 $upload_dir = wp_upload_dir();
682 $file_dir = $upload_dir['basedir'] . '/templateberg/' . 'ct-' . $theme . '-' . $paged . '.json';
683 if ( file_exists( $file_dir ) ) {
684 $file_url = $upload_dir['baseurl'] . '/templateberg/' . 'ct-' . $theme . '-' . $paged . '.json';
685 $body_args = array(
686 /*API version*/
687 'api_version' => TEMPLATEBERG_VERSION,
688 /*lang*/
689 'site_lang' => get_bloginfo( 'language' ),
690 );
691 $raw_json = wp_safe_remote_get(
692 $file_url,
693 array(
694 'timeout' => 100,
695 'body' => $body_args,
696 )
697 );
698 if ( ! is_wp_error( $raw_json ) ) {
699 $templates_list = json_decode( wp_remote_retrieve_body( $raw_json ), true );
700 } else {
701 $templates_list = false;
702 }
703 }
704 }
705 return $templates_list;
706 }
707 }
708
709 /**
710 * Function check if templates has set up
711 *
712 * @param [array] $attr
713 * @return boolean
714 */
715 if ( ! function_exists( 'templateberg_current_theme_is_nothing' ) ) {
716 function templateberg_current_theme_is_nothing() {
717 $is_available = false;
718 $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
719 $theme = get_stylesheet();
720 $themes_list = templateberg_get_current_theme_templates( $paged, $theme );
721 if ( 'nothing' === $themes_list ) {
722 $is_available = true;
723 }
724 return $is_available;
725 }
726 }
727