PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.0-dev2
Elementor Website Builder – more than just a page builder v4.1.0-dev2
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / cloud-library / module.php

module.php in Elementor Website Builder – more than just a page builder 4.1.0-dev2, at modules/cloud-library/module.php

209 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if ( $this->is_screenshot_proxy_mode( $_GET ) ) { // phpcs:ignore -- Checking nonce inside the method.
56 echo $this->get_proxy_data( htmlspecialchars( $_GET['href'] ) ); // phpcs:ignore -- Nonce was checked on the above method
57 die;
58 }
59 }
60
61 public function get_proxy_data( $url ) {
62 $response = wp_safe_remote_get( $url );
63
64 if ( is_wp_error( $response ) ) {
65 return '';
66 }
67
68 $content_type = wp_remote_retrieve_headers( $response )->offsetGet( 'content-type' );
69
70 header( 'content-type: ' . $content_type );
71
72 return wp_remote_retrieve_body( $response );
73 }
74
75 public function localize_settings( $settings ) {
76 if ( isset( $settings['i18n'] ) ) {
77 $settings['i18n']['folder'] = esc_html__( 'Folder', 'elementor' );
78 }
79
80 $settings['library']['doc_types'] = $this->get_document_types();
81
82 return $settings;
83 }
84
85 private function register_app() {
86 add_action( 'elementor/connect/apps/register', function ( ConnectModule $connect_module ) {
87 $connect_module->register_app( 'cloud-library', Cloud_Library::get_class_name() );
88 } );
89
90 add_action( 'elementor/frontend/render_mode/register', [ $this, 'register_render_mode' ] );
91
92 add_action( 'elementor/documents/register', function ( Documents_Manager $documents_manager ) {
93 $documents_manager->register_document_type(
94 Documents\Cloud_Template_Preview::TYPE,
95 Documents\Cloud_Template_Preview::get_class_full_name()
96 );
97 });
98 }
99
100 /**
101 * @param Render_Mode_Manager $manager
102 *
103 * @throws \Exception If render mode registration fails.
104 */
105 public function register_render_mode( Render_Mode_Manager $manager ) {
106 $manager->register_render_mode( Render_Mode_Preview::class );
107 }
108
109 private function set_cloud_library_settings() {
110 if ( ! Plugin::$instance->common ) {
111 return;
112 }
113
114 /** @var ConnectModule $connect */
115 $connect = Plugin::$instance->common->get_component( 'connect' );
116
117 /** @var Library $library */
118 $library = $connect->get_app( 'library' );
119
120 if ( ! $library ) {
121 return;
122 }
123
124 Plugin::$instance->app->set_settings( 'cloud-library', [
125 'library_connect_url' => esc_url( $library->get_admin_url( 'authorize', [
126 'utm_source' => 'template-library',
127 'utm_medium' => 'wp-dash',
128 'utm_campaign' => 'library-connect',
129 'utm_content' => 'cloud-library',
130 'source' => 'cloud-library',
131 ] ) ),
132 'library_connect_title_copy' => esc_html__( 'Connect to your Elementor account', 'elementor' ),
133 'library_connect_sub_title_copy' => esc_html__( 'Then you can find all your templates in one convenient library.', 'elementor' ),
134 'library_connect_button_copy' => esc_html__( 'Connect', 'elementor' ),
135 ] );
136 }
137
138 private function get_document_types() {
139 $document_types = Plugin::$instance->documents->get_document_types( [
140 'show_in_library' => true,
141 ] );
142
143 $data = [];
144
145 foreach ( $document_types as $name => $document_type ) {
146 $data[ $name ] = $document_type::get_title();
147 }
148
149 return $data;
150 }
151
152 public function print_content() {
153 if ( ! $this->print_preview_callback ) {
154 $this->print_preview_callback = [ $this, 'print_thumbnail_preview_callback' ];
155 }
156
157 call_user_func( $this->print_preview_callback );
158 }
159
160 private function print_thumbnail_preview_callback() {
161 $doc = Plugin::$instance->documents->get_current();
162
163 if ( ! $doc ) {
164 $render_mode = Plugin::$instance->frontend->render_mode_manager->get_current();
165 if ( $render_mode instanceof Render_Mode_Preview ) {
166 $doc = $render_mode->get_document();
167 }
168 }
169
170 if ( ! $doc ) {
171 echo '<div class="elementor-alert elementor-alert-danger">' . esc_html__( 'Document not found for preview.', 'elementor' ) . '</div>';
172 return;
173 }
174
175 Plugin::$instance->documents->switch_to_document( $doc );
176
177 $content = $doc->get_content( true );
178
179 echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
180 }
181
182
183 protected function is_screenshot_proxy_mode( array $query_params ) {
184 $is_proxy = isset( $query_params['screenshot_proxy'] );
185
186 if ( $is_proxy ) {
187 if ( ! wp_verify_nonce( $query_params['nonce'], 'screenshot-proxy' ) ) {
188 // WP >= 6.2-alpha
189 if ( class_exists( '\WpOrg\Requests\Exception\Http\Status403' ) ) {
190 throw new \WpOrg\Requests\Exception\Http\Status403();
191 } else {
192 throw new \Requests_Exception_HTTP_403();
193 }
194 }
195
196 if ( ! $query_params['href'] ) {
197 // WP >= 6.2-alpha
198 if ( class_exists( '\WpOrg\Requests\Exception\Http\Status400' ) ) {
199 throw new \WpOrg\Requests\Exception\Http\Status400();
200 } else {
201 throw new \Requests_Exception_HTTP_400();
202 }
203 }
204 }
205
206 return $is_proxy;
207 }
208 }
209