PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.4.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.4.0
3.8.0 3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 All 112 releases
templately / includes / Core / Importer / Runners / Templates.php

Templates.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.4.0, at includes/Core/Importer/Runners/Templates.php

217 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Core\Importer\Runners;
4
5 use Exception;
6 use Templately\Builder\PageTemplates;
7 use Templately\Builder\Types\BaseTemplate;
8 use Templately\Core\Importer\Utils\Utils;
9 use Templately\Utils\Helper;
10
11 class Templates extends BaseRunner {
12 protected $imported_types = [];
13
14 public function get_name(): string {
15 return 'templates';
16 }
17
18 public function get_label(): string {
19 return __( 'Templates', 'templately' );
20 }
21
22 public function log_message(): string {
23 return __( 'Importing Templates (i.e: Header, Footer, etc)', 'templately' );
24 }
25
26 public function should_run( $data, $imported_data = [] ): bool {
27 return ! empty( $this->manifest['templates'] );
28 // return isset( $data['templates'] ) && $data['templates'] && ! empty( $this->manifest['templates'] );
29 }
30
31 /**
32 * @throws Exception
33 */
34 public function import( $data, $imported_data ): array {
35 $results = $data["imported_data"]["templates"] ?? [];
36 $templates = $this->manifest['templates'];
37 $path = $this->dir_path . 'templates' . DIRECTORY_SEPARATOR;
38
39 $_extra_pages = [];
40
41
42 $total = count( $templates );
43
44 $results = $this->loop( $templates, function($id, $template_settings, $results ) use( $path, $total ) {
45 $template_content = Utils::read_json_file( $path . $id . '.json' );
46
47 $import = $this->import_template( $id, $template_settings, $template_content );
48 if ( $import ) {
49 $results['templates']['succeed'][ $id ] = $import['id'];
50 $results['templates']['template_types'][] = $template_settings['type'];
51
52 if ( $template_settings['type'] === 'archive' || $template_settings['type'] === 'product_archive' || $template_settings['type'] === 'course_archive' ) {
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] = [
57 'old_id' => $id,
58 'page_id' => $page_id,
59 'archive_id' => $archive_page_id
60 ];
61 }
62 }
63
64 } else {
65 $results['templates']['failed'][ $id ] = $import;
66 }
67
68 // Broadcast Log
69 $processed = 0;
70 $processed_template = array_merge($results['templates']['succeed'] ?? [], $results['templates']['failed'] ?? []);
71 array_walk_recursive($processed_template, function($item, $key) use (&$processed) {
72 $processed++;
73 });
74 $progress = floor( ( 100 * $processed ) / $total );
75 $this->log( $progress );
76
77 $results['templates']['__attachments'][ $id ] = isset($import['__attachments']) ? $import['__attachments'] : [];
78 // Add the template to the processed templates and update the session data
79
80
81 return $results;
82 }); // , null, true
83
84 return $results;
85 }
86
87 private function import_template( $id, $template_settings, $template_content ) {
88 $type = $template_settings['type'];
89
90 /**
91 * @var BaseTemplate $template
92 */
93
94 $post_data = [
95 'post_title' => $template_settings['title'] ?? ucfirst( $type ) . ' - (by Templately)',
96 'post_status' => 'publish',
97 'post_type' => 'templately_library',
98 ];
99
100 $meta = [];
101 $result = [];
102
103 if ( $this->manifest['platform'] == 'gutenberg' ) {
104 $meta['_wp_page_template'] = PageTemplates::TEMPLATE_HEADER_FOOTER;
105 }
106
107 $template = $this->factory->create( $type, $post_data, $meta );
108
109 if ( is_wp_error( $template ) ) {
110 return false;
111 }
112 if (!$template->is_elementor_template()) {
113 $attachments = $this->json->parse_images($template_content['content']);
114
115 if (!empty($attachments)) {
116 $result['__attachments'] = $attachments;
117 // $manifest_content = &$this->manifest['templates'][$id];
118 // if(!isset($manifest_content['__attachments'])){
119 // $manifest_content['__attachments'] = [];
120 // }
121 // $manifest_content['__attachments'] = $attachments;
122 }
123 }
124
125 $template_content['id'] = $id;
126 unset($template_settings['conditions']);
127 $template_content['import_settings'] = $template_settings;
128
129 if($this->platform === 'elementor' && $this->is_ai_content($id)){
130 $template_content['content'] = [];
131 }
132 else if($this->platform === 'gutenberg' && $this->is_ai_content($id)){
133 $template_content['content'] = '';
134 }
135
136 $template->import( $template_content );
137
138 if($template->has_logo($template_content)){
139 $this->manifest['templates'][$id]['has_logo'] = true;
140 }
141 $result['id'] = $template->get_main_id();
142 return $result;
143 }
144
145
146 /**
147 * @param $template_settings
148 * @param $platform
149 *
150 * @return false|int
151 */
152 private function create_archive_page( $template_settings, $platform ) {
153 try {
154 $archive_page_id = $template_settings['page_settings']['archive_page_id'] ?? null;
155 if($archive_page_id && !empty($this->manifest['content']['page'][$archive_page_id])){
156 return false;
157 }
158
159 $type = $template_settings['type'];
160
161 $archive_page = wp_insert_post( [
162 'post_title' => $template_settings['title'] ?? ucfirst( $type ) . ' - (by Templately)',
163 'post_status' => 'publish',
164 'post_type' => 'page',
165 'post_content' => '',
166 'page_template' => $platform === 'elementor' ? 'elementor_header_footer' : PageTemplates::TEMPLATE_HEADER_FOOTER,
167 ] );
168
169 if ( is_wp_error( $archive_page ) ) {
170 return false;
171 }
172
173 if($type === 'archive'){
174 Utils::update_option( 'page_for_posts', $archive_page );
175 }
176
177 if($type === 'product_archive'){
178 Utils::update_option( 'woocommerce_shop_page_id', $archive_page );
179 }
180 return $archive_page;
181 } catch ( \Exception $e ) {
182 return false;
183 }
184 }
185
186 /**
187 * Only for gutenberg create a single page template.
188 * @return void
189 */
190 private function create_page_template() {
191 try {
192 $meta = [];
193 $data = [];
194
195 $post_data = [
196 'post_title' => 'Single Page - (by Templately)',
197 'post_status' => 'publish',
198 'post_type' => 'templately_library',
199 ];
200
201 if ( $this->manifest['platform'] == 'elementor' ) {
202 $meta['_wp_page_template'] = 'elementor_header_footer';
203 $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);
204 } elseif ( $this->manifest['platform'] == 'gutenberg' ) {
205 $meta['_wp_page_template'] = PageTemplates::TEMPLATE_HEADER_FOOTER;
206 $data = ['content' => '<!-- wp:post-content /-->'];
207 }
208
209 $template = $this->factory->create( 'page_single', $post_data, $meta );
210 $template->import( $data );
211
212 // return $archive_page;
213 } catch ( \Exception $e ) {
214 return;
215 }
216 }
217 }