| @@ -5,8 +5,9 @@ | ||
| 5 | 5 | use Exception; |
| 6 | 6 | use Templately\Builder\PageTemplates; |
| 7 | 7 | use Templately\Builder\Types\BaseTemplate; |
| 8 | 8 | use Templately\Core\Importer\Utils\Utils; |
| 9 | +use Templately\Core\Importer\WPImport; | |
| 9 | 10 | use Templately\Utils\Helper; |
| 10 | 11 | |
| 11 | 12 | class Templates extends BaseRunner { |
| 12 | 13 | protected $imported_types = []; |
| @@ -31,61 +32,46 @@ | ||
| 31 | 32 | /** |
| 32 | 33 | * @throws Exception |
| 33 | 34 | */ |
| 34 | 35 | public function import( $data, $imported_data ): array { |
| 35 | - $results = $data["imported_data"]["templates"] ?? []; | |
| 36 | + $results = []; | |
| 36 | 37 | $templates = $this->manifest['templates']; |
| 37 | 38 | $path = $this->dir_path . 'templates' . DIRECTORY_SEPARATOR; |
| 38 | 39 | |
| 39 | 40 | $_extra_pages = []; |
| 40 | 41 | |
| 41 | - | |
| 42 | 42 | $total = count( $templates ); |
| 43 | - | |
| 44 | - $results = $this->loop( $templates, function($id, $template_settings, $results ) use( $path, $total ) { | |
| 43 | + $processed = 0; | |
| 44 | + foreach ( $templates as $id => $template_settings ) { | |
| 45 | 45 | $template_content = Utils::read_json_file( $path . $id . '.json' ); |
| 46 | 46 | |
| 47 | 47 | $import = $this->import_template( $id, $template_settings, $template_content ); |
| 48 | 48 | if ( $import ) { |
| 49 | - $results['templates']['succeed'][ $id ] = $import['id']; | |
| 50 | - $results['templates']['template_types'][] = $template_settings['type']; | |
| 49 | + $results['succeed'][ $id ] = $import; | |
| 50 | + $results['template_types'][] = $template_settings['type']; | |
| 51 | 51 | |
| 52 | 52 | if ( $template_settings['type'] === 'archive' || $template_settings['type'] === 'product_archive' || $template_settings['type'] === 'course_archive' ) { |
| 53 | 53 | $page_id = $this->create_archive_page( $template_settings, $this->manifest['platform'] ); |
| 54 | - if ( $page_id && isset($template_settings['page_settings']['archive_page_id']) ) { | |
| 55 | - $archive_page_id = $template_settings['page_settings']['archive_page_id']; | |
| 56 | - $results['archive_settings'][$archive_page_id] = [ | |
| 54 | + if ( $page_id ) { | |
| 55 | + $_extra_pages['archive_settings'] = [ | |
| 57 | 56 | 'old_id' => $id, |
| 58 | 57 | 'page_id' => $page_id, |
| 59 | - 'archive_id' => $archive_page_id | |
| 58 | + 'archive_id' => $import | |
| 60 | 59 | ]; |
| 61 | 60 | } |
| 62 | 61 | } |
| 63 | - else if($template_settings['type'] === 'fluent_product_single'){ | |
| 64 | - $this->create_page_template(); | |
| 65 | - } | |
| 66 | 62 | |
| 67 | 63 | } else { |
| 68 | - $results['templates']['failed'][ $id ] = $import; | |
| 64 | + $results['failed'][ $id ] = $import; | |
| 69 | 65 | } |
| 70 | 66 | |
| 71 | 67 | // Broadcast Log |
| 72 | - $processed = 0; | |
| 73 | - $processed_template = array_merge($results['templates']['succeed'] ?? [], $results['templates']['failed'] ?? []); | |
| 74 | - array_walk_recursive($processed_template, function($item, $key) use (&$processed) { | |
| 75 | - $processed++; | |
| 76 | - }); | |
| 68 | + $processed += 1; | |
| 77 | 69 | $progress = floor( ( 100 * $processed ) / $total ); |
| 78 | 70 | $this->log( $progress ); |
| 71 | + } | |
| 79 | 72 | |
| 80 | - $results['templates']['__attachments'][ $id ] = isset($import['__attachments']) ? $import['__attachments'] : []; | |
| 81 | - // Add the template to the processed templates and update the session data | |
| 82 | - | |
| 83 | - | |
| 84 | - return $results; | |
| 85 | - }); // , null, true | |
| 86 | - | |
| 87 | - return $results; | |
| 73 | + return array_merge( [ 'templates' => $results ], $_extra_pages ); | |
| 88 | 74 | } |
| 89 | 75 | |
| 90 | 76 | private function import_template( $id, $template_settings, $template_content ) { |
| 91 | 77 | $type = $template_settings['type']; |
| @@ -100,9 +86,8 @@ | ||
| 100 | 86 | 'post_type' => 'templately_library', |
| 101 | 87 | ]; |
| 102 | 88 | |
| 103 | 89 | $meta = []; |
| 104 | - $result = []; | |
| 105 | 90 | |
| 106 | 91 | if ( $this->manifest['platform'] == 'gutenberg' ) { |
| 107 | 92 | $meta['_wp_page_template'] = PageTemplates::TEMPLATE_HEADER_FOOTER; |
| 108 | 93 | } |
| @@ -115,14 +100,13 @@ | ||
| 115 | 100 | if (!$template->is_elementor_template()) { |
| 116 | 101 | $attachments = $this->json->parse_images($template_content['content']); |
| 117 | 102 | |
| 118 | 103 | if (!empty($attachments)) { |
| 119 | - $result['__attachments'] = $attachments; | |
| 120 | - // $manifest_content = &$this->manifest['templates'][$id]; | |
| 121 | - // if(!isset($manifest_content['__attachments'])){ | |
| 122 | - // $manifest_content['__attachments'] = []; | |
| 123 | - // } | |
| 124 | - // $manifest_content['__attachments'] = $attachments; | |
| 104 | + $manifest_content = &$this->manifest['templates'][$id]; | |
| 105 | + if(!isset($manifest_content['__attachments'])){ | |
| 106 | + $manifest_content['__attachments'] = []; | |
| 107 | + } | |
| 108 | + $manifest_content['__attachments'] = $attachments; | |
| 125 | 109 | } |
| 126 | 110 | } |
| 127 | 111 | |
| 128 | 112 | $template_content['id'] = $id; |
| @@ -127,23 +111,15 @@ | ||
| 127 | 111 | |
| 128 | 112 | $template_content['id'] = $id; |
| 129 | 113 | unset($template_settings['conditions']); |
| 130 | 114 | $template_content['import_settings'] = $template_settings; |
| 131 | - | |
| 132 | - if($this->platform === 'elementor' && $this->is_ai_content($id)){ | |
| 133 | - $template_content['content'] = []; | |
| 134 | - } | |
| 135 | - else if($this->platform === 'gutenberg' && $this->is_ai_content($id)){ | |
| 136 | - $template_content['content'] = ''; | |
| 137 | - } | |
| 138 | - | |
| 139 | 115 | $template->import( $template_content ); |
| 140 | 116 | |
| 141 | 117 | if($template->has_logo($template_content)){ |
| 142 | 118 | $this->manifest['templates'][$id]['has_logo'] = true; |
| 143 | 119 | } |
| 144 | - $result['id'] = $template->get_main_id(); | |
| 145 | - return $result; | |
| 120 | + | |
| 121 | + return $template->get_main_id(); | |
| 146 | 122 | } |
| 147 | 123 | |
| 148 | 124 | |
| 149 | 125 | /** |
| @@ -153,13 +129,8 @@ | ||
| 153 | 129 | * @return false|int |
| 154 | 130 | */ |
| 155 | 131 | private function create_archive_page( $template_settings, $platform ) { |
| 156 | 132 | try { |
| 157 | - $archive_page_id = $template_settings['page_settings']['archive_page_id'] ?? null; | |
| 158 | - if($archive_page_id && !empty($this->manifest['content']['page'][$archive_page_id])){ | |
| 159 | - return false; | |
| 160 | - } | |
| 161 | - | |
| 162 | 133 | $type = $template_settings['type']; |
| 163 | 134 | |
| 164 | 135 | $archive_page = wp_insert_post( [ |
| 165 | 136 | 'post_title' => $template_settings['title'] ?? ucfirst( $type ) . ' - (by Templately)', |
| @@ -179,58 +150,10 @@ | ||
| 179 | 150 | |
| 180 | 151 | if($type === 'product_archive'){ |
| 181 | 152 | Utils::update_option( 'woocommerce_shop_page_id', $archive_page ); |
| 182 | 153 | } |
| 183 | - | |
| 184 | - if($type === 'course_archive'){ | |
| 185 | - // get page slug from $archive_page id and update learndash_settings_permalinks option to courses. | |
| 186 | - $post_name = get_post_field( 'post_name', $archive_page ); | |
| 187 | - | |
| 188 | - if(class_exists('\LearnDash_Settings_Section')){ | |
| 189 | - $section = \LearnDash_Settings_Section::get_section_instance( 'LearnDash_Settings_Section_Permalinks' ); | |
| 190 | - if(!empty($section)){ | |
| 191 | - $section->set_setting('courses', $post_name); | |
| 192 | - if(function_exists('learndash_setup_rewrite_flush')){ | |
| 193 | - learndash_setup_rewrite_flush(); | |
| 194 | - } | |
| 195 | - } | |
| 196 | - } | |
| 197 | - } | |
| 198 | - | |
| 199 | 154 | return $archive_page; |
| 200 | 155 | } catch ( \Exception $e ) { |
| 201 | 156 | return false; |
| 202 | - } | |
| 203 | - } | |
| 204 | - | |
| 205 | - /** | |
| 206 | - * Only for gutenberg create a single page template. | |
| 207 | - * @return void | |
| 208 | - */ | |
| 209 | - private function create_page_template() { | |
| 210 | - try { | |
| 211 | - $meta = []; | |
| 212 | - $data = []; | |
| 213 | - | |
| 214 | - $post_data = [ | |
| 215 | - 'post_title' => 'Single Page - (by Templately)', | |
| 216 | - 'post_status' => 'publish', | |
| 217 | - 'post_type' => 'templately_library', | |
| 218 | - ]; | |
| 219 | - | |
| 220 | - if ( $this->manifest['platform'] == 'elementor' ) { | |
| 221 | - $meta['_wp_page_template'] = 'elementor_header_footer'; | |
| 222 | - $data = json_decode('{"content":[{"id":"4a86515d","settings":[],"elements":[{"id":"30a46db1","settings":{"content_width":"full"},"elements":[],"isInner":false,"widgetType":"tl-post-content","elType":"widget"}],"isInner":false,"elType":"container"}],"settings":{"template":"elementor_header_footer"},"metadata":[]}', true); | |
| 223 | - } elseif ( $this->manifest['platform'] == 'gutenberg' ) { | |
| 224 | - $meta['_wp_page_template'] = PageTemplates::TEMPLATE_HEADER_FOOTER; | |
| 225 | - $data = ['content' => '<!-- wp:post-content /-->']; | |
| 226 | - } | |
| 227 | - | |
| 228 | - $template = $this->factory->create( 'page_single', $post_data, $meta ); | |
| 229 | - $template->import( $data ); | |
| 230 | - | |
| 231 | - // return $archive_page; | |
| 232 | - } catch ( \Exception $e ) { | |
| 233 | - return; | |
| 234 | 157 | } |
| 235 | 158 | } |
| 236 | 159 | } |