| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Modules\FullSiteImport\Runners; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Templately\Modules\ThemeBuilder\PageTemplates; |
| 7 |
use Templately\Modules\ThemeBuilder\Types\BaseTemplate; |
| 8 |
use Templately\Modules\FullSiteImport\Exception\FatalErrorException; |
| 9 |
use Templately\Modules\FullSiteImport\Utils\Utils; |
| 10 |
use Templately\Utils\Helper; |
| 11 |
|
| 12 |
class Templates extends BaseRunner { |
| 13 |
protected $imported_types = []; |
| 14 |
|
| 15 |
public function get_name(): string { |
| 16 |
return 'templates'; |
| 17 |
} |
| 18 |
|
| 19 |
public function get_label(): string { |
| 20 |
return __( 'Templates', 'templately' ); |
| 21 |
} |
| 22 |
|
| 23 |
public function log_message(): string { |
| 24 |
return __( 'Importing Templates (i.e: Header, Footer, etc)', 'templately' ); |
| 25 |
} |
| 26 |
|
| 27 |
public function should_run( $data, $imported_data = [] ): bool { |
| 28 |
return ! empty( $this->manifest['templates'] ); |
| 29 |
// return isset( $data['templates'] ) && $data['templates'] && ! empty( $this->manifest['templates'] ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @throws Exception |
| 34 |
*/ |
| 35 |
public function import( $data, $imported_data ): array { |
| 36 |
$results = $data["imported_data"]["templates"] ?? []; |
| 37 |
$templates = $this->manifest['templates']; |
| 38 |
$path = $this->dir_path . 'templates' . DIRECTORY_SEPARATOR; |
| 39 |
|
| 40 |
// Importing Elementor templates requires Elementor to be loaded; without it the |
| 41 |
// template factory and the document import below hit an uncatchable "Class not |
| 42 |
// found" fatal. Fail fast with a message the user can act on instead. |
| 43 |
// |
| 44 |
// `Import::run()` now preflights the same condition for EVERY import via |
| 45 |
// `Dependencies::verify_platform_available()`, so in the normal pipeline this |
| 46 |
// is unreachable. Kept as a local guard because it is two class_exists calls |
| 47 |
// and this runner is the actual site of the fatal — if a future caller ever |
| 48 |
// drives a runner without going through Import::run(), this is what stops it. |
| 49 |
if ( $this->manifest['platform'] === 'elementor' && ! class_exists( '\\Elementor\\Core\\Base\\Document' ) ) { |
| 50 |
throw new FatalErrorException( __( 'Elementor is required to import this template pack, but it is not active. Please install and activate Elementor, then try again.', 'templately' ) ); |
| 51 |
} |
| 52 |
|
| 53 |
if ( $this->manifest['platform'] === 'elementor' ) { |
| 54 |
$this->json->set_source_url( $this->manifest['site'] ?? '' ); |
| 55 |
} |
| 56 |
|
| 57 |
$_extra_pages = []; |
| 58 |
|
| 59 |
|
| 60 |
$total = count( $templates ); |
| 61 |
|
| 62 |
$results = $this->loop( $templates, function($id, $template_settings, $results ) use( $path, $total ) { |
| 63 |
$template_content = Utils::read_json_file( $path . $id . '.json' ); |
| 64 |
|
| 65 |
$import = $this->import_template( $id, $template_settings, $template_content ); |
| 66 |
if ( $import ) { |
| 67 |
$results['templates']['succeed'][ $id ] = $import['id']; |
| 68 |
$results['templates']['template_types'][] = $template_settings['type']; |
| 69 |
|
| 70 |
if ( ! empty( $import['__link_rewrites'] ) ) { |
| 71 |
$results['templates']['__link_rewrites'][ $id ] = true; |
| 72 |
} |
| 73 |
|
| 74 |
if ( $template_settings['type'] === 'archive' || $template_settings['type'] === 'product_archive' || $template_settings['type'] === 'course_archive' ) { |
| 75 |
$page_id = Utils::create_archive_page( $template_settings, $this->manifest['platform'], $this->manifest ); |
| 76 |
if ( $page_id && isset($template_settings['page_settings']['archive_page_id']) ) { |
| 77 |
$archive_page_id = $template_settings['page_settings']['archive_page_id']; |
| 78 |
$results['archive_settings'][$archive_page_id] = [ |
| 79 |
'old_id' => $id, |
| 80 |
'page_id' => $page_id, |
| 81 |
'archive_id' => $archive_page_id |
| 82 |
]; |
| 83 |
} |
| 84 |
} |
| 85 |
else if($template_settings['type'] === 'fluent_product_single'){ |
| 86 |
Utils::create_page_template( $this->manifest['platform'] ); |
| 87 |
} |
| 88 |
|
| 89 |
} else { |
| 90 |
$results['templates']['failed'][ $id ] = $import; |
| 91 |
} |
| 92 |
|
| 93 |
// Broadcast Log |
| 94 |
$processed = 0; |
| 95 |
$processed_template = array_merge($results['templates']['succeed'] ?? [], $results['templates']['failed'] ?? []); |
| 96 |
array_walk_recursive($processed_template, function($item, $key) use (&$processed) { |
| 97 |
$processed++; |
| 98 |
}); |
| 99 |
$progress = floor( ( 100 * $processed ) / $total ); |
| 100 |
$this->log( $progress ); |
| 101 |
|
| 102 |
$results['templates']['__attachments'][ $id ] = isset($import['__attachments']) ? $import['__attachments'] : []; |
| 103 |
// Add the template to the processed templates and update the session data |
| 104 |
|
| 105 |
|
| 106 |
return $results; |
| 107 |
}); // , null, true |
| 108 |
|
| 109 |
return $results; |
| 110 |
} |
| 111 |
|
| 112 |
private function import_template( $id, $template_settings, $template_content ) { |
| 113 |
$type = $template_settings['type']; |
| 114 |
|
| 115 |
/** |
| 116 |
* @var BaseTemplate $template |
| 117 |
*/ |
| 118 |
|
| 119 |
$post_data = [ |
| 120 |
'post_title' => $template_settings['title'] ?? ucfirst( $type ) . ' - (by Templately)', |
| 121 |
'post_status' => 'publish', |
| 122 |
'post_type' => 'templately_library', |
| 123 |
]; |
| 124 |
|
| 125 |
$meta = []; |
| 126 |
$result = []; |
| 127 |
|
| 128 |
if ( $this->manifest['platform'] == 'gutenberg' ) { |
| 129 |
$meta['_wp_page_template'] = PageTemplates::TEMPLATE_HEADER_FOOTER; |
| 130 |
} |
| 131 |
|
| 132 |
$template = $this->factory->create( $type, $post_data, $meta ); |
| 133 |
|
| 134 |
if ( is_wp_error( $template ) ) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
if (!$template->is_elementor_template()) { |
| 138 |
$attachments = $this->json->parse_images($template_content['content']); |
| 139 |
|
| 140 |
if (!empty($attachments)) { |
| 141 |
$result['__attachments'] = $attachments; |
| 142 |
// $manifest_content = &$this->manifest['templates'][$id]; |
| 143 |
// if(!isset($manifest_content['__attachments'])){ |
| 144 |
// $manifest_content['__attachments'] = []; |
| 145 |
// } |
| 146 |
// $manifest_content['__attachments'] = $attachments; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
$template_content['id'] = $id; |
| 151 |
unset($template_settings['conditions']); |
| 152 |
$template_content['import_settings'] = $template_settings; |
| 153 |
|
| 154 |
// Detect source-site links (e.g. Mega Menu / EA Info Box / Button URLs). When |
| 155 |
// present, flag the template so the Finalizer picks it up and rewrites them to |
| 156 |
// the real imported permalinks once every page exists (see Finalizer::prepare). |
| 157 |
if ( $this->manifest['platform'] === 'elementor' && ! empty( $template_content['content'] ) ) { |
| 158 |
if ( $this->json->replace_source_urls( $template_content['content'] ) > 0 ) { |
| 159 |
$result['__link_rewrites'] = true; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if($this->platform === 'elementor' && $this->is_ai_content($id)){ |
| 164 |
$template_content['content'] = []; |
| 165 |
} |
| 166 |
else if($this->platform === 'gutenberg' && $this->is_ai_content($id)){ |
| 167 |
$template_content['content'] = ''; |
| 168 |
} |
| 169 |
|
| 170 |
// While importing an Elementor document, Elementor builds each widget's control |
| 171 |
// stack and fires its `register_controls` hook. An outdated add-on whose callback |
| 172 |
// references a class/constant removed from newer Elementor (Essential Addons' |
| 173 |
// `Elementor\Core\Schemes\Typography`, a `Controls_Manager::ALERT` constant) |
| 174 |
// throws a PHP \Error there — which `catch ( Exception )` cannot contain, so ONE |
| 175 |
// broken widget aborted the entire Full Site Import. |
| 176 |
// |
| 177 |
// Contained to this template: the rest of the pack still imports. Deliberately NOT |
| 178 |
// a SkippableErrorException — that is only honoured when the opt-in |
| 179 |
// `templately_enable_fsi_skip_on_error` option is set (Loop::_is_skip_feature_enabled, |
| 180 |
// default false), so on a default install throwing it would re-abort the import and |
| 181 |
// leave users worse off than this fix found them. The failure is instead surfaced on |
| 182 |
// the SSE stream so it is visible in the import log rather than silent. |
| 183 |
try { |
| 184 |
$template->import( $template_content ); |
| 185 |
} catch ( \Error $e ) { |
| 186 |
$label = $this->manifest['templates'][ $id ]['name'] ?? $id; |
| 187 |
|
| 188 |
Helper::log( |
| 189 |
sprintf( 'Elementor document import failed for template #%s: %s', $template->get_main_id(), $e->getMessage() ), |
| 190 |
'fsi', |
| 191 |
'error' |
| 192 |
); |
| 193 |
|
| 194 |
$this->log( |
| 195 |
0, |
| 196 |
sprintf( |
| 197 |
/* translators: %s: template name */ |
| 198 |
__( 'Skipped "%s" — a plugin conflict stopped this template from importing. The rest of the pack was imported.', 'templately' ), |
| 199 |
$label |
| 200 |
), |
| 201 |
'eventLog' |
| 202 |
); |
| 203 |
|
| 204 |
$result['__import_failed'] = true; |
| 205 |
} |
| 206 |
|
| 207 |
if($template->has_logo($template_content)){ |
| 208 |
$this->manifest['templates'][$id]['has_logo'] = true; |
| 209 |
} |
| 210 |
$result['id'] = $template->get_main_id(); |
| 211 |
return $result; |
| 212 |
} |
| 213 |
} |