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.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / helpers / TemplateImportHelper.php
tutor / helpers Last commit date
DateTimeHelper.php 1 year ago HttpHelper.php 1 year ago PluginInstaller.php 1 year ago QueryHelper.php 1 year ago SessionHelper.php 3 years ago TemplateImportHelper.php 1 year ago ValidationHelper.php 1 year ago
TemplateImportHelper.php
166 lines
1 <?php
2 /**
3 * TemplateImportHelper methods
4 *
5 * @package Tutor\Helpers
6 * @author Tutor <support@themeum.com>
7 * @link https://tutor.com
8 * @since 3.6.0
9 */
10
11 namespace Tutor\Helpers;
12
13 use Tutor\Traits\JsonResponse;
14
15 /**
16 * TemplateImportHelper methods
17 */
18 class TemplateImportHelper {
19
20 use JsonResponse;
21
22 /**
23 * Template list endpoint.
24 *
25 * @since 3.6.0
26 *
27 * @var string
28 */
29 public $template_list_endpoint;
30
31 /**
32 * Template download endpoint.
33 *
34 * @since 3.6.0
35 *
36 * @var string
37 */
38 public $template_download_endpoint;
39
40 /**
41 * $template_list_transient_key description.
42 *
43 * @since 3.6.0
44 *
45 * @var string
46 */
47 public $template_list_transient_key = 'tutor_template_list_cache';
48
49 /**
50 * Constructor.
51 *
52 * @since 3.6.0
53 *
54 * @return void
55 */
56 public function __construct() {
57 $this->template_list_endpoint = self::make_url( 'theme-templates' );
58 $this->template_download_endpoint = self::make_url( 'theme-template-download' );
59 }
60
61 /**
62 * Get base url.
63 *
64 * @since 3.6.0
65 *
66 * @return string The base URL for the template import API.
67 */
68 private static function get_base_url() {
69 $url = 'https://tutorlms.com/wp-json/themeum-products/v1/tutor';
70 if ( defined( 'TEMPLATE_IMPORT_BASE_URL' ) && TEMPLATE_IMPORT_BASE_URL ) {
71 $url = TEMPLATE_IMPORT_BASE_URL;
72 }
73
74 return $url;
75 }
76
77 /**
78 * Make url
79 *
80 * @since 3.6.0
81 *
82 * @param string $url_path url path.
83 *
84 * @return string full url.
85 */
86 public static function make_url( $url_path ) {
87 return self::get_base_url() . '/' . ltrim( $url_path, '/' );
88 }
89
90 /**
91 * Get Template list.
92 *
93 * @since 3.6.0
94 *
95 * @throws \Exception If there is an error fetching or decoding the templates.
96 */
97 public function get_template_list() {
98 $cached_template_list = get_transient( $this->template_list_transient_key );
99 if ( false !== $cached_template_list ) {
100 return $cached_template_list;
101 }
102
103 try {
104 $response = wp_remote_get(
105 $this->template_list_endpoint,
106 array(
107 'headers' => array(
108 'Secret-Key' => 't344d5d71sae7dcb546b8cf55e594808',
109 ),
110 )
111 );
112 $response_status_code = wp_remote_retrieve_response_code( $response );
113 if ( is_wp_error( $response ) ) {
114 throw new \Exception( 'Failed to fetch templates: ' . $response->get_error_message() );
115 }
116 $template_list = json_decode( wp_remote_retrieve_body( $response ), true );
117 if ( json_last_error() !== JSON_ERROR_NONE ) {
118 throw new \Exception( 'Failed to decode JSON response: ' . json_last_error_msg() );
119 }
120 if ( 200 !== $response_status_code ) {
121 throw new \Exception( 'Failed to fetch templates: ' . $template_list['response'] );
122 }
123 set_transient( $this->template_list_transient_key, $template_list['body_response'], HOUR_IN_SECONDS );
124 return $template_list['body_response'];
125 } catch ( \Exception $e ) {
126 return array();
127 }
128 }
129
130 /**
131 * Get Template download url
132 *
133 * @since 3.6.0
134 *
135 * @param string $template_id The ID of the template to download.
136 *
137 *
138 * return string The download URL for the specified template.
139 */
140 public function get_template_download_url( $template_id ) {
141 $tutor_license_info = get_option( 'tutor_license_info' );
142 $website_url = get_site_url();
143 $args = array(
144 'body' => array(
145 'slug' => $template_id,
146 'website_url' => $website_url,
147 'license_key' => $tutor_license_info['license_key'] ?? '',
148 ),
149 'headers' => array(
150 'Secret-Key' => 't344d5d71sae7dcb546b8cf55e594808',
151 ),
152 );
153 $response = wp_remote_post( $this->template_download_endpoint, $args );
154 $response_body = wp_remote_retrieve_body( $response );
155 $data = json_decode( $response_body, true );
156 if ( is_wp_error( $response ) ) {
157 self::json_response( $data['response'], null, 400 );
158 }
159 if ( empty( $data['body_response'] ) ) {
160 self::json_response( $data['response'], null, 400 );
161 }
162 $template_download_url = $data['body_response'];
163 return $template_download_url;
164 }
165 }
166