Completed
Branch ci (4cd40d)
by litefeel
03:54 queued 01:39
created
lib/database.php 1 patch
Spacing   +120 added lines, -121 removed lines patch added patch discarded remove patch
@@ -21,21 +21,21 @@  discard block
 block discarded – undo
21 21
 	 *
22 22
 	 * @var array
23 23
 	 */
24
-	protected $whitelisted_post_types = array( 'post', 'page' );
24
+	protected $whitelisted_post_types = array('post', 'page');
25 25
 
26 26
 	/**
27 27
 	 * Currently whitelisted post statuses.
28 28
 	 *
29 29
 	 * @var array
30 30
 	 */
31
-	protected $whitelisted_post_statuses = array( 'publish' );
31
+	protected $whitelisted_post_statuses = array('publish');
32 32
 
33 33
 	/**
34 34
 	 * Instantiates a new Database object.
35 35
 	 *
36 36
 	 * @param Writing_On_GitHub $app Application container.
37 37
 	 */
38
-	public function __construct( Writing_On_GitHub $app ) {
38
+	public function __construct(Writing_On_GitHub $app) {
39 39
 		$this->app = $app;
40 40
 	}
41 41
 
@@ -45,31 +45,31 @@  discard block
 block discarded – undo
45 45
 	 * @return Writing_On_GitHub_Post[]|WP_Error
46 46
 	 */
47 47
 	public function fetch_all_supported(bool $force = false, bool aaa cc) {
48
-		$args  = array(
48
+		$args = array(
49 49
 			'post_type'   => $this->get_whitelisted_post_types(),
50 50
 			'post_status' => $this->get_whitelisted_post_statuses(),
51 51
 			'nopaging'    => true,
52 52
 			'fields'      => 'ids',
53 53
 		);
54 54
 
55
-		$query = new WP_Query( apply_filters( 'wogh_pre_fetch_all_supported', $args ) );
55
+		$query = new WP_Query(apply_filters('wogh_pre_fetch_all_supported', $args));
56 56
 
57 57
 		$post_ids = $query->get_posts();
58 58
 
59
-		if ( ! $post_ids ) {
59
+		if ( ! $post_ids) {
60 60
 			return new WP_Error(
61 61
 				'no_results',
62
-				__( 'Querying for supported posts returned no results.', 'writing-on-github' )
62
+				__('Querying for supported posts returned no results.', 'writing-on-github')
63 63
 			);
64 64
 		}
65 65
 
66 66
 		$results = array();
67
-		foreach ( $post_ids as $post_id ) {
67
+		foreach ($post_ids as $post_id) {
68 68
 			// Do not export posts that have already been exported
69
-			if ( ! get_post_meta( $post_id, '_wogh_sha', true ) ||
70
-				 ! get_post_meta( $post_id, '_wogh_github_path', true) ) {
69
+			if ( ! get_post_meta($post_id, '_wogh_sha', true) ||
70
+				 ! get_post_meta($post_id, '_wogh_github_path', true)) {
71 71
 
72
-				$results[] = new Writing_On_GitHub_Post( $post_id, $this->app->api() );
72
+				$results[] = new Writing_On_GitHub_Post($post_id, $this->app->api());
73 73
 			}
74 74
 		}
75 75
 
@@ -83,10 +83,10 @@  discard block
 block discarded – undo
83 83
 	 *
84 84
 	 * @return WP_Error|Writing_On_GitHub_Post
85 85
 	 */
86
-	public function fetch_by_id( $post_id ) {
87
-		$post = new Writing_On_GitHub_Post( $post_id, $this->app->api() );
86
+	public function fetch_by_id($post_id) {
87
+		$post = new Writing_On_GitHub_Post($post_id, $this->app->api());
88 88
 
89
-		if ( ! $this->is_post_supported( $post ) ) {
89
+		if ( ! $this->is_post_supported($post)) {
90 90
 			return new WP_Error(
91 91
 				'unsupported_post',
92 92
 				sprintf(
@@ -111,7 +111,7 @@  discard block
 block discarded – undo
111 111
 	 *
112 112
 	 * @return string|WP_Error
113 113
 	 */
114
-	public function save_posts( array $posts ) {
114
+	public function save_posts(array $posts) {
115 115
 
116 116
 		/**
117 117
 		 * Whether an error has occurred.
@@ -120,20 +120,19 @@  discard block
 block discarded – undo
120 120
 		 */
121 121
 		$error = false;
122 122
 
123
-		foreach ( $posts as $post ) {
124
-			$args = apply_filters( 'wogh_pre_import_args', $this->post_args( $post ), $post );
123
+		foreach ($posts as $post) {
124
+			$args = apply_filters('wogh_pre_import_args', $this->post_args($post), $post);
125 125
 
126 126
 			remove_filter('content_save_pre', 'wp_filter_post_kses');
127 127
 			$post_id = $post->is_new() ?
128
-				wp_insert_post( $args, true ) :
129
-				wp_update_post( $args, true );
128
+				wp_insert_post($args, true) : wp_update_post($args, true);
130 129
 			add_filter('content_save_pre', 'wp_filter_post_kses');
131 130
 
132
-			if ( is_wp_error( $post_id ) ) {
133
-				if ( ! $error ) {
131
+			if (is_wp_error($post_id)) {
132
+				if ( ! $error) {
134 133
 					$error = $post_id;
135 134
 				} else {
136
-					$error->add( $post_id->get_error_code(), $post_id->get_error_message() );
135
+					$error->add($post_id->get_error_code(), $post_id->get_error_message());
137 136
 				}
138 137
 
139 138
 				// Abort saving if updating the post fails.
@@ -142,53 +141,53 @@  discard block
 block discarded – undo
142 141
 
143 142
 			// $this->set_revision_author( $post_id, $user_id );
144 143
 
145
-			if ( $post->is_new() ) {
144
+			if ($post->is_new()) {
146 145
 				$author = false;
147 146
 				$meta = $post->get_meta();
148
-				if ( ! empty( $meta ) && ! empty( $meta['author'] ) ) {
147
+				if ( ! empty($meta) && ! empty($meta['author'])) {
149 148
 					$author = $meta['author'];
150 149
 				}
151
-				$user    = $this->fetch_commit_user( $author );
152
-				$user_id = ! is_wp_error( $user ) ? $user->ID : 0;
153
-				$this->set_post_author( $post_id, $user_id );
150
+				$user    = $this->fetch_commit_user($author);
151
+				$user_id = ! is_wp_error($user) ? $user->ID : 0;
152
+				$this->set_post_author($post_id, $user_id);
154 153
 			}
155 154
 
156
-			$post->set_post( get_post( $post_id ) );
155
+			$post->set_post(get_post($post_id));
157 156
 
158
-			$meta = apply_filters( 'wogh_pre_import_meta', $post->get_meta(), $post );
157
+			$meta = apply_filters('wogh_pre_import_meta', $post->get_meta(), $post);
159 158
 
160
-			unset( $meta['tags'] );
161
-			unset( $meta['categories'] );
162
-			unset( $meta['author'] );
163
-			unset( $meta['post_date'] );
164
-			unset( $meta['post_excerpt'] );
165
-			unset( $meta['permalink'] );
159
+			unset($meta['tags']);
160
+			unset($meta['categories']);
161
+			unset($meta['author']);
162
+			unset($meta['post_date']);
163
+			unset($meta['post_excerpt']);
164
+			unset($meta['permalink']);
166 165
 
167
-			foreach ( $meta as $key => $value ) {
168
-				update_post_meta( $post_id, $key, $value );
166
+			foreach ($meta as $key => $value) {
167
+				update_post_meta($post_id, $key, $value);
169 168
 			}
170 169
 		}
171 170
 
172
-		if ( $error ) {
171
+		if ($error) {
173 172
 			return $error;
174 173
 		}
175 174
 
176
-		return __( 'Successfully saved posts.', 'writing-on-github' );
175
+		return __('Successfully saved posts.', 'writing-on-github');
177 176
 	}
178 177
 
179
-	protected function post_args( $post ) {
178
+	protected function post_args($post) {
180 179
 		$args = $post->get_args();
181 180
 		$meta = $post->get_meta();
182 181
 
183 182
 		// update tags
184
-		if ( isset( $meta['tags'] ) && $meta['tags'] ) {
183
+		if (isset($meta['tags']) && $meta['tags']) {
185 184
 			$args['tags_input'] = $meta['tags'];
186 185
 		}
187 186
 
188 187
 		// update categories
189
-		if ( isset( $meta['categories'] ) && $meta['categories'] ) {
188
+		if (isset($meta['categories']) && $meta['categories']) {
190 189
 			$categories = $meta['categories'];
191
-			if (!is_array($categories)) {
190
+			if ( ! is_array($categories)) {
192 191
 				$categories = array($categories);
193 192
 			}
194 193
 			$terms = get_terms(array(
@@ -204,7 +203,7 @@  discard block
 block discarded – undo
204 203
 			}
205 204
 
206 205
 			$ids = array();
207
-			if (!empty($terms)) {
206
+			if ( ! empty($terms)) {
208 207
 				foreach ($terms as $id => $name) {
209 208
 					$ids[] = $id;
210 209
 					unset($map[$name]);
@@ -212,7 +211,7 @@  discard block
 block discarded – undo
212 211
 			}
213 212
 
214 213
 			// create new terms
215
-			if (!empty($map)) {
214
+			if ( ! empty($map)) {
216 215
 				foreach ($map as $name => $value) {
217 216
 					$term = wp_insert_term($name, 'category', array('parent' => 0));
218 217
 					// array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
@@ -233,77 +232,77 @@  discard block
 block discarded – undo
233 232
 	 *
234 233
 	 * @return string|WP_Error
235 234
 	 */
236
-	public function delete_post_by_path( $path ) {
237
-		$query = new WP_Query( array(
235
+	public function delete_post_by_path($path) {
236
+		$query = new WP_Query(array(
238 237
 			'meta_key'       => '_wogh_github_path',
239 238
 			'meta_value'     => $path,
240 239
 			'meta_compare'   => '=',
241 240
 			'posts_per_page' => 1,
242 241
 			'fields'         => 'ids',
243
-		) );
242
+		));
244 243
 
245 244
 		$post_id = $query->get_posts();
246
-		$post_id = array_pop( $post_id );
245
+		$post_id = array_pop($post_id);
247 246
 
248
-		if ( ! $post_id ) {
249
-			$parts     = explode( '/', $path );
250
-			$filename  = array_pop( $parts );
251
-			$directory = $parts ? array_shift( $parts ) : '';
247
+		if ( ! $post_id) {
248
+			$parts     = explode('/', $path);
249
+			$filename  = array_pop($parts);
250
+			$directory = $parts ? array_shift($parts) : '';
252 251
 
253
-			if ( false !== strpos( $directory, 'post' ) ) {
254
-				preg_match( '/([0-9]{4})-([0-9]{2})-([0-9]{2})-(.*)\.md/', $filename, $matches );
252
+			if (false !== strpos($directory, 'post')) {
253
+				preg_match('/([0-9]{4})-([0-9]{2})-([0-9]{2})-(.*)\.md/', $filename, $matches);
255 254
 				$title = $matches[4];
256 255
 
257
-				$query = new WP_Query( array(
256
+				$query = new WP_Query(array(
258 257
 					'name'     => $title,
259 258
 					'posts_per_page' => 1,
260 259
 					'post_type' => $this->get_whitelisted_post_types(),
261 260
 					'fields'         => 'ids',
262
-				) );
261
+				));
263 262
 
264 263
 				$post_id = $query->get_posts();
265
-				$post_id = array_pop( $post_id );
264
+				$post_id = array_pop($post_id);
266 265
 			}
267 266
 
268
-			if ( ! $post_id ) {
269
-				preg_match( '/(.*)\.md/', $filename, $matches );
267
+			if ( ! $post_id) {
268
+				preg_match('/(.*)\.md/', $filename, $matches);
270 269
 				$title = $matches[1];
271 270
 
272
-				$query = new WP_Query( array(
271
+				$query = new WP_Query(array(
273 272
 					'name'     => $title,
274 273
 					'posts_per_page' => 1,
275 274
 					'post_type' => $this->get_whitelisted_post_types(),
276 275
 					'fields'         => 'ids',
277
-				) );
276
+				));
278 277
 
279 278
 				$post_id = $query->get_posts();
280
-				$post_id = array_pop( $post_id );
279
+				$post_id = array_pop($post_id);
281 280
 			}
282 281
 		}
283 282
 
284
-		if ( ! $post_id ) {
283
+		if ( ! $post_id) {
285 284
 			return new WP_Error(
286 285
 				'path_not_found',
287 286
 				sprintf(
288
-					__( 'Post not found for path %s.', 'writing-on-github' ),
287
+					__('Post not found for path %s.', 'writing-on-github'),
289 288
 					$path
290 289
 				)
291 290
 			);
292 291
 		}
293 292
 
294
-		$result = wp_delete_post( $post_id );
293
+		$result = wp_delete_post($post_id);
295 294
 
296 295
 		// If deleting fails...
297
-		if ( false === $result ) {
298
-			$post = get_post( $post_id );
296
+		if (false === $result) {
297
+			$post = get_post($post_id);
299 298
 
300 299
 			// ...and the post both exists and isn't in the trash...
301
-			if ( $post && 'trash' !== $post->post_status ) {
300
+			if ($post && 'trash' !== $post->post_status) {
302 301
 				// ... then something went wrong.
303 302
 				return new WP_Error(
304 303
 					'db_error',
305 304
 					sprintf(
306
-						__( 'Failed to delete post ID %d.', 'writing-on-github' ),
305
+						__('Failed to delete post ID %d.', 'writing-on-github'),
307 306
 						$post_id
308 307
 					)
309 308
 				);
@@ -311,25 +310,25 @@  discard block
 block discarded – undo
311 310
 		}
312 311
 
313 312
 		return sprintf(
314
-			__( 'Successfully deleted post ID %d.', 'writing-on-github' ),
313
+			__('Successfully deleted post ID %d.', 'writing-on-github'),
315 314
 			$post_id
316 315
 		);
317 316
 	}
318 317
 
319
-	public function delete_post( $post_id ) {
320
-		$result = wp_delete_post( $post_id );
318
+	public function delete_post($post_id) {
319
+		$result = wp_delete_post($post_id);
321 320
 
322 321
 		// If deleting fails...
323
-		if ( false === $result ) {
324
-			$post = get_post( $post_id );
322
+		if (false === $result) {
323
+			$post = get_post($post_id);
325 324
 
326 325
 			// ...and the post both exists and isn't in the trash...
327
-			if ( $post && 'trash' !== $post->post_status ) {
326
+			if ($post && 'trash' !== $post->post_status) {
328 327
 				// ... then something went wrong.
329 328
 				return new WP_Error(
330 329
 					'db_error',
331 330
 					sprintf(
332
-						__( 'Failed to delete post ID %d.', 'writing-on-github' ),
331
+						__('Failed to delete post ID %d.', 'writing-on-github'),
333 332
 						$post_id
334 333
 					)
335 334
 				);
@@ -337,7 +336,7 @@  discard block
 block discarded – undo
337 336
 		}
338 337
 
339 338
 		return sprintf(
340
-			__( 'Successfully deleted post ID %d.', 'writing-on-github' ),
339
+			__('Successfully deleted post ID %d.', 'writing-on-github'),
341 340
 			$post_id
342 341
 		);
343 342
 	}
@@ -348,7 +347,7 @@  discard block
 block discarded – undo
348 347
 	 * @return array
349 348
 	 */
350 349
 	protected function get_whitelisted_post_types() {
351
-		return apply_filters( 'wogh_whitelisted_post_types', $this->whitelisted_post_types );
350
+		return apply_filters('wogh_whitelisted_post_types', $this->whitelisted_post_types);
352 351
 	}
353 352
 
354 353
 	/**
@@ -357,7 +356,7 @@  discard block
 block discarded – undo
357 356
 	 * @return array
358 357
 	 */
359 358
 	protected function get_whitelisted_post_statuses() {
360
-		return apply_filters( 'wogh_whitelisted_post_statuses', $this->whitelisted_post_statuses );
359
+		return apply_filters('wogh_whitelisted_post_statuses', $this->whitelisted_post_statuses);
361 360
 	}
362 361
 
363 362
 	/**
@@ -367,12 +366,12 @@  discard block
 block discarded – undo
367 366
 	 *
368 367
 	 * @return string Whitelist formatted for query
369 368
 	 */
370
-	protected function format_for_query( $whitelist ) {
371
-		foreach ( $whitelist as $key => $value ) {
372
-			$whitelist[ $key ] = "'$value'";
369
+	protected function format_for_query($whitelist) {
370
+		foreach ($whitelist as $key => $value) {
371
+			$whitelist[$key] = "'$value'";
373 372
 		}
374 373
 
375
-		return implode( ', ', $whitelist );
374
+		return implode(', ', $whitelist);
376 375
 	}
377 376
 
378 377
 	/**
@@ -383,25 +382,25 @@  discard block
 block discarded – undo
383 382
 	 *
384 383
 	 * @return boolean                          True if supported, false if not.
385 384
 	 */
386
-	protected function is_post_supported( Writing_On_GitHub_Post $post ) {
387
-		if ( wp_is_post_revision( $post->id ) ) {
385
+	protected function is_post_supported(Writing_On_GitHub_Post $post) {
386
+		if (wp_is_post_revision($post->id)) {
388 387
 			return false;
389 388
 		}
390 389
 
391 390
 		// We need to allow trashed posts to be queried, but they are not whitelisted for export.
392
-		if ( ! in_array( $post->status(), $this->get_whitelisted_post_statuses() ) && 'trash' !== $post->status() ) {
391
+		if ( ! in_array($post->status(), $this->get_whitelisted_post_statuses()) && 'trash' !== $post->status()) {
393 392
 			return false;
394 393
 		}
395 394
 
396
-		if ( ! in_array( $post->type(), $this->get_whitelisted_post_types() ) ) {
395
+		if ( ! in_array($post->type(), $this->get_whitelisted_post_types())) {
397 396
 			return false;
398 397
 		}
399 398
 
400
-		if ( $post->has_password() ) {
399
+		if ($post->has_password()) {
401 400
 			return false;
402 401
 		}
403 402
 
404
-		return apply_filters( 'wogh_is_post_supported', true, $post );
403
+		return apply_filters('wogh_is_post_supported', true, $post);
405 404
 	}
406 405
 
407 406
 	/**
@@ -414,35 +413,35 @@  discard block
 block discarded – undo
414 413
 	 *
415 414
 	 * @return WP_Error|WP_User
416 415
 	 */
417
-	protected function fetch_commit_user( $display_name ) {
416
+	protected function fetch_commit_user($display_name) {
418 417
 		// If we can't find a user and a default hasn't been set,
419 418
 		// we're just going to set the revision author to 0.
420 419
 		$user = false;
421 420
 
422
-		if ( ! empty( $display_name ) ) {
423
-			$search_string = esc_attr( $display_name );
424
-			$query = new WP_User_Query( array(
421
+		if ( ! empty($display_name)) {
422
+			$search_string = esc_attr($display_name);
423
+			$query = new WP_User_Query(array(
425 424
 				'search'         => "{$search_string}",
426 425
 				'search_columns' => array(
427 426
 					'display_name',
428 427
 					'user_nicename',
429 428
 					'user_login',
430 429
 				)
431
-			) );
430
+			));
432 431
 			$users = $query->get_results();
433 432
 			$user = empty($users) ? false : $users[0];
434 433
 		}
435 434
 
436
-		if ( ! $user ) {
435
+		if ( ! $user) {
437 436
 			// Use the default user.
438
-			$user = get_user_by( 'id', (int) get_option( 'wogh_default_user' ) );
437
+			$user = get_user_by('id', (int) get_option('wogh_default_user'));
439 438
 		}
440 439
 
441
-		if ( ! $user ) {
440
+		if ( ! $user) {
442 441
 			return new WP_Error(
443 442
 				'user_not_found',
444 443
 				sprintf(
445
-					__( 'Commit user not found for email %s', 'writing-on-github' ),
444
+					__('Commit user not found for email %s', 'writing-on-github'),
446 445
 					$email
447 446
 				)
448 447
 			);
@@ -460,28 +459,28 @@  discard block
 block discarded – undo
460 459
 	 *
461 460
 	 * @return string|WP_Error
462 461
 	 */
463
-	protected function set_revision_author( $post_id, $user_id ) {
464
-		$revision = wp_get_post_revisions( $post_id );
462
+	protected function set_revision_author($post_id, $user_id) {
463
+		$revision = wp_get_post_revisions($post_id);
465 464
 
466
-		if ( ! $revision ) {
467
-			$new_revision = wp_save_post_revision( $post_id );
465
+		if ( ! $revision) {
466
+			$new_revision = wp_save_post_revision($post_id);
468 467
 
469
-			if ( ! $new_revision || is_wp_error( $new_revision ) ) {
470
-				return new WP_Error( 'db_error', 'There was a problem saving a new revision.' );
468
+			if ( ! $new_revision || is_wp_error($new_revision)) {
469
+				return new WP_Error('db_error', 'There was a problem saving a new revision.');
471 470
 			}
472 471
 
473 472
 			// `wp_save_post_revision` returns the ID, whereas `get_post_revision` returns the whole object
474 473
 			// in order to be consistent, let's make sure we have the whole object before continuing.
475
-			$revision = get_post( $new_revision );
474
+			$revision = get_post($new_revision);
476 475
 
477
-			if ( ! $revision ) {
478
-				return new WP_Error( 'db_error', 'There was a problem retrieving the newly recreated revision.' );
476
+			if ( ! $revision) {
477
+				return new WP_Error('db_error', 'There was a problem retrieving the newly recreated revision.');
479 478
 			}
480 479
 		} else {
481
-			$revision = array_shift( $revision );
480
+			$revision = array_shift($revision);
482 481
 		}
483 482
 
484
-		return $this->set_post_author( $revision->ID, $user_id );
483
+		return $this->set_post_author($revision->ID, $user_id);
485 484
 	}
486 485
 
487 486
 	/**
@@ -494,7 +493,7 @@  discard block
 block discarded – undo
494 493
 	 *
495 494
 	 * @return string|WP_Error
496 495
 	 */
497
-	protected function set_post_author( $post_id, $user_id ) {
496
+	protected function set_post_author($post_id, $user_id) {
498 497
 		global $wpdb;
499 498
 
500 499
 		$result = $wpdb->update(
@@ -505,25 +504,25 @@  discard block
 block discarded – undo
505 504
 			array(
506 505
 				'ID' => (int) $post_id,
507 506
 			),
508
-			array( '%d' ),
509
-			array( '%d' )
507
+			array('%d'),
508
+			array('%d')
510 509
 		);
511 510
 
512
-		if ( false === $result ) {
513
-			return new WP_Error( 'db_error', $wpdb->last_error );
511
+		if (false === $result) {
512
+			return new WP_Error('db_error', $wpdb->last_error);
514 513
 		}
515 514
 
516
-		if ( 0 === $result ) {
515
+		if (0 === $result) {
517 516
 			return sprintf(
518
-				__( 'No change for post ID %d.', 'writing-on-github' ),
517
+				__('No change for post ID %d.', 'writing-on-github'),
519 518
 				$post_id
520 519
 			);
521 520
 		}
522 521
 
523
-		clean_post_cache( $post_id );
522
+		clean_post_cache($post_id);
524 523
 
525 524
 		return sprintf(
526
-			__( 'Successfully updated post ID %d.', 'writing-on-github' ),
525
+			__('Successfully updated post ID %d.', 'writing-on-github'),
527 526
 			$post_id
528 527
 		);
529 528
 	}
@@ -536,7 +535,7 @@  discard block
 block discarded – undo
536 535
 	 *
537 536
 	 * @return bool|int
538 537
 	 */
539
-	public function set_post_sha( $post, $sha ) {
540
-		return update_post_meta( $post->id, '_wogh_sha', $sha );
538
+	public function set_post_sha($post, $sha) {
539
+		return update_post_meta($post->id, '_wogh_sha', $sha);
541 540
 	}
542 541
 }
Please login to merge, or discard this patch.