Current File : /home/kelaby89/muzza.fit/wp-content/themes/deadlift/inc/admin/admin-import.php
<?php
/**
 * Deadlift: Demo import
 *
 * @since Deadlift 1.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * Add QDI plugin support
 *
 * * NOT USED
 *
 * @param array $themes
 * @return array
 */
function deadlift_qdi_support( $themes ) {

	$theme_slug = deadlift_get_theme_slug();

   return array_merge( $themes, array( $theme_slug ) );
}
add_filter( 'quick-demo-import/supported_themes', 'deadlift_qdi_support' );

/**
 * Demo import setup
 *
 * @return array
 */
function deadlift_qdi_demo_packages() {

	return array(
		'demos' => array(
			'wolftheme' => array(
				'title'      => ucfirst( deadlift_get_theme_slug() ),
				'preview'    => 'https://preview.wolfthemes.live/' . deadlift_get_theme_slug() . '/landing',
				'screenshot' => 'https://updates.wolfthemes.com/' . deadlift_get_theme_slug() . '/screenshot.jpg',
				'zip'        => 'https://updates.wolfthemes.com/' . deadlift_get_theme_slug() . '/demo.zip',
				'core_options' => array(
					'page_on_front'  => 'Home', // Demo home page title.
					'page_for_posts' => 'Blog', // Demo blog page title.
				),
				'customizer_data_update' => array(
					'nav_menu_locations' => array(
						'primary' => 'Primary menu', // 'registered nav menu location => 'menu title'
						'secondary'  => 'Secondary menu', // 'registered nav menu location => 'menu title'
						'mobile'  => 'Mobile menu', // 'registered nav menu location => 'menu title'
					),
				),
				'plugins_list' => deadlift_recommended_plugins()
			),
		),
	);
}
add_filter( 'quick-demo-import/demo_packages', 'deadlift_qdi_demo_packages' );


remove_action( 'vc_activation_hook', 'vc_page_welcome_set_redirect', 100 );
remove_action( 'admin_init', 'vc_page_welcome_redirect', 100 );
delete_transient('_revslider_welcome_screen_activation_redirect');

/**
 * Download external plugins before starting
 */
function deadlift_download_external_plugins() {

	if ( ! current_user_can( 'install_plugins' ) ) {
		return;
	}

	if ( ! function_exists( 'download_url' ) ) {
		require_once( ABSPATH . "wp-admin" . '/includes/file.php');
	}

	$plugins = deadlift_recommended_external_plugins();
	foreach ( $plugins as $slug => $url ) {

		if ( ! is_dir( WP_PLUGIN_DIR . '/' . $slug ) ) {
			if ( download_url( $url ) ) {

				$filename = download_url( $url );

				$plugin_path = WP_PLUGIN_DIR;

				WP_Filesystem();

				if ( is_file( $filename ) ) {
					$unzipfile = unzip_file( $filename, $plugin_path );

					if ( $unzipfile ) {
						$new_filename = str_replace( '-master', '', $slug );

						if ( file_exists( WP_PLUGIN_DIR . '/' . $slug . '-master' ) && 'envato-market' !== $slug ) {
							rename( $plugin_path . '/' . $slug . '-master', $plugin_path . '/' . $new_filename );
						}
					}

					unlink( $filename );
				}
			}
		}
	}

	foreach ( $plugins as $slug => $url ) {
		if ( file_exists( WP_PLUGIN_DIR . '/' . $slug . '/' . $slug . '.php' ) && ! is_plugin_active( $slug . '/' . $slug . '.php' ) ) {
			activate_plugin( $slug . '/' . $slug . '.php' );
		}
	}
	set_transient('_revslider_welcome_screen_activation_redirect', true, 60); // revslider
	delete_transient( '_vc_page_welcome_redirect' ); // js_composer
}

/**
 * Confirm import text
 */
function deadlift_confirm_import_text() {
	ob_start();
	?>
	<h1><?php esc_html_e( 'Install demo content', 'deadlift' ); ?></h1>
	<h2>
		<?php esc_html_e( 'Importing demo data is the easiest way to setup your webste. It will allow you to quickly edit everything instead of creating content from scratch.', 'deadlift' ); ?>
	</h2>
	<hr>
	<p><?php esc_html_e( 'Before importing the demo, please consider the following', 'deadlift' ); ?>:</p>
	<ol>
		<li class="warning">
			<?php
			printf(
				/* Translators: Before import warnings. */
				wp_kses_post( __( 'Before you begin, <strong>make sure that your server settings fulfill the <a href="%s" target="_blank">server requirements</a></strong>.', 'deadlift' ) ),
				esc_url( 'https://wolfthemes.ticksy.com/article/11651/' )
			);
			?>
		</li>
		<li class="warning"><?php esc_html_e( 'Make sure that your hosting service provider allows external connections, so the images can be downloaded from our server.', 'deadlift' ); ?></li>
		<li class="warning"><?php esc_html_e( 'It is strongly recommended to import the demo on a fresh WordPress install to exactly replicate the theme demo.', 'deadlift' ); ?></li>
		<li>
			<?php
			printf(
				/* Translators: Before import warnings. */
				wp_kses_post( __( 'You can use a plugin like <a href="%s" target="_blank">WordPress Reset</a> to delete any existing content from your installation.', 'deadlift' ) ),
				esc_url( 'https://wordpress.org/plugins/wordpress-reset/' )
			);
			?><br>
			<?php esc_html_e( 'Please disable all plugins expect WordPress Reset before resetting your installation.', 'deadlift' ); ?>
		</li>
		<li><?php esc_html_e( 'It will install and activate additional plugins so the data will look the same as the demo.', 'deadlift' ); ?></li>
		<li>
		<?php
			printf(
				wp_kses_post( __( 'Importing the demo content may <strong>take a while</strong> (up to an hour). Have a snack and don\'t refresh the page or close the window until it\'s done!', 'deadlift' ) )
			);
		?>
		</li>
	</ol>
	<?php
	return ob_get_clean();
}

/**
 * Enqueue scripts and pass data to JS.
 *
 * @return void
 */
function deadlift_import_admin_scripts() {

	wp_enqueue_style( 'deadlift-import', get_template_directory_uri() . '/assets/css/admin/import.css', array(), '1.0.0' );

	wp_enqueue_script( 'deadlift-import', get_template_directory_uri() . '/assets/js/admin/import.js', array( 'jquery' ), '1.0.0', true );
	wp_localize_script(
		'deadlift-import',
		'wolfthemeAdmin',
		array(
			'uri'          => '/themes.php?page=demo-importer&browse=all', // Importer page URI.
			'dismissNonce' => wp_create_nonce( 'deadlift_dismiss_notice_nonce' ), // Dismiss nonce.
			'installNonce' => wp_create_nonce( 'deadlift_qdi_install_nonce' ), // Install nonce.
			'confirmImport' => deadlift_confirm_import_text(),
		)
	);
}
add_action( 'admin_enqueue_scripts', 'deadlift_import_admin_scripts' );

function deadlift_import_admin_notice() {
	if ( get_option( '_deadlift_hide_notice_flag' ) ) {
		return;
	}
	?>
	<div class="notice notice-info is-dismissible deadlift-notice">
		<h2 class="deadlift-qdi-notice-title"><?php esc_html_e( 'Install the required plugins to import the demo', 'deadlift' ); ?></h2>
		<p class="plugin-install-info"><?php esc_html_e( 'Clicking the button will install required plugins and redirect you to the demo import page.', 'deadlift' ); ?>
		<br><?php esc_html_e( 'Make sure that your hosting service provider allows external connections, so the plugins can be downloaded.', 'deadlift' ); ?></p>
		<button class="deadlift-install-import-plugin-btn btn-get-qdi button button-primary button-hero"><?php esc_html_e( 'Install Plugins', 'deadlift' ); ?></button>
		<br><br>
	</div>
	<?php
}
add_action( 'admin_notices', 'deadlift_import_admin_notice' );

/**
 * Install and activate Quick Demo Import plugin.
 *
 * @return void
 */
function deadlift_install_qdi_plugin() {
	check_ajax_referer( 'deadlift_qdi_install_nonce', 'security' );
	update_option( '_deadlift_hide_notice_flag', 1 );

	$state = '';
	deadlift_download_external_plugins();

	if ( class_exists( 'QuickDemoImport' ) ) {
		$state = 'activated';
	} elseif ( file_exists( WP_PLUGIN_DIR . '/quick-demo-import/qdi.php' ) ) {
		$state = 'installed';
	}

	if ( 'activated' === $state ) {
		$response['redirect'] = admin_url( '/themes.php?page=demo-importer&browse=all' );
	} elseif ( 'installed' === $state ) {
		$response['redirect'] = admin_url( '/themes.php?page=demo-importer&browse=all' );
		if ( current_user_can( 'activate_plugin' ) ) {
			$result = activate_plugin( 'quick-demo-import/qdi.php' );

			if ( is_wp_error( $result ) ) {
				$response['errorCode']    = $result->get_error_code();
				$response['errorMessage'] = $result->get_error_message();
			}
		}
	} else {
		wp_enqueue_style( 'plugin-install' );
		wp_enqueue_script( 'plugin-install' );

		$response['redirect'] = admin_url( '/themes.php?page=demo-importer&browse=all' );

		/**
		 * Install Plugin.
		 */
		include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
		include_once ABSPATH . 'wp-admin/includes/plugin-install.php';

		$api = plugins_api(
			'plugin_information',
			array(
				'slug'   => sanitize_key( wp_unslash( 'quick-demo-import' ) ),
				'fields' => array(
					'sections' => false,
				),
			)
		);

		$skin     = new WP_Ajax_Upgrader_Skin();
		$upgrader = new Plugin_Upgrader( $skin );
		$result   = $upgrader->install( $api->download_link );

		if ( $result ) {
			$response['installed'] = 'succeed';
		} else {
			$response['installed'] = 'failed';
		}
		if ( current_user_can( 'activate_plugin' ) ) {
			$result = activate_plugin( 'quick-demo-import/qdi.php' );

			if ( is_wp_error( $result ) ) {
				$response['errorCode']    = $result->get_error_code();
				$response['errorMessage'] = $result->get_error_message();
			}
		}
	}
	wp_send_json( $response );
	exit();
}
add_action( 'wp_ajax_deadlift-install-qdi-plugin', 'deadlift_install_qdi_plugin' );

/**
 * Dismiss the admin notice.
 *
 * @return void
 */
function deadlift_dismiss_notice() {
	check_ajax_referer( 'deadlift_dismiss_notice_nonce', 'security' );
	update_option( '_deadlift_hide_notice_flag', 1 );
	wp_send_json_success();
	exit();
}
add_action( 'wp_ajax_deadlift-dismiss-notice', 'deadlift_dismiss_notice' );

/**
 * Replace hard coded URLs from demo data content to local URL
 *
 * Scan external URL and replace them by local ones in post content
 */
function deadlift_replace_content_urls_after_import() {

	$pages = get_posts( array( 'posts_per_page' => -1 ) );

	$url_regex       = '/(http:|https:)?\/\/[a-zA-Z0-9\/.?&=_-]+/';
	$demo_url_reg_ex = '/(http|https)?:\/\/([a-z0-9.]+)\wolfthemes.live/';

	foreach ( $pages as $page ) {

		$page_id = $page->ID;
		$content = get_post( $page_id )->post_content;
		$content = preg_replace_callback(
			$url_regex,
			function ( $matches ) use ( $demo_url_reg_ex ) {

				$output = '';

				if ( isset( $matches[0] ) ) {
					$url = $matches[0];
					if ( preg_match( $demo_url_reg_ex, $url, $matches ) ) {

						if ( isset( $matches[0] ) ) {

							$deadlift_root_url = $matches[0];
							$site_url           = home_url( '/' ); // current site url.
							$url_array          = explode( '/', $url );

							if ( isset( $url_array[3] ) ) {

								$demo_slug = $url_array[3];

								$deadlift_url = $deadlift_root_url . '/' . $demo_slug . '/';

								$output .= str_replace( $deadlift_root_url . '/app/uploads', $site_url . '/wp-content/uploads', $url );
								$output .= str_replace( $deadlift_url, $site_url, $url );
							}
						}
					}
				}

				return $output;
			},
			$content
		);

		/* Update content */
		$post = array(
			'ID'           => $page_id,
			'post_content' => $content,
		);

		/* Update the post into the database */
		wp_update_post( $post );
	}

	/* Replace app folder occurences */
	foreach ( $pages as $page ) {
		$page_id = $page->ID;
		$content = get_post( $page_id )->post_content;
	}
}

function deadlift_qdi_assign_plugin_pages() {
	/* Assign plugins pages */
	$wolf_pages = array(
		'Portfolio',
		'Albums',
		'Videos',
		'Discography',
		'Events',
		'Artists',
		'Jobs',
		'Wishlist',
	);

	foreach ( $wolf_pages as $page_title ) {

		$page = deadlift_get_page_by_title( $page_title );

		if ( $page ) {
			update_option( '_wolf_' . strtolower( $page_title ) . '_page_id', $page->ID );
		}
	}
}

/**
 * Before import
 */
function deadlift_qdi_before_import() {
	flush_rewrite_rules();
}
add_action( 'quick-demo-import/ajax_before_demo_import', 'deadlift_qdi_before_import' );


/**
 * After import
 */
function deadlift_qdi_after_import() {

	add_option( deadlift_get_theme_slug() . '_demo_data_imported', true );

	/* Set pretty permalinks if they're not set yet */
	if ( ! get_option( 'permalink_structure' ) ) {
		update_option( 'permalink_structure', '/%year%/%monthnum%/%postname%/' );
		flush_rewrite_rules();
	}

	deadlift_replace_content_urls_after_import();
	deadlift_qdi_assign_plugin_pages();

	if ( did_action( 'elementor/loaded' ) ) {
		\Elementor\Plugin::instance()->files_manager->clear_cache();
	}
}
add_action( 'quick-demo-import/ajax_after_demo_import', 'deadlift_qdi_after_import' );
Page not found – Hello World !