| 1 |
<?php |
| 2 |
namespace FL\Assistant\Controllers; |
| 3 |
use FL\Assistant\Data\Repository\CommentsRepository; |
| 4 |
use FL\Assistant\Data\Repository\PostsRepository; |
| 5 |
use FL\Assistant\Data\Transformers\CommentTransformer; |
| 6 |
use FL\Assistant\System\Contracts\ControllerAbstract; |
| 7 |
use WP_REST_Server; |
| 8 |
|
| 9 |
/**0 |
| 10 |
* REST API logic for comments. |
| 11 |
*/ |
| 12 |
class CommentsController extends ControllerAbstract { |
| 13 |
|
| 14 |
|
| 15 |
protected $posts; |
| 16 |
|
| 17 |
protected $comments; |
| 18 |
|
| 19 |
protected $transformer; |
| 20 |
|
| 21 |
public function __construct( |
| 22 |
PostsRepository $posts, |
| 23 |
CommentsRepository $comments, |
| 24 |
CommentTransformer $transformer |
| 25 |
) { |
| 26 |
$this->posts = $posts; |
| 27 |
$this->comments = $comments; |
| 28 |
$this->transformer = $transformer; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Register routes. |
| 33 |
*/ |
| 34 |
public function register_routes() { |
| 35 |
$this->route( |
| 36 |
'/comments', |
| 37 |
[ |
| 38 |
[ |
| 39 |
'methods' => WP_REST_Server::READABLE, |
| 40 |
'callback' => [ $this, 'index' ], |
| 41 |
'permission_callback' => function () { |
| 42 |
return current_user_can( 'moderate_comments' ); |
| 43 |
}, |
| 44 |
], |
| 45 |
] |
| 46 |
); |
| 47 |
|
| 48 |
$this->route( |
| 49 |
'/comments/count', |
| 50 |
[ |
| 51 |
[ |
| 52 |
'methods' => WP_REST_Server::READABLE, |
| 53 |
'callback' => [ $this, 'comments_count' ], |
| 54 |
'permission_callback' => function () { |
| 55 |
return current_user_can( 'moderate_comments' ); |
| 56 |
}, |
| 57 |
], |
| 58 |
] |
| 59 |
); |
| 60 |
|
| 61 |
$this->route( |
| 62 |
'/comments/(?P<id>\d+)', |
| 63 |
[ |
| 64 |
[ |
| 65 |
'methods' => WP_REST_Server::READABLE, |
| 66 |
'callback' => [ $this, 'read' ], |
| 67 |
'args' => [ |
| 68 |
'id' => [ |
| 69 |
'required' => true, |
| 70 |
'type' => 'number', |
| 71 |
], |
| 72 |
], |
| 73 |
'permission_callback' => function () { |
| 74 |
return current_user_can( 'moderate_comments' ); |
| 75 |
}, |
| 76 |
], |
| 77 |
[ |
| 78 |
'methods' => WP_REST_Server::CREATABLE, |
| 79 |
'callback' => [ $this, 'update' ], |
| 80 |
'args' => [ |
| 81 |
'id' => [ |
| 82 |
'required' => true, |
| 83 |
'type' => 'number', |
| 84 |
], |
| 85 |
'action' => [ |
| 86 |
'required' => true, |
| 87 |
'type' => 'string', |
| 88 |
], |
| 89 |
], |
| 90 |
'permission_callback' => function () { |
| 91 |
return current_user_can( 'moderate_comments' ); |
| 92 |
}, |
| 93 |
], |
| 94 |
] |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns an array of comments and related data. |
| 100 |
*/ |
| 101 |
public function index( $request ) { |
| 102 |
$params = $request->get_params(); |
| 103 |
|
| 104 |
$post_types = array_keys( $this->posts->get_types() ); |
| 105 |
$args = array_merge( [ 'post_type' => $post_types ], $params ); |
| 106 |
|
| 107 |
return $this->comments->paginate( $args ) |
| 108 |
->apply_transform( $this->transformer ) |
| 109 |
->to_rest_response(); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Returns the number of comments found given |
| 114 |
* the current args. |
| 115 |
*/ |
| 116 |
public function comments_count( $request ) { |
| 117 |
$counts = wp_count_comments(); |
| 118 |
|
| 119 |
return rest_ensure_response( |
| 120 |
[ |
| 121 |
'approved' => $counts->approved, |
| 122 |
'pending' => $counts->moderated, |
| 123 |
'spam' => $counts->spam, |
| 124 |
'trash' => $counts->trash, |
| 125 |
'total' => $counts->total_comments, |
| 126 |
] |
| 127 |
); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Returns data for a single comment. |
| 132 |
*/ |
| 133 |
public function read( $request ) { |
| 134 |
$id = $request->get_param( 'id' ); |
| 135 |
$comment = $this->comments->find( $id, $this->transformer ); |
| 136 |
|
| 137 |
return rest_ensure_response( $comment ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Updates a single comment based on the specified action. |
| 142 |
*/ |
| 143 |
public function update( $request ) { |
| 144 |
$id = $request->get_param( 'id' ); |
| 145 |
$action = $request->get_param( 'action' ); |
| 146 |
$comment = get_comment( $id ); |
| 147 |
$data = $request->get_param( 'data' ); |
| 148 |
|
| 149 |
switch ( $action ) { |
| 150 |
case 'approve': |
| 151 |
wp_set_comment_status( $comment, 'approve' ); |
| 152 |
break; |
| 153 |
case 'unapprove': |
| 154 |
wp_set_comment_status( $comment, 'hold' ); |
| 155 |
break; |
| 156 |
case 'spam': |
| 157 |
wp_spam_comment( $comment ); |
| 158 |
break; |
| 159 |
case 'unspam': |
| 160 |
wp_unspam_comment( $comment ); |
| 161 |
break; |
| 162 |
case 'trash': |
| 163 |
if ( ! EMPTY_TRASH_DAYS ) { |
| 164 |
wp_delete_comment( $comment ); |
| 165 |
} else { |
| 166 |
wp_trash_comment( $comment ); |
| 167 |
} |
| 168 |
break; |
| 169 |
case 'untrash': |
| 170 |
wp_untrash_comment( $comment ); |
| 171 |
break; |
| 172 |
case 'content': |
| 173 |
wp_update_comment( |
| 174 |
[ |
| 175 |
'comment_ID' => $id, |
| 176 |
'comment_content' => $data['content'], |
| 177 |
] |
| 178 |
); |
| 179 |
break; |
| 180 |
} |
| 181 |
$comment = get_comment( $id ); |
| 182 |
return rest_ensure_response( |
| 183 |
[ |
| 184 |
'success' => true, |
| 185 |
'commentData' => $comment, |
| 186 |
] |
| 187 |
); |
| 188 |
} |
| 189 |
} |
| 190 |
|