| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
|---|
| 4 | exit; // Exit if accessed directly. |
|---|
| 5 | } |
|---|
| 6 | |
|---|
| 7 | use WprAddons\Admin\Templates\WPR_Templates_Data; |
|---|
| 8 | use WprAddons\Classes\Utilities; |
|---|
| 9 | use Elementor\Plugin; |
|---|
| 10 | |
|---|
| 11 | // Register Menus |
|---|
| 12 | function wpr_addons_add_templates_kit_menu() { |
|---|
| 13 | add_submenu_page( 'wpr-addons', 'Templates Kit', 'Templates Kit', 'manage_options', 'wpr-templates-kit', 'wpr_addons_templates_kit_page' ); |
|---|
| 14 | } |
|---|
| 15 | add_action( 'admin_menu', 'wpr_addons_add_templates_kit_menu' ); |
|---|
| 16 | |
|---|
| 17 | // Import Template Kit |
|---|
| 18 | add_action( 'wp_ajax_wpr_activate_required_theme', 'wpr_activate_required_theme' ); |
|---|
| 19 | add_action( 'wp_ajax_wpr_activate_required_plugins', 'wpr_activate_required_plugins' ); |
|---|
| 20 | add_action( 'wp_ajax_wpr_fix_royal_compatibility', 'wpr_fix_royal_compatibility' ); |
|---|
| 21 | add_action( 'wp_ajax_wpr_reset_previous_import', 'wpr_reset_previous_import' ); |
|---|
| 22 | add_action( 'wp_ajax_wpr_import_templates_kit', 'wpr_import_templates_kit' ); |
|---|
| 23 | add_action( 'wp_ajax_wpr_final_settings_setup', 'wpr_final_settings_setup' ); |
|---|
| 24 | add_action( 'wp_ajax_wpr_search_query_results', 'wpr_search_query_results' ); |
|---|
| 25 | add_action( 'init', 'disable_default_woo_pages_creation', 2 ); |
|---|
| 26 | |
|---|
| 27 | // Set Image Timeout |
|---|
| 28 | if ( version_compare( get_bloginfo( 'version' ), '5.1.0', '>=' ) ) { |
|---|
| 29 | add_filter( 'http_request_timeout', 'set_image_request_timeout', 10, 2 ); |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | ** Render Templates Kit Page |
|---|
| 34 | */ |
|---|
| 35 | function wpr_addons_templates_kit_page() { |
|---|
| 36 | |
|---|
| 37 | ?> |
|---|
| 38 | |
|---|
| 39 | <div class="wpr-templates-kit-page"> |
|---|
| 40 | |
|---|
| 41 | <header> |
|---|
| 42 | <div class="wpr-templates-kit-logo"> |
|---|
| 43 | <div><img src="<?php echo !empty(get_option('wpr_wl_plugin_logo')) ? esc_url(wp_get_attachment_image_src(get_option('wpr_wl_plugin_logo'), 'full')[0]) : esc_url(WPR_ADDONS_ASSETS_URL .'img/logo-40x40.png'); ?>"></div> |
|---|
| 44 | <div class="back-btn"><?php printf( esc_html__('%s Back to Library', 'wpr-addons'), '<span class="dashicons dashicons-arrow-left-alt2"></span>'); ?></div> |
|---|
| 45 | </div> |
|---|
| 46 | |
|---|
| 47 | <div class="wpr-templates-kit-search"> |
|---|
| 48 | <input type="text" autocomplete="off" placeholder="<?php esc_html_e('Search Templates Kit...', 'wpr-addons'); ?>"> |
|---|
| 49 | <span class="dashicons dashicons-search"></span> |
|---|
| 50 | </div> |
|---|
| 51 | |
|---|
| 52 | <div class="wpr-templates-kit-price-filter"> |
|---|
| 53 | <span data-price="mixed"><?php esc_html_e('Price: Mixed', 'wpr-addons'); ?></span> |
|---|
| 54 | <span class="dashicons dashicons-arrow-down-alt2"></span> |
|---|
| 55 | <ul> |
|---|
| 56 | <li><?php esc_html_e('Mixed', 'wpr-addons'); ?></li> |
|---|
| 57 | <li><?php esc_html_e('Free', 'wpr-addons'); ?></li> |
|---|
| 58 | <li><?php esc_html_e('Premium', 'wpr-addons'); ?></li> |
|---|
| 59 | </ul> |
|---|
| 60 | </div> |
|---|
| 61 | |
|---|
| 62 | <div class="wpr-templates-kit-filters"> |
|---|
| 63 | <div>Filter: All</div> |
|---|
| 64 | <ul> |
|---|
| 65 | <li data-filter="all">Blog</li> |
|---|
| 66 | <li data-filter="blog">Blog</li> |
|---|
| 67 | <li data-filter="business">Business</li> |
|---|
| 68 | <li data-filter="ecommerce">eCommerce</li> |
|---|
| 69 | <li data-filter="beauty">Beauty</li> |
|---|
| 70 | </ul> |
|---|
| 71 | </div> |
|---|
| 72 | </header> |
|---|
| 73 | |
|---|
| 74 | <div class="wpr-templates-kit-page-title"> |
|---|
| 75 | <h1><?php esc_html_e('Royal Elementor Templates Kit', 'wpr-addons'); ?></h1> |
|---|
| 76 | <p><?php esc_html_e('Import any Templates Kit with just a Single click', 'wpr-addons'); ?></p> |
|---|
| 77 | <p> |
|---|
| 78 | <a href="https://www.youtube.com/watch?v=kl2xBoWW81o" class="wpr-options-button button" target="_blank"> |
|---|
| 79 | <?php esc_html_e('Video Tutorial', 'wpr-addons'); ?> |
|---|
| 80 | <span class="dashicons dashicons-video-alt3"></span> |
|---|
| 81 | </a> |
|---|
| 82 | </p> |
|---|
| 83 | </div> |
|---|
| 84 | |
|---|
| 85 | <div class="wpr-templates-kit-grid main-grid" data-theme-status="<?php echo esc_attr(get_theme_status()); ?>"> |
|---|
| 86 | <?php |
|---|
| 87 | $kits = WPR_Templates_Data::get_available_kits(); |
|---|
| 88 | $sorted_kits = []; |
|---|
| 89 | |
|---|
| 90 | foreach ($kits as $slug => $kit) { |
|---|
| 91 | foreach ($kit as $version => $data ) { |
|---|
| 92 | $sorted_kits[$slug .'-'. $version] = $data; |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | // Sort by Priority |
|---|
| 97 | uasort($sorted_kits, function ($item1, $item2) { |
|---|
| 98 | if ($item1['priority'] == $item2['priority']) return 0; |
|---|
| 99 | return $item1['priority'] < $item2['priority'] ? -1 : 1; |
|---|
| 100 | }); |
|---|
| 101 | |
|---|
| 102 | // Loop |
|---|
| 103 | foreach ($sorted_kits as $kit_id => $data) { |
|---|
| 104 | echo '<div class="grid-item" data-kit-id="'. esc_attr($kit_id) .'" data-tags="'. esc_attr($data['tags']) .'" data-plugins="'. esc_attr($data['plugins']) .'" data-pages="'. esc_attr($data['pages']) .'" data-price="'. esc_attr($data['price']) .'">'; |
|---|
| 105 | echo '<div class="image-wrap">'; |
|---|
| 106 | echo '<img src="'. esc_url('https://royal-elementor-addons.com/library/templates-kit/'. $kit_id .'/home.jpg') .'">'; |
|---|
| 107 | echo '<div class="image-overlay"><span class="dashicons dashicons-search"></span></div>'; |
|---|
| 108 | echo '</div>'; |
|---|
| 109 | echo '<footer>'; |
|---|
| 110 | echo '<h3>'. esc_html($data['name']) .'</h3>'; |
|---|
| 111 | if ( $data['woo-builder'] ) { |
|---|
| 112 | echo '<span class="wpr-woo-builder-label">'. esc_html__( 'Woo Builder', 'wpr-addons' ) .'</span>'; |
|---|
| 113 | } elseif ( $data['theme-builder'] ) { |
|---|
| 114 | echo '<span class="wpr-theme-builder-label">'. esc_html__( 'Theme Builder', 'wpr-addons' ) .'</span>'; |
|---|
| 115 | } |
|---|
| 116 | echo '</footer>'; |
|---|
| 117 | echo '</div>'; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | ?> |
|---|
| 121 | |
|---|
| 122 | </div> |
|---|
| 123 | |
|---|
| 124 | <div class="wpr-templates-kit-single"> |
|---|
| 125 | <div class="wpr-templates-kit-grid single-grid"></div> |
|---|
| 126 | |
|---|
| 127 | <footer class="action-buttons-wrap"> |
|---|
| 128 | <a href="https://royal-elementor-addons.com/" class="preview-demo button" target="_blank"><?php esc_html_e('Preview Demo', 'wpr-addons'); ?> <span class="dashicons dashicons-external"></span></a> |
|---|
| 129 | |
|---|
| 130 | <div class="import-template-buttons"> |
|---|
| 131 | <?php |
|---|
| 132 | echo '<button class="import-kit button">'. esc_html__('Import Templates Kit', 'wpr-addons') .' <span class="dashicons dashicons-download"></span></button>'; |
|---|
| 133 | echo '<a href="https://royal-elementor-addons.com/?ref=rea-plugin-backend-templates-upgrade-pro#purchasepro" class="get-access button" target="_blank">'. esc_html__('Get Access', 'wpr-addons') .' <span class="dashicons dashicons-external"></span></a>'; |
|---|
| 134 | ?> |
|---|
| 135 | <button class="import-template button"><?php printf( esc_html__( 'Import %s Template', 'wpr-addons' ), '<strong></strong>' ); ?></button> |
|---|
| 136 | |
|---|
| 137 | </div> |
|---|
| 138 | </footer> |
|---|
| 139 | </div> |
|---|
| 140 | |
|---|
| 141 | <div class="wpr-import-kit-popup-wrap"> |
|---|
| 142 | <div class="overlay"></div> |
|---|
| 143 | <div class="wpr-import-kit-popup"> |
|---|
| 144 | <header> |
|---|
| 145 | <h3><?php esc_html_e('Template Kit is being imported', 'wpr-addons'); ?><span>.</span></h3> |
|---|
| 146 | <span class="dashicons dashicons-no-alt close-btn"></span> |
|---|
| 147 | </header> |
|---|
| 148 | <div class="content"> |
|---|
| 149 | <p><?php esc_html_e('The import process can take a few seconds depending on the size of the kit you are importing and speed of the connection.', 'wpr-addons'); ?></p> |
|---|
| 150 | <p><?php esc_html_e('Please do NOT close this browser window until import is completed.', 'wpr-addons'); ?></p> |
|---|
| 151 | |
|---|
| 152 | <div class="progress-wrap"> |
|---|
| 153 | <div class="progress-bar"></div> |
|---|
| 154 | <strong></strong> |
|---|
| 155 | </div> |
|---|
| 156 | |
|---|
| 157 | <div class="wpr-import-help"> |
|---|
| 158 | <a href="https://royal-elementor-addons.com/contactus/?ref=rea-plugin-backend-templates-import-screen" target="_blank">Having trouble with template import? Get help <span class="dashicons dashicons-sos"></span></a> |
|---|
| 159 | </div> |
|---|
| 160 | </div> |
|---|
| 161 | </div> |
|---|
| 162 | </div> |
|---|
| 163 | |
|---|
| 164 | <div class="wpr-templates-kit-not-found"> |
|---|
| 165 | <img src="<?php echo esc_url(WPR_ADDONS_ASSETS_URL .'img/not-found.png'); ?>"> |
|---|
| 166 | <h1><?php esc_html_e('No Search Results Found.', 'wpr-addons'); ?></h1> |
|---|
| 167 | <p><?php esc_html_e('Cant find a Templates Kit you are looking for?', 'wpr-addons'); ?></p> |
|---|
| 168 | <a href="https://royal-elementor-addons.com/library/request-new-kit-red.html" target="_blank"><?php esc_html_e('Request Templates Kit.', 'wpr-addons'); ?></a> |
|---|
| 169 | </div> |
|---|
| 170 | |
|---|
| 171 | </div> |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | <?php |
|---|
| 175 | |
|---|
| 176 | } // End wpr_addons_templates_kit_page() |
|---|
| 177 | |
|---|
| 178 | /** |
|---|
| 179 | ** Get Theme Status |
|---|
| 180 | */ |
|---|
| 181 | function get_theme_status() { |
|---|
| 182 | $theme = wp_get_theme(); |
|---|
| 183 | |
|---|
| 184 | // Theme installed and activate. |
|---|
| 185 | if ( 'Royal Elementor Kit' === $theme->name || 'Royal Elementor Kit' === $theme->parent_theme ) { |
|---|
| 186 | return 'req-theme-active'; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | // Theme installed but not activate. |
|---|
| 190 | foreach ( (array) wp_get_themes() as $theme_dir => $theme ) { |
|---|
| 191 | if ( 'Royal Elementor Kit' === $theme->name || 'Royal Elementor Kit' === $theme->parent_theme ) { |
|---|
| 192 | return 'req-theme-inactive'; |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | return 'req-theme-not-installed'; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | /** |
|---|
| 200 | ** Install/Activate Required Theme |
|---|
| 201 | */ |
|---|
| 202 | function wpr_activate_required_theme() { |
|---|
| 203 | // Get Current Theme |
|---|
| 204 | $theme = get_option('stylesheet'); |
|---|
| 205 | |
|---|
| 206 | // Activate Royal Elementor Kit Theme |
|---|
| 207 | if ( 'ashe-pro-premium' !== $theme && 'bard-pro-premium' !== $theme |
|---|
| 208 | && 'vayne-pro-premium' !== $theme && 'kayn-pro-premium' !== $theme ) { |
|---|
| 209 | switch_theme( 'royal-elementor-kit' ); |
|---|
| 210 | set_transient( 'royal-elementor-kit_activation_notice', true ); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | // TODO: maybe return back - 'ashe' !== $theme && 'bard' !== $theme && |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | /** |
|---|
| 217 | ** Activate Required Plugins |
|---|
| 218 | */ |
|---|
| 219 | function wpr_activate_required_plugins() { |
|---|
| 220 | if ( isset($_POST['plugin']) ) { |
|---|
| 221 | if ( 'contact-form-7' == $_POST['plugin'] ) { |
|---|
| 222 | if ( !is_plugin_active( 'contact-form-7/wp-contact-form-7.php' ) ) { |
|---|
| 223 | activate_plugin( 'contact-form-7/wp-contact-form-7.php' ); |
|---|
| 224 | } |
|---|
| 225 | } elseif ( 'woocommerce' == $_POST['plugin'] ) { |
|---|
| 226 | if ( !is_plugin_active( 'woocommerce/woocommerce.php' ) ) { |
|---|
| 227 | activate_plugin( 'woocommerce/woocommerce.php' ); |
|---|
| 228 | } |
|---|
| 229 | } elseif ( 'media-library-assistant' == $_POST['plugin'] ) { |
|---|
| 230 | if ( !is_plugin_active( 'media-library-assistant/index.php' ) ) { |
|---|
| 231 | activate_plugin( 'media-library-assistant/index.php' ); |
|---|
| 232 | } |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | /** |
|---|
| 238 | ** Deactivate Extra Plugins |
|---|
| 239 | */ |
|---|
| 240 | function wpr_fix_royal_compatibility() { |
|---|
| 241 | // Get currently active plugins |
|---|
| 242 | $active_plugins = (array) get_option( 'active_plugins', array() ); |
|---|
| 243 | $active_plugins = array_values($active_plugins); |
|---|
| 244 | $required_plugins = [ |
|---|
| 245 | 'elementor/elementor.php', |
|---|
| 246 | 'royal-elementor-addons/wpr-addons.php', |
|---|
| 247 | 'royal-elementor-addons-pro/wpr-addons-pro.php', |
|---|
| 248 | 'wpr-addons-pro/wpr-addons-pro.php', |
|---|
| 249 | 'contact-form-7/wp-contact-form-7.php', |
|---|
| 250 | 'woocommerce/woocommerce.php', |
|---|
| 251 | 'really-simple-ssl/rlrsssl-really-simple-ssl.php', |
|---|
| 252 | 'wp-mail-smtp/wp_mail_smtp.php', |
|---|
| 253 | 'updraftplus/updraftplus.php', |
|---|
| 254 | 'temporary-login-without-password/temporary-login-without-password.php', |
|---|
| 255 | 'wp-reset/wp-reset.php' |
|---|
| 256 | ]; |
|---|
| 257 | |
|---|
| 258 | // Deactivate Extra Import Plugins |
|---|
| 259 | foreach ( $active_plugins as $key => $value ) { |
|---|
| 260 | if ( ! in_array($value, $required_plugins) ) { |
|---|
| 261 | $active_key = array_search($value, $active_plugins);; |
|---|
| 262 | unset($active_plugins[$active_key]); |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | // Set Active Plugins |
|---|
| 267 | update_option( 'active_plugins', array_values($active_plugins) ); |
|---|
| 268 | |
|---|
| 269 | // Get Current Theme |
|---|
| 270 | $theme = get_option('stylesheet'); |
|---|
| 271 | |
|---|
| 272 | // Activate Royal Elementor Kit Theme |
|---|
| 273 | if ( 'ashe-pro-premium' !== $theme && 'bard-pro-premium' !== $theme |
|---|
| 274 | && 'vayne-pro-premium' !== $theme && 'kayn-pro-premium' !== $theme ) { |
|---|
| 275 | switch_theme( 'royal-elementor-kit' ); |
|---|
| 276 | set_transient( 'royal-elementor-kit_activation_notice', true ); |
|---|
| 277 | } |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | /** |
|---|
| 281 | ** Import Template Kit |
|---|
| 282 | */ |
|---|
| 283 | function wpr_import_templates_kit() { |
|---|
| 284 | |
|---|
| 285 | // Temp Define Importers |
|---|
| 286 | if ( ! defined('WP_LOAD_IMPORTERS') ) { |
|---|
| 287 | define('WP_LOAD_IMPORTERS', true); |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | // Include if Class Does NOT Exist |
|---|
| 291 | if ( ! class_exists( 'WP_Import' ) ) { |
|---|
| 292 | $class_wp_importer = WPR_ADDONS_PATH .'admin/import/class-wordpress-importer.php'; |
|---|
| 293 | if ( file_exists( $class_wp_importer ) ) { |
|---|
| 294 | require $class_wp_importer; |
|---|
| 295 | } |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | if ( class_exists( 'WP_Import' ) ) { |
|---|
| 299 | $kit = isset($_POST['wpr_templates_kit']) ? sanitize_file_name(wp_unslash($_POST['wpr_templates_kit'])) : ''; |
|---|
| 300 | $file = isset($_POST['wpr_templates_kit_single']) ? sanitize_file_name(wp_unslash($_POST['wpr_templates_kit_single'])) : ''; |
|---|
| 301 | |
|---|
| 302 | // Tmp |
|---|
| 303 | update_option( 'wpr-import-kit-id', $kit ); |
|---|
| 304 | |
|---|
| 305 | // Regenerate Extra Image Sizes |
|---|
| 306 | add_filter( 'intermediate_image_sizes_advanced', 'disable_extra_image_sizes', 10, 3 ); |
|---|
| 307 | |
|---|
| 308 | // No Limit for Execution |
|---|
| 309 | set_time_limit(0); |
|---|
| 310 | |
|---|
| 311 | // Download Import File |
|---|
| 312 | $local_file_path = download_template( $kit, $file ); |
|---|
| 313 | |
|---|
| 314 | // Prepare for Import |
|---|
| 315 | $wp_import = new WP_Import( $local_file_path, ['fetch_attachments' => true] ); |
|---|
| 316 | |
|---|
| 317 | // Import |
|---|
| 318 | ob_start(); |
|---|
| 319 | $wp_import->run(); |
|---|
| 320 | ob_end_clean(); |
|---|
| 321 | |
|---|
| 322 | // Delete Import File |
|---|
| 323 | unlink( $local_file_path ); |
|---|
| 324 | |
|---|
| 325 | // Send to JS |
|---|
| 326 | echo esc_html(serialize( $wp_import )); |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | /** |
|---|
| 332 | ** Download Template |
|---|
| 333 | */ |
|---|
| 334 | function download_template( $kit, $file ) { |
|---|
| 335 | $file = ! $file ? 'main' : $file; |
|---|
| 336 | |
|---|
| 337 | // Avoid Cache |
|---|
| 338 | $randomNum = substr(str_shuffle("0123456789abcdefghijklmnopqrstvwxyzABCDEFGHIJKLMNOPQRSTVWXYZ"), 0, 7); |
|---|
| 339 | |
|---|
| 340 | // Remote and Local Files |
|---|
| 341 | $remote_file_url = 'https://royal-elementor-addons.com/library/templates-kit/'. $kit .'/main.xml?='. $randomNum; |
|---|
| 342 | |
|---|
| 343 | // If the function it's not available, require it. |
|---|
| 344 | if ( ! function_exists( 'download_url' ) ) { |
|---|
| 345 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | $tmp_file = download_url( $remote_file_url ); |
|---|
| 349 | |
|---|
| 350 | // WP Error. |
|---|
| 351 | if ( is_wp_error( $tmp_file ) ) { |
|---|
| 352 | // Fallback URL |
|---|
| 353 | $remote_file_url = 'https://mysitetutorial.com/library/templates-kit/'. $kit .'/main.xml?='. $randomNum; |
|---|
| 354 | $tmp_file = download_url( $remote_file_url ); |
|---|
| 355 | |
|---|
| 356 | if ( is_wp_error( $tmp_file ) ) { |
|---|
| 357 | wp_send_json_error([ |
|---|
| 358 | 'error' => esc_html__('Error: Import File download failed.', 'wpr-addons'), |
|---|
| 359 | 'help' => esc_html__('Please contact Customer Support and send this Error.', 'wpr-addons') |
|---|
| 360 | ]); |
|---|
| 361 | return false; |
|---|
| 362 | } |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | // Array based on $_FILE as seen in PHP file uploads. |
|---|
| 366 | $file_args = [ |
|---|
| 367 | 'name' => 'main.xml', |
|---|
| 368 | 'tmp_name' => $tmp_file, |
|---|
| 369 | 'error' => 0, |
|---|
| 370 | 'size' => filesize( $tmp_file ), |
|---|
| 371 | ]; |
|---|
| 372 | |
|---|
| 373 | $defaults = array( |
|---|
| 374 | 'test_form' => false, |
|---|
| 375 | 'test_size' => true, |
|---|
| 376 | 'test_upload' => true, |
|---|
| 377 | 'mimes' => [ |
|---|
| 378 | 'xml' => 'text/xml', |
|---|
| 379 | 'json' => 'text/plain', |
|---|
| 380 | ], |
|---|
| 381 | 'wp_handle_sideload' => 'upload', |
|---|
| 382 | ); |
|---|
| 383 | |
|---|
| 384 | // Move the temporary file into the uploads directory. |
|---|
| 385 | $local_file = wp_handle_sideload( $file_args, $defaults ); |
|---|
| 386 | |
|---|
| 387 | if ( isset( $local_file['error'] ) ) { |
|---|
| 388 | wp_send_json_error([ |
|---|
| 389 | 'error' => esc_html__('Error: Import File upload failed.', 'wpr-addons'), |
|---|
| 390 | 'help' => esc_html__('Please contact Customer Support and send this Error.', 'wpr-addons') |
|---|
| 391 | ]); |
|---|
| 392 | return false; |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | // Success. |
|---|
| 396 | return $local_file['file']; |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | /** |
|---|
| 400 | ** Reset Previous Import |
|---|
| 401 | */ |
|---|
| 402 | function wpr_reset_previous_import() { |
|---|
| 403 | $args = [ |
|---|
| 404 | 'post_type' => [ |
|---|
| 405 | 'page', |
|---|
| 406 | 'post', |
|---|
| 407 | 'product', |
|---|
| 408 | 'wpr_mega_menu', |
|---|
| 409 | 'wpr_templates', |
|---|
| 410 | 'elementor_library', |
|---|
| 411 | 'attachment' |
|---|
| 412 | ], |
|---|
| 413 | 'post_status' => 'any', |
|---|
| 414 | 'posts_per_page' => '-1', |
|---|
| 415 | 'meta_key' => '_wpr_demo_import_item' |
|---|
| 416 | ]; |
|---|
| 417 | |
|---|
| 418 | $imported_items = new WP_Query ( $args ); |
|---|
| 419 | |
|---|
| 420 | if ( $imported_items->have_posts() ) { |
|---|
| 421 | while ( $imported_items->have_posts() ) { |
|---|
| 422 | $imported_items->the_post(); |
|---|
| 423 | |
|---|
| 424 | // Dont Delete Elementor Kit |
|---|
| 425 | if ( 'Default Kit' == get_the_title() ) { |
|---|
| 426 | continue; |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | // Delete Posts |
|---|
| 430 | wp_delete_post( get_the_ID(), true ); |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | // Reset |
|---|
| 434 | wp_reset_query(); |
|---|
| 435 | |
|---|
| 436 | $imported_terms = get_terms([ |
|---|
| 437 | 'meta_key' => '_wpr_demo_import_item', |
|---|
| 438 | 'posts_per_page' => -1, |
|---|
| 439 | 'hide_empty' => false, |
|---|
| 440 | ]); |
|---|
| 441 | |
|---|
| 442 | if ( !empty($imported_terms) ) { |
|---|
| 443 | foreach( $imported_terms as $imported_term ) { |
|---|
| 444 | // Delete Terms |
|---|
| 445 | wp_delete_term( $imported_term->term_id, $imported_term->taxonomy ); |
|---|
| 446 | } |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | wp_send_json_success( esc_html__('Previous Import Files have been successfully Reset.', 'wpr-addons') ); |
|---|
| 450 | } else { |
|---|
| 451 | wp_send_json_success( esc_html__('There is no Data for Reset.', 'wpr-addons') ); |
|---|
| 452 | } |
|---|
| 453 | } |
|---|
| 454 | |
|---|
| 455 | /** |
|---|
| 456 | ** Import Elementor Site Settings |
|---|
| 457 | */ |
|---|
| 458 | function import_elementor_site_settings( $kit ) { |
|---|
| 459 | // Avoid Cache |
|---|
| 460 | // $randomNum = substr(str_shuffle("0123456789abcdefghijklmnopqrstvwxyzABCDEFGHIJKLMNOPQRSTVWXYZ"), 0, 7); |
|---|
| 461 | |
|---|
| 462 | // Get Remote File |
|---|
| 463 | $site_settings = @file_get_contents('https://royal-elementor-addons.com/library/templates-kit/'. $kit .'/site-settings.json'); |
|---|
| 464 | |
|---|
| 465 | if ( false !== $site_settings ) { |
|---|
| 466 | $site_settings = json_decode($site_settings, true); |
|---|
| 467 | |
|---|
| 468 | if ( ! empty($site_settings['settings']) ) { |
|---|
| 469 | $default_kit = \Elementor\Plugin::$instance->documents->get_doc_for_frontend( get_option( 'elementor_active_kit' ) ); |
|---|
| 470 | |
|---|
| 471 | $kit_settings = $default_kit->get_settings(); |
|---|
| 472 | $new_settings = $site_settings['settings']; |
|---|
| 473 | $settings = array_merge($kit_settings, $new_settings); |
|---|
| 474 | |
|---|
| 475 | $default_kit->save( [ 'settings' => $settings ] ); |
|---|
| 476 | } |
|---|
| 477 | } |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | /** |
|---|
| 481 | ** Setup WPR Templates |
|---|
| 482 | */ |
|---|
| 483 | function setup_wpr_templates( $kit ) { |
|---|
| 484 | $kit = isset($kit) ? sanitize_text_field(wp_unslash($kit)) : ''; |
|---|
| 485 | |
|---|
| 486 | // Check if kit has Theme Builder templates |
|---|
| 487 | $kit_name = substr($kit, 0, strripos($kit, '-v')); |
|---|
| 488 | $kit_version = substr($kit, (strripos($kit, '-v') + 1), strlen($kit)); |
|---|
| 489 | $get_available_kits = WPR_Templates_Data::get_available_kits(); |
|---|
| 490 | $has_theme_builder = $get_available_kits[$kit_name][$kit_version]['theme-builder']; |
|---|
| 491 | $has_woo_builder = $get_available_kits[$kit_name][$kit_version]['woo-builder']; |
|---|
| 492 | $has_off_canvas = $get_available_kits[$kit_name][$kit_version]['off-canvas']; |
|---|
| 493 | |
|---|
| 494 | // Set Home & Blog Pages |
|---|
| 495 | $home_page = get_page_by_path('home-'. $kit); |
|---|
| 496 | $blog_page = get_page_by_path('blog-'. $kit); |
|---|
| 497 | |
|---|
| 498 | if ( $home_page ) { |
|---|
| 499 | update_option( 'show_on_front', 'page' ); |
|---|
| 500 | update_option( 'page_on_front', $home_page->ID ); |
|---|
| 501 | |
|---|
| 502 | if ( $blog_page ) { |
|---|
| 503 | update_option( 'page_for_posts', $blog_page->ID ); |
|---|
| 504 | } |
|---|
| 505 | } |
|---|
| 506 | |
|---|
| 507 | // Set Headers and Footers |
|---|
| 508 | update_option('wpr_header_conditions', '{"user-header-'. $kit .'-header":["global"]}'); |
|---|
| 509 | update_post_meta( Utilities::get_template_id('user-header-'. $kit), 'wpr_header_show_on_canvas', 'true' ); |
|---|
| 510 | update_option('wpr_footer_conditions', '{"user-footer-'. $kit .'-footer":["global"]}'); |
|---|
| 511 | update_post_meta( Utilities::get_template_id('user-footer-'. $kit), 'wpr_footer_show_on_canvas', 'true' ); |
|---|
| 512 | |
|---|
| 513 | // Theme Builder |
|---|
| 514 | if ( $has_theme_builder ) { |
|---|
| 515 | update_option('wpr_archive_conditions', '{"user-archive-'. $kit .'-blog":["archive/posts"],"user-archive-'. $kit .'-author":["archive/author"],"user-archive-'. $kit .'-date":["archive/date"],"user-archive-'. $kit .'-category-tag":["archive/categories/all","archive/tags/all"],"user-archive-'. $kit .'-search":["archive/search"]}'); |
|---|
| 516 | update_option('wpr_single_conditions', '{"user-single-'. $kit .'-404":["single/page_404"],"user-single-'. $kit .'-post":["single/posts/all"],"user-single-'. $kit .'-page":["single/pages/all"]}'); |
|---|
| 517 | } |
|---|
| 518 | |
|---|
| 519 | // WooCommerce Builder |
|---|
| 520 | if ( $has_woo_builder ) { |
|---|
| 521 | update_option('wpr_product_archive_conditions', '{"user-product_archive-'. $kit .'-shop":["product_archive/products"],"user-product_archive-'. $kit .'-product-category-tag":["product_archive/product_cat/all","product_archive/product_tag/all"]}'); |
|---|
| 522 | update_option('wpr_product_single_conditions', '{"user-product_single-'. $kit .'-product":["product_single/product"]}'); |
|---|
| 523 | |
|---|
| 524 | $shop_id = get_page_by_path('shop-'. $kit) ? get_page_by_path('shop-'. $kit)->ID : ''; |
|---|
| 525 | $cart_id = get_page_by_path('cart-'. $kit) ? get_page_by_path('cart-'. $kit)->ID : ''; |
|---|
| 526 | $checkout_id = get_page_by_path('checkout-'. $kit) ? get_page_by_path('checkout-'. $kit)->ID : ''; |
|---|
| 527 | $myaccount_id = get_page_by_path('my-account-'. $kit) ? get_page_by_path('my-account-'. $kit)->ID : ''; |
|---|
| 528 | |
|---|
| 529 | update_option('woocommerce_shop_page_id', $shop_id); |
|---|
| 530 | update_option('woocommerce_cart_page_id', $cart_id); |
|---|
| 531 | update_option('woocommerce_checkout_page_id', $checkout_id); |
|---|
| 532 | |
|---|
| 533 | if ( '' !== $myaccount_id ) { |
|---|
| 534 | update_option('woocommerce_myaccount_page_id', $myaccount_id); |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | // Update Options |
|---|
| 538 | update_option( 'woocommerce_queue_flush_rewrite_rules', 'yes' ); |
|---|
| 539 | |
|---|
| 540 | // Enable Elementor Builder for WooCommerce CPT |
|---|
| 541 | // $cpt_support = get_option( 'elementor_cpt_support' ); |
|---|
| 542 | |
|---|
| 543 | // if ( ! in_array( 'product', $cpt_support ) ) { |
|---|
| 544 | // $cpt_support[] = 'product'; |
|---|
| 545 | // update_option( 'elementor_cpt_support', $cpt_support ); |
|---|
| 546 | // } |
|---|
| 547 | } |
|---|
| 548 | |
|---|
| 549 | // Set Popups |
|---|
| 550 | if ( $has_off_canvas ) { |
|---|
| 551 | update_option('wpr_popup_conditions', '{"user-popup-'. $kit .'-off-canvas":["global"],"user-popup-'. $kit .'-popup":["global"]}'); |
|---|
| 552 | } else { |
|---|
| 553 | update_option('wpr_popup_conditions', '{"user-popup-'. $kit .'-popup":["global"]}'); |
|---|
| 554 | } |
|---|
| 555 | } |
|---|
| 556 | |
|---|
| 557 | /** |
|---|
| 558 | ** Fix Elementor Images |
|---|
| 559 | */ |
|---|
| 560 | function wpr_fix_elementor_images() { |
|---|
| 561 | $args = array( |
|---|
| 562 | 'post_type' => ['wpr_templates', 'wpr_mega_menu', 'page'], |
|---|
| 563 | 'posts_per_page' => '-1', |
|---|
| 564 | 'meta_key' => '_elementor_version' |
|---|
| 565 | ); |
|---|
| 566 | $elementor_pages = new WP_Query ( $args ); |
|---|
| 567 | |
|---|
| 568 | // Check that we have query results. |
|---|
| 569 | if ( $elementor_pages->have_posts() ) { |
|---|
| 570 | |
|---|
| 571 | // Start looping over the query results. |
|---|
| 572 | while ( $elementor_pages->have_posts() ) { |
|---|
| 573 | |
|---|
| 574 | $elementor_pages->the_post(); |
|---|
| 575 | |
|---|
| 576 | // Replace Demo with Current |
|---|
| 577 | $site_url = get_site_url(); |
|---|
| 578 | $site_url = str_replace( '/', '\/', $site_url ); |
|---|
| 579 | $demo_site_url = 'https://demosites.royal-elementor-addons.com/'. get_option('wpr-import-kit-id'); |
|---|
| 580 | $demo_site_url = str_replace( '/', '\/', $demo_site_url ); |
|---|
| 581 | |
|---|
| 582 | // Elementor Data |
|---|
| 583 | $data = get_post_meta( get_the_ID(), '_elementor_data', true ); |
|---|
| 584 | |
|---|
| 585 | if ( ! empty( $data ) ) { |
|---|
| 586 | $data = preg_replace('/\\\{1}\/sites\\\{1}\/\d+/', '', $data); |
|---|
| 587 | $data = str_replace( $demo_site_url, $site_url, $data ); |
|---|
| 588 | $data = json_decode( $data, true ); |
|---|
| 589 | } |
|---|
| 590 | |
|---|
| 591 | update_metadata( 'post', get_the_ID(), '_elementor_data', $data ); |
|---|
| 592 | |
|---|
| 593 | // Elementor Page Settings |
|---|
| 594 | $page_settings = get_post_meta( get_the_ID(), '_elementor_page_settings', true ); |
|---|
| 595 | $page_settings = json_encode($page_settings); |
|---|
| 596 | |
|---|
| 597 | if ( ! empty( $page_settings ) ) { |
|---|
| 598 | $page_settings = preg_replace('/\\\{1}\/sites\\\{1}\/\d+/', '', $page_settings); |
|---|
| 599 | $page_settings = str_replace( $demo_site_url, $site_url, $page_settings ); |
|---|
| 600 | $page_settings = json_decode( $page_settings, true ); |
|---|
| 601 | } |
|---|
| 602 | |
|---|
| 603 | update_metadata( 'post', get_the_ID(), '_elementor_page_settings', $page_settings ); |
|---|
| 604 | |
|---|
| 605 | } |
|---|
| 606 | |
|---|
| 607 | } |
|---|
| 608 | |
|---|
| 609 | // Clear Elementor Cache |
|---|
| 610 | Plugin::$instance->files_manager->clear_cache(); |
|---|
| 611 | } |
|---|
| 612 | |
|---|
| 613 | /** |
|---|
| 614 | ** Final Settings Setup |
|---|
| 615 | */ |
|---|
| 616 | function wpr_final_settings_setup() { |
|---|
| 617 | $kit = !empty(get_option('wpr-import-kit-id')) ? esc_html(get_option('wpr-import-kit-id')) : ''; |
|---|
| 618 | |
|---|
| 619 | // Elementor Site Settings |
|---|
| 620 | import_elementor_site_settings($kit); |
|---|
| 621 | |
|---|
| 622 | // Setup WPR Templates |
|---|
| 623 | setup_wpr_templates($kit); |
|---|
| 624 | |
|---|
| 625 | // Fix Elementor Images |
|---|
| 626 | wpr_fix_elementor_images(); |
|---|
| 627 | |
|---|
| 628 | // Track Kit |
|---|
| 629 | wpr_track_imported_kit( $kit ); |
|---|
| 630 | |
|---|
| 631 | // Clear DB |
|---|
| 632 | delete_option('wpr-import-kit-id'); |
|---|
| 633 | |
|---|
| 634 | // Delete Hello World Post |
|---|
| 635 | $post = get_page_by_path('hello-world', OBJECT, 'post'); |
|---|
| 636 | if ( $post ) { |
|---|
| 637 | wp_delete_post($post->ID,true); |
|---|
| 638 | } |
|---|
| 639 | |
|---|
| 640 | // Regenerate Extra Image Sizes |
|---|
| 641 | regenerate_extra_image_sizes(); |
|---|
| 642 | } |
|---|
| 643 | |
|---|
| 644 | /** |
|---|
| 645 | ** Regenerate Extra Image Sizes |
|---|
| 646 | */ |
|---|
| 647 | function disable_extra_image_sizes( $new_sizes, $image_meta, $attachment_id ) { |
|---|
| 648 | $all_attachments = get_option( 'st_attachments', array() ); |
|---|
| 649 | |
|---|
| 650 | // If the cron job is already scheduled, bail. |
|---|
| 651 | if ( in_array( $attachment_id, $all_attachments, true ) ) { |
|---|
| 652 | return $new_sizes; |
|---|
| 653 | } |
|---|
| 654 | |
|---|
| 655 | $all_attachments[] = $attachment_id; |
|---|
| 656 | |
|---|
| 657 | update_option( 'st_attachments', $all_attachments, 'no' ); |
|---|
| 658 | |
|---|
| 659 | // Return blank array of sizes to not generate any sizes in this request. |
|---|
| 660 | return array(); |
|---|
| 661 | } |
|---|
| 662 | |
|---|
| 663 | /** |
|---|
| 664 | ** Regenerate Extra Image Sizes |
|---|
| 665 | */ |
|---|
| 666 | function regenerate_extra_image_sizes() { |
|---|
| 667 | $all_attachments = get_option( 'st_attachments', array() ); |
|---|
| 668 | |
|---|
| 669 | if ( empty( $all_attachments ) ) { |
|---|
| 670 | return; |
|---|
| 671 | } |
|---|
| 672 | |
|---|
| 673 | foreach ( $all_attachments as $attachment_id ) { |
|---|
| 674 | $file = get_attached_file( $attachment_id ); |
|---|
| 675 | if ( false !== $file ) { |
|---|
| 676 | wp_generate_attachment_metadata( $attachment_id, $file ); |
|---|
| 677 | } |
|---|
| 678 | } |
|---|
| 679 | update_option( 'st_attachments', array(), 'no' ); |
|---|
| 680 | } |
|---|
| 681 | |
|---|
| 682 | /** |
|---|
| 683 | ** Validate Image Extension |
|---|
| 684 | */ |
|---|
| 685 | function wpr_validate_image_ext( $link = '' ) { |
|---|
| 686 | return preg_match( '/^((https?:\/\/)|(www\.))([a-z0-9-].?)+(:[0-9]+)?\/[\w\-\@]+\.(jpg|png|gif|jpeg|svg)\/?$/i', $link ); |
|---|
| 687 | } |
|---|
| 688 | |
|---|
| 689 | /** |
|---|
| 690 | ** Set Timeout for Image Request |
|---|
| 691 | */ |
|---|
| 692 | function set_image_request_timeout( $timeout_value, $url ) { |
|---|
| 693 | if ( strpos( $url, 'https://royal-elementor-addons.com/' ) === false ) { |
|---|
| 694 | return $timeout_value; |
|---|
| 695 | } |
|---|
| 696 | |
|---|
| 697 | $valid_ext = preg_match( '/^((https?:\/\/)|(www\.))([a-z0-9-].?)+(:[0-9]+)?\/[\w\-\@]+\.(jpg|png|gif|jpeg|svg)\/?$/i', $url ); |
|---|
| 698 | |
|---|
| 699 | if ( $valid_ext ) { |
|---|
| 700 | $timeout_value = 300; |
|---|
| 701 | } |
|---|
| 702 | |
|---|
| 703 | return $timeout_value; |
|---|
| 704 | } |
|---|
| 705 | |
|---|
| 706 | /** |
|---|
| 707 | ** Prevent WooCommerce creating default pages |
|---|
| 708 | */ |
|---|
| 709 | function disable_default_woo_pages_creation() { |
|---|
| 710 | add_filter( 'woocommerce_create_pages', '__return_empty_array' ); |
|---|
| 711 | } |
|---|
| 712 | |
|---|
| 713 | /** |
|---|
| 714 | ** Add .xml and .svg files as supported format in the uploader. |
|---|
| 715 | */ |
|---|
| 716 | function custom_upload_mimes( $mimes ) { |
|---|
| 717 | // Allow SVG files. |
|---|
| 718 | $mimes['svg'] = 'image/svg+xml'; |
|---|
| 719 | $mimes['svgz'] = 'image/svg+xml'; |
|---|
| 720 | |
|---|
| 721 | // Allow XML files. |
|---|
| 722 | $mimes['xml'] = 'text/xml'; |
|---|
| 723 | |
|---|
| 724 | // Allow JSON files. |
|---|
| 725 | $mimes['json'] = 'application/json'; |
|---|
| 726 | |
|---|
| 727 | return $mimes; |
|---|
| 728 | } |
|---|
| 729 | |
|---|
| 730 | add_filter( 'upload_mimes', 'custom_upload_mimes', 99 ); |
|---|
| 731 | |
|---|
| 732 | function real_mime_types_5_1_0( $defaults, $file, $filename, $mimes, $real_mime ) { |
|---|
| 733 | return real_mimes( $defaults, $filename ); |
|---|
| 734 | } |
|---|
| 735 | |
|---|
| 736 | function real_mime_types( $defaults, $file, $filename, $mimes ) { |
|---|
| 737 | return real_mimes( $defaults, $filename ); |
|---|
| 738 | } |
|---|
| 739 | |
|---|
| 740 | function real_mimes( $defaults, $filename ) { |
|---|
| 741 | if ( strpos( $filename, 'main' ) !== false ) { |
|---|
| 742 | $defaults['ext'] = 'xml'; |
|---|
| 743 | $defaults['type'] = 'text/xml'; |
|---|
| 744 | } |
|---|
| 745 | |
|---|
| 746 | return $defaults; |
|---|
| 747 | } |
|---|
| 748 | |
|---|
| 749 | if ( version_compare( get_bloginfo( 'version' ), '5.1.0', '>=' ) ) { |
|---|
| 750 | add_filter( 'wp_check_filetype_and_ext', 'real_mime_types_5_1_0', 10, 5, 99 ); |
|---|
| 751 | } else { |
|---|
| 752 | add_filter( 'wp_check_filetype_and_ext', 'real_mime_types', 10, 4 ); |
|---|
| 753 | } |
|---|
| 754 | |
|---|
| 755 | /** |
|---|
| 756 | ** Search Query Results |
|---|
| 757 | */ |
|---|
| 758 | function wpr_search_query_results() { |
|---|
| 759 | // Freemius OptIn |
|---|
| 760 | if ( ! ( wpr_fs()->is_registered() && wpr_fs()->is_tracking_allowed() || wpr_fs()->is_pending_activation() ) ) { |
|---|
| 761 | return; |
|---|
| 762 | } |
|---|
| 763 | |
|---|
| 764 | $search_query = isset($_POST['search_query']) ? sanitize_text_field(wp_unslash($_POST['search_query'])) : ''; |
|---|
| 765 | |
|---|
| 766 | wp_remote_post( 'http://reastats.kinsta.cloud/wp-json/templates-kit-search/data', [ |
|---|
| 767 | 'body' => [ |
|---|
| 768 | 'search_query' => $search_query |
|---|
| 769 | ] |
|---|
| 770 | ] ); |
|---|
| 771 | } |
|---|
| 772 | |
|---|
| 773 | /** |
|---|
| 774 | ** Search Query Results |
|---|
| 775 | */ |
|---|
| 776 | function wpr_track_imported_kit( $kit ) { |
|---|
| 777 | // Freemius OptIn |
|---|
| 778 | if ( ! ( wpr_fs()->is_registered() && wpr_fs()->is_tracking_allowed() || wpr_fs()->is_pending_activation() ) ) { |
|---|
| 779 | return; |
|---|
| 780 | } |
|---|
| 781 | |
|---|
| 782 | wp_remote_post( 'http://reastats.kinsta.cloud/wp-json/templates-kit-import/data', [ |
|---|
| 783 | 'body' => [ |
|---|
| 784 | 'imported_kit' => $kit |
|---|
| 785 | ] |
|---|
| 786 | ] ); |
|---|
| 787 | } |
|---|