'',
'name' => '',
'preview_url' => '',
'author_name' => '',
'author_url' => '',
'icon' => 'icon.png',
'screenshot' => 'screenshot.png',
'page_builder' => array(),
'uploads_url' => array(),
'min_version' => GP_PREMIUM_VERSION,
'plugins' => '',
'documentation' => '',
)
);
$this->helpers = new GeneratePress_Sites_Helper();
$this->directory = trailingslashit( $config['directory'] );
$provider = parse_url( $this->directory ); // phpcs:ignore -- Prefer parse_url().
if ( ! isset( $provider['host'] ) ) {
return;
}
if ( ! in_array( $provider['host'], (array) get_transient( 'generatepress_sites_trusted_providers' ) ) ) {
return;
}
$this->name = $config['name'];
$this->slug = str_replace( ' ', '_', strtolower( $this->name ) );
$this->preview_url = $config['preview_url'];
$this->author_name = $config['author_name'];
$this->author_url = $config['author_url'];
$this->description = $config['description'];
$this->icon = $config['icon'];
$this->screenshot = $config['screenshot'];
$this->page_builder = $config['page_builder'];
$this->min_version = $config['min_version'];
$this->uploads_url = $config['uploads_url'];
$this->plugins = $config['plugins'];
$this->documentation = $config['documentation'];
$this->installable = true;
if ( empty( $this->min_version ) ) {
$this->min_version = GP_PREMIUM_VERSION;
}
if ( version_compare( GP_PREMIUM_VERSION, $config['min_version'], '<' ) ) {
$this->installable = false;
}
add_action( 'generate_inside_sites_container', array( $this, 'build_box' ) );
add_action( "wp_ajax_generate_setup_demo_content_{$this->slug}", array( $this, 'setup_demo_content' ), 10, 0 );
add_action( "wp_ajax_generate_check_plugins_{$this->slug}", array( $this, 'check_plugins' ), 10, 0 );
add_action( "wp_ajax_generate_backup_options_{$this->slug}", array( $this, 'backup_options' ), 10, 0 );
add_action( "wp_ajax_generate_import_options_{$this->slug}", array( $this, 'import_options' ), 10, 0 );
add_action( "wp_ajax_generate_activate_plugins_{$this->slug}", array( $this, 'activate_plugins' ), 10, 0 );
add_action( "wp_ajax_generate_import_site_options_{$this->slug}", array( $this, 'import_site_options' ), 10, 0 );
add_action( "wp_ajax_generate_download_content_{$this->slug}", array( $this, 'download_content' ), 10, 0 );
add_action( "wp_ajax_generate_import_content_{$this->slug}", array( $this, 'import_content' ), 10, 0 );
add_action( "wp_ajax_generate_import_widgets_{$this->slug}", array( $this, 'import_widgets' ), 10, 0 );
// Don't do the WC setup. This wouldn't be necessary if they used an activation hook.
add_filter( 'woocommerce_prevent_automatic_wizard_redirect', '__return_true' );
}
/**
* Build the site details, including the screenshot and description.
*
* @since 1.6
*/
public function site_details() {
printf(
'
',
esc_attr( $this->name )
);
?>
documentation ) : ?>
author_name && 'GeneratePress' !== $this->author_name ) : ?>
$this->slug,
'preview_url' => $this->preview_url,
'plugins' => $this->plugins,
);
$page_builders = array();
foreach ( (array) $this->page_builder as $builder ) {
$page_builders = str_replace( ' ', '-', strtolower( $builder ) );
}
$site_classes = array(
'site-box',
$page_builders,
! $this->installable ? 'disabled-site' : '',
);
?>
author_name ); ?>
name ); ?>
site_controls(); ?>
site_details(); ?>
'',
'disabled' => 'disabled',
'style' => ! $options ? 'display: none;' : '',
)
);
submit_button(
__( 'Import Options', 'gp-premium' ),
'button-primary import-options site-action',
'submit',
false,
array(
'id' => '',
'style' => $options ? 'display:none' : '',
)
);
submit_button(
__( 'Import Content', 'gp-premium' ),
'button-primary import-content site-action',
'submit',
false,
array(
'id' => '',
'disabled' => 'disabled',
'style' => 'display: none;',
)
);
submit_button(
__( 'View Your Site', 'gp-premium' ),
'button-primary view-site',
'submit',
false,
array(
'id' => '',
'style' => 'display: none;',
)
);
}
/**
* Backup our existing GeneratePress options.
*
* @since 1.6
*/
public function backup_options() {
check_ajax_referer( 'generate_sites_nonce', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'You are not allowed to perform this action', 'gp-premium' ) );
return; // Just in case.
}
$theme_mods = GeneratePress_Sites_Helper::get_theme_mods();
$settings = GeneratePress_Sites_Helper::get_theme_settings();
$data = array(
'mods' => array(),
'options' => array(),
);
foreach ( $theme_mods as $theme_mod ) {
$data['mods'][ $theme_mod ] = get_theme_mod( $theme_mod );
}
foreach ( $settings as $setting ) {
$data['options'][ $setting ] = get_option( $setting );
}
echo wp_json_encode( $data );
die();
}
/**
* Tells our JS which files exist.
*
* @since 1.8
*/
public function setup_demo_content() {
check_ajax_referer( 'generate_sites_nonce', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'You are not allowed to perform this action', 'gp-premium' ) );
return; // Just in case.
}
$settings = GeneratePress_Sites_Helper::get_options( $this->directory . 'options.json' );
$data['plugins'] = $settings['plugins'];
if ( GeneratePress_Sites_Helper::file_exists( $this->directory . 'content.xml' ) ) {
$data['content'] = true;
} else {
$data['content'] = false;
}
if ( GeneratePress_Sites_Helper::file_exists( $this->directory . 'widgets.wie' ) ) {
$data['widgets'] = true;
} else {
$data['widgets'] = false;
}
// Backup our plugins early.
$backup_data = get_option( '_generatepress_site_library_backup', array() );
$backup_data['plugins'] = get_option( 'active_plugins', array() );
update_option( '_generatepress_site_library_backup', $backup_data );
wp_send_json( $data );
die();
}
/**
* Import our demo GeneratePress options.
*
* @since 1.6
*/
public function import_options() {
check_ajax_referer( 'generate_sites_nonce', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'You are not allowed to perform this action', 'gp-premium' ) );
return; // Just in case.
}
if ( ! GeneratePress_Sites_Helper::file_exists( $this->directory . 'options.json' ) ) {
wp_send_json_error( __( 'No theme options exist.', 'gp-premium' ) );
}
// Delete existing backup.
delete_option( '_generatepress_site_library_backup' );
// Backup options.
$backup_data = get_option( '_generatepress_site_library_backup', array() );
$theme_mods = GeneratePress_Sites_Helper::get_theme_mods();
$settings = GeneratePress_Sites_Helper::get_theme_settings();
$data = array(
'mods' => array(),
'options' => array(),
);
foreach ( $theme_mods as $theme_mod ) {
$data['mods'][ $theme_mod ] = get_theme_mod( $theme_mod );
}
foreach ( $settings as $setting ) {
$data['options'][ $setting ] = get_option( $setting );
}
$backup_data['theme_options'] = $data;
$modules = generatepress_get_site_premium_modules();
$active_modules = array();
foreach ( $modules as $name => $key ) {
if ( 'activated' === get_option( $key ) ) {
$active_modules[ $name ] = $key;
}
}
$backup_data['modules'] = $active_modules;
$settings = GeneratePress_Sites_Helper::get_options( $this->directory . 'options.json' );
// Remove all existing theme options.
$option_keys = array(
'generate_settings',
'generate_background_settings',
'generate_blog_settings',
'generate_hooks',
'generate_page_header_settings',
'generate_secondary_nav_settings',
'generate_spacing_settings',
'generate_menu_plus_settings',
'generate_woocommerce_settings',
);
foreach ( $option_keys as $key ) {
delete_option( $key );
}
// Need to backup these items before we remove all theme mods.
$backup_data['site_options']['nav_menu_locations'] = get_theme_mod( 'nav_menu_locations' );
$backup_data['site_options']['custom_logo'] = get_theme_mod( 'custom_logo' );
// Remove existing theme mods.
remove_theme_mods();
// Remove existing activated premium modules.
$premium_modules = generatepress_get_site_premium_modules();
foreach ( $premium_modules as $name => $key ) {
delete_option( $key );
}
// Activate necessary modules.
foreach ( $settings['modules'] as $name => $key ) {
// Only allow valid premium modules.
if ( ! in_array( $key, $premium_modules ) ) {
GeneratePress_Sites_Helper::log( 'Bad premium module key: ' . $key );
continue;
}
update_option( $key, 'activated' );
}
// Set theme mods.
foreach ( $settings['mods'] as $key => $val ) {
// Only allow valid theme mods.
if ( ! in_array( $key, GeneratePress_Sites_Helper::get_theme_mods() ) ) {
GeneratePress_Sites_Helper::log( 'Bad theme mod key: ' . $key );
continue;
}
set_theme_mod( $key, $val );
}
// Set theme options.
foreach ( $settings['options'] as $key => $val ) {
// Only allow valid options.
if ( ! in_array( $key, GeneratePress_Sites_Helper::get_theme_settings() ) ) {
GeneratePress_Sites_Helper::log( 'Bad theme setting key: ' . $key );
continue;
}
// Import any images.
if ( is_array( $val ) || is_object( $val ) ) {
foreach ( $val as $option_name => $option_value ) {
if ( is_string( $option_value ) && preg_match( '/\.(jpg|jpeg|png|gif)/i', $option_value ) ) {
$data = GeneratePress_Sites_Helper::sideload_image( $option_value );
if ( ! is_wp_error( $data ) ) {
$val[ $option_name ] = $data->url;
}
}
}
}
update_option( $key, $val );
}
// Remove dynamic CSS cache.
delete_option( 'generate_dynamic_css_output' );
delete_option( 'generate_dynamic_css_cached_version' );
$dynamic_css_data = get_option( 'generatepress_dynamic_css_data', array() );
if ( isset( $dynamic_css_data['updated_time'] ) ) {
unset( $dynamic_css_data['updated_time'] );
}
update_option( 'generatepress_dynamic_css_data', $dynamic_css_data );
// Custom CSS.
$css = $settings['custom_css'];
$css = '/* GeneratePress Site CSS */ ' . $css . ' /* End GeneratePress Site CSS */';
$current_css = wp_get_custom_css_post();
if ( isset( $current_css->post_content ) ) {
preg_match( '#(/\* GeneratePress Site CSS).*?(End GeneratePress Site CSS \*/)#s', $current_css->post_content, $matches );
if ( ! empty( $matches ) ) {
$backup_data['css'] = $matches[0];
}
$current_css->post_content = preg_replace( '#(/\\* GeneratePress Site CSS \\*/).*?(/\\* End GeneratePress Site CSS \\*/)#s', '', $current_css->post_content );
$css = $current_css->post_content . $css;
}
wp_update_custom_css_post( $css );
update_option( '_generatepress_site_library_backup', $backup_data );
die();
}
/**
* Download the content from the .xml file.
*/
public function download_content() {
check_ajax_referer( 'generate_sites_nonce', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'You are not allowed to perform this action', 'gp-premium' ) );
return; // Just in case.
}
// Increase PHP max execution time.
set_time_limit( apply_filters( 'generate_sites_content_import_time_limit', 300 ) );
$xml_path = $this->directory . 'content.xml';
$xml_file = GeneratePress_Sites_Helper::download_file( $xml_path );
$xml_path = $xml_file['data']['file'];
if ( file_exists( $xml_path ) ) {
set_transient( 'generatepress_sites_content_file', $xml_path, HOUR_IN_SECONDS );
}
die();
}
/**
* Import our demo content.
*
* @since 1.6
*/
public function import_content() {
check_ajax_referer( 'generate_sites_nonce', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'You are not allowed to perform this action', 'gp-premium' ) );
return; // Just in case.
}
// Increase PHP max execution time.
set_time_limit( apply_filters( 'generate_sites_content_import_time_limit', 300 ) );
// Disable import of authors.
add_filter( 'wxr_importer.pre_process.user', '__return_false' );
// Keep track of our progress.
add_action( 'wxr_importer.processed.post', array( $this, 'track_post' ) );
add_action( 'wxr_importer.processed.term', array( $this, 'track_term' ) );
// Disables generation of multiple image sizes (thumbnails) in the content import step.
if ( ! apply_filters( 'generate_sites_regen_thumbnails', true ) ) {
add_filter( 'intermediate_image_sizes_advanced', '__return_null' );
}
$backup_data = get_option( '_generatepress_site_library_backup', array() );
$backup_data['content'] = true;
update_option( '_generatepress_site_library_backup', $backup_data );
// Import content.
$content = get_transient( 'generatepress_sites_content_file' );
if ( $content ) {
GeneratePress_Sites_Helper::import_xml( $content, $this->slug );
delete_transient( 'generatepress_sites_content_file' );
}
die();
}
/**
* Import our widgets.
*
* @since 1.6
*/
public function import_widgets() {
check_ajax_referer( 'generate_sites_nonce', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'You are not allowed to perform this action', 'gp-premium' ) );
return; // Just in case.
}
$widgets_path = $this->directory . 'widgets.wie';
$wie_file = GeneratePress_Sites_Helper::download_file( $widgets_path );
$wie_path = $wie_file['data']['file'];
$data = implode( '', file( $wie_path ) );
$data = json_decode( $data );
GeneratePress_Sites_Helper::clear_widgets();
$widgets_importer = GeneratePress_Sites_Widget_Importer::instance();
$widgets_importer->wie_import_data( $data );
die();
}
/**
* Import any necessary site options.
*
* @since 1.6
*/
public function import_site_options() {
check_ajax_referer( 'generate_sites_nonce', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'You are not allowed to perform this action', 'gp-premium' ) );
return; // Just in case.
}
$backup_data = get_option( '_generatepress_site_library_backup', array() );
$settings = GeneratePress_Sites_Helper::get_options( $this->directory . 'options.json' );
delete_option( 'generate_page_header_global_locations' );
foreach ( $settings['site_options'] as $key => $val ) {
switch ( $key ) {
case 'page_for_posts':
case 'page_on_front':
$backup_data['site_options'][ $key ] = get_option( $key );
GeneratePress_Sites_Helper::set_reading_pages( $key, $val, $this->slug );
break;
case 'woocommerce_shop_page_id':
case 'woocommerce_cart_page_id':
case 'woocommerce_checkout_page_id':
case 'woocommerce_myaccount_page_id':
$backup_data['site_options'][ $key ] = get_option( $key );
GeneratePress_Sites_Helper::set_woocommerce_pages( $key, $val, $this->slug );
break;
case 'nav_menu_locations':
GeneratePress_Sites_Helper::set_nav_menu_locations( $val );
break;
case 'page_header_global_locations':
GeneratePress_Sites_Helper::set_global_page_header_locations( $val, $this->slug );
break;
case 'page_headers':
GeneratePress_Sites_Helper::set_page_headers( $val, $this->slug );
break;
case 'element_locations':
GeneratePress_Sites_Helper::set_element_locations( $val, $this->slug );
break;
case 'element_exclusions':
GeneratePress_Sites_Helper::set_element_exclusions( $val, $this->slug );
break;
case 'custom_logo':
$data = GeneratePress_Sites_Helper::sideload_image( $val );
if ( ! is_wp_error( $data ) && isset( $data->attachment_id ) ) {
set_theme_mod( 'custom_logo', $data->attachment_id );
update_post_meta( $data->attachment_id, '_wp_attachment_is_custom_header', get_option( 'stylesheet' ) );
} else {
remove_theme_mod( 'custom_logo' );
}
break;
default:
if ( in_array( $key, (array) generatepress_sites_disallowed_options() ) ) {
GeneratePress_Sites_Helper::log( 'Disallowed option: ' . $key );
} else {
$backup_data['site_options'][ $key ] = get_option( $key );
delete_option( $key );
update_option( $key, $val );
}
break;
}
}
// Set our backed up options.
update_option( '_generatepress_site_library_backup', $backup_data );
// Update any custom menu link URLs.
GeneratePress_Sites_Helper::update_menu_urls( $this->preview_url );
// Clear page builder cache.
GeneratePress_Sites_Helper::clear_page_builder_cache();
wp_send_json( __( 'Site options imported', 'gp-premium' ) );
die();
}
/**
* Activates our freshly installed plugins.
*
* @since 1.6
*/
public function activate_plugins() {
check_ajax_referer( 'generate_sites_nonce', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'You are not allowed to perform this action', 'gp-premium' ) );
return; // Just in case.
}
$settings = GeneratePress_Sites_Helper::get_options( $this->directory . 'options.json' );
$plugins = $settings['plugins'];
if ( ! empty( $plugins ) ) {
$pro_plugins = GeneratePress_Sites_Helper::check_for_pro_plugins();
foreach ( $plugins as $plugin ) {
// If the plugin has a pro version and it exists, activate it instead.
if ( array_key_exists( $plugin, $pro_plugins ) ) {
if ( file_exists( WP_PLUGIN_DIR . '/' . $pro_plugins[ $plugin ] ) ) {
$plugin = $pro_plugins[ $plugin ];
}
}
// Install BB lite if pro doesn't exist.
if ( 'bb-plugin/fl-builder.php' === $plugin && ! file_exists( WP_PLUGIN_DIR . '/bb-plugin/fl-builder.php' ) ) {
$plugin = 'beaver-builder-lite-version/fl-builder.php';
}
if ( ! is_plugin_active( $plugin ) ) {
activate_plugin( $plugin, '', false, true );
}
}
wp_send_json( __( 'Plugins activated', 'gp-premium' ) );
}
die();
}
/**
* Checks a few things:
* 1. Is the plugin installed already?
* 2. Is the plugin active already?
* 3. Can the plugin be downloaded from WordPress.org?
*
* @since 1.6
*/
public function check_plugins() {
check_ajax_referer( 'generate_sites_nonce', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'You are not allowed to perform this action', 'gp-premium' ) );
return; // Just in case.
}
if ( GeneratePress_Sites_Helper::file_exists( $this->directory . 'options.json' ) ) {
$data['options'] = true;
$settings = GeneratePress_Sites_Helper::get_options( $this->directory . 'options.json' );
$data['modules'] = $settings['modules'];
$data['plugins'] = $settings['plugins'];
if ( ! is_array( $data['plugins'] ) ) {
return;
}
$plugin_data = array();
foreach ( $data['plugins'] as $name => $slug ) {
$basename = strtok( $slug, '/' );
$plugin_data[ $name ] = array(
'name' => $name,
'slug' => $slug,
'installed' => GeneratePress_Sites_Helper::is_plugin_installed( $slug ) ? true : false,
'active' => is_plugin_active( $slug ) ? true : false,
'repo' => GeneratePress_Sites_Helper::file_exists( 'https://api.wordpress.org/plugins/info/1.0/' . $basename ) ? true : false,
);
}
$data['plugin_data'] = $plugin_data;
}
wp_send_json(
array(
'plugins' => $data['plugins'],
'plugin_data' => $data['plugin_data'],
)
);
die();
}
/**
* Track Imported Post
*
* @param int $post_id Post ID.
* @return void
*/
public function track_post( $post_id ) {
update_post_meta( $post_id, '_generatepress_sites_imported_post', true );
}
/**
* Track Imported Term
*
* @param int $term_id Term ID.
* @return void
*/
public function track_term( $term_id ) {
$term = get_term( $term_id );
update_term_meta( $term_id, '_generatepress_sites_imported_term', true );
}
}