Bulk edit set_value support array as input

This commit is contained in:
Leo Germani 2019-07-30 15:17:54 -03:00
parent 42cddd93f6
commit a80aeff4c1
3 changed files with 197 additions and 40 deletions

View File

@ -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' ),
],
],
),

View File

@ -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,11 +458,17 @@ class Bulk_Edit {
if ($tax instanceof Entities\Taxonomy) {
$term = $taxRepo->term_exists($tax, $value, 0, true);
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($value, $tax->get_db_identifier());
$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' ) );
}
@ -474,7 +483,13 @@ class Bulk_Edit {
$query = "INSERT IGNORE INTO $wpdb->term_relationships (object_id, term_taxonomy_id) $insert_q";
return $wpdb->query($query);
$return = $wpdb->query($query);
}
return $return;
//TODO update term count
@ -485,7 +500,13 @@ class Bulk_Edit {
global $wpdb;
$insert_q = $this->_build_select( $wpdb->prepare("post_id, %s, %s", $metadatum->get_id(), $value) );
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) );
$query = "INSERT IGNORE INTO $wpdb->postmeta (post_id, meta_key, meta_value) $insert_q";
@ -499,12 +520,17 @@ class Bulk_Edit {
];
$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 );
$core_query = $wpdb->prepare( "UPDATE $wpdb->posts SET $column = %s WHERE ID IN ($update_q)", $v );
$wpdb->query($core_query);
}
return $affected;
$return = $affected;
}
return $return; // return last value
}

View File

@ -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);
}
}