| @@ -4,8 +4,9 @@ | ||
| 4 | 4 | |
| 5 | 5 | |
| 6 | 6 | use Exception; |
| 7 | 7 | use Templately\Core\Importer\Utils\Utils; |
| 8 | +use Templately\Utils\Helper; | |
| 8 | 9 | |
| 9 | 10 | class Finalizer extends BaseRunner { |
| 10 | 11 | private $options = []; |
| 11 | 12 | private $type_to_check = [ 'templates', 'content' ]; |
| @@ -15,9 +16,8 @@ | ||
| 15 | 16 | private $sub_type = ''; |
| 16 | 17 | private $extra_content; |
| 17 | 18 | |
| 18 | 19 | private $total_counts = 0; |
| 19 | - private $processed_items = 0; | |
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | 22 | * @var array|mixed |
| 23 | 23 | */ |
| @@ -61,9 +61,9 @@ | ||
| 61 | 61 | if ( empty( $templates ) || ! is_array( $templates ) ) { |
| 62 | 62 | return; |
| 63 | 63 | } |
| 64 | 64 | foreach ( $templates as $id => $template ) { |
| 65 | - if ( ! isset( $template['data'] ) && !isset( $template['__attachments']) ) { | |
| 65 | + if ( ! isset( $template['data'] ) && !isset( $template['__attachments']) && !isset($template['has_logo']) ) { | |
| 66 | 66 | continue; |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | // if ( ! isset( $template['data']['form'] ) && ! isset( $template['data']['nav_menus'] )) { |
| @@ -89,19 +89,62 @@ | ||
| 89 | 89 | if ( ! empty( $imported_data['extra-content'] ) ) { |
| 90 | 90 | $this->extra_content = $imported_data['extra-content']; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - $this->log( 0 ); | |
| 94 | 93 | add_action('templately_import.finalize_gutenberg_attachment', [$this, 'post_log'], 10, 2); |
| 95 | 94 | |
| 95 | + // Get the processed templates from the session data | |
| 96 | + $processed = $this->origin->get_progress(); | |
| 97 | + | |
| 98 | + if(empty($processed)){ | |
| 99 | + $this->log( 0 ); | |
| 100 | + $processed = ["__started__"]; | |
| 101 | + $this->origin->update_progress( $processed); | |
| 102 | + } | |
| 103 | + | |
| 96 | 104 | foreach ( $this->options as $type => $contents ) { |
| 97 | 105 | $this->type = $type; |
| 106 | + | |
| 107 | + // If the template has been processed, skip it | |
| 108 | + if (in_array($type, $processed)) { | |
| 109 | + continue; | |
| 110 | + } | |
| 111 | + | |
| 98 | 112 | if ( $type == 'templates' ) { |
| 99 | - $this->finalize_imports( $contents ); | |
| 113 | + $this->finalize_imports( $contents, $type ); | |
| 114 | + | |
| 115 | + $processed[] = $type; | |
| 116 | + $this->origin->update_progress( $processed); | |
| 117 | + // If it's not the last item, send the SSE message and exit | |
| 118 | + if( end($this->options) !== $contents ) { | |
| 119 | + $this->sse_message( [ | |
| 120 | + 'type' => 'continue', | |
| 121 | + 'action' => 'continue', | |
| 122 | + 'results' => __METHOD__ . '::' . __LINE__, | |
| 123 | + ] ); | |
| 124 | + exit; | |
| 125 | + } | |
| 100 | 126 | } else { |
| 101 | 127 | foreach ( $contents as $post_type => $templates ) { |
| 128 | + // If the template has been processed, skip it | |
| 129 | + if (in_array("$type::$post_type", $processed)) { | |
| 130 | + continue; | |
| 131 | + } | |
| 132 | + | |
| 102 | 133 | $this->sub_type = $post_type; |
| 103 | - $this->finalize_imports( $templates ); | |
| 134 | + $this->finalize_imports( $templates, $type, $post_type ); | |
| 135 | + | |
| 136 | + $processed[] = "$type::$post_type"; | |
| 137 | + $this->origin->update_progress( $processed); | |
| 138 | + // If it's not the last item, send the SSE message and exit | |
| 139 | + if( end($contents) !== $templates ) { | |
| 140 | + $this->sse_message( [ | |
| 141 | + 'type' => 'continue', | |
| 142 | + 'action' => 'continue', | |
| 143 | + 'results' => __METHOD__ . '::' . __LINE__, | |
| 144 | + ] ); | |
| 145 | + exit; | |
| 146 | + } | |
| 104 | 147 | } |
| 105 | 148 | } |
| 106 | 149 | } |
| 107 | 150 | |
| @@ -119,25 +162,55 @@ | ||
| 119 | 162 | rmdir( $upload_dir['basedir'] . '/eb-style/' ); |
| 120 | 163 | } |
| 121 | 164 | } |
| 122 | 165 | |
| 123 | - private function finalize_imports( $templates ) { | |
| 166 | + private function finalize_imports( $templates, $type, $post_type = null ) { | |
| 167 | + // Get the processed templates from the session data | |
| 168 | + $processed = $this->origin->get_progress(); | |
| 169 | + | |
| 124 | 170 | foreach ( $templates as $old_template_id => $template_settings ) { |
| 171 | + // If the template has been processed, skip it | |
| 172 | + if (in_array($old_template_id, $processed)) { | |
| 173 | + continue; | |
| 174 | + } | |
| 175 | + | |
| 125 | 176 | try { |
| 126 | 177 | $path = $this->dir_path . $this->type . DIRECTORY_SEPARATOR; |
| 127 | 178 | if ( ! empty( $this->sub_type ) ) { |
| 128 | 179 | $path .= $this->sub_type . DIRECTORY_SEPARATOR; |
| 129 | 180 | } |
| 181 | + | |
| 182 | + if($post_type && isset($this->imported_data[$type]['__attachments'][$post_type][$old_template_id])){ | |
| 183 | + $template_settings['__attachments'] = $this->imported_data[$type]['__attachments'][$post_type][$old_template_id]; | |
| 184 | + } | |
| 185 | + else if(empty($post_type) && isset($this->imported_data[$type]['__attachments'][$old_template_id])){ | |
| 186 | + $template_settings['__attachments'] = $this->imported_data[$type]['__attachments'][$old_template_id]; | |
| 187 | + } | |
| 188 | + | |
| 130 | 189 | $path .= "{$old_template_id}.json"; |
| 131 | 190 | $template_json = Utils::read_json_file( $path ); |
| 132 | - $this->json->prepare( $template_json, $template_settings, $this->extra_content['form'][ $old_template_id ] ?? [] )->update(); | |
| 191 | + $params = $this->origin->get_request_params(); | |
| 192 | + $this->json->prepare( $template_json, $template_settings, $this->extra_content['form'][ $old_template_id ] ?? [], $params )->update(); | |
| 133 | 193 | |
| 134 | 194 | // Broadcast Log |
| 135 | - $this->processed_items += 1; | |
| 136 | - $progress = floor( ( 100 * $this->processed_items ) / $this->total_counts ); | |
| 195 | + $progress = floor( ( 100 * count($processed) ) / $this->total_counts ); | |
| 137 | 196 | $this->log( $progress ); |
| 138 | 197 | } catch ( Exception $e ) { |
| 139 | 198 | continue; |
| 199 | + } | |
| 200 | + | |
| 201 | + // Add the template to the processed templates and update the session data | |
| 202 | + $processed[] = $old_template_id; | |
| 203 | + $this->origin->update_progress( $processed); | |
| 204 | + | |
| 205 | + // If it's not the last item, send the SSE message and exit | |
| 206 | + if( end($templates) !== $template_settings) { | |
| 207 | + $this->sse_message( [ | |
| 208 | + 'type' => 'continue', | |
| 209 | + 'action' => 'continue', | |
| 210 | + 'results' => __METHOD__ . '::' . __LINE__, | |
| 211 | + ] ); | |
| 212 | + exit; | |
| 140 | 213 | } |
| 141 | 214 | } |
| 142 | 215 | } |
| 143 | 216 | |