mirror of
https://github.com/proelements/proelements.git
synced 2026-04-06 04:13:48 +00:00
v3.33.1
This commit is contained in:
78
modules/custom-code/import-export/export-runner.php
Normal file
78
modules/custom-code/import-export/export-runner.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace ElementorPro\Modules\CustomCode\ImportExport;
|
||||
|
||||
use Elementor\App\Modules\ImportExport\Runners\Export\Export_Runner_Base;
|
||||
use ElementorPro\Modules\CustomCode\Module as Custom_Code_Module;
|
||||
use ElementorPro\Modules\CustomCode\Custom_Code_Metabox;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Export_Runner extends Export_Runner_Base {
|
||||
public static function get_name(): string {
|
||||
return 'custom-code';
|
||||
}
|
||||
|
||||
public function should_export( array $data ) {
|
||||
return (
|
||||
isset( $data['include'] ) &&
|
||||
in_array( 'settings', $data['include'], true )
|
||||
);
|
||||
}
|
||||
|
||||
public function export( array $data ) {
|
||||
$code_snippets = $this->get_custom_code_snippets();
|
||||
|
||||
if ( empty( $code_snippets ) ) {
|
||||
return [
|
||||
'manifest' => [],
|
||||
'files' => [],
|
||||
];
|
||||
}
|
||||
|
||||
$snippets_data = [];
|
||||
$manifest = [];
|
||||
|
||||
foreach ( $code_snippets as $snippet ) {
|
||||
$data = $this->prepare_snippet_data( $snippet );
|
||||
$snippets_data[] = $data;
|
||||
$manifest['custom-code'][ $snippet->ID ] = $data;
|
||||
}
|
||||
|
||||
return [
|
||||
'files' => [
|
||||
'path' => Import_Export::FILE_NAME,
|
||||
'data' => $snippets_data,
|
||||
],
|
||||
'manifest' => [
|
||||
$manifest,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function get_custom_code_snippets() {
|
||||
return get_posts( [
|
||||
'post_type' => Custom_Code_Module::CPT,
|
||||
'posts_per_page' => -1,
|
||||
'post_status' => 'publish',
|
||||
] );
|
||||
}
|
||||
|
||||
private function prepare_snippet_data( $snippet ) {
|
||||
$location = get_post_meta( $snippet->ID, '_elementor_' . Custom_Code_Metabox::FIELD_LOCATION, true );
|
||||
$priority = get_post_meta( $snippet->ID, '_elementor_' . Custom_Code_Metabox::FIELD_PRIORITY, true );
|
||||
$conditions = get_post_meta( $snippet->ID, '_elementor_conditions', true );
|
||||
|
||||
return [
|
||||
'ID' => $snippet->ID,
|
||||
'post_title' => $snippet->post_title,
|
||||
'post_content' => $snippet->post_content,
|
||||
'post_status' => $snippet->post_status,
|
||||
'location' => $location,
|
||||
'priority' => $priority,
|
||||
'conditions' => $conditions,
|
||||
];
|
||||
}
|
||||
}
|
||||
25
modules/custom-code/import-export/import-export.php
Normal file
25
modules/custom-code/import-export/import-export.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace ElementorPro\Modules\CustomCode\ImportExport;
|
||||
|
||||
use Elementor\App\Modules\ImportExport\Processes\Export;
|
||||
use Elementor\App\Modules\ImportExport\Processes\Import;
|
||||
use Elementor\App\Modules\ImportExport\Processes\Revert;
|
||||
|
||||
class Import_Export {
|
||||
const FILE_NAME = 'custom-code';
|
||||
|
||||
public function register_hooks() {
|
||||
add_action( 'elementor/import-export/export-kit', function ( Export $export ) {
|
||||
$export->register( new Export_Runner() );
|
||||
} );
|
||||
|
||||
add_action( 'elementor/import-export/import-kit', function ( Import $import ) {
|
||||
$import->register( new Import_Runner() );
|
||||
} );
|
||||
|
||||
add_action( 'elementor/import-export/revert-kit', function ( Revert $revert ) {
|
||||
$revert->register( new Revert_Runner() );
|
||||
} );
|
||||
}
|
||||
}
|
||||
139
modules/custom-code/import-export/import-runner.php
Normal file
139
modules/custom-code/import-export/import-runner.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace ElementorPro\Modules\CustomCode\ImportExport;
|
||||
|
||||
use Elementor\App\Modules\ImportExport\Runners\Import\Import_Runner_Base;
|
||||
use Elementor\App\Modules\ImportExport\Utils as ImportExportUtils;
|
||||
use ElementorPro\Modules\CustomCode\Module as Custom_Code_Module;
|
||||
use ElementorPro\Modules\CustomCode\Custom_Code_Metabox;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Import_Runner extends Import_Runner_Base {
|
||||
private $session_id;
|
||||
private $imported_snippets = [];
|
||||
|
||||
public static function get_name(): string {
|
||||
return 'custom-code';
|
||||
}
|
||||
|
||||
public function should_import( array $data ) {
|
||||
return (
|
||||
isset( $data['include'] ) &&
|
||||
in_array( 'settings', $data['include'], true )
|
||||
);
|
||||
}
|
||||
|
||||
public function import( array $data, array $imported_data ) {
|
||||
$this->session_id = $data['session_id'];
|
||||
|
||||
$custom_code_file_path = $data['extracted_directory_path'] . Import_Export::FILE_NAME;
|
||||
$snippets_data = ImportExportUtils::read_json_file( $custom_code_file_path );
|
||||
|
||||
$result = [];
|
||||
if ( empty( $snippets_data ) ) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
foreach ( $snippets_data as $snippet_data ) {
|
||||
$this->import_snippet( $snippet_data );
|
||||
}
|
||||
|
||||
if ( empty( $this->imported_snippets ) ) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$result['site-settings']['custom-code'] = $this->imported_snippets;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function get_import_session_metadata(): array {
|
||||
return [
|
||||
'imported_snippets' => $this->imported_snippets,
|
||||
];
|
||||
}
|
||||
|
||||
private function import_snippet( $snippet_data ) {
|
||||
$existing_snippet = $this->get_existing_snippet( $snippet_data['post_title'] );
|
||||
|
||||
if ( $existing_snippet ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$snippet_id = $this->create_snippet( $snippet_data );
|
||||
|
||||
if ( $snippet_id ) {
|
||||
$this->imported_snippets[] = [
|
||||
'id' => $snippet_id,
|
||||
'title' => $snippet_data['post_title'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
private function get_existing_snippet( $snippet_title ) {
|
||||
$snippet_query = new \WP_Query( [
|
||||
'post_type' => Custom_Code_Module::CPT,
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => 1,
|
||||
'title' => $snippet_title,
|
||||
] );
|
||||
|
||||
if ( $snippet_query->have_posts() ) {
|
||||
$snippet_post = $snippet_query->posts[0];
|
||||
return [
|
||||
'id' => $snippet_post->ID,
|
||||
'title' => $snippet_post->post_title,
|
||||
];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function create_snippet( $snippet_data ) {
|
||||
$snippet_id = wp_insert_post( [
|
||||
'post_title' => $snippet_data['post_title'],
|
||||
'post_content' => $snippet_data['post_content'],
|
||||
'post_status' => $snippet_data['post_status'],
|
||||
'post_type' => Custom_Code_Module::CPT,
|
||||
] );
|
||||
|
||||
if ( is_wp_error( $snippet_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->set_session_post_meta( $snippet_id, $this->session_id );
|
||||
|
||||
if ( ! empty( $snippet_data['location'] ) ) {
|
||||
update_post_meta( $snippet_id, '_elementor_' . Custom_Code_Metabox::FIELD_LOCATION, $snippet_data['location'] );
|
||||
}
|
||||
|
||||
if ( ! empty( $snippet_data['device_mode'] ) ) {
|
||||
update_post_meta( $snippet_id, '_elementor_' . Custom_Code_Metabox::FIELD_DEVICE_MODE, $snippet_data['device_mode'] );
|
||||
}
|
||||
|
||||
if ( ! empty( $snippet_data['user_roles'] ) ) {
|
||||
update_post_meta( $snippet_id, '_elementor_' . Custom_Code_Metabox::FIELD_USER_ROLES, $snippet_data['user_roles'] );
|
||||
}
|
||||
|
||||
if ( ! empty( $snippet_data['site_languages'] ) ) {
|
||||
update_post_meta( $snippet_id, '_elementor_' . Custom_Code_Metabox::FIELD_SITE_LANGUAGES, $snippet_data['site_languages'] );
|
||||
}
|
||||
|
||||
if ( ! empty( $snippet_data['url_conditions'] ) ) {
|
||||
update_post_meta( $snippet_id, '_elementor_' . Custom_Code_Metabox::FIELD_URL_CONDITIONS, $snippet_data['url_conditions'] );
|
||||
}
|
||||
|
||||
if ( ! empty( $snippet_data['date_conditions'] ) ) {
|
||||
update_post_meta( $snippet_id, '_elementor_' . Custom_Code_Metabox::FIELD_DATE_CONDITIONS, $snippet_data['date_conditions'] );
|
||||
}
|
||||
|
||||
if ( ! empty( $snippet_data['time_conditions'] ) ) {
|
||||
update_post_meta( $snippet_id, '_elementor_' . Custom_Code_Metabox::FIELD_TIME_CONDITIONS, $snippet_data['time_conditions'] );
|
||||
}
|
||||
|
||||
return $snippet_id;
|
||||
}
|
||||
}
|
||||
38
modules/custom-code/import-export/revert-runner.php
Normal file
38
modules/custom-code/import-export/revert-runner.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace ElementorPro\Modules\CustomCode\ImportExport;
|
||||
|
||||
use Elementor\App\Modules\ImportExport\Runners\Revert\Revert_Runner_Base;
|
||||
use ElementorPro\Modules\CustomCode\Module as Custom_Code_Module;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Revert_Runner extends Revert_Runner_Base {
|
||||
|
||||
public static function get_name(): string {
|
||||
return 'custom-code';
|
||||
}
|
||||
|
||||
public function should_revert( array $data ): bool {
|
||||
return (
|
||||
isset( $data['runners'] ) &&
|
||||
array_key_exists( static::get_name(), $data['runners'] )
|
||||
);
|
||||
}
|
||||
|
||||
public function revert( array $data ) {
|
||||
$metadata = $data['runners'][ static::get_name() ] ?? [];
|
||||
|
||||
$this->revert_imported_snippets( $metadata );
|
||||
}
|
||||
|
||||
private function revert_imported_snippets( $metadata ) {
|
||||
$imported_snippets = $metadata['imported_snippets'] ?? [];
|
||||
|
||||
foreach ( $imported_snippets as $snippet ) {
|
||||
wp_delete_post( $snippet['id'], true );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user