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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 9 months ago HttpHelper.php 11 months ago PluginInstaller.php 1 year ago QueryHelper.php 9 months ago SessionHelper.php 3 years ago TemplateImportHelper.php 11 months ago ValidationHelper.php 10 months ago
TemplateImportHelper.php
151 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 * Constructor.
42 *
43 * @since 3.6.0
44 *
45 * @return void
46 */
47 public function __construct() {
48 $this->template_list_endpoint = self::make_url( 'theme-templates' );
49 $this->template_download_endpoint = self::make_url( 'theme-template-download' );
50 }
51
52 /**
53 * Get base url.
54 *
55 * @since 3.6.0
56 *
57 * @return string The base URL for the template import API.
58 */
59 private static function get_base_url() {
60 $url = 'https://tutorlms.com/wp-json/themeum-products/v1/tutor';
61 if ( defined( 'TEMPLATE_IMPORT_BASE_URL' ) && TEMPLATE_IMPORT_BASE_URL ) {
62 $url = TEMPLATE_IMPORT_BASE_URL;
63 }
64
65 return $url;
66 }
67
68 /**
69 * Make url
70 *
71 * @since 3.6.0
72 *
73 * @param string $url_path url path.
74 *
75 * @return string full url.
76 */
77 public static function make_url( $url_path ) {
78 return self::get_base_url() . '/' . ltrim( $url_path, '/' );
79 }
80
81 /**
82 * Get Template list.
83 *
84 * @since 3.6.0
85 *
86 * @throws \Exception If there is an error fetching or decoding the templates.
87 */
88 public function get_template_list() {
89 try {
90 $response = wp_remote_get(
91 $this->template_list_endpoint,
92 array(
93 'headers' => array(
94 'Secret-Key' => 't344d5d71sae7dcb546b8cf55e594808',
95 ),
96 )
97 );
98 $response_status_code = wp_remote_retrieve_response_code( $response );
99 if ( is_wp_error( $response ) ) {
100 throw new \Exception( 'Failed to fetch templates: ' . $response->get_error_message() );
101 }
102 $template_list = json_decode( wp_remote_retrieve_body( $response ), true );
103 if ( json_last_error() !== JSON_ERROR_NONE ) {
104 throw new \Exception( 'Failed to decode JSON response: ' . json_last_error_msg() );
105 }
106 if ( 200 !== $response_status_code ) {
107 throw new \Exception( 'Failed to fetch templates: ' . $template_list['response'] );
108 }
109 return $template_list['body_response'];
110 } catch ( \Exception $e ) {
111 return array();
112 }
113 }
114
115 /**
116 * Get Template download url
117 *
118 * @since 3.6.0
119 *
120 * @param string $template_id The ID of the template to download.
121 *
122 *
123 * return string The download URL for the specified template.
124 */
125 public function get_template_download_url( $template_id ) {
126 $tutor_license_info = get_option( 'tutor_license_info' );
127 $website_url = get_site_url();
128 $args = array(
129 'body' => array(
130 'slug' => $template_id,
131 'website_url' => $website_url,
132 'license_key' => $tutor_license_info['license_key'] ?? '',
133 ),
134 'headers' => array(
135 'Secret-Key' => 't344d5d71sae7dcb546b8cf55e594808',
136 ),
137 );
138 $response = wp_remote_post( $this->template_download_endpoint, $args );
139 $response_body = wp_remote_retrieve_body( $response );
140 $data = json_decode( $response_body, true );
141 if ( is_wp_error( $response ) ) {
142 self::json_response( $data['response'], null, 400 );
143 }
144 if ( empty( $data['body_response'] ) ) {
145 self::json_response( $data['response'], null, 400 );
146 }
147 $template_download_url = $data['body_response'];
148 return $template_download_url;
149 }
150 }
151