first commit

This commit is contained in:
2024-07-15 11:28:08 +02:00
commit f52d538ea5
21891 changed files with 6161164 additions and 0 deletions

View File

@@ -0,0 +1,216 @@
<?php
/**
* Importer extensions
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( ! class_exists( 'Jet_Data_Importer_Extensions' ) ) {
/**
* Define Jet_Data_Importer_Extensions class
*/
class Jet_Data_Importer_Extensions {
/**
* A reference to an instance of this class.
*
* @since 1.0.0
* @var object
*/
private static $instance = null;
/**
* Constructor for the class
*/
public function __construct() {
// Prevent from errors triggering while MotoPress Booking posts importing (loving it)
add_filter( 'jet-data-importer/import/skip-post', array( $this, 'prevent_import_errors' ), 10, 2 );
// Clear fonts cache after import
add_action( 'jet-data-importer/import/finish', array( $this, 'clear_fonts_cache' ) );
// Allow switch Monstroid2 skins
add_action( 'jet-data-importer/import/before-skip-redirect', array( $this, 'switch_skin_on_skip' ) );
add_action( 'jet-data-importer/import/before-options-processing', array( $this, 'set_container_width' ) );
add_action( 'jet-data-importer/import/after-options-processing', array( $this, 'set_required_options' ) );
add_action( 'jet-data-importer/import/after-import-tables', array( $this, 'clear_woo_transients' ) );
}
/**
* Delete WooCommerce-related transients after new tables are imported
*
* @return void
*/
public function clear_woo_transients() {
delete_transient( 'wc_attribute_taxonomies' );
}
/**
* Preset elemntor container width if it was not passed in XML
*/
public function set_container_width( $data ) {
if ( ! isset( $data['elementor_container_width'] ) ) {
update_option( 'elementor_container_width', 1200 );
}
}
/**
* Set required Kava Extra and Jet Elements options
*/
public function set_required_options() {
if ( class_exists( 'Kava_Extra' ) ) {
$options = get_option( 'kava-extra-settings' );
if ( ! $options ) {
update_option( 'kava-extra-settings', array(
'nucleo-mini-package' => 'true',
) );
}
unset( $options );
}
if ( class_exists( 'Jet_Elements' ) ) {
$options = get_option( 'jet-elements-settings' );
if ( empty( $options ) ) {
$options = array();
}
if ( empty( $options['api_key'] ) ) {
$options['api_key'] = 'AIzaSyDlhgz2x94h0UZb7kZXOBjwAtszoCRtDLM';
}
update_option( 'jet-elements-settings', $options );
}
}
/**
* Switch Monstroid2 skin on skip demo content import.
*
* @return null
*/
public function switch_skin_on_skip() {
if ( ! isset( $_GET['file'] ) ) {
return;
}
preg_match( '/demo-content\/(.*?)\//', base64_decode( $_GET['file'] ), $matches );
if ( empty( $matches ) || ! isset( $matches[1] ) ) {
return;
}
$skin = esc_attr( $matches[1] );
$map = array(
'default' => 'default',
'skin-1' => 'skin1',
'skin-2' => 'skin2',
'skin-3' => 'skin8',
'skin-4' => 'skin3',
'skin-5' => 'skin4',
'skin-6' => 'skin9',
'skin-7' => 'skin5',
'skin-8' => 'skin7',
'skin-9' => 'skin6',
);
$mapped_skin = isset( $map[ $skin ] ) ? $map[ $skin ] : false;
if ( ! $mapped_skin ) {
return;
}
$skin_file = get_stylesheet_directory() . '/tm-style-switcher-pressets/' . $mapped_skin . '.json';
if ( ! file_exists( $skin_file ) ) {
return;
}
ob_start();
include $skin_file;
$skin_data = ob_get_clean();
$skin_data = json_decode( $skin_data, true );
if ( empty( $skin_data ) || ! isset( $skin_data['mods'] ) ) {
return;
}
foreach ( $skin_data['mods'] as $mod => $value ) {
set_theme_mod( $mod, $value );
}
}
/**
* Ckear Google fonts cache.
*
* @return void
*/
public function clear_fonts_cache() {
delete_transient( 'cherry_google_fonts_url' );
delete_transient( 'cx_google_fonts_url_kava' );
}
/**
* Prevent PHP errors on import.
*
* @param bool $skip Default skip value.
* @param array $data Plugin data.
* @return bool
*/
public function prevent_import_errors( $skip, $data ) {
if ( isset( $data['post_type'] ) && 'mphb_booking' === $data['post_type'] ) {
return true;
}
return $skip;
}
/**
* Returns the instance.
*
* @since 1.0.0
* @return object
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
}
}
/**
* Returns instance of Jet_Data_Importer_Extensions
*
* @return object
*/
function jdi_extensions() {
return Jet_Data_Importer_Extensions::get_instance();
}
jdi_extensions();

View File

@@ -0,0 +1,956 @@
<?php
/**
* Importer interface
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( ! class_exists( 'Jet_Data_Importer_Interface' ) ) {
/**
* Define Jet_Data_Importer_Interface class
*/
class Jet_Data_Importer_Interface {
/**
* A reference to an instance of this class.
*
* @since 1.0.0
* @var object
*/
private static $instance = null;
/**
* Variable for settings array.
*
* @var array
*/
public $settings = array();
/**
* Importer instance
*
* @var object
*/
private $importer = null;
/**
* Path to import file
*
* @var string|bool
*/
private $import_file = null;
/**
* Returns XML-files count
*
* @var int
*/
private $xml_count = null;
/**
* Importer slug
*
* @var string
*/
public $slug = 'import';
/**
* Data storage.
*
* @var array
*/
public $data = array();
/**
* Constructor for the class
*/
function __construct() {
add_action( 'admin_menu', array( $this, 'menu_page' ) );
add_action( 'wp_ajax_jet-data-import-chunk', array( $this, 'import_chunk' ) );
add_action( 'wp_ajax_jet-data-thumbnails', array( $this, 'regenerate_chunk' ) );
add_action( 'wp_ajax_jet-data-import-get-file-path', array( $this, 'get_file_path' ) );
add_action( 'wp_ajax_jet-data-import-remove-content', array( $this, 'remove_content' ) );
add_action( 'cherry-data-importer/before-messages', array( $this, 'check_server_params' ) );
add_action( 'admin_footer', array( $this, 'advanced_popup' ) );
add_action( 'init', array( $this, 'maybe_skip_installation' ), 20 );
}
/**
* Maybe skip demo content installation
*
* @return bool|void
*/
public function maybe_skip_installation() {
if ( ! isset( $_GET['page'] ) || jdi()->slug !== $_GET['page'] ) {
return false;
}
if ( ! isset( $_GET['step'] ) || '2' !== $_GET['step'] ) {
return false;
}
if ( isset( $_GET['type'] ) && 'skip' === $_GET['type'] ) {
require_once jdi()->path( 'includes/import/class-jet-data-importer-extensions.php' );
/**
* Hook before redirect on demo content installation skip
*/
do_action( 'jet-data-importer/import/before-skip-redirect' );
wp_redirect( jdi()->page_url( array( 'step' => 4 ) ) );
die();
}
return false;
}
/**
* Check user password before content replacing.
*
* @return void
*/
public function remove_content() {
$this->validate_request();
if ( empty( $_REQUEST['password'] ) ) {
jdi_cache()->write_cache();
wp_send_json_error( array(
'message' => esc_html__( 'Password is empty', 'jet-data-importer' ),
) );
}
$password = esc_attr( $_REQUEST['password'] );
$user_id = get_current_user_id();
$data = get_userdata( $user_id );
if ( wp_check_password( $password, $data->user_pass, $user_id ) ) {
jdi_tools()->clear_content();
jdi_cache()->write_cache();
wp_send_json_success( array(
'message' => esc_html__( 'Content successfully removed', 'jet-data-importer' ),
'slider' => jdi_slider()->render( false ),
) );
} else {
jdi_cache()->write_cache();
wp_send_json_error( array(
'message' => esc_html__( 'Entered password is invalid', 'jet-data-importer' ),
) );
}
}
/**
* PopUp installation
*/
public function advanced_popup() {
if ( ! $this->is_advanced_import() ) {
return;
}
if ( ! isset( $_GET['tab'] ) || 'import' !== $_GET['tab'] ) {
return;
}
jdi()->get_template( 'advanced-popup.php' );
}
/**
* Show content install type after plugins installation finished by Wizard.
*
* @return void
*/
public function wizard_popup() {
if ( ! isset( $_GET['step'] ) || 2 !== intval( $_GET['step'] ) ) {
return;
}
jdi()->get_template( 'advanced-popup.php' );
}
/**
* Check server params and show warning message if some of them don't meet requirements
*
* @return void
*/
public function check_server_params() {
$messages = '';
$format = esc_html__( '%1$s: %2$s required, yours - %3$s', 'jet-data-importer' );
foreach ( jdi_tools()->server_params() as $param => $data ) {
$val = ini_get( $param );
$val = ini_get( $param );
$current_value = wp_convert_hr_to_bytes( $val );
$recommended_value = wp_convert_hr_to_bytes( $data['value'] . $data['units'] );
if ( $current_value < $recommended_value ) {
$current = sprintf(
$format,
$param,
'<strong>' . $data['value'] . $data['units'] . '</strong>',
'<strong>' . (int) $val . $data['units'] . '</strong>'
);
$messages .= '<div>' . $current . '</div>';
}
}
if ( empty( $messages ) ) {
return;
}
$heading = '<div class="cdi-server-messages__heading">' . esc_html__( 'Some parameters from your server don\'t meet the requirements:', 'jet-data-importer' ) . '</div>';
echo '<div class="cdi-server-messages">' . $heading . $messages . '</div>';
}
/**
* Returns current chunk size
*
* @return void
*/
public function chunk_size() {
$size = jdi()->get_setting( array( 'import', 'chunk_size' ) );
$size = intval( $size );
if ( ! $size ) {
return jdi()->chunk_size;
} else {
return $size;
}
}
/**
* Init importer
*
* @return void
*/
public function menu_page() {
jdi()->register_tab(
array(
'id' => $this->slug,
'name' => esc_html__( 'Import', 'jet-data-importer' ),
'cb' => array( $this, 'dispatch' ),
)
);
}
/**
* Run Jet importer
*
* @return void
*/
public function dispatch() {
$step = ! empty( $_GET['step'] ) ? intval( $_GET['step'] ) : 1;
ob_start();
jdi_tools()->get_page_title( '<h2 class="page-title">', '</h2>', true );
wp_enqueue_script( 'jet-data-import' );
switch ( $step ) {
case 2:
$this->import_step();
break;
case 3:
$this->regenerate_thumbnails();
break;
case 4:
$this->import_after();
break;
default:
$this->import_before();
break;
}
return ob_get_clean();
}
/**
* First import step
*
* @return void
*/
private function import_before() {
jdi()->get_template( 'import-before.php' );
}
/**
* Last import step
*
* @return void
*/
private function import_after() {
jdi()->get_template( 'import-after.php' );
}
/**
* Show main content import step
*
* @return void
*/
private function import_step() {
if ( empty( $_GET['file'] ) || 'null' === $_GET['file'] ) {
wp_redirect( add_query_arg(
array(
'page' => jdi()->slug,
'tab' => $this->slug,
'step' => 1,
),
esc_url( admin_url( 'admin.php' ) )
) );
die();
}
$importer = $this->get_importer();
if ( ! $importer ) {
_e( 'Error! Import file not found. Please contact our support team.', 'jet-data-importer' );
return;
}
$importer->prepare_import();
$count = jdi_cache()->get( 'total_count' );
$chunks_count = ceil( intval( $count ) / $this->chunk_size() );
// Adds final step with ID and URL remapping. Sometimes it's expensice step separate it
$chunks_count++;
jdi_cache()->update( 'chunks_count', $chunks_count );
jdi()->get_template( 'import.php' );
jdi_cache()->write_cache();
}
/**
* Process regenerate thumbnails step.
*
* @return void
*/
public function regenerate_thumbnails() {
$count = wp_count_attachments();
$count = (array) $count;
$step = jdi()->get_setting( array( 'import', 'regenerate_chunk_size' ) );
$total = 0;
foreach ( $count as $mime => $num ) {
if ( false === strpos( $mime, 'image' ) ) {
continue;
}
$total = $total + (int) $num;
}
wp_localize_script( 'jet-data-import', 'JetRegenerateData', array(
'totalImg' => $total,
'totalSteps' => ceil( $total / $step ),
'step' => $step,
) );
jdi()->get_template( 'regenerate.php' );
}
/**
* Returns true if regenerate thumbnails step is required, false - if not.
*
* @return boolean
*/
private function is_regenerate_required() {
$count = wp_count_attachments();
$count = (array) $count;
if ( empty( $count ) ) {
return false;
}
$total = 0;
if ( ! empty( $count['image/jpeg'] ) ) {
$total += absint( $count['image/jpeg'] );
}
if ( ! empty( $count['image/png'] ) ) {
$total += absint( $count['image/png'] );
}
if ( 0 === $total ) {
return false;
}
return true;
}
/**
* Validate import-related ajax request.
*
* @return void
*/
private function validate_request() {
if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'jet-data-import' ) ) {
jdi_cache()->write_cache();
wp_send_json_error( array(
'message' => esc_html__( 'You don\'t have permissions to do this', 'jet-data-importer' ),
) );
}
if ( ! current_user_can( 'import' ) ) {
jdi_cache()->write_cache();
wp_send_json_error( array(
'message' => esc_html__( 'You don\'t have permissions to do this', 'jet-data-importer' ),
) );
}
}
/**
* Process single regenerate chunk
*
* @return void
*/
public function regenerate_chunk() {
$this->validate_request();
$required = array(
'offset',
'step',
'total',
);
foreach ( $required as $field ) {
if ( ! isset( $_REQUEST[ $field ] ) ) {
jdi_cache()->write_cache();
wp_send_json_error( array(
'message' => sprintf(
esc_html__( '%s is missing in request', 'jet-data-importer' ), $field
),
) );
}
}
$offset = (int) $_REQUEST['offset'];
$step = (int) $_REQUEST['step'];
$total = (int) $_REQUEST['total'];
$is_last = ( $total * $step <= $offset + $step ) ? true : false;
$attachments = get_posts( array(
'post_type' => 'attachment',
'numberposts' => $step,
'offset' => $offset,
) );
if ( ! empty( $attachments ) ) {
foreach ( $attachments as $attachment ) {
$id = $attachment->ID;
$file = get_attached_file( $id );
$metadata = wp_generate_attachment_metadata( $id, $file );
wp_update_attachment_metadata( $id, $metadata );
}
}
$data = array(
'action' => 'jet-data-thumbnails',
'offset' => $offset + $step,
'step' => $step,
'total' => $total,
'isLast' => $is_last,
'complete' => round( ( $offset + $step ) * 100 / ( $total * $step ) ),
);
if ( $is_last ) {
$data['redirect'] = jdi()->page_url( array( 'tab' => $this->slug, 'step' => 4 ) );
}
jdi_cache()->write_cache();
wp_send_json_success( $data );
}
/**
* Process single chunk import
*
* @return void
*/
public function import_chunk() {
$this->validate_request();
if ( empty( $_REQUEST['chunk'] ) ) {
jdi_cache()->write_cache();
wp_send_json_error( array(
'message' => esc_html__( 'Chunk number is missing in request', 'jet-data-importer' ),
) );
}
$chunk = intval( $_REQUEST['chunk'] );
$chunks = jdi_cache()->get( 'chunks_count' );
$processed = jdi_cache()->get( 'processed_summary' );
require_once jdi()->path( 'includes/import/class-jet-data-importer-extensions.php' );
switch ( $chunk ) {
case $chunks:
// Process last step (remapping and finalizing)
$this->remap_all();
jdi_cache()->clear_cache();
flush_rewrite_rules();
$redirect = jdi()->page_url(
array(
'tab' => $this->slug,
'step' => $this->is_regenerate_required() ? 3 : 4,
)
);
/**
* Hook on last import chunk
*/
do_action( 'jet-data-importer/import/finish' );
$data = array(
'isLast' => true,
'complete' => 100,
'processed' => $processed,
'redirect' => $redirect,
);
// Remove XML file for remote files after successfull import.
$file = $this->get_import_file();
if ( $file && isset( $_REQUEST['file'] ) && 'remote' === $_REQUEST['file'] ) {
unlink( $file );
}
break;
default:
// Process regular step
$offset = $this->chunk_size() * ( $chunk - 1 );
$importer = $this->get_importer();
$importer->chunked_import( $this->chunk_size(), $offset );
/**
* Hook on last import chunk
*/
do_action( 'jet-data-importer/import/chunk', $chunk );
$data = array(
'action' => 'jet-data-import-chunk',
'chunk' => $chunk + 1,
'complete' => round( ( $chunk * 100 ) / $chunks ),
'processed' => $processed,
);
break;
}
jdi_cache()->write_cache();
wp_send_json_success( $data );
}
/**
* Return importer object
*
* @return object
*/
public function get_importer() {
if ( null !== $this->importer ) {
return $this->importer;
}
require_once jdi()->path( 'includes/import/class-jet-wxr-importer.php' );
$options = array();
$file = $this->get_import_file();
if ( ! $file ) {
return false;
}
return $this->importer = new Jet_WXR_Importer( $options, $file );
}
/**
* Get path to imported XML file
*
* @return [type] [description]
*/
public function get_import_file() {
if ( null !== $this->import_file ) {
return $this->import_file;
}
$file = null;
if ( ! empty( $_REQUEST['file'] ) ) {
$file = jdi_tools()->esc_path( esc_attr( $_REQUEST['file'] ) );
}
if ( ! $file || ! file_exists( $file ) ) {
$file = jdi()->get_setting( array( 'xml', 'path' ) );
}
if ( is_array( $file ) ) {
$file = $file[0];
}
if ( isset( $_REQUEST['file'] ) && 'remote' === $_REQUEST['file'] ) {
$import_settings = jdi()->get_setting( array( 'advanced_import' ) );
$slug = isset( $_REQUEST['skin'] ) ? esc_attr( $_REQUEST['skin'] ) : 'default';
$xml_type = isset( $_REQUEST['xml_type'] ) ? esc_attr( $_REQUEST['xml_type'] ) : 'lite';
if ( isset( $import_settings[ $slug ][ $xml_type ] ) ) {
$file = $this->get_remote_file( $import_settings[ $slug ][ $xml_type ] );
}
}
if ( ! $file ) {
return false;
} else {
$this->import_file = $file;
return $this->import_file;
}
}
/**
* Get remoe file by URL
*
* @param [type] $file_path [description]
* @return [type] [description]
*/
public function get_remote_file( $file_url ) {
$filename = basename( $file_url );
$base_path = jdi_files_manager()->base_path();
if ( is_file( $base_path . $filename ) ) {
return $base_path . $filename;
}
$tmpath = download_url( esc_url( $file_url ) );
if ( ! $tmpath ) {
return false;
}
if ( ! copy( $tmpath, $base_path . $filename ) ) {
return false;
}
unlink( $tmpath );
return $base_path . $filename;
}
/**
* Remap all required data after installation completed
*
* @return void
*/
public function remap_all() {
require_once jdi()->path( 'includes/import/class-jet-data-importer-remap-callbacks.php' );
/**
* Attach all posts remapping related callbacks to this hook
*
* @param array Posts remapping data. Format: old_id => new_id
*/
do_action( 'jet-data-importer/import/remap-posts', jdi_cache()->get( 'posts', 'mapping' ) );
/**
* Attach all terms remapping related callbacks to this hook
*
* @param array Terms remapping data. Format: old_id => new_id
*/
do_action( 'jet-data-importer/import/remap-terms', jdi_cache()->get( 'term_id', 'mapping' ) );
/**
* Attach all comments remapping related callbacks to this hook
*
* @param array COmments remapping data. Format: old_id => new_id
*/
do_action( 'jet-data-importer/import/remap-comments', jdi_cache()->get( 'comments', 'mapping' ) );
/**
* Attach all posts_meta remapping related callbacks to this hook
*
* @param array posts_meta data. Format: new_id => related keys array
*/
do_action( 'jet-data-importer/import/remap-posts-meta', jdi_cache()->get( 'posts_meta', 'requires_remapping' ) );
/**
* Attach all terms meta remapping related callbacks to this hook
*
* @param array terms meta data. Format: new_id => related keys array
*/
do_action( 'jet-data-importer/import/remap-terms-meta', jdi_cache()->get( 'terms_meta', 'requires_remapping' ) );
}
/**
* Get welcome message for importer starter page
*
* @return string
*/
public function get_welcome_message() {
$files = $this->get_xml_count();
if ( 0 === $files ) {
$message = __( 'Upload XML file with demo content', 'jet-data-importer' );
}
if ( 1 === $files ) {
$message = __( 'We found 1 XML file with demo content in your theme, install it?', 'jet-data-importer' );
}
if ( 1 < $files ) {
$message = sprintf(
__( 'We found %s XML files in your theme. Please select one of them to install', 'jet-data-importer' ),
$files
);
}
return '<div class="cdi-message">' . $message . '</div>';
}
/**
* Get available XML count
*
* @return int
*/
public function get_xml_count() {
if ( null !== $this->xml_count ) {
return $this->xml_count;
}
$files = jdi()->get_setting( array( 'xml', 'path' ) );
if ( ! $files ) {
$this->xml_count = 0;
} elseif ( ! is_array( $files ) ) {
$this->xml_count = 1;
} else {
$this->xml_count = count( $files );
}
return $this->xml_count;
}
/**
* Returns HTML-markup of import files select
*
* @return string
*/
public function get_import_files_select( $before = '<div>', $after = '</div>' ) {
$files = jdi()->get_setting( array( 'xml', 'path' ) );
if ( ! $files && ! is_array( $files ) ) {
return;
}
if ( 1 > count( $files ) ) {
return;
}
$wrap_format = '<select name="import_file">%1$s</select>';
$item_format = '<option value="%1$s" %3$s>%2$s</option>';
$selected = 'selected="selected"';
$result = '';
foreach ( $files as $name => $file ) {
$result .= sprintf( $item_format, jdi_tools()->secure_path( $file ), $name, $selected );
$selected = '';
}
return $before . sprintf( $wrap_format, $result ) . $after;
}
/**
* Retuns HTML markup for import file uploader
*
* @param string $before HTML markup before input.
* @param string $after HTML markup after input.
* @return string
*/
public function get_import_file_input( $before = '<div>', $after = '</div>' ) {
if ( ! jdi()->get_setting( array( 'xml', 'use_upload' ) ) ) {
return;
}
$result = '<div class="import-file">';
$result .= '<input type="hidden" name="upload_file" class="import-file__input">';
$result .= '<input type="text" name="upload_file_nicename" class="import-file__placeholder">';
$result .= '<button class="cdi-btn primary" id="jet-file-upload">';
$result .= esc_html__( 'Upload File', 'jet-data-importer' );
$result .= '</button>';
$result .= '</div>';
return $before . $result . $after;
}
/**
* Check if advanced import is allowed
*
* @since 1.1.0
* @return boolean
*/
public function is_advanced_import() {
$advanced = jdi()->get_setting( array( 'advanced_import' ) );
return ! empty( $advanced );
}
/**
* Show advanced import block.
*
* @since 1.1.0
* @return null
*/
public function advanced_import() {
if ( ! $this->is_advanced_import() ) {
return;
}
$advanced = jdi()->get_setting( array( 'advanced_import' ) );
foreach ( $advanced as $slug => $item ) {
$this->data['advanced-item'] = $item;
$this->data['advanced-slug'] = $slug;
jdi()->get_template( 'import-advanced.php' );
}
}
/**
* Show password form if is replace installation type.
*
* @since 1.1.0
* @return null
*/
public function remove_content_form() {
if ( ! isset( $_GET['type'] ) || 'replace' !== $_GET['type'] ) {
return;
}
if ( ! current_user_can( 'delete_users' ) ) {
esc_html_e(
'You don\'t have permissions to replace content, please re-enter with admiistrator account',
'jet-data-importer'
);
return;
}
jdi()->get_template( 'remove-content-form.php' );
}
/**
* Retrieve XML file path by URL
*
* @return string
*/
public function get_file_path() {
$this->validate_request();
if ( ! isset( $_REQUEST['file'] ) ) {
jdi_cache()->write_cache();
wp_send_json_error( array(
'message' => esc_html__( 'XML file not passed', 'jet-data-importer' ),
) );
}
$path = str_replace( home_url( '/' ), ABSPATH, esc_url( $_REQUEST['file'] ) );
jdi_cache()->write_cache();
wp_send_json_success( array(
'path' => jdi_tools()->secure_path( $path ),
) );
}
/**
* Returns the instance.
*
* @since 1.0.0
* @return object
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
}
}
/**
* Returns instance of Jet_Data_Importer_Interface
*
* @return object
*/
function jdi_interface() {
return Jet_Data_Importer_Interface::get_instance();
}

View File

@@ -0,0 +1,694 @@
<?php
/**
* Jimporter post processing callbacks
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( ! class_exists( 'Jet_Data_Importer_Callbacks' ) ) {
/**
* Define Jet_Data_Importer_Callbacks class
*/
class Jet_Data_Importer_Callbacks {
/**
* A reference to an instance of this class.
*
* @since 1.0.0
* @var object
*/
private static $instance = null;
/**
* Holder for terms data
*
* @var array
*/
public $terms = array();
public $pages = null;
/**
* Store processed shortcodes data
*
* @var array
*/
private $shortcodes_data = array();
/**
* Constructor for the class
*/
public function __construct() {
// Manipulations with posts remap array
add_action( 'jet-data-importer/import/remap-posts', array( $this, 'process_options' ) );
add_action( 'jet-data-importer/import/remap-posts', array( $this, 'postprocess_posts' ) );
add_action( 'jet-data-importer/import/remap-posts', array( $this, 'process_thumbs' ) );
add_action( 'jet-data-importer/import/remap-posts', array( $this, 'process_elementor_pages_posts' ) );
add_action( 'jet-data-importer/import/remap-posts', array( $this, 'process_home_page' ) );
// Manipulations with terms remap array
add_action( 'jet-data-importer/import/remap-terms', array( $this, 'process_term_parents' ) );
add_action( 'jet-data-importer/import/remap-terms', array( $this, 'process_nav_menu' ) );
add_action( 'jet-data-importer/import/remap-terms', array( $this, 'process_nav_menu_widgets' ) );
add_action( 'jet-data-importer/import/remap-terms', array( $this, 'process_elementor_pages_terms' ) );
add_action( 'jet-data-importer/import/remap-terms', array( $this, 'process_home_page' ) );
}
public function elementor_pages() {
if ( null === $this->pages ) {
$this->pages = get_posts( array(
'post_type' => array( 'page', 'jet-theme-core', 'elementor_library' ),
'posts_per_page' => -1,
) );
}
return $this->pages;
}
/**
* Remap elementor images
*
* @todo remplace images in elementor widgets with imported.
* @return void
*/
public function process_elementor_pages_posts( $data ) {
$pages = $this->elementor_pages();
foreach ( $pages as $page ) {
$elementor_data = get_post_meta( $page->ID, '_elementor_data', true );
if ( empty( $elementor_data ) ) {
continue;
}
$new_data = preg_replace_callback(
'/(\"id\":\"?(\d+)\"?,\"url\":\"([^\"\']*?)\"|\"url\":\"([^\"\']*?)\",\"id\":\"?(\d+)\"?)/',
function( $match ) use ( $data ) {
$id = false;
if ( ! empty( $match[1] ) ) {
$id = $match[1];
} elseif ( ! empty( $match[4] ) ) {
$id = $match[4];
}
if ( ! $id || ! isset( $data[ $id ] ) ) {
return $match[0];
} else {
$result = sprintf(
'"url":%2$s,"id":%1$s',
$data[ $id ],
json_encode( wp_get_attachment_url( $data[ $id ] ) )
);
return $result;
}
},
$elementor_data
);
$ids_keys = apply_filters( 'jet-data-importer/import/posts/elementor-ids-to-remap', array(
'panel_template_id',
'item_template_id',
) );
$ids_keys = implode( '|', $ids_keys );
$ids_regex = "/\\\"({$ids_keys})\\\":\\\"(\d+)\\\"/";
$new_data = preg_replace_callback( $ids_regex, function( $match ) use ( $data ) {
if ( isset( $data[ $match[2] ] ) ) {
return sprintf(
'"%1$s":"%2$s"',
$match[1],
$data[ $match[2] ]
);
} else {
return $match[0];
}
}, $new_data );
update_post_meta( $page->ID, '_elementor_data', wp_slash( $new_data ) );
}
}
public function process_elementor_pages_terms( $data ) {
$pages = $this->elementor_pages();
$ids_keys = apply_filters( 'jet-data-importer/import/terms/elementor-ids-to-remap', array(
'category_ids',
'menu',
'nav_menu',
) );
$ids_keys = implode( '|', $ids_keys );
$regex = '\"(' . $ids_keys . ')\":(\".*?\"|\[.*?\])';
foreach ( $pages as $page ) {
$elementor_data = get_post_meta( $page->ID, '_elementor_data', true );
if ( empty( $elementor_data ) ) {
continue;
}
$new_data = preg_replace_callback( '/' . $regex . '/', function( $match ) use ( $data ) {
$val = json_decode( $match[2], true );
if ( ! is_array( $val ) ) {
$new = isset( $data[ $val ] ) ? $data[ $val ] : $val;
$new = '"' . $new . '"';
} else {
$new = array();
foreach ( $val as $old_id ) {
$new = isset( $data[ $old_id ] ) ? $data[ $old_id ] : $old_id;
}
$new = json_encode( $new );
}
return sprintf(
'"%1$s":%2$s',
$match[1],
$new
);
}, $elementor_data );
update_post_meta( $page->ID, '_elementor_data', wp_slash( $new_data ) );
}
}
/**
* Remap IDs in home page content
*
* @param array $data Mapped terms data.
* @return void|false
*/
public function process_home_page( $data ) {
$regex = apply_filters( 'jet-data-importer/import/home-regex-replace', array() );
if ( empty( $regex ) ) {
return false;
}
$pages = apply_filters( 'jet-data-importer/import/add-pages-to-replace', array() );
if ( ! empty( $pages ) ) {
$pages = array_map( array( $this, 'get_page_ids' ), $pages );
}
$home_id = get_option( 'page_on_front' );
$pages = array_merge( array( $home_id ), $pages );
$pages = array_filter( $pages );
if ( ! $pages ) {
return false;
}
$regex = array_map( array( $this, 'prepare_regex' ), $regex );
foreach ( $pages as $page_id ) {
$page = get_post( $page_id );
$this->terms = $data;
$content = preg_replace_callback( $regex, array( $this, 'replace_ids' ), $page->post_content );
$new_page = array(
'ID' => $page_id,
'post_content' => $content,
);
wp_update_post( $new_page );
}
}
/**
* Get page ids by slug
*
* @return int|bool
*/
public function get_page_ids( $slug ) {
$page = get_page_by_path( $slug );
if ( $page ) {
return $page->ID;
} else {
return false;
}
return get_page_by_path( $slug );
}
/**
* Replace ids in shortcodes
*
* @return string
*/
public function replace_ids( $matches ) {
if ( 5 !== count( $matches ) ) {
return $matches[0];
}
$tag = $matches[2];
$attr = $matches[3];
$data = $this->shortcodes_data;
$delimiter = isset( $data[ $tag ][ $attr ] ) ? $data[ $tag ][ $attr ] : ',';
$ids = explode( $delimiter, $matches[4] );
$new_ids = array();
foreach ( $ids as $id ) {
if ( isset( $this->terms[ $id ] ) ) {
$new_ids[] = $this->terms[ $id ];
} else {
$new_ids[] = $id;
}
}
$new_ids = implode( $delimiter, $new_ids );
$return = sprintf( '%1$s="%2$s"', $matches[1], $new_ids );
return $return;
}
/**
* Callback for regex map
*
* @param array $item Regex item.
* @return string
*/
public function prepare_regex( $item ) {
$delimiter = isset( $item['delimiter'] ) ? $item['delimiter'] : ',';
$tag = $item['shortcode'];
$attr = $item['attr'];
if ( ! isset( $this->shortcodes_data[ $tag ] ) ) {
$this->shortcodes_data[ $tag ] = array();
}
$this->shortcodes_data[ $tag ][ $attr ] = $delimiter;
return '/(\[(' . $item['shortcode'] . ')[^\]]*(' . $item['attr'] . '))="([0-9\,\s]*)"/';
}
/**
* Set correctly term parents
*
* @param array $data Mapped terms data.
* @return void|false
*/
public function process_term_parents( $data ) {
$remap_terms = jdi_cache()->get( 'terms', 'requires_remapping' );
$processed_term_slug = jdi_cache()->get( 'term_slug', 'mapping' );
if ( empty( $remap_terms ) ) {
return false;
}
foreach ( $remap_terms as $term_id => $taxonomy ) {
$parent_slug = get_term_meta( $term_id, '_wxr_import_parent', true );
if ( ! $parent_slug ) {
continue;
}
$term_mapping_key = $taxonomy . '-' . $parent_slug;
if ( empty( $processed_term_slug[ $term_mapping_key ] ) ) {
continue;
}
wp_update_term( $term_id, $taxonomy, array(
'parent' => (int) $processed_term_slug[ $term_mapping_key ],
) );
}
}
/**
* Replace term thumbnails IDs with new ones
*
* @param array $data
* @return void
*/
public function process_thumbs( $data ) {
global $wpdb;
$query_term = "
SELECT term_id, meta_key, meta_value
FROM $wpdb->termmeta
WHERE meta_key LIKE '%_thumb'
";
$thumb_term = $wpdb->get_results( $query_term, ARRAY_A );
$query_post = "
SELECT post_id, meta_key, meta_value
FROM $wpdb->postmeta
WHERE meta_key = '_thumbnail_id'
";
$thumb_post = $wpdb->get_results( $query_post, ARRAY_A );
if ( empty( $thumb_term ) ) {
$thumb_term = array();
}
if ( empty( $thumb_post ) ) {
$thumb_post = array();
}
$thumbnails = array_merge( $thumb_term, $thumb_post );
foreach ( $thumbnails as $thumb_data ) {
$meta_key = $thumb_data['meta_key'];
$current = $thumb_data['meta_value'];
if ( '_thumbnail_id' === $meta_key ){
$id = $thumb_data['post_id'];
$func = 'update_post_meta';
} else {
$id = $thumb_data['term_id'];
$func = 'update_term_meta';
}
if ( ! empty( $data[ $current ] ) ) {
call_user_func( $func, $id, $meta_key, $data[ $current ] );
}
}
}
/**
* Post-process posts.
*
* @param array $todo Remap data.
* @return void
*/
public function postprocess_posts( $mapping ) {
$todo = jdi_cache()->get( 'posts', 'requires_remapping' );
$user_slug = jdi_cache()->get( 'user_slug', 'mapping' );
$url_remap = jdi_cache()->get_group( 'url_remap' );
foreach ( $todo as $post_id => $_ ) {
$data = array();
$updated_links = '';
$old_links = '';
$post = get_post( $post_id );
$parent_id = get_post_meta( $post_id, '_wxr_import_parent', true );
if ( ! empty( $parent_id ) && isset( $mapping['post'][ $parent_id ] ) ) {
$data['post_parent'] = $mapping['post'][ $parent_id ];
}
$author_slug = get_post_meta( $post_id, '_wxr_import_user_slug', true );
if ( ! empty( $author_slug ) && isset( $user_slug[ $author_slug ] ) ) {
$data['post_author'] = $user_slug[ $author_slug ];
}
$has_attachments = get_post_meta( $post_id, '_wxr_import_has_attachment_refs', true );
if ( ! empty( $has_attachments ) ) {
$content = $post->post_content;
// Replace all the URLs we've got
$new_content = str_replace( array_keys( $url_remap ), $url_remap, $content );
if ( $new_content !== $content ) {
$data['post_content'] = $new_content;
}
}
if ( in_array( get_post_type( $post_id ), array( 'page', 'post' ) ) ) {
$old_links = ! empty( $data['post_content'] ) ? $data['post_content'] : $post->post_content;
$updated_links = str_replace( jdi_cache()->get( 'home' ), home_url(), $old_links );
if ( $updated_links !== $old_links ) {
$data['post_content'] = $updated_links;
}
}
if ( get_post_type( $post_id ) === 'nav_menu_item' ) {
$this->postprocess_menu_item( $post_id );
}
// Do we have updates to make?
if ( empty( $data ) ) {
continue;
}
// Run the update
$data['ID'] = $post_id;
$result = wp_update_post( $data, true );
if ( is_wp_error( $result ) ) {
continue;
}
// Clear out our temporary meta keys
delete_post_meta( $post_id, '_wxr_import_parent' );
delete_post_meta( $post_id, '_wxr_import_user_slug' );
delete_post_meta( $post_id, '_wxr_import_has_attachment_refs' );
}
}
/**
* Post-process menu items.
*
* @param int $post_id Processed post ID
* @return void
*/
public function postprocess_menu_item( $post_id ) {
$menu_object_id = get_post_meta( $post_id, '_wxr_import_menu_item', true );
if ( empty( $menu_object_id ) ) {
// No processing needed!
return;
}
$processed_term_id = jdi_cache()->get( 'term_id', 'mapping' );
$processed_posts = jdi_cache()->get( 'posts', 'mapping' );
$menu_item_type = get_post_meta( $post_id, '_menu_item_type', true );
switch ( $menu_item_type ) {
case 'taxonomy':
if ( isset( $processed_term_id[ $menu_object_id ] ) ) {
$menu_object = $processed_term_id[ $menu_object_id ];
}
break;
case 'post_type':
if ( isset( $processed_posts[ $menu_object_id ] ) ) {
$menu_object = $processed_posts[ $menu_object_id ];
}
break;
default:
// Cannot handle this.
return;
}
if ( ! empty( $menu_object ) ) {
update_post_meta( $post_id, '_menu_item_object_id', wp_slash( $menu_object ) );
}
delete_post_meta( $post_id, '_wxr_import_menu_item' );
}
/**
* Remap page ids in imported options
*
* @param array $data Remap data.
* @return void
*/
public function process_options( $data ) {
$options_to_process = array(
'page_on_front',
'page_for_posts',
);
foreach ( $options_to_process as $key ) {
$current = get_option( $key );
if ( ! $current || ! isset( $data[ $current ] ) ) {
continue;
}
update_option( $key, $data[ $current ] );
}
// Update Jet Theme Core conditions
$conditions = get_option( 'jet_site_conditions' );
if ( ! empty( $conditions ) ) {
$new_conditions = array();
foreach ( $conditions as $location => $condition ) {
$new_conditions[ $location ] = array();
foreach ( $condition as $template_id => $rules ) {
if ( isset( $data[ $template_id ] ) ) {
$new_conditions[ $location ][ $data[ $template_id ] ] = $rules;
} else {
$new_conditions[ $location ][ $template_id ] = $rules;
}
}
}
update_option( 'jet_site_conditions', $new_conditions );
}
}
/**
* Remap nav menu ids
*
* @param array $data Remap data.
* @return void
*/
public function process_nav_menu( $data ) {
$locations = get_nav_menu_locations();
if ( empty( $locations ) ) {
return;
}
$new_locations = array();
foreach ( $locations as $location => $id ) {
if ( isset( $data[ $id ] ) ) {
$new_locations[ $location ] = $data[ $id ];
} else {
$new_locations[ $location ] = $id;
}
}
set_theme_mod( 'nav_menu_locations', $new_locations );
}
/**
* Remap menu IDs in widgets
*
* @param array $data Remap data.
* @return void
*/
public function process_nav_menu_widgets( $data ) {
$widget_menus = get_option( 'widget_nav_menu' );
if ( empty( $widget_menus ) ) {
return;
}
$new_widgets = array();
foreach ( $widget_menus as $key => $widget ) {
if ( '_multiwidget' === $key ) {
$new_widgets['_multiwidget'] = $widget;
continue;
}
if ( empty( $widget['nav_menu'] ) ) {
$new_widgets[] = $widget;
continue;
}
$id = $widget['nav_menu'];
if ( isset( $data[ $id ] ) ) {
$widget['nav_menu'] = $data[ $id ];
}
$new_widgets[ $key ] = $widget;
}
update_option( 'widget_nav_menu', $new_widgets );
}
/**
* Returns the instance.
*
* @since 1.0.0
* @return object
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
}
}
/**
* Returns instance of Jet_Data_Importer_Callbacks
*
* @return object
*/
function jdi_remap_callbacks() {
return Jet_Data_Importer_Callbacks::get_instance();
}
jdi_remap_callbacks();

File diff suppressed because it is too large Load Diff