| 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 |
|
| 23 |
Plugin::$instance->db->switch_to_post( $this->document->get_main_id() ); |
| 24 |
|
| 25 |
Plugin::$instance->documents->switch_to_document( $this->document ); |
| 26 |
|
| 27 |
add_filter( 'template_include', [ $this, 'filter_template' ] ); |
| 28 |
|
| 29 |
add_action( 'wp_footer', [ $this, 'cleanup' ], 999 ); |
| 30 |
|
| 31 |
parent::__construct( $this->document->get_main_id() ); |
| 32 |
} |
| 33 |
|
| 34 |
public static function get_name() { |
| 35 |
return self::MODE; |
| 36 |
} |
| 37 |
|
| 38 |
public function prepare_render() { |
| 39 |
parent::prepare_render(); |
| 40 |
show_admin_bar( false ); |
| 41 |
} |
| 42 |
|
| 43 |
public function filter_template() { |
| 44 |
return ELEMENTOR_PATH . 'modules/page-templates/templates/canvas.php'; |
| 45 |
} |
| 46 |
|
| 47 |
public function cleanup() { |
| 48 |
if ( $this->document && $this->document->get_main_id() ) { |
| 49 |
wp_delete_post( $this->document->get_main_id(), true ); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
public function enqueue_scripts() { |
| 54 |
$suffix = ( Utils::is_script_debug() || Utils::is_elementor_tests() ) ? '' : '.min'; |
| 55 |
|
| 56 |
wp_enqueue_script( |
| 57 |
'cloud-library-screenshot', |
| 58 |
ELEMENTOR_ASSETS_URL . "/js/cloud-library-screenshot{$suffix}.js", |
| 59 |
[], |
| 60 |
ELEMENTOR_VERSION, |
| 61 |
true |
| 62 |
); |
| 63 |
|
| 64 |
$config = [ |
| 65 |
'selector' => '.elementor-' . $this->document->get_main_id(), |
| 66 |
'home_url' => home_url(), |
| 67 |
'post_id' => $this->document->get_main_id(), |
| 68 |
'template_id' => $this->template_id, |
| 69 |
]; |
| 70 |
|
| 71 |
wp_add_inline_script( 'cloud-library-screenshot', 'var ElementorScreenshotConfig = ' . wp_json_encode( $config ) . ';' ); |
| 72 |
} |
| 73 |
|
| 74 |
private function create_document() { |
| 75 |
if ( ! Plugin::$instance->common ) { |
| 76 |
Plugin::$instance->init_common(); |
| 77 |
} |
| 78 |
|
| 79 |
$document = Plugin::$instance->templates_manager->get_source( 'cloud' )->create_document_for_preview( $this->template_id ); |
| 80 |
|
| 81 |
if ( is_wp_error( $document ) ) { |
| 82 |
wp_die(); |
| 83 |
} |
| 84 |
|
| 85 |
return $document; |
| 86 |
} |
| 87 |
} |
| 88 |
|