Beginning of Bulk Edit Class
This commit is contained in:
parent
8bc9d0a34c
commit
24d4106b37
|
@ -0,0 +1,139 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tainacan;
|
||||||
|
use Tainacan\Repositories;
|
||||||
|
|
||||||
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bulk_Edit class handles bulk item edition
|
||||||
|
*/
|
||||||
|
class Bulk_Edit {
|
||||||
|
|
||||||
|
private $meta_key = '_tnc_bulk';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The ID of the current bulk edition group.
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a bulk edit object
|
||||||
|
*
|
||||||
|
* This object have an ID that identifies the group of selected items that are to be affected by the changes.
|
||||||
|
*
|
||||||
|
* When itialized it adds a postmeta to the posts of these groups so they can be easily fetched as a group in all operations.
|
||||||
|
*
|
||||||
|
* The object can be initialized in three different ways:
|
||||||
|
* 1. passing an array of Items IDs, using the items_ids params
|
||||||
|
* 2. passing a query array, that will be passed to the fetch method of items repository to create the group (in this case you also need to inform the collection id)
|
||||||
|
* 3. passing an group ID, generated by this class in a previous initialization using one of the methods above.
|
||||||
|
*
|
||||||
|
* When initializing using methods 1 or 2, controllers should then call the get_id() method and store it if they want to perform future requests that wil affect this same group of items
|
||||||
|
*
|
||||||
|
* Note: if the ID paramater is passed, other paramaters will be ignored.
|
||||||
|
*
|
||||||
|
* @param array $params {
|
||||||
|
*
|
||||||
|
* Initialization paramaters
|
||||||
|
*
|
||||||
|
* @type int $collection_id The items collection ID. Required if initializing using a query search
|
||||||
|
* @type array $query The query paramaters used to fetch items that will be part of this bulk edit group
|
||||||
|
* @type array $items_ids an array containing the IDs of items that will be part of this bulk edit group
|
||||||
|
* @type string $id The ID of the Bulk edit group.
|
||||||
|
*
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
public function __construct($params) {
|
||||||
|
|
||||||
|
if (isset($params['id']) && !empty($params['id'])) {
|
||||||
|
$this->id = $params['id'];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$id = uniqid();
|
||||||
|
$this->id = $id;
|
||||||
|
|
||||||
|
if (isset($params['query']) && is_array($params['query'])) {
|
||||||
|
|
||||||
|
if (!isset($params['collection_id']) || !is_integer($params['collection_id'])) {
|
||||||
|
throw new \Exception('Collection ID must be informed when creating a group via query');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Here we use the fetch method to parse the parameter and use WP_Query
|
||||||
|
*
|
||||||
|
* However, we add a filter so the query is not executed. We just want WP_Query to build it for us
|
||||||
|
* and then we can use it to INSERT the postmeta with the bulk group ID
|
||||||
|
*/
|
||||||
|
|
||||||
|
// this avoids wp_query to run the query. We just want to build the query
|
||||||
|
add_filter('posts_pre_query', '__return_empty_array');
|
||||||
|
|
||||||
|
// this adds the meta key and meta value to the SELECT query so it can be used directly in the INSERT below
|
||||||
|
add_filter('posts_fields_request', [&$this, 'add_fields_to_query'], 10, 2);
|
||||||
|
|
||||||
|
$itemsRepo = Repositories\Items::get_instance();
|
||||||
|
$params['query']['fields'] = 'ids';
|
||||||
|
$items_query = $itemsRepo->fetch($params['query'], $params['collection_id']);
|
||||||
|
|
||||||
|
remove_filter('posts_pre_query', '__return_empty_array');
|
||||||
|
remove_filter('posts_fields_request', [&$this, 'add_fields_to_query']);
|
||||||
|
|
||||||
|
$wpdb->query( "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) {$items_query->request}" );
|
||||||
|
|
||||||
|
return;
|
||||||
|
|
||||||
|
} elseif (isset($params['items_ids']) && is_array($params['items_ids'])) {
|
||||||
|
$items_ids = array_filter($params['items_ids'], 'is_integer');
|
||||||
|
|
||||||
|
$insert_q = '';
|
||||||
|
foreach ($items_ids as $item_id) {
|
||||||
|
$insert_q .= $wpdb->prepare( "(%d, %s, %s),", $item_id, $this->meta_key, $this->get_id() );
|
||||||
|
}
|
||||||
|
$insert_q = rtrim($insert_q, ',');
|
||||||
|
|
||||||
|
$wpdb->query( "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES $insert_q" );
|
||||||
|
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internally used to filter WP_Query and build the INSERT statement.
|
||||||
|
* Must be public becaus is registered as a filter callback
|
||||||
|
*/
|
||||||
|
public function add_fields_to_query($fields, &$wp_query) {
|
||||||
|
global $wpdb;
|
||||||
|
if ( $wp_query->get('fields') == 'ids' ) { // just to make sure we are in the right query
|
||||||
|
$fields .= $wpdb->prepare( ", %s, %s", $this->meta_key, $this->get_id() );
|
||||||
|
}
|
||||||
|
return $fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current group ID
|
||||||
|
* @return string the group ID
|
||||||
|
*/
|
||||||
|
public function get_id() {
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the number of items selected in the current bulk group
|
||||||
|
public function count_posts() {
|
||||||
|
global $wpdb;
|
||||||
|
$id = $this->get_id();
|
||||||
|
if (!empty($id)) {
|
||||||
|
return $wpdb->get_var( $wpdb->prepare("SELECT COUNT(post_id) FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s", $this->meta_key, $id) );
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,121 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tainacan\Tests;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class TestCollections
|
||||||
|
*
|
||||||
|
* @package Test_Tainacan
|
||||||
|
*/
|
||||||
|
|
||||||
|
use Tainacan\Entities;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sample test case.
|
||||||
|
*/
|
||||||
|
class BulkEdit extends TAINACAN_UnitTestCase {
|
||||||
|
|
||||||
|
public $items_ids = [];
|
||||||
|
|
||||||
|
function setUp() {
|
||||||
|
parent::setUp();
|
||||||
|
$collection = $this->tainacan_entity_factory->create_entity(
|
||||||
|
'collection',
|
||||||
|
array(
|
||||||
|
'name' => 'test_col',
|
||||||
|
'status' => 'publish'
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
$this->collection = $collection;
|
||||||
|
|
||||||
|
$metadatum = $this->tainacan_entity_factory->create_entity(
|
||||||
|
'metadatum',
|
||||||
|
array(
|
||||||
|
'name' => 'metadado',
|
||||||
|
'status' => 'publish',
|
||||||
|
'collection' => $collection,
|
||||||
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->metadatum = $metadatum;
|
||||||
|
|
||||||
|
for ($i = 1; $i<=40; $i++) {
|
||||||
|
|
||||||
|
$item = $this->tainacan_entity_factory->create_entity(
|
||||||
|
'item',
|
||||||
|
array(
|
||||||
|
'title' => 'testeItem ' . $i,
|
||||||
|
'collection' => $collection,
|
||||||
|
'status' => 'publish'
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->items_ids[] = $item->get_id();
|
||||||
|
|
||||||
|
$this->tainacan_item_metadata_factory->create_item_metadata($item, $metadatum, $i % 2 == 0 ? 'even' : 'odd');
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_setup() {
|
||||||
|
$this->assertEquals(40, sizeof($this->items_ids));
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_init_by_query() {
|
||||||
|
|
||||||
|
|
||||||
|
$query = [
|
||||||
|
'meta_query' => [
|
||||||
|
[
|
||||||
|
'key' => $this->metadatum->get_id(),
|
||||||
|
'value' => 'even'
|
||||||
|
]
|
||||||
|
],
|
||||||
|
'posts_per_page' => -1
|
||||||
|
];
|
||||||
|
|
||||||
|
$bulk = new \Tainacan\Bulk_Edit([
|
||||||
|
'query' => $query,
|
||||||
|
'collection_id' => $this->collection->get_id()
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(20, $bulk->count_posts());
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_init_by_ids() {
|
||||||
|
|
||||||
|
$ids = array_slice($this->items_ids, 2, 7);
|
||||||
|
|
||||||
|
$bulk = new \Tainacan\Bulk_Edit([
|
||||||
|
'items_ids' => $ids,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(7, $bulk->count_posts());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_init_by_bulk_id() {
|
||||||
|
|
||||||
|
$ids = array_slice($this->items_ids, 4, 11);
|
||||||
|
|
||||||
|
$bulk = new \Tainacan\Bulk_Edit([
|
||||||
|
'items_ids' => $ids,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$id = $bulk->get_id();
|
||||||
|
|
||||||
|
$newBulk = new \Tainacan\Bulk_Edit([
|
||||||
|
'id' => $id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(11, $newBulk->count_posts());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue