| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\App\Modules\ImportExport\Processes; |
| 4 |
|
| 5 |
use Elementor\App\Modules\ImportExport\Module; |
| 6 |
use Elementor\App\Modules\ImportExport\Utils; |
| 7 |
use Elementor\Core\Utils\Str; |
| 8 |
use Elementor\Plugin; |
| 9 |
|
| 10 |
use Elementor\App\Modules\ImportExport\Runners\Export\Elementor_Content; |
| 11 |
use Elementor\App\Modules\ImportExport\Runners\Export\Export_Runner_Base; |
| 12 |
use Elementor\App\Modules\ImportExport\Runners\Export\Plugins; |
| 13 |
use Elementor\App\Modules\ImportExport\Runners\Export\Site_Settings; |
| 14 |
use Elementor\App\Modules\ImportExport\Runners\Export\Taxonomies; |
| 15 |
use Elementor\App\Modules\ImportExport\Runners\Export\Templates; |
| 16 |
use Elementor\App\Modules\ImportExport\Runners\Export\Wp_Content; |
| 17 |
|
| 18 |
class Export { |
| 19 |
const ZIP_ARCHIVE_MODULE_MISSING = 'zip-archive-module-is-missing'; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var Export_Runner_Base[] |
| 23 |
*/ |
| 24 |
protected $runners = []; |
| 25 |
|
| 26 |
/** |
| 27 |
* Selected content types to export. |
| 28 |
* |
| 29 |
* @var array |
| 30 |
*/ |
| 31 |
private $settings_include; |
| 32 |
|
| 33 |
/** |
| 34 |
* The kit information. (e.g: title, description) |
| 35 |
* |
| 36 |
* @var array $export_data |
| 37 |
*/ |
| 38 |
private $settings_kit_info; |
| 39 |
|
| 40 |
/** |
| 41 |
* Selected plugins to export. |
| 42 |
* Contains the plugins essential data for export. (e.g: name, path, version, etc.) |
| 43 |
* |
| 44 |
* @var array |
| 45 |
*/ |
| 46 |
private $settings_selected_plugins; |
| 47 |
|
| 48 |
/** |
| 49 |
* Selected custom post types to export. |
| 50 |
* |
| 51 |
* @var array |
| 52 |
*/ |
| 53 |
private $settings_selected_custom_post_types; |
| 54 |
|
| 55 |
/** |
| 56 |
* The output data of the export process. |
| 57 |
* Will be written into the manifest.json file. |
| 58 |
* |
| 59 |
* @var array |
| 60 |
*/ |
| 61 |
private $manifest_data; |
| 62 |
|
| 63 |
/** |
| 64 |
* The zip archive object. |
| 65 |
* |
| 66 |
* @var \ZipArchive |
| 67 |
*/ |
| 68 |
private $zip; |
| 69 |
|
| 70 |
public function __construct( $settings = [] ) { |
| 71 |
$this->settings_include = ! empty( $settings['include'] ) ? $settings['include'] : null; |
| 72 |
$this->settings_kit_info = ! empty( $settings['kitInfo'] ) ? $settings['kitInfo'] : null; |
| 73 |
$this->settings_selected_plugins = isset( $settings['plugins'] ) ? $settings['plugins'] : null; |
| 74 |
$this->settings_selected_custom_post_types = isset( $settings['selectedCustomPostTypes'] ) ? $settings['selectedCustomPostTypes'] : null; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Register a runner. |
| 79 |
* |
| 80 |
* @param Export_Runner_Base $runner_instance |
| 81 |
*/ |
| 82 |
public function register( Export_Runner_Base $runner_instance ) { |
| 83 |
$this->runners[ $runner_instance::get_name() ] = $runner_instance; |
| 84 |
} |
| 85 |
|
| 86 |
public function register_default_runners() { |
| 87 |
$this->register( new Site_Settings() ); |
| 88 |
$this->register( new Plugins() ); |
| 89 |
$this->register( new Templates() ); |
| 90 |
$this->register( new Taxonomies() ); |
| 91 |
$this->register( new Elementor_Content() ); |
| 92 |
$this->register( new Wp_Content() ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Execute the export process. |
| 97 |
* |
| 98 |
* @return array The export data output. |
| 99 |
* |
| 100 |
* @throws \Exception If no export runners have been specified. |
| 101 |
*/ |
| 102 |
public function run() { |
| 103 |
if ( empty( $this->runners ) ) { |
| 104 |
throw new \Exception( 'Couldn’t execute the export process because no export runners have been specified. Try again by specifying export runners.' ); |
| 105 |
} |
| 106 |
|
| 107 |
$this->set_default_settings(); |
| 108 |
|
| 109 |
$this->init_zip_archive(); |
| 110 |
$this->init_manifest_data(); |
| 111 |
|
| 112 |
$data = [ |
| 113 |
'include' => $this->settings_include, |
| 114 |
'selected_plugins' => $this->settings_selected_plugins, |
| 115 |
'selected_custom_post_types' => $this->settings_selected_custom_post_types, |
| 116 |
]; |
| 117 |
|
| 118 |
foreach ( $this->runners as $runner ) { |
| 119 |
if ( $runner->should_export( $data ) ) { |
| 120 |
$export_result = $runner->export( $data ); |
| 121 |
$this->handle_export_result( $export_result ); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
$this->add_json_file( 'manifest', $this->manifest_data ); |
| 126 |
|
| 127 |
$zip_file_name = $this->zip->filename; |
| 128 |
$this->zip->close(); |
| 129 |
|
| 130 |
return [ |
| 131 |
'manifest' => $this->manifest_data, |
| 132 |
'file_name' => $zip_file_name, |
| 133 |
]; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Set default settings for the export. |
| 138 |
*/ |
| 139 |
private function set_default_settings() { |
| 140 |
if ( ! is_array( $this->get_settings_include() ) ) { |
| 141 |
$this->settings_include( $this->get_default_settings_include() ); |
| 142 |
} |
| 143 |
|
| 144 |
if ( ! is_array( $this->get_settings_kit_info() ) ) { |
| 145 |
$this->settings_kit_info( $this->get_default_settings_kit_info() ); |
| 146 |
} |
| 147 |
|
| 148 |
if ( ! is_array( $this->get_settings_selected_custom_post_types() ) && in_array( 'content', $this->settings_include, true ) ) { |
| 149 |
$this->settings_selected_custom_post_types( $this->get_default_settings_custom_post_types() ); |
| 150 |
} |
| 151 |
|
| 152 |
if ( ! is_array( $this->get_settings_selected_plugins() ) && in_array( 'plugins', $this->settings_include, true ) ) { |
| 153 |
$this->settings_selected_plugins( $this->get_default_settings_selected_plugins() ); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
public function settings_include( $included_settings ) { |
| 158 |
$this->settings_include = $included_settings; |
| 159 |
} |
| 160 |
|
| 161 |
public function get_settings_include() { |
| 162 |
return $this->settings_include; |
| 163 |
} |
| 164 |
|
| 165 |
private function settings_kit_info( $kit_info ) { |
| 166 |
$this->settings_kit_info = $kit_info; |
| 167 |
} |
| 168 |
|
| 169 |
private function get_settings_kit_info() { |
| 170 |
return $this->settings_kit_info; |
| 171 |
} |
| 172 |
|
| 173 |
public function settings_selected_custom_post_types( $selected_custom_post_types ) { |
| 174 |
$this->settings_selected_custom_post_types = $selected_custom_post_types; |
| 175 |
} |
| 176 |
|
| 177 |
public function get_settings_selected_custom_post_types() { |
| 178 |
return $this->settings_selected_custom_post_types; |
| 179 |
} |
| 180 |
|
| 181 |
public function settings_selected_plugins( $plugins ) { |
| 182 |
$this->settings_selected_plugins = $plugins; |
| 183 |
} |
| 184 |
|
| 185 |
public function get_settings_selected_plugins() { |
| 186 |
return $this->settings_selected_plugins; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Get the default settings of which content types should be exported. |
| 191 |
* |
| 192 |
* @return array |
| 193 |
*/ |
| 194 |
private function get_default_settings_include() { |
| 195 |
return [ 'templates', 'content', 'settings', 'plugins' ]; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Get the default settings of the kit info. |
| 200 |
* |
| 201 |
* @return array |
| 202 |
*/ |
| 203 |
private function get_default_settings_kit_info() { |
| 204 |
return [ |
| 205 |
'title' => 'kit', |
| 206 |
'description' => '', |
| 207 |
]; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Get the default settings of the plugins that should be exported. |
| 212 |
* |
| 213 |
* @return array{name: string, plugin:string, pluginUri: string, version: string} |
| 214 |
*/ |
| 215 |
private function get_default_settings_selected_plugins() { |
| 216 |
$installed_plugins = Plugin::$instance->wp->get_plugins(); |
| 217 |
|
| 218 |
return $installed_plugins->map( function ( $item, $key ) { |
| 219 |
return [ |
| 220 |
'name' => $item['Name'], |
| 221 |
'plugin' => $key, |
| 222 |
'pluginUri' => $item['PluginURI'], |
| 223 |
'version' => $item['Version'], |
| 224 |
]; |
| 225 |
} )->all(); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Get the default settings of all the custom post types that should be exported. |
| 230 |
* Should be all the custom post types that are not built in to WordPress and not part of Elementor. |
| 231 |
* |
| 232 |
* @return array |
| 233 |
*/ |
| 234 |
private function get_default_settings_custom_post_types() { |
| 235 |
return Utils::get_registered_cpt_names(); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Init the zip archive. |
| 240 |
* |
| 241 |
* @throws \Error If export process fails, file creation errors occur, or data serialization fails. |
| 242 |
*/ |
| 243 |
private function init_zip_archive() { |
| 244 |
if ( ! class_exists( '\ZipArchive' ) ) { |
| 245 |
throw new \Error( static::ZIP_ARCHIVE_MODULE_MISSING ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 246 |
} |
| 247 |
|
| 248 |
$zip = new \ZipArchive(); |
| 249 |
|
| 250 |
$temp_dir = Plugin::$instance->uploads_manager->create_unique_dir(); |
| 251 |
|
| 252 |
$zip_file_name = $temp_dir . sanitize_title( $this->settings_kit_info['title'] ) . '.zip'; |
| 253 |
|
| 254 |
$zip->open( $zip_file_name, \ZipArchive::CREATE | \ZipArchive::OVERWRITE ); |
| 255 |
|
| 256 |
$this->zip = $zip; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Init the manifest data and add some basic info to it. |
| 261 |
*/ |
| 262 |
private function init_manifest_data() { |
| 263 |
$kit_post = Plugin::$instance->kits_manager->get_active_kit()->get_post(); |
| 264 |
|
| 265 |
$manifest_data = [ |
| 266 |
'name' => sanitize_title( $this->settings_kit_info['title'] ), |
| 267 |
'title' => $this->settings_kit_info['title'], |
| 268 |
'description' => $this->settings_kit_info['description'], |
| 269 |
'author' => get_the_author_meta( 'display_name', $kit_post->post_author ), |
| 270 |
'version' => Module::FORMAT_VERSION, |
| 271 |
'elementor_version' => ELEMENTOR_VERSION, |
| 272 |
'created' => gmdate( 'Y-m-d H:i:s' ), |
| 273 |
'thumbnail' => get_the_post_thumbnail_url( $kit_post ), |
| 274 |
'site' => get_site_url(), |
| 275 |
]; |
| 276 |
|
| 277 |
$this->manifest_data = $manifest_data; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Handle the export process output. |
| 282 |
* Add the manifest data from the runner to the manifest.json file. |
| 283 |
* Create files according to the files array that should be exported by the runner. |
| 284 |
* |
| 285 |
* @param array $export_result |
| 286 |
*/ |
| 287 |
private function handle_export_result( $export_result ) { |
| 288 |
foreach ( $export_result['manifest'] as $data ) { |
| 289 |
$this->manifest_data += $data; |
| 290 |
} |
| 291 |
|
| 292 |
if ( isset( $export_result['files']['path'] ) ) { |
| 293 |
$export_result['files'] = [ $export_result['files'] ]; |
| 294 |
} |
| 295 |
|
| 296 |
foreach ( $export_result['files'] as $file ) { |
| 297 |
$file_extension = pathinfo( $file['path'], PATHINFO_EXTENSION ); |
| 298 |
if ( empty( $file_extension ) ) { |
| 299 |
$this->add_json_file( |
| 300 |
$file['path'], |
| 301 |
$file['data'] |
| 302 |
); |
| 303 |
} else { |
| 304 |
$this->add_file( |
| 305 |
$file['path'], |
| 306 |
$file['data'] |
| 307 |
); |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Add json file to the zip archive. |
| 314 |
* |
| 315 |
* @param string $path The relative path to the file. |
| 316 |
* @param array $content The content of the file. |
| 317 |
* @param int $json_flags |
| 318 |
*/ |
| 319 |
private function add_json_file( $path, array $content, $json_flags = 0 ) { |
| 320 |
if ( ! Str::ends_with( $path, '.json' ) ) { |
| 321 |
$path .= '.json'; |
| 322 |
} |
| 323 |
|
| 324 |
$this->add_file( $path, wp_json_encode( $content, $json_flags ) ); |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Add file to the zip archive. |
| 329 |
* |
| 330 |
* @param string $file |
| 331 |
* @param string $content The content of the file. |
| 332 |
*/ |
| 333 |
private function add_file( $file, $content ) { |
| 334 |
$this->zip->addFromString( $file, $content ); |
| 335 |
} |
| 336 |
} |
| 337 |
|