Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retaining relations between multiple languages and sorting by flag #149

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
84 changes: 76 additions & 8 deletions includes/ContentImport/ContentImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,22 @@ public function handle_import( array $data = [] ) {
if ( ! $this->pre_flight_check() || false === $sources = $this->parse_sources() ) {
return $data;
}


list( $source_blog_id, $source_post_id ) = $sources;




// Handle relations between posts
$relations = [];
$relations = $this->parse_relations();


if ( $source_blog_id === get_current_blog_id() ) {
return $data;
}



$source_lang = MslsBlogCollection::get_blog_language( $source_blog_id );
$dest_blog_id = get_current_blog_id();
$dest_lang = MslsBlogCollection::get_blog_language( get_current_blog_id() );
Expand Down Expand Up @@ -127,13 +136,15 @@ public function handle_import( array $data = [] ) {

$import_coordinates->parse_importers_from_request();

$data = $this->import_content( $import_coordinates, $data );
$data = $this->import_content( $import_coordinates, $data, $relations );

if ( $this->has_created_post ) {
$this->update_inserted_blog_post_data($dest_blog_id, $dest_post_id, $data );
$this->redirect_to_blog_post( $dest_blog_id, $dest_post_id );

}

$this->redirect_to_blog_post( $dest_blog_id, $dest_post_id, $relations );

return $data;
}

Expand Down Expand Up @@ -176,6 +187,26 @@ public function parse_sources() {

return array_map( 'intval', $import_data );
}


/**
* Parses the source blog and post IDs from the $_POST array validating them.
*
* @return array|bool
*/
public function parse_relations() {
if ( ! isset( $_POST['msls_relations'] ) ) {
return false;
}

$import_rels = array_filter( explode( ',', trim( $_POST['msls_relations'] )) );

if ( count( $import_rels ) < 1 ) {
return false;
}

return $import_rels;
}

protected function get_the_blog_post_ID( $blog_id ) {
switch_to_blog( $blog_id );
Expand Down Expand Up @@ -238,7 +269,7 @@ public function handle( $handle ) {
*
* @return array An array of modified post fields.
*/
public function import_content( ImportCoordinates $import_coordinates, array $post_fields = [] ) {
public function import_content( ImportCoordinates $import_coordinates, array $post_fields = []) {
if ( ! $import_coordinates->validate() ) {
return $post_fields;
}
Expand Down Expand Up @@ -278,10 +309,13 @@ public function import_content( ImportCoordinates $import_coordinates, array $po
$this->logger = $this->logger ?: new ImportLogger( $import_coordinates );
$this->relations = $this->relations ?: new Relations( $import_coordinates );


if ( ! empty( $importers ) && is_array( $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 );

foreach ( $importers as $key => $importer ) {
Expand All @@ -305,6 +339,7 @@ public function import_content( ImportCoordinates $import_coordinates, array $po
* @param Relations $relations
*/
do_action( 'msls_content_import_after_import', $import_coordinates, $this->logger, $this->relations );


/**
* Filters the data after the import ran.
Expand Down Expand Up @@ -333,11 +368,44 @@ protected function update_inserted_blog_post_data($blog_id, $post_id, array $dat
return $data;
}

protected function redirect_to_blog_post( $dest_blog_id, $post_id ) {
protected function redirect_to_blog_post( $dest_blog_id, $post_id, array $relations = [] ) {


switch_to_blog( $dest_blog_id );
$edit_post_link = html_entity_decode( get_edit_post_link( $post_id ) );

$path = get_edit_post_link( $post_id );

// add relations to other languages
if (count($relations) > 0) {

// Add relations to other langs
foreach ($relations as $ext_rel) {
$ext_rel = explode('|', $ext_rel);
list( $relation_blog_id, $relation_post_id ) = $ext_rel;

// Check if relations are valid WP posts
switch_to_blog( $relation_blog_id );
$relation_post = get_post( $relation_post_id );
$relation_languages[] = MslsBlogCollection::get_blog_language( $relation_blog_id );
restore_current_blog();

if ( ! $relation_post instanceof \WP_Post ) {
continue;
}

// Add verified ids
$relation_ids[] = $relation_post_id;
}

if ( null !== $relation_ids && null !== $relation_languages ) {
$path = add_query_arg( [ 'msls_id' => implode(',', $relation_ids), 'msls_lang' => implode(',', $relation_languages) ], $path );
}


}

$edit_post_link = html_entity_decode( $path );
wp_redirect( $edit_post_link );
die();
}

/**
Expand Down
168 changes: 94 additions & 74 deletions includes/ContentImport/MetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

class MetaBox extends MslsRegistryInstance {
protected $data = [];
protected $language = [];

/**
* Renders the content import metabox.
Expand All @@ -30,6 +31,7 @@ public function render() {
}, array_keys( $languages ) ) );
$has_translation = count( $available ) >= 1;


if ( $has_input || $has_translation ) {
add_thickbox();
$label_template = __( 'Import content from %s', 'multisite-language-switcher' );
Expand All @@ -39,6 +41,7 @@ public function render() {
'multisite-language-switcher' )
. '</legend>';
foreach ( $languages as $language => $label ) {

$id = $mydata->{$language};
$blog = $blogs->get_blog_id( $language );
$label = sprintf( $label_template, $label );
Expand All @@ -47,11 +50,14 @@ public function render() {
$blog = $blogs->get_blog_id( $language );
}
if ( null !== $id ) {
$this->data = [
'msls_import' => "{$blog}|{$id}",
$t_data = [
'msls_import_' . $language => "{$blog}|{$id}",
];
$output .= sprintf( '<a class="button button-primary thickbox" href="%s" title="%s">%s</a>',
$this->inline_thickbox_url( $this->data ),
$this->data[] = $t_data;
$this->language[] = $language;

$output .= sprintf( '<a class="button button-primary thickbox" href="%s" title="%s">%s</a><br>',
$this->inline_thickbox_url( $t_data, $language ),
$label,
$label
);
Expand All @@ -68,89 +74,103 @@ public function render() {
echo $output;
}

protected function inline_thickbox_url( array $data = [] ) {
protected function inline_thickbox_url( array $data = [], $language ) {
$args = array_merge( [
'modal' => true,
'width' => 770, // meh, just a guess on *most* devices
'height' => 770,
'inlineId' => 'msls-import-dialog-' . str_replace( '|', '-', $data['msls_import'] ),
'inlineId' => 'msls-import-dialog-' . str_replace( '|', '-', $data['msls_import_' . $language] ),
], $data );



return esc_url(
'#TB_inline' . add_query_arg( $args, '' )
'#TB_inline_' . $language . add_query_arg( $args, '' )
);

}

public function print_modal_html() {
echo $this->inline_thickbox_html( true, $this->data );
echo $this->inline_thickbox_html( true, $this->data, $this->language );
}

protected function inline_thickbox_html( $echo = true, array $data = [] ) {
if ( ! isset( $data['msls_import'] ) ) {
return '';
}

$slug = str_replace( '|', '-', $data['msls_import'] );

ob_start();
?>
<div style="display: none;" id="msls-import-dialog-<?php echo esc_attr( $slug ) ?>">
<h3><?php esc_html_e( 'Select what should be imported and how', 'multisite-language-switcher' ) ?></h3>

<form action="<?php echo add_query_arg( [] ) ?>" method="post">

<?php wp_nonce_field( MslsPlugin::path(), 'msls_noncename' ); ?>

<?php foreach ( $data as $key => $value ) : ?>
<input type="hidden" name="<?php echo esc_attr( $key ) ?>" value="<?php echo esc_attr( $value ) ?>">
<?php endforeach; ?>

<?php /** @var ImportersFactory $factory */
foreach ( Map::instance()->factories() as $slug => $factory ) : ?>
<?php $details = $factory->details() ?>
<h4><?php echo esc_html( $details->name ) ?></h4>
<?php if ( empty( $details->importers ) ) : ?>
<p><?php esc_html_e( 'No importers available for this type of content.', 'multisite-language-switcher' ) ?></p>
<?php else: ?>
<ul>
<li>
<label>
<input type="radio" name="msls_importers[<?php echo esc_attr( $details->slug ) ?>]">
<?php esc_html_e( 'Off - Do not import this type of content in the destination post.', 'multisite-language-switcher' ) ?>
</label>
</li>
<?php foreach ( $details->importers as $importer_slug => $importer_info ) : ?>
<li>
<label>
<input type="radio" name="msls_importers[<?php echo esc_attr( $details->slug ) ?>]"
value="<?php echo esc_attr( $importer_slug ) ?>"
<?php checked( $details->selected, $importer_slug ) ?>
>
<?php echo( esc_html( sprintf( '%s - %s', $importer_info->name, $importer_info->description ) ) ) ?>
</label>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php endforeach; ?>

<div>
<input
type="submit"
class="button button-primary"
value="<?php esc_html_e( 'Import Content', 'multisite-language-switcher' ) ?>"
>
</div>
</form>
</div>

<?php
$html = ob_get_clean();
protected function inline_thickbox_html( $echo = true, array $data_arr = [], $language_arr = [] ) {


// Attach information about other existing translations to the form
$postmeta = array_reduce($data_arr, 'array_merge', array());
//$site_ids = implode(',', $language_ar);
$site_langs = implode(',', $postmeta);

foreach ( $language_arr as $idx => $language ) {
$data = $data_arr[$idx];

if ( ! isset( $data['msls_import_' . $language] ) ) {
return '';
}

if ( $echo ) {
echo $html;
$slug = str_replace( '|', '-', $data['msls_import_' . $language] );
ob_start();
?>
<div style="display: none;" id="msls-import-dialog-<?php echo esc_attr( $slug ) ?>">
<h3><?php esc_html_e( 'Select what should be imported and how', 'multisite-language-switcher' ) ?></h3>

<form action="<?php echo add_query_arg( [] ) ?>" method="post">
<?php wp_nonce_field( MslsPlugin::path(), 'msls_noncename' ); ?>

<?php foreach ( $data as $key => $value ) : ?>
<input type="hidden" name="msls_import" value="<?php echo esc_attr( $value ) ?>">
<?php endforeach; ?>

<?php /* Data about other translation links */ ?>
<input type="hidden" name="msls_relations" value="<?php echo esc_attr( $site_langs ) ?>">


<?php /** @var ImportersFactory $factory */
foreach ( Map::instance()->factories() as $slug => $factory ) : ?>
<?php $details = $factory->details() ?>
<h4><?php echo esc_html( $details->name ) ?></h4>
<?php if ( empty( $details->importers ) ) : ?>
<p><?php esc_html_e( 'No importers available for this type of content.', 'multisite-language-switcher' ) ?></p>
<?php else: ?>
<ul>
<li>
<label>
<input type="radio" name="msls_importers[<?php echo esc_attr( $details->slug ) ?>]">
<?php esc_html_e( 'Off - Do not import this type of content in the destination post.', 'multisite-language-switcher' ) ?>
</label>
</li>
<?php foreach ( $details->importers as $importer_slug => $importer_info ) : ?>
<li>
<label>
<input type="radio" name="msls_importers[<?php echo esc_attr( $details->slug ) ?>]"
value="<?php echo esc_attr( $importer_slug ) ?>"
<?php checked( $details->selected, $importer_slug ) ?>
>
<?php echo( esc_html( sprintf( '%s - %s', $importer_info->name, $importer_info->description ) ) ) ?>
</label>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php endforeach; ?>

<div>
<input
type="submit"
class="button button-primary"
value="<?php esc_html_e( 'Import Content', 'multisite-language-switcher' ) ?>"
>
</div>
</form>
</div>

<?php
$html = ob_get_clean();

if ( $echo ) {
echo $html;
}
}

return $html;
//return $html;
}
}
5 changes: 3 additions & 2 deletions includes/MslsAdminIcon.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ public function set_src( $src ) {
* @return MslsAdminIcon
*/
public function set_href( $id ) {
//foreach ($id as $idx) {
$this->href = get_edit_post_link( $id );

//}
return $this;
}

Expand Down Expand Up @@ -246,7 +247,7 @@ public function get_edit_new() {
$path = $this->path;

if ( null !== $this->id && null !== $this->origin_language ) {
$path = add_query_arg( [ 'msls_id' => $this->id, 'msls_lang' => $this->origin_language ], $this->path );
$path = add_query_arg( [ 'msls_id' => implode(',', $this->id), 'msls_lang' => implode(',', $this->origin_language) ], $this->path );
}

/**
Expand Down
Loading