Skip to content

Solution for the issue #147 and partial solution for the feature request #145 #148

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

Closed
wants to merge 11 commits into from
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
125 changes: 116 additions & 9 deletions includes/MslsCustomColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,78 @@ 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
* @param array $columns
* @return array
*/
public function th( $columns ) {

$blogs = $this->collection->get();
if ( $blogs ) {
if ( $blogs ) {
$arr = [];
foreach ( $blogs as $blog ) {
$language = $blog->get_language();
Expand All @@ -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;
Expand All @@ -76,31 +129,85 @@ 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 );

$language = $blog->get_language();

$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();
}

}
}
}
Expand Down
20 changes: 20 additions & 0 deletions includes/MslsCustomColumnTaxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 44 additions & 23 deletions includes/MslsMetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ public function render_select() {

$selects = '';
$p_object = get_post_type_object( $type );

if ( $p_object->hierarchical ) {
$args = [
'post_type' => $type,
Expand All @@ -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
*
Expand All @@ -228,7 +228,8 @@ public function render_select() {
$icon,
$selects
);



restore_current_blog();
}

Expand Down Expand Up @@ -291,7 +292,10 @@ public function render_option( $post_id, $msls_id ) {
*/
public function render_input( $echo = true ) {
$blogs = $this->collection->get();




if ( $blogs ) {
global $post;

Expand Down Expand Up @@ -406,30 +410,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;
}
}
Loading