Comment.php
1.58 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace ACP\Editing\RequestHandler\Query;
use AC;
use AC\Request;
use ACP\Editing\ApplyFilter\RowsPerIteration;
use ACP\Editing\RequestHandler;
use ACP\Editing\Response;
use WP_Comment_Query;
final class Comment implements RequestHandler {
/**
* @var Request
*/
private $request;
/**
* @var AC\Ajax\Handler
*/
private $handler;
public function __construct() {
$handler = new AC\Ajax\Handler( false );
$handler->set_action( 'pre_get_comments' )
->set_callback( [ $this, 'send' ] )
->set_priority( PHP_INT_MAX - 100 );
$this->handler = $handler;
}
public function handle( Request $request ) {
$this->request = $request;
$this->handler->register();
}
public function send( WP_Comment_Query $query ) {
check_ajax_referer( 'ac-ajax' );
$this->handler->deregister();
$query->query_vars['fields'] = '*';
$query->query_vars['number'] = $this->get_rows_per_iteration();
$query->query_vars['offset'] = $this->get_offset();
$comments = $query->get_comments();
$comment_ids = wp_list_pluck( $comments, 'comment_ID' );
$comment_ids = array_map( 'intval', $comment_ids );
$response = new Response\QueryRows( $comment_ids, $this->get_rows_per_iteration() );
$response->success();
}
/**
* @return int
*/
private function get_rows_per_iteration() {
return ( new RowsPerIteration( $this->request ) )->apply_filters( 2000 );
}
/**
* @return int
*/
protected function get_offset() {
$page = (int) $this->request->filter( 'ac_page', 1, FILTER_SANITIZE_NUMBER_INT );
return ( $page - 1 ) * $this->get_rows_per_iteration();
}
}