| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace RenzoJohnson\Blocks\Transfer; |
| 6 |
|
| 7 |
\defined( 'ABSPATH' ) || exit; |
| 8 |
|
| 9 |
final class TransferService { |
| 10 |
|
| 11 |
public function __construct( |
| 12 |
private readonly TransferValidator $validator, |
| 13 |
private readonly BlockTransferRepository $blocks, |
| 14 |
) { |
| 15 |
} |
| 16 |
|
| 17 |
/** @return array<string, mixed> */ |
| 18 |
public function export_payload(): array { |
| 19 |
$items = array(); |
| 20 |
|
| 21 |
foreach ( $this->blocks->all() as $block ) { |
| 22 |
$post = $block->get_post(); |
| 23 |
$locale = \get_post_meta( (int) $block->id(), '_locale', true ); |
| 24 |
|
| 25 |
$items[] = array( |
| 26 |
'title' => (string) $block->title(), |
| 27 |
'status' => $post instanceof \WP_Post ? $post->post_status : 'publish', |
| 28 |
'locale' => \is_string( $locale ) ? $locale : '', |
| 29 |
'properties' => $block->get_properties(), |
| 30 |
'categories' => $this->blocks->term_names( (int) $block->id(), 'blocks_category' ), |
| 31 |
'tags' => $this->blocks->term_names( (int) $block->id(), 'blocks_tag' ), |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
return array( |
| 36 |
'format' => TransferValidator::FORMAT, |
| 37 |
'plugin' => 'blocks', |
| 38 |
'version' => BLOCKS_VERSION, |
| 39 |
'site' => (string) \home_url( '/' ), |
| 40 |
'exported' => \gmdate( 'c' ), |
| 41 |
'blocks' => $items, |
| 42 |
'variables' => array(), |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @param array<mixed, mixed> $payload |
| 48 |
* @return array{created: int, updated: int, skipped: int, variables: int}|\WP_Error |
| 49 |
*/ |
| 50 |
public function import_payload( array $payload, bool $overwrite ): array|\WP_Error { |
| 51 |
$validated = $this->validator->validate( $payload ); |
| 52 |
|
| 53 |
if ( \is_wp_error( $validated ) ) { |
| 54 |
return $validated; |
| 55 |
} |
| 56 |
|
| 57 |
$created = 0; |
| 58 |
$updated = 0; |
| 59 |
$skipped = 0; |
| 60 |
|
| 61 |
foreach ( $validated['blocks'] as $item ) { |
| 62 |
if ( ! \is_array( $item ) ) { |
| 63 |
return new \WP_Error( 'blocks_import_internal', \__( 'Validated block data was lost.', 'blocks' ) ); |
| 64 |
} |
| 65 |
|
| 66 |
$requested_locale = (string) $item['locale']; |
| 67 |
$effective_locale = '' !== $requested_locale ? $requested_locale : (string) \get_locale(); |
| 68 |
$existing = $this->blocks->find( (string) $item['title'], $requested_locale, $effective_locale ); |
| 69 |
|
| 70 |
if ( $existing instanceof \Blocks && ! $overwrite ) { |
| 71 |
++$skipped; |
| 72 |
continue; |
| 73 |
} |
| 74 |
|
| 75 |
$creating = ! $existing instanceof \Blocks; |
| 76 |
$block = $creating |
| 77 |
? \Blocks::get_template( |
| 78 |
array( |
| 79 |
'title' => $item['title'], |
| 80 |
'locale' => $effective_locale, |
| 81 |
) |
| 82 |
) |
| 83 |
: $existing; |
| 84 |
|
| 85 |
if ( ! $block instanceof \Blocks ) { |
| 86 |
return new \WP_Error( 'blocks_import_model', \__( 'A block could not be prepared for import.', 'blocks' ) ); |
| 87 |
} |
| 88 |
|
| 89 |
$block->set_title( (string) $item['title'] ); |
| 90 |
$block->set_properties( (array) $item['properties'] ); |
| 91 |
$block->post_status = (string) $item['status']; |
| 92 |
$post_id = $block->save(); |
| 93 |
|
| 94 |
if ( ! \is_int( $post_id ) || 0 >= $post_id ) { |
| 95 |
return new \WP_Error( 'blocks_import_save', \__( 'A block could not be saved.', 'blocks' ) ); |
| 96 |
} |
| 97 |
|
| 98 |
$this->blocks->sync_feature_indexes( $block, $post_id ); |
| 99 |
$term_result = $this->blocks->assign_terms( $post_id, (array) $item['categories'], (array) $item['tags'] ); |
| 100 |
|
| 101 |
if ( \is_wp_error( $term_result ) ) { |
| 102 |
return $term_result; |
| 103 |
} |
| 104 |
|
| 105 |
if ( $creating ) { |
| 106 |
++$created; |
| 107 |
} else { |
| 108 |
++$updated; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return array( |
| 113 |
'created' => $created, |
| 114 |
'updated' => $updated, |
| 115 |
'skipped' => $skipped, |
| 116 |
'variables' => 0, |
| 117 |
); |
| 118 |
} |
| 119 |
} |
| 120 |
|