Repository URL to install this package:
<?php
/**
* This file has helper functions for the Site Library.
*
* @package GP Premium
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // No direct access, please.
}
/**
* Site Library helper class.
*/
class GeneratePress_Site_Library_Helper {
/**
* Instance.
*
* @access private
* @var object Instance
* @since 1.6
*/
private static $instance;
/**
* Background processing.
*
* @access private
* @var object Instance
* @since 1.6
*/
public static $background_process;
/**
* Initiator.
*
* @since 1.6
* @return object initialized object of class.
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Get mapped term IDs.
*/
public static function get_mapped_term_ids() {
return (array) get_transient( 'generatepress_sites_mapped_term_ids' );
}
/**
* Get mapped post IDs.
*
* @param string $slug The slug of the post.
*/
public static function get_mapped_post_ids( $slug ) {
return (array) get_option( "generatepress_sites_mapped_ids_{$slug}", array() );
}
/**
* Moves the existing widgets into the inactive area before new widgets are added.
* This prevents sites looking messed up due to existing widgets.
*
* @since 1.6
*/
public static function clear_widgets() {
$data = get_option( 'sidebars_widgets' );
$backup_data = get_option( '_generatepress_site_library_backup', array() );
$backup_data['widgets'] = $data;
// Set our backed up options.
update_option( '_generatepress_site_library_backup', $backup_data );
$all_widgets = array();
foreach ( $data as $sidebar_id => $widgets ) {
// Skip inactive widgets (should not be in export file).
if ( 'wp_inactive_widgets' === $sidebar_id ) {
continue;
}
if ( is_array( $widgets ) ) {
foreach ( $widgets as $widget_instance_id => $widget ) {
$all_widgets[] = $widget;
}
}
}
$results['wp_inactive_widgets'] = $all_widgets;
update_option( 'sidebars_widgets', $results );
}
/**
* Checks to see whether options exist or not.
*
* @since 1.8
*
* @return bool
*/
public static function do_options_exist() {
$theme_mods = self::get_theme_mods();
$settings = self::get_theme_settings();
$has_data = array(
'mods' => array(),
'options' => array(),
);
foreach ( $theme_mods as $theme_mod ) {
if ( get_theme_mod( $theme_mod ) ) {
$has_data['mods'][ $theme_mod ] = get_theme_mod( $theme_mod );
}
}
foreach ( $settings as $setting ) {
if ( get_option( $setting ) ) {
// The blog module runs a migration script on activation for now. This checks if those migrated values have been changed.
if ( 'generate_blog_settings' === $setting && function_exists( 'generate_blog_get_defaults' ) ) {
$defaults = generate_blog_get_defaults();
$options = get_option( $setting );
$diff = array();
foreach ( $options as $option => $value ) {
if ( isset( $defaults[ $option ] ) && $value !== $defaults[ $option ] ) {
$diff[ $option ] = $value;
}
}
if ( empty( $diff ) ) {
continue;
}
}
$has_data['options'][ $setting ] = get_option( $setting );
}
}
if ( ! array_filter( $has_data ) ) {
return false;
} else {
return true;
}
}
/**
* Imports our content and custom CSS.
*
* @since 1.6
* @param string $path Path to the file.
* @param string $slug File slug.
*/
public static function import_xml( $path, $slug ) {
if ( ! class_exists( 'WP_Importer' ) ) {
require_once ABSPATH . '/wp-admin/includes/class-wp-importer.php';
}
require_once GP_PREMIUM_DIR_PATH . 'site-library/libs/wxr-importer/WXRImporter.php';
require_once GP_PREMIUM_DIR_PATH . 'site-library/libs/wxr-importer/WPImporterLogger.php';
require_once GP_PREMIUM_DIR_PATH . 'site-library/libs/wxr-importer/WXRImportInfo.php';
require_once GP_PREMIUM_DIR_PATH . 'site-library/classes/class-content-importer.php';
if ( ! function_exists( 'wp_crop_image' ) ) {
require_once ABSPATH . 'wp-admin/includes/image.php';
}
add_filter( 'upload_mimes', array( __CLASS__, 'mime_types' ) );
add_filter( 'wp_check_filetype_and_ext', array( __CLASS__, 'check_real_mime_type' ), 10, 4 );
add_filter( 'wp_prepare_attachment_for_js', array( __CLASS__, 'add_svg_image_support' ), 10, 3 );
$options = array(
'fetch_attachments' => true,
'default_author' => 0,
);
$current_css = wp_get_custom_css_post();
$logger = new GeneratePress\WPContentImporter2\WPImporterLogger();
$importer = new GeneratePress_Sites_Content_Importer( $options );
$importer->set_logger( $logger );
$result = $importer->import( $path );
// Get all mapped post and term data.
$existing_data = self::get_mapped_post_ids( $slug );
$mapped_data = $importer->get_importer_data();
$mapped_posts = $mapped_data['mapping']['post'];
// Merge exiting mapped posts with any new ones. Existing posts don't get mapped, so we need to preserve them.
$all_data = $mapped_posts + $existing_data;
// Set our site specific mapped posts with all of our data.
update_option( 'generatepress_sites_mapped_ids_' . $slug, $all_data, false );
// Set mapped term IDs.
// These are always the same, even if the site has been imported before. No fancy stuff needed.
$term_mapping = $mapped_data['mapping']['term_id'];
set_transient( 'generatepress_sites_mapped_term_ids', $term_mapping, 0.1 * HOUR_IN_SECONDS );
wp_update_custom_css_post( $current_css->post_content );
// Page builders need so much extra work.
self::update_page_builder_content();
remove_filter( 'upload_mimes', array( __CLASS__, 'mime_types' ) );
remove_filter( 'wp_check_filetype_and_ext', array( __CLASS__, 'check_real_mime_type' ), 10, 4 );
remove_filter( 'wp_prepare_attachment_for_js', array( __CLASS__, 'add_svg_image_support' ), 10, 3 );
}
/**
* List plugins that have a pro version.
*
* We want to check to see if these exist before installing or activating
* the free versions.
*
* @since 1.6
*
* @return array
*/
public static function check_for_pro_plugins() {
return apply_filters(
'generate_sites_pro_plugins',
array(
'beaver-builder-lite-version/fl-builder.php' => 'bb-plugin/fl-builder.php',
'ultimate-addons-for-beaver-builder-lite/bb-ultimate-addon.php' => 'bb-ultimate-addon/bb-ultimate-addon.php',
'powerpack-addon-for-beaver-builder/bb-powerpack-lite.php' => 'bbpowerpack/bb-powerpack.php',
)
);
}
/**
* Check to see if required plugins are active.
*
* @since 1.6
*
* @param string $plugin The plugin to check for.
*/
public static function is_plugin_installed( $plugin ) {
$pro_plugins = self::check_for_pro_plugins();
// Check to see if this plugin has a pro version.
if ( array_key_exists( $plugin, $pro_plugins ) ) {
if ( file_exists( WP_PLUGIN_DIR . '/' . $pro_plugins[ $plugin ] ) ) {
return true;
}
}
if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin ) ) {
return true;
}
return false;
}
/**
* Allow SVG images.
*
* @since 1.6
*
* @param array $response Attachment response.
* @param object $attachment Attachment object.
* @param array $meta Attachment meta data.
*/
public static function add_svg_image_support( $response, $attachment, $meta ) {
if ( ! function_exists( 'simplexml_load_file' ) ) {
return $response;
}
if ( ! empty( $response['sizes'] ) ) {
return $response;
}
if ( 'image/svg+xml' !== $response['mime'] ) {
return $response;
}
$svg_path = get_attached_file( $attachment->ID );
$dimensions = self::get_svg_dimensions( $svg_path );
$response['sizes'] = array(
'full' => array(
'url' => $response['url'],
'width' => $dimensions->width,
'height' => $dimensions->height,
'orientation' => $dimensions->width > $dimensions->height ? 'landscape' : 'portrait',
),
);
return $response;
}
/**
* Get the dimensions of the uploaded SVG.
*
* @since 1.6
*
* @param string $svg SVG file path.
* @return array Return SVG file height & width for valid SVG file.
*/
public static function get_svg_dimensions( $svg ) {
$svg = simplexml_load_file( $svg );
if ( false === $svg ) {
$width = '0';
$height = '0';
} else {
$attributes = $svg->attributes();
$width = (string) $attributes->width;
$height = (string) $attributes->height;
}
return (object) array(
'width' => $width,
'height' => $height,
);
}
/**
* Taken from the core media_sideload_image function and
* modified to return an array of data instead of html.
*
* @since 1.6
*
* @param string $file The image file path.
* @return array An array of image data.
*/
public static function sideload_image( $file ) {
$data = new stdClass();
if ( ! function_exists( 'media_handle_sideload' ) ) {
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
}
Loading ...