1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Term relationships sync module. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-sync |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Sync\Modules; |
9
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Sync\Defaults; |
11
|
|
|
use Automattic\Jetpack\Sync\Listener; |
12
|
|
|
use Automattic\Jetpack\Sync\Settings; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class to handle sync for term relationships. |
16
|
|
|
*/ |
17
|
|
|
class Term_Relationships extends Module { |
18
|
|
|
/** |
19
|
|
|
* Sync module name. |
20
|
|
|
* |
21
|
|
|
* @access public |
22
|
|
|
* |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
|
|
public function name() { |
26
|
|
|
return 'term_relationships'; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The id field in the database. |
31
|
|
|
* |
32
|
|
|
* @access public |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public function id_field() { |
37
|
|
|
return 'object_id'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The table in the database. |
42
|
|
|
* |
43
|
|
|
* @access public |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function table_name() { |
48
|
|
|
return 'term_relationships'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Initialize term relationships action listeners for full sync. |
53
|
|
|
* |
54
|
|
|
* @access public |
55
|
|
|
* |
56
|
|
|
* @param callable $callable Action handler callable. |
57
|
|
|
*/ |
58
|
|
|
public function init_full_sync_listeners( $callable ) { |
59
|
|
|
add_action( 'jetpack_full_sync_term_relationships', $callable, 10, 2 ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Initialize the module in the sender. |
64
|
|
|
* |
65
|
|
|
* @access public |
66
|
|
|
*/ |
67
|
|
|
public function init_before_send() { |
68
|
|
|
// Full sync. |
69
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_term_relationships', array( $this, 'expand_term_relationships' ) ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Enqueue the term relationships actions for full sync. |
74
|
|
|
* |
75
|
|
|
* @access public |
76
|
|
|
* |
77
|
|
|
* @todo This method has similarities with Automattic\Jetpack\Sync\Modules\Module::enqueue_all_ids_as_action. Refactor to keep DRY. |
78
|
|
|
* @see Automattic\Jetpack\Sync\Modules\Module::enqueue_all_ids_as_action |
79
|
|
|
* |
80
|
|
|
* @param array $config Full sync configuration for this sync module. |
81
|
|
|
* @param int $max_items_to_enqueue Maximum number of items to enqueue. |
82
|
|
|
* @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
83
|
|
|
* @return array Number of actions enqueued, and next module state. |
84
|
|
|
*/ |
85
|
|
|
public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
86
|
|
|
global $wpdb; |
87
|
|
|
$items_per_page = 1000; |
88
|
|
|
$chunk_count = 0; |
89
|
|
|
$previous_interval_end = $state ? $state : array( |
90
|
|
|
'object_id' => '~0', |
91
|
|
|
'term_taxonomy_id' => '~0', |
92
|
|
|
); |
93
|
|
|
$listener = Listener::get_instance(); |
94
|
|
|
$action_name = 'jetpack_full_sync_term_relationships'; |
95
|
|
|
|
96
|
|
|
// Count down from max_id to min_id so we get term relationships for the newest posts and terms first. |
97
|
|
|
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
98
|
|
|
while ( $ids = $wpdb->get_results( "SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id <= {$previous_interval_end['object_id']} AND term_taxonomy_id < {$previous_interval_end['term_taxonomy_id']} ORDER BY object_id DESC, term_taxonomy_id DESC LIMIT {$items_per_page}", ARRAY_A ) ) { |
99
|
|
|
// Request term relationships in groups of N for efficiency. |
100
|
|
|
$chunked_ids = array_chunk( $ids, self::ARRAY_CHUNK_SIZE ); |
101
|
|
|
|
102
|
|
|
// If we hit our row limit, process and return. |
103
|
|
View Code Duplication |
if ( $chunk_count + count( $chunked_ids ) >= $max_items_to_enqueue ) { |
104
|
|
|
$remaining_items_count = $max_items_to_enqueue - $chunk_count; |
105
|
|
|
$remaining_items = array_slice( $chunked_ids, 0, $remaining_items_count ); |
106
|
|
|
$remaining_items_with_previous_interval_end = $this->get_chunks_with_preceding_end( $remaining_items, $previous_interval_end ); |
107
|
|
|
$listener->bulk_enqueue_full_sync_actions( $action_name, $remaining_items_with_previous_interval_end ); |
108
|
|
|
|
109
|
|
|
$last_chunk = end( $remaining_items ); |
110
|
|
|
return array( $remaining_items_count + $chunk_count, end( $last_chunk ) ); |
111
|
|
|
} |
112
|
|
|
$chunked_ids_with_previous_end = $this->get_chunks_with_preceding_end( $chunked_ids, $previous_interval_end ); |
113
|
|
|
|
114
|
|
|
$listener->bulk_enqueue_full_sync_actions( $action_name, $chunked_ids_with_previous_end ); |
115
|
|
|
|
116
|
|
|
$chunk_count += count( $chunked_ids ); |
117
|
|
|
|
118
|
|
|
// The $ids are ordered in descending order. |
119
|
|
|
$previous_interval_end = end( $ids ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return array( $chunk_count, true ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Retrieve an estimated number of actions that will be enqueued. |
127
|
|
|
* |
128
|
|
|
* @access public |
129
|
|
|
* |
130
|
|
|
* @param array $config Full sync configuration for this sync module. |
131
|
|
|
* @return int Number of items yet to be enqueued. |
132
|
|
|
*/ |
133
|
|
|
public function estimate_full_sync_actions( $config ) { |
134
|
|
|
global $wpdb; |
135
|
|
|
|
136
|
|
|
$query = "SELECT COUNT(*) FROM $wpdb->term_relationships"; |
137
|
|
|
|
138
|
|
|
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared |
139
|
|
|
$count = $wpdb->get_var( $query ); |
140
|
|
|
|
141
|
|
|
return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Retrieve the actions that will be sent for this module during a full sync. |
146
|
|
|
* |
147
|
|
|
* @access public |
148
|
|
|
* |
149
|
|
|
* @return array Full sync actions of this module. |
150
|
|
|
*/ |
151
|
|
|
public function get_full_sync_actions() { |
152
|
|
|
return array( 'jetpack_full_sync_term_relationships' ); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Expand the term relationships within a hook before they are serialized and sent to the server. |
157
|
|
|
* |
158
|
|
|
* @access public |
159
|
|
|
* |
160
|
|
|
* @param array $args The hook parameters. |
161
|
|
|
* @return array $args The expanded hook parameters. |
162
|
|
|
*/ |
163
|
|
|
public function expand_term_relationships( $args ) { |
164
|
|
|
list( $term_relationships, $previous_end ) = $args; |
165
|
|
|
|
166
|
|
|
return array( |
167
|
|
|
'term_relationships' => $term_relationships, |
168
|
|
|
'previous_end' => $previous_end, |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|