Current File : /home/kelaby89/muzza.fit/wp-content/themes/deadlift/inc/admin/import-functions.php
<?php
/**
 * Deadlift admin import functions functions
 *
 * @link https://wordpress.org/plugins/one-click-demo-import/
 *
 * @package WordPress
 * @subpackage Deadlift
 * @version 1.8.2
 */

defined( 'ABSPATH' ) || exit;

/**
 * Plugin install
 */
add_filter( 'woocommerce_prevent_automatic_wizard_redirect', '__return_true' );

/**
 * Check if plugin is active
 *
 * @param string $plugin
 * @return bool
 */
function deadlift_plugin_is_active( $plugin ) {
	return in_array( $plugin, (array) get_option( 'active_plugins', array() ), true );
}

/**
 * All plugins are installed
 *
 * @return bool
 */
function deadlift_recommended_plugin_installed() {

	if ( get_option( '_deadlift_hide_theme_plugin_notice_flag' ) ) {
		return true;
	} else {
		$plugins  = deadlift_get_required_plugins();
		$complete = true;

		foreach ( $plugins as $slug => $url ) {
			if ( ! deadlift_plugin_is_active( $slug ) ) {
				$complete = false;
				break;
			}
		}

		return $complete;
	}
}

/**
 * Theme import admin notice
 *
 * @return void
 */
function deadlift_import_admin_notice() {

	if ( ! deadlift_recommended_plugin_installed() ) {
		?>
		<div class="notice notice-info is-dismissible deadlift-notice deadlift-custom-plugin-instal-notice">
			<h2 class="deadlift-ocdi-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
			printf(
				/* translators: %s: help URL */
				deadlift_kses( __( 'Make sure that <strong>your hosting service provider allows external connections via the WordPress "wp_safe_remote_post" function</strong> so the plugins can be downloaded. (<a href="%s" target="_blank">more info</a>)', 'deadlift' ) ),
				'https://wlfthm.es/hosting-external-connection-issue'
			);
			?>
			</p>
			<button class="deadlift-install-import-plugin-btn btn-get-ocdi button button-primary button-hero"><?php esc_html_e( 'Install Plugins', 'deadlift' ); ?></button>
			<br><br><a href="#" class="btn-get-ocdi-discard"><small><?php esc_html_e( 'No, I want to install everything by myself.', 'deadlift' ); ?></small></a>
			<br><br>
		</div>
		<?php
		return;
	}
}
add_action( 'admin_notices', 'deadlift_import_admin_notice' );

/**
 * Import external plugins
 */
function deadlift_import_external_plugin() {

	check_ajax_referer( 'deadlift_ocdi_install_nonce', 'security' );

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

	if ( ! isset( $_POST['slug'] ) ) {
		wp_send_json( 'no plugin sepcified' );
	}

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

	$plugin_list = deadlift_get_required_plugins();

	$slug = esc_attr( $_POST['slug'] );
	$url  = ( isset( $plugin_list[ $slug ] ) ) ? $plugin_list[ $slug ] : null;

	$is_external = wp_http_validate_url( $url );

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

		if ( ! $is_external ) {
			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( $_POST['slug'] ) ),
					'fields' => array(
						'sections' => false,
					),
				)
			);
			$url = $api->download_link;
		}

		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 );

				wp_send_json( $slug . ' imported' );
			}
		}
	} else {
		wp_send_json( $slug . ' already installed' );
	}

	exit();
}
add_action( 'wp_ajax_deadlift_import_external_plugin', 'deadlift_import_external_plugin' );

/**
 * Activate all plugins
 */
function deadlift_activate_all_plugins() {

	check_ajax_referer( 'deadlift_ocdi_activate_nonce', 'security' );

	if ( ! current_user_can( 'activate_plugin' ) ) {
		wp_send_json( 'user can not' );
	}

	if ( empty( $_POST['slug'] ) ) {
		wp_send_json( 'no plugin sepcified' );
	}

	$plugin_list = deadlift_get_required_plugins();
	$slug        = sanitize_key( wp_unslash( $_POST['slug'] ) );
	$path        = ( isset( $plugin_list[ $slug ] ) ) ? $plugin_list[ $slug ] : null;
	$is_external = wp_http_validate_url( $path );

	if ( $is_external ) {
		$path = esc_attr( $slug . '/' . $slug . '.php' );
	}

	if ( file_exists( WP_PLUGIN_DIR . '/' . $path ) ) {
		if ( ! deadlift_plugin_is_active( $path ) ) {

			$result = activate_plugin( $path, false, false, true );
			add_filter( 'woocommerce_enable_setup_wizard', '__return_false' );

			if ( is_wp_error( $result ) ) {
				wp_send_json( $path . ' error' );
			}

			if ( 'revslider' === $slug ) {
				delete_transient( '_revslider_welcome_screen_activation_redirect' );
			}

			if ( 'js_composer' === $slug ) {
				delete_transient( '_vc_page_welcome_redirect' );
			}

			wp_send_json( $slug . ' activated' );

		} else {
			wp_send_json( $slug . ' already active' );
		}
	} else {
		echo esc_attr( $path );
		wp_send_json( $slug . ' not found' );
	}

	exit();
}
add_action( 'wp_ajax_deadlift_activate_all_plugins', 'deadlift_activate_all_plugins' );

/**
 * Dismiss the admin notice.
 *
 * @return void
 */
function deadlift_plugin_install_dismiss_notice() {
	check_ajax_referer( 'deadlift_dismiss_notice_nonce', 'security' );
	update_option( '_deadlift_hide_theme_plugin_notice_flag', 1 );

	flush_rewrite_rules(); // permalink rebuilt after plugin activation

	wp_send_json_success();
	exit();
}
add_action( 'wp_ajax_deadlift_plugin_install_dismiss_notice', 'deadlift_plugin_install_dismiss_notice' );

/**
 * 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(
			'adminUrl'                 => admin_url(),
			'redirectUrl'              => admin_url( '/themes.php?page=one-click-demo-import' ), // Importer page URI.
			'dismissNonce'             => wp_create_nonce( 'deadlift_dismiss_notice_nonce' ), // Dismiss nonce.
			'installNonce'             => wp_create_nonce( 'deadlift_ocdi_install_nonce' ), // Install nonce.
			'activateNonce'            => wp_create_nonce( 'deadlift_ocdi_activate_nonce' ), // Activate nonce.
			'updatesNonce'             => wp_create_nonce( 'updates' ),
			'requiredPlugins'          => deadlift_get_required_plugins(),
			'installingPluginsMessage' => esc_html__( 'Installing plugins...', 'deadlift' ),
			'activatingPluginsMessage' => esc_html__( 'Activating plugins...', 'deadlift' ),
			'redirectingMessage'       => esc_html__( 'Redirecting...', 'deadlift' ),
		)
	);
}
add_action( 'admin_enqueue_scripts', 'deadlift_import_admin_scripts' );

/**
 * Disable OCDI PT branding
 *
 * @param  bool $bool disable OCDI branding.
 * @return bool
 */
function deadlift_disable_ocdi_pt_branding( $bool ) {
	return true;
}
add_filter( 'pt-ocdi/disable_pt_branding', 'deadlift_disable_ocdi_pt_branding' );

/**
 * Flush rewrite rules before import
 *
 * Make sure all CPT are registered
 */
function deadlift_flush_rewrite_rules_before_import() {
	flush_rewrite_rules();
}
add_action( 'pt-ocdi/before_content_import_execution', 'deadlift_flush_rewrite_rules_before_import' );

/**
 * Set menu location after demo import
 *
 * @param array $menus the array of menu names.
 */
function deadlift_set_menu_locations( $menus = array() ) {

	$menu_to_insert = array();

	foreach ( $menus as $location => $name ) {
		$menu = get_term_by( 'name', $name, 'nav_menu' );

		if ( $menu ) {
			$menu_to_insert[ $location ] = $menu->term_id;
		}
	}

	set_theme_mod( 'nav_menu_locations', $menu_to_insert );
}

/**
 * Set pages after import
 *
 * Assign each possible page from plugin (Home and Blog pages, Wolf plugins pages, WooCommerce pages etc...)
 */
function deadlift_set_pages_after_import() {

	/* Assign front page and posts page (blog page). */
	$front_page = deadlift_get_page_by_title( 'Home' );
	$blog_page  = deadlift_get_page_by_title( 'Blog' );

	update_option( 'show_on_front', 'page' );

	if ( $front_page ) {
		update_option( 'page_on_front', $front_page->ID );
	} else {
		$front_page = deadlift_get_page_by_title( 'Main Home' );

		if ( $front_page ) {
			update_option( 'page_on_front', $front_page->ID );
		}
	}

	if ( $blog_page ) {
		update_option( 'page_for_posts', $blog_page->ID );
	}

	/* 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 );
		}
	}

	/* Assign WooCommerce pages */
	$woocommerce_pages = array(
		'Shop',
		'Cart',
		'Checkout',
		'My Account',
		'Terms & Conditions',
	);

	foreach ( $woocommerce_pages as $page_title ) {

		$page = deadlift_get_page_by_title( $page_title );

		if ( 'My Account' === $page_title ) {

			$page_slug = 'myaccount';

		} elseif ( 'Terms & Conditions' === $page_title ) {

			$page_slug = 'terms';

		} else {
			$page_slug = strtolower( $page_title );
		}

		if ( $page ) {
			update_option( 'woocommerce_' . $page_slug . '_page_id', $page->ID );
		}
	}
}
add_action( 'pt-ocdi/after_import', 'deadlift_set_pages_after_import' );

/**
 * Demo importer Intro text
 *
 * @param  string $default_text the default demo importer intro text to filter.
 * @return string
 */
function deadlift_plugin_intro_text( $default_text ) {

	ob_start();

	?>
	<div class="deadlift-ocdi-intro-text">
		<h1><?php esc_html_e( 'Install demo content', 'deadlift' ); ?></h1>

		<p class="about-description">
			<?php esc_html_e( 'Importing demo data is the easiest way to setup your theme. It will allow you to quickly edit everything instead of creating content from scratch.', 'deadlift' ); ?>
		</p>
		<section class="deadlift-ocdi-notice-container">
			<main class="deadlift-ocdi-notice">
				<h4><?php esc_html_e( 'Important', 'deadlift' ); ?></h4>

				<ul>
					<li class="warning">
						<?php
						printf(
							/* translators: %s: theme admin "about" page URL */
							deadlift_kses( __( 'Before you begin, <strong>make sure that your server settings fulfill the <a href="%s" target="_blank">server requirements</a></strong>.', 'deadlift' ) ),
							esc_url( admin_url( 'themes.php?page=deadlift-about#system-status' ) )
						);
						?>
					</li>
					<li class="warning">
						<?php
						printf(
							/* translators: %s: help URL */
							deadlift_kses( __( 'Make sure that <strong>your hosting service provider allows external connections via the WordPress "wp_safe_remote_post" function</strong>, so the images can be downloaded from our server. (<a href="%s" target="_blank">more info</a>)', 'deadlift' ) ),
							'https://wlfthm.es/hosting-external-connection-issue'
						);
						?>
					</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 class="warning">
					<?php
						printf(
							/* translators: %s: WordPress reset plugin page URl */
							deadlift_kses( __( 'We recommend resetting your install using <a href="%s" target="_blank">WordPress Reset</a> plugin.', 'deadlift' ) ),
							'https://wordpress.org/plugins/wordpress-reset/'
						);
					?>
					<br>
					<strong><?php esc_html_e( 'Please disable all plugins except WordPress Reset before resetting your installation ( in "Tools" -> "Reset").', 'deadlift' ); ?></strong>
					</li>

					<?php if ( ! deadlift_recommended_plugin_installed() ) : ?>
						<li>
							<?php
								printf(
									/* translators: %s: WordPress reset plugin page URl */
									deadlift_kses( __( 'Make sure <a href="%s" target="_blank">all the required plugins</a> are installed and activated.', 'deadlift' ) ),
									esc_url( admin_url( '/themes.php?page=tgmpa-install-plugins' ) )
								);
							?>
						</li>
					<?php endif; ?>
					<li>
						<?php
							printf(
								/* translators: %s: default site language */
								deadlift_kses( __( 'You will need to import the Revolution Sliders afterward as explained in <a href="%s" target="_blank">this post</a>.', 'deadlift' ) ),
								'https://wlfthm.es/import-revsliders'
							);
						?>
					</li>
					<li>
					<?php
						printf(
							/* translators: %s: default site language */
							deadlift_kses( __( 'The <strong>Site Language</strong> must be set to "%s" in the "Settings" > "General" panel . You will be able to change it afterwards.', 'deadlift' ) ),
							'English (United States)'
						);
					?>
					</li>
					<li><?php esc_html_e( 'Deactivate all 3rd party plugins except the one recommended by the theme.', 'deadlift' ); ?></li>
					<li><?php esc_html_e( 'Some of the images may be replaced by placeholder images if they are copyrighted material.', 'deadlift' ); ?></li>
					<li>
					<?php
						printf(
							/* translators: %s: OCDI plugin page URl */
							deadlift_kses( __( 'If you have any issue importing the demo content, please check the <a href="%s" target="_blank">plugin troubleshooting documentation</a>.', 'deadlift' ) ),
							'https://github.com/proteusthemes/one-click-demo-import/blob/master/docs/import-problems.md'
						);
					?>
					</li>
					<li>
					<?php
						printf(
							deadlift_kses( __( 'Importing the demo content may <strong>take a while</strong>. Have a snack and don\'t refresh the page or close the window until it\'s done!', 'deadlift' ) )
						);
					?>
					</li>
				</ul>
			</main><!-- .deadlift-ocdi-notice -->
			<aside class="woilftheme-ocdi-notice-aside">
				<h3><?php esc_html_e( 'Installation Service', 'deadlift' ); ?></h3>
				<p style="font-size:18px">
				<?php
					printf(
						deadlift_kses(
							/* translators: %s: WolfThemes services page URL */
							__( '<strong>Ready to experience a website makeover without the hassle?</strong>', 'deadlift' )
						),
						'https://wolfthemes.com/services'
					);
				?>
				</p>
				<p><a href="https://wolfthemes.com/services" class="button button-primary button-hero">
					<?php esc_attr_e( 'Order Installation Service', 'deadlift' ); ?></a>
				</p>
				<hr>
				<h3><?php esc_html_e( 'Recommended Hosting', 'deadlift' ); ?></h3>
				<p>
				<?php
				printf(
					deadlift_kses(
					/* translators: 1: support articles link, 2: support articles link */
						__( 'For a smooth experience, we recommended <a href="%s" target="_blank">Siteground</a> which we have tested and approved!.', 'deadlift' )
					),
					'https://www.siteground.com/recommended?referrer_id=8486532'
				);
				?>
		</p>
			<a href="https://www.siteground.com/recommended?referrer_id=8486532" target="_blank"><img src="<?php echo esc_url( get_stylesheet_directory_uri() . '/assets/img/admin/siteground-logo.png' ); ?>" alt="Siteground"></a>
			</aside>
		</section>
		<hr>
		<h4><?php esc_html_e( 'Warning', 'deadlift' ); ?></h4>
		<p>
		<?php
			printf(
				deadlift_kses(
					/* translators: 1: support articles link, 2: support articles link */
					__(
						'Successfully importing the demo data into WordPress is not something we can guarantee for all users.<br>There are a lot of variables that come into play, over which we have no control. Most of the time, the main issues are <a href="%1$s" target="_blank">bad shared hosting servers</a>.<br>If you need recommendations on choosing your hosting service provider, here is a list of <a href="%3$s" target="_blank">recommended WordPress hosts</a>.<br>
					If you cannot import the demo data using the one-click demo importer, you can still use the alternative way described in <a href="%3$s" target="_blank">this post</a>.',
						'deadlift'
					)
				),
				'https://wolfthemes.ticksy.com/article/11668/',
				'https://wolfthemes.ticksy.com/article/13381/',
				'https://wolfthemes.ticksy.com/article/19447'
			);
		?>
		</p>
		<p style="font-size:18px">
		<?php
			printf(
				deadlift_kses(
					/* translators: %s: WolfThemes services page URL */
					__( '<strong>Do you want us to take care of everything for you? <a target="_blank" href="%s">Learn more &rarr;</a></strong>', 'deadlift' )
				),
				'https://wolfthemes.com/services'
			);
		?>
		</p>
		<hr>
	</div><!-- .deadlift-ocdi-intro-text -->
	<?php
	return ob_get_clean();
}
add_filter( 'pt-ocdi/plugin_intro_text', 'deadlift_plugin_intro_text' );

/**
 * 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.(com|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;
	}
}
add_action( 'pt-ocdi/after_import', 'deadlift_replace_content_urls_after_import' );

/**
 * Replace hard coded URLs from demo data to local URL
 */
function deadlift_replace_menu_item_custom_urls_after_import() {

	/* Update hard coded links in menu items */
	$main_menu       = get_term_by( 'name', 'Primary Menu', 'nav_menu' );
	$demo_url_reg_ex = '/(http|https)?:\/\/([a-z0-9.]+)\wolfthemes.com/';

	if ( $main_menu ) {

		$nav_items = wp_get_nav_menu_items( $main_menu->term_id );

		foreach ( $nav_items as $nav_item ) {

			if ( 'custom' === $nav_item->type ) {

				$nav_item_url = $nav_item->url;
				if ( preg_match( $demo_url_reg_ex, $nav_item_url, $matches ) ) {

					if ( isset( $matches[0] ) ) {
						$deadlift_root_url = $matches[0];

						$site_url  = home_url( '/' ); // current site url.
						$url_array = explode( '/', $nav_item_url );

						if ( isset( $url_array[3] ) ) {
							$demo_slug = $url_array[3];

							$deadlift_url    = $deadlift_root_url . '/' . $demo_slug . '/';
							$new_nav_item_url = str_replace( $deadlift_url, $site_url, $nav_item_url );
							$menu_item_db_id  = $nav_item->ID;
							update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw( $new_nav_item_url ) );
						}
					}
				}
			}
		}
	}
}
add_action( 'pt-ocdi/after_import', 'deadlift_replace_menu_item_custom_urls_after_import' );

/**
 * Remove image mods like logos
 *
 * As they logo image previews don't appear after import, it may be confusing for users
 * We will remove the logo mods until it's fixed by the cusomizer import/export plugin or WordPress core
 */
function deadlift_remove_mods_after_import() {
	remove_theme_mod( 'logo_dark' );
	remove_theme_mod( 'logo_light' );
	remove_theme_mod( 'logo_svg' );
	remove_theme_mod( 'custom_css' );
	remove_theme_mod( 'wp_css' );
}
add_action( 'pt-ocdi/after_import', 'deadlift_remove_mods_after_import' );

/**
 * Set permalinks after import
 */
function deadlift_set_permalinks_and_flags_after_import() {

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

	/* Set pretty permalinks if they're not set yet */
		update_option( 'permalink_structure', '/%year%/%monthnum%/%postname%/' );
		flush_rewrite_rules();
}
add_action( 'pt-ocdi/after_import', 'deadlift_set_permalinks_and_flags_after_import' );

/**
 * Generate Elementor CSS cache after demo data import
 */
function deadlift_generate_elementor_css_cache() {
	if ( did_action( 'elementor/loaded' ) ) {
		\Elementor\Plugin::instance()->files_manager->clear_cache();
	}
}
add_action( 'pt-ocdi/after_import', 'deadlift_generate_elementor_css_cache' );

/**
 * Flush rewrite rules after import
 *
 * Make sure all CPT are registered
 */
function deadlift_flush_rewrite_rules_after_import() {
	update_option( 'permalink_structure', '/%year%/%monthnum%/%postname%/' );
	flush_rewrite_rules();
}
add_action( 'pt-ocdi/after_content_import_execution', 'deadlift_flush_rewrite_rules_after_import' );

/**
 * Default time of one AJAX call
 *
 * @return void
 */
function deadlift_ocdi_change_time_of_single_ajax_call() {
	return 50;
}
add_filter( 'ocdi/time_for_one_ajax_call', 'deadlift_ocdi_change_time_of_single_ajax_call' );

Page not found – Hello World !