| 1 |
<?php |
| 2 |
namespace Elementor\Modules\CloudLibrary; |
| 3 |
|
| 4 |
use Elementor\Core\Base\Module as BaseModule; |
| 5 |
use Elementor\Core\Common\Modules\Connect\Module as ConnectModule; |
| 6 |
use Elementor\Core\Documents_Manager; |
| 7 |
use Elementor\Core\Frontend\Render_Mode_Manager; |
| 8 |
use Elementor\Modules\CloudLibrary\Connect\Cloud_Library; |
| 9 |
use Elementor\Core\Common\Modules\Connect\Apps\Library; |
| 10 |
use Elementor\Core\Experiments\Manager as ExperimentsManager; |
| 11 |
use Elementor\Plugin; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; // Exit if accessed directly. |
| 15 |
} |
| 16 |
|
| 17 |
class Module extends BaseModule { |
| 18 |
|
| 19 |
/** |
| 20 |
* @var callable |
| 21 |
*/ |
| 22 |
protected $print_preview_callback; |
| 23 |
|
| 24 |
public function get_name(): string { |
| 25 |
return 'cloud-library'; |
| 26 |
} |
| 27 |
|
| 28 |
public function __construct() { |
| 29 |
parent::__construct(); |
| 30 |
|
| 31 |
$this->register_app(); |
| 32 |
|
| 33 |
add_action( 'elementor/init', function () { |
| 34 |
$this->set_cloud_library_settings(); |
| 35 |
}, 12 /** After the initiation of the connect cloud library */ ); |
| 36 |
|
| 37 |
add_filter( 'elementor/editor/localize_settings', function ( $settings ) { |
| 38 |
return $this->localize_settings( $settings ); |
| 39 |
}, 11 /** After Elementor Core */ ); |
| 40 |
|
| 41 |
add_filter( 'elementor/render_mode/module', function( $module_name ) { |
| 42 |
$render_mode_manager = \Elementor\Plugin::$instance->frontend->render_mode_manager; |
| 43 |
|
| 44 |
if ( $render_mode_manager ) { |
| 45 |
$current_render_mode = $render_mode_manager->get_current(); |
| 46 |
|
| 47 |
if ( $current_render_mode instanceof \Elementor\Modules\CloudLibrary\Render_Mode_Preview ) { |
| 48 |
return 'cloud-library'; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return $module_name; |
| 53 |
}, 12); |
| 54 |
} |
| 55 |
|
| 56 |
public function localize_settings( $settings ) { |
| 57 |
if ( isset( $settings['i18n'] ) ) { |
| 58 |
$settings['i18n']['folder'] = esc_html__( 'Folder', 'elementor' ); |
| 59 |
} |
| 60 |
|
| 61 |
$settings['library']['doc_types'] = $this->get_document_types(); |
| 62 |
|
| 63 |
return $settings; |
| 64 |
} |
| 65 |
|
| 66 |
private function register_app() { |
| 67 |
add_action( 'elementor/connect/apps/register', function ( ConnectModule $connect_module ) { |
| 68 |
$connect_module->register_app( 'cloud-library', Cloud_Library::get_class_name() ); |
| 69 |
} ); |
| 70 |
|
| 71 |
add_action( 'elementor/frontend/render_mode/register', [ $this, 'register_render_mode' ] ); |
| 72 |
|
| 73 |
add_action( 'elementor/documents/register', function ( Documents_Manager $documents_manager ) { |
| 74 |
$documents_manager->register_document_type( |
| 75 |
Documents\Cloud_Template_Preview::TYPE, |
| 76 |
Documents\Cloud_Template_Preview::get_class_full_name() |
| 77 |
); |
| 78 |
}); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @param Render_Mode_Manager $manager |
| 83 |
* |
| 84 |
* @throws \Exception If render mode registration fails. |
| 85 |
*/ |
| 86 |
public function register_render_mode( Render_Mode_Manager $manager ) { |
| 87 |
$manager->register_render_mode( Render_Mode_Preview::class ); |
| 88 |
} |
| 89 |
|
| 90 |
private function set_cloud_library_settings() { |
| 91 |
if ( ! Plugin::$instance->common ) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
/** @var ConnectModule $connect */ |
| 96 |
$connect = Plugin::$instance->common->get_component( 'connect' ); |
| 97 |
|
| 98 |
/** @var Library $library */ |
| 99 |
$library = $connect->get_app( 'library' ); |
| 100 |
|
| 101 |
if ( ! $library ) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
Plugin::$instance->app->set_settings( 'cloud-library', [ |
| 106 |
'library_connect_url' => esc_url( $library->get_admin_url( 'authorize', [ |
| 107 |
'utm_source' => 'template-library', |
| 108 |
'utm_medium' => 'wp-dash', |
| 109 |
'utm_campaign' => 'library-connect', |
| 110 |
'utm_content' => 'cloud-library', |
| 111 |
'source' => 'cloud-library', |
| 112 |
] ) ), |
| 113 |
'library_connect_title_copy' => esc_html__( 'Connect to your Elementor account', 'elementor' ), |
| 114 |
'library_connect_sub_title_copy' => esc_html__( 'Then you can find all your templates in one convenient library.', 'elementor' ), |
| 115 |
'library_connect_button_copy' => esc_html__( 'Connect', 'elementor' ), |
| 116 |
] ); |
| 117 |
} |
| 118 |
|
| 119 |
private function get_document_types() { |
| 120 |
$document_types = Plugin::$instance->documents->get_document_types( [ |
| 121 |
'show_in_library' => true, |
| 122 |
] ); |
| 123 |
|
| 124 |
$data = []; |
| 125 |
|
| 126 |
foreach ( $document_types as $name => $document_type ) { |
| 127 |
$data[ $name ] = $document_type::get_title(); |
| 128 |
} |
| 129 |
|
| 130 |
return $data; |
| 131 |
} |
| 132 |
|
| 133 |
public function print_content() { |
| 134 |
if ( ! $this->print_preview_callback ) { |
| 135 |
$this->print_preview_callback = [ $this, 'print_thumbnail_preview_callback' ]; |
| 136 |
} |
| 137 |
|
| 138 |
call_user_func( $this->print_preview_callback ); |
| 139 |
} |
| 140 |
|
| 141 |
private function print_thumbnail_preview_callback() { |
| 142 |
$doc = Plugin::$instance->documents->get_current(); |
| 143 |
|
| 144 |
if ( ! $doc ) { |
| 145 |
$render_mode = Plugin::$instance->frontend->render_mode_manager->get_current(); |
| 146 |
if ( $render_mode instanceof Render_Mode_Preview ) { |
| 147 |
$doc = $render_mode->get_document(); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
if ( ! $doc ) { |
| 152 |
echo '<div class="elementor-alert elementor-alert-danger">' . esc_html__( 'Document not found for preview.', 'elementor' ) . '</div>'; |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
Plugin::$instance->documents->switch_to_document( $doc ); |
| 157 |
|
| 158 |
$content = $doc->get_content( true ); |
| 159 |
|
| 160 |
echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 161 |
} |
| 162 |
} |
| 163 |
|