| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer\Runners; |
| 4 |
|
| 5 |
use Templately\Builder\PageTemplates; |
| 6 |
use Templately\Core\Importer\Utils\Utils; |
| 7 |
use WP_Error; |
| 8 |
|
| 9 |
class GutenbergContent extends BaseRunner { |
| 10 |
|
| 11 |
public function get_name(): string { |
| 12 |
return 'content'; |
| 13 |
} |
| 14 |
|
| 15 |
public function get_label(): string { |
| 16 |
return __( 'Block Editor Content', 'templately' ); |
| 17 |
} |
| 18 |
|
| 19 |
public function should_run( $data, $imported_data = [] ): bool { |
| 20 |
return $this->platform == 'gutenberg' && ! empty( $this->manifest['content'] ); |
| 21 |
} |
| 22 |
|
| 23 |
public function should_log(): bool { |
| 24 |
return true; |
| 25 |
} |
| 26 |
|
| 27 |
public function get_action(): string { |
| 28 |
return 'eventLog'; |
| 29 |
} |
| 30 |
|
| 31 |
public function log_message(): string { |
| 32 |
return __( 'Importing Gutenberg Templates (Pages, Posts etc)', 'templately' ); |
| 33 |
} |
| 34 |
|
| 35 |
public function import( $data, $imported_data ): array { |
| 36 |
$results = []; |
| 37 |
$contents = $this->manifest['content']; |
| 38 |
$path = $this->dir_path . 'content' . DIRECTORY_SEPARATOR; |
| 39 |
|
| 40 |
$processed = 0; |
| 41 |
$total = array_reduce($contents, function($carry, $item) { |
| 42 |
return $carry + count($item); |
| 43 |
}, 0); |
| 44 |
|
| 45 |
foreach ( $contents as $type => $posts ) { |
| 46 |
foreach ( $posts as $id => $settings ) { |
| 47 |
$import = $this->import_page_content( $id, $type, $path, $settings ); |
| 48 |
|
| 49 |
if ( ! $import ) { |
| 50 |
$results[ $type ]['failed'][ $id ] = $import; |
| 51 |
} else { |
| 52 |
Utils::import_page_settings( $import, $settings ); |
| 53 |
$results[ $type ]['succeed'][ $id ] = $import; |
| 54 |
} |
| 55 |
|
| 56 |
// Broadcast Log |
| 57 |
$processed += 1; |
| 58 |
$progress = floor( ( 100 * $processed ) / $total ); |
| 59 |
$this->log( $progress, null, 'eventLog' ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return [ 'content' => $results ]; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @param $id |
| 68 |
* @param $type |
| 69 |
* @param $path |
| 70 |
* @param $settings |
| 71 |
* |
| 72 |
* @return false|int|void|WP_Error |
| 73 |
*/ |
| 74 |
private function import_page_content( $id, $type, $path, $settings ) { |
| 75 |
try { |
| 76 |
$json_content = Utils::read_json_file( $path . '/' . $type . '/' . $id . '.json' ); |
| 77 |
if ( ! empty( $json_content ) ) { |
| 78 |
|
| 79 |
/** |
| 80 |
* TODO: |
| 81 |
* |
| 82 |
* We can check if there is any data for settings. |
| 83 |
* if yes: ignore content from insert. |
| 84 |
* |
| 85 |
* Process the content while finalizing. |
| 86 |
*/ |
| 87 |
|
| 88 |
$post_data = [ |
| 89 |
'post_title' => $json_content['title'] ?? ucfirst( $type ) . ' - (by Templately)', |
| 90 |
'post_status' => 'publish', |
| 91 |
'post_type' => $type, |
| 92 |
'post_content' => wp_slash( $json_content['content'] ), |
| 93 |
'page_template' => PageTemplates::TEMPLATE_HEADER_FOOTER |
| 94 |
]; |
| 95 |
$inserted = wp_insert_post( $post_data ); |
| 96 |
|
| 97 |
if ( is_wp_error( $inserted ) ) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
return $inserted; |
| 102 |
} |
| 103 |
} catch ( \Exception $e ) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
} |
| 107 |
} |