add filters to entity methods

This commit is contained in:
Leo Germani 2018-11-24 14:28:16 -02:00
parent 0594b5c897
commit 201f8978fa
14 changed files with 121 additions and 44 deletions

View File

@ -65,7 +65,7 @@ class Collection extends Entity {
static $db_identifier_sufix = '_item';
public function __toString() {
return 'Hello, my name is ' . $this->get_name();
return apply_filters("tainacan-collection-to-string", $this->get_name(), $this);
}
public function _toArray() {
@ -78,7 +78,7 @@ class Collection extends Entity {
$array_collection['creation_date'] = $this->get_date_i18n( explode( ' ', $array_collection['creation_date'] )[0] );
$array_collection['modification_date'] = $this->get_date_i18n( explode( ' ', $array_collection['modification_date'] )[0] );
return $array_collection;
return apply_filters('tainacan-collection-to-array', $array_collection, $this);
}
/**
@ -200,21 +200,22 @@ class Collection extends Entity {
}
}
return $attachments_prepared;
return apply_filters("tainacan-collection-get-attachments", $attachments_prepared, $exclude, $this);
}
/**
* @return string
*/
function get_author_name() {
return get_the_author_meta( 'display_name', $this->get_author_id() );
$name = get_the_author_meta( 'display_name', $this->get_author_id() );
return apply_filters("tainacan-collection-get-author-name", $name, $this);
}
/**
* @return array
*/
function get_thumbnail() {
return array(
$thumbs = array(
'thumb' => get_the_post_thumbnail_url( $this->get_id(), 'thumbnail' ),
'full' => get_the_post_thumbnail_url( $this->get_id(), 'full' ),
'medium' => get_the_post_thumbnail_url( $this->get_id(), 'medium' ),
@ -224,13 +225,15 @@ class Collection extends Entity {
'tainacan_medium' => get_the_post_thumbnail_url( $this->get_id(), 'tainacan-medium' ),
'tainacan_medium_full' => get_the_post_thumbnail_url( $this->get_id(), 'tainacan-medium-full' ),
);
return apply_filters("tainacan-collection-get-thumbnail", $thumbs, $this);
}
/**
* @return false|string
*/
function get_header_image(){
return wp_get_attachment_url( $this->get_header_image_id() );
$header_image = wp_get_attachment_url( $this->get_header_image_id() );
return apply_filters("tainacan-collection-get-header-image", $header_image, $this);
}
/**

View File

@ -160,8 +160,9 @@ class Entity {
//prop is not set at object, try to get from database
$repository = $this->get_repository();
$value = $repository->get_mapped_property($this, $prop);
return $repository->get_mapped_property($this, $prop);
return apply_filters('tainacan-entity-get-property', $value, $prop, $this);
}
/**
@ -175,6 +176,7 @@ class Entity {
*/
protected function set_mapped_property($prop, $value) {
$this->set_validated(false);
$value = apply_filters('tainacan-entity-set-property', $value, $prop, $this);
$this->$prop = $value;
}
@ -358,8 +360,10 @@ class Entity {
foreach($map as $prop => $content) {
$attributes[$prop] = $this->get_mapped_property($prop);
}
return $attributes;
$hook_prefix = self::get_post_type();
return apply_filters("{$hook_prefix}-to-array", $attributes, $this);
}
public function _toJson(){

View File

@ -31,7 +31,7 @@ class Filter extends Entity {
protected $repository = 'Filters';
public function __toString(){
return 'Hello, my name is '. $this->get_name();
return apply_filters("tainacan-filter-to-string", $this->get_name(), $this);
}
/**
@ -51,8 +51,7 @@ class Filter extends Entity {
$filter_array['metadatum']['metadata_type_object'] = $meta_object->_toArray();
}
return $filter_array;
return apply_filters('tainacan-filter-to-array', $filter_array, $this);
}
/**

View File

@ -46,6 +46,36 @@ class Item_Metadata_Entity extends Entity {
}
/**
* Gets the string used before each value when concatenating multiple values
* to display item metadata value as html or string
*
* @return string
*/
public function get_multivalue_prefix() {
return apply_filters('tainacan-item-metadata-get-multivalue-prefix', '', $this);
}
/**
* Gets the string used after each value when concatenating multiple values
* to display item metadata value as html or string
*
* @return string
*/
public function get_multivalue_suffix() {
return apply_filters('tainacan-item-metadata-get-multivalue-suffix', '', $this);
}
/**
* Gets the string used in between each value when concatenating multiple values
* to display item metadata value as html or string
*
* @return string
*/
public function get_multivalue_separator() {
return apply_filters('tainacan-item-metadata-get-multivalue-separator', ', ', $this);
}
/**
* Get the value as a HTML string, with markup and links
* @return string
@ -72,13 +102,21 @@ class Item_Metadata_Entity extends Entity {
$total = sizeof($value);
$count = 0;
$prefix = $item_metadata->get_multivalue_prefix();
$suffix = $item_metadata->get_multivalue_suffix();
$separator = $item_metadata->get_multivalue_separator();
foreach ($value as $v) {
$return .= $prefix;
$return .= (string) $v;
$return .= $suffix;
$count ++;
if ($count < $total)
$return .= ', ';
$return .= $separator;
}
} else {
@ -150,7 +188,7 @@ class Item_Metadata_Entity extends Entity {
$as_array['item'] = $this->get_item()->_toArray();
$as_array['metadatum'] = $this->get_metadatum()->_toArray();
return $as_array;
return apply_filters('tainacan-item-metadata-to-array', $as_array, $this);
}
/**

View File

@ -46,7 +46,7 @@ class Item extends Entity {
}
public function __toString() {
return 'Hello, my name is ' . $this->get_title();
return apply_filters("tainacan-item-to-string", $this->get_title(), $this);
}
public function _toArray() {
@ -59,7 +59,7 @@ class Item extends Entity {
$array_item['creation_date'] = $this->get_date_i18n( explode( ' ', $array_item['creation_date'] )[0] );
$array_item['modification_date'] = $this->get_date_i18n( explode( ' ', $array_item['modification_date'] )[0] );
return $array_item;
return apply_filters('tainacan-item-to-array', $array_item, $this);
}
/**
@ -114,7 +114,7 @@ class Item extends Entity {
}
}
return $attachments_prepared;
return apply_filters("tainacan-item-get-attachments", $attachments_prepared, $exclude, $this);
}
@ -122,14 +122,15 @@ class Item extends Entity {
* @return string
*/
function get_author_name() {
return get_the_author_meta( 'display_name', $this->get_author_id() );
$name = get_the_author_meta( 'display_name', $this->get_author_id() );
return apply_filters("tainacan-item-get-author-name", $name, $this);
}
/**
* @return array
*/
function get_thumbnail() {
return array(
$thumbs = array(
'thumb' => get_the_post_thumbnail_url( $this->get_id(), 'thumbnail' ),
'full' => get_the_post_thumbnail_url( $this->get_id(), 'full' ),
'medium' => get_the_post_thumbnail_url( $this->get_id(), 'medium' ),
@ -139,6 +140,7 @@ class Item extends Entity {
'tainacan_medium' => get_the_post_thumbnail_url( $this->get_id(), 'tainacan-medium' ),
'tainacan_medium_full' => get_the_post_thumbnail_url( $this->get_id(), 'tainacan-medium-full' ),
);
return apply_filters("tainacan-item-get-thumbnail", $thumbs, $this);
}
/**
@ -661,9 +663,6 @@ class Item extends Entity {
$embed = $wp_embed->autoembed($url);
if ( $embed == $url ) {
// No embed handler found
// TODO: Add filter to allow customization
$output .= sprintf("<a href='%s' target='blank'>%s</a>", $url, $url);
} else {
$output .= $embed;
@ -674,7 +673,7 @@ class Item extends Entity {
}
return $output;
return apply_filters("tainacan-item-get-document-html", $output, $img_size, $this);
}

View File

@ -38,7 +38,7 @@ class Log extends Entity {
}
public function __toString() {
return 'Hello, my title is ' . $this->get_title();
return apply_filters("tainacan-log-to-string", $this->get_title(), $this);
}
/**
@ -49,7 +49,7 @@ class Log extends Entity {
$array_log['user_name'] = $this->get_user_name();
return $array_log;
return apply_filters('tainacan-log-to-array', $array_log, $this);
}

View File

@ -48,7 +48,7 @@ class Metadatum extends Entity {
protected $repository = 'Metadata';
public function __toString(){
return 'Hello, my name is '. $this->get_name();
return apply_filters("tainacan-metadatum-to-string", $this->get_name(), $this);
}

View File

@ -45,7 +45,7 @@ class Taxonomy extends Entity {
static $db_identifier_prefix = 'tnc_tax_';
public function __toString(){
return 'Hello, my name is '. $this->get_name();
return apply_filters("tainacan-taxonomy-to-string", $this->get_name(), $this);
}
/**

View File

@ -52,7 +52,7 @@ class Term extends Entity {
}
public function __toString(){
return 'Hello, my name is '. $this->get_name();
return apply_filters("tainacan-term-to-string", $this->get_name(), $this);
}
public function _toArray(){
@ -67,7 +67,7 @@ class Term extends Entity {
$term_array['header_image'] = $this->get_header_image();
$term_array['url'] = get_term_link( $this->get_id() );
return $term_array;
return apply_filters('tainacan-term-to-array', $term_array, $this);
}
// Getters

View File

@ -93,11 +93,16 @@ class Core_Description extends Metadata_Type {
if ( $item_metadata->is_multiple() ) {
$total = sizeof($value);
$count = 0;
$prefix = $item_metadata->get_multivalue_prefix();
$suffix = $item_metadata->get_multivalue_suffix();
$separator = $item_metadata->get_multivalue_separator();
foreach ( $value as $el ) {
$return .= $prefix;
$return .= nl2br($this->make_clickable_links($el));
$return .= $suffix;
$count ++;
if ($count < $total)
$return .= ', ';
$return .= $separator;
}
} else {
$return = nl2br($this->make_clickable_links($value));

View File

@ -84,6 +84,9 @@ class Relationship extends Metadata_Type {
$count = 1;
$total = sizeof($value);
$prefix = $item_metadata->get_multivalue_prefix();
$suffix = $item_metadata->get_multivalue_suffix();
$separator = $item_metadata->get_multivalue_separator();
foreach ( $value as $item_id ) {
@ -91,16 +94,23 @@ class Relationship extends Metadata_Type {
$item = new \Tainacan\Entities\Item($item_id);
if ( $item instanceof \Tainacan\Entities\Item ) {
$return .= $item->_toHtml();
}
$count ++;
if ( $count <= $total ) {
$return .= ', ';
if ( $item instanceof \Tainacan\Entities\Item ) {
$return .= $prefix;
$return .= $item->_toHtml();
$return .= $suffix;
if ( $count <= $total ) {
$return .= $separator;
}
}
} catch (Exception $e) {
// item not found
}

View File

@ -154,18 +154,27 @@ class Taxonomy extends Metadata_Type {
$count = 1;
$total = sizeof($value);
$prefix = $item_metadata->get_multivalue_prefix();
$suffix = $item_metadata->get_multivalue_suffix();
$separator = $item_metadata->get_multivalue_separator();
foreach ( $value as $term ) {
if ( $term instanceof \Tainacan\Entities\Term ) {
$return .= $term->_toHtml();
}
$count ++;
if ( $count <= $total ) {
$return .= ', ';
if ( $term instanceof \Tainacan\Entities\Term ) {
$return .= $prefix;
$return .= $term->_toHtml();
$return .= $suffix;
if ( $count <= $total ) {
$return .= $separator;
}
}
}
} else {

View File

@ -41,11 +41,16 @@ class Text extends Metadata_Type {
if ( $item_metadata->is_multiple() ) {
$total = sizeof($value);
$count = 0;
$prefix = $item_metadata->get_multivalue_prefix();
$suffix = $item_metadata->get_multivalue_suffix();
$separator = $item_metadata->get_multivalue_separator();
foreach ( $value as $el ) {
$return .= $prefix;
$return .= $this->make_clickable_links($el);
$return .= $suffix;
$count ++;
if ($count < $total)
$return .= ', ';
$return .= $separator;
}
} else {
$return = $this->make_clickable_links($value);

View File

@ -42,11 +42,16 @@ class Textarea extends Metadata_Type {
if ( $item_metadata->is_multiple() ) {
$total = sizeof($value);
$count = 0;
$prefix = $item_metadata->get_multivalue_prefix();
$suffix = $item_metadata->get_multivalue_suffix();
$separator = $item_metadata->get_multivalue_separator();
foreach ( $value as $el ) {
$return .= $prefix;
$return .= nl2br($this->make_clickable_links($el));
$return .= $suffix;
$count ++;
if ($count < $total)
$return .= ', ';
$return .= $separator;
}
} else {
$return = nl2br($this->make_clickable_links($value));