Last active
April 15, 2019 21:09
-
-
Save wpmudev-sls/18254a77bb81d549acc51e0b32d778f8 to your computer and use it in GitHub Desktop.
[Cloner] - Cloner integrate Elementor. Integrate Elementor in Cloner. It clones Elementor's post meta and upload folders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Cloner integrate Elementor | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Integrate Elementor in Cloner. It clones Elementor's post meta and upload folders | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'Cloner_Integrate_Elementor' ) ) { | |
class Cloner_Integrate_Elementor { | |
private static $_instance = null; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ) { | |
self::$_instance = new Cloner_Integrate_Elementor(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
if ( ! class_exists('WPMUDEV_Cloner') ) { | |
return; | |
} | |
add_action( 'wpmudev_copier-copied-posts', array( $this, 'elementor_meta'), 10, 5 ); | |
add_action( 'wpmudev_copier-copy-after_copying', array( $this, 'clone_uploads_files'), 10, 1 ); | |
} | |
public function elementor_meta( $source_blog_id, $posts_mapping, $user_id, $template, $type ) { | |
global $wpdb; | |
//$elementor_meta_fields = array( '_elementor_edit_mode', '_elementor_data', '_elementor_version', '_elementor_css' ); | |
$elementor_meta_fields = array( '_elementor_data' ); | |
foreach ( $posts_mapping as $source_post_id => $target_post_id ) { | |
$elementor_meta_values = array(); | |
$value = false; | |
switch_to_blog( $source_blog_id ); | |
foreach ( $elementor_meta_fields as $meta_field ) { | |
$value = get_post_meta( $source_post_id, $meta_field, true ); | |
if ( ! $value || empty( $value ) ) { | |
continue; | |
} | |
$elementor_meta_values[$meta_field] = $value; | |
} | |
restore_current_blog(); | |
if ( empty( $elementor_meta_values ) ) { | |
continue; | |
} | |
foreach ( $elementor_meta_values as $field => $value ) { | |
if ( '_elementor_data' === $field ) { | |
$value = wp_slash( $this->maybe_reset_content_attachments( $value ) ); | |
} else { | |
$value = maybe_unserialize( $value ); | |
} | |
update_metadata( 'post', $target_post_id, $field, $value ); | |
} | |
} | |
} | |
public function maybe_reset_content_attachments( $value ) { | |
$settings = wpmudev_cloner_get_settings(); | |
// If attachments are cloned, we can keep original url and attachment id | |
if ( in_array( 'attachment', $settings['to_copy'] ) ) { | |
return $value; | |
} | |
// $value could be either an array or json. | |
$uses_json = $this->uses_json( $value ) ? true : false; | |
$array = $uses_json ? json_decode( $value, true ) : $value; | |
$content = $this->reset_content_attachments( $array ); | |
return $uses_json ? json_encode( $content ) : $content; | |
} | |
protected function reset_content_attachments( &$array ) { | |
if ( ! is_array( $array ) ) { | |
return $array; | |
} | |
unset( $array['image']['id'] ); | |
foreach ( $array as &$value ) { | |
if ( is_array( $value ) ) { | |
$this->reset_content_attachments( $value ); | |
} | |
} | |
return $array; | |
} | |
protected function uses_json ( $string ) { | |
json_decode( $string ); | |
return ( json_last_error() == JSON_ERROR_NONE ); | |
} | |
public function clone_uploads_files( $source_blog_id ) { | |
global $wp_filesystem; | |
if ( ! $this->instantiate_filesystem() ) { | |
return new WP_Error( 'wp_filesystem_error', 'The Filesystem could not be instatiated' ); | |
} | |
$base_dir = wp_upload_dir()['basedir']; | |
$elementor_dirs = array( 'elementor' ); | |
switch_to_blog( $source_blog_id ); | |
$directories = glob( wp_upload_dir()['basedir'] . '/*' , GLOB_ONLYDIR ); | |
restore_current_blog(); | |
foreach ( $directories as $source_dir_path ) { | |
$source_dir = basename( $source_dir_path ); | |
//if ( ! in_array( $source_dir, $elementor_dirs ) ) { | |
//continue; | |
//} | |
$target_dir_path = "{$base_dir}/{$source_dir}"; | |
// Don't copy date (year) folders | |
if ( filter_var( $source_dir, FILTER_VALIDATE_INT ) && 4 == strlen( $source_dir ) ) { | |
continue; | |
} | |
if ( ! $this->create_dir( $target_dir_path ) ) { | |
continue; | |
} | |
$copied = copy_dir( | |
$source_dir_path, | |
$target_dir_path | |
); | |
if ( is_wp_error( $copied ) ){ | |
error_log( "Errors on copying Upload dir {$source_dir}\n" ); | |
error_log( print_r( $copied->get_error_messages(), true ) ); | |
return new WP_Error( 'cloner_dir_copy_error', 'The uploads dir could not be cloned' ); | |
} | |
} | |
} | |
public function instantiate_filesystem( $url = '', $method = '', $context = '', $fields = null ) { | |
global $wp_filesystem; | |
if ( ! function_exists( 'WP_Filesystem' ) ) { | |
require_once ABSPATH . "wp-admin/includes/file.php"; | |
} | |
$credentials = request_filesystem_credentials( $url, $method, false, $context, $fields ); | |
if ( false === $credentials ) { | |
return false; | |
} | |
// Check if credentials are correct | |
if ( ! WP_Filesystem( $credentials ) ) { | |
request_filesystem_credentials( $url, $method, true, $context ); | |
return false; | |
} | |
return true; | |
} | |
public function create_dir( $dir ) { | |
$fullpath = trailingslashit( wp_normalize_path( "{$dir}" ) ); | |
wp_mkdir_p( $fullpath ); | |
return true; | |
} | |
} | |
if ( ! function_exists( 'wpmudev_cloner_integrate_elementor' ) ) { | |
function wpmudev_cloner_integrate_elementor(){ | |
return Cloner_Integrate_Elementor::get_instance(); | |
}; | |
add_action( 'plugins_loaded', 'wpmudev_cloner_integrate_elementor', 10 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this patch! This is really important for the people cloning sites that use Elementor. Please keep it updated.