PluginProbe
Tutor LMS – eLearning and online course solution / 3.9.2
Tutor LMS – eLearning and online course solution v3.9.2
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 3.9.15 3.9.14 3.9.13 3.9.12 3.9.11 trunk 1.0.0 1.0.0-alpha 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 All 191 releases
tutor / helpers / TemplateImportHelper.php

TemplateImportHelper.php in Tutor LMS – eLearning and online course solution 3.9.2, at helpers/TemplateImportHelper.php

151 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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