diff --git a/includes/ContentImport/ContentImporter.php b/includes/ContentImport/ContentImporter.php index d59b14da..74329594 100644 --- a/includes/ContentImport/ContentImporter.php +++ b/includes/ContentImport/ContentImporter.php @@ -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() ); @@ -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; } @@ -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 ); @@ -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; } @@ -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 ) { @@ -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. @@ -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(); } /** diff --git a/includes/ContentImport/MetaBox.php b/includes/ContentImport/MetaBox.php index 6868261d..fdcddc04 100644 --- a/includes/ContentImport/MetaBox.php +++ b/includes/ContentImport/MetaBox.php @@ -11,6 +11,7 @@ class MetaBox extends MslsRegistryInstance { protected $data = []; + protected $language = []; /** * Renders the content import metabox. @@ -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' ); @@ -39,6 +41,7 @@ public function render() { 'multisite-language-switcher' ) . ''; foreach ( $languages as $language => $label ) { + $id = $mydata->{$language}; $blog = $blogs->get_blog_id( $language ); $label = sprintf( $label_template, $label ); @@ -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( '%s', - $this->inline_thickbox_url( $this->data ), + $this->data[] = $t_data; + $this->language[] = $language; + + $output .= sprintf( '%s
', + $this->inline_thickbox_url( $t_data, $language ), $label, $label ); @@ -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(); - ?> - - - $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(); + ?> + + + href = get_edit_post_link( $id ); - + //} return $this; } @@ -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 ); } /** diff --git a/includes/MslsCustomColumn.php b/includes/MslsCustomColumn.php index 2b5707ba..c3f26f80 100644 --- a/includes/MslsCustomColumn.php +++ b/includes/MslsCustomColumn.php @@ -28,16 +28,68 @@ public static function init() { if ( ! $options->is_excluded() ) { $post_type = MslsPostType::instance()->get_request(); - if ( ! empty( $post_type ) ) { add_filter( "manage_{$post_type}_posts_columns", [ $obj, 'th' ] ); add_action( "manage_{$post_type}_posts_custom_column", [ $obj, 'td' ], 10, 2 ); + add_action( "manage_edit-{$post_type}_sortable_columns", [ $obj, 'sortable_cols' ], 10, 2 ); + add_action( "pre_get_{$post_type}", [$obj, "orderby_translation"]); add_action( 'trashed_post', [ $obj, 'delete' ] ); } } return $obj; } + + /** + * Making stuff sortable + * @param WP_Query $query + */ + public function orderby_translation ($query) { + + $blogs = $this->collection->get(); + $orderby = $query->get('orderby'); + if ($blogs) { + // Scan data for matching language + foreach ( $blogs as $blog ) { + $language = $blog->get_language(); + $col = 'mslcol_' . $language; + if (strcmp($col, $orderby) == 0) { + //print_r('Matching language - not sure how to check existing language versions from here'); + } + } + + } + + $origin_language = MslsBlogCollection::get_blog_language(); + + if( ! is_admin() || ! $query->is_main_query() ) { + return; + } + + // Not sure how to go from here (limited dbaccess to look into records) + + } + + + /** + * Table header- Sorting + * @param array $columns + * @return array + */ + public function sortable_cols( $columns ) { + $blogs = $this->collection->get(); + if ( $blogs ) { + $blogs = $this->collection->get(); + foreach ( $blogs as $blog ) { + $language = $blog->get_language(); + $col_name = 'mslcol_' . $language; + $columns[$col_name] = $col_name; + } + } + return $columns; + } + + /** * Table header @@ -45,8 +97,9 @@ public static function init() { * @return array */ public function th( $columns ) { + $blogs = $this->collection->get(); - if ( $blogs ) { + if ( $blogs ) { $arr = []; foreach ( $blogs as $blog ) { $language = $blog->get_language(); @@ -59,9 +112,9 @@ public function th( $columns ) { $icon->set_origin_language( 'it_IT' ); } - $arr[] = $icon->get_icon(); + $sep_colname = 'mslcol_' . $blog->get_language(); + $columns[$sep_colname] = $icon->get_icon(); } - $columns['mslscol'] = implode( ' ', $arr ); } return $columns; @@ -76,11 +129,60 @@ public function th( $columns ) { * @codeCoverageIgnore */ public function td( $column_name, $item_id ) { - if ( 'mslscol' == $column_name ) { + + // Check for msl column name + $msl_pos = strpos($column_name, 'mslcol_'); + if ( $msl_pos == 0 ) { + // Get all blogs $blogs = $this->collection->get(); $origin_language = MslsBlogCollection::get_blog_language(); + + // Set original source + $ids[] = $item_id; + $langs[] = $origin_language; + + // Filter out the language + $columns_language = substr($column_name, strlen('mslcol_'), strlen($column_name)); + // print_r($columns_language); if ( $blogs ) { + + // Get interlinking between translations $mydata = MslsOptions::create( $item_id ); + foreach( $blogs as $blog) { + switch_to_blog( $blog->userblog_id ); + $lang = $blog->get_language(); + + // Set as nothing + $obj_id = null; + $term = null; + + // Handle Terms + if ($mydata instanceof MslsOptionsTaxTerm){ + + $temp = get_term( $mydata->$lang, $mydata->base ); + + if (!empty($temp) && !is_wp_error($temp)){ + $obj_id = $temp->term_id; + } + } + + // Handle Posts + if ($mydata instanceof MslsOptionsPost){ + $temp = get_post( $mydata->$lang ); + if (!empty($temp) && !is_wp_error($term)){ + $obj_id = $temp->ID; + } + } + + // Do not store empty records + if ( strcmp($blog->get_language(), $origin_language) != 0 && $obj_id != $item_id && $obj_id != null) { + $ids[] = $obj_id; + $langs[] = $blog->get_language(); + } + restore_current_blog(); + } + + // Print icons with changed links foreach ( $blogs as $blog ) { switch_to_blog( $blog->userblog_id ); @@ -88,19 +190,24 @@ public function td( $column_name, $item_id ) { $icon = MslsAdminIcon::create(); $icon->set_language( $language ); - $icon->set_id( $item_id ); - $icon->set_origin_language( $origin_language ); + $icon->set_id( $ids ); + $icon->set_origin_language( $langs ); $icon->set_icon_type( 'action' ); if ( $mydata->has_value( $language ) ) { $icon->set_href( $mydata->$language ); } - - echo $icon->get_a(); + + // Print only thye corresponding flag + if (strcmp($blog->get_language(), $columns_language) == 0 ) { + echo $icon->get_a(); + } + restore_current_blog(); } + } } } diff --git a/includes/MslsCustomColumnTaxonomy.php b/includes/MslsCustomColumnTaxonomy.php index c86986d6..2db30fd7 100644 --- a/includes/MslsCustomColumnTaxonomy.php +++ b/includes/MslsCustomColumnTaxonomy.php @@ -32,12 +32,32 @@ public static function init() { if ( ! empty( $taxonomy ) ) { add_filter( "manage_edit-{$taxonomy}_columns" , [ $obj, 'th' ] ); add_action( "manage_{$taxonomy}_custom_column" , [ $obj, 'column_default' ], -100, 3 ); + add_filter( "manage_edit-{$taxonomy}_sortable_columns", [ $obj, 'sortable_cols' ]); add_action( "delete_{$taxonomy}", [ $obj, 'delete' ] ); } } return $obj; } + + + /** + * Table header- Sorting + * @param array $columns + * @return array + */ + public function sortable_cols( $columns ) { + $blogs = $this->collection->get(); + if ( $blogs ) { + $blogs = $this->collection->get(); + foreach ( $blogs as $blog ) { + $language = $blog->get_language(); + $col_name = 'mslcol_' . $language; + $columns[$col_name] = $col_name; + } + } + return $columns; + } /** * Table body diff --git a/includes/MslsMetaBox.php b/includes/MslsMetaBox.php index c603f916..b0ae26d0 100644 --- a/includes/MslsMetaBox.php +++ b/includes/MslsMetaBox.php @@ -192,7 +192,6 @@ public function render_select() { $selects = ''; $p_object = get_post_type_object( $type ); - if ( $p_object->hierarchical ) { $args = [ 'post_type' => $type, @@ -203,6 +202,7 @@ public function render_select() { 'sort_column' => 'menu_order, post_title', 'echo' => 0, ]; + /** * Overrides the args for wp_dropdown_pages when using the HTML select in the MetaBox * @@ -215,6 +215,7 @@ public function render_select() { $selects .= wp_dropdown_pages( $args ); } else { + // Here $selects .= sprintf( '', $language, @@ -228,7 +229,8 @@ public function render_select() { $icon, $selects ); - + + restore_current_blog(); } @@ -289,9 +291,12 @@ public function render_option( $post_id, $msls_id ) { * * @param bool $echo Whether the metabox markup should be echoed to the page or not. */ - public function render_input( $echo = true ) { + public function render_input( $echo = false ) { $blogs = $this->collection->get(); + + + if ( $blogs ) { global $post; @@ -406,30 +411,47 @@ public function maybe_set_linked_post( MslsOptionsPost $mydata ) { return $mydata; } - $origin_lang = trim( $_GET['msls_lang'] ); - - if ( isset( $mydata->{$origin_lang} ) ) { - return $mydata; - } - - $origin_post_id = (int) $_GET['msls_id']; - - $origin_blog_id = $this->collection->get_blog_id( $origin_lang ); - - if ( null === $origin_blog_id ) { + // Extract all languages into arrays + $all_langs = explode(',',trim( $_GET['msls_lang'] )); + $all_ids = explode(',',trim( $_GET['msls_id'] )); + + // Check if eq sizes + if ( sizeof($all_langs) != sizeof($all_ids) ) { return $mydata; } - - switch_to_blog( $origin_blog_id ); - $origin_post = get_post( $origin_post_id ); - restore_current_blog(); - - if ( ! $origin_post instanceof \WP_Post ) { - return $mydata; + + // Loop over languages to set cross-linking between translations + foreach ($all_langs as $indx => $lang) { + + // Check if language source already exist + // if there's an existing reference - skip the post + if ( isset( $mydata->{$lang} ) ) { + continue; + } + + // Get ID for each language + $origin_post_id[$indx] = (int) $all_ids[$indx]; + $origin_blog_id[$indx] = $this->collection->get_blog_id( $lang ); + + // Skip if there's no proper record + if ( null === $origin_blog_id[$indx] ) { + continue; + } + + // Get post info for each language + switch_to_blog( $origin_blog_id ); + $origin_post = get_post( $origin_post_id[$indx] ); + restore_current_blog(); + + // Skip loop if there's no proper post + if ( ! $origin_post instanceof \WP_Post ) { + continue; + } + + // Create link between original post for the language + $mydata->{$lang} = $origin_post_id[$indx]; + } - - $mydata->{$origin_lang} = $origin_post_id; - return $mydata; } } diff --git a/includes/MslsOptionsPost.php b/includes/MslsOptionsPost.php index 625634c6..ef10231b 100644 --- a/includes/MslsOptionsPost.php +++ b/includes/MslsOptionsPost.php @@ -31,6 +31,7 @@ class MslsOptionsPost extends MslsOptions { * @return string */ public function get_postlink( $language ) { + if ( ! $this->has_value( $language ) ) { return ''; } diff --git a/includes/MslsPostTag.php b/includes/MslsPostTag.php index 6e3bea72..cc8cbd10 100644 --- a/includes/MslsPostTag.php +++ b/includes/MslsPostTag.php @@ -210,43 +210,55 @@ public function set( $term_id ) { } } - /** - * Sets the selected element in the data from the `$_GET` superglobal, if any. - * - * @param MslsOptionsTax $mydata - * - * @return MslsOptionsTax - */ + public function maybe_set_linked_term( MslsOptionsTax $mydata ) { if ( ! isset( $_GET['msls_id'], $_GET['msls_lang'] ) ) { return $mydata; } - $origin_lang = trim( $_GET['msls_lang'] ); - - if ( isset( $mydata->{$origin_lang} ) ) { - return $mydata; - } - - $origin_term_id = (int) $_GET['msls_id']; - - $origin_blog_id = $this->collection->get_blog_id( $origin_lang ); - - if ( null === $origin_blog_id ) { + // Extract all languages into arrays + $all_langs = explode(',',trim( $_GET['msls_lang'] )); + $all_ids = explode(',',trim( $_GET['msls_id'] )); + + // Check if eq sizes + if ( sizeof($all_langs) != sizeof($all_ids) ) { return $mydata; } + + // Loop over languages to set cross-linking between translations + foreach ($all_langs as $indx => $lang) { + + // Check if language source already exist + // if there's an existing reference - skip the post + if ( isset( $mydata->{$lang} ) ) { + continue; + } + + // Get ID for each language + $origin_term_id[$indx] = (int) $all_ids[$indx]; + $origin_blog_id[$indx] = $this->collection->get_blog_id( $lang ); + + // Skip if there's no proper record + if ( null === $origin_blog_id[$indx] ) { + continue; + } + + // Get post info for each language + switch_to_blog( $origin_blog_id[$indx] ); + $origin_term = get_term( $origin_term_id[$indx], $mydata->base ); + restore_current_blog(); - switch_to_blog( $origin_blog_id ); - $origin_term = get_term( $origin_term_id, $mydata->base ); - restore_current_blog(); - - if ( ! $origin_term instanceof \WP_Term ) { - return $mydata; + // Skip loop if there's no proper post + if ( ! $origin_term instanceof \WP_Term ) { + continue; + } + + // Create link between original post for the language + $mydata->{$lang} = $origin_term_id[$indx]; + } - - $mydata->{$origin_lang} = $origin_term_id; - return $mydata; } - } + + diff --git a/includes/MslsPostTagClassic.php b/includes/MslsPostTagClassic.php index 19e0e3bf..98a1ec38 100644 --- a/includes/MslsPostTagClassic.php +++ b/includes/MslsPostTagClassic.php @@ -76,6 +76,7 @@ public function print_option( MslsBlog $blog, $type, MslsOptionsTax $mydata, $it if ( $mydata->has_value( $language ) ) { $icon->set_href( $mydata->$language ); } + if ( ! empty( $terms ) ) { foreach ( $terms as $term ) { @@ -125,4 +126,6 @@ public function the_input( $tag, $title_format, $item_format ) { } return false; } + + }