PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.6.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.6.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 3.1.10 All 111 releases
templately / includes / Core / Importer / Runners / Templates.php

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

236 lines 7.3 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 else if($template_settings['type'] === 'fluent_product_single'){
64 $this->create_page_template();
65 }
66
67 } else {
68 $results['templates']['failed'][ $id ] = $import;
69 }
70
71 // 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 });
77 $progress = floor( ( 100 * $processed ) / $total );
78 $this->log( $progress );
79
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;
88 }
89
90 private function import_template( $id, $template_settings, $template_content ) {
91 $type = $template_settings['type'];
92
93 /**
94 * @var BaseTemplate $template
95 */
96
97 $post_data = [
98 'post_title' => $template_settings['title'] ?? ucfirst( $type ) . ' - (by Templately)',
99 'post_status' => 'publish',
100 'post_type' => 'templately_library',
101 ];
102
103 $meta = [];
104 $result = [];
105
106 if ( $this->manifest['platform'] == 'gutenberg' ) {
107 $meta['_wp_page_template'] = PageTemplates::TEMPLATE_HEADER_FOOTER;
108 }
109
110 $template = $this->factory->create( $type, $post_data, $meta );
111
112 if ( is_wp_error( $template ) ) {
113 return false;
114 }
115 if (!$template->is_elementor_template()) {
116 $attachments = $this->json->parse_images($template_content['content']);
117
118 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;
125 }
126 }
127
128 $template_content['id'] = $id;
129 unset($template_settings['conditions']);
130 $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 $template->import( $template_content );
140
141 if($template->has_logo($template_content)){
142 $this->manifest['templates'][$id]['has_logo'] = true;
143 }
144 $result['id'] = $template->get_main_id();
145 return $result;
146 }
147
148
149 /**
150 * @param $template_settings
151 * @param $platform
152 *
153 * @return false|int
154 */
155 private function create_archive_page( $template_settings, $platform ) {
156 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 $type = $template_settings['type'];
163
164 $archive_page = wp_insert_post( [
165 'post_title' => $template_settings['title'] ?? ucfirst( $type ) . ' - (by Templately)',
166 'post_status' => 'publish',
167 'post_type' => 'page',
168 'post_content' => '',
169 'page_template' => $platform === 'elementor' ? 'elementor_header_footer' : PageTemplates::TEMPLATE_HEADER_FOOTER,
170 ] );
171
172 if ( is_wp_error( $archive_page ) ) {
173 return false;
174 }
175
176 if($type === 'archive'){
177 Utils::update_option( 'page_for_posts', $archive_page );
178 }
179
180 if($type === 'product_archive'){
181 Utils::update_option( 'woocommerce_shop_page_id', $archive_page );
182 }
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 return $archive_page;
200 } catch ( \Exception $e ) {
201 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 }
235 }
236 }