From 8ef4cbc7a343fb100524d681c3f4447b238ac7b2 Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Thu, 16 Aug 2018 10:57:07 -0300 Subject: [PATCH] add post comment status support --- .../entities/class-tainacan-collection.php | 20 ++++++++++++- src/classes/entities/class-tainacan-item.php | 17 +++++++++++ .../class-tainacan-collections.php | 10 ++++++- .../repositories/class-tainacan-items.php | 28 ++++++++++++++++++- 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/src/classes/entities/class-tainacan-collection.php b/src/classes/entities/class-tainacan-collection.php index 3419fc330..315b5b789 100644 --- a/src/classes/entities/class-tainacan-collection.php +++ b/src/classes/entities/class-tainacan-collection.php @@ -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 diff --git a/src/classes/entities/class-tainacan-item.php b/src/classes/entities/class-tainacan-item.php index 0aa2a6fe5..e90494c5e 100644 --- a/src/classes/entities/class-tainacan-item.php +++ b/src/classes/entities/class-tainacan-item.php @@ -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); + } /** * diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index 8eec98c6b..21c800cda 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -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' ] )), + ] ] ); } diff --git a/src/classes/repositories/class-tainacan-items.php b/src/classes/repositories/class-tainacan-items.php index ef18d3c03..238989c5f 100644 --- a/src/classes/repositories/class-tainacan-items.php +++ b/src/classes/repositories/class-tainacan-items.php @@ -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; + } } \ No newline at end of file