| @@ -27,31 +27,50 @@ | ||
| 27 | 27 | |
| 28 | 28 | public function __construct() { |
| 29 | 29 | parent::__construct(); |
| 30 | 30 | |
| 31 | - $this->register_app(); | |
| 31 | + $this->register_experiments(); | |
| 32 | 32 | |
| 33 | - add_action( 'elementor/init', function () { | |
| 34 | - $this->set_cloud_library_settings(); | |
| 35 | - }, 12 /** After the initiation of the connect cloud library */ ); | |
| 33 | + if ( Plugin::$instance->experiments->is_feature_active( $this->get_name() ) ) { | |
| 34 | + $this->register_app(); | |
| 36 | 35 | |
| 37 | - add_filter( 'elementor/editor/localize_settings', function ( $settings ) { | |
| 38 | - return $this->localize_settings( $settings ); | |
| 39 | - }, 11 /** After Elementor Core */ ); | |
| 36 | + add_action( 'elementor/init', function () { | |
| 37 | + $this->set_cloud_library_settings(); | |
| 38 | + }, 12 /** After the initiation of the connect cloud library */ ); | |
| 40 | 39 | |
| 41 | - add_filter( 'elementor/render_mode/module', function( $module_name ) { | |
| 42 | - $render_mode_manager = \Elementor\Plugin::$instance->frontend->render_mode_manager; | |
| 40 | + add_filter( 'elementor/editor/localize_settings', function ( $settings ) { | |
| 41 | + return $this->localize_settings( $settings ); | |
| 42 | + }, 11 /** After Elementor Core */ ); | |
| 43 | 43 | |
| 44 | - if ( $render_mode_manager ) { | |
| 45 | - $current_render_mode = $render_mode_manager->get_current(); | |
| 44 | + add_filter( 'elementor/render_mode/module', function( $module_name ) { | |
| 45 | + $render_mode_manager = \Elementor\Plugin::$instance->frontend->render_mode_manager; | |
| 46 | 46 | |
| 47 | - if ( $current_render_mode instanceof \Elementor\Modules\CloudLibrary\Render_Mode_Preview ) { | |
| 47 | + if ( $render_mode_manager && $render_mode_manager->get_current() instanceof \Elementor\Modules\CloudLibrary\Render_Mode_Preview ) { | |
| 48 | 48 | return 'cloud-library'; |
| 49 | 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; | |
| 50 | 57 | } |
| 58 | + } | |
| 59 | + } | |
| 51 | 60 | |
| 52 | - return $module_name; | |
| 53 | - }, 12); | |
| 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 ); | |
| 54 | 73 | } |
| 55 | 74 | |
| 56 | 75 | public function localize_settings( $settings ) { |
| 57 | 76 | if ( isset( $settings['i18n'] ) ) { |
| @@ -62,8 +81,18 @@ | ||
| 62 | 81 | |
| 63 | 82 | return $settings; |
| 64 | 83 | } |
| 65 | 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 | + | |
| 66 | 95 | private function register_app() { |
| 67 | 96 | add_action( 'elementor/connect/apps/register', function ( ConnectModule $connect_module ) { |
| 68 | 97 | $connect_module->register_app( 'cloud-library', Cloud_Library::get_class_name() ); |
| 69 | 98 | } ); |
| @@ -80,9 +109,9 @@ | ||
| 80 | 109 | |
| 81 | 110 | /** |
| 82 | 111 | * @param Render_Mode_Manager $manager |
| 83 | 112 | * |
| 84 | - * @throws \Exception If render mode registration fails. | |
| 113 | + * @throws \Exception | |
| 85 | 114 | */ |
| 86 | 115 | public function register_render_mode( Render_Mode_Manager $manager ) { |
| 87 | 116 | $manager->register_render_mode( Render_Mode_Preview::class ); |
| 88 | 117 | } |
| @@ -140,23 +169,37 @@ | ||
| 140 | 169 | |
| 141 | 170 | private function print_thumbnail_preview_callback() { |
| 142 | 171 | $doc = Plugin::$instance->documents->get_current(); |
| 143 | 172 | |
| 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(); | |
| 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 | + } | |
| 148 | 191 | } |
| 149 | - } | |
| 150 | 192 | |
| 151 | - if ( ! $doc ) { | |
| 152 | - echo '<div class="elementor-alert elementor-alert-danger">' . esc_html__( 'Document not found for preview.', 'elementor' ) . '</div>'; | |
| 153 | - return; | |
| 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 | + } | |
| 154 | 201 | } |
| 155 | 202 | |
| 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 | |
| 203 | + return $is_proxy; | |
| 161 | 204 | } |
| 162 | 205 | } |