set taxmeta as private when tax turns private #274
This commit is contained in:
parent
6e7bd268ee
commit
fc2898f701
|
@ -40,6 +40,8 @@ class Metadata extends Repository {
|
||||||
add_filter( 'pre_trash_post', array( &$this, 'disable_delete_core_metadata' ), 10, 2 );
|
add_filter( 'pre_trash_post', array( &$this, 'disable_delete_core_metadata' ), 10, 2 );
|
||||||
add_filter( 'pre_delete_post', array( &$this, 'force_delete_core_metadata' ), 10, 3 );
|
add_filter( 'pre_delete_post', array( &$this, 'force_delete_core_metadata' ), 10, 3 );
|
||||||
|
|
||||||
|
add_action('tainacan-insert-tainacan-taxonomy', [$this, 'hook_taxonomies_saved_as_private']);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -373,57 +375,57 @@ class Metadata extends Repository {
|
||||||
|
|
||||||
//get parent collections
|
//get parent collections
|
||||||
$parents = get_post_ancestors( $collection_id );
|
$parents = get_post_ancestors( $collection_id );
|
||||||
|
|
||||||
//insert the actual collection
|
//insert the actual collection
|
||||||
if ( is_numeric($collection_id) ) {
|
if ( is_numeric($collection_id) ) {
|
||||||
$parents[] = $collection_id;
|
$parents[] = $collection_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
//search for default metadatum
|
//search for default metadatum
|
||||||
$parents[] = $this->get_default_metadata_attribute();
|
$parents[] = $this->get_default_metadata_attribute();
|
||||||
|
|
||||||
$results = [];
|
$results = [];
|
||||||
|
|
||||||
$args = array_merge( [
|
$args = array_merge( [
|
||||||
'parent' => 0
|
'parent' => 0
|
||||||
], $args );
|
], $args );
|
||||||
|
|
||||||
$original_meta_q = isset( $args['meta_query'] ) ? $args['meta_query'] : [];
|
$original_meta_q = isset( $args['meta_query'] ) ? $args['meta_query'] : [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Since we introduced roles & capabalities management, we can not rely
|
* Since we introduced roles & capabalities management, we can not rely
|
||||||
* on WordPress behavior when handling default post status values.
|
* on WordPress behavior when handling default post status values.
|
||||||
* WordPress checks if the current user can read_priva_posts, but this is
|
* WordPress checks if the current user can read_priva_posts, but this is
|
||||||
* not enough for us. We have to handle this ourselves to mimic WordPress behavior
|
* not enough for us. We have to handle this ourselves to mimic WordPress behavior
|
||||||
* considering how tainacan manages metadata capabilities
|
* considering how tainacan manages metadata capabilities
|
||||||
*/
|
*/
|
||||||
if ( ! isset($args['post_status']) ) {
|
if ( ! isset($args['post_status']) ) {
|
||||||
|
|
||||||
foreach ( $parents as $parent_id ) {
|
foreach ( $parents as $parent_id ) {
|
||||||
|
|
||||||
// Add public states.
|
// Add public states.
|
||||||
$statuses = get_post_stati( array( 'public' => true ) );
|
$statuses = get_post_stati( array( 'public' => true ) );
|
||||||
|
|
||||||
$read_private_cap = $this->get_default_metadata_attribute() == $parent_id ? 'tnc_rep_read_private_metadata' : 'tnc_col_' . $parent_id . '_read_private_metadata';
|
$read_private_cap = $this->get_default_metadata_attribute() == $parent_id ? 'tnc_rep_read_private_metadata' : 'tnc_col_' . $parent_id . '_read_private_metadata';
|
||||||
if ( current_user_can($read_private_cap) ) {
|
if ( current_user_can($read_private_cap) ) {
|
||||||
$statuses = array_merge( $statuses, get_post_stati( array( 'private' => true ) ) );
|
$statuses = array_merge( $statuses, get_post_stati( array( 'private' => true ) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
$args['post_status'] = $statuses;
|
$args['post_status'] = $statuses;
|
||||||
|
|
||||||
$meta_query = array(
|
$meta_query = array(
|
||||||
'key' => 'collection_id',
|
'key' => 'collection_id',
|
||||||
'value' => $parent_id,
|
'value' => $parent_id,
|
||||||
);
|
);
|
||||||
|
|
||||||
$args['meta_query'] = $original_meta_q;
|
$args['meta_query'] = $original_meta_q;
|
||||||
$args['meta_query'][] = $meta_query;
|
$args['meta_query'][] = $meta_query;
|
||||||
|
|
||||||
//var_dump($args);
|
//var_dump($args);
|
||||||
$results = array_merge($results, $this->fetch( $args, 'OBJECT' ));
|
$results = array_merge($results, $this->fetch( $args, 'OBJECT' ));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$meta_query = array(
|
$meta_query = array(
|
||||||
'key' => 'collection_id',
|
'key' => 'collection_id',
|
||||||
|
@ -434,15 +436,15 @@ class Metadata extends Repository {
|
||||||
$args = array_merge( [
|
$args = array_merge( [
|
||||||
'parent' => 0
|
'parent' => 0
|
||||||
], $args );
|
], $args );
|
||||||
|
|
||||||
$args['meta_query'] = $original_meta_q;
|
$args['meta_query'] = $original_meta_q;
|
||||||
$args['meta_query'][] = $meta_query;
|
$args['meta_query'][] = $meta_query;
|
||||||
|
|
||||||
$results = $this->fetch( $args, 'OBJECT' );
|
$results = $this->fetch( $args, 'OBJECT' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return $this->order_result(
|
return $this->order_result(
|
||||||
$results,
|
$results,
|
||||||
$collection,
|
$collection,
|
||||||
|
@ -476,51 +478,51 @@ class Metadata extends Repository {
|
||||||
if ( is_numeric($collection_id) ) {
|
if ( is_numeric($collection_id) ) {
|
||||||
$parents[] = $collection_id;
|
$parents[] = $collection_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
//search for default metadatum
|
//search for default metadatum
|
||||||
$parents[] = $this->get_default_metadata_attribute();
|
$parents[] = $this->get_default_metadata_attribute();
|
||||||
|
|
||||||
$results = [];
|
$results = [];
|
||||||
|
|
||||||
$args = array_merge( [
|
$args = array_merge( [
|
||||||
'parent' => 0
|
'parent' => 0
|
||||||
], $args );
|
], $args );
|
||||||
|
|
||||||
$original_meta_q = isset( $args['meta_query'] ) ? $args['meta_query'] : [];
|
$original_meta_q = isset( $args['meta_query'] ) ? $args['meta_query'] : [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Since we introduced roles & capabalities management, we can not rely
|
* Since we introduced roles & capabalities management, we can not rely
|
||||||
* on WordPress behavior when handling default post status values.
|
* on WordPress behavior when handling default post status values.
|
||||||
* WordPress checks if the current user can read_priva_posts, but this is
|
* WordPress checks if the current user can read_priva_posts, but this is
|
||||||
* not enough for us. We have to handle this ourselves to mimic WordPress behavior
|
* not enough for us. We have to handle this ourselves to mimic WordPress behavior
|
||||||
* considering how tainacan manages metadata capabilities
|
* considering how tainacan manages metadata capabilities
|
||||||
*/
|
*/
|
||||||
if ( ! isset($args['post_status']) ) {
|
if ( ! isset($args['post_status']) ) {
|
||||||
|
|
||||||
foreach ( $parents as $parent_id ) {
|
foreach ( $parents as $parent_id ) {
|
||||||
|
|
||||||
// Add public states.
|
// Add public states.
|
||||||
$statuses = get_post_stati( array( 'public' => true ) );
|
$statuses = get_post_stati( array( 'public' => true ) );
|
||||||
|
|
||||||
$read_private_cap = $this->get_default_metadata_attribute() == $parent_id ? 'tnc_rep_read_private_metadata' : 'tnc_col_' . $parent_id . '_read_private_metadata';
|
$read_private_cap = $this->get_default_metadata_attribute() == $parent_id ? 'tnc_rep_read_private_metadata' : 'tnc_col_' . $parent_id . '_read_private_metadata';
|
||||||
if ( current_user_can($read_private_cap) ) {
|
if ( current_user_can($read_private_cap) ) {
|
||||||
$statuses = array_merge( $statuses, get_post_stati( array( 'private' => true ) ) );
|
$statuses = array_merge( $statuses, get_post_stati( array( 'private' => true ) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
$args['post_status'] = $statuses;
|
$args['post_status'] = $statuses;
|
||||||
|
|
||||||
$meta_query = array(
|
$meta_query = array(
|
||||||
'key' => 'collection_id',
|
'key' => 'collection_id',
|
||||||
'value' => $parent_id,
|
'value' => $parent_id,
|
||||||
);
|
);
|
||||||
|
|
||||||
$args['meta_query'] = $original_meta_q;
|
$args['meta_query'] = $original_meta_q;
|
||||||
$args['meta_query'][] = $meta_query;
|
$args['meta_query'][] = $meta_query;
|
||||||
|
|
||||||
$results = array_merge($results, $this->fetch_ids( $args ));
|
$results = array_merge($results, $this->fetch_ids( $args ));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$meta_query = array(
|
$meta_query = array(
|
||||||
'key' => 'collection_id',
|
'key' => 'collection_id',
|
||||||
|
@ -531,10 +533,10 @@ class Metadata extends Repository {
|
||||||
$args = array_merge( [
|
$args = array_merge( [
|
||||||
'parent' => 0
|
'parent' => 0
|
||||||
], $args );
|
], $args );
|
||||||
|
|
||||||
$args['meta_query'] = $original_meta_q;
|
$args['meta_query'] = $original_meta_q;
|
||||||
$args['meta_query'][] = $meta_query;
|
$args['meta_query'][] = $meta_query;
|
||||||
|
|
||||||
$results = $this->fetch_ids( $args );
|
$results = $this->fetch_ids( $args );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -550,7 +552,7 @@ class Metadata extends Repository {
|
||||||
* @param Entities\Collection $collection
|
* @param Entities\Collection $collection
|
||||||
* @param bool $include_disabled Wether to include disabled metadata in the results or not
|
* @param bool $include_disabled Wether to include disabled metadata in the results or not
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function order_result( $result, Entities\Collection $collection, $include_disabled = false ) {
|
public function order_result( $result, Entities\Collection $collection, $include_disabled = false ) {
|
||||||
$order = $collection->get_metadata_order();
|
$order = $collection->get_metadata_order();
|
||||||
|
@ -585,7 +587,7 @@ class Metadata extends Repository {
|
||||||
$result_ordinate = array_merge( $result_ordinate, $not_ordinate );
|
$result_ordinate = array_merge( $result_ordinate, $not_ordinate );
|
||||||
|
|
||||||
return $result_ordinate;
|
return $result_ordinate;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -678,8 +680,8 @@ class Metadata extends Repository {
|
||||||
"UPDATE $wpdb->postmeta
|
"UPDATE $wpdb->postmeta
|
||||||
SET meta_key = %s
|
SET meta_key = %s
|
||||||
WHERE meta_key = %s AND post_id IN (
|
WHERE meta_key = %s AND post_id IN (
|
||||||
SELECT ID
|
SELECT ID
|
||||||
FROM $wpdb->posts
|
FROM $wpdb->posts
|
||||||
WHERE post_type = %s
|
WHERE post_type = %s
|
||||||
)", $new_title_metadatum->get_id(), $old_title_metadatum->get_id(), $item_post_type
|
)", $new_title_metadatum->get_id(), $old_title_metadatum->get_id(), $item_post_type
|
||||||
);
|
);
|
||||||
|
@ -690,8 +692,8 @@ class Metadata extends Repository {
|
||||||
"UPDATE $wpdb->postmeta
|
"UPDATE $wpdb->postmeta
|
||||||
SET meta_key = %s
|
SET meta_key = %s
|
||||||
WHERE meta_key = %s AND post_id IN (
|
WHERE meta_key = %s AND post_id IN (
|
||||||
SELECT ID
|
SELECT ID
|
||||||
FROM $wpdb->posts
|
FROM $wpdb->posts
|
||||||
WHERE post_type = %s
|
WHERE post_type = %s
|
||||||
)", $new_description_metadatum->get_id(), $old_description_metadatum->get_id(), $item_post_type
|
)", $new_description_metadatum->get_id(), $old_description_metadatum->get_id(), $item_post_type
|
||||||
);
|
);
|
||||||
|
@ -708,7 +710,7 @@ class Metadata extends Repository {
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function get_data_core_metadata( Entities\Collection $collection ) {
|
private function get_data_core_metadata( Entities\Collection $collection ) {
|
||||||
|
|
||||||
return $data_core_metadata = [
|
return $data_core_metadata = [
|
||||||
'core_description' => [
|
'core_description' => [
|
||||||
'name' => 'Description',
|
'name' => 'Description',
|
||||||
|
@ -775,11 +777,11 @@ class Metadata extends Repository {
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function disable_delete_core_metadata( $before, $post ) {
|
public function disable_delete_core_metadata( $before, $post ) {
|
||||||
|
|
||||||
if ( Entities\Metadatum::get_post_type() != $post->post_type ) {
|
if ( Entities\Metadatum::get_post_type() != $post->post_type ) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$metadatum = $this->fetch( $post->ID );
|
$metadatum = $this->fetch( $post->ID );
|
||||||
|
|
||||||
if ( $metadatum && in_array( $metadatum->get_metadata_type(), $this->core_metadata ) && is_numeric( $metadatum->get_collection_id() ) ) {
|
if ( $metadatum && in_array( $metadatum->get_metadata_type(), $this->core_metadata ) && is_numeric( $metadatum->get_collection_id() ) ) {
|
||||||
|
@ -799,11 +801,11 @@ class Metadata extends Repository {
|
||||||
* @internal param The $post_id post ID which is deleting
|
* @internal param The $post_id post ID which is deleting
|
||||||
*/
|
*/
|
||||||
public function force_delete_core_metadata( $before, $post, $force_delete ) {
|
public function force_delete_core_metadata( $before, $post, $force_delete ) {
|
||||||
|
|
||||||
if ( Entities\Metadatum::get_post_type() != $post->post_type ) {
|
if ( Entities\Metadatum::get_post_type() != $post->post_type ) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$metadatum = $this->fetch( $post->ID );
|
$metadatum = $this->fetch( $post->ID );
|
||||||
|
|
||||||
if ( $metadatum && in_array( $metadatum->get_metadata_type(), $this->core_metadata ) && is_numeric( $metadatum->get_collection_id() ) ) {
|
if ( $metadatum && in_array( $metadatum->get_metadata_type(), $this->core_metadata ) && is_numeric( $metadatum->get_collection_id() ) ) {
|
||||||
|
@ -915,7 +917,7 @@ class Metadata extends Repository {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return all possible values for a metadatum
|
* Return all possible values for a metadatum
|
||||||
*
|
*
|
||||||
* Each metadata is a label with the metadatum name and the value.
|
* Each metadata is a label with the metadatum name and the value.
|
||||||
*
|
*
|
||||||
|
@ -925,32 +927,32 @@ class Metadata extends Repository {
|
||||||
* @param int $metadatum_id The ID of the metadata to fetch values from
|
* @param int $metadatum_id The ID of the metadata to fetch values from
|
||||||
* @param array|string $args {
|
* @param array|string $args {
|
||||||
* Optional. Array or string of arguments.
|
* Optional. Array or string of arguments.
|
||||||
*
|
*
|
||||||
* @type mixed $collection_id The collection ID you want to consider or null for all collections. If a collectoin is set
|
* @type mixed $collection_id The collection ID you want to consider or null for all collections. If a collectoin is set
|
||||||
* then only values applied to items in this collection will be returned
|
* then only values applied to items in this collection will be returned
|
||||||
*
|
*
|
||||||
* @type int $number The number of values to return (for pagination). Default empty (unlimited)
|
* @type int $number The number of values to return (for pagination). Default empty (unlimited)
|
||||||
*
|
*
|
||||||
* @type int $offset The offset (for pagination). Default 0
|
* @type int $offset The offset (for pagination). Default 0
|
||||||
*
|
*
|
||||||
* @type array|bool $items_filter Array in the same format used in @see \Tainacan\Repositories\Items::fetch(). It will filter the results to only return values used in the items inside this criteria. If false, it will return all values, even unused ones. Defatul [] (all items)
|
* @type array|bool $items_filter Array in the same format used in @see \Tainacan\Repositories\Items::fetch(). It will filter the results to only return values used in the items inside this criteria. If false, it will return all values, even unused ones. Defatul [] (all items)
|
||||||
*
|
*
|
||||||
* @type array $include Array if ids to be included in the result. Default [] (nothing)
|
* @type array $include Array if ids to be included in the result. Default [] (nothing)
|
||||||
*
|
*
|
||||||
* @type array $search String to search. It will only return values that has this string. Default '' (nothing)
|
* @type array $search String to search. It will only return values that has this string. Default '' (nothing)
|
||||||
*
|
*
|
||||||
* @type array $parent_id Used by taxonomy metadata. The ID of the parent term to retrieve terms from. Default 0
|
* @type array $parent_id Used by taxonomy metadata. The ID of the parent term to retrieve terms from. Default 0
|
||||||
*
|
*
|
||||||
* @type bool $count_items Include the count of items that can be found in each value (uses $items_filter as well). Default false
|
* @type bool $count_items Include the count of items that can be found in each value (uses $items_filter as well). Default false
|
||||||
*
|
*
|
||||||
* @type string $last_term The last term returned when using a elasticsearch for calculates the facet.
|
* @type string $last_term The last term returned when using a elasticsearch for calculates the facet.
|
||||||
*
|
*
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
* @return array Array with the total number of values found. The total number of pages with the current number and the results with id and label for each value. Terms also include parent, taxonomy and number of children.
|
* @return array Array with the total number of values found. The total number of pages with the current number and the results with id and label for each value. Terms also include parent, taxonomy and number of children.
|
||||||
*/
|
*/
|
||||||
public function fetch_all_metadatum_values( $metadatum_id, $args = [] ) {
|
public function fetch_all_metadatum_values( $metadatum_id, $args = [] ) {
|
||||||
|
|
||||||
$defaults = array(
|
$defaults = array(
|
||||||
'collection_id' => null,
|
'collection_id' => null,
|
||||||
'search' => '',
|
'search' => '',
|
||||||
|
@ -963,34 +965,34 @@ class Metadata extends Repository {
|
||||||
'last_term' => ''
|
'last_term' => ''
|
||||||
);
|
);
|
||||||
$args = wp_parse_args($args, $defaults);
|
$args = wp_parse_args($args, $defaults);
|
||||||
|
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
|
|
||||||
$itemsRepo = Items::get_instance();
|
$itemsRepo = Items::get_instance();
|
||||||
$metadataRepo = Metadata::get_instance();
|
$metadataRepo = Metadata::get_instance();
|
||||||
|
|
||||||
$metadatum = $metadataRepo->fetch($metadatum_id);
|
$metadatum = $metadataRepo->fetch($metadatum_id);
|
||||||
$metadatum_type = $metadatum->get_metadata_type();
|
$metadatum_type = $metadatum->get_metadata_type();
|
||||||
$metadatum_options = $metadatum->get_metadata_type_options();
|
$metadatum_options = $metadatum->get_metadata_type_options();
|
||||||
|
|
||||||
if ( $metadatum_type === 'Tainacan\Metadata_Types\Taxonomy' ) {
|
if ( $metadatum_type === 'Tainacan\Metadata_Types\Taxonomy' ) {
|
||||||
$taxonomy_id = $metadatum_options['taxonomy_id'];
|
$taxonomy_id = $metadatum_options['taxonomy_id'];
|
||||||
$taxonomy_slug = Taxonomies::get_instance()->get_db_identifier_by_id($taxonomy_id);
|
$taxonomy_slug = Taxonomies::get_instance()->get_db_identifier_by_id($taxonomy_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$items_query = false;
|
$items_query = false;
|
||||||
if ( false !== $args['items_filter'] && is_array($args['items_filter']) ) {
|
if ( false !== $args['items_filter'] && is_array($args['items_filter']) ) {
|
||||||
|
|
||||||
$args['items_filter']['fields'] = 'ids';
|
$args['items_filter']['fields'] = 'ids';
|
||||||
unset($args['items_filter']['paged']);
|
unset($args['items_filter']['paged']);
|
||||||
unset($args['items_filter']['offset']);
|
unset($args['items_filter']['offset']);
|
||||||
unset($args['items_filter']['perpage']);
|
unset($args['items_filter']['perpage']);
|
||||||
$args['items_filter']['nopaging'] = 1;
|
$args['items_filter']['nopaging'] = 1;
|
||||||
|
|
||||||
// When filtering the items, we should consider only other metadata, and ignore current metadatum
|
// When filtering the items, we should consider only other metadata, and ignore current metadatum
|
||||||
// This is because the relation between values from the same metadatum when filtering item is OR,
|
// This is because the relation between values from the same metadatum when filtering item is OR,
|
||||||
// so when you filter items by one value of a metadatum you dont want to exclude all the other possibilities for that meta.
|
// so when you filter items by one value of a metadatum you dont want to exclude all the other possibilities for that meta.
|
||||||
// Only values of all other filters (facets) are reduced.
|
// Only values of all other filters (facets) are reduced.
|
||||||
if ( $metadatum_type == 'Tainacan\Metadata_Types\Taxonomy' && isset($args['items_filter']['tax_query']) && is_array($args['items_filter']['tax_query']) ) {
|
if ( $metadatum_type == 'Tainacan\Metadata_Types\Taxonomy' && isset($args['items_filter']['tax_query']) && is_array($args['items_filter']['tax_query']) ) {
|
||||||
|
@ -1004,12 +1006,12 @@ class Metadata extends Repository {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$filter = apply_filters('tainacan-fetch-all-metadatum-values', null, $metadatum, $args);
|
$filter = apply_filters('tainacan-fetch-all-metadatum-values', null, $metadatum, $args);
|
||||||
if ($filter !== null) {
|
if ($filter !== null) {
|
||||||
return $filter;
|
return $filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////
|
//////////////////////////////////////////
|
||||||
// Get the query for current items
|
// Get the query for current items
|
||||||
// this avoids wp_query to run the query. We just want to build the query
|
// this avoids wp_query to run the query. We just want to build the query
|
||||||
|
@ -1023,12 +1025,12 @@ class Metadata extends Repository {
|
||||||
////////////////////////////////////////////
|
////////////////////////////////////////////
|
||||||
////////////////////////////////////////////
|
////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
$pagination = '';
|
$pagination = '';
|
||||||
if ( $args['offset'] >= 0 && $args['number'] >= 1 ) {
|
if ( $args['offset'] >= 0 && $args['number'] >= 1 ) {
|
||||||
$pagination = $wpdb->prepare( "LIMIT %d,%d", (int) $args['offset'], (int) $args['number'] );
|
$pagination = $wpdb->prepare( "LIMIT %d,%d", (int) $args['offset'], (int) $args['number'] );
|
||||||
}
|
}
|
||||||
|
|
||||||
$search_q = '';
|
$search_q = '';
|
||||||
$search = trim($args['search']);
|
$search = trim($args['search']);
|
||||||
if (!empty($search)) {
|
if (!empty($search)) {
|
||||||
|
@ -1039,24 +1041,24 @@ class Metadata extends Repository {
|
||||||
} else {
|
} else {
|
||||||
$search_q = $wpdb->prepare("AND meta_value LIKE %s", '%' . $search . '%');
|
$search_q = $wpdb->prepare("AND meta_value LIKE %s", '%' . $search . '%');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $metadatum_type === 'Tainacan\Metadata_Types\Taxonomy' ) {
|
if ( $metadatum_type === 'Tainacan\Metadata_Types\Taxonomy' ) {
|
||||||
|
|
||||||
if ($items_query) {
|
if ($items_query) {
|
||||||
|
|
||||||
$check_hierarchy_q = $wpdb->prepare("SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s AND parent > 0 LIMIT 1", $taxonomy_slug);
|
$check_hierarchy_q = $wpdb->prepare("SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s AND parent > 0 LIMIT 1", $taxonomy_slug);
|
||||||
$has_hierarchy = ! is_null($wpdb->get_var($check_hierarchy_q));
|
$has_hierarchy = ! is_null($wpdb->get_var($check_hierarchy_q));
|
||||||
|
|
||||||
if ( ! $has_hierarchy ) {
|
if ( ! $has_hierarchy ) {
|
||||||
$base_query = $wpdb->prepare("FROM $wpdb->term_relationships tr
|
$base_query = $wpdb->prepare("FROM $wpdb->term_relationships tr
|
||||||
INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
|
INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
|
||||||
INNER JOIN $wpdb->terms t ON tt.term_id = t.term_id
|
INNER JOIN $wpdb->terms t ON tt.term_id = t.term_id
|
||||||
WHERE
|
WHERE
|
||||||
tt.parent = %d AND
|
tt.parent = %d AND
|
||||||
tr.object_id IN ($items_query) AND
|
tr.object_id IN ($items_query) AND
|
||||||
tt.taxonomy = %s
|
tt.taxonomy = %s
|
||||||
$search_q
|
$search_q
|
||||||
ORDER BY t.name ASC
|
ORDER BY t.name ASC
|
||||||
|
@ -1064,46 +1066,46 @@ class Metadata extends Repository {
|
||||||
$args['parent_id'],
|
$args['parent_id'],
|
||||||
$taxonomy_slug
|
$taxonomy_slug
|
||||||
);
|
);
|
||||||
|
|
||||||
$query = "SELECT DISTINCT t.name, t.term_id, tt.term_taxonomy_id, tt.parent $base_query $pagination";
|
$query = "SELECT DISTINCT t.name, t.term_id, tt.term_taxonomy_id, tt.parent $base_query $pagination";
|
||||||
|
|
||||||
$total_query = "SELECT COUNT(DISTINCT tt.term_taxonomy_id) $base_query";
|
$total_query = "SELECT COUNT(DISTINCT tt.term_taxonomy_id) $base_query";
|
||||||
$total = $wpdb->get_var($total_query);
|
$total = $wpdb->get_var($total_query);
|
||||||
|
|
||||||
$results = $wpdb->get_results($query);
|
$results = $wpdb->get_results($query);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
$base_query = $wpdb->prepare("
|
$base_query = $wpdb->prepare("
|
||||||
SELECT DISTINCT t.term_id, t.name, tt.parent, coalesce(tr.term_taxonomy_id, 0) as have_items
|
SELECT DISTINCT t.term_id, t.name, tt.parent, coalesce(tr.term_taxonomy_id, 0) as have_items
|
||||||
FROM
|
FROM
|
||||||
$wpdb->terms t INNER JOIN $wpdb->term_taxonomy tt ON t.term_id = tt.term_id
|
$wpdb->terms t INNER JOIN $wpdb->term_taxonomy tt ON t.term_id = tt.term_id
|
||||||
LEFT JOIN $wpdb->term_relationships tr ON tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id IN ($items_query)
|
LEFT JOIN $wpdb->term_relationships tr ON tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id IN ($items_query)
|
||||||
WHERE tt.taxonomy = %s ORDER BY t.name ASC", $taxonomy_slug
|
WHERE tt.taxonomy = %s ORDER BY t.name ASC", $taxonomy_slug
|
||||||
);
|
);
|
||||||
|
|
||||||
$all_hierarchy = $wpdb->get_results($base_query);
|
$all_hierarchy = $wpdb->get_results($base_query);
|
||||||
|
|
||||||
if (empty($search)) {
|
if (empty($search)) {
|
||||||
$results = $this->_process_terms_tree($all_hierarchy, $args['parent_id'], 'parent');
|
$results = $this->_process_terms_tree($all_hierarchy, $args['parent_id'], 'parent');
|
||||||
} else {
|
} else {
|
||||||
$results = $this->_process_terms_tree($all_hierarchy, $search, 'name');
|
$results = $this->_process_terms_tree($all_hierarchy, $search, 'name');
|
||||||
}
|
}
|
||||||
|
|
||||||
$total = count($results);
|
$total = count($results);
|
||||||
|
|
||||||
if ( $args['offset'] >= 0 && $args['number'] >= 1 ) {
|
if ( $args['offset'] >= 0 && $args['number'] >= 1 ) {
|
||||||
$results = array_slice($results, (int) $args['offset'], (int) $args['number']);
|
$results = array_slice($results, (int) $args['offset'], (int) $args['number']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
$parent_q = $wpdb->prepare("AND tt.parent = %d", $args['parent_id']);
|
$parent_q = $wpdb->prepare("AND tt.parent = %d", $args['parent_id']);
|
||||||
if ($search_q) {
|
if ($search_q) {
|
||||||
$parent_q = '';
|
$parent_q = '';
|
||||||
}
|
}
|
||||||
$base_query = $wpdb->prepare("FROM $wpdb->term_taxonomy tt
|
$base_query = $wpdb->prepare("FROM $wpdb->term_taxonomy tt
|
||||||
INNER JOIN $wpdb->terms t ON tt.term_id = t.term_id
|
INNER JOIN $wpdb->terms t ON tt.term_id = t.term_id
|
||||||
WHERE 1=1
|
WHERE 1=1
|
||||||
$parent_q
|
$parent_q
|
||||||
AND tt.taxonomy = %s
|
AND tt.taxonomy = %s
|
||||||
|
@ -1112,59 +1114,59 @@ class Metadata extends Repository {
|
||||||
",
|
",
|
||||||
$taxonomy_slug
|
$taxonomy_slug
|
||||||
);
|
);
|
||||||
|
|
||||||
$query = "SELECT DISTINCT t.name, t.term_id, tt.term_taxonomy_id, tt.parent $base_query $pagination";
|
$query = "SELECT DISTINCT t.name, t.term_id, tt.term_taxonomy_id, tt.parent $base_query $pagination";
|
||||||
|
|
||||||
$total_query = "SELECT COUNT(DISTINCT tt.term_taxonomy_id) $base_query";
|
$total_query = "SELECT COUNT(DISTINCT tt.term_taxonomy_id) $base_query";
|
||||||
$total = $wpdb->get_var($total_query);
|
$total = $wpdb->get_var($total_query);
|
||||||
|
|
||||||
$results = $wpdb->get_results($query);
|
$results = $wpdb->get_results($query);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// add selected to the result
|
// add selected to the result
|
||||||
if ( !empty($args['include']) ) {
|
if ( !empty($args['include']) ) {
|
||||||
if ( is_array($args['include']) && !empty($args['include']) ) {
|
if ( is_array($args['include']) && !empty($args['include']) ) {
|
||||||
|
|
||||||
// protect sql
|
// protect sql
|
||||||
$args['include'] = array_map(function($t) { return (int) $t; }, $args['include']);
|
$args['include'] = array_map(function($t) { return (int) $t; }, $args['include']);
|
||||||
|
|
||||||
$include_ids = implode(',', $args['include']);
|
$include_ids = implode(',', $args['include']);
|
||||||
$query_to_include = "SELECT DISTINCT t.name, t.term_id, tt.term_taxonomy_id, tt.parent FROM $wpdb->term_taxonomy tt
|
$query_to_include = "SELECT DISTINCT t.name, t.term_id, tt.term_taxonomy_id, tt.parent FROM $wpdb->term_taxonomy tt
|
||||||
INNER JOIN $wpdb->terms t ON tt.term_id = t.term_id
|
INNER JOIN $wpdb->terms t ON tt.term_id = t.term_id
|
||||||
WHERE
|
WHERE
|
||||||
t.term_id IN ($include_ids)";
|
t.term_id IN ($include_ids)";
|
||||||
|
|
||||||
$to_include = $wpdb->get_results($query_to_include);
|
$to_include = $wpdb->get_results($query_to_include);
|
||||||
|
|
||||||
// remove terms that will be included at the begining
|
// remove terms that will be included at the begining
|
||||||
$results = array_filter($results, function($t) use($args) { return !in_array($t->term_id, $args['include']); });
|
$results = array_filter($results, function($t) use($args) { return !in_array($t->term_id, $args['include']); });
|
||||||
|
|
||||||
$results = array_merge($to_include, $results);
|
$results = array_merge($to_include, $results);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$number = is_integer($args['number']) && $args['number'] >=1 ? $args['number'] : $total;
|
$number = is_integer($args['number']) && $args['number'] >=1 ? $args['number'] : $total;
|
||||||
if( $number < 1){
|
if( $number < 1){
|
||||||
$pages = 1;
|
$pages = 1;
|
||||||
} else {
|
} else {
|
||||||
$pages = ceil( $total / $number );
|
$pages = ceil( $total / $number );
|
||||||
}
|
}
|
||||||
|
|
||||||
$values = [];
|
$values = [];
|
||||||
foreach ($results as $r) {
|
foreach ($results as $r) {
|
||||||
|
|
||||||
$count_query = $wpdb->prepare("SELECT COUNT(term_id) FROM $wpdb->term_taxonomy WHERE parent = %d", $r->term_id);
|
$count_query = $wpdb->prepare("SELECT COUNT(term_id) FROM $wpdb->term_taxonomy WHERE parent = %d", $r->term_id);
|
||||||
$total_children = $wpdb->get_var($count_query);
|
$total_children = $wpdb->get_var($count_query);
|
||||||
|
|
||||||
$label = wp_specialchars_decode($r->name);
|
$label = wp_specialchars_decode($r->name);
|
||||||
$total_items = null;
|
$total_items = null;
|
||||||
|
|
||||||
if ( $args['count_items'] ) {
|
if ( $args['count_items'] ) {
|
||||||
$count_items_query = $args['items_filter'];
|
$count_items_query = $args['items_filter'];
|
||||||
$count_items_query['posts_per_page'] = 1;
|
$count_items_query['posts_per_page'] = 1;
|
||||||
|
@ -1177,11 +1179,11 @@ class Metadata extends Repository {
|
||||||
];
|
];
|
||||||
$count_items_results = $itemsRepo->fetch($count_items_query, $args['collection_id']);
|
$count_items_results = $itemsRepo->fetch($count_items_query, $args['collection_id']);
|
||||||
$total_items = $count_items_results->found_posts;
|
$total_items = $count_items_results->found_posts;
|
||||||
|
|
||||||
//$label .= " ($total_items)";
|
//$label .= " ($total_items)";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$values[] = [
|
$values[] = [
|
||||||
'value' => $r->term_id,
|
'value' => $r->term_id,
|
||||||
'label' => $label,
|
'label' => $label,
|
||||||
|
@ -1192,22 +1194,22 @@ class Metadata extends Repository {
|
||||||
'total_items' => $total_items,
|
'total_items' => $total_items,
|
||||||
'type' => 'Taxonomy'
|
'type' => 'Taxonomy'
|
||||||
];
|
];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
$items_query_clause = '';
|
$items_query_clause = '';
|
||||||
if ($items_query) {
|
if ($items_query) {
|
||||||
$items_query_clause = "AND post_id IN($items_query)";
|
$items_query_clause = "AND post_id IN($items_query)";
|
||||||
}
|
}
|
||||||
$base_query = $wpdb->prepare( "FROM $wpdb->postmeta WHERE meta_key = %s $search_q $items_query_clause ORDER BY meta_value", $metadatum_id );
|
$base_query = $wpdb->prepare( "FROM $wpdb->postmeta WHERE meta_key = %s $search_q $items_query_clause ORDER BY meta_value", $metadatum_id );
|
||||||
|
|
||||||
$total_query = "SELECT COUNT(DISTINCT meta_value) $base_query";
|
$total_query = "SELECT COUNT(DISTINCT meta_value) $base_query";
|
||||||
$query = "SELECT DISTINCT meta_value $base_query $pagination";
|
$query = "SELECT DISTINCT meta_value $base_query $pagination";
|
||||||
|
|
||||||
$results = $wpdb->get_col($query);
|
$results = $wpdb->get_col($query);
|
||||||
$total = $wpdb->get_var($total_query);
|
$total = $wpdb->get_var($total_query);
|
||||||
$number = is_integer($args['number']) && $args['number'] >=1 ? $args['number'] : $total;
|
$number = is_integer($args['number']) && $args['number'] >=1 ? $args['number'] : $total;
|
||||||
|
@ -1216,17 +1218,17 @@ class Metadata extends Repository {
|
||||||
} else {
|
} else {
|
||||||
$pages = ceil( $total / $number );
|
$pages = ceil( $total / $number );
|
||||||
}
|
}
|
||||||
|
|
||||||
// add selected to the result
|
// add selected to the result
|
||||||
if ( !empty($args['include']) ) {
|
if ( !empty($args['include']) ) {
|
||||||
if ( is_array($args['include']) ) {
|
if ( is_array($args['include']) ) {
|
||||||
$results = array_unique( array_merge($args['include'], $results) );
|
$results = array_unique( array_merge($args['include'], $results) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$values = [];
|
$values = [];
|
||||||
foreach ($results as $r) {
|
foreach ($results as $r) {
|
||||||
|
|
||||||
$label = $r;
|
$label = $r;
|
||||||
|
|
||||||
if ( $metadatum_type === 'Tainacan\Metadata_Types\Relationship' ) {
|
if ( $metadatum_type === 'Tainacan\Metadata_Types\Relationship' ) {
|
||||||
|
@ -1236,9 +1238,9 @@ class Metadata extends Repository {
|
||||||
}
|
}
|
||||||
$label = $_post->post_title;
|
$label = $_post->post_title;
|
||||||
}
|
}
|
||||||
|
|
||||||
$total_items = null;
|
$total_items = null;
|
||||||
|
|
||||||
if ( $args['count_items'] ) {
|
if ( $args['count_items'] ) {
|
||||||
$count_items_query = $args['items_filter'];
|
$count_items_query = $args['items_filter'];
|
||||||
$count_items_query['posts_per_page'] = 1;
|
$count_items_query['posts_per_page'] = 1;
|
||||||
|
@ -1251,50 +1253,50 @@ class Metadata extends Repository {
|
||||||
];
|
];
|
||||||
$count_items_results = $itemsRepo->fetch($count_items_query, $args['collection_id']);
|
$count_items_results = $itemsRepo->fetch($count_items_query, $args['collection_id']);
|
||||||
$total_items = $count_items_results->found_posts;
|
$total_items = $count_items_results->found_posts;
|
||||||
|
|
||||||
//$label .= " ($total_items)";
|
//$label .= " ($total_items)";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$values[] = [
|
$values[] = [
|
||||||
'label' => $label,
|
'label' => $label,
|
||||||
'value' => $r,
|
'value' => $r,
|
||||||
'total_items' => $total_items,
|
'total_items' => $total_items,
|
||||||
'type' => 'Text'
|
'type' => 'Text'
|
||||||
];
|
];
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'total' => $total,
|
'total' => $total,
|
||||||
'pages' => $pages,
|
'pages' => $pages,
|
||||||
'values' => $values,
|
'values' => $values,
|
||||||
'last_term' => $args['last_term']
|
'last_term' => $args['last_term']
|
||||||
];
|
];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method processes the result of the query for all terms in a taxonomy done in get_all_metadatum_values()
|
* This method processes the result of the query for all terms in a taxonomy done in get_all_metadatum_values()
|
||||||
* It efficiently runs through all the terms and checks what terms with a given $parent have items in itself or any of
|
* It efficiently runs through all the terms and checks what terms with a given $parent have items in itself or any of
|
||||||
* its descendants, keeping the order they originally came.
|
* its descendants, keeping the order they originally came.
|
||||||
*
|
*
|
||||||
* It returns an array with the term objects with the given $parent that have items considering items in its descendants. The objects are
|
* It returns an array with the term objects with the given $parent that have items considering items in its descendants. The objects are
|
||||||
* in the same format they came, as expected by the rest of the method.
|
* in the same format they came, as expected by the rest of the method.
|
||||||
*
|
*
|
||||||
* This method is public only for tests purposes, it should not be used anywhere else
|
* This method is public only for tests purposes, it should not be used anywhere else
|
||||||
*/
|
*/
|
||||||
public function _process_terms_tree($tree, $search_value, $search_type='parent') {
|
public function _process_terms_tree($tree, $search_value, $search_type='parent') {
|
||||||
|
|
||||||
$h_map = []; // all terms will mapped to this array
|
$h_map = []; // all terms will mapped to this array
|
||||||
$results = []; // terms that match search criteria will be copied to this array
|
$results = []; // terms that match search criteria will be copied to this array
|
||||||
foreach ( $tree as $h ) {
|
foreach ( $tree as $h ) {
|
||||||
|
|
||||||
// if current term comes with have_items = 1 from the database
|
// if current term comes with have_items = 1 from the database
|
||||||
// or, if it was temporarily added by its child that had have_items = 1:
|
// or, if it was temporarily added by its child that had have_items = 1:
|
||||||
if ( $h->have_items > 0 || ( isset($h_map[$h->term_id]) && $h_map[$h->term_id]->have_items > 0 ) ) {
|
if ( $h->have_items > 0 || ( isset($h_map[$h->term_id]) && $h_map[$h->term_id]->have_items > 0 ) ) {
|
||||||
|
|
||||||
// in the case of a parent that was temporarily added by a child, mark it as having items as well
|
// in the case of a parent that was temporarily added by a child, mark it as having items as well
|
||||||
$h->have_items = 1;
|
$h->have_items = 1;
|
||||||
$h_map[$h->term_id] = $h; // send it to the map array, overriding temporary item if existed
|
$h_map[$h->term_id] = $h; // send it to the map array, overriding temporary item if existed
|
||||||
|
@ -1304,25 +1306,25 @@ class Metadata extends Repository {
|
||||||
($search_type == 'name' && strpos(strtolower($h->name), strtolower($search_value)) !== false)) {
|
($search_type == 'name' && strpos(strtolower($h->name), strtolower($search_value)) !== false)) {
|
||||||
$results[$h->term_id] = $h;
|
$results[$h->term_id] = $h;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now that we know this ter have_items. Lets climb the tree all the way up
|
// Now that we know this ter have_items. Lets climb the tree all the way up
|
||||||
// marking all parents with have_items = 1
|
// marking all parents with have_items = 1
|
||||||
$_parent = $h->parent;
|
$_parent = $h->parent;
|
||||||
|
|
||||||
// If parent was not added to the map array yet
|
// If parent was not added to the map array yet
|
||||||
// Lets add a temporary entry
|
// Lets add a temporary entry
|
||||||
if ( $h->parent > 0 && !isset($h_map[$_parent]) ) {
|
if ( $h->parent > 0 && !isset($h_map[$_parent]) ) {
|
||||||
$h_map[$_parent] = (object)['have_items' => 1];
|
$h_map[$_parent] = (object)['have_items' => 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now lets loop thorough the map array until I check all my parents
|
// Now lets loop thorough the map array until I check all my parents
|
||||||
while( isset($h_map[$_parent]) && $h_map[$_parent]->have_items != 1 ) {
|
while( isset($h_map[$_parent]) && $h_map[$_parent]->have_items != 1 ) {
|
||||||
|
|
||||||
// If my parent was added before, but marked with have_items = 0
|
// If my parent was added before, but marked with have_items = 0
|
||||||
// Lets set it to 1
|
// Lets set it to 1
|
||||||
|
|
||||||
$h_map[$_parent]->have_items = 1;
|
$h_map[$_parent]->have_items = 1;
|
||||||
|
|
||||||
// If my parent is a whole object, and not a temporary one
|
// If my parent is a whole object, and not a temporary one
|
||||||
if ( isset($h_map[$_parent]->parent) ) {
|
if ( isset($h_map[$_parent]->parent) ) {
|
||||||
// if parent matches search criteira, add to results
|
// if parent matches search criteira, add to results
|
||||||
|
@ -1336,21 +1338,21 @@ class Metadata extends Repository {
|
||||||
// Quit loop. We have reached as high as we could in the tree
|
// Quit loop. We have reached as high as we could in the tree
|
||||||
$_parent = 0;
|
$_parent = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// if current term have_items = 0
|
// if current term have_items = 0
|
||||||
|
|
||||||
// add it to the map
|
// add it to the map
|
||||||
$h_map[$h->term_id] = $h;
|
$h_map[$h->term_id] = $h;
|
||||||
|
|
||||||
// if parent was not mapped yet, create a temporary entry for him
|
// if parent was not mapped yet, create a temporary entry for him
|
||||||
if ( $h->parent > 0 && !isset($h_map[$h->parent]) ) {
|
if ( $h->parent > 0 && !isset($h_map[$h->parent]) ) {
|
||||||
$h_map[$h->parent] = (object)['have_items' => $h->have_items];
|
$h_map[$h->parent] = (object)['have_items' => $h->have_items];
|
||||||
}
|
}
|
||||||
|
|
||||||
// if item matches search criteria, add it to the results
|
// if item matches search criteria, add it to the results
|
||||||
if(($search_type == 'parent' && $h->parent == $search_value) ||
|
if(($search_type == 'parent' && $h->parent == $search_value) ||
|
||||||
($search_type == 'name' && $h->have_items > 0 && strpos(strtolower($h->name), strtolower($search_value)) !== false)) {
|
($search_type == 'name' && $h->have_items > 0 && strpos(strtolower($h->name), strtolower($search_value)) !== false)) {
|
||||||
|
@ -1359,7 +1361,7 @@ class Metadata extends Repository {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Results have all terms that matches search criteria. Now we unset those who dont have items
|
// Results have all terms that matches search criteria. Now we unset those who dont have items
|
||||||
// and set it back to incremental keys]
|
// and set it back to incremental keys]
|
||||||
// we could have sent to $results only those with items, but doing that we would not preserve their order
|
// we could have sent to $results only those with items, but doing that we would not preserve their order
|
||||||
|
@ -1369,9 +1371,9 @@ class Metadata extends Repository {
|
||||||
}
|
}
|
||||||
return $return;
|
return $return;
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return $results;
|
return $results;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1421,10 +1423,10 @@ class Metadata extends Repository {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an index with the exploded values of metadata_type_options array. Each option is prefixed with '_option_'
|
* Creates an index with the exploded values of metadata_type_options array. Each option is prefixed with '_option_'
|
||||||
* This is useful to allow metadata to be queried based on a specific value of a metadata type option.
|
* This is useful to allow metadata to be queried based on a specific value of a metadata type option.
|
||||||
* For example, fetch all taxonomy metadata which the taxonomy_id metadata type option is equal to 4
|
* For example, fetch all taxonomy metadata which the taxonomy_id metadata type option is equal to 4
|
||||||
*
|
*
|
||||||
* $metadata_repository->fetch([
|
* $metadata_repository->fetch([
|
||||||
|
@ -1435,11 +1437,11 @@ class Metadata extends Repository {
|
||||||
* ]
|
* ]
|
||||||
* ]
|
* ]
|
||||||
* ])
|
* ])
|
||||||
*
|
*
|
||||||
* @var Entities\Metadatum $metadatum
|
* @var Entities\Metadatum $metadatum
|
||||||
*/
|
*/
|
||||||
private function update_metadata_type_index( Entities\Metadatum $metadatum ) {
|
private function update_metadata_type_index( Entities\Metadatum $metadatum ) {
|
||||||
|
|
||||||
$options = $this->get_mapped_property($metadatum, 'metadata_type_options');
|
$options = $this->get_mapped_property($metadatum, 'metadata_type_options');
|
||||||
if (!is_array($options)) {
|
if (!is_array($options)) {
|
||||||
return;
|
return;
|
||||||
|
@ -1447,9 +1449,9 @@ class Metadata extends Repository {
|
||||||
foreach ($options as $option => $value) {
|
foreach ($options as $option => $value) {
|
||||||
update_post_meta($metadatum->get_id(), '_option_' . $option, $value);
|
update_post_meta($metadatum->get_id(), '_option_' . $option, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
|
@ -1471,4 +1473,39 @@ class Metadata extends Repository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When a private taxonomy is saved, check if there are any public metadata and set them to private
|
||||||
|
*
|
||||||
|
* @param \Tainacan\Entities\Taxonomy $taxonomy
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function hook_taxonomies_saved_as_private($taxonomy) {
|
||||||
|
|
||||||
|
if ( $taxonomy instanceof Entities\Taxonomy ) {
|
||||||
|
|
||||||
|
$status_obj = get_post_status_object( $taxonomy->get_status() );
|
||||||
|
|
||||||
|
if ( is_object($status_obj) && ! $status_obj->public ) {
|
||||||
|
|
||||||
|
$stati = get_post_stati(['public' => true]);
|
||||||
|
|
||||||
|
$taxonomy_id = $taxonomy->get_id();
|
||||||
|
|
||||||
|
$metadata = $this->fetch(['metadata_type' => 'Tainacan\Metadata_Types\Taxonomy', '_option_taxonomy_id' => $taxonomy_id, 'post_status' => $stati], 'OBJECT');
|
||||||
|
|
||||||
|
foreach ($metadata as $meta) {
|
||||||
|
$meta->set_status( $taxonomy->get_status() );
|
||||||
|
if ( $meta->validate() ) {
|
||||||
|
$this->insert($meta);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,13 +15,13 @@ use Tainacan\Entities;
|
||||||
*/
|
*/
|
||||||
class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
|
|
||||||
|
|
||||||
function test_taxonomy_metadata_types() {
|
function test_taxonomy_metadata_types() {
|
||||||
|
|
||||||
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
||||||
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
|
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
|
||||||
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
|
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
|
||||||
|
|
||||||
$collection = $this->tainacan_entity_factory->create_entity(
|
$collection = $this->tainacan_entity_factory->create_entity(
|
||||||
'collection',
|
'collection',
|
||||||
array(
|
array(
|
||||||
|
@ -29,7 +29,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$tax = $this->tainacan_entity_factory->create_entity(
|
$tax = $this->tainacan_entity_factory->create_entity(
|
||||||
'taxonomy',
|
'taxonomy',
|
||||||
array(
|
array(
|
||||||
|
@ -39,7 +39,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$metadatum = $this->tainacan_entity_factory->create_entity(
|
$metadatum = $this->tainacan_entity_factory->create_entity(
|
||||||
'metadatum',
|
'metadatum',
|
||||||
array(
|
array(
|
||||||
|
@ -55,7 +55,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$i = $this->tainacan_entity_factory->create_entity(
|
$i = $this->tainacan_entity_factory->create_entity(
|
||||||
'item',
|
'item',
|
||||||
array(
|
array(
|
||||||
|
@ -66,7 +66,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$metadatum2 = $this->tainacan_entity_factory->create_entity(
|
$metadatum2 = $this->tainacan_entity_factory->create_entity(
|
||||||
'metadatum',
|
'metadatum',
|
||||||
array(
|
array(
|
||||||
|
@ -78,11 +78,11 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$term = $this->tainacan_entity_factory->create_entity(
|
$term = $this->tainacan_entity_factory->create_entity(
|
||||||
'term',
|
'term',
|
||||||
array(
|
array(
|
||||||
|
@ -122,23 +122,23 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
$check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($checkItem, $metadatum);
|
$check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($checkItem, $metadatum);
|
||||||
|
|
||||||
$this->assertEquals('Tainacan\Entities\Term', get_class($check_item_metadata->get_value()));
|
$this->assertEquals('Tainacan\Entities\Term', get_class($check_item_metadata->get_value()));
|
||||||
|
|
||||||
// test 2 metadata with same taxonomy
|
// test 2 metadata with same taxonomy
|
||||||
$metadatum2->set_metadata_type_options([
|
$metadatum2->set_metadata_type_options([
|
||||||
'taxonomy_id' => $tax->get_id(),
|
'taxonomy_id' => $tax->get_id(),
|
||||||
]);
|
]);
|
||||||
$metadatum2->set_status('publish');
|
$metadatum2->set_status('publish');
|
||||||
|
|
||||||
$this->assertFalse($metadatum2->validate(), 'Taxonomy Metadatum should not validate when using a taxonomy in use by another metadatum in the same collection');
|
$this->assertFalse($metadatum2->validate(), 'Taxonomy Metadatum should not validate when using a taxonomy in use by another metadatum in the same collection');
|
||||||
$errors = $metadatum2->get_errors();
|
$errors = $metadatum2->get_errors();
|
||||||
$this->assertInternalType('array', $errors);
|
$this->assertInternalType('array', $errors);
|
||||||
$this->assertArrayHasKey('taxonomy_id', $errors[0]['metadata_type_options']);
|
$this->assertArrayHasKey('taxonomy_id', $errors[0]['metadata_type_options']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_relate_taxonomy() {
|
function test_relate_taxonomy() {
|
||||||
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
||||||
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
||||||
|
|
||||||
$collection = $this->tainacan_entity_factory->create_entity(
|
$collection = $this->tainacan_entity_factory->create_entity(
|
||||||
'collection',
|
'collection',
|
||||||
array(
|
array(
|
||||||
|
@ -147,7 +147,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$tax = $this->tainacan_entity_factory->create_entity(
|
$tax = $this->tainacan_entity_factory->create_entity(
|
||||||
'taxonomy',
|
'taxonomy',
|
||||||
array(
|
array(
|
||||||
|
@ -156,7 +156,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$tax2 = $this->tainacan_entity_factory->create_entity(
|
$tax2 = $this->tainacan_entity_factory->create_entity(
|
||||||
'taxonomy',
|
'taxonomy',
|
||||||
array(
|
array(
|
||||||
|
@ -165,7 +165,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$tax3 = $this->tainacan_entity_factory->create_entity(
|
$tax3 = $this->tainacan_entity_factory->create_entity(
|
||||||
'taxonomy',
|
'taxonomy',
|
||||||
array(
|
array(
|
||||||
|
@ -174,9 +174,9 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertNotContains($tax->get_db_identifier(), get_object_taxonomies($collection->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created');
|
$this->assertNotContains($tax->get_db_identifier(), get_object_taxonomies($collection->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created');
|
||||||
|
|
||||||
$metadatum = $this->tainacan_entity_factory->create_entity(
|
$metadatum = $this->tainacan_entity_factory->create_entity(
|
||||||
'metadatum',
|
'metadatum',
|
||||||
array(
|
array(
|
||||||
|
@ -192,34 +192,34 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertContains($tax->get_db_identifier(), get_object_taxonomies($collection->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created');
|
$this->assertContains($tax->get_db_identifier(), get_object_taxonomies($collection->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created');
|
||||||
|
|
||||||
|
|
||||||
$checkTax = $Tainacan_Taxonomies->fetch($tax->get_id());
|
$checkTax = $Tainacan_Taxonomies->fetch($tax->get_id());
|
||||||
$this->assertContains($collection->get_id(), $checkTax->get_collections_ids(), 'Collection must be added to taxonomy when metadatum is created');
|
$this->assertContains($collection->get_id(), $checkTax->get_collections_ids(), 'Collection must be added to taxonomy when metadatum is created');
|
||||||
|
|
||||||
|
|
||||||
$metadatum->set_metadata_type_options([
|
$metadatum->set_metadata_type_options([
|
||||||
'taxonomy_id' => $tax2->get_id(),
|
'taxonomy_id' => $tax2->get_id(),
|
||||||
'allow_new_terms' => 'no'
|
'allow_new_terms' => 'no'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$metadatum->validate();
|
$metadatum->validate();
|
||||||
$metadatum = $Tainacan_Metadata->insert($metadatum);
|
$metadatum = $Tainacan_Metadata->insert($metadatum);
|
||||||
|
|
||||||
$checkTax = $Tainacan_Taxonomies->fetch($tax->get_id());
|
$checkTax = $Tainacan_Taxonomies->fetch($tax->get_id());
|
||||||
$checkTax2 = $Tainacan_Taxonomies->fetch($tax2->get_id());
|
$checkTax2 = $Tainacan_Taxonomies->fetch($tax2->get_id());
|
||||||
$this->assertContains($collection->get_id(), $checkTax2->get_collections_ids(), 'Collection must be added to taxonomy when metadatum is updated');
|
$this->assertContains($collection->get_id(), $checkTax2->get_collections_ids(), 'Collection must be added to taxonomy when metadatum is updated');
|
||||||
$this->assertNotContains($collection->get_id(), $checkTax->get_collections_ids(), 'Collection must be removed from taxonomy when metadatum is updated');
|
$this->assertNotContains($collection->get_id(), $checkTax->get_collections_ids(), 'Collection must be removed from taxonomy when metadatum is updated');
|
||||||
|
|
||||||
$metadatum = $Tainacan_Metadata->trash($metadatum);
|
$metadatum = $Tainacan_Metadata->trash($metadatum);
|
||||||
|
|
||||||
$checkTax2 = $Tainacan_Taxonomies->fetch($tax2->get_id());
|
$checkTax2 = $Tainacan_Taxonomies->fetch($tax2->get_id());
|
||||||
|
|
||||||
$this->assertNotContains($collection->get_id(), $checkTax2->get_collections_ids(), 'Collection must be removed from taxonomy when metadatum is deleted');
|
$this->assertNotContains($collection->get_id(), $checkTax2->get_collections_ids(), 'Collection must be removed from taxonomy when metadatum is deleted');
|
||||||
|
|
||||||
|
|
||||||
$metadatum_repo = $this->tainacan_entity_factory->create_entity(
|
$metadatum_repo = $this->tainacan_entity_factory->create_entity(
|
||||||
'metadatum',
|
'metadatum',
|
||||||
array(
|
array(
|
||||||
|
@ -235,18 +235,18 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertContains($tax3->get_db_identifier(), get_object_taxonomies($collection->get_db_identifier()), 'Taxonommy used by repository level metadatum must be assigned to all collections post types');
|
$this->assertContains($tax3->get_db_identifier(), get_object_taxonomies($collection->get_db_identifier()), 'Taxonommy used by repository level metadatum must be assigned to all collections post types');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_relate_taxonomy_match() {
|
function test_relate_taxonomy_match() {
|
||||||
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
||||||
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
||||||
|
|
||||||
$collection = $this->tainacan_entity_factory->create_entity(
|
$collection = $this->tainacan_entity_factory->create_entity(
|
||||||
'collection',
|
'collection',
|
||||||
array(
|
array(
|
||||||
|
@ -255,7 +255,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$collection2 = $this->tainacan_entity_factory->create_entity(
|
$collection2 = $this->tainacan_entity_factory->create_entity(
|
||||||
'collection',
|
'collection',
|
||||||
array(
|
array(
|
||||||
|
@ -264,7 +264,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$tax = $this->tainacan_entity_factory->create_entity(
|
$tax = $this->tainacan_entity_factory->create_entity(
|
||||||
'taxonomy',
|
'taxonomy',
|
||||||
array(
|
array(
|
||||||
|
@ -273,7 +273,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$tax2 = $this->tainacan_entity_factory->create_entity(
|
$tax2 = $this->tainacan_entity_factory->create_entity(
|
||||||
'taxonomy',
|
'taxonomy',
|
||||||
array(
|
array(
|
||||||
|
@ -282,7 +282,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$tax3 = $this->tainacan_entity_factory->create_entity(
|
$tax3 = $this->tainacan_entity_factory->create_entity(
|
||||||
'taxonomy',
|
'taxonomy',
|
||||||
array(
|
array(
|
||||||
|
@ -291,7 +291,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$metadatum = $this->tainacan_entity_factory->create_entity(
|
$metadatum = $this->tainacan_entity_factory->create_entity(
|
||||||
'metadatum',
|
'metadatum',
|
||||||
array(
|
array(
|
||||||
|
@ -307,7 +307,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$metadatum2 = $this->tainacan_entity_factory->create_entity(
|
$metadatum2 = $this->tainacan_entity_factory->create_entity(
|
||||||
'metadatum',
|
'metadatum',
|
||||||
array(
|
array(
|
||||||
|
@ -323,7 +323,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$metadatum3 = $this->tainacan_entity_factory->create_entity(
|
$metadatum3 = $this->tainacan_entity_factory->create_entity(
|
||||||
'metadatum',
|
'metadatum',
|
||||||
array(
|
array(
|
||||||
|
@ -339,7 +339,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$metadatum4 = $this->tainacan_entity_factory->create_entity(
|
$metadatum4 = $this->tainacan_entity_factory->create_entity(
|
||||||
'metadatum',
|
'metadatum',
|
||||||
array(
|
array(
|
||||||
|
@ -355,34 +355,34 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$checkTax = $Tainacan_Taxonomies->fetch($tax->get_id());
|
$checkTax = $Tainacan_Taxonomies->fetch($tax->get_id());
|
||||||
$checkTax2 = $Tainacan_Taxonomies->fetch($tax2->get_id());
|
$checkTax2 = $Tainacan_Taxonomies->fetch($tax2->get_id());
|
||||||
$checkTax3 = $Tainacan_Taxonomies->fetch($tax3->get_id());
|
$checkTax3 = $Tainacan_Taxonomies->fetch($tax3->get_id());
|
||||||
|
|
||||||
$this->assertEquals( sizeof($checkTax->get_collections_ids()), sizeof($checkTax->get_collections()) );
|
$this->assertEquals( sizeof($checkTax->get_collections_ids()), sizeof($checkTax->get_collections()) );
|
||||||
$this->assertEquals( sizeof($checkTax2->get_collections_ids()), sizeof($checkTax2->get_collections()) );
|
$this->assertEquals( sizeof($checkTax2->get_collections_ids()), sizeof($checkTax2->get_collections()) );
|
||||||
$this->assertEquals( sizeof($checkTax3->get_collections_ids()), sizeof($checkTax3->get_collections()) );
|
$this->assertEquals( sizeof($checkTax3->get_collections_ids()), sizeof($checkTax3->get_collections()) );
|
||||||
|
|
||||||
$this->assertNotContains($checkTax3->get_db_identifier(), get_object_taxonomies($collection->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created');
|
$this->assertNotContains($checkTax3->get_db_identifier(), get_object_taxonomies($collection->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created');
|
||||||
$this->assertContains($checkTax->get_db_identifier(), get_object_taxonomies($collection->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created');
|
$this->assertContains($checkTax->get_db_identifier(), get_object_taxonomies($collection->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created');
|
||||||
$this->assertContains($checkTax2->get_db_identifier(), get_object_taxonomies($collection->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created');
|
$this->assertContains($checkTax2->get_db_identifier(), get_object_taxonomies($collection->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created');
|
||||||
|
|
||||||
$this->assertNotContains($checkTax->get_db_identifier(), get_object_taxonomies($collection2->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created');
|
$this->assertNotContains($checkTax->get_db_identifier(), get_object_taxonomies($collection2->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created');
|
||||||
$this->assertContains($checkTax3->get_db_identifier(), get_object_taxonomies($collection2->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created');
|
$this->assertContains($checkTax3->get_db_identifier(), get_object_taxonomies($collection2->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created');
|
||||||
$this->assertContains($checkTax2->get_db_identifier(), get_object_taxonomies($collection2->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created');
|
$this->assertContains($checkTax2->get_db_identifier(), get_object_taxonomies($collection2->get_db_identifier()), 'Collection must be added to taxonomy when metadatum is created');
|
||||||
|
|
||||||
$this->assertEquals(2, sizeof( get_object_taxonomies($collection2->get_db_identifier()) ));
|
$this->assertEquals(2, sizeof( get_object_taxonomies($collection2->get_db_identifier()) ));
|
||||||
$this->assertEquals(2, sizeof( get_object_taxonomies($collection->get_db_identifier()) ));
|
$this->assertEquals(2, sizeof( get_object_taxonomies($collection->get_db_identifier()) ));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @group fetch_by_collection
|
* @group fetch_by_collection
|
||||||
*/
|
*/
|
||||||
function test_fetch_by_collection() {
|
function test_fetch_by_collection() {
|
||||||
|
|
||||||
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
||||||
|
|
||||||
$collection = $this->tainacan_entity_factory->create_entity(
|
$collection = $this->tainacan_entity_factory->create_entity(
|
||||||
'collection',
|
'collection',
|
||||||
array(
|
array(
|
||||||
|
@ -391,7 +391,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$collection2 = $this->tainacan_entity_factory->create_entity(
|
$collection2 = $this->tainacan_entity_factory->create_entity(
|
||||||
'collection',
|
'collection',
|
||||||
array(
|
array(
|
||||||
|
@ -400,7 +400,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$collection2_c = $this->tainacan_entity_factory->create_entity(
|
$collection2_c = $this->tainacan_entity_factory->create_entity(
|
||||||
'collection',
|
'collection',
|
||||||
array(
|
array(
|
||||||
|
@ -410,7 +410,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$collection2_gc = $this->tainacan_entity_factory->create_entity(
|
$collection2_gc = $this->tainacan_entity_factory->create_entity(
|
||||||
'collection',
|
'collection',
|
||||||
array(
|
array(
|
||||||
|
@ -420,7 +420,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$tax = $this->tainacan_entity_factory->create_entity(
|
$tax = $this->tainacan_entity_factory->create_entity(
|
||||||
'taxonomy',
|
'taxonomy',
|
||||||
array(
|
array(
|
||||||
|
@ -429,7 +429,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$tax2 = $this->tainacan_entity_factory->create_entity(
|
$tax2 = $this->tainacan_entity_factory->create_entity(
|
||||||
'taxonomy',
|
'taxonomy',
|
||||||
array(
|
array(
|
||||||
|
@ -438,7 +438,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$tax3 = $this->tainacan_entity_factory->create_entity(
|
$tax3 = $this->tainacan_entity_factory->create_entity(
|
||||||
'taxonomy',
|
'taxonomy',
|
||||||
array(
|
array(
|
||||||
|
@ -447,7 +447,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$tax4 = $this->tainacan_entity_factory->create_entity(
|
$tax4 = $this->tainacan_entity_factory->create_entity(
|
||||||
'taxonomy',
|
'taxonomy',
|
||||||
array(
|
array(
|
||||||
|
@ -456,8 +456,8 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
// metadata 1 in repo level for every one
|
// metadata 1 in repo level for every one
|
||||||
$metadatum_repo = $this->tainacan_entity_factory->create_entity(
|
$metadatum_repo = $this->tainacan_entity_factory->create_entity(
|
||||||
'metadatum',
|
'metadatum',
|
||||||
|
@ -474,7 +474,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
// meta 2 in collection 1 just for it
|
// meta 2 in collection 1 just for it
|
||||||
$metadatum2 = $this->tainacan_entity_factory->create_entity(
|
$metadatum2 = $this->tainacan_entity_factory->create_entity(
|
||||||
'metadatum',
|
'metadatum',
|
||||||
|
@ -491,7 +491,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
// meta 3 in collection 2 for it and chidlren and grand children
|
// meta 3 in collection 2 for it and chidlren and grand children
|
||||||
$metadatum3 = $this->tainacan_entity_factory->create_entity(
|
$metadatum3 = $this->tainacan_entity_factory->create_entity(
|
||||||
'metadatum',
|
'metadatum',
|
||||||
array(
|
array(
|
||||||
|
@ -507,7 +507,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
// meta 4 in collection 2c only for children and grand children
|
// meta 4 in collection 2c only for children and grand children
|
||||||
$metadatum4 = $this->tainacan_entity_factory->create_entity(
|
$metadatum4 = $this->tainacan_entity_factory->create_entity(
|
||||||
'metadatum',
|
'metadatum',
|
||||||
|
@ -524,27 +524,27 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$taxonomies_1 = $Tainacan_Taxonomies->fetch_by_collection($collection);
|
$taxonomies_1 = $Tainacan_Taxonomies->fetch_by_collection($collection);
|
||||||
$this->assertEquals(2, sizeof($taxonomies_1));
|
$this->assertEquals(2, sizeof($taxonomies_1));
|
||||||
|
|
||||||
$taxonomies_2 = $Tainacan_Taxonomies->fetch_by_collection($collection2);
|
$taxonomies_2 = $Tainacan_Taxonomies->fetch_by_collection($collection2);
|
||||||
$this->assertEquals(2, sizeof($taxonomies_2));
|
$this->assertEquals(2, sizeof($taxonomies_2));
|
||||||
|
|
||||||
$taxonomies_3 = $Tainacan_Taxonomies->fetch_by_collection($collection2_c);
|
$taxonomies_3 = $Tainacan_Taxonomies->fetch_by_collection($collection2_c);
|
||||||
$this->assertEquals(3, sizeof($taxonomies_3));
|
$this->assertEquals(3, sizeof($taxonomies_3));
|
||||||
|
|
||||||
$taxonomies_4 = $Tainacan_Taxonomies->fetch_by_collection($collection2_gc);
|
$taxonomies_4 = $Tainacan_Taxonomies->fetch_by_collection($collection2_gc);
|
||||||
$this->assertEquals(3, sizeof($taxonomies_4));
|
$this->assertEquals(3, sizeof($taxonomies_4));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_values_and_html() {
|
function test_values_and_html() {
|
||||||
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
||||||
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
||||||
$Tainacan_ItemMetadata = \Tainacan\Repositories\Item_Metadata::get_instance();
|
$Tainacan_ItemMetadata = \Tainacan\Repositories\Item_Metadata::get_instance();
|
||||||
|
|
||||||
$collection = $this->tainacan_entity_factory->create_entity(
|
$collection = $this->tainacan_entity_factory->create_entity(
|
||||||
'collection',
|
'collection',
|
||||||
array(
|
array(
|
||||||
|
@ -552,7 +552,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$tax = $this->tainacan_entity_factory->create_entity(
|
$tax = $this->tainacan_entity_factory->create_entity(
|
||||||
'taxonomy',
|
'taxonomy',
|
||||||
array(
|
array(
|
||||||
|
@ -561,7 +561,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$item = $this->tainacan_entity_factory->create_entity(
|
$item = $this->tainacan_entity_factory->create_entity(
|
||||||
'item',
|
'item',
|
||||||
array(
|
array(
|
||||||
|
@ -571,7 +571,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$metadatum = $this->tainacan_entity_factory->create_entity(
|
$metadatum = $this->tainacan_entity_factory->create_entity(
|
||||||
'metadatum',
|
'metadatum',
|
||||||
array(
|
array(
|
||||||
|
@ -587,32 +587,32 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$meta = new \Tainacan\Entities\Item_Metadata_Entity($item, $metadatum);
|
$meta = new \Tainacan\Entities\Item_Metadata_Entity($item, $metadatum);
|
||||||
|
|
||||||
$meta->set_value('new_term');
|
$meta->set_value('new_term');
|
||||||
|
|
||||||
$meta->validate();
|
$meta->validate();
|
||||||
|
|
||||||
$meta = $Tainacan_ItemMetadata->insert($meta);
|
$meta = $Tainacan_ItemMetadata->insert($meta);
|
||||||
|
|
||||||
$this->assertInternalType( 'string', $meta->get_value_as_html() );
|
$this->assertInternalType( 'string', $meta->get_value_as_html() );
|
||||||
$this->assertInternalType( 'string', $meta->get_value_as_string() );
|
$this->assertInternalType( 'string', $meta->get_value_as_string() );
|
||||||
|
|
||||||
$this->assertInternalType( 'integer', strpos($meta->get_value_as_html(), '<a ') );
|
$this->assertInternalType( 'integer', strpos($meta->get_value_as_html(), '<a ') );
|
||||||
$this->assertFalse( strpos($meta->get_value_as_string(), '<a ') );
|
$this->assertFalse( strpos($meta->get_value_as_string(), '<a ') );
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_validate_private_taxonomy() {
|
function test_validate_private_taxonomy() {
|
||||||
|
|
||||||
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
||||||
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
||||||
$Tainacan_ItemMetadata = \Tainacan\Repositories\Item_Metadata::get_instance();
|
$Tainacan_ItemMetadata = \Tainacan\Repositories\Item_Metadata::get_instance();
|
||||||
|
|
||||||
$collection = $this->tainacan_entity_factory->create_entity(
|
$collection = $this->tainacan_entity_factory->create_entity(
|
||||||
'collection',
|
'collection',
|
||||||
array(
|
array(
|
||||||
|
@ -620,7 +620,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$tax = $this->tainacan_entity_factory->create_entity(
|
$tax = $this->tainacan_entity_factory->create_entity(
|
||||||
'taxonomy',
|
'taxonomy',
|
||||||
array(
|
array(
|
||||||
|
@ -629,9 +629,9 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$meta = new \Tainacan\Entities\Metadatum();
|
$meta = new \Tainacan\Entities\Metadatum();
|
||||||
|
|
||||||
$meta->set_name('test meta');
|
$meta->set_name('test meta');
|
||||||
$meta->set_status('publish');
|
$meta->set_status('publish');
|
||||||
$meta->set_metadata_type('Tainacan\Metadata_Types\Taxonomy');
|
$meta->set_metadata_type('Tainacan\Metadata_Types\Taxonomy');
|
||||||
|
@ -640,23 +640,71 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
'taxonomy_id' => $tax->get_id(),
|
'taxonomy_id' => $tax->get_id(),
|
||||||
'allow_new_terms' => 'yes'
|
'allow_new_terms' => 'yes'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertFalse($meta->validate(), 'Metadatum should not validate because taxonomy is private');
|
$this->assertFalse($meta->validate(), 'Metadatum should not validate because taxonomy is private');
|
||||||
|
|
||||||
$meta->set_status('private');
|
$meta->set_status('private');
|
||||||
|
|
||||||
$this->assertTrue($meta->validate(), 'Metadatum should validate because it is private now');
|
$this->assertTrue($meta->validate(), 'Metadatum should validate because it is private now');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function test_set_metadata_to_private_after_taxonomy_is_set_to_private() {
|
||||||
|
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
||||||
|
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
||||||
|
$Tainacan_ItemMetadata = \Tainacan\Repositories\Item_Metadata::get_instance();
|
||||||
|
|
||||||
|
$collection = $this->tainacan_entity_factory->create_entity(
|
||||||
|
'collection',
|
||||||
|
array(
|
||||||
|
'name' => 'test',
|
||||||
|
'status' => 'publish'
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
$tax = $this->tainacan_entity_factory->create_entity(
|
||||||
|
'taxonomy',
|
||||||
|
array(
|
||||||
|
'name' => 'tax_test',
|
||||||
|
'status' => 'publish'
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
$metadatum = $this->tainacan_entity_factory->create_entity(
|
||||||
|
'metadatum',
|
||||||
|
array(
|
||||||
|
'name' => 'meta',
|
||||||
|
'description' => 'description',
|
||||||
|
'collection' => $collection,
|
||||||
|
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy',
|
||||||
|
'status' => 'publish',
|
||||||
|
'metadata_type_options' => [
|
||||||
|
'taxonomy_id' => $tax->get_id(),
|
||||||
|
'allow_new_terms' => 'yes'
|
||||||
|
]
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
$tax->set_status('private');
|
||||||
|
$tax->validate();
|
||||||
|
$Tainacan_Taxonomies->insert($tax);
|
||||||
|
|
||||||
|
$checkMeta = $Tainacan_Metadata->fetch( $metadatum->get_id() );
|
||||||
|
|
||||||
|
$this->assertEquals('private', $checkMeta->get_status());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
function test_get_taxonomy_method() {
|
function test_get_taxonomy_method() {
|
||||||
|
|
||||||
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
||||||
|
|
||||||
$collection = $this->tainacan_entity_factory->create_entity(
|
$collection = $this->tainacan_entity_factory->create_entity(
|
||||||
'collection',
|
'collection',
|
||||||
array(
|
array(
|
||||||
|
@ -664,7 +712,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$tax = $this->tainacan_entity_factory->create_entity(
|
$tax = $this->tainacan_entity_factory->create_entity(
|
||||||
'taxonomy',
|
'taxonomy',
|
||||||
array(
|
array(
|
||||||
|
@ -674,7 +722,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$metadatum = $this->tainacan_entity_factory->create_entity(
|
$metadatum = $this->tainacan_entity_factory->create_entity(
|
||||||
'metadatum',
|
'metadatum',
|
||||||
array(
|
array(
|
||||||
|
@ -690,14 +738,14 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
$object = $metadatum->get_metadata_type_object();
|
$object = $metadatum->get_metadata_type_object();
|
||||||
|
|
||||||
$taxCheck = $object->get_taxonomy();
|
$taxCheck = $object->get_taxonomy();
|
||||||
|
|
||||||
$this->assertTrue( $taxCheck instanceof \Tainacan\Entities\Taxonomy );
|
$this->assertTrue( $taxCheck instanceof \Tainacan\Entities\Taxonomy );
|
||||||
$this->assertEquals($tax->get_id(), $taxCheck->get_id());
|
$this->assertEquals($tax->get_id(), $taxCheck->get_id());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue