updated plugin GP Premium
version 2.0.3
This commit is contained in:
@ -0,0 +1,163 @@
|
||||
<?php
|
||||
/**
|
||||
* This file handles Beaver Builder functionality during import.
|
||||
*
|
||||
* @package GP Premium
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // No direct access, please.
|
||||
}
|
||||
|
||||
/**
|
||||
* Search Beaver Builder content for images to download.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
class GeneratePress_Sites_Process_Beaver_Builder {
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->image_importer = new GeneratePress_Sites_Image_Importer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Import
|
||||
*
|
||||
* @since 1.6
|
||||
* @return void
|
||||
*/
|
||||
public function import() {
|
||||
GeneratePress_Site_Library_Helper::log( '== Start Processing Beaver Builder Images ==' );
|
||||
|
||||
$post_ids = GeneratePress_Site_Library_Helper::get_all_posts();
|
||||
|
||||
if ( is_array( $post_ids ) ) {
|
||||
foreach ( $post_ids as $post_id ) {
|
||||
$this->import_single_post( $post_id );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update post meta.
|
||||
*
|
||||
* @param integer $post_id Post ID.
|
||||
* @return void
|
||||
*/
|
||||
public function import_single_post( $post_id = 0 ) {
|
||||
|
||||
if ( ! empty( $post_id ) ) {
|
||||
|
||||
// Get page builder data.
|
||||
$data = get_post_meta( $post_id, '_fl_builder_data', true );
|
||||
|
||||
if ( ! empty( $data ) ) {
|
||||
foreach ( $data as $key => $el ) {
|
||||
// Import background images.
|
||||
if ( 'row' === $el->type || 'column' === $el->type ) {
|
||||
$data[ $key ]->settings = $this->import_background_images( $el->settings );
|
||||
}
|
||||
|
||||
// Import module images.
|
||||
if ( 'module' === $el->type ) {
|
||||
$data[ $key ]->settings = $this->import_module_images( $el->settings );
|
||||
}
|
||||
}
|
||||
|
||||
// Update page builder data.
|
||||
update_post_meta( $post_id, '_fl_builder_data', $data );
|
||||
update_post_meta( $post_id, '_fl_builder_draft', $data );
|
||||
|
||||
// Clear all cache.
|
||||
FLBuilderModel::delete_asset_cache_for_all_posts();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Import Module Images.
|
||||
*
|
||||
* @param object $settings Module settings object.
|
||||
* @return object
|
||||
*/
|
||||
public function import_module_images( $settings ) {
|
||||
|
||||
/**
|
||||
* 1) Set photos.
|
||||
*/
|
||||
$settings = $this->import_photo( $settings );
|
||||
|
||||
/**
|
||||
* 2) Set `$settings->data` for Only type 'image-icon'
|
||||
*
|
||||
* @todo Remove the condition `'image-icon' === $settings->type` if `$settings->data` is used only for the Image Icon.
|
||||
*/
|
||||
if ( isset( $settings->data ) && isset( $settings->photo ) && ! empty( $settings->photo ) && 'image-icon' === $settings->type ) {
|
||||
$settings->data = FLBuilderPhoto::get_attachment_data( $settings->photo );
|
||||
}
|
||||
|
||||
/**
|
||||
* 3) Set `list item` module images
|
||||
*/
|
||||
if ( isset( $settings->add_list_item ) ) {
|
||||
foreach ( $settings->add_list_item as $key => $value ) {
|
||||
$settings->add_list_item[ $key ] = $this->import_photo( $value );
|
||||
}
|
||||
}
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper: Import BG Images.
|
||||
*
|
||||
* @param object $settings Row settings object.
|
||||
* @return object
|
||||
*/
|
||||
public function import_background_images( $settings ) {
|
||||
|
||||
if ( ! empty( $settings->bg_image ) && ! empty( $settings->bg_image_src ) ) {
|
||||
$image = array(
|
||||
'url' => $settings->bg_image_src,
|
||||
'id' => $settings->bg_image,
|
||||
);
|
||||
|
||||
$downloaded_image = $this->image_importer->import( $image );
|
||||
|
||||
$settings->bg_image_src = $downloaded_image['url'];
|
||||
$settings->bg_image = $downloaded_image['id'];
|
||||
}
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper: Import Photo.
|
||||
*
|
||||
* @param object $settings Row settings object.
|
||||
* @return object
|
||||
*/
|
||||
public function import_photo( $settings ) {
|
||||
|
||||
if ( ! empty( $settings->photo ) && ! empty( $settings->photo_src ) ) {
|
||||
$image = array(
|
||||
'url' => $settings->photo_src,
|
||||
'id' => $settings->photo,
|
||||
);
|
||||
|
||||
$downloaded_image = $this->image_importer->import( $image );
|
||||
|
||||
$settings->photo_src = $downloaded_image['url'];
|
||||
$settings->photo = $downloaded_image['id'];
|
||||
}
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* This file extends the Content Importer.
|
||||
*
|
||||
* @package GP Premium
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // No direct access, please.
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend the Importer.
|
||||
*/
|
||||
class GeneratePress_Sites_Content_Importer extends GeneratePress\WPContentImporter2\WXRImporter {
|
||||
/**
|
||||
* Constructor method.
|
||||
*
|
||||
* @param array $options Importer options.
|
||||
*/
|
||||
public function __construct( $options = array() ) {
|
||||
parent::__construct( $options );
|
||||
|
||||
// Set current user to $mapping variable.
|
||||
// Fixes the [WARNING] Could not find the author for ... log warning messages.
|
||||
$current_user_obj = wp_get_current_user();
|
||||
$this->mapping['user_slug'][ $current_user_obj->user_login ] = $current_user_obj->ID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all protected variables from the WXR_Importer needed for continuing the import.
|
||||
*/
|
||||
public function get_importer_data() {
|
||||
return array(
|
||||
'mapping' => $this->mapping,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets all protected variables from the WXR_Importer needed for continuing the import.
|
||||
*
|
||||
* @param array $data with set variables.
|
||||
*/
|
||||
public function set_importer_data( $data ) {
|
||||
// phpcs:ignore -- Commented out code for now.
|
||||
// $this->mapping = empty( $data['mapping'] ) ? array() : $data['mapping'];
|
||||
// $this->requires_remapping = empty( $data['requires_remapping'] ) ? array() : $data['requires_remapping'];
|
||||
}
|
||||
}
|
@ -0,0 +1,200 @@
|
||||
<?php
|
||||
/**
|
||||
* This file handles image imports in the Site Library.
|
||||
*
|
||||
* @package GP Premium
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // No direct access, please.
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads and updates images.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
class GeneratePress_Sites_Image_Importer {
|
||||
|
||||
/**
|
||||
* Images IDs
|
||||
*
|
||||
* @var array The Array of already image IDs.
|
||||
* @since 1.6
|
||||
*/
|
||||
private $already_imported_ids = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
public function __construct() {
|
||||
|
||||
if ( ! function_exists( 'WP_Filesystem' ) ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/file.php';
|
||||
}
|
||||
|
||||
WP_Filesystem();
|
||||
}
|
||||
|
||||
/**
|
||||
* Process Image Download
|
||||
*
|
||||
* @since 1.6
|
||||
* @param array $attachments Attachment array.
|
||||
* @return array Attachment array.
|
||||
*/
|
||||
public function process( $attachments ) {
|
||||
|
||||
$downloaded_images = array();
|
||||
|
||||
foreach ( $attachments as $key => $attachment ) {
|
||||
$downloaded_images[] = $this->import( $attachment );
|
||||
}
|
||||
|
||||
return $downloaded_images;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Hash Image.
|
||||
*
|
||||
* @since 1.6
|
||||
* @param string $attachment_url Attachment URL.
|
||||
* @return string Hash string.
|
||||
*/
|
||||
private function get_hash_image( $attachment_url ) {
|
||||
return sha1( $attachment_url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Saved Image.
|
||||
*
|
||||
* @since 1.6
|
||||
* @param string $attachment Attachment Data.
|
||||
* @return string Hash string.
|
||||
*/
|
||||
private function get_saved_image( $attachment ) {
|
||||
|
||||
global $wpdb;
|
||||
|
||||
// Already imported? Then return!
|
||||
if ( isset( $this->already_imported_ids[ $attachment['id'] ] ) ) {
|
||||
GeneratePress_Site_Library_Helper::log( 'Successfully replaced: ' . $attachment['url'] );
|
||||
|
||||
return $this->already_imported_ids[ $attachment['id'] ];
|
||||
}
|
||||
|
||||
// 1. Is already imported in Batch Import Process?
|
||||
$post_id = $wpdb->get_var(
|
||||
$wpdb->prepare(
|
||||
'SELECT `post_id` FROM `' . $wpdb->postmeta . '`
|
||||
WHERE `meta_key` = \'_generatepress_sites_image_hash\'
|
||||
AND `meta_value` = %s
|
||||
;',
|
||||
$this->get_hash_image( $attachment['url'] )
|
||||
)
|
||||
);
|
||||
|
||||
// 2. Is image already imported though XML?
|
||||
if ( empty( $post_id ) ) {
|
||||
// Get file name without extension.
|
||||
// To check it exist in attachment.
|
||||
$filename = preg_replace( '/\\.[^.\\s]{3,4}$/', '', basename( $attachment['url'] ) );
|
||||
|
||||
$post_id = $wpdb->get_var(
|
||||
$wpdb->prepare(
|
||||
'SELECT `post_id` FROM `' . $wpdb->postmeta . '`
|
||||
WHERE `meta_key` = \'_wp_attached_file\'
|
||||
AND `meta_value` LIKE %s
|
||||
;',
|
||||
'%' . $filename . '%'
|
||||
)
|
||||
);
|
||||
|
||||
GeneratePress_Site_Library_Helper::log( 'Successfully replaced: ' . $attachment['url'] );
|
||||
}
|
||||
|
||||
if ( $post_id ) {
|
||||
$new_attachment = array(
|
||||
'id' => $post_id,
|
||||
'url' => wp_get_attachment_url( $post_id ),
|
||||
);
|
||||
|
||||
$this->already_imported_ids[ $attachment['id'] ] = $new_attachment;
|
||||
|
||||
return $new_attachment;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Import Image
|
||||
*
|
||||
* @since 1.6
|
||||
* @param array $attachment Attachment array.
|
||||
* @return array Attachment array.
|
||||
*/
|
||||
public function import( $attachment ) {
|
||||
|
||||
$saved_image = $this->get_saved_image( $attachment );
|
||||
|
||||
if ( $saved_image ) {
|
||||
return $saved_image;
|
||||
}
|
||||
|
||||
$file_content = wp_remote_retrieve_body( wp_safe_remote_get( $attachment['url'] ) );
|
||||
|
||||
// Empty file content?
|
||||
if ( empty( $file_content ) ) {
|
||||
GeneratePress_Site_Library_Helper::log( 'Failed to replace: ' . $attachment['url'] );
|
||||
GeneratePress_Site_Library_Helper::log( 'Error: Failed wp_remote_retrieve_body().' );
|
||||
|
||||
return $attachment;
|
||||
}
|
||||
|
||||
// Extract the file name and extension from the URL.
|
||||
$filename = basename( $attachment['url'] );
|
||||
|
||||
$upload = wp_upload_bits(
|
||||
$filename,
|
||||
null,
|
||||
$file_content
|
||||
);
|
||||
|
||||
$post = array(
|
||||
'post_title' => $filename,
|
||||
'guid' => $upload['url'],
|
||||
);
|
||||
|
||||
$info = wp_check_filetype( $upload['file'] );
|
||||
|
||||
if ( $info ) {
|
||||
$post['post_mime_type'] = $info['type'];
|
||||
} else {
|
||||
// For now just return the origin attachment.
|
||||
return $attachment;
|
||||
}
|
||||
|
||||
$post_id = wp_insert_attachment( $post, $upload['file'] );
|
||||
|
||||
wp_update_attachment_metadata(
|
||||
$post_id,
|
||||
wp_generate_attachment_metadata( $post_id, $upload['file'] )
|
||||
);
|
||||
|
||||
update_post_meta( $post_id, '_generatepress_sites_image_hash', $this->get_hash_image( $attachment['url'] ) );
|
||||
|
||||
$new_attachment = array(
|
||||
'id' => $post_id,
|
||||
'url' => $upload['url'],
|
||||
);
|
||||
|
||||
GeneratePress_Site_Library_Helper::log( 'Successfully replaced: ' . $attachment['url'] );
|
||||
|
||||
$this->already_imported_ids[ $attachment['id'] ] = $new_attachment;
|
||||
|
||||
return $new_attachment;
|
||||
}
|
||||
}
|
@ -0,0 +1,275 @@
|
||||
<?php
|
||||
/**
|
||||
* This file handles the widget imports.
|
||||
*
|
||||
* @package GP Premium
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // No direct access, please.
|
||||
}
|
||||
|
||||
/**
|
||||
* Widget importer class.
|
||||
*/
|
||||
class GeneratePress_Sites_Widget_Importer {
|
||||
|
||||
/**
|
||||
* Instance.
|
||||
*
|
||||
* @var $_instance
|
||||
*/
|
||||
private static $_instance = null; // phpcs:ignore -- Want the underscore.
|
||||
|
||||
/**
|
||||
* Get our instance.
|
||||
*/
|
||||
public static function instance() {
|
||||
if ( ! isset( self::$_instance ) ) {
|
||||
self::$_instance = new self();
|
||||
}
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Available widgets
|
||||
*
|
||||
* Gather site's widgets into array with ID base, name, etc.
|
||||
* Used by export and import functions.
|
||||
*
|
||||
* @since 0.4
|
||||
* @global array $wp_registered_widget_updates
|
||||
* @return array Widget information
|
||||
*/
|
||||
public function wie_available_widgets() {
|
||||
global $wp_registered_widget_controls;
|
||||
|
||||
$widget_controls = $wp_registered_widget_controls;
|
||||
$available_widgets = array();
|
||||
|
||||
foreach ( $widget_controls as $widget ) {
|
||||
// No duplicates.
|
||||
if ( ! empty( $widget['id_base'] ) && ! isset( $available_widgets[ $widget['id_base'] ] ) ) {
|
||||
$available_widgets[ $widget['id_base'] ]['id_base'] = $widget['id_base'];
|
||||
$available_widgets[ $widget['id_base'] ]['name'] = $widget['name'];
|
||||
}
|
||||
}
|
||||
|
||||
return apply_filters( 'wie_available_widgets', $available_widgets ); // phpcs:ignore -- Keep the plugin prefix.
|
||||
}
|
||||
|
||||
/**
|
||||
* Import widget JSON data
|
||||
*
|
||||
* @since 0.4
|
||||
* @global array $wp_registered_sidebars
|
||||
* @param object $data JSON widget data from .wie file.
|
||||
* @return array Results array
|
||||
*/
|
||||
public function wie_import_data( $data ) {
|
||||
global $wp_registered_sidebars;
|
||||
|
||||
// Have valid data?
|
||||
// If no data or could not decode.
|
||||
if ( empty( $data ) || ! is_object( $data ) ) {
|
||||
wp_die(
|
||||
esc_html__( 'Import data could not be read. Please try a different file.', 'widget-importer-exporter' ),
|
||||
'',
|
||||
array(
|
||||
'back_link' => true,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Hook before import.
|
||||
do_action( 'wie_before_import' ); // phpcs:ignore -- Keep the plugin prefix.
|
||||
|
||||
$data = apply_filters( 'wie_import_data', $data ); // phpcs:ignore -- Keep the plugin prefix.
|
||||
|
||||
// Get all available widgets site supports.
|
||||
$available_widgets = $this->wie_available_widgets();
|
||||
|
||||
// Get all existing widget instances.
|
||||
$widget_instances = array();
|
||||
|
||||
foreach ( $available_widgets as $widget_data ) {
|
||||
$widget_instances[ $widget_data['id_base'] ] = get_option( 'widget_' . $widget_data['id_base'] );
|
||||
}
|
||||
|
||||
// Begin results.
|
||||
$results = array();
|
||||
|
||||
// Loop import data's sidebars.
|
||||
foreach ( $data as $sidebar_id => $widgets ) {
|
||||
// Skip inactive widgets (should not be in export file).
|
||||
if ( 'wp_inactive_widgets' === $sidebar_id ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if sidebar is available on this site.
|
||||
// Otherwise add widgets to inactive, and say so.
|
||||
if ( isset( $wp_registered_sidebars[ $sidebar_id ] ) ) {
|
||||
$sidebar_available = true;
|
||||
$use_sidebar_id = $sidebar_id;
|
||||
$sidebar_message_type = 'success';
|
||||
$sidebar_message = '';
|
||||
} else {
|
||||
$sidebar_available = false;
|
||||
$use_sidebar_id = 'wp_inactive_widgets'; // Add to inactive if sidebar does not exist in theme.
|
||||
$sidebar_message_type = 'error';
|
||||
$sidebar_message = esc_html__( 'Widget area does not exist in theme (using Inactive)', 'widget-importer-exporter' );
|
||||
}
|
||||
|
||||
// Result for sidebar
|
||||
// Sidebar name if theme supports it; otherwise ID.
|
||||
$results[ $sidebar_id ]['name'] = ! empty( $wp_registered_sidebars[ $sidebar_id ]['name'] ) ? $wp_registered_sidebars[ $sidebar_id ]['name'] : $sidebar_id;
|
||||
$results[ $sidebar_id ]['message_type'] = $sidebar_message_type;
|
||||
$results[ $sidebar_id ]['message'] = $sidebar_message;
|
||||
$results[ $sidebar_id ]['widgets'] = array();
|
||||
|
||||
// Loop widgets.
|
||||
foreach ( $widgets as $widget_instance_id => $widget ) {
|
||||
$fail = false;
|
||||
|
||||
// Get id_base (remove -# from end) and instance ID number.
|
||||
$id_base = preg_replace( '/-[0-9]+$/', '', $widget_instance_id );
|
||||
$instance_id_number = str_replace( $id_base . '-', '', $widget_instance_id );
|
||||
|
||||
// Does site support this widget?
|
||||
if ( ! $fail && ! isset( $available_widgets[ $id_base ] ) ) {
|
||||
$fail = true;
|
||||
$widget_message_type = 'error';
|
||||
$widget_message = esc_html__( 'Site does not support widget', 'widget-importer-exporter' ); // Explain why widget not imported.
|
||||
}
|
||||
|
||||
// Filter to modify settings object before conversion to array and import
|
||||
// Leave this filter here for backwards compatibility with manipulating objects (before conversion to array below)
|
||||
// Ideally the newer wie_widget_settings_array below will be used instead of this.
|
||||
$widget = apply_filters( 'wie_widget_settings', $widget ); // phpcs:ignore -- Keep the plugin prefix.
|
||||
|
||||
// Convert multidimensional objects to multidimensional arrays
|
||||
// Some plugins like Jetpack Widget Visibility store settings as multidimensional arrays
|
||||
// Without this, they are imported as objects and cause fatal error on Widgets page
|
||||
// If this creates problems for plugins that do actually intend settings in objects then may need to consider other approach: https://wordpress.org/support/topic/problem-with-array-of-arrays
|
||||
// It is probably much more likely that arrays are used than objects, however.
|
||||
$widget = json_decode( wp_json_encode( $widget ), true );
|
||||
|
||||
// Filter to modify settings array
|
||||
// This is preferred over the older wie_widget_settings filter above
|
||||
// Do before identical check because changes may make it identical to end result (such as URL replacements).
|
||||
$widget = apply_filters( 'wie_widget_settings_array', $widget ); // phpcs:ignore -- Keep the plugin prefix.
|
||||
|
||||
// Does widget with identical settings already exist in same sidebar?
|
||||
if ( ! $fail && isset( $widget_instances[ $id_base ] ) ) {
|
||||
// Get existing widgets in this sidebar.
|
||||
$sidebars_widgets = get_option( 'sidebars_widgets' );
|
||||
$sidebar_widgets = isset( $sidebars_widgets[ $use_sidebar_id ] ) ? $sidebars_widgets[ $use_sidebar_id ] : array(); // Check Inactive if that's where will go.
|
||||
|
||||
// Loop widgets with ID base.
|
||||
$single_widget_instances = ! empty( $widget_instances[ $id_base ] ) ? $widget_instances[ $id_base ] : array();
|
||||
|
||||
foreach ( $single_widget_instances as $check_id => $check_widget ) {
|
||||
// Is widget in same sidebar and has identical settings?
|
||||
if ( in_array( "$id_base-$check_id", $sidebar_widgets, true ) && (array) $widget === $check_widget ) {
|
||||
$fail = true;
|
||||
$widget_message_type = 'warning';
|
||||
// Explain why widget not imported.
|
||||
$widget_message = esc_html__( 'Widget already exists', 'widget-importer-exporter' );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// No failure.
|
||||
if ( ! $fail ) {
|
||||
// Add widget instance.
|
||||
$single_widget_instances = get_option( 'widget_' . $id_base ); // All instances for that widget ID base, get fresh every time.
|
||||
|
||||
$single_widget_instances = ! empty( $single_widget_instances ) ? $single_widget_instances : array(
|
||||
'_multiwidget' => 1, // Start fresh if have to.
|
||||
);
|
||||
|
||||
$single_widget_instances[] = $widget; // Add it.
|
||||
|
||||
// Get the key it was given.
|
||||
end( $single_widget_instances );
|
||||
|
||||
$new_instance_id_number = key( $single_widget_instances );
|
||||
|
||||
// If key is 0, make it 1
|
||||
// When 0, an issue can occur where adding a widget causes data from other widget to load,
|
||||
// and the widget doesn't stick (reload wipes it).
|
||||
if ( '0' === strval( $new_instance_id_number ) ) {
|
||||
$new_instance_id_number = 1;
|
||||
$single_widget_instances[ $new_instance_id_number ] = $single_widget_instances[0];
|
||||
unset( $single_widget_instances[0] );
|
||||
}
|
||||
|
||||
// Move _multiwidget to end of array for uniformity.
|
||||
if ( isset( $single_widget_instances['_multiwidget'] ) ) {
|
||||
$multiwidget = $single_widget_instances['_multiwidget'];
|
||||
unset( $single_widget_instances['_multiwidget'] );
|
||||
$single_widget_instances['_multiwidget'] = $multiwidget;
|
||||
}
|
||||
|
||||
// Update option with new widget.
|
||||
update_option( 'widget_' . $id_base, $single_widget_instances );
|
||||
|
||||
// Assign widget instance to sidebar.
|
||||
// Which sidebars have which widgets, get fresh every time.
|
||||
$sidebars_widgets = get_option( 'sidebars_widgets' );
|
||||
|
||||
// Avoid rarely fatal error when the option is an empty string
|
||||
// https://github.com/stevengliebe/widget-importer-exporter/pull/11.
|
||||
if ( ! $sidebars_widgets ) {
|
||||
$sidebars_widgets = array();
|
||||
}
|
||||
|
||||
// Use ID number from new widget instance.
|
||||
$new_instance_id = $id_base . '-' . $new_instance_id_number;
|
||||
|
||||
// Add new instance to sidebar.
|
||||
$sidebars_widgets[ $use_sidebar_id ][] = $new_instance_id;
|
||||
|
||||
// Save the amended data.
|
||||
update_option( 'sidebars_widgets', $sidebars_widgets );
|
||||
|
||||
// After widget import action.
|
||||
$after_widget_import = array(
|
||||
'sidebar' => $use_sidebar_id,
|
||||
'sidebar_old' => $sidebar_id,
|
||||
'widget' => $widget,
|
||||
'widget_type' => $id_base,
|
||||
'widget_id' => $new_instance_id,
|
||||
'widget_id_old' => $widget_instance_id,
|
||||
'widget_id_num' => $new_instance_id_number,
|
||||
'widget_id_num_old' => $instance_id_number,
|
||||
);
|
||||
|
||||
do_action( 'wie_after_widget_import', $after_widget_import ); // phpcs:ignore -- Keep the plugin prefix.
|
||||
|
||||
// Success message.
|
||||
if ( $sidebar_available ) {
|
||||
$widget_message_type = 'success';
|
||||
$widget_message = esc_html__( 'Imported', 'widget-importer-exporter' );
|
||||
} else {
|
||||
$widget_message_type = 'warning';
|
||||
$widget_message = esc_html__( 'Imported to Inactive', 'widget-importer-exporter' );
|
||||
}
|
||||
}
|
||||
|
||||
// Result for widget instance.
|
||||
$results[ $sidebar_id ]['widgets'][ $widget_instance_id ]['name'] = isset( $available_widgets[ $id_base ]['name'] ) ? $available_widgets[ $id_base ]['name'] : $id_base; // Widget name or ID if name not available (not supported by site).
|
||||
$results[ $sidebar_id ]['widgets'][ $widget_instance_id ]['title'] = ! empty( $widget['title'] ) ? $widget['title'] : esc_html__( 'No Title', 'widget-importer-exporter' ); // Show "No Title" if widget instance is untitled.
|
||||
$results[ $sidebar_id ]['widgets'][ $widget_instance_id ]['message_type'] = $widget_message_type;
|
||||
$results[ $sidebar_id ]['widgets'][ $widget_instance_id ]['message'] = $widget_message;
|
||||
}
|
||||
}
|
||||
|
||||
// Hook after import.
|
||||
do_action( 'wie_after_import' ); // phpcs:ignore -- Keep the plugin prefix.
|
||||
|
||||
// Return results.
|
||||
return apply_filters( 'wie_import_results', $results ); // phpcs:ignore -- Keep the plugin prefix.
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user