improves get_metadata_html

This commit is contained in:
Leo Germani 2018-05-30 12:06:45 -03:00
parent f9dd1c3a9f
commit 8cb4d0061e
2 changed files with 119 additions and 35 deletions

View File

@ -454,26 +454,37 @@ class Item extends Entity {
* If an ID, a slug or a Tainacan\Entities\Field object is passed, it returns only one metadata, otherwise
* it returns all metadata
*
* @param int|string|Tainacan\Entities\Field $field Field object, ID or slug to retrieve only one field. empty returns all fields
* @param array|string $args {
* Optional. Array or string of arguments.
*
* @type bool $hide_empty Wether to hide or not fields the item has no value to
* Default: true
* @type string $before_title String to be added before each metadata title
* Default '<h3>'
* @type string $after_title String to be added after each metadata title
* Default '</h3>'
* @type string $before_value String to be added before each metadata value
* Default '<p>'
* @type string $after_value String to be added after each metadata value
* Default '</p>'
* }
* @param array|string $args {
* Optional. Array or string of arguments.
*
* @type mixed $metadata Field object, ID or slug to retrieve only one field. empty returns all metadata
*
* @type array $metadata__in Array of metadata IDs or Slugs to be retrieved. Default none
*
* @type array $metadata__not_in Array of metadata IDs (slugs not accepted) to excluded. Default none
*
* @type bool $exclude_title Exclude the Core Title Metadata from result. Default false
*
* @type bool $exclude_description Exclude the Core Description Metadata from result. Default false
*
* @type bool $exclude_core Exclude Core Metadata (title and description) from result. Default false
*
* @type bool $hide_empty Wether to hide or not fields the item has no value to
* Default: true
* @type string $before_title String to be added before each metadata title
* Default '<h3>'
* @type string $after_title String to be added after each metadata title
* Default '</h3>'
* @type string $before_value String to be added before each metadata value
* Default '<p>'
* @type string $after_value String to be added after each metadata value
* Default '</p>'
* }
*
* @return string The HTML output
* @throws \Exception
*/
public function get_metadata_as_html($field = null, $args = array()) {
public function get_metadata_as_html($args = array()) {
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
$Tainacan_Fields = \Tainacan\Repositories\Fields::get_instance();
@ -481,6 +492,12 @@ class Item extends Entity {
$return = '';
$defaults = array(
'metadata' => null,
'metadata__in' => null,
'metadata__not_in' => null,
'exclude_title' => false,
'exclude_description' => false,
'exclude_core' => false,
'hide_empty' => true,
'before_title' => '<h3>',
'after_title' => '</h3>',
@ -489,7 +506,7 @@ class Item extends Entity {
);
$args = wp_parse_args($args, $defaults);
if (!is_null($field)) {
if (!is_null($args['metadata'])) {
$field_object = null;
@ -505,6 +522,16 @@ class Item extends Entity {
}
if ( $field_object instanceof \Tainacan\Entities\Field ) {
if ( is_array($args['metadata__not_in'])
&& (
in_array($field_object->get_slug(), $args['metadata__not_in']) ||
in_array($field_object->get_id(), $args['metadata__not_in'])
)
) {
return $return;
}
$item_meta = new \Tainacan\Entities\Item_Metadata_Entity($this, $field_object);
if ($item_meta->has_value() || !$args['hide_empty']) {
$return .= $args['before_title'] . $field_object->get_name() . $args['after_title'];
@ -516,10 +543,57 @@ class Item extends Entity {
return $return;
}
$query_args = [];
$post__in = [];
$post__not_in = [];
$post__name_in = [];
if (is_array($args['metadata__in'])) {
foreach ($args['metadata__in'] as $meta) {
if (is_string($meta)) {
$post__name_in[] = $meta;
} elseif (is_integer($meta)) {
$post__in[] = $meta;
}
}
}
if (is_array($args['metadata__not_in'])) {
foreach ($args['metadata__not_in'] as $meta) {
if (is_integer($meta)) {
$post__not_in[] = $meta;
}
}
}
if (sizeof($post__in) > 0) {
$query_args['post__in'] = $post__in;
}
if (sizeof($post__not_in) > 0) {
$query_args['post__not_in'] = $post__not_in;
}
if (sizeof($post__name_in) > 0) {
$query_args['post__name_in'] = $post__name_in;
}
$fields = $this->get_fields();
$fields = $this->get_fields($query_args);
foreach ( $fields as $item_meta ) {
$fto = $item_meta->get_field()->get_field_type_object();
if ( $fto->get_core() ) {
if ( $args['exclude_core'] ) {
continue;
} elseif ( $args['exclude_title'] && $fto->get_related_mapped_prop() == 'title' ) {
continue;
} elseif ( $args['exclude_description'] && $fto->get_related_mapped_prop() == 'description' ) {
continue;
}
}
if ($item_meta->has_value() || !$args['hide_empty']) {
$return .= $args['before_title'] . $item_meta->get_field()->get_name() . $args['after_title'];
$return .= $args['before_value'] . $item_meta->get_value_as_html() . $args['after_value'];

View File

@ -13,25 +13,35 @@ use \Tainacan\Repositories;
* If an ID, a slug or a Tainacan\Entities\Field object is passed, it returns only one metadata, otherwise
* it returns all metadata
*
* @param int|string|Tainacan\Entities\Field $field Field object, ID or slug to retrieve only one field. empty returns all fields
* @param array|string $args
* @param array|string $args {
* Optional. Array or string of arguments.
*
* @type bool $hide_empty Wether to hide or not fields the item has no value to
* Default: true
* @type string $before_title String to be added before each metadata title
* Default '<h3>'
* @type string $after_title String to be added after each metadata title
* Default '</h3>'
* @type string $before_value String to be added before each metadata value
* Default '<p>'
* @type string $after_value String to be added after each metadata value
* Default '</p>'
* }
* Optional. Array or string of arguments.
*
* @type mixed $metadata Field object, ID or slug to retrieve only one field. empty returns all metadata
*
* @type array $metadata__in Array of metadata IDs or Slugs to be retrieved. Default none
*
* @type array $metadata__not_in Array of metadata IDs (slugs not accepted) to excluded. Default none
*
* @type bool $exclude_title Exclude the Core Title Metadata from result. Default false
*
* @type bool $exclude_description Exclude the Core Description Metadata from result. Default false
*
* @type bool $exclude_core Exclude Core Metadata (title and description) from result. Default false
*
* @type bool $hide_empty Wether to hide or not fields the item has no value to
* Default: true
* @type string $before_title String to be added before each metadata title
* Default '<h3>'
* @type string $after_title String to be added after each metadata title
* Default '</h3>'
* @type string $before_value String to be added before each metadata value
* Default '<p>'
* @type string $after_value String to be added after each metadata value
* Default '</p>'
* }
* @return string The HTML output
*/
function tainacan_get_the_metadata($field = null, $args = array()) {
function tainacan_get_the_metadata($args = array()) {
$post = get_post();
$theme_helper = \Tainacan\Theme_Helper::get_instance();
@ -40,7 +50,7 @@ function tainacan_get_the_metadata($field = null, $args = array()) {
$item = new Entities\Item($post);
return $item->get_metadata_as_html($field, $args);
return $item->get_metadata_as_html($args);
}