mirror of
https://github.com/proelements/proelements.git
synced 2026-04-28 06:00:40 +00:00
v3.33.1
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
namespace ElementorPro\Core\App\Modules\ImportExport;
|
||||
|
||||
use Elementor\Core\Base\Module as BaseModule;
|
||||
use ElementorPro\Plugin;
|
||||
use ElementorPro\Modules\ThemeBuilder\Module as ThemeBuilderModule;
|
||||
use Elementor\App\Modules\ImportExport\Processes\Export;
|
||||
use Elementor\App\Modules\ImportExport\Processes\Import;
|
||||
use Elementor\App\Modules\ImportExport\Processes\Revert;
|
||||
use ElementorPro\Core\App\Modules\ImportExport\Runners\Import\Templates as ImportTemplates;
|
||||
use ElementorPro\Core\App\Modules\ImportExport\Runners\Export\Templates as ExportTemplates;
|
||||
use ElementorPro\Core\App\Modules\ImportExport\Runners\Revert\Templates as RevertTemplates;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Module extends BaseModule {
|
||||
|
||||
public function get_name() {
|
||||
return 'import-export';
|
||||
}
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
$this->add_actions();
|
||||
}
|
||||
|
||||
private function add_actions() {
|
||||
add_filter( 'elementor/import/get_default_settings_conflicts', function( array $conflicts, array $templates ) {
|
||||
return $this->apply_conditions_conflicts( $conflicts, $templates );
|
||||
}, 10, 2 );
|
||||
|
||||
add_action( 'elementor/import-export/import-kit', function( Import $import ) {
|
||||
$this->register_import_kit_runners( $import );
|
||||
} );
|
||||
|
||||
add_action( 'elementor/import-export/export-kit', function( Export $export ) {
|
||||
$this->register_export_kit_runners( $export );
|
||||
} );
|
||||
|
||||
add_action( 'elementor/import-export/revert-kit', function( Revert $revert ) {
|
||||
$this->register_revert_kit_runners( $revert );
|
||||
} );
|
||||
}
|
||||
|
||||
private function apply_conditions_conflicts( $conflicts, $templates ) {
|
||||
/** @var ThemeBuilderModule $theme_builder_module */
|
||||
$theme_builder_module = Plugin::instance()->modules_manager->get_modules( 'theme-builder' );
|
||||
|
||||
if ( ! $theme_builder_module ) {
|
||||
return $conflicts;
|
||||
}
|
||||
|
||||
return $conflicts + $theme_builder_module->get_conditions_conflicts( $templates );
|
||||
}
|
||||
|
||||
private function register_import_kit_runners( Import $import ) {
|
||||
$import->register( new ImportTemplates() );
|
||||
}
|
||||
|
||||
private function register_export_kit_runners( Export $export ) {
|
||||
$export->register( new ExportTemplates() );
|
||||
}
|
||||
|
||||
private function register_revert_kit_runners( Revert $revert ) {
|
||||
$revert->register( new RevertTemplates() );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace ElementorPro\Core\App\Modules\ImportExport\Runners\Export;
|
||||
|
||||
use Elementor\App\Modules\ImportExport\Runners\Export\Export_Runner_Base;
|
||||
use Elementor\Core\Base\Document;
|
||||
use Elementor\Plugin;
|
||||
use Elementor\TemplateLibrary\Source_Local;
|
||||
|
||||
class Templates extends Export_Runner_Base {
|
||||
|
||||
public static function get_name() : string {
|
||||
return 'templates';
|
||||
}
|
||||
|
||||
public function should_export( array $data ) {
|
||||
return (
|
||||
isset( $data['include'] ) &&
|
||||
in_array( 'templates', $data['include'], true )
|
||||
);
|
||||
}
|
||||
|
||||
public function export( array $data ) {
|
||||
$template_types = array_values( Source_Local::get_template_types() );
|
||||
|
||||
$query_args = [
|
||||
'post_type' => Source_Local::CPT,
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => -1,
|
||||
'meta_query' => [
|
||||
[
|
||||
'key' => Document::TYPE_META_KEY,
|
||||
'value' => $template_types,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$templates_query = new \WP_Query( $query_args );
|
||||
|
||||
$templates_manifest_data = [];
|
||||
$files = [];
|
||||
|
||||
foreach ( $templates_query->posts as $template_post ) {
|
||||
$template_id = $template_post->ID;
|
||||
|
||||
$template_document = Plugin::$instance->documents->get( $template_id );
|
||||
|
||||
$templates_manifest_data[ $template_id ] = $template_document->get_export_summary();
|
||||
|
||||
$files[] = [
|
||||
'path' => 'templates/' . $template_id,
|
||||
'data' => $template_document->get_export_data(),
|
||||
];
|
||||
}
|
||||
|
||||
$manifest_data['templates'] = $templates_manifest_data;
|
||||
|
||||
return [
|
||||
'files' => $files,
|
||||
'manifest' => [
|
||||
$manifest_data,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace ElementorPro\Core\App\Modules\ImportExport\Runners\Import;
|
||||
|
||||
use Elementor\App\Modules\ImportExport\Runners\Import\Import_Runner_Base;
|
||||
use Elementor\App\Modules\ImportExport\Utils as ImportExportUtils;
|
||||
use Elementor\Core\Base\Document;
|
||||
use Elementor\Plugin;
|
||||
use ElementorPro\Modules\ThemeBuilder\Classes\Conditions_Manager;
|
||||
use ElementorPro\Modules\ThemeBuilder\Module as ThemeBuilderModule;
|
||||
use ElementorPro\Plugin as ProPlugin;
|
||||
use Elementor\TemplateLibrary\Source_Local;
|
||||
|
||||
class Templates extends Import_Runner_Base {
|
||||
|
||||
private $import_session_id;
|
||||
|
||||
private $templates_conditions = [];
|
||||
|
||||
public static function get_name() : string {
|
||||
return 'templates';
|
||||
}
|
||||
|
||||
public function should_import( array $data ) {
|
||||
return (
|
||||
isset( $data['include'] ) &&
|
||||
in_array( 'templates', $data['include'], true ) &&
|
||||
! empty( $data['extracted_directory_path'] ) &&
|
||||
! empty( $data['manifest']['templates'] )
|
||||
);
|
||||
}
|
||||
|
||||
public function import( array $data, array $imported_data ) {
|
||||
$this->import_session_id = $data['session_id'];
|
||||
|
||||
$path = $data['extracted_directory_path'] . 'templates/';
|
||||
$templates = $data['manifest']['templates'];
|
||||
|
||||
$result['templates'] = [
|
||||
'succeed' => [],
|
||||
'failed' => [],
|
||||
];
|
||||
|
||||
foreach ( $templates as $id => $template_settings ) {
|
||||
try {
|
||||
$template_data = ImportExportUtils::read_json_file( $path . $id );
|
||||
$import = $this->import_template( $id, $template_settings, $template_data );
|
||||
|
||||
$result['templates']['succeed'][ $id ] = $import;
|
||||
} catch ( \Exception $error ) {
|
||||
$result['templates']['failed'][ $id ] = $error->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function import_template( $id, array $template_settings, array $template_data ) {
|
||||
$doc_type = $template_settings['doc_type'];
|
||||
|
||||
$new_document = Plugin::$instance->documents->create(
|
||||
$doc_type,
|
||||
[
|
||||
'post_title' => $template_settings['title'],
|
||||
'post_type' => Source_Local::CPT,
|
||||
'post_status' => 'publish',
|
||||
]
|
||||
);
|
||||
|
||||
if ( is_wp_error( $new_document ) ) {
|
||||
throw new \Exception( $new_document->get_error_message() );
|
||||
}
|
||||
|
||||
$template_data['import_settings'] = $template_settings;
|
||||
$template_data['id'] = $id;
|
||||
|
||||
$this->set_templates_conditions( $template_data );
|
||||
|
||||
$new_attachment_callback = function( $attachment_id ) {
|
||||
$this->set_session_post_meta( $attachment_id, $this->import_session_id );
|
||||
};
|
||||
|
||||
add_filter( 'elementor/template_library/import_images/new_attachment', $new_attachment_callback );
|
||||
|
||||
$new_document->import( $template_data );
|
||||
|
||||
remove_filter( 'elementor/template_library/import_images/new_attachment', $new_attachment_callback );
|
||||
|
||||
$document_id = $new_document->get_main_id();
|
||||
|
||||
$this->set_session_post_meta( $document_id, $this->import_session_id );
|
||||
|
||||
return $document_id;
|
||||
}
|
||||
|
||||
public function get_import_session_metadata() : array {
|
||||
return [
|
||||
'template_conditions' => $this->templates_conditions,
|
||||
];
|
||||
}
|
||||
|
||||
private function set_templates_conditions( $template_data ) {
|
||||
$conditions = $template_data['import_settings']['conditions'] ?? [];
|
||||
|
||||
if ( empty( $conditions ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$condition = $conditions[0];
|
||||
|
||||
$condition = rtrim( implode( '/', $condition ), '/' );
|
||||
|
||||
/** @var ThemeBuilderModule $theme_builder_module */
|
||||
$theme_builder_module = ProPlugin::instance()->modules_manager->get_modules( 'theme-builder' );
|
||||
$conditions_manager = $theme_builder_module->get_conditions_manager();
|
||||
|
||||
$conflicts = $conditions_manager->get_conditions_conflicts_by_location(
|
||||
$condition,
|
||||
$template_data['import_settings']['location']
|
||||
);
|
||||
|
||||
foreach ( $conflicts as $template ) {
|
||||
$template_document = Plugin::$instance->documents->get( $template['template_id'] );
|
||||
|
||||
$template_conditions = $theme_builder_module->get_conditions_manager()->get_document_conditions( $template_document );
|
||||
|
||||
$this->templates_conditions[ $template['template_id'] ] = $template_conditions;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace ElementorPro\Core\App\Modules\ImportExport\Runners\Revert;
|
||||
|
||||
use Elementor\App\Modules\ImportExport\Runners\Revert\Revert_Runner_Base;
|
||||
use Elementor\Core\Base\Document;
|
||||
use Elementor\Plugin;
|
||||
use ElementorPro\Modules\ThemeBuilder\Module as ThemeBuilderModule;
|
||||
use ElementorPro\Plugin as ProPlugin;
|
||||
use Elementor\TemplateLibrary\Source_Local;
|
||||
|
||||
class Templates extends Revert_Runner_Base {
|
||||
|
||||
public static function get_name() : string {
|
||||
return 'templates';
|
||||
}
|
||||
|
||||
public function should_revert( array $data ) : bool {
|
||||
return (
|
||||
isset( $data['runners'] ) &&
|
||||
array_key_exists( static::get_name(), $data['runners'] )
|
||||
);
|
||||
}
|
||||
|
||||
public function revert( array $data ) {
|
||||
$template_types = array_values( Source_Local::get_template_types() );
|
||||
|
||||
$query_args = [
|
||||
'post_type' => Source_Local::CPT,
|
||||
'post_status' => 'any',
|
||||
'posts_per_page' => -1,
|
||||
'meta_query' => [
|
||||
[
|
||||
'key' => Document::TYPE_META_KEY,
|
||||
'value' => $template_types,
|
||||
],
|
||||
[
|
||||
'key' => static::META_KEY_ELEMENTOR_IMPORT_SESSION_ID,
|
||||
'value' => $data['session_id'],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$templates_query = new \WP_Query( $query_args );
|
||||
|
||||
foreach ( $templates_query->posts as $template_post ) {
|
||||
$template_document = Plugin::$instance->documents->get( $template_post->ID );
|
||||
$template_document->delete();
|
||||
}
|
||||
|
||||
/** @var ThemeBuilderModule $theme_builder_module */
|
||||
$theme_builder_module = ProPlugin::instance()->modules_manager->get_modules( 'theme-builder' );
|
||||
|
||||
$theme_builder_module->get_conditions_manager()->clear_cache();
|
||||
|
||||
$old_conditions = $data['runners']['templates']['template_conditions'] ?? [];
|
||||
|
||||
foreach ( $old_conditions as $template_id => $conditions ) {
|
||||
$theme_builder_module->get_conditions_manager()->save_conditions( $template_id, $conditions );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user