| 1 |
<?php |
| 2 |
namespace Elementor\Modules\CloudKitLibrary; |
| 3 |
|
| 4 |
use Elementor\Modules\CloudKitLibrary\Data\Controller as Cloud_Kits_Controller; |
| 5 |
use Elementor\Core\Utils\Exceptions; |
| 6 |
use Elementor\Plugin; |
| 7 |
use Elementor\Core\Base\Module as BaseModule; |
| 8 |
use Elementor\Modules\CloudKitLibrary\Connect\Cloud_Kits; |
| 9 |
use Elementor\Core\Common\Modules\Connect\Module as ConnectModule; |
| 10 |
use Elementor\App\Modules\ImportExportCustomization\Module as ImportExportCustomization_Module; |
| 11 |
use Elementor\App\Modules\KitLibrary\Connect\Kit_Library as Kit_Library_Api; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; // Exit if accessed directly. |
| 15 |
} |
| 16 |
|
| 17 |
class Module extends BaseModule { |
| 18 |
|
| 19 |
public function get_name(): string { |
| 20 |
return 'cloud-kit-library'; |
| 21 |
} |
| 22 |
|
| 23 |
public function __construct() { |
| 24 |
parent::__construct(); |
| 25 |
|
| 26 |
add_action( 'elementor/connect/apps/register', function ( ConnectModule $connect_module ) { |
| 27 |
$connect_module->register_app( 'cloud-kits', Cloud_Kits::get_class_name() ); |
| 28 |
} ); |
| 29 |
|
| 30 |
add_filter( 'elementor/export/kit/export-result', [ $this, 'handle_export_kit_result' ], 10, 6 ); |
| 31 |
add_filter( 'elementor/import/kit/result/cloud', [ $this, 'handle_import_kit_from_cloud' ], 10, 1 ); |
| 32 |
add_filter( 'elementor/import/kit_thumbnail', [ $this, 'handle_import_kit_thumbnail' ], 10, 3 ); |
| 33 |
|
| 34 |
add_action( 'elementor/kit_library/registered', function () { |
| 35 |
Plugin::$instance->data_manager_v2->register_controller( new Cloud_Kits_Controller() ); |
| 36 |
} ); |
| 37 |
} |
| 38 |
|
| 39 |
public function handle_import_kit_thumbnail( $thumbnail, $kit_id, $referrer ) { |
| 40 |
if ( ImportExportCustomization_Module::REFERRER_KIT_LIBRARY === $referrer ) { |
| 41 |
|
| 42 |
if ( empty( $kit_id ) ) { |
| 43 |
return ''; |
| 44 |
} |
| 45 |
|
| 46 |
$api = new Kit_Library_Api(); |
| 47 |
$kit = $api->get_by_id( $kit_id ); |
| 48 |
|
| 49 |
if ( is_wp_error( $kit ) ) { |
| 50 |
return ''; |
| 51 |
} |
| 52 |
|
| 53 |
return $kit->thumbnail; |
| 54 |
} |
| 55 |
|
| 56 |
if ( ImportExportCustomization_Module::REFERRER_CLOUD === $referrer ) { |
| 57 |
if ( empty( $kit_id ) ) { |
| 58 |
return ''; |
| 59 |
} |
| 60 |
|
| 61 |
$kit = self::get_app()->get_kit( [ 'id' => $kit_id ] ); |
| 62 |
|
| 63 |
if ( is_wp_error( $kit ) ) { |
| 64 |
return ''; |
| 65 |
} |
| 66 |
|
| 67 |
return $kit['thumbnailUrl'] ?? ''; |
| 68 |
} |
| 69 |
|
| 70 |
return $thumbnail; |
| 71 |
} |
| 72 |
|
| 73 |
public function handle_export_kit_result( $result, $source, $export, $settings, $file, $file_size ) { |
| 74 |
if ( ImportExportCustomization_Module::EXPORT_SOURCE_CLOUD !== $source ) { |
| 75 |
return $result; |
| 76 |
} |
| 77 |
|
| 78 |
unset( $result['file'] ); |
| 79 |
|
| 80 |
$raw_screen_shot = base64_decode( substr( $settings['screenShotBlob'], strlen( 'data:image/png;base64,' ) ) ); |
| 81 |
$title = $export['manifest']['title']; |
| 82 |
$description = $export['manifest']['description']; |
| 83 |
|
| 84 |
$kit = self::get_app()->create_kit( |
| 85 |
$title, |
| 86 |
$description, |
| 87 |
$file, |
| 88 |
$raw_screen_shot, |
| 89 |
$settings['include'], |
| 90 |
$settings['customization']['content']['mediaFormat'] ?? 'link', |
| 91 |
$file_size, |
| 92 |
); |
| 93 |
|
| 94 |
if ( is_wp_error( $kit ) ) { |
| 95 |
return $kit; |
| 96 |
} |
| 97 |
|
| 98 |
$result['kit'] = $kit; |
| 99 |
|
| 100 |
return $result; |
| 101 |
} |
| 102 |
|
| 103 |
public function handle_import_kit_from_cloud( $args ) { |
| 104 |
$kit = self::get_app()->get_kit( [ |
| 105 |
'id' => $args['kit_id'], |
| 106 |
] ); |
| 107 |
|
| 108 |
if ( is_wp_error( $kit ) ) { |
| 109 |
throw new \Error( ImportExportCustomization_Module::CLOUD_KIT_LIBRARY_ERROR_LOADING_RESOURCE ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 110 |
} |
| 111 |
|
| 112 |
if ( empty( $kit['downloadUrl'] ) ) { |
| 113 |
throw new \Error( ImportExportCustomization_Module::KIT_LIBRARY_ERROR_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 114 |
} |
| 115 |
|
| 116 |
$data = [ |
| 117 |
'file_name' => self::get_remote_kit_zip( $kit['downloadUrl'] ), |
| 118 |
'referrer' => ImportExportCustomization_Module::REFERRER_CLOUD, |
| 119 |
'file_url' => $kit['downloadUrl'], |
| 120 |
'kit' => $kit, |
| 121 |
]; |
| 122 |
|
| 123 |
if ( ! empty( $kit['mediaDownloadUrl'] ) ) { |
| 124 |
$media_zip = self::get_remote_kit_zip( $kit['mediaDownloadUrl'], 'media.zip' ); |
| 125 |
$data['media_file_name'] = $media_zip; |
| 126 |
} |
| 127 |
|
| 128 |
return $data; |
| 129 |
} |
| 130 |
|
| 131 |
public static function get_remote_kit_zip( $url, $file_name = 'kit.zip' ) { |
| 132 |
$remote_zip_request = wp_safe_remote_get( $url, [ |
| 133 |
'timeout' => 300, |
| 134 |
] ); |
| 135 |
|
| 136 |
if ( is_wp_error( $remote_zip_request ) ) { |
| 137 |
Plugin::$instance->logger->get_logger()->error( $remote_zip_request->get_error_message() ); |
| 138 |
throw new \Error( ImportExportCustomization_Module::CLOUD_KIT_LIBRARY_ERROR_LOADING_RESOURCE ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 139 |
} |
| 140 |
|
| 141 |
if ( 200 !== $remote_zip_request['response']['code'] ) { |
| 142 |
Plugin::$instance->logger->get_logger()->error( $remote_zip_request['response']['message'] ); |
| 143 |
throw new \Error( ImportExportCustomization_Module::CLOUD_KIT_LIBRARY_ERROR_LOADING_RESOURCE ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 144 |
} |
| 145 |
|
| 146 |
return Plugin::$instance->uploads_manager->create_temp_file( $remote_zip_request['body'], $file_name ); |
| 147 |
} |
| 148 |
|
| 149 |
public static function get_app(): Cloud_Kits { |
| 150 |
$cloud_kits_app = Plugin::$instance->common->get_component( 'connect' )->get_app( 'cloud-kits' ); |
| 151 |
|
| 152 |
if ( ! $cloud_kits_app ) { |
| 153 |
$error_message = esc_html__( 'Cloud-Kits is not instantiated.', 'elementor' ); |
| 154 |
|
| 155 |
throw new \Exception( $error_message, Exceptions::FORBIDDEN ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 156 |
} |
| 157 |
|
| 158 |
return $cloud_kits_app; |
| 159 |
} |
| 160 |
} |
| 161 |
|