Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
33 changes: 17 additions & 16 deletions MultisiteLanguageSwitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
define( 'MSLS_PLUGIN_PATH', plugin_basename( __FILE__ ) );
define( 'MSLS_PLUGIN__FILE__', __FILE__ );

require_once __DIR__ . '/includes/aliases.php';
require_once __DIR__ . '/includes/deprectated.php';

/**
Expand Down Expand Up @@ -93,7 +94,7 @@ function msls_the_switcher( array $arr = array() ): void {
* @return string
*/
function msls_get_flag_url( string $locale ): string {
return ( new \lloc\Msls\MslsOptions() )->get_flag_url( $locale );
return ( new \lloc\Msls\Options\Options() )->get_flag_url( $locale );
}

/**
Expand Down Expand Up @@ -123,7 +124,7 @@ function msls_get_permalink( string $locale, string $preset = '' ): string {
$blog = msls_blog( $locale );

if ( $blog ) {
$options = \lloc\Msls\MslsOptions::create();
$options = \lloc\Msls\Options\Options::create();
$url = $blog->get_url( $options );
}

Expand Down Expand Up @@ -151,12 +152,12 @@ function msls_blog_collection(): \lloc\Msls\MslsBlogCollection {
}

/**
* Gets the MslsOptions instance
* Gets the Options instance
*
* @return \lloc\Msls\MslsOptions
* @return \lloc\Msls\Options\Options
*/
function msls_options(): \lloc\Msls\MslsOptions {
return \lloc\Msls\MslsOptions::instance();
function msls_options(): \lloc\Msls\Options\Options {
return \lloc\Msls\Options\Options::instance();
}

/**
Expand Down Expand Up @@ -196,17 +197,17 @@ function msls_output(): \lloc\Msls\MslsOutput {
}

/**
* Retrieves the MslsOptionsPost instance.
* Retrieves the OptionsPost instance.
*
* @param int $id
* @return \lloc\Msls\MslsOptionsPost
* @return \lloc\Msls\Options\OptionsPost
*/
function msls_get_post( int $id ): \lloc\Msls\MslsOptionsPost {
return new \lloc\Msls\MslsOptionsPost( $id );
function msls_get_post( int $id ): \lloc\Msls\Options\OptionsPost {
return new \lloc\Msls\Options\OptionsPost( $id );
}

/**
* Retrieves the MslsOptionsTax instance.
* Retrieves the OptionsTax instance.
*
* Determines the current query based on conditional tags:
* - is_category
Expand All @@ -217,11 +218,11 @@ function msls_get_post( int $id ): \lloc\Msls\MslsOptionsPost {
* @return \lloc\Msls\OptionsTaxInterface
*/
function msls_get_tax( int $id ): \lloc\Msls\OptionsTaxInterface {
return \lloc\Msls\MslsOptionsTax::create( $id );
return \lloc\Msls\Options\OptionsTax::create( $id );
}

/**
* Retrieves the MslsOptionsQuery instance.
* Retrieves the OptionsQuery instance.
*
* Determines the current query based on conditional tags:
* - is_day
Expand All @@ -230,10 +231,10 @@ function msls_get_tax( int $id ): \lloc\Msls\OptionsTaxInterface {
* - is_author
* - is_post_type_archive
*
* @return ?\lloc\Msls\MslsOptionsQuery
* @return ?\lloc\Msls\Options\OptionsQuery
*/
function msls_get_query(): ?\lloc\Msls\MslsOptionsQuery {
return \lloc\Msls\MslsOptionsQuery::create();
function msls_get_query(): ?\lloc\Msls\Options\OptionsQuery {
return \lloc\Msls\Options\OptionsQuery::create();
}

/**
Expand Down
66 changes: 35 additions & 31 deletions includes/ContentImport/ContentImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use lloc\Msls\ContentImport\Importers\WithRequestPostAttributes;
use lloc\Msls\MslsBlogCollection;
use lloc\Msls\MslsMain;
use lloc\Msls\MslsOptionsPost;
use lloc\Msls\Options\OptionsPost;
use lloc\Msls\MslsRegistryInstance;
use lloc\Msls\MslsRequest;

Expand Down 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( OptionsPost::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
2 changes: 1 addition & 1 deletion includes/ContentImport/Importers/AttachmentsImporters.php
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'] ?? '',
)
);
if ( isset( $found[0]->ID ) ) {
Expand Down
Loading