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.0 3.6.0 3.6.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 / template-import / TemplateImporter.php
tutor / template-import Last commit date
TemplateImportInit.php 7 months ago TemplateImporter.php 7 months ago
TemplateImporter.php
243 lines
1 <?php
2 /**
3 * Template importer
4 *
5 * @package Tutor\TemplateImporter
6 * @author Themeum <support@themeum.com>
7 * @link https://themeum.com
8 * @since 3.9.2
9 */
10
11 namespace Tutor\TemplateImport;
12
13 use AllowDynamicProperties;
14 use Droip\ExportImport\TemplateImport;
15 use Tutor\Helpers\TemplateImportHelper;
16 use TUTOR\Input;
17 use Tutor\Traits\JsonResponse;
18
19 defined( 'ABSPATH' ) || exit;
20
21 /**
22 * Template import handler class
23 *
24 * @since 3.9.2
25 */
26 #[AllowDynamicProperties]
27 class TemplateImporter {
28
29 use JsonResponse;
30
31 /**
32 * Template dependency endpoint
33 *
34 * @var string
35 */
36 public $template_import_dependency_api;
37
38 /**
39 * Register default hooks and actions for WordPress
40 *
41 * @since 3.9.2
42 *
43 * @param TemplateImportHelper $helper Template helper.
44 */
45 public function __construct( TemplateImportHelper $helper ) {
46 $this->template_helper_cls = $helper;
47 $this->template_import_dependency_api = $this->template_helper_cls->make_url( 'template-import-dependencies' );
48
49 add_action( 'wp_ajax_tutor_template_required_plugin_install', array( $this, 'tutor_template_required_plugin_install' ) );
50 add_action( 'wp_ajax_import_droip_template', array( $this, 'import_droip_template' ) );
51 add_action( 'wp_ajax_process_droip_template', array( $this, 'process_droip_template' ) );
52 add_action( 'wp_ajax_tutor_template_import_list', array( $this, 'tutor_template_import_list' ) );
53 }
54
55 /**
56 * AJAX callback to install a plugin.
57 *
58 * @since 3.9.2
59 *
60 * @return array response array
61 */
62 public function tutor_template_required_plugin_install() {
63 if ( current_user_can( 'manage_options' ) === false ) {
64 return $this->json_response( __( 'Permission denied!', 'tutor' ), array(), 400 );
65 }
66
67 tutor_utils()->check_nonce();
68 $plugin_name = Input::post( 'plugin_name' );
69 try {
70 $required_plugins = $this->template_dependency();
71 $plugin_info = $required_plugins[ $plugin_name ];
72 if ( empty( $plugin_info ) ) {
73 return $this->json_response( __( 'Required plugin info missing!', 'tutor' ), array(), 400 );
74 }
75 $this->installing_plugin( $plugin_info );
76 } catch ( \Throwable $th ) {
77 return $this->json_response( __( 'Something went wrong!', 'tutor' ), array(), 400 );
78 }
79 }
80
81 /**
82 * Template Dependency
83 *
84 * @since 3.9.2
85 *
86 * @return array
87 */
88 public function template_dependency() {
89 $dependent_plugins = array();
90 $response = wp_remote_get(
91 $this->template_import_dependency_api,
92 array(
93 'headers' => array(
94 'Secret-Key' => 't344d5d71sae7dcb546b8cf55e594808',
95 ),
96 )
97 );
98 if ( is_wp_error( $response ) ) {
99 return array();
100 }
101
102 $body = wp_remote_retrieve_body( $response );
103 $data = json_decode( $body, true );
104
105 if ( ! empty( $data ) && 200 === $data['status'] ) {
106 $dependent_plugins = $data['body_response'] ?? array();
107 }
108
109 return $dependent_plugins;
110 }
111
112 /**
113 * Install plugin.
114 *
115 * @param array $plugin_info installed plugin details.
116 *
117 * @since 3.9.2
118 *
119 * @return array
120 */
121 public function installing_plugin( $plugin_info ) {
122 try {
123 if ( ! class_exists( 'WP_Upgrader' ) ) {
124 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
125 }
126 if ( 'plugin' === $plugin_info['type'] && ! empty( $plugin_info['src'] ) ) {
127 if ( ! function_exists( 'plugins_api' ) ) {
128 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
129 }
130
131 $is_install_plugin = $this->is_plugin_installed( $plugin_info['path'] );
132 if ( ! $is_install_plugin ) {
133 $upgrader = new \Plugin_Upgrader( new \WP_Ajax_Upgrader_Skin() );
134
135 $installed = $upgrader->install( $plugin_info['src'] );
136 if ( is_wp_error( $installed ) ) {
137 return $this->json_response( __( 'Plugin installation error!', 'tutor' ), array(), 400 );
138 }
139 }
140
141 $activate = activate_plugin( $plugin_info['path'], '', false, false );
142 return $this->json_response( __( 'Plugin installed successfully!', 'tutor' ) );
143 } elseif ( 'theme' === $plugin_info['type'] && ! empty( $plugin_info['src'] ) ) {
144 require_once ABSPATH . 'wp-admin/includes/theme-install.php';
145
146 $is_theme_installed = wp_get_theme( $plugin_info['slug'] )->exists();
147
148 if ( ! $is_theme_installed ) {
149 $upgrader = new \Theme_Upgrader( new \WP_Ajax_Upgrader_Skin() );
150
151 $installed = $upgrader->install( $plugin_info['src'] );
152 if ( is_wp_error( $installed ) ) {
153 return $this->json_response( __( 'Theme installation error!', 'tutor' ), array(), 400 );
154 }
155 }
156 switch_theme( $plugin_info['slug'] );
157 if ( wp_get_theme()->get_stylesheet() !== $plugin_info['slug'] ) {
158 return $this->json_response( __( 'Error: while activating theme!', 'tutor' ), array(), 400 );
159 }
160
161 return $this->json_response( __( 'Theme installed and activated successfully.', 'tutor' ) );
162 } else {
163 return $this->json_response( __( 'Plugin or theme nothing installed!', 'tutor' ) );
164 }
165 } catch ( \Throwable $th ) {
166 return $this->json_response( __( 'Something went wrong!', 'tutor' ), array(), 400 );
167 }
168 }
169
170 /**
171 * Add comment or reply
172 *
173 * @since 3.9.2
174 *
175 * @return Array
176 */
177 public function import_droip_template() {
178 try {
179 if ( current_user_can( 'manage_options' ) === false ) {
180 return self::json_response( __( 'Permission denied!', 'tutor' ), null, 400 );
181 }
182 tutor_utils()->check_nonce();
183 $template_id = Input::post( 'template_id' );
184 $selected_mode = Input::post( 'selected_mode' );
185 $template_to_download = $this->template_helper_cls->get_template_download_url( $template_id );
186 $template_import = new TemplateImport();
187 $is_import = $template_import->import( $template_to_download, true, $selected_mode );
188
189 if ( $is_import ) {
190 return self::json_response( __( 'Content imported', 'tutor' ), null, 200 );
191 } else {
192 return self::json_response( __( 'Content importing error!', 'tutor' ), null, 400 );
193 }
194 } catch ( \Throwable $th ) {
195 return self::json_response( __( 'Something went wrong!', 'tutor' ), null, 400 );
196 }
197 }
198
199 /**
200 * Process_droip_template description
201 *
202 * @since 3.9.2
203 *
204 * @return array description
205 */
206 public function process_droip_template() {
207 $template_import = new TemplateImport();
208 $is_process = $template_import->process();
209 return self::json_response( '', $is_process, 200 );
210 }
211
212 /**
213 * Check plugin is install or not
214 *
215 * @param string $plugin_path plugin-slug.
216 *
217 * @since 3.9.2
218 *
219 * @return bool
220 */
221 private function is_plugin_installed( $plugin_path ) {
222 $installed_plugins = get_plugins();
223 foreach ( $installed_plugins as $plugin_file => $plugin_data ) {
224 if ( $plugin_path === $plugin_file ) {
225 return true;
226 }
227 }
228 return false;
229 }
230
231 /**
232 * Get Template list.
233 *
234 * @since 3.9.2
235 */
236 public function tutor_template_import_list() {
237 ob_start();
238 require_once tutor()->path . 'views/templates/templates-list.php';
239 $contents = ob_get_clean();
240 $this->json_response( __( 'Successfully fetched!', 'tutor' ), $contents );
241 }
242 }
243