Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions includes/ContentImport/ContentImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,12 @@ public function set_relations( Relations $relations ): void {
* @return string[] The updated, if needed, data array.
*/
public function handle_import( array $data = array() ) {
if ( ! $this->pre_flight_check() ) {
return $data;
}

$sources = $this->parse_sources();
if ( ! $this->pre_flight_check() || false === $sources ) {
if ( null === $sources ) {
return $data;
}

Expand Down Expand Up @@ -176,34 +180,34 @@ protected function pre_flight_check() {
/**
* Parses the source blog and post IDs from the $_POST array validating them.
*
* @return int[]|bool
* @return array{0: int, 1: int}|null
*/
public function parse_sources() {
public function parse_sources(): ?array {
if ( ! MslsRequest::has_var( 'msls_import' ) ) {
return false;
return null;
}

$msls_import = MslsRequest::get_var( 'msls_import' );
$import_data = array_filter( explode( '|', trim( $msls_import ) ), 'is_numeric' );
$import_data = array_values( array_filter( explode( '|', trim( $msls_import ) ), 'is_numeric' ) );

if ( count( $import_data ) !== 2 ) {
return false;
return null;
}

return array_map( 'intval', $import_data );
return array( (int) $import_data[0], (int) $import_data[1] );
}

/**
* @param int $blog_id
*
* @return int
*/
protected function get_the_blog_post_ID( $blog_id ) {
protected function get_the_blog_post_ID( $blog_id ): int {
switch_to_blog( $blog_id );

$id = get_the_ID();

if ( ! empty( $id ) ) {
if ( false !== $id && ! empty( $id ) ) {
restore_current_blog();

return $id;
Expand All @@ -226,19 +230,19 @@ protected function get_the_blog_post_ID( $blog_id ) {
* @param int $blog_id
* @param array<string, mixed> $data
*
* @return bool|int
* @return int
*/
protected function insert_blog_post( $blog_id, array $data = array() ) {
protected function insert_blog_post( $blog_id, array $data = array() ): int {
if ( empty( $data ) ) {
return false;
return 0;
}

switch_to_blog( $blog_id );

if ( ! empty( $data['post_type'] ) && ! post_type_exists( $data['post_type'] ) ) {
restore_current_blog();

return false;
return 0;
}

$this->handle( false );
Expand All @@ -249,7 +253,7 @@ protected function insert_blog_post( $blog_id, array $data = array() ) {
}
$this->handle( true );

$this->has_created_post = $post_id > 0 ? $post_id : false;
$this->has_created_post = $post_id > 0 ? $post_id : 0;

restore_current_blog();

Expand Down Expand Up @@ -319,29 +323,29 @@ public function import_content( ImportCoordinates $import_coordinates, array $po
$importers = Map::instance()->make( $import_coordinates );
}

if ( is_null( $this->get_logger() ) ) {
$this->set_logger( new ImportLogger( $import_coordinates ) );
}
$logger = $this->logger ?? new ImportLogger( $import_coordinates );
$this->set_logger( $logger );

if ( is_null( $this->get_relations() ) ) {
$this->set_relations( new Relations( $import_coordinates ) );
}
$relations = $this->relations ?? new Relations( $import_coordinates );
$this->set_relations( $relations );

if ( ! empty( $importers ) ) {
$source_post_id = $import_coordinates->source_post_id;
$dest_lang = $import_coordinates->dest_lang;
$dest_post_id = $import_coordinates->dest_post_id;
$this->relations->should_create( MslsOptionsPost::create( $source_post_id ), $dest_lang, $dest_post_id );
$relations->should_create( MslsOptionsPost::create( $source_post_id ), $dest_lang, $dest_post_id );

foreach ( $importers as $key => $importer ) {
/** @var Importer $importer */
if ( ! $importer instanceof Importer ) {
continue;
}
$post_fields = $importer->import( $post_fields );
$this->logger->merge( $importer->get_logger() );
$this->relations->merge( $importer->get_relations() );
$logger->merge( $importer->get_logger() );
$relations->merge( $importer->get_relations() );
}

$this->relations->create();
$this->logger->save();
$relations->create();
$logger->save();
}

/**
Expand All @@ -353,7 +357,7 @@ public function import_content( ImportCoordinates $import_coordinates, array $po
*
* @since TBD
*/
do_action( self::MSLS_AFTER_IMPORT_ACTION, $import_coordinates, $this->logger, $this->relations );
do_action( self::MSLS_AFTER_IMPORT_ACTION, $import_coordinates, $logger, $relations );

/**
* Filters the data after the import ran.
Expand All @@ -367,8 +371,8 @@ public function import_content( ImportCoordinates $import_coordinates, array $po
'msls_content_import_data_after_import',
$post_fields,
$import_coordinates,
$this->logger,
$this->relations
$logger,
$relations
);
}

Expand All @@ -395,7 +399,7 @@ protected function update_inserted_blog_post_data( $blog_id, $post_id, array $da
*/
protected function redirect_to_blog_post( $dest_blog_id, $post_id ) {
switch_to_blog( $dest_blog_id );
$edit_post_link = html_entity_decode( get_edit_post_link( $post_id ) );
$edit_post_link = html_entity_decode( get_edit_post_link( $post_id ) ?? '' );
wp_safe_redirect( $edit_post_link );
die();
}
Expand Down
7 changes: 7 additions & 0 deletions includes/ContentImport/ImportCoordinates.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,14 @@ public function parse_importers_from_request(): void {
}
}

if ( ! is_array( $importers ) ) {
return;
}

foreach ( $importers as $importer_type => $slug ) {
if ( ! is_string( $slug ) ) {
continue;
}
$this->set_importer_for( $importer_type, $slug );
}
}
Expand Down
16 changes: 12 additions & 4 deletions includes/ContentImport/ImportLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class ImportLogger {

/**
* @var string
* @var non-empty-string
*/
protected string $levels_delimiter = '/';

Expand Down Expand Up @@ -154,9 +154,9 @@ public function get_levels_delimiter(): string {
/**
* Sets the string that will be used to split paths into levels.
*
* @param string $levels_delimiter
* @param non-empty-string $levels_delimiter
*/
public function set_levels_delimiter( $levels_delimiter ): void {
public function set_levels_delimiter( string $levels_delimiter ): void {
$this->levels_delimiter = $levels_delimiter;
}

Expand Down Expand Up @@ -197,9 +197,17 @@ public function get_error( $where ) {
protected function get_nested_value( $where ) {
$path = $this->build_path( $where );

$data = $this->data[ array_shift( $path ) ];
$first = array_shift( $path );
if ( null === $first || ! isset( $this->data[ $first ] ) ) {
return null;
}

$data = $this->data[ $first ];

foreach ( $path as $frag ) {
if ( ! is_array( $data ) || ! isset( $data[ $frag ] ) ) {
return null;
}
$data = $data[ $frag ];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class AttachmentsImporters extends ImportersBaseFactory {
const TYPE = 'attachments';

/**
* @var array<string, string>
* @var array<string, class-string<Importer>>
*/
protected array $importers_map = array(
Linking::TYPE => Linking::class,
Expand Down
5 changes: 4 additions & 1 deletion includes/ContentImport/Importers/ImportersBaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract class ImportersBaseFactory extends MslsRegistryInstance implements Impo
const TYPE = 'none';

/**
* @var array<string, string> An array defining the slug and Importer class relationships in
* @var array<string, class-string<Importer>> An array defining the slug and Importer class relationships in
* the shape [ <slug> => <importer-class> ]
*/
protected array $importers_map = array();
Expand Down Expand Up @@ -67,6 +67,9 @@ public function make( ImportCoordinates $import_coordinates ) {

// If there is some incoherence, return the null-doing base importer.
$class = ! empty( $slug ) && isset( $map[ $slug ] ) ? $map[ $slug ] : BaseImporter::class;
if ( ! is_string( $class ) || ! is_a( $class, Importer::class, true ) ) {
$class = BaseImporter::class;
}

return new $class( $import_coordinates );
}
Expand Down
2 changes: 1 addition & 1 deletion includes/ContentImport/Importers/PostFieldsImporters.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PostFieldsImporters extends ImportersBaseFactory {
const TYPE = 'post-fields';

/**
* @var array<string, string>
* @var array<string, class-string<Importer>>
*/
protected array $importers_map = array(
Duplicating::TYPE => Duplicating::class,
Expand Down
2 changes: 1 addition & 1 deletion includes/ContentImport/Importers/PostMetaImporters.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PostMetaImporters extends ImportersBaseFactory {
const TYPE = 'post-meta';

/**
* @var array<string, string>
* @var array<string, class-string<Importer>>
*/
protected array $importers_map = array(
Duplicating::TYPE => Duplicating::class,
Expand Down
15 changes: 10 additions & 5 deletions includes/ContentImport/Importers/PostThumbnail/Linking.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,20 @@ public function import( array $data ) {
// In some instances, the folder sep. `/` might be duplicated, we de-duplicate it.
array_walk(
$source_upload_dir,
function ( &$entry ) {
$entry = str_replace( '//', '/', $entry );
function ( &$entry ): void {
if ( is_string( $entry ) ) {
$entry = str_replace( '//', '/', $entry );
}
}
);
$subdir = is_string( $source_upload_dir['subdir'] ) ? $source_upload_dir['subdir'] : '';
$path = is_string( $source_upload_dir['path'] ) ? $source_upload_dir['path'] : '';

$source_uploads_dir = untrailingslashit(
str_replace(
$source_upload_dir['subdir'],
$subdir,
'',
$source_upload_dir['path']
$path
)
);
$source_post_thumbnail_file = $source_uploads_dir . '/' . $source_post_thumbnail_meta['_wp_attached_file'];
Expand All @@ -91,7 +96,7 @@ function ( &$entry ) {
$found = get_posts(
array(
'post_type' => 'attachment',
'title' => $attachment['post_title'],
'title' => $attachment['post_title'] ?? '',
Comment thread
lloc marked this conversation as resolved.
)
);
if ( isset( $found[0]->ID ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PostThumbnailImporters extends ImportersBaseFactory {
const TYPE = 'post-thumbnail';

/**
* @var array<string, string>
* @var array<string, class-string<Importer>>
*/
protected array $importers_map = array(
Linking::TYPE => Linking::class,
Expand Down
13 changes: 9 additions & 4 deletions includes/ContentImport/Importers/Terms/ShallowDuplicating.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ public function import( array $data ) {

switch_to_blog( $source_blog_id );

$source_terms = wp_get_post_terms( $source_post_id, get_taxonomies() );
$source_terms = wp_get_post_terms( $source_post_id, get_taxonomies() );
if ( is_wp_error( $source_terms ) ) {
restore_current_blog();

return $data;
}

$source_terms_ids = wp_list_pluck( $source_terms, 'term_id' );
$msls_terms = array_combine(
$source_terms_ids,
Expand All @@ -62,7 +68,6 @@ public function import( array $data ) {

switch_to_blog( $this->import_coordinates->dest_blog_id );

/** @var \WP_Term $term */
foreach ( $source_terms as $term ) {
// is there a translation for the term in this blog?
$msls_term = $msls_terms[ $term->term_id ];
Expand All @@ -72,7 +77,7 @@ public function import( array $data ) {
$dest_term_id = $this->create_local_term( $term, $msls_term, $dest_lang );
}

if ( false === $dest_term_id ) {
if ( ! is_int( $dest_term_id ) ) {
continue;
}

Expand All @@ -82,7 +87,7 @@ public function import( array $data ) {
// While we think the term translation exists it might not, let's create it.
$dest_term_id = $this->create_local_term( $term, $msls_term, $dest_lang );

if ( false === $dest_term_id ) {
if ( ! is_int( $dest_term_id ) ) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion includes/ContentImport/Importers/TermsImporters.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TermsImporters extends ImportersBaseFactory {
const TYPE = 'terms';

/**
* @var array<string, string>
* @var array<string, class-string<Importer>>
*/
protected array $importers_map = array(
ShallowDuplicating::TYPE => ShallowDuplicating::class,
Expand Down
9 changes: 8 additions & 1 deletion includes/ContentImport/MetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ class MetaBox extends MslsRegistryInstance {
* Renders the content import metabox.
*/
public function render(): void {
$post = get_post();
$post = get_post();
if ( ! $post instanceof \WP_Post ) {
return;
}

$mydata = new MslsOptionsPost( $post->ID );
$languages = MslsOptionsPost::instance()->get_available_languages();
$current = MslsBlogCollection::get_blog_language( get_current_blog_id() );
Expand Down Expand Up @@ -185,6 +189,9 @@ protected function inline_thickbox_html( $output = true, array $data = array() )
<?php

$html = ob_get_clean();
if ( false === $html ) {
$html = '';
}

if ( $output ) {
echo wp_kses( $html, Component::get_allowed_html() );
Expand Down
Loading
Loading