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