Skip to content

Commit

Permalink
(chore): add comments to rest api
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinsiebel committed Aug 11, 2020
1 parent a1e6aaf commit 40afbcd
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 25 deletions.
18 changes: 9 additions & 9 deletions bitbucket-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ pipelines:
script:
- composer install --no-interaction --no-progress --prefer-dist --ignore-platform-reqs
- ./vendor/bin/phpunit --testsuite "Unit Test Suite"
- step:
name: Deploy docs to production
caches:
- composer
- vendor-directory
deployment: production
script:
- apt-get update && apt-get install -y rsync
- ssh [email protected] mkdir -p /data/www/docs.openwebconcept.nl/htdocs/plugins/openpub && rsync -avH ./docs/* -e "ssh" [email protected]:/data/www/docs.openwebconcept.nl/htdocs/plugins/openpub
# - step:
# name: Deploy docs to production
# caches:
# - composer
# - vendor-directory
# deployment: production
# script:
# - apt-get update && apt-get install -y rsync
# - ssh [email protected] mkdir -p /data/www/docs.openwebconcept.nl/htdocs/plugins/openpub && rsync -avH ./docs/* -e "ssh" [email protected]:/data/www/docs.openwebconcept.nl/htdocs/plugins/openpub

definitions:
caches:
Expand Down
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"description": "OpenPub base plugin",
"authors": [
{
"name": "Yard Internet",
"name": "Yard Digital Agency",
"email": "[email protected]",
"homepage": "https://www.yardinternet.nl"
"homepage": "https://www.yard.nl"
}
],
"type": "wordpress-plugin",
Expand All @@ -24,6 +24,7 @@
"phpunit/phpunit": "~8.0",
"10up/wp_mock": "dev-master",
"friendsofphp/php-cs-fixer": "^2.0",
"phpstan/phpstan": "^0.12",
"szepeviktor/phpstan-wordpress": "^0.6.0"
},
"autoload": {
Expand All @@ -37,14 +38,13 @@
}
},
"scripts": {
"unit": "clear && ./vendor/bin/phpunit --stderr --testsuite 'Unit Test Suite' --colors=always",
"unit-coverage": "clear && ./vendor/bin/phpunit --stderr --testsuite 'Unit Test Suite' --prepend tests/xdebug-filter.php --colors=always --coverage-html ./tests/coverage",
"integration": "clear && ./vendor/bin/phpunit --stderr --testsuite 'Integration Test Suite' --colors=always",
"cs": "./vendor/bin/php-cs-fixer fix",
"docs": "./vendor/bin/phpdoc -d ./src/Base -t ./docs",
"stan": "./vendor/bin/phpstan analyse src tests",
"test": [
"@unit",
"@integration"
"@unit"
],
"cs": "./vendor/bin/php-cs-fixer fix",
"phpstan": "./vendor/bin/phpstan analyse --no-progress"
"unit": "clear && ./vendor/bin/phpunit --testsuite 'Unit Test Suite' --colors=always",
"unit-coverage": "clear && ./vendor/bin/phpunit --testsuite 'Unit Test Suite' --colors=always --coverage-html ./tests/coverage"
}
}
4 changes: 4 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions config/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
*/
'item' => [
'fields' => [
'comments' => OWC\OpenPub\Base\RestAPI\ItemFields\CommentField::class,
'connected' => OWC\OpenPub\Base\RestAPI\ItemFields\ConnectedField::class,
'downloads' => OWC\OpenPub\Base\RestAPI\ItemFields\DownloadsField::class,
'expired' => OWC\OpenPub\Base\RestAPI\ItemFields\ExpiredField::class,
'highlighted' => OWC\OpenPub\Base\RestAPI\ItemFields\HighlightedItemField::class,
'taxonomies' => OWC\OpenPub\Base\RestAPI\ItemFields\TaxonomyField::class,
'image' => OWC\OpenPub\Base\RestAPI\ItemFields\FeaturedImageField::class,
'downloads' => OWC\OpenPub\Base\RestAPI\ItemFields\DownloadsField::class,
'links' => OWC\OpenPub\Base\RestAPI\ItemFields\LinksField::class,
'synonyms' => OWC\OpenPub\Base\RestAPI\ItemFields\SynonymsField::class,
'notes' => OWC\OpenPub\Base\RestAPI\ItemFields\NotesField::class,
'synonyms' => OWC\OpenPub\Base\RestAPI\ItemFields\SynonymsField::class,
'taxonomies' => OWC\OpenPub\Base\RestAPI\ItemFields\TaxonomyField::class,

],
],
'theme' => [
Expand All @@ -31,13 +33,13 @@
'search' => [
'fields' => [
'connected' => OWC\OpenPub\Base\RestAPI\ItemFields\ConnectedField::class,
'downloads' => OWC\OpenPub\Base\RestAPI\ItemFields\DownloadsField::class,
'expired' => OWC\OpenPub\Base\RestAPI\ItemFields\ExpiredField::class,
'taxonomies' => OWC\OpenPub\Base\RestAPI\ItemFields\TaxonomyField::class,
'image' => OWC\OpenPub\Base\RestAPI\ItemFields\FeaturedImageField::class,
'downloads' => OWC\OpenPub\Base\RestAPI\ItemFields\DownloadsField::class,
'links' => OWC\OpenPub\Base\RestAPI\ItemFields\LinksField::class,
'synonyms' => OWC\OpenPub\Base\RestAPI\ItemFields\SynonymsField::class,
'notes' => OWC\OpenPub\Base\RestAPI\ItemFields\NotesField::class,
'synonyms' => OWC\OpenPub\Base\RestAPI\ItemFields\SynonymsField::class,
'taxonomies' => OWC\OpenPub\Base\RestAPI\ItemFields\TaxonomyField::class,
],
],
],
Expand Down
2 changes: 1 addition & 1 deletion config/posttypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
'public' => true,
'show_ui' => true,
'supports' => ['title', 'editor', 'thumbnail', 'excerpt', 'revisions'],
'supports' => ['title', 'editor', 'thumbnail', 'excerpt', 'revisions', 'comments'],
'menu_icon' => 'dashicons-format-aside',
'show_in_rest' => true,
'admin_cols' => [
Expand Down
57 changes: 57 additions & 0 deletions src/Base/RestAPI/ItemFields/CommentField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace OWC\OpenPub\Base\RestAPI\ItemFields;

class CommentField extends \OWC\OpenPub\Base\Support\CreatesFields
{
/**
* Creates an array of comments.
*
* @param \WP_Post $post
*
* @return array
*/
public function create(\WP_Post $post): array
{
$result = [];
$result['count'] = (int) $post->comment_count;
$result['status'] = $post->comment_status;
$result['items'] = [];

if (!in_array($post->comment_status, ['open'])) {
return $result;
}

$result['items'] = $this->getComments($post->ID);

return $result;
}

/**
* Get comment items of a post.
*
* @param int $postID
*
* @return array
*/
protected function getComments(int $postID): array
{
$comments = get_comments([
'post_id' => $postID,
'status' => 'approve'
]);

if (! $comments) {
return [];
}

return array_map(function (\WP_Comment $comment) {
return [
'id' => $comment->comment_ID,
'author' => $comment->comment_author,
'content' => $comment->comment_content,
'date' => $comment->comment_date
];
}, $comments);
}
}
7 changes: 7 additions & 0 deletions tests/Unit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
'active_plugins' => ['openpub-base/openpub-base.php'],
];

class WP_CLI
{
public static function add_command()
{
}
}

if (! function_exists('get_echo')) {

/**
Expand Down

0 comments on commit 40afbcd

Please sign in to comment.