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 |