| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer\Runners; |
| 4 |
|
| 5 |
|
| 6 |
use Exception; |
| 7 |
use Templately\Core\Importer\Utils\Utils; |
| 8 |
|
| 9 |
class Finalizer extends BaseRunner { |
| 10 |
private $options = []; |
| 11 |
private $type_to_check = [ 'templates', 'content' ]; |
| 12 |
private $imported_data; |
| 13 |
|
| 14 |
private $type = ''; |
| 15 |
private $sub_type = ''; |
| 16 |
private $extra_content; |
| 17 |
|
| 18 |
private $total_counts = 0; |
| 19 |
private $processed_items = 0; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var array|mixed |
| 23 |
*/ |
| 24 |
protected $map_post_ids = []; |
| 25 |
/** |
| 26 |
* @var array|mixed |
| 27 |
*/ |
| 28 |
protected $map_term_ids = []; |
| 29 |
|
| 30 |
public function get_name(): string { |
| 31 |
return 'finalize'; |
| 32 |
} |
| 33 |
|
| 34 |
public function get_label(): string { |
| 35 |
return __( 'Finalizing Your Imports', 'templately' ); |
| 36 |
} |
| 37 |
|
| 38 |
public function log_message(): string { |
| 39 |
return __( 'Finalizing Your Imports', 'templately' ); |
| 40 |
} |
| 41 |
|
| 42 |
public function should_run( $data, $imported_data = [] ): bool { |
| 43 |
$data = []; |
| 44 |
|
| 45 |
foreach ( $this->type_to_check as $type ) { |
| 46 |
$contents = ! empty ( $this->manifest[ $type ] ) ? $this->manifest[ $type ] : []; |
| 47 |
if ( $type == 'templates' ) { |
| 48 |
$this->prepare( $data, $contents, $type ); |
| 49 |
} else { |
| 50 |
foreach ( $contents as $post_type => $templates ) { |
| 51 |
$this->prepare( $data, $templates, $type, $post_type ); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
$this->options = &$data; |
| 56 |
|
| 57 |
return ! empty( $data ) || $this->platform == 'gutenberg'; |
| 58 |
} |
| 59 |
|
| 60 |
private function prepare( &$data, $templates, $type, $sub_type = null ) { |
| 61 |
if ( empty( $templates ) || ! is_array( $templates ) ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
foreach ( $templates as $id => $template ) { |
| 65 |
if ( ! isset( $template['data'] ) && !isset( $template['__attachments']) ) { |
| 66 |
continue; |
| 67 |
} |
| 68 |
|
| 69 |
// if ( ! isset( $template['data']['form'] ) && ! isset( $template['data']['nav_menus'] )) { |
| 70 |
// continue; |
| 71 |
// } |
| 72 |
|
| 73 |
$this->total_counts += 1; |
| 74 |
|
| 75 |
if ( $sub_type ) { |
| 76 |
$data[ $type ][ $sub_type ][ $id ] = $template; |
| 77 |
} else { |
| 78 |
$data[ $type ][ $id ] = $template; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public function import( $data, $imported_data ): array { |
| 84 |
$this->imported_data = &$imported_data; |
| 85 |
|
| 86 |
$this->json->imported_data = $this->imported_data; |
| 87 |
$this->json->map_post_ids = Utils::map_old_new_post_ids( $this->imported_data ); |
| 88 |
$this->json->map_term_ids = Utils::map_old_new_term_ids( $this->imported_data ); |
| 89 |
if ( ! empty( $imported_data['extra-content'] ) ) { |
| 90 |
$this->extra_content = $imported_data['extra-content']; |
| 91 |
} |
| 92 |
|
| 93 |
$this->log( 0 ); |
| 94 |
add_action('templately_import.finalize_gutenberg_attachment', [$this, 'post_log'], 10, 2); |
| 95 |
|
| 96 |
foreach ( $this->options as $type => $contents ) { |
| 97 |
$this->type = $type; |
| 98 |
if ( $type == 'templates' ) { |
| 99 |
$this->finalize_imports( $contents ); |
| 100 |
} else { |
| 101 |
foreach ( $contents as $post_type => $templates ) { |
| 102 |
$this->sub_type = $post_type; |
| 103 |
$this->finalize_imports( $templates ); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if ( $this->platform == 'gutenberg' ) { |
| 109 |
$this->regenerate_assets(); |
| 110 |
} |
| 111 |
|
| 112 |
return []; |
| 113 |
} |
| 114 |
|
| 115 |
private function regenerate_assets() { |
| 116 |
$upload_dir = wp_upload_dir(); |
| 117 |
if ( is_dir( $upload_dir['basedir'] . '/eb-style/' ) ) { |
| 118 |
array_map( 'unlink', glob( $upload_dir['basedir'] . '/eb-style/*.min.css' ) ); |
| 119 |
rmdir( $upload_dir['basedir'] . '/eb-style/' ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
private function finalize_imports( $templates ) { |
| 124 |
foreach ( $templates as $old_template_id => $template_settings ) { |
| 125 |
try { |
| 126 |
$path = $this->dir_path . $this->type . DIRECTORY_SEPARATOR; |
| 127 |
if ( ! empty( $this->sub_type ) ) { |
| 128 |
$path .= $this->sub_type . DIRECTORY_SEPARATOR; |
| 129 |
} |
| 130 |
$path .= "{$old_template_id}.json"; |
| 131 |
$template_json = Utils::read_json_file( $path ); |
| 132 |
$this->json->prepare( $template_json, $template_settings, $this->extra_content['form'][ $old_template_id ] ?? [] )->update(); |
| 133 |
|
| 134 |
// Broadcast Log |
| 135 |
$this->processed_items += 1; |
| 136 |
$progress = floor( ( 100 * $this->processed_items ) / $this->total_counts ); |
| 137 |
$this->log( $progress ); |
| 138 |
} catch ( Exception $e ) { |
| 139 |
continue; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
public function post_log($id, $size_dimension = null){ |
| 145 |
$this->log(-1, "Imported attachment: $id" . ( $size_dimension ? " - $size_dimension" : ''), 'eventLog'); |
| 146 |
} |
| 147 |
} |