wpmudev-sls
4/23/2018 - 5:35 PM

Cloner Integration With Elememnot

Cloner Integration With Elememnot

<?php

/**
* Plugin Name: Cloner Integration With Elementor
* Plugin URI: https://premium.wpmudev.org/
* Description: This plugin used a hack to fix compatibility issue with elementor plugin
* Author: Ariful Islam @ WPMUDEV
* Author URI: https://premium.wpmudev.org/profile/itsarifulislam
* License: GPLv2 or later
*/


if ( ! defined( 'ABSPATH' ) ) {
	exit;
}


if ( ! class_exists( 'Cloner_Elementor_Integration' ) ) {

	class Cloner_Elementor_Integration {

		private static $_instance = null;

		public $elementor_post_meta = array();

		public static function get_instance() {

			if( is_null( self::$_instance ) ) {
				self::$_instance = new Cloner_Elementor_Integration();
			}

			return self::$_instance;

		}

		private function __construct() {

			global $blog_templates;

			if ( class_exists('WPMUDEV_Cloner') ) {

				add_action( 'wpmudev_copier-copy-after_copying', array( $this, 'copy_elementor_css_files'), 10, 1 );

				add_action( 'wpmudev_copier-copy-post', array( $this, 'get_the_elementor_meta_data'), 10, 6 );

				add_action( 'wpmudev_copier-copied-posts', array( $this, 'update_elementor_meta_data'), 10 );

			}
			
		}

		public function update_elementor_meta_data() {

			global $wpdb;

			$table = $wpdb->get_blog_prefix() . 'postmeta';

			foreach ($this->elementor_post_meta as $data) {

				$wpdb->query( $wpdb->prepare( "UPDATE $table SET meta_value = %s WHERE post_id = %d AND meta_key = %s", $data['meta_value'], $data['post_id'], '_elementor_data') );

			}

		}

		public function get_the_elementor_meta_data( $source_blog_id, $post_id, $new_post, $user_id, $template, $post ) {

			if ( $post && $post->meta && isset( $post->meta['_elementor_data'] ) ) {
				
				$this->elementor_post_meta[] = array(
					'post_id' => $post_id,
					'meta_value' => $post->meta['_elementor_data']
				);

			}

		}

		public function copy_elementor_css_files( $source_blog_id ) {

			global $wpdb;

			$elementor_css_path = '/elementor/css/';

			switch_to_blog( $source_blog_id );
			$source = wp_upload_dir()['basedir'] . $elementor_css_path;
			
			restore_current_blog();
			$destination = wp_upload_dir()['basedir'] . $elementor_css_path;

			require_once ABSPATH . "wp-admin/includes/file.php";

			if ( ! WP_Filesystem() ) {
				exit;
			}

			global $wp_filesystem;
			$wp_filesystem->mkdir( $destination );
			
			copy_dir( $source, $destination );

		}

	}


	function Cloner_Elementor_Integration(){

		$GLOBALS['Cloner_Elementor_Integration'] = Cloner_Elementor_Integration::get_instance();

	}

	add_action( 'plugins_loaded', 'Cloner_Elementor_Integration' );

}