Status.php
1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace ACP\Editing\Service\Comment;
use AC\Request;
use ACP\Editing\Service;
use ACP\Editing\View;
use RuntimeException;
class Status implements Service {
public function get_view( $context ) {
return new View\Select( [
'1' => __( 'Approved' ),
'0' => __( 'Pending' ),
'spam' => __( 'Spam' ),
'trash' => __( 'Trash' ),
] );
}
public function get_value( $id ) {
return get_comment( $id )->comment_approved;
}
/**
* @param int $id
* @param string $status
*
* @return bool
*/
private function set_comment_status( $id, $status ) {
$result = wp_set_comment_status( $id, $status );
if ( is_wp_error( $result ) ) {
throw new RuntimeException( $result->get_error_message() );
}
return $result;
}
public function update( Request $request ) {
switch ( $request->get( 'value', '' ) ) {
case 'trash' :
return wp_trash_comment( $request->get( 'id' ) );
case 'spam' :
return wp_spam_comment( $request->get( 'id' ) );
case '1' :
return $this->set_comment_status( $request->get( 'id' ), 'approve' );
case '0' :
return $this->set_comment_status( $request->get( 'id' ), 'hold' );
default:
return false;
}
}
}