diff --git a/src/api/endpoints/class-tainacan-rest-bulkedit-controller.php b/src/api/endpoints/class-tainacan-rest-bulkedit-controller.php index dfcbc83d5..381d2ac10 100644 --- a/src/api/endpoints/class-tainacan-rest-bulkedit-controller.php +++ b/src/api/endpoints/class-tainacan-rest-bulkedit-controller.php @@ -119,8 +119,8 @@ class REST_Bulkedit_Controller extends REST_Controller { 'description' => __( 'The metadatum ID', 'tainacan' ), ], 'value' => [ - 'type' => 'string/integer', - 'description' => __( 'The value to be added', 'tainacan' ), + 'type' => 'string/integer/array', + 'description' => __( 'The value to be set', 'tainacan' ), ], ], ), diff --git a/src/classes/class-tainacan-bulk-edit.php b/src/classes/class-tainacan-bulk-edit.php index 7209914f5..a080008f1 100644 --- a/src/classes/class-tainacan-bulk-edit.php +++ b/src/classes/class-tainacan-bulk-edit.php @@ -296,7 +296,10 @@ class Bulk_Edit { $dummyItem = new Entities\Item(); $dummyItem->set_status('publish'); $checkItemMetadata = new Entities\Item_Metadata_Entity($dummyItem, $metadatum); - $checkItemMetadata->set_value( $metadatum->is_multiple() ? [$value] : $value ); + if ( $metadatum->is_multiple() && !is_array($value)) { + $value = [$value]; + } + $checkItemMetadata->set_value( $value ); if ($checkItemMetadata->validate()) { $this->_remove_values($metadatum); @@ -455,27 +458,39 @@ class Bulk_Edit { if ($tax instanceof Entities\Taxonomy) { - $term = $taxRepo->term_exists($tax, $value, 0, true); - $term_id = false; - - if (false === $term) { - $term = wp_insert_term($value, $tax->get_db_identifier()); - if (is_WP_Error($term) || !isset($term['term_taxonomy_id'])) { - return new \WP_Error( 'error', __( 'Error adding term', 'tainacan' ) ); - } - $term_id = $term['term_taxonomy_id']; - } else { - $term_id = $term->term_taxonomy_id; + if ( !is_array($value) ) { + $value = [$value]; } + + foreach ($value as $v) { + + $term = $taxRepo->term_exists($tax, $v, 0, true); + $term_id = false; + + if (false === $term) { + $term = wp_insert_term($v, $tax->get_db_identifier()); + if (is_WP_Error($term) || !isset($term['term_taxonomy_id'])) { + return new \WP_Error( 'error', __( 'Error adding term', 'tainacan' ) ); + } + $term_id = $term['term_taxonomy_id']; + } else { + $term_id = $term->term_taxonomy_id; + } + + + + $insert_q = $this->_build_select( $wpdb->prepare("post_id, %d", $term_id) ); + + $query = "INSERT IGNORE INTO $wpdb->term_relationships (object_id, term_taxonomy_id) $insert_q"; + + $return = $wpdb->query($query); + + } + + return $return; - $insert_q = $this->_build_select( $wpdb->prepare("post_id, %d", $term_id) ); - - $query = "INSERT IGNORE INTO $wpdb->term_relationships (object_id, term_taxonomy_id) $insert_q"; - - return $wpdb->query($query); - //TODO update term count @@ -484,27 +499,38 @@ class Bulk_Edit { } else { global $wpdb; - - $insert_q = $this->_build_select( $wpdb->prepare("post_id, %s, %s", $metadatum->get_id(), $value) ); - - $query = "INSERT IGNORE INTO $wpdb->postmeta (post_id, meta_key, meta_value) $insert_q"; - - $affected = $wpdb->query($query); - - if ($type->get_core()) { - $field = $type->get_related_mapped_prop(); - $map_field = [ - 'title' => 'post_title', - 'description' => 'post_content' - ]; - $column = $map_field[$field]; - $update_q = $this->_build_select( "post_id" ); - $core_query = $wpdb->prepare( "UPDATE $wpdb->posts SET $column = %s WHERE ID IN ($update_q)", $value ); - - $wpdb->query($core_query); + + if ( !is_array($value) ) { + $value = [$value]; } + + foreach ($value as $v) { + + $insert_q = $this->_build_select( $wpdb->prepare("post_id, %s, %s", $metadatum->get_id(), $v) ); - return $affected; + $query = "INSERT IGNORE INTO $wpdb->postmeta (post_id, meta_key, meta_value) $insert_q"; + + $affected = $wpdb->query($query); + + if ($type->get_core()) { + $field = $type->get_related_mapped_prop(); + $map_field = [ + 'title' => 'post_title', + 'description' => 'post_content' + ]; + $column = $map_field[$field]; + $update_q = $this->_build_select( "post_id" ); + $core_query = $wpdb->prepare( "UPDATE $wpdb->posts SET $column = %s WHERE ID IN ($update_q)", $v ); + + $wpdb->query($core_query); + } + + $return = $affected; + + } + + return $return; // return last value + } diff --git a/tests/test-bulkedit.php b/tests/test-bulkedit.php index 55f35805a..ff58fe172 100644 --- a/tests/test-bulkedit.php +++ b/tests/test-bulkedit.php @@ -1431,7 +1431,138 @@ class BulkEdit extends TAINACAN_UnitApiTestCase { } - + function test_set_multiple_tax_meta() { + $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); + + $query = [ + 'meta_query' => [ + [ + 'key' => $this->metadatum->get_id(), + 'value' => 'even' + ] + ], + 'posts_per_page' => 5 + ]; + + $bulk = new \Tainacan\Bulk_Edit([ + 'query' => $query, + 'collection_id' => $this->collection->get_id() + ]); + + + + + $bulk->set_value($this->category, ['super', 'dooper']); + + + + + $items = $Tainacan_Items->fetch([ + 'tax_query' => [ + [ + 'taxonomy' => $this->taxonomy->get_db_identifier(), + 'field' => 'name', + 'terms' => 'bad' + ] + ], + 'posts_per_page' => -1 + ]); + + $this->assertEquals(35, $items->found_posts); + + $items = $Tainacan_Items->fetch([ + 'tax_query' => [ + [ + 'taxonomy' => $this->taxonomy->get_db_identifier(), + 'field' => 'name', + 'terms' => 'good' + ] + ], + 'posts_per_page' => -1 + ]); + + $this->assertEquals(35, $items->found_posts); + + $items = $Tainacan_Items->fetch([ + 'tax_query' => [ + [ + 'taxonomy' => $this->taxonomy->get_db_identifier(), + 'field' => 'name', + 'terms' => 'super' + ] + ], + 'posts_per_page' => -1 + ]); + + $this->assertEquals(5, $items->found_posts); + + $items = $Tainacan_Items->fetch([ + 'tax_query' => [ + [ + 'taxonomy' => $this->taxonomy->get_db_identifier(), + 'field' => 'name', + 'terms' => 'dooper' + ] + ], + 'posts_per_page' => -1 + ]); + + $this->assertEquals(5, $items->found_posts); + + + + } + + function test_set_regular_multiple_meta() { + $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); + + $query = [ + 'meta_query' => [ + [ + 'key' => $this->metadatum->get_id(), + 'value' => 'even' + ] + ], + 'posts_per_page' => 5 + ]; + + $bulk = new \Tainacan\Bulk_Edit([ + 'query' => $query, + 'collection_id' => $this->collection->get_id() + ]); + + // single valued metadatum dont accept array + $error = $bulk->set_value($this->metadatum, ['super', 'dooper']); + $this->assertTrue(is_wp_error($error)); + + $bulk->set_value($this->multiple_meta, ['super', 'dooper']); + + + $items = $Tainacan_Items->fetch([ + 'meta_query' => [ + [ + 'key' => $this->multiple_meta->get_id(), + 'value' => 'super' + ] + ], + 'posts_per_page' => -1 + ]); + + $this->assertEquals(5, $items->found_posts); + + $items = $Tainacan_Items->fetch([ + 'meta_query' => [ + [ + 'key' => $this->multiple_meta->get_id(), + 'value' => 'dooper' + ] + ], + 'posts_per_page' => -1 + ]); + + $this->assertEquals(5, $items->found_posts); + + } } \ No newline at end of file