Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
Allow to remove all terms of a remote post. #154
Browse files Browse the repository at this point in the history
  • Loading branch information
tfrommen committed Aug 24, 2015
1 parent 3e2d2ec commit 203609d
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions inc/advanced-translator/Mlp_Advanced_Translator_Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,19 @@ public function save( $post_id, WP_Post $post ) {
}

$available_blogs = get_site_option( 'inpsyde_multilingual' );

if ( empty( $available_blogs ) ) {
return;
}

// auto-drafts
$post_id = $this->basic_data->get_real_post_id( $post_id );
$post_type = $this->basic_data->get_real_post_type( $post );

$source_blog_id = get_current_blog_id();

$thumb_data = $this->get_source_thumb_data( $post_id );
$related_blogs = $this->relations->get_related_sites( $source_blog_id, FALSE );

$related_blogs = $this->relations->get_related_sites( $source_blog_id );
if ( empty( $related_blogs ) ) {
return;
}
Expand Down Expand Up @@ -140,9 +141,9 @@ public function save( $post_id, WP_Post $post ) {
do_action( 'mlp_before_post_synchronization', $this->save_context );

foreach ( $this->post_request_data[ $this->name_base ] as $remote_blog_id => $post_data ) {

if ( ! blog_exists( $remote_blog_id )
or ! in_array( $remote_blog_id, $related_blogs )
if (
! blog_exists( $remote_blog_id )
|| ! in_array( $remote_blog_id, $related_blogs )
) {
continue;
}
Expand All @@ -154,7 +155,6 @@ public function save( $post_id, WP_Post $post ) {
$new_post = $this->create_post_to_send( $post_data, $post_type, $remote_blog_id );

if ( array() !== $new_post ) {

$sync_thumb = ! empty( $post_data[ 'thumbnail' ] );
$update = ! empty( $post_data[ 'remote_post_id' ] ) && 0 < $post_data[ 'remote_post_id' ];
$new_id = $this->sync_post( $new_post, $post_id, $remote_blog_id, $update );
Expand All @@ -167,9 +167,8 @@ public function save( $post_id, WP_Post $post ) {
$this->copy_thumb( $new_id, $thumb_data );
}

if ( ! empty( $post_data[ 'tax' ] ) ) {
$this->set_remote_tax_terms( $new_id, $post_data[ 'tax' ] );
}
$tax_data = empty( $post_data[ 'tax' ] ) ? array() : (array) $post_data[ 'tax' ];
$this->set_remote_tax_terms( $new_id, $tax_data );
}

restore_current_blog();
Expand Down Expand Up @@ -472,35 +471,31 @@ private function get_taxonomies_with_terms( WP_Post $post ) {
* @param int $new_id
* @param array $tax_data
*
* @return bool TRUE on complete success, FALSE when there were errors.
* @return bool TRUE on complete success, FALSE when there were errors.
*/
private function set_remote_tax_terms( $new_id, array $tax_data ) {

$errors = array();
$post = get_post( $new_id );
$taxonomies = get_object_taxonomies( $post, 'names' );

foreach ( $taxonomies as $taxonomy ) {
$post = get_post( $new_id );

if ( empty( $tax_data[ $taxonomy ] ) ) {
// all existing terms removed
$term_ids = array();
} else {
$term_ids = (array) $tax_data[ $taxonomy ];
$taxonomies = get_object_taxonomies( $post, 'objects' );
foreach ( $taxonomies as $taxonomy => $properties ) {
if ( ! current_user_can( $properties->cap->assign_terms, $taxonomy ) ) {
continue;
}

$terms = array();

$term_ids = empty( $tax_data[ $taxonomy ] ) ? array() : (array) $tax_data[ $taxonomy ];
foreach ( $term_ids as $term_id ) {
$term = get_term_by( 'id', (int) $term_id, $taxonomy );

if ( $term ) {
$terms[] = $term->term_id;
}
}

$set = wp_set_object_terms( $new_id, $terms, $taxonomy );

if ( is_wp_error( $set ) ) {
$errors[ $taxonomy ] = $set;
}
Expand Down

0 comments on commit 203609d

Please sign in to comment.