TemplateExport.php
728 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * FormValidator class |
| 5 | * |
| 6 | * This class is responsible for validating form data |
| 7 | * |
| 8 | * @package kirki |
| 9 | */ |
| 10 | |
| 11 | namespace Kirki\ExportImport; |
| 12 | |
| 13 | use Kirki\Ajax\UserData; |
| 14 | use Kirki\API\ContentManager\ContentManagerHelper; |
| 15 | use Kirki\HelperFunctions; |
| 16 | use Kirki\Staging; |
| 17 | |
| 18 | if ( ! defined( 'ABSPATH' ) ) { |
| 19 | exit; // Exit if accessed directly. |
| 20 | } |
| 21 | |
| 22 | class TemplateExport { |
| 23 | |
| 24 | |
| 25 | private $discarded_meta = array( '_edit_lock', '_edit_last' ); |
| 26 | public $asset_urls = array(); |
| 27 | private $asset_urls_tracker = array(); |
| 28 | private $only_published_pages = true; |
| 29 | |
| 30 | public function __construct() { |
| 31 | |
| 32 | } |
| 33 | |
| 34 | public function export( $is_assets_with, $only_published_pages = true ) { |
| 35 | $environment = array( |
| 36 | 'asset_urls_tracker' => $this->asset_urls_tracker, |
| 37 | 'asset_urls' => $this->asset_urls, |
| 38 | 'template_data' => array( 'exportId' => uniqid( '', true ) ), |
| 39 | 'only_published_pages' => $only_published_pages, |
| 40 | 'queue' => array( |
| 41 | 'pages_symbols_popups_templates_utility_pages', |
| 42 | 'viewPorts_customFonts', |
| 43 | 'globalStyleBlocks_variables', |
| 44 | 'contentManager', |
| 45 | 'contentManagerRefFields', |
| 46 | 'siteInfo', |
| 47 | ), |
| 48 | ); |
| 49 | |
| 50 | if ( $is_assets_with ) { |
| 51 | $environment['queue'][] = 'assetUrls'; |
| 52 | } |
| 53 | |
| 54 | HelperFunctions::update_global_data_using_key( 'kirki_project_export', $environment ); |
| 55 | return array( |
| 56 | 'status' => 'init', |
| 57 | 'queue' => $environment['queue'], |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | public function process() { |
| 62 | $environment = HelperFunctions::get_global_data_using_key( 'kirki_project_export' ); |
| 63 | if ( ! $environment ) { |
| 64 | return array( |
| 65 | 'status' => false, |
| 66 | 'message' => 'Nothing found to export', |
| 67 | ); |
| 68 | } |
| 69 | $site_name = get_bloginfo( 'name' ); |
| 70 | $template_data = $environment['template_data']; |
| 71 | $template_data['variable_prefix'] = $site_name; |
| 72 | $this->only_published_pages = $environment['only_published_pages']; |
| 73 | |
| 74 | $formattedString = preg_replace( '/[^a-zA-Z ]/', '', $site_name ); // Remove numbers & special symbols |
| 75 | $formattedString = str_replace( ' ', '_', strtolower( $formattedString ) ); |
| 76 | $template_data['batch_id'] = $formattedString; |
| 77 | $template_data['prefix'] = $formattedString; |
| 78 | |
| 79 | $queue = $environment['queue']; |
| 80 | if ( count( $queue ) > 0 ) { |
| 81 | $this->asset_urls_tracker = $environment['asset_urls_tracker']; |
| 82 | $this->asset_urls = $environment['asset_urls']; |
| 83 | |
| 84 | $current_task = array_shift( $queue ); |
| 85 | switch ( $current_task ) { |
| 86 | case 'pages_symbols_popups_templates_utility_pages':{ |
| 87 | $template_data['pages'] = $this->get_all_pages(); |
| 88 | $template_data['symbols'] = $this->get_all_symbols(); |
| 89 | $template_data['popups'] = $this->get_all_popups(); |
| 90 | $template_data['templates'] = $this->get_all_templates(); |
| 91 | $template_data['utility_pages'] = $this->get_all_utility_pages(); |
| 92 | break; |
| 93 | } |
| 94 | case 'viewPorts_customFonts':{ |
| 95 | $template_data['viewPorts'] = $this->get_view_ports(); |
| 96 | $template_data['customFonts'] = $this->get_custom_fonts(); |
| 97 | break; |
| 98 | } |
| 99 | case 'globalStyleBlocks_variables':{ |
| 100 | $template_data['globalStyleBlocks'] = $this->get_global_style_blocks(); |
| 101 | $template_data['variables'] = UserData::get_kirki_variable_data(); |
| 102 | break; |
| 103 | } |
| 104 | case 'contentManager': { |
| 105 | $template_data['contentManager'] = $this->get_all_content_manager_data(); |
| 106 | break; |
| 107 | } |
| 108 | case 'contentManagerRefFields': { |
| 109 | $template_data['contentManagerRefFields'] = $this->get_all_content_manager_ref_fields(); |
| 110 | break; |
| 111 | } |
| 112 | case 'assetUrls':{ |
| 113 | $this->extract_asset_url_from_template_data( $template_data ); |
| 114 | $template_data['assetUrls'] = $this->asset_urls_tracker; |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | case 'siteInfo':{ |
| 119 | $template_data['siteInfo'] = array( |
| 120 | 'siteName' => get_bloginfo( 'name' ), |
| 121 | 'siteUrl' => get_bloginfo( 'url' ), |
| 122 | 'siteDescription' => get_bloginfo( 'description' ), |
| 123 | 'siteLogo' => get_site_icon_url(), |
| 124 | 'page_on_front' => get_option( 'page_on_front' ), |
| 125 | 'show_on_front' => get_option( 'show_on_front' ), |
| 126 | ); |
| 127 | break; |
| 128 | } |
| 129 | default: |
| 130 | // code... |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | $environment['queue'] = $queue; |
| 135 | $environment['asset_urls_tracker'] = $this->asset_urls_tracker; |
| 136 | $environment['asset_urls'] = $this->asset_urls; |
| 137 | $environment['template_data'] = $template_data; |
| 138 | HelperFunctions::update_global_data_using_key( 'kirki_project_export', $environment ); |
| 139 | return array( |
| 140 | 'status' => 'exporting', |
| 141 | 'queue' => $queue, |
| 142 | 'done' => $current_task, |
| 143 | ); |
| 144 | } else { |
| 145 | HelperFunctions::update_global_data_using_key( 'kirki_project_export', false ); |
| 146 | $zip_file_url = $this->export_and_get_url( $template_data ); |
| 147 | return array( |
| 148 | 'status' => 'done', |
| 149 | 'queue' => $queue, |
| 150 | 'zip_file_url' => $zip_file_url, |
| 151 | ); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private function export_and_get_url( $template_data ) { |
| 156 | // creating a zip file name for template export. |
| 157 | $site_name = get_bloginfo( 'name' ); |
| 158 | // Check if $site_name is empty or not found |
| 159 | if ( empty( $site_name ) ) { |
| 160 | $site_name = 'kirki-template'; // Fallback name |
| 161 | } |
| 162 | // Convert the site name to a slug format |
| 163 | $zip_filename = strtolower( str_replace( ' ', '-', $site_name ) ); |
| 164 | $zip_file_url = $this->make_zip_and_get_url( $zip_filename, $template_data ); |
| 165 | |
| 166 | return $zip_file_url; |
| 167 | } |
| 168 | private function extract_asset_url_from_template_data( $template_data ) { |
| 169 | $this->extract_asset_url_from_content_manager( $template_data['contentManager'] ); |
| 170 | $this->extract_asset_url_from_pages( $template_data['pages'] ); |
| 171 | $this->extract_asset_url_from_template( $template_data['templates'] ); |
| 172 | $this->extract_asset_url_from_popups( $template_data['popups'] ); |
| 173 | $this->extract_asset_url_from_symbols( $template_data['symbols'] ); |
| 174 | $this->extract_asset_url_from_string( json_encode( $template_data['globalStyleBlocks'] ) ); |
| 175 | } |
| 176 | |
| 177 | private function extract_asset_url_from_string( $string ) { |
| 178 | if ( ! $string ) { |
| 179 | return; |
| 180 | } |
| 181 | $string = stripslashes( $string ); |
| 182 | // Define a regular expression pattern to match any image URLs |
| 183 | $pattern = '/https?:\/\/[^\s]+\/[^\s]+\.(jpg|jpeg|png|gif|svg|webp)/i'; |
| 184 | // Find all image URLs matching the pattern in the string |
| 185 | preg_match_all( $pattern, $string, $matches ); |
| 186 | // Output the found image URLs |
| 187 | $image_urls = $matches[0]; |
| 188 | foreach ( $image_urls as $key => $url ) { |
| 189 | $this->push_to_asset_urls_tracker( false, $url ); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | private function extract_asset_url_from_content_manager( $contentManager ) { |
| 194 | $types = array( 'image', 'video', 'gallery' ); |
| 195 | |
| 196 | foreach ( $contentManager as $key => $collection ) { |
| 197 | $asset_field_ids = array(); // [{type: 'image', id: 'kirki_cm_field_952_dp23nqfx'}] |
| 198 | $meta = $collection->meta; |
| 199 | |
| 200 | if ( isset( $meta['kirki_cm_fields'] ) && ! empty( $meta['kirki_cm_fields'] ) ) { |
| 201 | $fields = maybe_unserialize( $meta['kirki_cm_fields'][0] ); |
| 202 | |
| 203 | // kirki_cm_field_952_dp23nqfx |
| 204 | if ( is_array( $fields ) && ! empty( $fields ) ) { |
| 205 | foreach ( $fields as $field ) { |
| 206 | if ( isset( $field['type'] ) && in_array( $field['type'], $types ) ) { |
| 207 | $asset_field_ids[] = array( |
| 208 | 'type' => $field['type'], |
| 209 | 'id' => 'kirki_cm_field_' . $collection->ID . '_' . $field['id'], |
| 210 | ); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | if ( count( $collection->children ) > 0 ) { |
| 217 | foreach ( $collection->children as $collection_data ) { |
| 218 | $meta = $collection_data->meta; |
| 219 | |
| 220 | foreach ( $asset_field_ids as $asset_field ) { |
| 221 | if ( isset( $meta[ $asset_field['id'] ] ) && ! empty( $meta[ $asset_field['id'] ] ) ) { |
| 222 | $asset = maybe_unserialize( $meta[ $asset_field['id'] ][0] ); |
| 223 | $this->track_asset_and_push_to_tracker( $asset, $asset_field['type'] ); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | private function track_asset_and_push_to_tracker( $asset, $type = 'image' ) { |
| 232 | if ( $type === 'gallery' && is_array( $asset ) ) { |
| 233 | foreach ( $asset as $item ) { |
| 234 | if ( ! empty( $item ) && isset( $item['id'], $item['url'] ) ) { |
| 235 | $this->push_to_asset_urls_tracker( $item['id'], $item['url'] ); |
| 236 | } |
| 237 | } |
| 238 | } elseif ( ! empty( $asset ) && isset( $asset['id'], $asset['url'] ) ) { |
| 239 | $this->push_to_asset_urls_tracker( $asset['id'], $asset['url'] ); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | private function extract_asset_url_from_pages( $pages ) { |
| 244 | foreach ( $pages as $page ) { |
| 245 | $meta = $page->meta; |
| 246 | if ( isset( $meta['kirki'] ) && ! empty( $meta['kirki'] ) ) { |
| 247 | $kirki_data = maybe_unserialize( $meta['kirki'][0] ); |
| 248 | |
| 249 | if ( is_array( $kirki_data ) && ! empty( $kirki_data ) ) { |
| 250 | $this->exact_asset_url_from_blocks( $kirki_data['blocks'], 'pages' ); |
| 251 | } |
| 252 | } |
| 253 | if ( ! empty( $meta['kirki_global_style_block_random'] ) ) { |
| 254 | $kirki_global_style_block_random = maybe_unserialize( $meta['kirki_global_style_block_random'][0] ); |
| 255 | $this->extract_asset_url_from_string( json_encode( $kirki_global_style_block_random ) ); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | private function extract_asset_url_from_template( $templates ) { |
| 261 | foreach ( $templates as $template ) { |
| 262 | $meta = $template->meta; |
| 263 | if ( isset( $meta['kirki'] ) && ! empty( $meta['kirki'] ) ) { |
| 264 | $kirki_data = maybe_unserialize( $meta['kirki'][0] ); |
| 265 | |
| 266 | if ( is_array( $kirki_data ) && ! empty( $kirki_data ) ) { |
| 267 | $this->exact_asset_url_from_blocks( $kirki_data['blocks'], 'templates' ); |
| 268 | } |
| 269 | } |
| 270 | if ( ! empty( $meta['kirki_global_style_block_random'] ) ) { |
| 271 | $kirki_global_style_block_random = maybe_unserialize( $meta['kirki_global_style_block_random'][0] ); |
| 272 | $this->extract_asset_url_from_string( json_encode( $kirki_global_style_block_random ) ); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | private function extract_asset_url_from_popups( $popups ) { |
| 278 | foreach ( $popups as $popup ) { |
| 279 | $meta = $popup->meta; |
| 280 | |
| 281 | if ( isset( $meta['kirki'] ) && ! empty( $meta['kirki'] ) ) { |
| 282 | $kirki_data = maybe_unserialize( $meta['kirki'][0] ); |
| 283 | if ( is_array( $kirki_data ) && ! empty( $kirki_data ) ) { |
| 284 | $this->exact_asset_url_from_blocks( $kirki_data, 'popups' ); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | private function extract_asset_url_from_symbols( $symbols ) { |
| 291 | foreach ( $symbols as $symbol ) { |
| 292 | |
| 293 | $meta = $symbol->meta; |
| 294 | if ( isset( $meta['kirki'] ) && ! empty( $meta['kirki'] ) ) { |
| 295 | $kirki_data = maybe_unserialize( $meta['kirki'][0] ); |
| 296 | |
| 297 | if ( is_array( $kirki_data ) && ! empty( $kirki_data ) ) { |
| 298 | $this->exact_asset_url_from_blocks( $kirki_data['data'], 'symbols' ); |
| 299 | $this->extract_asset_url_from_string( json_encode( $kirki_data['styleBlocks'] ) ); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | |
| 306 | |
| 307 | private function exact_asset_url_from_blocks( $blocks, $context ) { |
| 308 | $upload_dir = wp_upload_dir(); |
| 309 | $base_url = $upload_dir['baseurl']; |
| 310 | |
| 311 | // $asset_urls = []; |
| 312 | |
| 313 | foreach ( $blocks as $key => $block ) { |
| 314 | if ( $block['name'] === 'image' && $block['properties']['attributes']['src'] ) { |
| 315 | $this->push_to_asset_urls_tracker( $block['properties']['wp_attachment_id'], $block['properties']['attributes']['src'] ); |
| 316 | } |
| 317 | |
| 318 | if ( $block['name'] === 'video' && $block['properties']['attributes']['src'] ) { |
| 319 | $this->push_to_asset_urls_tracker( false, $block['properties']['attributes']['src'] ); |
| 320 | if ( $block['properties']['thumbnail']['url'] ) { |
| 321 | $this->push_to_asset_urls_tracker( $block['properties']['thumbnail']['wp_attachment_id'], $block['properties']['thumbnail']['url'] ); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | if ( $block['name'] === 'lottie' && $block['properties']['lottie']['src'] ) { |
| 326 | $this->push_to_asset_urls_tracker( false, $block['properties']['lottie']['src'] ); |
| 327 | } |
| 328 | if ( $block['name'] === 'lightbox' && $block['properties']['lightbox']['thumbnail']['src'] ) { |
| 329 | $this->push_to_asset_urls_tracker( false, $block['properties']['lightbox']['thumbnail']['src'] ); |
| 330 | |
| 331 | $lightbox_media = $block['properties']['lightbox']['media']; |
| 332 | |
| 333 | foreach ( $lightbox_media as $key => $media_item ) { |
| 334 | if ( $media_item['sources']['original'] ) { |
| 335 | $this->push_to_asset_urls_tracker( false, $media_item['sources']['original'] ); |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | private function count( $array ) { |
| 343 | if ( isset( $array ) && is_array( $array ) ) { |
| 344 | return count( $array ); |
| 345 | } |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | private function make_zip_and_get_url( $zip_filename, $template_data ) { |
| 350 | try { |
| 351 | if ( empty( $wp_filesystem ) ) { |
| 352 | require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 353 | WP_Filesystem(); |
| 354 | } |
| 355 | |
| 356 | global $wp_filesystem; |
| 357 | $zip = new \ZipArchive(); |
| 358 | |
| 359 | $upload_dir = wp_upload_dir(); |
| 360 | |
| 361 | // Step 1: create zip file path and json file path |
| 362 | $zip_file_path = $upload_dir['basedir'] . "/$zip_filename.zip"; |
| 363 | |
| 364 | // site-settings.json |
| 365 | $site_settings_filename = 'site-settings.json'; |
| 366 | $site_settings_file_path = $upload_dir['basedir'] . '/' . $site_settings_filename; |
| 367 | $site_settings_data = array(); |
| 368 | $site_settings_data['exportId'] = $template_data['exportId']; |
| 369 | $site_settings_data['variable_prefix'] = $template_data['variable_prefix']; |
| 370 | $site_settings_data['batch_id'] = $template_data['batch_id']; |
| 371 | $site_settings_data['prefix'] = $template_data['prefix']; |
| 372 | $site_settings_data['siteInfo'] = $template_data['siteInfo']; |
| 373 | $site_settings_file_content = json_encode( $site_settings_data, JSON_PRETTY_PRINT ); |
| 374 | $site_settings_file_write = $wp_filesystem->put_contents( |
| 375 | $site_settings_file_path, |
| 376 | $site_settings_file_content, |
| 377 | FS_CHMOD_FILE // predefined mode settings for WP files |
| 378 | ); |
| 379 | |
| 380 | // assets.json |
| 381 | $assets_filename = 'assets.json'; |
| 382 | $assets_file_path = $upload_dir['basedir'] . '/' . $assets_filename; |
| 383 | $assets_data = array(); |
| 384 | $assets_data['assetUrls'] = $template_data['assetUrls']; |
| 385 | $assets_file_content = json_encode( $assets_data, JSON_PRETTY_PRINT ); |
| 386 | $assets_file_write = $wp_filesystem->put_contents( |
| 387 | $assets_file_path, |
| 388 | $assets_file_content, |
| 389 | FS_CHMOD_FILE // predefined mode settings for WP files |
| 390 | ); |
| 391 | |
| 392 | // pages.json |
| 393 | $pages_filename = 'pages.json'; |
| 394 | $pages_file_path = $upload_dir['basedir'] . '/' . $pages_filename; |
| 395 | $pages_data = array(); |
| 396 | $pages_data['pages'] = $template_data['pages']; |
| 397 | $pages_data['templates'] = $template_data['templates']; |
| 398 | $pages_data['utility_pages'] = $template_data['utility_pages']; |
| 399 | $pages_file_content = json_encode( $pages_data, JSON_PRETTY_PRINT ); |
| 400 | $pages_file_write = $wp_filesystem->put_contents( |
| 401 | $pages_file_path, |
| 402 | $pages_file_content, |
| 403 | FS_CHMOD_FILE // predefined mode settings for WP files |
| 404 | ); |
| 405 | |
| 406 | // popups.json |
| 407 | $popups_filename = 'popups.json'; |
| 408 | $popups_file_path = $upload_dir['basedir'] . '/' . $popups_filename; |
| 409 | $popups_data = array(); |
| 410 | $popups_data['popups'] = $template_data['popups']; |
| 411 | $popups_file_content = json_encode( $popups_data, JSON_PRETTY_PRINT ); |
| 412 | $popups_file_write = $wp_filesystem->put_contents( |
| 413 | $popups_file_path, |
| 414 | $popups_file_content, |
| 415 | FS_CHMOD_FILE // predefined mode settings for WP files |
| 416 | ); |
| 417 | |
| 418 | // content-manager.json |
| 419 | $content_manager_filename = 'content-manager.json'; |
| 420 | $content_manager_file_path = $upload_dir['basedir'] . '/' . $content_manager_filename; |
| 421 | $content_manager_data = array(); |
| 422 | $content_manager_data['contentManager'] = $template_data['contentManager']; |
| 423 | $content_manager_file_content = json_encode( $content_manager_data, JSON_PRETTY_PRINT ); |
| 424 | $content_manager_file_write = $wp_filesystem->put_contents( |
| 425 | $content_manager_file_path, |
| 426 | $content_manager_file_content, |
| 427 | FS_CHMOD_FILE // predefined mode settings for WP files |
| 428 | ); |
| 429 | |
| 430 | // content-manager-ref-fields.json |
| 431 | $content_manager_ref_fields_filename = 'content-manager-ref-fields.json'; |
| 432 | $content_manager_ref_fields_file_path = $upload_dir['basedir'] . '/' . $content_manager_ref_fields_filename; |
| 433 | $content_manager_ref_fields_data = array(); |
| 434 | $content_manager_ref_fields_data['contentManagerRefFields'] = $template_data['contentManagerRefFields']; |
| 435 | $content_manager_ref_fields_file_content = json_encode( $content_manager_ref_fields_data, JSON_PRETTY_PRINT ); |
| 436 | $content_manager_ref_fields_file_write = $wp_filesystem->put_contents( |
| 437 | $content_manager_ref_fields_file_path, |
| 438 | $content_manager_ref_fields_file_content, |
| 439 | FS_CHMOD_FILE // predefined mode settings for WP files |
| 440 | ); |
| 441 | |
| 442 | // symbols.json |
| 443 | $symbols_filename = 'symbols.json'; |
| 444 | $symbols_file_path = $upload_dir['basedir'] . '/' . $symbols_filename; |
| 445 | $symbols_data = array(); |
| 446 | $symbols_data['symbols'] = $template_data['symbols']; |
| 447 | $symbols_file_content = json_encode( $symbols_data, JSON_PRETTY_PRINT ); |
| 448 | $symbols_file_write = $wp_filesystem->put_contents( |
| 449 | $symbols_file_path, |
| 450 | $symbols_file_content, |
| 451 | FS_CHMOD_FILE // predefined mode settings for WP files |
| 452 | ); |
| 453 | |
| 454 | // styles.json |
| 455 | $styles_filename = 'styles.json'; |
| 456 | $styles_file_path = $upload_dir['basedir'] . '/' . $styles_filename; |
| 457 | $styles_data = array(); |
| 458 | $styles_data['viewPorts'] = $template_data['viewPorts']; |
| 459 | $styles_data['customFonts'] = $template_data['customFonts']; |
| 460 | $styles_data['globalStyleBlocks'] = $template_data['globalStyleBlocks']; |
| 461 | $styles_data['variables'] = $template_data['variables']; |
| 462 | $styles_file_content = json_encode( $styles_data, JSON_PRETTY_PRINT ); |
| 463 | $styles_file_write = $wp_filesystem->put_contents( |
| 464 | $styles_file_path, |
| 465 | $styles_file_content, |
| 466 | FS_CHMOD_FILE // predefined mode settings for WP files |
| 467 | ); |
| 468 | |
| 469 | // single.json |
| 470 | $single_filename = 'single.json'; |
| 471 | $single_file_path = $upload_dir['basedir'] . '/' . $single_filename; |
| 472 | $single_data = array(); |
| 473 | $single_data['exportId'] = $template_data['exportId']; |
| 474 | $single_data['batch_id'] = $template_data['batch_id']; |
| 475 | |
| 476 | $template_info = array( |
| 477 | 'symbols' => $this->count( $template_data['symbols'] ), |
| 478 | 'popups' => $this->count( $template_data['popups'] ), |
| 479 | 'pages' => $this->count( $template_data['pages'] ), |
| 480 | 'templates' => $this->count( $template_data['templates'] ), |
| 481 | 'utilityPages' => $this->count( $template_data['utility_pages'] ), |
| 482 | 'variables' => $this->count( $template_data['variables']['data'] ), |
| 483 | 'images' => $this->count( $template_data['assetUrls'] ), |
| 484 | 'fonts' => $this->count( $template_data['customFonts'] ), |
| 485 | 'contentManager' => $this->count( $template_data['contentManager'] ), |
| 486 | 'contentManager' => array_reduce( |
| 487 | $template_data['contentManager'], |
| 488 | function( $sum, $item ) { |
| 489 | return $sum + ( isset( $item->children ) ? count( $item->children ) : 0 ); |
| 490 | }, |
| 491 | 0 |
| 492 | ), |
| 493 | 'contentManagerRefFields' => $this->count( $template_data['contentManagerRefFields'] ), |
| 494 | 'contentManager_list' => array_map( |
| 495 | function( $item ) { |
| 496 | return $item->post_title . ' (' . ( isset( $item->children ) ? count( $item->children ) : 0 ) . ')'; |
| 497 | }, |
| 498 | $template_data['contentManager'] |
| 499 | ), |
| 500 | ); |
| 501 | |
| 502 | $single_data['template_info'] = $template_info; |
| 503 | |
| 504 | $single_data['pages'] = $this->extractPageData( $template_data['pages'] ); |
| 505 | $single_data['templates'] = $this->extractPageData( $template_data['templates'] ); |
| 506 | $single_data['utility_pages'] = $this->extractPageData( $template_data['utility_pages'] ); |
| 507 | $single_file_content = json_encode( $single_data, JSON_PRETTY_PRINT ); |
| 508 | $single_file_write = $wp_filesystem->put_contents( |
| 509 | $single_file_path, |
| 510 | $single_file_content, |
| 511 | FS_CHMOD_FILE // predefined mode settings for WP files |
| 512 | ); |
| 513 | |
| 514 | if ( ! $site_settings_file_write || ! $assets_file_write || ! $pages_file_write || ! $popups_file_write || ! $content_manager_file_write || ! $symbols_file_write || ! $styles_file_write || ! $single_file_write || ! $content_manager_ref_fields_file_write ) { |
| 515 | // throw new \Exception('Failed to write template file'); |
| 516 | wp_send_json_error( 'Failed to write template file' ); |
| 517 | } |
| 518 | |
| 519 | // Step 3: Open the zip file |
| 520 | if ( true !== $zip->open( $zip_file_path, \ZipArchive::CREATE ) ) { |
| 521 | // throw new \Exception('Failed to create zip file'); |
| 522 | wp_send_json_error( 'Failed to write template file' ); |
| 523 | } |
| 524 | |
| 525 | // Step 4: Add file to the zip file |
| 526 | foreach ( $template_data['assetUrls'] as $key => $asset_item ) { |
| 527 | $url = $asset_item['url']; |
| 528 | $file_name = basename( $url ); // Extract only the filename (e.g., "image.jpg") |
| 529 | |
| 530 | $file_path = $upload_dir['basedir'] . str_replace( $upload_dir['baseurl'], '', $url ); |
| 531 | $zip_path = 'assets/' . $file_name; // Place all files inside "media/" without subdirectories |
| 532 | |
| 533 | // Check if file exists |
| 534 | if ( ! file_exists( $file_path ) ) { |
| 535 | // error_log("File not found: " . $file_path); |
| 536 | continue; // Skip to the next file |
| 537 | } |
| 538 | |
| 539 | // Try adding the file to the ZIP |
| 540 | if ( ! $zip->addFile( $file_path, $zip_path ) ) { |
| 541 | // error_log("Failed to add file to ZIP: " . $file_path); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | if ( false === $zip->addFile( $site_settings_file_path, $site_settings_filename ) ) { |
| 546 | // throw new \Exception('Failed to add assets.json file to zip'); |
| 547 | wp_send_json_error( 'Failed to write site-settings.json file' ); |
| 548 | } |
| 549 | |
| 550 | if ( isset( $template_data['assetUrls'] ) && ! empty( $template_data['assetUrls'] ) ) { |
| 551 | if ( false === $zip->addFile( $assets_file_path, $assets_filename ) ) { |
| 552 | // throw new \Exception('Failed to add assets.json file to zip'); |
| 553 | wp_send_json_error( 'Failed to write assets.json file' ); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | if ( false === $zip->addFile( $pages_file_path, $pages_filename ) ) { |
| 558 | // throw new \Exception('Failed to add pages.json file to zip'); |
| 559 | wp_send_json_error( 'Failed to write pages.json file' ); |
| 560 | } |
| 561 | |
| 562 | if ( false === $zip->addFile( $popups_file_path, $popups_filename ) ) { |
| 563 | // throw new \Exception('Failed to add popups.json file to zip'); |
| 564 | wp_send_json_error( 'Failed to write popups.json file' ); |
| 565 | } |
| 566 | |
| 567 | if ( false === $zip->addFile( $content_manager_file_path, $content_manager_filename ) ) { |
| 568 | // throw new \Exception('Failed to add content-manager.json file to zip'); |
| 569 | wp_send_json_error( 'Failed to write content-manager.json file' ); |
| 570 | } |
| 571 | |
| 572 | if ( false === $zip->addFile( $content_manager_ref_fields_file_path, $content_manager_ref_fields_filename ) ) { |
| 573 | // throw new \Exception('Failed to add content-manager-ref-fields.json file to zip'); |
| 574 | wp_send_json_error( 'Failed to write content-manager-ref-fields.json file' ); |
| 575 | } |
| 576 | |
| 577 | if ( false === $zip->addFile( $symbols_file_path, $symbols_filename ) ) { |
| 578 | // throw new \Exception('Failed to add symbols.json file to zip'); |
| 579 | wp_send_json_error( 'Failed to write symbols.json file' ); |
| 580 | |
| 581 | } |
| 582 | if ( false === $zip->addFile( $styles_file_path, $styles_filename ) ) { |
| 583 | // throw new \Exception('Failed to add styles.json file to zip'); |
| 584 | wp_send_json_error( 'Failed to write styles.json file' ); |
| 585 | } |
| 586 | if ( false === $zip->addFile( $single_file_path, $single_filename ) ) { |
| 587 | // throw new \Exception('Failed to add styles.json file to zip'); |
| 588 | wp_send_json_error( 'Failed to write styles.json file' ); |
| 589 | } |
| 590 | |
| 591 | $zip->close(); |
| 592 | |
| 593 | // Step 6: remove the template file after zip file is created |
| 594 | wp_delete_file( $site_settings_file_path ); |
| 595 | wp_delete_file( $assets_file_path ); |
| 596 | wp_delete_file( $pages_file_path ); |
| 597 | wp_delete_file( $popups_file_path ); |
| 598 | wp_delete_file( $content_manager_file_path ); |
| 599 | wp_delete_file( $content_manager_ref_fields_file_path ); |
| 600 | wp_delete_file( $symbols_file_path ); |
| 601 | wp_delete_file( $styles_file_path ); |
| 602 | wp_delete_file( $single_file_path ); |
| 603 | |
| 604 | // Step 7: Download the created zip file |
| 605 | return home_url( "/?page-export=true&file-name=$zip_filename.zip" ); |
| 606 | } catch ( \Exception $e ) { |
| 607 | // return false; |
| 608 | wp_send_json_error( 'Failed to write template file' ); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | function extractPageData( $pages ) { |
| 613 | $result = array(); |
| 614 | foreach ( $pages as $page ) { |
| 615 | if ( isset( $page->post_title ) && isset( $page->post_name ) ) { |
| 616 | $result[] = array( |
| 617 | 'post_title' => $page->post_title, |
| 618 | 'post_name' => $page->post_name, |
| 619 | ); |
| 620 | } |
| 621 | } |
| 622 | return $result; // Returns an array of page data |
| 623 | } |
| 624 | |
| 625 | |
| 626 | private function get_global_style_blocks() { |
| 627 | return HelperFunctions::get_global_data_using_key( KIRKI_GLOBAL_STYLE_BLOCK_META_KEY ); |
| 628 | } |
| 629 | |
| 630 | private function get_all_pages() { |
| 631 | return $this->get_posts_with_meta( 'page', array( 'publish' ) ); |
| 632 | } |
| 633 | |
| 634 | private function get_all_popups() { |
| 635 | return $this->get_posts_with_meta('kirki_popup', array( 'publish', 'draft' ) ); |
| 636 | } |
| 637 | |
| 638 | private function get_all_templates() { |
| 639 | return $this->get_posts_with_meta('kirki_template', array( 'publish' ) ); |
| 640 | } |
| 641 | |
| 642 | private function get_all_utility_pages() { |
| 643 | return $this->get_posts_with_meta('kirki_utility', array( 'publish' ) ); |
| 644 | } |
| 645 | |
| 646 | private function get_all_symbols() { |
| 647 | return $this->get_posts_with_meta('kirki_symbol', array( 'draft' ) ); |
| 648 | } |
| 649 | |
| 650 | private function get_all_content_manager_data() { |
| 651 | $parent_posts = $this->get_posts_with_meta('kirki_cm', array( 'publish' ) ); |
| 652 | |
| 653 | foreach ( $parent_posts as $key => $parent_post ) { |
| 654 | $parent_posts[ $key ]->children = $this->get_posts_with_meta('kirki_cm_' . $parent_post->ID, array( 'publish' ) ); |
| 655 | } |
| 656 | |
| 657 | return $parent_posts; |
| 658 | } |
| 659 | |
| 660 | private function get_view_ports() { |
| 661 | $control = HelperFunctions::get_global_data_using_key( KIRKI_USER_CONTROLLER_META_KEY ); |
| 662 | |
| 663 | if ( ! $control ) { |
| 664 | $control = array(); |
| 665 | } |
| 666 | if ( ! isset( $control['viewport'] ) ) { |
| 667 | $control['viewport'] = HelperFunctions::get_initial_view_ports(); |
| 668 | } |
| 669 | return $control['viewport']; |
| 670 | } |
| 671 | |
| 672 | private function get_custom_fonts() { |
| 673 | $custom_fonts = HelperFunctions::get_global_data_using_key( KIRKI_USER_CUSTOM_FONTS_META_KEY ); |
| 674 | return $custom_fonts ? $custom_fonts : array(); |
| 675 | } |
| 676 | |
| 677 | private function get_posts_with_meta( $post_type, $post_status = array( 'publish' ) ) { |
| 678 | $all_posts = get_posts( |
| 679 | array( |
| 680 | 'post_type' => $post_type, |
| 681 | 'posts_per_page' => -1, |
| 682 | 'post_status' => $post_status, |
| 683 | ) |
| 684 | ); |
| 685 | foreach ( $all_posts as $key => $post ) { |
| 686 | $post_meta = get_post_meta( $post->ID ); |
| 687 | foreach ( $this->discarded_meta as $discarded_meta_key ) { |
| 688 | unset( $post_meta[ $discarded_meta_key ] ); |
| 689 | } |
| 690 | if ( $this->only_published_pages ) { |
| 691 | $stage_metas = Staging::get_all_stage_related_meta_names( $post->ID ); |
| 692 | foreach ( $stage_metas as $discarded_meta_key ) { |
| 693 | unset( $post_meta[ $discarded_meta_key ] ); |
| 694 | } |
| 695 | } |
| 696 | $post->meta = $post_meta; |
| 697 | |
| 698 | // Get the post's content manager fields if available |
| 699 | // $cm_ref_fields = ContentManagerHelper::get_post_cm_ref_fields($post->ID); |
| 700 | // if (isset($cm_ref_fields) && !empty($cm_ref_fields)) { |
| 701 | // $post->cm_ref_fields = $cm_ref_fields; |
| 702 | // } |
| 703 | |
| 704 | $all_posts[ $key ] = $post; |
| 705 | } |
| 706 | |
| 707 | return $all_posts; |
| 708 | } |
| 709 | |
| 710 | private function push_to_asset_urls_tracker( $attachment_id, $url ) { |
| 711 | if ( ! $attachment_id ) { |
| 712 | $attachment_id = attachment_url_to_postid( $url ); |
| 713 | } |
| 714 | if ( $attachment_id ) { |
| 715 | $this->asset_urls_tracker[ $attachment_id ] = array( |
| 716 | 'attachment_id' => $attachment_id, |
| 717 | 'url' => $url, |
| 718 | ); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | private function get_all_content_manager_ref_fields() { |
| 723 | $cm_ref_fields = ContentManagerHelper::get_post_cm_ref_fields(); |
| 724 | |
| 725 | return $cm_ref_fields; |
| 726 | } |
| 727 | } |
| 728 |