← All changes
|
includes/Core/Importer/Runners/GutenbergContent.php
+43
-11
3.1.1
→
3.1.9
View file →
| @@ -36,13 +36,21 @@ | ||
| 36 | 36 | return __( 'Importing Gutenberg Templates (Pages, Posts etc)', 'templately' ); |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | public function import( $data, $imported_data ): array { |
| 40 | - $results = []; | |
| 40 | + $results = $data["imported_data"]["content"] ?? []; | |
| 41 | 41 | $contents = $this->manifest['content']; |
| 42 | 42 | $path = $this->dir_path . 'content' . DIRECTORY_SEPARATOR; |
| 43 | 43 | |
| 44 | - if(isset($this->manifest['has_settings']) && $this->manifest['has_settings']){ | |
| 44 | + $processed_templates = $this->origin->get_progress(); | |
| 45 | + | |
| 46 | + if(empty($processed_templates)){ | |
| 47 | + $this->log( 0 ); | |
| 48 | + $processed_templates = ["__started__"]; | |
| 49 | + $this->origin->update_progress( $processed_templates); | |
| 50 | + } | |
| 51 | + | |
| 52 | + if(isset($this->manifest['has_settings']) && $this->manifest['has_settings'] && !in_array("global_colors", $processed_templates)){ | |
| 45 | 53 | $file = $this->dir_path . "settings.json"; |
| 46 | 54 | $settings = Utils::read_json_file( $file ); |
| 47 | 55 | |
| 48 | 56 | if(!empty($data['color'])){ |
| @@ -61,8 +69,9 @@ | ||
| 61 | 69 | if(!empty($data['logo']['id'])){ |
| 62 | 70 | $site_logo_id = $data['logo']['id']; |
| 63 | 71 | $settings['site_logo'] = $site_logo_id; |
| 64 | 72 | Utils::update_option( 'site_logo', $site_logo_id ); |
| 73 | + $this->origin->update_imported_list('attachment', $data['logo']['id']); | |
| 65 | 74 | } |
| 66 | 75 | else if(!empty($data['logo']) && empty(get_option('site_logo'))){ |
| 67 | 76 | // demo logo |
| 68 | 77 | $site_logo = Utils::upload_logo($data['logo']); |
| @@ -68,8 +77,9 @@ | ||
| 68 | 77 | $site_logo = Utils::upload_logo($data['logo']); |
| 69 | 78 | if(!empty($site_logo['id'])){ |
| 70 | 79 | $settings['site_logo'] = $site_logo['id']; |
| 71 | 80 | Utils::update_option( 'site_logo', $site_logo['id'] ); |
| 81 | + $this->origin->update_imported_list('attachment', $site_logo['id']); | |
| 72 | 82 | } |
| 73 | 83 | } |
| 74 | 84 | |
| 75 | 85 | $settings = array_map('json_encode', $settings); |
| @@ -75,8 +85,11 @@ | ||
| 75 | 85 | $settings = array_map('json_encode', $settings); |
| 76 | 86 | |
| 77 | 87 | // Save the settings to the 'eb_global_styles' option |
| 78 | 88 | Utils::update_option('eb_global_styles', $settings); |
| 89 | + | |
| 90 | + $processed_templates[] = "global_colors"; | |
| 91 | + $this->origin->update_progress( $processed_templates, [ 'content' => $results ]); | |
| 79 | 92 | } |
| 80 | 93 | |
| 81 | 94 | $processed = 0; |
| 82 | 95 | $total = array_reduce($contents, function($carry, $item) { |
| @@ -84,21 +97,42 @@ | ||
| 84 | 97 | }, 0); |
| 85 | 98 | |
| 86 | 99 | foreach ( $contents as $type => $posts ) { |
| 87 | 100 | foreach ( $posts as $id => $settings ) { |
| 101 | + if (in_array("$type::$id", $processed_templates)) { | |
| 102 | + continue; | |
| 103 | + } | |
| 88 | 104 | $import = $this->import_page_content( $id, $type, $path, $settings ); |
| 89 | 105 | |
| 90 | 106 | if ( ! $import ) { |
| 91 | 107 | $results[ $type ]['failed'][ $id ] = $import; |
| 92 | 108 | } else { |
| 93 | - Utils::import_page_settings( $import, $settings ); | |
| 94 | - $results[ $type ]['succeed'][ $id ] = $import; | |
| 109 | + Utils::import_page_settings( $import['id'], $settings ); | |
| 110 | + $results[ $type ]['succeed'][ $id ] = $import['id']; | |
| 95 | 111 | } |
| 96 | 112 | |
| 97 | 113 | // Broadcast Log |
| 98 | - $processed += 1; | |
| 114 | + $processed = 0; | |
| 115 | + array_walk_recursive($results, function($item) use (&$processed) { | |
| 116 | + $processed++; | |
| 117 | + }); | |
| 99 | 118 | $progress = floor( ( 100 * $processed ) / $total ); |
| 100 | 119 | $this->log( $progress, null, 'eventLog' ); |
| 120 | + | |
| 121 | + | |
| 122 | + $results['__attachments'][$type][ $id ] = isset($import['__attachments']) ? $import['__attachments'] : []; | |
| 123 | + // Add the template to the processed templates and update the session data | |
| 124 | + $processed_templates[] = "$type::$id"; | |
| 125 | + $this->origin->update_progress( $processed_templates, [ 'content' => $results ]); | |
| 126 | + // If it's not the last item, send the SSE message and exit | |
| 127 | + if( end($contents) !== $posts || end($posts) !== $settings) { | |
| 128 | + $this->sse_message( [ | |
| 129 | + 'type' => 'continue', | |
| 130 | + 'action' => 'continue', | |
| 131 | + 'results' => __METHOD__ . '::' . __LINE__, | |
| 132 | + ] ); | |
| 133 | + exit; | |
| 134 | + } | |
| 101 | 135 | } |
| 102 | 136 | } |
| 103 | 137 | |
| 104 | 138 | return [ 'content' => $results ]; |
| @@ -140,17 +174,15 @@ | ||
| 140 | 174 | } |
| 141 | 175 | |
| 142 | 176 | $attachments = $this->json->parse_images($json_content['content']); |
| 143 | 177 | |
| 178 | + $result = []; | |
| 144 | 179 | if (!empty($attachments)) { |
| 145 | - $manifest_content = &$this->manifest['content'][$type][$id]; | |
| 146 | - if(!isset($manifest_content['__attachments'])){ | |
| 147 | - $manifest_content['__attachments'] = []; | |
| 148 | - } | |
| 149 | - $manifest_content['__attachments'] = $attachments; | |
| 180 | + $result['__attachments'] = $attachments; | |
| 150 | 181 | } |
| 182 | + $result['id'] = $inserted; | |
| 151 | 183 | |
| 152 | - return $inserted; | |
| 184 | + return $result; | |
| 153 | 185 | } |
| 154 | 186 | } catch ( \Exception $e ) { |
| 155 | 187 | return false; |
| 156 | 188 | } |