| 1 |
<?php |
| 2 |
namespace Elementor\Modules\CloudLibrary; |
| 3 |
|
| 4 |
use Elementor\Core\Frontend\RenderModes\Render_Mode_Base; |
| 5 |
use Elementor\Plugin; |
| 6 |
use Elementor\Utils; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
class Render_Mode_Preview extends Render_Mode_Base { |
| 13 |
const ENQUEUE_SCRIPTS_PRIORITY = 1000; |
| 14 |
|
| 15 |
const MODE = 'cloud-template-preview'; |
| 16 |
|
| 17 |
protected int $template_id; |
| 18 |
|
| 19 |
public function __construct( $template_id ) { |
| 20 |
$this->template_id = $template_id; |
| 21 |
$this->document = $this->create_document(); |
| 22 |
parent::__construct( $this->document->get_main_id() ); |
| 23 |
} |
| 24 |
|
| 25 |
public static function get_name() { |
| 26 |
return self::MODE; |
| 27 |
} |
| 28 |
|
| 29 |
public function prepare_render() { |
| 30 |
parent::prepare_render(); |
| 31 |
show_admin_bar( false ); |
| 32 |
|
| 33 |
add_filter( 'template_include', [ $this, 'filter_template' ] ); |
| 34 |
} |
| 35 |
|
| 36 |
public function filter_template() { |
| 37 |
return ELEMENTOR_PATH . 'modules/page-templates/templates/canvas.php'; |
| 38 |
} |
| 39 |
|
| 40 |
public function enqueue_scripts() { |
| 41 |
$suffix = ( Utils::is_script_debug() || Utils::is_elementor_tests() ) ? '' : '.min'; |
| 42 |
|
| 43 |
wp_enqueue_script( |
| 44 |
'dom-to-image', |
| 45 |
ELEMENTOR_ASSETS_URL . "/lib/dom-to-image/js/dom-to-image{$suffix}.js", |
| 46 |
[], |
| 47 |
'2.6.0', |
| 48 |
true |
| 49 |
); |
| 50 |
|
| 51 |
wp_enqueue_script( |
| 52 |
'html2canvas', |
| 53 |
ELEMENTOR_ASSETS_URL . "/lib/html2canvas/js/html2canvas{$suffix}.js", |
| 54 |
[], |
| 55 |
'1.4.1', |
| 56 |
true |
| 57 |
); |
| 58 |
|
| 59 |
wp_enqueue_script( |
| 60 |
'cloud-library-screenshot', |
| 61 |
ELEMENTOR_ASSETS_URL . "/js/cloud-library-screenshot{$suffix}.js", |
| 62 |
[ 'dom-to-image', 'html2canvas' ], |
| 63 |
ELEMENTOR_VERSION, |
| 64 |
true |
| 65 |
); |
| 66 |
|
| 67 |
$config = [ |
| 68 |
'selector' => '.elementor-' . $this->document->get_main_id(), |
| 69 |
'home_url' => home_url(), |
| 70 |
'post_id' => $this->document->get_main_id(), |
| 71 |
'template_id' => $this->template_id, |
| 72 |
]; |
| 73 |
|
| 74 |
wp_add_inline_script( 'cloud-library-screenshot', 'var ElementorScreenshotConfig = ' . wp_json_encode( $config ) . ';' ); |
| 75 |
} |
| 76 |
|
| 77 |
private function create_document() { |
| 78 |
if ( ! Plugin::$instance->common ) { |
| 79 |
Plugin::$instance->init_common(); |
| 80 |
} |
| 81 |
|
| 82 |
$document = Plugin::$instance->templates_manager->get_source( 'cloud' )->create_document_for_preview( $this->template_id ); |
| 83 |
|
| 84 |
if ( is_wp_error( $document ) ) { |
| 85 |
wp_die(); |
| 86 |
} |
| 87 |
|
| 88 |
Plugin::$instance->documents->switch_to_document( $document ); |
| 89 |
|
| 90 |
return $document; |
| 91 |
} |
| 92 |
} |
| 93 |
|