|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wordlift\Dataset\Background; |
|
4
|
|
|
|
|
5
|
|
|
use Wordlift\Dataset\Background\Stages\Sync_Background_Process_Posts_Stage; |
|
6
|
|
|
use Wordlift\Dataset\Background\Stages\Sync_Background_Process_Stage; |
|
7
|
|
|
use Wordlift\Dataset\Background\Stages\Sync_Background_Process_Terms_Stage; |
|
8
|
|
|
use Wordlift\Dataset\Background\Stages\Sync_Background_Process_Users_Stage; |
|
9
|
|
|
use Wordlift\Dataset\Sync_Object_Adapter_Factory; |
|
10
|
|
|
use Wordlift\Dataset\Sync_Service; |
|
11
|
|
|
|
|
12
|
|
|
class Sync_Background_Process_Started_State extends Abstract_Sync_Background_Process_State { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var Sync_Background_Process |
|
16
|
|
|
*/ |
|
17
|
|
|
private $context; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Sync_Service |
|
21
|
|
|
*/ |
|
22
|
|
|
private $sync_service; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Sync_Background_Process_Stage[] |
|
26
|
|
|
*/ |
|
27
|
|
|
private $stages; |
|
28
|
|
|
|
|
29
|
|
|
private $batch_size = 5; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Sync_Background_Process_Started_State constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Sync_Background_Process $context |
|
35
|
|
|
* @param Sync_Service $sync_service |
|
36
|
|
|
* @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
|
37
|
|
|
*/ |
|
38
|
|
|
function __construct( $context, $sync_service, $sync_object_adapter_factory ) { |
|
|
|
|
|
|
39
|
|
|
parent::__construct( Sync_Background_Process::STATE_STARTED ); |
|
40
|
|
|
|
|
41
|
|
|
$this->context = $context; |
|
42
|
|
|
$this->sync_service = $sync_service; |
|
43
|
|
|
|
|
44
|
|
|
$this->stages = array( |
|
|
|
|
|
|
45
|
|
|
new Sync_Background_Process_Posts_Stage( $sync_object_adapter_factory ), |
|
46
|
|
|
new Sync_Background_Process_Terms_Stage( $sync_object_adapter_factory ), |
|
47
|
|
|
new Sync_Background_Process_Users_Stage( $sync_object_adapter_factory ), |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
function enter() { |
|
|
|
|
|
|
52
|
|
|
// Delete the KG contents. |
|
53
|
|
|
$this->sync_service->delete_all(); |
|
54
|
|
|
|
|
55
|
|
|
// Clear caches. |
|
56
|
|
|
do_action( 'wl_ttl_cache_cleaner__flush' ); |
|
57
|
|
|
|
|
58
|
|
|
$counts = array_map( function ( $item ) { |
|
59
|
|
|
return $item->count(); |
|
60
|
|
|
}, $this->stages ); |
|
61
|
|
|
|
|
62
|
|
|
update_option( '_wl_sync_background_process_count', $counts, true ); |
|
63
|
|
|
update_option( '_wl_sync_background_process_stage', 0, true ); |
|
64
|
|
|
update_option( '_wl_sync_background_process_offset', 0, true ); |
|
65
|
|
|
update_option( '_wl_sync_background_process_started', time(), true ); |
|
66
|
|
|
update_option( '_wl_sync_background_process_updated', time(), true ); |
|
67
|
|
|
|
|
68
|
|
|
$this->context->set_state( Sync_Background_Process::STATE_STARTED ); |
|
69
|
|
|
|
|
70
|
|
|
$this->context->push_to_queue( true ); |
|
71
|
|
|
$this->context->save()->dispatch(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
function leave() { |
|
|
|
|
|
|
75
|
|
|
$this->context->set_state( null ); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
function task( $args ) { |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
$offset = get_option( '_wl_sync_background_process_offset' ); |
|
81
|
|
|
$stage = get_option( '_wl_sync_background_process_stage' ); |
|
82
|
|
|
$counts = get_option( '_wl_sync_background_process_count' ); |
|
83
|
|
|
$batch_size = min( $counts[ $stage ] - $offset, $this->batch_size ); |
|
84
|
|
|
|
|
85
|
|
|
add_filter( 'wl_api_service__request', array( $this, 'api_service__request' ) ); |
|
86
|
|
|
try { |
|
87
|
|
|
$object_adapters = $this->stages[ $stage ]->get_sync_object_adapters( $offset, $batch_size ); |
|
88
|
|
|
$this->sync_service->sync_many( $object_adapters, true ); |
|
89
|
|
|
} catch ( \Exception $e ) { |
|
90
|
|
|
// ignored. |
|
91
|
|
|
} |
|
92
|
|
|
remove_filter( 'wl_api_service__request', array( $this, 'api_service__request' ) ); |
|
93
|
|
|
|
|
94
|
|
|
update_option( '_wl_sync_background_process_updated', time(), true ); |
|
95
|
|
|
|
|
96
|
|
|
// Increase the offset. |
|
97
|
|
|
if ( ( $offset + $batch_size ) < $counts[ $stage ] ) { |
|
98
|
|
|
update_option( '_wl_sync_background_process_offset', $offset + $batch_size, true ); |
|
99
|
|
|
|
|
100
|
|
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// Increase the stage. |
|
104
|
|
|
if ( ( $stage + 1 ) < count( $this->stages ) ) { |
|
105
|
|
|
update_option( '_wl_sync_background_process_stage', $stage + 1, true ); |
|
106
|
|
|
update_option( '_wl_sync_background_process_offset', 0, true ); |
|
107
|
|
|
|
|
108
|
|
|
return true; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// Stop processing. |
|
112
|
|
|
$this->context->stop(); |
|
113
|
|
|
|
|
114
|
|
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Hook to provide a request to update the status on the server. |
|
119
|
|
|
* |
|
120
|
|
|
* @param array $args |
|
121
|
|
|
* |
|
122
|
|
|
* @return mixed |
|
123
|
|
|
*/ |
|
124
|
|
|
public function api_service__request( $args ) { |
|
125
|
|
|
|
|
126
|
|
|
$state_header_value = str_replace( "\n", '', wp_json_encode( $this->context->get_info() ) ); |
|
127
|
|
|
|
|
128
|
|
|
$args['headers']['X-Wordlift-Dataset-Sync-State-V1'] = $state_header_value; |
|
129
|
|
|
|
|
130
|
|
|
return $args; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
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.