|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file defines a class which implements a progress tracker for ajax calls, i.e. the ajax client |
|
4
|
|
|
* receives the updates and sends new calls to proceed forward. |
|
5
|
|
|
* |
|
6
|
|
|
* @since 1.0.0 |
|
7
|
|
|
* @package Wordlift_Framework\Tasks |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Wordlift\Tasks; |
|
11
|
|
|
|
|
12
|
|
|
use Wordlift_Log_Service; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Define the Task_Ajax_Progress class. |
|
16
|
|
|
* |
|
17
|
|
|
* @since 1.0.0 |
|
18
|
|
|
*/ |
|
19
|
|
|
class Task_Ajax_Progress implements Task_Progress { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The AJAX action, used to generate new nonces. |
|
23
|
|
|
* |
|
24
|
|
|
* @since 1.0.0 |
|
25
|
|
|
* @access private |
|
26
|
|
|
* @var string $action The AJAX action. |
|
27
|
|
|
*/ |
|
28
|
|
|
private $action; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The total number of items to process. |
|
32
|
|
|
* |
|
33
|
|
|
* @since 1.0.0 |
|
34
|
|
|
* @access private |
|
35
|
|
|
* @var int The total number of items to process. |
|
36
|
|
|
*/ |
|
37
|
|
|
private $count; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The current item index. |
|
41
|
|
|
* |
|
42
|
|
|
* @since 1.0.0 |
|
43
|
|
|
* @access private |
|
44
|
|
|
* @var int $index The current item index. |
|
45
|
|
|
*/ |
|
46
|
|
|
private $index; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var Wordlift_Log_Service |
|
50
|
|
|
*/ |
|
51
|
|
|
private $log; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Create a Task_Ajax_Progress instance with the specified |
|
55
|
|
|
* AJAX action. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $action The AJAX action. |
|
58
|
|
|
* |
|
59
|
|
|
* @since 1.0.0 |
|
60
|
|
|
* |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct( $action ) { |
|
63
|
|
|
|
|
64
|
|
|
$this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
65
|
|
|
|
|
66
|
|
|
$this->action = $action; |
|
67
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritDoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
function set_count( $value ) { |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
$this->log->debug( "New count $value for action $this->action..." ); |
|
76
|
|
|
|
|
77
|
|
|
$this->count = $value; |
|
78
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritDoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
function set_progress( $index, $item ) { |
|
|
|
|
|
|
85
|
|
|
$this->index = $index; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritDoc} |
|
90
|
|
|
*/ |
|
91
|
|
|
function finish() { |
|
|
|
|
|
|
92
|
|
|
wp_send_json_success( array( |
|
93
|
|
|
'count' => $this->count, |
|
94
|
|
|
'index' => $this->index, |
|
95
|
|
|
// $this->index is zero based. |
|
96
|
|
|
'complete' => $this->index >= $this->count - 1, |
|
97
|
|
|
'nonce' => wp_create_nonce( $this->action ), |
|
98
|
|
|
) ); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.