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 |