1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Jetpack_Sync_Reindex { |
|
|
|
|
4
|
|
|
|
5
|
|
|
static function init() { |
6
|
|
|
add_action( 'wp_ajax_jetpack-sync-reindex-trigger', array( __CLASS__, 'sync_reindex_trigger' ) ); |
7
|
|
|
add_action( 'wp_ajax_jetpack-sync-reindex-status', array( __CLASS__, 'sync_reindex_status' ) ); |
8
|
|
|
} |
9
|
|
|
|
10
|
|
|
static function reindex_if_needed() { |
11
|
|
|
if ( self::reindex_needed() ) { |
|
|
|
|
12
|
|
|
self::reindex_trigger(); |
|
|
|
|
13
|
|
|
} |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
static function reindex_needed() { |
17
|
|
|
return ( self::_get_post_count_local() != self::_get_post_count_cloud() ); |
|
|
|
|
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
static function reindex_trigger() { |
21
|
|
|
$response = array( 'status' => 'ERROR' ); |
22
|
|
|
|
23
|
|
|
// Force a privacy check |
24
|
|
|
Jetpack::check_privacy( JETPACK__PLUGIN_FILE ); |
25
|
|
|
|
26
|
|
|
Jetpack::load_xml_rpc_client(); |
27
|
|
|
$client = new Jetpack_IXR_Client( array( |
28
|
|
|
'user_id' => JETPACK_MASTER_USER, |
29
|
|
|
) ); |
30
|
|
|
|
31
|
|
|
$client->query( 'jetpack.reindexTrigger' ); |
32
|
|
|
|
33
|
|
|
if ( ! $client->isError() ) { |
34
|
|
|
$response = $client->getResponse(); |
35
|
|
|
Jetpack_Options::update_option( 'sync_bulk_reindexing', true ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $response; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function reindex_status() { |
42
|
|
|
$response = array( 'status' => 'ERROR' ); |
43
|
|
|
|
44
|
|
|
// Assume reindexing is done if it was not triggered in the first place |
45
|
|
|
if ( false === Jetpack_Options::get_option( 'sync_bulk_reindexing' ) ) { |
46
|
|
|
return array( 'status' => 'DONE' ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
Jetpack::load_xml_rpc_client(); |
50
|
|
|
$client = new Jetpack_IXR_Client( array( |
51
|
|
|
'user_id' => JETPACK_MASTER_USER, |
52
|
|
|
) ); |
53
|
|
|
|
54
|
|
|
$client->query( 'jetpack.reindexStatus' ); |
55
|
|
|
|
56
|
|
|
if ( ! $client->isError() ) { |
57
|
|
|
$response = $client->getResponse(); |
58
|
|
|
if ( 'DONE' == $response['status'] ) { |
59
|
|
|
Jetpack_Options::delete_option( 'sync_bulk_reindexing' ); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $response; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
static function reindex_ui() { |
67
|
|
|
$strings = json_encode( array( |
68
|
|
|
'WAITING' => array( |
69
|
|
|
'action' => __( 'Refresh Status', 'jetpack' ), |
70
|
|
|
'status' => __( 'Indexing request queued and waiting…', 'jetpack' ), |
71
|
|
|
), |
72
|
|
|
'INDEXING' => array( |
73
|
|
|
'action' => __( 'Refresh Status', 'jetpack' ), |
74
|
|
|
'status' => __( 'Indexing posts', 'jetpack' ), |
75
|
|
|
), |
76
|
|
|
'DONE' => array( |
77
|
|
|
'action' => __( 'Reindex Posts', 'jetpack' ), |
78
|
|
|
'status' => __( 'Posts indexed.', 'jetpack' ), |
79
|
|
|
), |
80
|
|
|
'ERROR' => array( |
81
|
|
|
'action' => __( 'Refresh Status', 'jetpack' ), |
82
|
|
|
'status' => __( 'Status unknown.', 'jetpack' ), |
83
|
|
|
), |
84
|
|
|
'ERROR:LARGE' => array( |
85
|
|
|
'action' => __( 'Refresh Status', 'jetpack' ), |
86
|
|
|
'status' => __( 'This site is too large, please contact Jetpack support to sync.', 'jetpack' ), |
87
|
|
|
), |
88
|
|
|
) ); |
89
|
|
|
|
90
|
|
|
wp_enqueue_script( |
91
|
|
|
'jetpack_sync_reindex_control', |
92
|
|
|
plugins_url( '_inc/jquery.jetpack-sync.js', JETPACK__PLUGIN_FILE ), |
93
|
|
|
array( 'jquery' ), |
94
|
|
|
JETPACK__VERSION |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$template = <<<EOT |
98
|
|
|
<p class="jetpack_sync_reindex_control" id="jetpack_sync_reindex_control" data-strings="%s"> |
99
|
|
|
<input type="submit" class="jetpack_sync_reindex_control_action button" value="%s" disabled /> |
100
|
|
|
<span class="jetpack_sync_reindex_control_status">…</span> |
101
|
|
|
</p> |
102
|
|
|
EOT; |
103
|
|
|
|
104
|
|
|
return sprintf( |
105
|
|
|
$template, |
106
|
|
|
esc_attr( $strings ), |
107
|
|
|
esc_attr__( 'Refresh Status', 'jetpack' ) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function _get_post_count_local() { |
112
|
|
|
global $wpdb; |
113
|
|
|
|
114
|
|
|
return (int) $wpdb->get_var( |
115
|
|
|
"SELECT count(*) |
116
|
|
|
FROM {$wpdb->posts} |
117
|
|
|
WHERE post_status = 'publish' AND post_password = ''" |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function _get_post_count_cloud() { |
122
|
|
|
$blog_id = Jetpack::init()->get_option( 'id' ); |
123
|
|
|
|
124
|
|
|
$body = array( |
125
|
|
|
'size' => 1, |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$response = wp_remote_post( |
129
|
|
|
"https://public-api.wordpress.com/rest/v1/sites/$blog_id/search", |
130
|
|
|
array( |
131
|
|
|
'timeout' => 10, |
132
|
|
|
'user-agent' => 'jetpack_related_posts', |
133
|
|
|
'sslverify' => true, |
134
|
|
|
'body' => $body, |
135
|
|
|
) |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
if ( is_wp_error( $response ) ) { |
139
|
|
|
return 0; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$results = json_decode( wp_remote_retrieve_body( $response ), true ); |
143
|
|
|
|
144
|
|
|
return (int) $results['results']['total']; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
View Code Duplication |
static function sync_reindex_trigger() { |
148
|
|
|
if ( Jetpack::current_user_is_connection_owner() && current_user_can( 'manage_options' ) ) { |
149
|
|
|
echo json_encode( self::reindex_trigger() ); |
|
|
|
|
150
|
|
|
} else { |
151
|
|
|
echo '{"status":"ERROR"}'; |
152
|
|
|
} |
153
|
|
|
exit; |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
View Code Duplication |
static function sync_reindex_status(){ |
157
|
|
|
if ( Jetpack::current_user_is_connection_owner() && current_user_can( 'manage_options' ) ) { |
158
|
|
|
echo json_encode( self::reindex_status() ); |
|
|
|
|
159
|
|
|
} else { |
160
|
|
|
echo '{"status":"ERROR"}'; |
161
|
|
|
} |
162
|
|
|
exit; |
|
|
|
|
163
|
|
|
} |
164
|
|
|
} |
This check looks for classes that have been defined more than once.
If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.
This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.