Passed
Push — ci ( dd2e2e...6fcd36 )
by litefeel
02:42
created
lib/import.php 2 patches
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -26,12 +26,12 @@  discard block
 block discarded – undo
26 26
 		$this->app = $app;
27 27
 	}
28 28
 
29
-    /**
30
-     * Imports a payload.
31
-     * @param  Writing_On_GitHub_Payload $payload
32
-     *
33
-     * @return string|WP_Error
34
-     */
29
+	/**
30
+	 * Imports a payload.
31
+	 * @param  Writing_On_GitHub_Payload $payload
32
+	 *
33
+	 * @return string|WP_Error
34
+	 */
35 35
 	public function payload( Writing_On_GitHub_Payload $payload ) {
36 36
 
37 37
 		$result = $this->app->api()->fetch()->compare( $payload->get_before_commit_id() );
@@ -52,7 +52,7 @@  discard block
 block discarded – undo
52 52
 	/**
53 53
 	 * import blob by files
54 54
 	 * @param  Writing_On_GitHub_File_Info[] $files
55
-     *
55
+	 *
56 56
 	 * @return string|WP_Error
57 57
 	 */
58 58
 	protected function import_files( $files ) {
@@ -103,13 +103,13 @@  discard block
 block discarded – undo
103 103
 		return __( 'Payload processed', 'writing-on-github' );
104 104
 	}
105 105
 
106
-    /**
107
-     * Do compare
108
-     * @param  Writing_On_GitHub_File_Info[]|WP_Error $files
109
-     * @param  int[] &$delete_ids
110
-     *
111
-     * @return string|WP_Error
112
-     */
106
+	/**
107
+	 * Do compare
108
+	 * @param  Writing_On_GitHub_File_Info[]|WP_Error $files
109
+	 * @param  int[] &$delete_ids
110
+	 *
111
+	 * @return string|WP_Error
112
+	 */
113 113
 	protected function compare( $files, &$delete_ids ) {
114 114
 		if ( is_wp_error( $files ) ) {
115 115
 			return $files;
Please login to merge, or discard this patch.
Spacing   +78 added lines, -78 removed lines patch added patch discarded remove patch
@@ -22,7 +22,7 @@  discard block
 block discarded – undo
22 22
 	 *
23 23
 	 * @param Writing_On_GitHub $app Application container.
24 24
 	 */
25
-	public function __construct( Writing_On_GitHub $app ) {
25
+	public function __construct(Writing_On_GitHub $app) {
26 26
 		$this->app = $app;
27 27
 	}
28 28
 
@@ -32,21 +32,21 @@  discard block
 block discarded – undo
32 32
      *
33 33
      * @return string|WP_Error
34 34
      */
35
-	public function payload( Writing_On_GitHub_Payload $payload ) {
35
+	public function payload(Writing_On_GitHub_Payload $payload) {
36 36
 
37
-		$result = $this->app->api()->fetch()->compare( $payload->get_before_commit_id() );
37
+		$result = $this->app->api()->fetch()->compare($payload->get_before_commit_id());
38 38
 
39
-		if ( is_wp_error( $result ) ) {
39
+		if (is_wp_error($result)) {
40 40
 			return $result;
41 41
 		}
42 42
 
43
-		$result = $this->import_files( $result );
43
+		$result = $this->import_files($result);
44 44
 
45
-		if ( is_wp_error( $result ) ) {
45
+		if (is_wp_error($result)) {
46 46
 			return $files;
47 47
 		}
48 48
 
49
-		return __( 'Payload processed', 'writing-on-github' );
49
+		return __('Payload processed', 'writing-on-github');
50 50
 	}
51 51
 
52 52
 	/**
@@ -55,23 +55,23 @@  discard block
 block discarded – undo
55 55
      *
56 56
 	 * @return string|WP_Error
57 57
 	 */
58
-	protected function import_files( $files ) {
58
+	protected function import_files($files) {
59 59
 
60
-		$error 		= false;
60
+		$error = false;
61 61
 		$delete_ids = false;
62 62
 
63
-		$result = $this->compare( $files, $delete_ids );
63
+		$result = $this->compare($files, $delete_ids);
64 64
 
65
-		if ( is_wp_error( $result ) ) {
65
+		if (is_wp_error($result)) {
66 66
 			return $result;
67 67
 		}
68 68
 
69
-		if ( $delete_ids ) {
69
+		if ($delete_ids) {
70 70
 			foreach ($delete_ids as $id) {
71
-				$result = $this->app->database()->delete_post( $id );
72
-				if ( is_wp_error( $result ) ) {
73
-					if ( $error ) {
74
-						$error->add( $result->get_error_code(), $result->get_error_message() );
71
+				$result = $this->app->database()->delete_post($id);
72
+				if (is_wp_error($result)) {
73
+					if ($error) {
74
+						$error->add($result->get_error_code(), $result->get_error_message());
75 75
 					} else {
76 76
 						$error = $result;
77 77
 					}
@@ -90,17 +90,17 @@  discard block
 block discarded – undo
90 90
 	public function master() {
91 91
 		$result = $this->app->api()->fetch()->tree_recursive();
92 92
 
93
-		if ( is_wp_error( $result ) ) {
93
+		if (is_wp_error($result)) {
94 94
 			return $result;
95 95
 		}
96 96
 
97
-		$result = $this->import_files( $result );
97
+		$result = $this->import_files($result);
98 98
 
99
-		if ( is_wp_error( $result ) ) {
99
+		if (is_wp_error($result)) {
100 100
 			return $result;
101 101
 		}
102 102
 
103
-		return __( 'Payload processed', 'writing-on-github' );
103
+		return __('Payload processed', 'writing-on-github');
104 104
 	}
105 105
 
106 106
     /**
@@ -110,8 +110,8 @@  discard block
 block discarded – undo
110 110
      *
111 111
      * @return string|WP_Error
112 112
      */
113
-	protected function compare( $files, &$delete_ids ) {
114
-		if ( is_wp_error( $files ) ) {
113
+	protected function compare($files, &$delete_ids) {
114
+		if (is_wp_error($files)) {
115 115
 			return $files;
116 116
 		}
117 117
 
@@ -120,62 +120,62 @@  discard block
 block discarded – undo
120 120
 
121 121
 		$idsmap = array();
122 122
 
123
-		foreach ( $files as $file ) {
124
-			if ( ! $this->importable_file( $file ) ) {
123
+		foreach ($files as $file) {
124
+			if ( ! $this->importable_file($file)) {
125 125
 				continue;
126 126
 			}
127 127
 
128
-			$blob = $this->app->api()->fetch()->blob( $file );
128
+			$blob = $this->app->api()->fetch()->blob($file);
129 129
 			// network error ?
130
-			if ( ! $blob instanceof Writing_On_GitHub_Blob ) {
130
+			if ( ! $blob instanceof Writing_On_GitHub_Blob) {
131 131
 				continue;
132 132
 			}
133 133
 
134
-			if ( $this->importable_raw_file( $blob ) ) {
135
-				$this->import_raw_file( $blob, $file->status == 'removed' );
134
+			if ($this->importable_raw_file($blob)) {
135
+				$this->import_raw_file($blob, $file->status == 'removed');
136 136
 				continue;
137 137
 			}
138 138
 
139
-			if ( ! $this->importable_blob( $blob ) ) {
139
+			if ( ! $this->importable_blob($blob)) {
140 140
 				continue;
141 141
 			}
142 142
 
143
-			$post = $this->blob_to_post( $blob );
143
+			$post = $this->blob_to_post($blob);
144 144
 
145
-			if ( $file->status == 'removed' ) {
146
-				if ( $blob->id() ) {
145
+			if ($file->status == 'removed') {
146
+				if ($blob->id()) {
147 147
 					$idsmap[$blob->id()] = true;
148 148
 				}
149
-			} elseif ( $post != false ) {
149
+			} elseif ($post != false) {
150 150
 				$posts[] = $post;
151
-				if ( $post->is_new() ) {
151
+				if ($post->is_new()) {
152 152
 					$new[] = $post;
153 153
 				}
154 154
 			}
155 155
 		}
156 156
 
157
-		foreach ( $posts as $post ) {
158
-			if ( $post->id() && isset( $idsmap[ $post->id() ] ) ) {
159
-				unset( $idsmap[ $post->id() ] );
157
+		foreach ($posts as $post) {
158
+			if ($post->id() && isset($idsmap[$post->id()])) {
159
+				unset($idsmap[$post->id()]);
160 160
 			}
161 161
 		}
162 162
 		$delete_ids = array();
163
-		foreach ( $idsmap as $id => $value ) {
163
+		foreach ($idsmap as $id => $value) {
164 164
 			$delete_ids[] = $id;
165 165
 		}
166 166
 
167 167
 		// $this->app->database()->save_posts( $posts, $commit->author_email() );
168 168
 
169
-		$result = $this->app->database()->save_posts( $posts );
169
+		$result = $this->app->database()->save_posts($posts);
170 170
 
171
-		if ( is_wp_error( $result ) ) {
171
+		if (is_wp_error($result)) {
172 172
 			return $result;
173 173
 		}
174 174
 
175
-		if ( ! empty( $new ) ) {
176
-			$result = $this->app->export()->new_posts( $new );
175
+		if ( ! empty($new)) {
176
+			$result = $this->app->export()->new_posts($new);
177 177
 
178
-			if ( is_wp_error( $result ) ) {
178
+			if (is_wp_error($result)) {
179 179
 				return $result;
180 180
 			}
181 181
 		}
@@ -190,12 +190,12 @@  discard block
 block discarded – undo
190 190
 	 *
191 191
 	 * @return bool
192 192
 	 */
193
-	protected function importable_file( Writing_On_GitHub_File_Info $file ) {
193
+	protected function importable_file(Writing_On_GitHub_File_Info $file) {
194 194
 
195 195
 		// only _pages and _posts
196
-		if ( strncasecmp($file->path, '_pages/', strlen('_pages/') ) != 0 &&
197
-			 strncasecmp($file->path, '_posts/', strlen('_posts/') ) != 0 &&
198
-			 strncasecmp($file->path, 'images/', strlen('images/') ) != 0 ) {
196
+		if (strncasecmp($file->path, '_pages/', strlen('_pages/')) != 0 &&
197
+			 strncasecmp($file->path, '_posts/', strlen('_posts/')) != 0 &&
198
+			 strncasecmp($file->path, 'images/', strlen('images/')) != 0) {
199 199
 			return false;
200 200
 		}
201 201
 
@@ -214,7 +214,7 @@  discard block
 block discarded – undo
214 214
 	 *
215 215
 	 * @return bool
216 216
 	 */
217
-	protected function importable_blob( Writing_On_GitHub_Blob $blob ) {
217
+	protected function importable_blob(Writing_On_GitHub_Blob $blob) {
218 218
 		// global $wpdb;
219 219
 
220 220
 		// // Skip the repo's readme.
@@ -227,20 +227,20 @@  discard block
 block discarded – undo
227 227
 		// 	return false;
228 228
 		// }
229 229
 
230
-		if ( ! $blob->has_frontmatter() ) {
230
+		if ( ! $blob->has_frontmatter()) {
231 231
 			return false;
232 232
 		}
233 233
 
234 234
 		return true;
235 235
 	}
236 236
 
237
-	protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) {
238
-		if ( $blob->has_frontmatter() ) {
237
+	protected function importable_raw_file(Writing_On_GitHub_Blob $blob) {
238
+		if ($blob->has_frontmatter()) {
239 239
 			return false;
240 240
 		}
241 241
 
242 242
 		// only images
243
-		if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) {
243
+		if (strncasecmp($blob->path(), 'images/', strlen('images/')) != 0) {
244 244
 			return false;
245 245
 		}
246 246
 
@@ -252,16 +252,16 @@  discard block
 block discarded – undo
252 252
 	 * @param  Writing_On_GitHub_Blob $blob
253 253
 	 * @param  bool                   $is_remove
254 254
 	 */
255
-	protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) {
255
+	protected function import_raw_file(Writing_On_GitHub_Blob $blob, $is_remove) {
256 256
 		$arr = wp_upload_dir();
257 257
 		$path = $arr['basedir'] . '/writing-on-github/' . $blob->path();
258
-		if ( $is_remove ) {
259
-			if ( file_exists($path) ) {
258
+		if ($is_remove) {
259
+			if (file_exists($path)) {
260 260
 				unlink($path);
261 261
 			}
262 262
 		} else {
263 263
 			$dirname = dirname($path);
264
-			if ( ! file_exists($dirname) ) {
264
+			if ( ! file_exists($dirname)) {
265 265
 				wp_mkdir_p($dirname);
266 266
 			}
267 267
 
@@ -276,57 +276,57 @@  discard block
 block discarded – undo
276 276
 	 *
277 277
 	 * @return Writing_On_GitHub_Post|false
278 278
 	 */
279
-	protected function blob_to_post( Writing_On_GitHub_Blob $blob ) {
280
-		$args = array( 'post_content' => $blob->content_import() );
279
+	protected function blob_to_post(Writing_On_GitHub_Blob $blob) {
280
+		$args = array('post_content' => $blob->content_import());
281 281
 		$meta = $blob->meta();
282 282
 
283 283
 		$id = false;
284 284
 
285
-		if ( $meta ) {
286
-			if ( array_key_exists( 'layout', $meta ) ) {
285
+		if ($meta) {
286
+			if (array_key_exists('layout', $meta)) {
287 287
 				$args['post_type'] = $meta['layout'];
288
-				unset( $meta['layout'] );
288
+				unset($meta['layout']);
289 289
 			}
290 290
 
291
-			if ( array_key_exists( 'published', $meta ) ) {
291
+			if (array_key_exists('published', $meta)) {
292 292
 				$args['post_status'] = true === $meta['published'] ? 'publish' : 'draft';
293
-				unset( $meta['published'] );
293
+				unset($meta['published']);
294 294
 			}
295 295
 
296
-			if ( array_key_exists( 'post_title', $meta ) ) {
296
+			if (array_key_exists('post_title', $meta)) {
297 297
 				$args['post_title'] = $meta['post_title'];
298
-				unset( $meta['post_title'] );
298
+				unset($meta['post_title']);
299 299
 			}
300 300
 
301
-			if ( array_key_exists( 'post_name', $meta ) ) {
301
+			if (array_key_exists('post_name', $meta)) {
302 302
 				$args['post_name'] = $meta['post_name'];
303
-				unset( $meta['post_name'] );
303
+				unset($meta['post_name']);
304 304
 			}
305 305
 
306
-			if ( array_key_exists( 'ID', $meta ) ) {
306
+			if (array_key_exists('ID', $meta)) {
307 307
 				$id = $args['ID'] = $meta['ID'];
308 308
 				$blob->set_id($id);
309
-				unset( $meta['ID'] );
309
+				unset($meta['ID']);
310 310
 			}
311 311
 		}
312 312
 
313 313
 		$meta['_wogh_sha'] = $blob->sha();
314 314
 
315
-		if ( $id ) {
316
-			$old_sha = get_post_meta( $id, '_wogh_sha', true );
317
-			$old_github_path = get_post_meta( $id, '_wogh_github_path', true );
315
+		if ($id) {
316
+			$old_sha = get_post_meta($id, '_wogh_sha', true);
317
+			$old_github_path = get_post_meta($id, '_wogh_github_path', true);
318 318
 
319 319
 			// dont save post when has same sha
320
-			if ( $old_sha  && $old_sha == $meta['_wogh_sha'] &&
321
-				 $old_github_path && $old_github_path == $blob->path() ) {
320
+			if ($old_sha && $old_sha == $meta['_wogh_sha'] &&
321
+				 $old_github_path && $old_github_path == $blob->path()) {
322 322
 				return false;
323 323
 			}
324 324
 		}
325 325
 
326
-		$post = new Writing_On_GitHub_Post( $args, $this->app->api() );
327
-		$post->set_old_github_path( $blob->path() );
328
-		$post->set_meta( $meta );
329
-		$blob->set_id( $post->id() );
326
+		$post = new Writing_On_GitHub_Post($args, $this->app->api());
327
+		$post->set_old_github_path($blob->path());
328
+		$post->set_meta($meta);
329
+		$blob->set_id($post->id());
330 330
 
331 331
 		return $post;
332 332
 	}
Please login to merge, or discard this patch.
lib/client/fetch.php 1 patch
Spacing   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -16,13 +16,13 @@  discard block
 block discarded – undo
16 16
 	 *
17 17
 	 * @return Writing_On_GitHub_File_Info[]|WP_Error
18 18
 	 */
19
-	public function compare( $sha ) {
19
+	public function compare($sha) {
20 20
 		// https://api.github.com/repos/litefeel/testwpsync/compare/861f87e8851b8debb78db548269d29f8da4d94ac...master
21 21
 		$endpoint = $this->compare_endpoint();
22 22
 		$branch = $this->branch();
23
-		$data = $this->call( 'GET', "$endpoint/$sha...$branch" );
23
+		$data = $this->call('GET', "$endpoint/$sha...$branch");
24 24
 
25
-		if ( is_wp_error( $data ) ) {
25
+		if (is_wp_error($data)) {
26 26
 			return $data;
27 27
 		}
28 28
 
@@ -44,13 +44,13 @@  discard block
 block discarded – undo
44 44
 	 *
45 45
 	 * @return mixed
46 46
 	 */
47
-	public function remote_contents( $post ) {
48
-		return $this->call( 'GET', $this->content_endpoint( $post->github_path() ) );
47
+	public function remote_contents($post) {
48
+		return $this->call('GET', $this->content_endpoint($post->github_path()));
49 49
 	}
50 50
 
51
-	public function exists( $path ) {
52
-		$result = $this->call( 'GET', $this->content_endpoint( $path ) );
53
-		if ( is_wp_error( $result ) ) {
51
+	public function exists($path) {
52
+		$result = $this->call('GET', $this->content_endpoint($path));
53
+		if (is_wp_error($result)) {
54 54
 			return false;
55 55
 		}
56 56
 		return true;
@@ -63,27 +63,27 @@  discard block
 block discarded – undo
63 63
 	 *
64 64
 	 * @return Writing_On_GitHub_File_Info[]|WP_Error
65 65
 	 */
66
-	public function tree_recursive( $sha = '_default' ) {
66
+	public function tree_recursive($sha = '_default') {
67 67
 
68
-		if ( '_default' === $sha ) {
68
+		if ('_default' === $sha) {
69 69
 			$sha = $this->branch();
70 70
 		}
71 71
 
72
-		$data = $this->call( 'GET', $this->tree_endpoint() . '/' . $sha . '?recursive=1' );
72
+		$data = $this->call('GET', $this->tree_endpoint() . '/' . $sha . '?recursive=1');
73 73
 
74
-		if ( is_wp_error( $data ) ) {
74
+		if (is_wp_error($data)) {
75 75
 			return $data;
76 76
 		}
77 77
 
78 78
 		$files = array();
79 79
 
80
-		foreach ( $data->tree as $index => $thing ) {
80
+		foreach ($data->tree as $index => $thing) {
81 81
 			// We need to remove the trees because
82 82
 			// the recursive tree includes both
83 83
 			// the subtrees as well the subtrees' blobs.
84
-			if ( 'blob' === $thing->type ) {
84
+			if ('blob' === $thing->type) {
85 85
 				$thing->status = '';
86
-				$files[] = new Writing_On_GitHub_File_Info( $thing );
86
+				$files[] = new Writing_On_GitHub_File_Info($thing);
87 87
 			}
88 88
 		}
89 89
 
@@ -97,14 +97,14 @@  discard block
 block discarded – undo
97 97
 	 *
98 98
 	 * @return Writing_On_GitHub_Blob|WP_Error
99 99
 	 */
100
-	public function blob( Writing_On_GitHub_File_Info $fileinfo ) {
101
-		$data = $this->call( 'GET', $this->blob_endpoint() . '/' . $fileinfo->sha );
100
+	public function blob(Writing_On_GitHub_File_Info $fileinfo) {
101
+		$data = $this->call('GET', $this->blob_endpoint() . '/' . $fileinfo->sha);
102 102
 
103
-		if ( is_wp_error( $data ) ) {
103
+		if (is_wp_error($data)) {
104 104
 			return $data;
105 105
 		}
106 106
 
107 107
 		$data->path = $fileinfo->path;
108
-		return new Writing_On_GitHub_Blob( $data );
108
+		return new Writing_On_GitHub_Blob($data);
109 109
 	}
110 110
 }
Please login to merge, or discard this patch.
lib/database.php 2 patches
Indentation   +45 added lines, -45 removed lines patch added patch discarded remove patch
@@ -42,10 +42,10 @@  discard block
 block discarded – undo
42 42
 	/**
43 43
 	 * Queries the database for all of the supported posts.
44 44
 	 *
45
-     * @param  bool $force
46
-     *
47
-     * @return Writing_On_GitHub_Post[]|WP_Error
48
-     */
45
+	 * @param  bool $force
46
+	 *
47
+	 * @return Writing_On_GitHub_Post[]|WP_Error
48
+	 */
49 49
 	public function fetch_all_supported( $force = false ) {
50 50
 		$args  = array(
51 51
 			'post_type'   => $this->get_whitelisted_post_types(),
@@ -187,45 +187,45 @@  discard block
 block discarded – undo
187 187
 
188 188
 		// update tags
189 189
 		if ( isset( $meta['tags'] ) && $meta['tags'] ) {
190
-		    $args['tags_input'] = $meta['tags'];
190
+			$args['tags_input'] = $meta['tags'];
191 191
 		}
192 192
 
193 193
 		// update categories
194 194
 		if ( isset( $meta['categories'] ) && $meta['categories'] ) {
195
-		    $categories = $meta['categories'];
196
-		    if (!is_array($categories)) {
197
-		        $categories = array($categories);
198
-		    }
199
-		    $terms = get_terms(array(
200
-		        'taxonomy' => 'category',
201
-		        'fields' => 'id=>name',
202
-		        'hide_empty' => 0,
203
-		        'name' => $categories
204
-		        )
205
-		    );
206
-		    $map = array();
207
-		    foreach ($categories as $name) {
208
-		        $map[$name] = 1;
209
-		    }
210
-
211
-		    $ids = array();
212
-		    if (!empty($terms)) {
213
-		        foreach ($terms as $id => $name) {
214
-		            $ids[] = $id;
215
-		            unset($map[$name]);
216
-		        }
217
-		    }
218
-
219
-		    // create new terms
220
-		    if (!empty($map)) {
221
-		        foreach ($map as $name => $value) {
222
-		            $term = wp_insert_term($name, 'category', array('parent' => 0));
223
-		            // array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
224
-		            $ids[] = $term['term_id'];
225
-		        }
226
-		    }
227
-
228
-		    $args['post_category'] = $ids;
195
+			$categories = $meta['categories'];
196
+			if (!is_array($categories)) {
197
+				$categories = array($categories);
198
+			}
199
+			$terms = get_terms(array(
200
+				'taxonomy' => 'category',
201
+				'fields' => 'id=>name',
202
+				'hide_empty' => 0,
203
+				'name' => $categories
204
+				)
205
+			);
206
+			$map = array();
207
+			foreach ($categories as $name) {
208
+				$map[$name] = 1;
209
+			}
210
+
211
+			$ids = array();
212
+			if (!empty($terms)) {
213
+				foreach ($terms as $id => $name) {
214
+					$ids[] = $id;
215
+					unset($map[$name]);
216
+				}
217
+			}
218
+
219
+			// create new terms
220
+			if (!empty($map)) {
221
+				foreach ($map as $name => $value) {
222
+					$term = wp_insert_term($name, 'category', array('parent' => 0));
223
+					// array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
224
+					$ids[] = $term['term_id'];
225
+				}
226
+			}
227
+
228
+			$args['post_category'] = $ids;
229 229
 		}
230 230
 
231 231
 		return $args;
@@ -427,12 +427,12 @@  discard block
 block discarded – undo
427 427
 		if ( ! empty( $display_name ) ) {
428 428
 			$search_string = esc_attr( $display_name );
429 429
 			$query = new WP_User_Query( array(
430
-			    'search'         => "{$search_string}",
431
-			    'search_columns' => array(
432
-			        'display_name',
433
-			        'user_nicename',
434
-			        'user_login',
435
-			    )
430
+				'search'         => "{$search_string}",
431
+				'search_columns' => array(
432
+					'display_name',
433
+					'user_nicename',
434
+					'user_login',
435
+				)
436 436
 			) );
437 437
 			$users = $query->get_results();
438 438
 			$user = empty($users) ? false : $users[0];
Please login to merge, or discard this patch.
Spacing   +115 added lines, -116 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
 
@@ -46,32 +46,32 @@  discard block
 block discarded – undo
46 46
      *
47 47
      * @return Writing_On_GitHub_Post[]|WP_Error
48 48
      */
49
-	public function fetch_all_supported( $force = false ) {
50
-		$args  = array(
49
+	public function fetch_all_supported($force = false) {
50
+		$args = array(
51 51
 			'post_type'   => $this->get_whitelisted_post_types(),
52 52
 			'post_status' => $this->get_whitelisted_post_statuses(),
53 53
 			'nopaging'    => true,
54 54
 			'fields'      => 'ids',
55 55
 		);
56 56
 
57
-		$query = new WP_Query( apply_filters( 'wogh_pre_fetch_all_supported', $args ) );
57
+		$query = new WP_Query(apply_filters('wogh_pre_fetch_all_supported', $args));
58 58
 
59 59
 		$post_ids = $query->get_posts();
60 60
 
61
-		if ( ! $post_ids ) {
61
+		if ( ! $post_ids) {
62 62
 			return new WP_Error(
63 63
 				'no_results',
64
-				__( 'Querying for supported posts returned no results.', 'writing-on-github' )
64
+				__('Querying for supported posts returned no results.', 'writing-on-github')
65 65
 			);
66 66
 		}
67 67
 
68 68
 		$results = array();
69
-		foreach ( $post_ids as $post_id ) {
69
+		foreach ($post_ids as $post_id) {
70 70
 			// Do not export posts that have already been exported
71
-			if ( $force || ! get_post_meta( $post_id, '_wogh_sha', true ) ||
72
-				 ! get_post_meta( $post_id, '_wogh_github_path', true ) ) {
71
+			if ($force || ! get_post_meta($post_id, '_wogh_sha', true) ||
72
+				 ! get_post_meta($post_id, '_wogh_github_path', true)) {
73 73
 
74
-				$results[] = new Writing_On_GitHub_Post( $post_id, $this->app->api() );
74
+				$results[] = new Writing_On_GitHub_Post($post_id, $this->app->api());
75 75
 			}
76 76
 		}
77 77
 
@@ -85,10 +85,10 @@  discard block
 block discarded – undo
85 85
 	 *
86 86
 	 * @return WP_Error|Writing_On_GitHub_Post
87 87
 	 */
88
-	public function fetch_by_id( $post_id ) {
89
-		$post = new Writing_On_GitHub_Post( $post_id, $this->app->api() );
88
+	public function fetch_by_id($post_id) {
89
+		$post = new Writing_On_GitHub_Post($post_id, $this->app->api());
90 90
 
91
-		if ( ! $this->is_post_supported( $post ) ) {
91
+		if ( ! $this->is_post_supported($post)) {
92 92
 			return new WP_Error(
93 93
 				'unsupported_post',
94 94
 				sprintf(
@@ -112,7 +112,7 @@  discard block
 block discarded – undo
112 112
 	 *
113 113
 	 * @return string|WP_Error
114 114
 	 */
115
-	public function save_posts( array $posts ) {
115
+	public function save_posts(array $posts) {
116 116
 
117 117
 		/**
118 118
 		 * Whether an error has occurred.
@@ -121,20 +121,19 @@  discard block
 block discarded – undo
121 121
 		 */
122 122
 		$error = false;
123 123
 
124
-		foreach ( $posts as $post ) {
125
-			$args = apply_filters( 'wogh_pre_import_args', $this->post_args( $post ), $post );
124
+		foreach ($posts as $post) {
125
+			$args = apply_filters('wogh_pre_import_args', $this->post_args($post), $post);
126 126
 
127
-			remove_filter( 'content_save_pre', 'wp_filter_post_kses' );
127
+			remove_filter('content_save_pre', 'wp_filter_post_kses');
128 128
 			$post_id = $post->is_new() ?
129
-				wp_insert_post( $args, true ) :
130
-				wp_update_post( $args, true );
131
-			add_filter( 'content_save_pre', 'wp_filter_post_kses' );
129
+				wp_insert_post($args, true) : wp_update_post($args, true);
130
+			add_filter('content_save_pre', 'wp_filter_post_kses');
132 131
 
133
-			if ( is_wp_error( $post_id ) ) {
134
-				if ( ! $error ) {
132
+			if (is_wp_error($post_id)) {
133
+				if ( ! $error) {
135 134
 					$error = $post_id;
136 135
 				} else {
137
-					$error->add( $post_id->get_error_code(), $post_id->get_error_message() );
136
+					$error->add($post_id->get_error_code(), $post_id->get_error_message());
138 137
 				}
139 138
 
140 139
 				// Abort saving if updating the post fails.
@@ -143,20 +142,20 @@  discard block
 block discarded – undo
143 142
 
144 143
 			// $this->set_revision_author( $post_id, $user_id );
145 144
 
146
-			if ( $post->is_new() ) {
145
+			if ($post->is_new()) {
147 146
 				$author = false;
148 147
 				$meta = $post->get_meta();
149
-				if ( ! empty( $meta ) && ! empty( $meta['author'] ) ) {
148
+				if ( ! empty($meta) && ! empty($meta['author'])) {
150 149
 					$author = $meta['author'];
151 150
 				}
152
-				$user    = $this->fetch_commit_user( $author );
153
-				$user_id = ! is_wp_error( $user ) ? $user->ID : 0;
154
-				$this->set_post_author( $post_id, $user_id );
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);
155 154
 			}
156 155
 
157
-			$post->set_post( get_post( $post_id ) );
156
+			$post->set_post(get_post($post_id));
158 157
 
159
-			$meta = apply_filters( 'wogh_pre_import_meta', $post->get_meta(), $post );
158
+			$meta = apply_filters('wogh_pre_import_meta', $post->get_meta(), $post);
160 159
 
161 160
 			// unset( $meta['tags'] );
162 161
 			// unset( $meta['categories'] );
@@ -171,14 +170,14 @@  discard block
 block discarded – undo
171 170
 			// }
172 171
 		}
173 172
 
174
-		if ( $error ) {
173
+		if ($error) {
175 174
 			return $error;
176 175
 		}
177 176
 
178
-		return __( 'Successfully saved posts.', 'writing-on-github' );
177
+		return __('Successfully saved posts.', 'writing-on-github');
179 178
 	}
180 179
 
181
-	protected function post_args( $post ) {
180
+	protected function post_args($post) {
182 181
 		$args = $post->get_args();
183 182
 		$meta = $post->get_meta();
184 183
 
@@ -186,14 +185,14 @@  discard block
 block discarded – undo
186 185
 		$args['post_content'] = addslashes($args['post_content']);
187 186
 
188 187
 		// update tags
189
-		if ( isset( $meta['tags'] ) && $meta['tags'] ) {
188
+		if (isset($meta['tags']) && $meta['tags']) {
190 189
 		    $args['tags_input'] = $meta['tags'];
191 190
 		}
192 191
 
193 192
 		// update categories
194
-		if ( isset( $meta['categories'] ) && $meta['categories'] ) {
193
+		if (isset($meta['categories']) && $meta['categories']) {
195 194
 		    $categories = $meta['categories'];
196
-		    if (!is_array($categories)) {
195
+		    if ( ! is_array($categories)) {
197 196
 		        $categories = array($categories);
198 197
 		    }
199 198
 		    $terms = get_terms(array(
@@ -209,7 +208,7 @@  discard block
 block discarded – undo
209 208
 		    }
210 209
 
211 210
 		    $ids = array();
212
-		    if (!empty($terms)) {
211
+		    if ( ! empty($terms)) {
213 212
 		        foreach ($terms as $id => $name) {
214 213
 		            $ids[] = $id;
215 214
 		            unset($map[$name]);
@@ -217,7 +216,7 @@  discard block
 block discarded – undo
217 216
 		    }
218 217
 
219 218
 		    // create new terms
220
-		    if (!empty($map)) {
219
+		    if ( ! empty($map)) {
221 220
 		        foreach ($map as $name => $value) {
222 221
 		            $term = wp_insert_term($name, 'category', array('parent' => 0));
223 222
 		            // array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
@@ -238,77 +237,77 @@  discard block
 block discarded – undo
238 237
 	 *
239 238
 	 * @return string|WP_Error
240 239
 	 */
241
-	public function delete_post_by_path( $path ) {
242
-		$query = new WP_Query( array(
240
+	public function delete_post_by_path($path) {
241
+		$query = new WP_Query(array(
243 242
 			'meta_key'       => '_wogh_github_path',
244 243
 			'meta_value'     => $path,
245 244
 			'meta_compare'   => '=',
246 245
 			'posts_per_page' => 1,
247 246
 			'fields'         => 'ids',
248
-		) );
247
+		));
249 248
 
250 249
 		$post_id = $query->get_posts();
251
-		$post_id = array_pop( $post_id );
250
+		$post_id = array_pop($post_id);
252 251
 
253
-		if ( ! $post_id ) {
254
-			$parts     = explode( '/', $path );
255
-			$filename  = array_pop( $parts );
256
-			$directory = $parts ? array_shift( $parts ) : '';
252
+		if ( ! $post_id) {
253
+			$parts     = explode('/', $path);
254
+			$filename  = array_pop($parts);
255
+			$directory = $parts ? array_shift($parts) : '';
257 256
 
258
-			if ( false !== strpos( $directory, 'post' ) ) {
259
-				preg_match( '/([0-9]{4})-([0-9]{2})-([0-9]{2})-(.*)\.md/', $filename, $matches );
257
+			if (false !== strpos($directory, 'post')) {
258
+				preg_match('/([0-9]{4})-([0-9]{2})-([0-9]{2})-(.*)\.md/', $filename, $matches);
260 259
 				$title = $matches[4];
261 260
 
262
-				$query = new WP_Query( array(
261
+				$query = new WP_Query(array(
263 262
 					'name'     => $title,
264 263
 					'posts_per_page' => 1,
265 264
 					'post_type' => $this->get_whitelisted_post_types(),
266 265
 					'fields'         => 'ids',
267
-				) );
266
+				));
268 267
 
269 268
 				$post_id = $query->get_posts();
270
-				$post_id = array_pop( $post_id );
269
+				$post_id = array_pop($post_id);
271 270
 			}
272 271
 
273
-			if ( ! $post_id ) {
274
-				preg_match( '/(.*)\.md/', $filename, $matches );
272
+			if ( ! $post_id) {
273
+				preg_match('/(.*)\.md/', $filename, $matches);
275 274
 				$title = $matches[1];
276 275
 
277
-				$query = new WP_Query( array(
276
+				$query = new WP_Query(array(
278 277
 					'name'     => $title,
279 278
 					'posts_per_page' => 1,
280 279
 					'post_type' => $this->get_whitelisted_post_types(),
281 280
 					'fields'         => 'ids',
282
-				) );
281
+				));
283 282
 
284 283
 				$post_id = $query->get_posts();
285
-				$post_id = array_pop( $post_id );
284
+				$post_id = array_pop($post_id);
286 285
 			}
287 286
 		}
288 287
 
289
-		if ( ! $post_id ) {
288
+		if ( ! $post_id) {
290 289
 			return new WP_Error(
291 290
 				'path_not_found',
292 291
 				sprintf(
293
-					__( 'Post not found for path %s.', 'writing-on-github' ),
292
+					__('Post not found for path %s.', 'writing-on-github'),
294 293
 					$path
295 294
 				)
296 295
 			);
297 296
 		}
298 297
 
299
-		$result = wp_delete_post( $post_id );
298
+		$result = wp_delete_post($post_id);
300 299
 
301 300
 		// If deleting fails...
302
-		if ( false === $result ) {
303
-			$post = get_post( $post_id );
301
+		if (false === $result) {
302
+			$post = get_post($post_id);
304 303
 
305 304
 			// ...and the post both exists and isn't in the trash...
306
-			if ( $post && 'trash' !== $post->post_status ) {
305
+			if ($post && 'trash' !== $post->post_status) {
307 306
 				// ... then something went wrong.
308 307
 				return new WP_Error(
309 308
 					'db_error',
310 309
 					sprintf(
311
-						__( 'Failed to delete post ID %d.', 'writing-on-github' ),
310
+						__('Failed to delete post ID %d.', 'writing-on-github'),
312 311
 						$post_id
313 312
 					)
314 313
 				);
@@ -316,25 +315,25 @@  discard block
 block discarded – undo
316 315
 		}
317 316
 
318 317
 		return sprintf(
319
-			__( 'Successfully deleted post ID %d.', 'writing-on-github' ),
318
+			__('Successfully deleted post ID %d.', 'writing-on-github'),
320 319
 			$post_id
321 320
 		);
322 321
 	}
323 322
 
324
-	public function delete_post( $post_id ) {
325
-		$result = wp_delete_post( $post_id );
323
+	public function delete_post($post_id) {
324
+		$result = wp_delete_post($post_id);
326 325
 
327 326
 		// If deleting fails...
328
-		if ( false === $result ) {
329
-			$post = get_post( $post_id );
327
+		if (false === $result) {
328
+			$post = get_post($post_id);
330 329
 
331 330
 			// ...and the post both exists and isn't in the trash...
332
-			if ( $post && 'trash' !== $post->post_status ) {
331
+			if ($post && 'trash' !== $post->post_status) {
333 332
 				// ... then something went wrong.
334 333
 				return new WP_Error(
335 334
 					'db_error',
336 335
 					sprintf(
337
-						__( 'Failed to delete post ID %d.', 'writing-on-github' ),
336
+						__('Failed to delete post ID %d.', 'writing-on-github'),
338 337
 						$post_id
339 338
 					)
340 339
 				);
@@ -342,7 +341,7 @@  discard block
 block discarded – undo
342 341
 		}
343 342
 
344 343
 		return sprintf(
345
-			__( 'Successfully deleted post ID %d.', 'writing-on-github' ),
344
+			__('Successfully deleted post ID %d.', 'writing-on-github'),
346 345
 			$post_id
347 346
 		);
348 347
 	}
@@ -353,7 +352,7 @@  discard block
 block discarded – undo
353 352
 	 * @return array
354 353
 	 */
355 354
 	protected function get_whitelisted_post_types() {
356
-		return apply_filters( 'wogh_whitelisted_post_types', $this->whitelisted_post_types );
355
+		return apply_filters('wogh_whitelisted_post_types', $this->whitelisted_post_types);
357 356
 	}
358 357
 
359 358
 	/**
@@ -362,7 +361,7 @@  discard block
 block discarded – undo
362 361
 	 * @return array
363 362
 	 */
364 363
 	protected function get_whitelisted_post_statuses() {
365
-		return apply_filters( 'wogh_whitelisted_post_statuses', $this->whitelisted_post_statuses );
364
+		return apply_filters('wogh_whitelisted_post_statuses', $this->whitelisted_post_statuses);
366 365
 	}
367 366
 
368 367
 	/**
@@ -372,12 +371,12 @@  discard block
 block discarded – undo
372 371
 	 *
373 372
 	 * @return string Whitelist formatted for query
374 373
 	 */
375
-	protected function format_for_query( $whitelist ) {
376
-		foreach ( $whitelist as $key => $value ) {
377
-			$whitelist[ $key ] = "'$value'";
374
+	protected function format_for_query($whitelist) {
375
+		foreach ($whitelist as $key => $value) {
376
+			$whitelist[$key] = "'$value'";
378 377
 		}
379 378
 
380
-		return implode( ', ', $whitelist );
379
+		return implode(', ', $whitelist);
381 380
 	}
382 381
 
383 382
 	/**
@@ -388,25 +387,25 @@  discard block
 block discarded – undo
388 387
 	 *
389 388
 	 * @return boolean                          True if supported, false if not.
390 389
 	 */
391
-	protected function is_post_supported( Writing_On_GitHub_Post $post ) {
392
-		if ( wp_is_post_revision( $post->id ) ) {
390
+	protected function is_post_supported(Writing_On_GitHub_Post $post) {
391
+		if (wp_is_post_revision($post->id)) {
393 392
 			return false;
394 393
 		}
395 394
 
396 395
 		// We need to allow trashed posts to be queried, but they are not whitelisted for export.
397
-		if ( ! in_array( $post->status(), $this->get_whitelisted_post_statuses() ) && 'trash' !== $post->status() ) {
396
+		if ( ! in_array($post->status(), $this->get_whitelisted_post_statuses()) && 'trash' !== $post->status()) {
398 397
 			return false;
399 398
 		}
400 399
 
401
-		if ( ! in_array( $post->type(), $this->get_whitelisted_post_types() ) ) {
400
+		if ( ! in_array($post->type(), $this->get_whitelisted_post_types())) {
402 401
 			return false;
403 402
 		}
404 403
 
405
-		if ( $post->has_password() ) {
404
+		if ($post->has_password()) {
406 405
 			return false;
407 406
 		}
408 407
 
409
-		return apply_filters( 'wogh_is_post_supported', true, $post );
408
+		return apply_filters('wogh_is_post_supported', true, $post);
410 409
 	}
411 410
 
412 411
 	/**
@@ -419,35 +418,35 @@  discard block
 block discarded – undo
419 418
 	 *
420 419
 	 * @return WP_Error|WP_User
421 420
 	 */
422
-	protected function fetch_commit_user( $display_name ) {
421
+	protected function fetch_commit_user($display_name) {
423 422
 		// If we can't find a user and a default hasn't been set,
424 423
 		// we're just going to set the revision author to 0.
425 424
 		$user = false;
426 425
 
427
-		if ( ! empty( $display_name ) ) {
428
-			$search_string = esc_attr( $display_name );
429
-			$query = new WP_User_Query( array(
426
+		if ( ! empty($display_name)) {
427
+			$search_string = esc_attr($display_name);
428
+			$query = new WP_User_Query(array(
430 429
 			    'search'         => "{$search_string}",
431 430
 			    'search_columns' => array(
432 431
 			        'display_name',
433 432
 			        'user_nicename',
434 433
 			        'user_login',
435 434
 			    )
436
-			) );
435
+			));
437 436
 			$users = $query->get_results();
438 437
 			$user = empty($users) ? false : $users[0];
439 438
 		}
440 439
 
441
-		if ( ! $user ) {
440
+		if ( ! $user) {
442 441
 			// Use the default user.
443
-			$user = get_user_by( 'id', (int) get_option( 'wogh_default_user' ) );
442
+			$user = get_user_by('id', (int) get_option('wogh_default_user'));
444 443
 		}
445 444
 
446
-		if ( ! $user ) {
445
+		if ( ! $user) {
447 446
 			return new WP_Error(
448 447
 				'user_not_found',
449 448
 				sprintf(
450
-					__( 'Commit user not found for email %s', 'writing-on-github' ),
449
+					__('Commit user not found for email %s', 'writing-on-github'),
451 450
 					$email
452 451
 				)
453 452
 			);
@@ -465,28 +464,28 @@  discard block
 block discarded – undo
465 464
 	 *
466 465
 	 * @return string|WP_Error
467 466
 	 */
468
-	protected function set_revision_author( $post_id, $user_id ) {
469
-		$revision = wp_get_post_revisions( $post_id );
467
+	protected function set_revision_author($post_id, $user_id) {
468
+		$revision = wp_get_post_revisions($post_id);
470 469
 
471
-		if ( ! $revision ) {
472
-			$new_revision = wp_save_post_revision( $post_id );
470
+		if ( ! $revision) {
471
+			$new_revision = wp_save_post_revision($post_id);
473 472
 
474
-			if ( ! $new_revision || is_wp_error( $new_revision ) ) {
475
-				return new WP_Error( 'db_error', 'There was a problem saving a new revision.' );
473
+			if ( ! $new_revision || is_wp_error($new_revision)) {
474
+				return new WP_Error('db_error', 'There was a problem saving a new revision.');
476 475
 			}
477 476
 
478 477
 			// `wp_save_post_revision` returns the ID, whereas `get_post_revision` returns the whole object
479 478
 			// in order to be consistent, let's make sure we have the whole object before continuing.
480
-			$revision = get_post( $new_revision );
479
+			$revision = get_post($new_revision);
481 480
 
482
-			if ( ! $revision ) {
483
-				return new WP_Error( 'db_error', 'There was a problem retrieving the newly recreated revision.' );
481
+			if ( ! $revision) {
482
+				return new WP_Error('db_error', 'There was a problem retrieving the newly recreated revision.');
484 483
 			}
485 484
 		} else {
486
-			$revision = array_shift( $revision );
485
+			$revision = array_shift($revision);
487 486
 		}
488 487
 
489
-		return $this->set_post_author( $revision->ID, $user_id );
488
+		return $this->set_post_author($revision->ID, $user_id);
490 489
 	}
491 490
 
492 491
 	/**
@@ -499,7 +498,7 @@  discard block
 block discarded – undo
499 498
 	 *
500 499
 	 * @return string|WP_Error
501 500
 	 */
502
-	protected function set_post_author( $post_id, $user_id ) {
501
+	protected function set_post_author($post_id, $user_id) {
503 502
 		global $wpdb;
504 503
 
505 504
 		$result = $wpdb->update(
@@ -510,25 +509,25 @@  discard block
 block discarded – undo
510 509
 			array(
511 510
 				'ID' => (int) $post_id,
512 511
 			),
513
-			array( '%d' ),
514
-			array( '%d' )
512
+			array('%d'),
513
+			array('%d')
515 514
 		);
516 515
 
517
-		if ( false === $result ) {
518
-			return new WP_Error( 'db_error', $wpdb->last_error );
516
+		if (false === $result) {
517
+			return new WP_Error('db_error', $wpdb->last_error);
519 518
 		}
520 519
 
521
-		if ( 0 === $result ) {
520
+		if (0 === $result) {
522 521
 			return sprintf(
523
-				__( 'No change for post ID %d.', 'writing-on-github' ),
522
+				__('No change for post ID %d.', 'writing-on-github'),
524 523
 				$post_id
525 524
 			);
526 525
 		}
527 526
 
528
-		clean_post_cache( $post_id );
527
+		clean_post_cache($post_id);
529 528
 
530 529
 		return sprintf(
531
-			__( 'Successfully updated post ID %d.', 'writing-on-github' ),
530
+			__('Successfully updated post ID %d.', 'writing-on-github'),
532 531
 			$post_id
533 532
 		);
534 533
 	}
@@ -541,7 +540,7 @@  discard block
 block discarded – undo
541 540
 	 *
542 541
 	 * @return bool|int
543 542
 	 */
544
-	public function set_post_sha( $post, $sha ) {
545
-		return update_post_meta( $post->id, '_wogh_sha', $sha );
543
+	public function set_post_sha($post, $sha) {
544
+		return update_post_meta($post->id, '_wogh_sha', $sha);
546 545
 	}
547 546
 }
Please login to merge, or discard this patch.