Update elementor pro
This commit is contained in:
@@ -13,6 +13,8 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
|
||||
class Widget extends Library_Document {
|
||||
|
||||
const EXPORT_GROUP = 'global-widget';
|
||||
|
||||
public static function get_properties() {
|
||||
$properties = parent::get_properties();
|
||||
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
namespace ElementorPro\Modules\GlobalWidget\ImportExportCustomization;
|
||||
|
||||
use Elementor\Core\Base\Document;
|
||||
use Elementor\TemplateLibrary\Source_Local;
|
||||
use ElementorPro\Modules\GlobalWidget\Module;
|
||||
use ElementorPro\Plugin;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
class Export {
|
||||
|
||||
public function add_global_widgets_to_export( $export_data, $data, $customization ) {
|
||||
if ( ! isset( $customization['globalWidgets']['enabled'] ) || ! $customization['globalWidgets']['enabled'] ) {
|
||||
return $export_data;
|
||||
}
|
||||
|
||||
$global_widgets_data = $this->export_global_widgets();
|
||||
|
||||
if ( empty( $global_widgets_data['files'] ) ) {
|
||||
return $export_data;
|
||||
}
|
||||
|
||||
$export_data['files'] = array_merge( $export_data['files'], $global_widgets_data['files'] );
|
||||
|
||||
if ( ! empty( $global_widgets_data['manifest'] ) ) {
|
||||
$export_data['manifest'][0]['templates'] = ( $export_data['manifest'][0]['templates'] ?? [] ) + $global_widgets_data['manifest'];
|
||||
}
|
||||
|
||||
return $export_data;
|
||||
}
|
||||
|
||||
private function export_global_widgets() {
|
||||
$query_args = [
|
||||
'post_type' => Source_Local::CPT,
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => -1,
|
||||
'meta_query' => [
|
||||
[
|
||||
'key' => Document::TYPE_META_KEY,
|
||||
'value' => Module::TEMPLATE_TYPE,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$global_widgets_query = new \WP_Query( $query_args );
|
||||
|
||||
$manifest_data = [];
|
||||
$files = [];
|
||||
|
||||
foreach ( $global_widgets_query->posts as $widget_post ) {
|
||||
$widget_id = $widget_post->ID;
|
||||
|
||||
$widget_document = Plugin::elementor()->documents->get( $widget_id );
|
||||
|
||||
if ( ! $widget_document ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$manifest_data[ $widget_id ] = $widget_document->get_export_summary();
|
||||
|
||||
$files[] = [
|
||||
'path' => 'templates/' . $widget_id,
|
||||
'data' => $widget_document->get_export_data(),
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'files' => $files,
|
||||
'manifest' => $manifest_data,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
namespace ElementorPro\Modules\GlobalWidget\ImportExportCustomization;
|
||||
|
||||
use Elementor\Core\Base\Document;
|
||||
use Elementor\TemplateLibrary\Source_Local;
|
||||
use Elementor\App\Modules\ImportExportCustomization\Utils as ImportExportCustomizationUtils;
|
||||
use ElementorPro\Modules\GlobalWidget\Module;
|
||||
use ElementorPro\Plugin;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
class Import {
|
||||
|
||||
public function add_global_widgets_to_import( $result, $data, $customization, $runner ) {
|
||||
if ( $customization && ( ! isset( $customization['globalWidgets']['enabled'] ) || ! $customization['globalWidgets']['enabled'] ) ) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$global_widgets_result = $this->import_global_widgets( $data );
|
||||
|
||||
if ( ! empty( $global_widgets_result ) ) {
|
||||
$result['templates']['succeed'] = array_merge( $result['templates']['succeed'], $global_widgets_result['succeed'] );
|
||||
$result['templates']['failed'] = array_merge( $result['templates']['failed'], $global_widgets_result['failed'] );
|
||||
|
||||
foreach ( $global_widgets_result['succeed_summary'] as $doc_type => $count ) {
|
||||
$result['templates']['succeed_summary'][ $doc_type ] = ( $result['templates']['succeed_summary'][ $doc_type ] ?? 0 ) + $count;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function import_global_widgets( $data ) {
|
||||
$path = $data['extracted_directory_path'] . 'templates/';
|
||||
$templates = $data['manifest']['templates'];
|
||||
|
||||
$result = [
|
||||
'succeed' => [],
|
||||
'failed' => [],
|
||||
'succeed_summary' => [],
|
||||
];
|
||||
|
||||
foreach ( $templates as $id => $template_settings ) {
|
||||
if ( Module::TEMPLATE_TYPE !== $template_settings['doc_type'] ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$template_data = ImportExportCustomizationUtils::read_json_file( $path . $id );
|
||||
$imported_id = $this->import_global_widget( $id, $template_settings, $template_data, $data );
|
||||
|
||||
$result['succeed'][ $id ] = $imported_id;
|
||||
$result['succeed_summary'][ $template_settings['doc_type'] ] = ( $result['succeed_summary'][ $template_settings['doc_type'] ] ?? 0 ) + 1;
|
||||
} catch ( \Exception $error ) {
|
||||
$result['failed'][ $id ] = $error->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function import_global_widget( $id, array $template_settings, array $template_data, array $import_data ) {
|
||||
$new_document = Plugin::elementor()->documents->create(
|
||||
Module::TEMPLATE_TYPE,
|
||||
[
|
||||
'post_title' => $template_settings['title'],
|
||||
'post_type' => Source_Local::CPT,
|
||||
'post_status' => 'publish',
|
||||
]
|
||||
);
|
||||
|
||||
if ( is_wp_error( $new_document ) ) {
|
||||
throw new \Exception( esc_html( $new_document->get_error_message() ) );
|
||||
}
|
||||
|
||||
$template_data['import_settings'] = $template_settings;
|
||||
$template_data['id'] = $id;
|
||||
|
||||
if ( isset( $import_data['session_id'] ) ) {
|
||||
$new_attachment_callback = function( $attachment_id ) use ( $import_data ) {
|
||||
$this->set_session_post_meta( $attachment_id, $import_data['session_id'] );
|
||||
};
|
||||
|
||||
add_filter( 'elementor/template_library/import_images/new_attachment', $new_attachment_callback );
|
||||
}
|
||||
|
||||
$new_document->import( $template_data );
|
||||
|
||||
if ( isset( $new_attachment_callback ) ) {
|
||||
remove_filter( 'elementor/template_library/import_images/new_attachment', $new_attachment_callback );
|
||||
}
|
||||
|
||||
$document_id = $new_document->get_main_id();
|
||||
|
||||
if ( isset( $import_data['session_id'] ) ) {
|
||||
$this->set_session_post_meta( $document_id, $import_data['session_id'] );
|
||||
}
|
||||
|
||||
return $document_id;
|
||||
}
|
||||
|
||||
private function set_session_post_meta( $post_id, $session_id ) {
|
||||
update_post_meta( $post_id, '_elementor_import_session_id', $session_id );
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,14 @@ use Elementor\Core\Documents_Manager;
|
||||
use Elementor\Core\Utils\Promotions\Filtered_Promotions_Manager;
|
||||
use Elementor\Element_Base;
|
||||
use Elementor\TemplateLibrary\Source_Local;
|
||||
use Elementor\App\Modules\ImportExportCustomization\Utils as ImportExportCustomizationUtils;
|
||||
use ElementorPro\Base\Module_Base;
|
||||
use ElementorPro\Modules\GlobalWidget\Documents\Widget;
|
||||
use ElementorPro\Modules\GlobalWidget\ImportExportCustomization;
|
||||
use ElementorPro\Plugin;
|
||||
use ElementorPro\License\API;
|
||||
use ElementorPro\Modules\Tiers\Module as Tiers;
|
||||
use Elementor\Core\Base\Document;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
@@ -32,6 +35,9 @@ class Module extends Module_Base {
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
$this->add_component( 'import_export_import', new ImportExportCustomization\Import() );
|
||||
$this->add_component( 'import_export_export', new ImportExportCustomization\Export() );
|
||||
|
||||
$this->add_hooks();
|
||||
|
||||
Plugin::elementor()->data_manager->register_controller_instance( new Data\Controller() );
|
||||
@@ -250,6 +256,7 @@ class Module extends Module_Base {
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
private function add_hooks() {
|
||||
add_action( 'elementor/documents/register', [ $this, 'register_documents' ] );
|
||||
add_action( 'elementor/template-library/after_save_template', [ $this, 'set_template_widget_type_meta' ], 10, 2 );
|
||||
@@ -266,5 +273,7 @@ class Module extends Module_Base {
|
||||
add_filter( 'elementor/document/save/data', function ( $data, $document ) {
|
||||
return $this->get_document_data( $data, $document );
|
||||
}, 10, 2 );
|
||||
add_filter( 'elementor/import-export-customization/export/templates_data', [ $this->get_component( 'import_export_export' ), 'add_global_widgets_to_export' ], 10, 3 );
|
||||
add_filter( 'elementor/import-export-customization/import/templates_result', [ $this->get_component( 'import_export_import' ), 'add_global_widgets_to_import' ], 10, 4 );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user