Completed
Push — add/sync-action ( ff861d...d04c20 )
by
unknown
10:36
created

Jetpack_Sync_WP_Replicastore::full_sync_start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
require_once 'interface.jetpack-sync-replicastore.php';
4
5
/**
6
 * An implementation of iJetpack_Sync_Replicastore which returns data stored in a WordPress.org DB.
7
 * This is useful to compare values in the local WP DB to values in the synced replica store
8
 */
9
class Jetpack_Sync_WP_Replicastore implements iJetpack_Sync_Replicastore {
10
	public function set_wp_version( $version ) {
11
		// makes no sense here?
12
	}
13
	public function get_wp_version() {
14
		global $wp_version;
15
		return $wp_version;
16
	}
17
18
	public function reset() {
19
		global $wpdb;
20
21
		$wpdb->query( "DELETE FROM $wpdb->posts" );
22
		$wpdb->query( "DELETE FROM $wpdb->comments" );
23
24
		// also need to delete terms from cache
25
		$term_ids = $wpdb->get_col( "SELECT term_id FROM $wpdb->terms" );
26
		foreach( $term_ids as $term_id ) {
27
			wp_cache_delete( $term_id, 'terms' );
28
		}
29
		
30
		$wpdb->query( "DELETE FROM $wpdb->terms" );
31
32
		$wpdb->query( "DELETE FROM $wpdb->term_taxonomy" );
33
		$wpdb->query( "DELETE FROM $wpdb->term_relationships" );
34
35
		// callables and constants
36
		$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'jetpack_%'" );
37
		$wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key NOT LIKE '_%'" ); //TODO: delete by special prefix?
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
38
	}
39
40
	function full_sync_start() {
41
		$this->reset();
42
	}
43
	
44
	function full_sync_end() {
45
		// noop right now
46
	}
47
	
48
	public function post_count( $status = null ) {
49
		return count( $this->get_posts( $status ) );
50
	}
51
52
	public function get_posts( $status = null ) {
53
		$args = array( 'orderby' => 'ID' );
54
55
		if ( $status ) {
56
			$args['post_status'] = $status;
57
		} else {
58
			$args['post_status'] = 'any';
59
		}
60
61
		return get_posts( $args );
62
	}
63
64
	public function get_post( $id ) {
65
		return get_post( $id );
66
	}
67
68
	public function upsert_post( $post ) {
69
		global $blog_id, $wpdb;
70
71
		// reject the post if it's not a WP_Post
72
		if ( ! $post instanceof WP_Post ) {
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
73
			return;
74
		}
75
76
		$post = $post->to_array();
77
78
		// reject posts without an ID
79
		if ( ! isset( $post['ID'] ) ) {
80
			return;
81
		}
82
83
		$now     = current_time( 'mysql' );
84
		$now_gmt = get_gmt_from_date( $now );
85
86
		$defaults = array(
87
			'ID'                    => 0,
88
			'post_author'           => '0',
89
			'post_content'          => '',
90
			'post_content_filtered' => '',
91
			'post_title'            => '',
92
			'post_name'             => '',
93
			'post_excerpt'          => '',
94
			'post_status'           => 'draft',
95
			'post_type'             => 'post',
96
			'comment_status'        => '',
97
			'comment_status'        => 'closed',
98
			'comment_count'         => '0',
99
			'ping_status'           => '',
100
			'post_password'         => '',
101
			'to_ping'               => '',
102
			'pinged'                => '',
103
			'post_parent'           => 0,
104
			'menu_order'            => 0,
105
			'guid'                  => '',
106
			'post_date'             => $now,
107
			'post_date_gmt'         => $now_gmt,
108
			'post_modified'         => $now,
109
			'post_modified_gmt'     => $now_gmt,
110
		);
111
112
		$post = array_intersect_key( $post, $defaults );
113
114
		$post = sanitize_post( $post, 'db' );
115
116
		unset( $post['filter'] );
117
118
		$exists = $wpdb->get_var( $wpdb->prepare( "SELECT EXISTS( SELECT 1 FROM $wpdb->posts WHERE ID = %d )", $post['ID'] ) );
119
120
		if ( $exists ) {
121
			$affected_rows = $wpdb->update( $wpdb->posts, $post, array( 'ID' => $post['ID'] ) );
0 ignored issues
show
Unused Code introduced by
$affected_rows is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
122
		} else {
123
			$affected_rows = $wpdb->insert( $wpdb->posts, $post );
0 ignored issues
show
Unused Code introduced by
$affected_rows is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
124
		}
125
126
		clean_post_cache( $post['ID'] );
127
	}
128
129
	public function delete_post( $post_id ) {
130
		wp_delete_post( $post_id, true );
131
	}
132
133
	public function posts_checksum() {
134
		global $wpdb;
135
136
		$query = <<<ENDSQL
137
			SELECT CONV(BIT_XOR(CRC32(CONCAT(ID,post_modified))), 10, 16) 
138
				FROM $wpdb->posts
139
				WHERE post_type <> 'revision'
140
ENDSQL;
141
142
		return $wpdb->get_var($query);
143
	}
144
145
	public function comment_count( $status = null ) {
146
		return count( $this->get_comments() );
147
	}
148
149
	public function get_comments( $status = null ) {
150
		$args = array( 'orderby' => 'ID', 'status' => 'all' );
151
152
		if ( $status ) {
153
			$args['status'] = $status;
154
		}
155
156
		return get_comments( $args );
157
	}
158
159
	public function get_comment( $id ) {
160
		return get_comment( $id );
161
	}
162
163
	public function upsert_comment( $comment ) {
164
		global $wpdb, $wp_version;
165
166
		if ( version_compare( $wp_version, '4.4', '<' ) ) {
167
			$comment = (array) $comment;
168
		} else {
169
			// WP 4.4 introduced the WP_Comment Class
170
			$comment = $comment->to_array();
171
		}
172
		
173
		// filter by fields on comment table
174
		$comment_fields_whitelist = array(
175
			'comment_ID',
176
			'comment_post_ID',
177
			'comment_author',
178
			'comment_author_email',
179
			'comment_author_url',
180
			'comment_author_IP',
181
			'comment_date',
182
			'comment_date_gmt',
183
			'comment_content',
184
			'comment_karma',
185
			'comment_approved',
186
			'comment_agent',
187
			'comment_type',
188
			'comment_parent',
189
			'user_id'
190
		);
191
192
		foreach ( $comment as $key => $value ) {
193
			if ( ! in_array( $key, $comment_fields_whitelist ) ) {
194
				unset( $comment[ $key ] );
195
			}
196
		}
197
198
		$exists = $wpdb->get_var(
199
			$wpdb->prepare(
200
				"SELECT EXISTS( SELECT 1 FROM $wpdb->comments WHERE comment_ID = %d )",
201
				$comment['comment_ID']
202
			)
203
		);
204
205
		if ( $exists ) {
206
			$wpdb->update( $wpdb->comments, $comment, array( 'comment_ID' => $comment['comment_ID'] ) );
207
		} else {
208
			$wpdb->insert( $wpdb->comments, $comment );
209
		}
210
211
		wp_update_comment_count( $comment['comment_post_ID'] );
212
	}
213
214
	public function trash_comment( $comment_id ) {
215
		wp_delete_comment( $comment_id );
216
	}
217
218
	public function delete_comment( $comment_id ) {
219
		wp_delete_comment( $comment_id, true );
220
	}
221
222
	public function spam_comment( $comment_id ) {
223
		wp_spam_comment( $comment_id );
224
	}
225
226
	public function comments_checksum() {
227
		global $wpdb;
228
229
		$query = <<<ENDSQL
230
			SELECT CONV(BIT_XOR(CRC32(CONCAT(comment_ID,comment_content))), 10, 16) FROM $wpdb->comments
231
ENDSQL;
232
233
		return $wpdb->get_var($query);
234
	}
235
236
	public function update_option( $option, $value ) {
237
		return update_option( $option, $value );
238
	}
239
240
	public function get_option( $option ) {
241
		return get_option( $option );
242
	}
243
244
	public function delete_option( $option ) {
245
		return delete_option( $option );
246
	}
247
248
	public function set_theme_support( $theme_support ) {
249
		// noop
250
	}
251
252
	public function current_theme_supports( $feature ) {
253
		return current_theme_supports( $feature );
254
	}
255
256
257
	// meta
258
	public function get_metadata( $type, $object_id, $meta_key = '', $single = false ) {
259
		return get_metadata( $type, $object_id, $meta_key, $single );
260
	}
261
	public function upsert_metadata( $type, $object_id, $meta_key, $meta_value, $meta_id ) {
262
		// add_metadata( $type, $object_id, $meta_key, $value, $unique );
263
	}
264
	public function delete_metadata( $type, $object_id, $meta_ids ) {
265
		// TODO: SQL delete
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
266
		// delete_metadata( $meta_type, $object_id, $meta_key, $meta_value, $delete_all );
267
	}
268
269
	// constants
270
	public function get_constant( $constant ) {
271
		// TODO: Implement get_constant() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
272
	}
273
	public function set_constants( $constants ) {
274
		// TODO: Implement get_constant() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
275
	}
276
277
	public function get_updates( $type ) {
278
		// TODO: Implement get_updates() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
279
	}
280
281
	public function set_updates( $type, $updates ) {
282
		// TODO: Implement set_updates() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
283
	}
284
285
	// functions
286
	public function get_callable( $constant ) {
287
		// TODO: Implement get_constant() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
288
	}
289
	public function set_callables( $constants ) {
290
		// TODO: Implement get_constant() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
291
	}
292
293
	// network options
294
	public function get_site_option( $option ) {
295
		// TODO: Implement get_site_option
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
296
	}
297
298
	public function update_site_option( $option, $value ) {
299
		// TODO: Implement update_site_option
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
300
	}
301
302
	public function delete_site_option( $option ) {
303
		// TODO: Implement delete_site_option
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
304
	}
305
306
	// terms
307
	public function get_terms( $taxonomy ) {
308
		// TODO: Implement get_terms() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
309
	}
310
	public function get_term( $taxonomy, $term_id, $is_term_id = true ) {
311
		// TODO: Implement get_term() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
312
	}
313
	public function get_the_terms( $object_id, $taxonomy ) {
314
		// TODO: Implement get_the_terms() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
315
	}
316
	public function update_object_terms( $object_id, $taxonomy, $terms, $append ) {
317
		// TODO: Implement update_object_terms method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
318
	}
319
	public function update_term( $term_object ) {
320
		// TODO: Implement update_term() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
321
	}
322
	public function delete_term( $term_id, $taxonomy ) {
323
		// TODO: Implement delete_term() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
324
	}
325
	public function delete_object_terms( $object_id, $tt_ids ) {
326
		// TODO: Implement delete_object_terms() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
327
	}
328
329
	// users
330
	public function user_count() {
331
332
	}
333
	public function get_user( $user_id ) {
334
		// TODO: Implement get_user() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
335
	}
336
	public function upsert_user( $user ) {
337
		// TODO: Implement update_user() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
338
	}
339
	public function delete_user( $user_id ) {
340
		// TODO: Implement delete_user() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
341
	}
342
343
	public function checksum_all() {
344
		return array(
345
			'posts' => $this->posts_checksum(),
346
			'comments' => $this->comments_checksum()
347
		);
348
	}
349
}
350