adapt api to list all values from core fields

This commit is contained in:
eduardohumberto 2018-03-26 23:59:49 -03:00
parent 6daccd8a31
commit 3c95559469
4 changed files with 62 additions and 1 deletions

View File

@ -248,9 +248,23 @@ class TAINACAN_REST_Controller extends WP_REST_Controller {
private function prepare_meta($mapped, $request, $query, $mapped_v, $args){
$request_meta_query = $request[$mapped];
// if the meta/date/taxquery has a root relation
if( isset( $request_meta_query['relation']) )
$args[ $mapped_v ]['relation'] = $request_meta_query['relation'];
// If is a multidimensional array (array of array)
if($this->contains_array($request_meta_query, $query)) {
foreach ( $request_meta_query as $index1 => $a ) {
// handle core field
if( array_key_exists("key", $a) ){
$field = new Tainacan\Entities\Field($a['key']);
if( strpos( $field->get_field_type(), 'Core') !== false ){
// $args[ 'post_title_in' ] = ( is_array( $a['value'] ) ) ? $a['value'] : [$a['value']];
continue;
}
}
foreach ( $query as $mapped_meta => $meta_v ) {
if ( isset( $a[ $meta_v ] ) ) {
$args[ $mapped_v ][ $index1 ][ $meta_v ] = $request[ $mapped ][ $index1 ][ $meta_v ];

View File

@ -118,7 +118,6 @@
},
mountQuery( search ){
let query = []
query['search'] = search;
if( this.searchFields.length > 0){
query['metaquery'] = [];
const metaquery = query['metaquery'];
@ -131,6 +130,8 @@
}
query['metaquery'] = metaquery;
} else {
query['search'] = search;
}
return query;
}

View File

@ -641,6 +641,30 @@ class Fields extends Repository {
// Clear the result cache
$wpdb->flush();
$field = new Entities\Field( $field_id );
// handle core titles
if( strpos( $field->get_field_type(), 'Core') !== false ){
$collection = new Entities\Collection( $collection_id );
$Tainacan_Items = \Tainacan\Repositories\Items::getInstance();
$items = $Tainacan_Items->fetch( [], $collection, 'OBJECT');
$return = [];
foreach ($items as $item) {
if( strpos( $field->get_field_type(), 'Core_Title') !== false ){
$return[] = [ 'item_id' => $item->get_id(), 'field_id' => $field_id, 'mvalue' => $item->get_title() ];
} else {
$return[] = [ 'item_id' => $item->get_id(), 'field_id' => $field_id, 'mvalue' => $item->get_description() ];
}
}
if (!empty($return)) {
$results[] = $return;
}
return $results;
}
$item_post_type = "%%{$collection_id}_item";
$collection = new Entities\Collection($collection_id);

View File

@ -25,6 +25,7 @@ class Items extends Repository {
protected function __construct()
{
parent::__construct();
add_filter( 'posts_where', array(&$this, 'title_in_posts_where'), 10, 2 );
}
public function get_map() {
@ -311,4 +312,25 @@ class Items extends Repository {
return new Entities\Item( wp_trash_post( $args[0] ) );
}
/**
* allow wp query filter post by array of titles
*
* @param $where
* @param $wp_query
* @return string
*/
public function title_in_posts_where( $where, &$wp_query ) {
global $wpdb;
if ( $post_title_in = $wp_query->get( 'post_title_in' ) ) {
if(is_array( $post_title_in )){
$quotes = [];
foreach ($post_title_in as $title) {
$quotes[] = "'" . esc_sql( $wpdb->esc_like( $title ) ). "'";
}
}
$where .= ' AND ' . $wpdb->posts . '.post_title IN ( ' .implode(',', $quotes ) . ')';
}
return $where;
}
}