PluginProbe
Elementor Website Builder – more than just a page builder / 3.30.0-dev3
Elementor Website Builder – more than just a page builder v3.30.0-dev3
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 3.30.0-dev3, at modules/cloud-library/module.php

206 lines 6.3 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_experiments();
32
33 if ( Plugin::$instance->experiments->is_feature_active( $this->get_name() ) ) {
34 $this->register_app();
35
36 add_action( 'elementor/init', function () {
37 $this->set_cloud_library_settings();
38 }, 12 /** After the initiation of the connect cloud library */ );
39
40 add_filter( 'elementor/editor/localize_settings', function ( $settings ) {
41 return $this->localize_settings( $settings );
42 }, 11 /** After Elementor Core */ );
43
44 add_filter( 'elementor/render_mode/module', function( $module_name ) {
45 $render_mode_manager = \Elementor\Plugin::$instance->frontend->render_mode_manager;
46
47 if ( $render_mode_manager && $render_mode_manager->get_current() instanceof \Elementor\Modules\CloudLibrary\Render_Mode_Preview ) {
48 return 'cloud-library';
49 }
50
51 return $module_name;
52 }, 12);
53
54 if ( $this->is_screenshot_proxy_mode( $_GET ) ) { // phpcs:ignore -- Checking nonce inside the method.
55 echo $this->get_proxy_data( htmlspecialchars( $_GET['href'] ) ); // phpcs:ignore -- Nonce was checked on the above method
56 die;
57 }
58 }
59 }
60
61 public function get_proxy_data( $url ) {
62 $response = wp_safe_remote_get( utf8_decode( $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_experiments() {
86 Plugin::$instance->experiments->add_feature( [
87 'name' => $this->get_name(),
88 'title' => esc_html__( 'Cloud Library', 'elementor' ),
89 'description' => esc_html__( 'Cloud Templates and Website Templates empowers you to save and manage design elements across all your projects. This feature is associated and connected to your Elementor Pro account and can be accessed from any website associated with your account.', 'elementor' ),
90 'release_status' => ExperimentsManager::RELEASE_STATUS_BETA,
91 'default' => ExperimentsManager::STATE_ACTIVE,
92 ] );
93 }
94
95 private function register_app() {
96 add_action( 'elementor/connect/apps/register', function ( ConnectModule $connect_module ) {
97 $connect_module->register_app( 'cloud-library', Cloud_Library::get_class_name() );
98 } );
99
100 add_action( 'elementor/frontend/render_mode/register', [ $this, 'register_render_mode' ] );
101
102 add_action( 'elementor/documents/register', function ( Documents_Manager $documents_manager ) {
103 $documents_manager->register_document_type(
104 Documents\Cloud_Template_Preview::TYPE,
105 Documents\Cloud_Template_Preview::get_class_full_name()
106 );
107 });
108 }
109
110 /**
111 * @param Render_Mode_Manager $manager
112 *
113 * @throws \Exception
114 */
115 public function register_render_mode( Render_Mode_Manager $manager ) {
116 $manager->register_render_mode( Render_Mode_Preview::class );
117 }
118
119 private function set_cloud_library_settings() {
120 if ( ! Plugin::$instance->common ) {
121 return;
122 }
123
124 /** @var ConnectModule $connect */
125 $connect = Plugin::$instance->common->get_component( 'connect' );
126
127 /** @var Library $library */
128 $library = $connect->get_app( 'library' );
129
130 if ( ! $library ) {
131 return;
132 }
133
134 Plugin::$instance->app->set_settings( 'cloud-library', [
135 'library_connect_url' => esc_url( $library->get_admin_url( 'authorize', [
136 'utm_source' => 'template-library',
137 'utm_medium' => 'wp-dash',
138 'utm_campaign' => 'library-connect',
139 'utm_content' => 'cloud-library',
140 'source' => 'cloud-library',
141 ] ) ),
142 'library_connect_title_copy' => esc_html__( 'Connect to your Elementor account', 'elementor' ),
143 'library_connect_sub_title_copy' => esc_html__( 'Then you can find all your templates in one convenient library.', 'elementor' ),
144 'library_connect_button_copy' => esc_html__( 'Connect', 'elementor' ),
145 ] );
146 }
147
148 private function get_document_types() {
149 $document_types = Plugin::$instance->documents->get_document_types( [
150 'show_in_library' => true,
151 ] );
152
153 $data = [];
154
155 foreach ( $document_types as $name => $document_type ) {
156 $data[ $name ] = $document_type::get_title();
157 }
158
159 return $data;
160 }
161
162 public function print_content() {
163 if ( ! $this->print_preview_callback ) {
164 $this->print_preview_callback = [ $this, 'print_thumbnail_preview_callback' ];
165 }
166
167 call_user_func( $this->print_preview_callback );
168 }
169
170 private function print_thumbnail_preview_callback() {
171 $doc = Plugin::$instance->documents->get_current();
172
173 // PHPCS - should not be escaped.
174 echo Plugin::$instance->frontend->get_builder_content_for_display( $doc->get_main_id(), true ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
175
176 wp_delete_post( $doc->get_main_id(), true );
177 }
178
179
180 protected function is_screenshot_proxy_mode( array $query_params ) {
181 $is_proxy = isset( $query_params['screenshot_proxy'] );
182
183 if ( $is_proxy ) {
184 if ( ! wp_verify_nonce( $query_params['nonce'], 'screenshot-proxy' ) ) {
185 // WP >= 6.2-alpha
186 if ( class_exists( '\WpOrg\Requests\Exception\Http\Status403' ) ) {
187 throw new \WpOrg\Requests\Exception\Http\Status403();
188 } else {
189 throw new \Requests_Exception_HTTP_403();
190 }
191 }
192
193 if ( ! $query_params['href'] ) {
194 // WP >= 6.2-alpha
195 if ( class_exists( '\WpOrg\Requests\Exception\Http\Status400' ) ) {
196 throw new \WpOrg\Requests\Exception\Http\Status400();
197 } else {
198 throw new \Requests_Exception_HTTP_400();
199 }
200 }
201 }
202
203 return $is_proxy;
204 }
205 }
206