add post comment status support

This commit is contained in:
Jacson Passold 2018-08-16 10:57:07 -03:00
parent f743e0d77d
commit 8ef4cbc7a3
4 changed files with 72 additions and 3 deletions

View File

@ -33,7 +33,8 @@ class Collection extends Entity {
$cover_page_id,
$header_image_id,
$header_image,
$moderators_ids;
$moderators_ids,
$comment_status;
/**
* {@inheritDoc}
@ -487,6 +488,14 @@ class Collection extends Entity {
return $repo->get_core_description_metadatum($this);
}
/**
* Checks if comments are allowed for the current Collection.
* @return string "open"|"closed"
*/
public function get_comment_status() {
return $this->get_mapped_property('comment_status');
}
/**
* Set the collection name
@ -679,6 +688,15 @@ class Collection extends Entity {
$this->set_mapped_property( 'moderators_ids', $value );
}
/**
* Sets if comments are allowed for the current Collection.
*
* @param $value string "open"|"closed"
*/
public function set_comment_status( $value ) {
$this->set_mapped_property('comment_status', $value);
}
// Moderators methods

View File

@ -266,6 +266,14 @@ class Item extends Entity {
public function get_capabilities() {
return $this->get_collection()->get_items_capabilities();
}
/**
* Checks if comments are allowed for the current Collection.
* @return string "open"|"closed"
*/
public function get_comment_status() {
return apply_filters('comments_open', $this->get_mapped_property('comment_status'), $this->get_id());
}
/**
* Define the title
@ -358,6 +366,15 @@ class Item extends Entity {
$this->cap = $item_collection->get_items_capabilities();
}
}
/**
* Sets if comments are allowed for the current Item.
*
* @param $value string "open"|"closed"
*/
public function set_comment_status( $value ) {
$this->set_mapped_property('comment_status', $value);
}
/**
*

View File

@ -194,7 +194,15 @@ class Collections extends Repository {
'map' => 'meta',
'title' => __( 'Thumbnail', 'tainacan' ),
'description' => __( 'Squared reduced-size version of a picture that helps recognizing and organizing files', 'tainacan' )
]
],
'comment_status' => [
'map' => 'comment_status',
'title' => __( 'Comment Status', 'tainacan' ),
'type' => 'string',
'description' => __( 'The status of collection comment, if is allowed, so is "open", or is "closed".', 'tainacan' ),
'default' => get_default_comment_status(Entities\Collection::get_post_type()),
'validation' => v::optional(stringType()->in( [ 'open', 'closed' ] )),
]
] );
}

View File

@ -25,6 +25,7 @@ class Items extends Repository {
parent::__construct();
add_filter( 'posts_where', array( &$this, 'title_in_posts_where' ), 10, 2 );
add_filter( 'posts_where', array( &$this, 'content_in_posts_where' ), 10, 2 );
add_filter( 'comments_open', [$this, 'hook_comments_open']);
add_action( 'tainacan-api-item-updated', array( &$this, 'hook_api_updated_item' ), 10, 2 );
}
@ -105,7 +106,15 @@ class Items extends Repository {
'map' => 'meta',
'title' => __( 'Thumbnail', 'tainacan' ),
'description' => __( 'Squared reduced-size version of a picture that helps recognizing and organizing files', 'tainacan' )
]
],
'comment_status' => [
'map' => 'comment_status',
'title' => __( 'Comment Status', 'tainacan' ),
'type' => 'string',
'description' => __( 'The status of item comment, if is allowed, so is "open" or is "closed".', 'tainacan' ),
'default' => get_default_comment_status(Entities\Collection::get_post_type()),
'validation' => v::optional(stringType()->in( [ 'open', 'closed' ] )),
]
] );
}
@ -466,5 +475,22 @@ class Items extends Repository {
}
/**
* Return if comment are open for this item (post_id) and the collection too
*
* @param bool $open_comment
* @param integer $post_id Item id
* @return bool
*/
public function hook_comments_open($open_comment, $post_id) {
$item = self::get_entity_by_post($post_id);
if($item != false && $item instanceof Entities\Item) {
$collection = $item->get_collection();
if($collection->get_comment_status() !== 'open' ) return false;
}
return $open_comment;
}
}