PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.5
Elementor Website Builder – more than just a page builder v3.32.5
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.32.5, 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_experiments();
32
33 $this->register_app();
34
35 add_action( 'elementor/init', function () {
36 $this->set_cloud_library_settings();
37 }, 12 /** After the initiation of the connect cloud library */ );
38
39 add_filter( 'elementor/editor/localize_settings', function ( $settings ) {
40 return $this->localize_settings( $settings );
41 }, 11 /** After Elementor Core */ );
42
43 add_filter( 'elementor/render_mode/module', function( $module_name ) {
44 $render_mode_manager = \Elementor\Plugin::$instance->frontend->render_mode_manager;
45
46 if ( $render_mode_manager && $render_mode_manager->get_current() instanceof \Elementor\Modules\CloudLibrary\Render_Mode_Preview ) {
47 return 'cloud-library';
48 }
49
50 return $module_name;
51 }, 12);
52
53 if ( $this->is_screenshot_proxy_mode( $_GET ) ) { // phpcs:ignore -- Checking nonce inside the method.
54 echo $this->get_proxy_data( htmlspecialchars( $_GET['href'] ) ); // phpcs:ignore -- Nonce was checked on the above method
55 die;
56 }
57 }
58
59 public function get_proxy_data( $url ) {
60 $response = wp_safe_remote_get( utf8_decode( $url ) );
61
62 if ( is_wp_error( $response ) ) {
63 return '';
64 }
65
66 $content_type = wp_remote_retrieve_headers( $response )->offsetGet( 'content-type' );
67
68 header( 'content-type: ' . $content_type );
69
70 return wp_remote_retrieve_body( $response );
71 }
72
73 public function localize_settings( $settings ) {
74 if ( isset( $settings['i18n'] ) ) {
75 $settings['i18n']['folder'] = esc_html__( 'Folder', 'elementor' );
76 }
77
78 $settings['library']['doc_types'] = $this->get_document_types();
79
80 return $settings;
81 }
82
83 private function register_experiments() {
84 Plugin::$instance->experiments->add_feature( [
85 'name' => $this->get_name(),
86 'title' => esc_html__( 'Cloud Library', 'elementor' ),
87 'release_status' => ExperimentsManager::RELEASE_STATUS_STABLE,
88 'default' => ExperimentsManager::STATE_ACTIVE,
89 'hidden' => true,
90 'mutable' => false,
91 'new_site' => [
92 'always_active' => true,
93 'minimum_installation_version' => '3.32.0',
94 ],
95 ] );
96 }
97
98 private function register_app() {
99 add_action( 'elementor/connect/apps/register', function ( ConnectModule $connect_module ) {
100 $connect_module->register_app( 'cloud-library', Cloud_Library::get_class_name() );
101 } );
102
103 add_action( 'elementor/frontend/render_mode/register', [ $this, 'register_render_mode' ] );
104
105 add_action( 'elementor/documents/register', function ( Documents_Manager $documents_manager ) {
106 $documents_manager->register_document_type(
107 Documents\Cloud_Template_Preview::TYPE,
108 Documents\Cloud_Template_Preview::get_class_full_name()
109 );
110 });
111 }
112
113 /**
114 * @param Render_Mode_Manager $manager
115 *
116 * @throws \Exception
117 */
118 public function register_render_mode( Render_Mode_Manager $manager ) {
119 $manager->register_render_mode( Render_Mode_Preview::class );
120 }
121
122 private function set_cloud_library_settings() {
123 if ( ! Plugin::$instance->common ) {
124 return;
125 }
126
127 /** @var ConnectModule $connect */
128 $connect = Plugin::$instance->common->get_component( 'connect' );
129
130 /** @var Library $library */
131 $library = $connect->get_app( 'library' );
132
133 if ( ! $library ) {
134 return;
135 }
136
137 Plugin::$instance->app->set_settings( 'cloud-library', [
138 'library_connect_url' => esc_url( $library->get_admin_url( 'authorize', [
139 'utm_source' => 'template-library',
140 'utm_medium' => 'wp-dash',
141 'utm_campaign' => 'library-connect',
142 'utm_content' => 'cloud-library',
143 'source' => 'cloud-library',
144 ] ) ),
145 'library_connect_title_copy' => esc_html__( 'Connect to your Elementor account', 'elementor' ),
146 'library_connect_sub_title_copy' => esc_html__( 'Then you can find all your templates in one convenient library.', 'elementor' ),
147 'library_connect_button_copy' => esc_html__( 'Connect', 'elementor' ),
148 ] );
149 }
150
151 private function get_document_types() {
152 $document_types = Plugin::$instance->documents->get_document_types( [
153 'show_in_library' => true,
154 ] );
155
156 $data = [];
157
158 foreach ( $document_types as $name => $document_type ) {
159 $data[ $name ] = $document_type::get_title();
160 }
161
162 return $data;
163 }
164
165 public function print_content() {
166 if ( ! $this->print_preview_callback ) {
167 $this->print_preview_callback = [ $this, 'print_thumbnail_preview_callback' ];
168 }
169
170 call_user_func( $this->print_preview_callback );
171 }
172
173 private function print_thumbnail_preview_callback() {
174 $doc = Plugin::$instance->documents->get_current();
175
176 // PHPCS - should not be escaped.
177 echo Plugin::$instance->frontend->get_builder_content_for_display( $doc->get_main_id(), true ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
178
179 wp_delete_post( $doc->get_main_id(), true );
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