Passed
Push — ci ( 3d0aed...c9c5cb )
by litefeel
02:33
created
lib/import.php 1 patch
Spacing   +70 added lines, -70 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,24 +32,24 @@  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
             /* @var WP_Error $result */
41 41
             return $result;
42 42
         }
43 43
 
44
-        if ( is_array( $result ) ) {
45
-            $result = $this->import_files( $result );
44
+        if (is_array($result)) {
45
+            $result = $this->import_files($result);
46 46
         }
47 47
 
48
-        if ( is_wp_error( $result ) ) {
48
+        if (is_wp_error($result)) {
49 49
             return $files;
50 50
         }
51 51
 
52
-        return __( 'Payload processed', 'writing-on-github' );
52
+        return __('Payload processed', 'writing-on-github');
53 53
     }
54 54
 
55 55
     /**
@@ -58,33 +58,33 @@  discard block
 block discarded – undo
58 58
      *
59 59
      * @return true|WP_Error
60 60
      */
61
-    protected function import_files( $files ) {
61
+    protected function import_files($files) {
62 62
 
63 63
         $error = true;
64 64
 
65
-        foreach ( $files as $file ) {
66
-            if ( ! $this->importable_file( $file ) ) {
65
+        foreach ($files as $file) {
66
+            if ( ! $this->importable_file($file)) {
67 67
                 continue;
68 68
             }
69 69
 
70
-            $blob = $this->app->api()->fetch()->blob( $file );
70
+            $blob = $this->app->api()->fetch()->blob($file);
71 71
             // network error ?
72
-            if ( ! $blob instanceof Writing_On_GitHub_Blob ) {
72
+            if ( ! $blob instanceof Writing_On_GitHub_Blob) {
73 73
                 continue;
74 74
             }
75 75
 
76 76
             $is_remove = 'removed' == $file->status;
77 77
 
78 78
             $result = false;
79
-            if ( $this->importable_raw_file( $blob ) ) {
80
-                $result = $this->import_raw_file( $blob, $is_remove );
81
-            } elseif ( $this->importable_post( $blob ) ) {
82
-                $result = $this->import_post( $blob, $is_remove );
79
+            if ($this->importable_raw_file($blob)) {
80
+                $result = $this->import_raw_file($blob, $is_remove);
81
+            } elseif ($this->importable_post($blob)) {
82
+                $result = $this->import_post($blob, $is_remove);
83 83
             }
84 84
 
85
-            if ( is_wp_error( $result ) ) {
85
+            if (is_wp_error($result)) {
86 86
                 /* @var WP_Error $result */
87
-                $error = wogh_append_error( $error, $result );
87
+                $error = wogh_append_error($error, $result);
88 88
             }
89 89
         }
90 90
 
@@ -99,21 +99,21 @@  discard block
 block discarded – undo
99 99
     public function master() {
100 100
         $result = $this->app->api()->fetch()->tree_recursive();
101 101
 
102
-        if ( is_wp_error( $result ) ) {
102
+        if (is_wp_error($result)) {
103 103
             /* @var WP_Error $result */
104 104
             return $result;
105 105
         }
106 106
 
107
-        if ( is_array( $result ) ) {
108
-            $result = $this->import_files( $result );
107
+        if (is_array($result)) {
108
+            $result = $this->import_files($result);
109 109
         }
110 110
 
111
-        if ( is_wp_error( $result ) ) {
111
+        if (is_wp_error($result)) {
112 112
             /* @var WP_Error $result */
113 113
             return $result;
114 114
         }
115 115
 
116
-        return __( 'Payload processed', 'writing-on-github' );
116
+        return __('Payload processed', 'writing-on-github');
117 117
     }
118 118
 
119 119
     /**
@@ -123,14 +123,14 @@  discard block
 block discarded – undo
123 123
      *
124 124
      * @return bool
125 125
      */
126
-    protected function importable_file( Writing_On_GitHub_File_Info $file ) {
126
+    protected function importable_file(Writing_On_GitHub_File_Info $file) {
127 127
 
128 128
         $path = $file->path;
129 129
 
130 130
         // only _pages, _posts and images
131
-        $prefixs = array( '_pages/', '_posts/', 'images/');
131
+        $prefixs = array('_pages/', '_posts/', 'images/');
132 132
         foreach ($prefixs as $prefix) {
133
-            if ( ! strncasecmp($path, $prefix, strlen( $prefix ) ) ) {
133
+            if ( ! strncasecmp($path, $prefix, strlen($prefix))) {
134 134
                 return true;
135 135
             }
136 136
         }
@@ -144,7 +144,7 @@  discard block
 block discarded – undo
144 144
      *
145 145
      * @return bool
146 146
      */
147
-    protected function importable_post( Writing_On_GitHub_Blob $blob ) {
147
+    protected function importable_post(Writing_On_GitHub_Blob $blob) {
148 148
         // global $wpdb;
149 149
 
150 150
         // // Skip the repo's readme.
@@ -157,7 +157,7 @@  discard block
 block discarded – undo
157 157
         //  return false;
158 158
         // }
159 159
 
160
-        if ( ! $blob->has_frontmatter() ) {
160
+        if ( ! $blob->has_frontmatter()) {
161 161
             return false;
162 162
         }
163 163
 
@@ -170,37 +170,37 @@  discard block
 block discarded – undo
170 170
      * @param  bool                   $is_remove
171 171
      * @return WP_Error|bool
172 172
      */
173
-    protected function import_post( Writing_On_GitHub_Blob $blob, $is_remove ) {
173
+    protected function import_post(Writing_On_GitHub_Blob $blob, $is_remove) {
174 174
 
175
-        if ( $is_remove ) {
175
+        if ($is_remove) {
176 176
             $id = $blob->id();
177
-            if ( empty( $id ) ) {
177
+            if (empty($id)) {
178 178
                 return false;
179 179
             }
180
-            $result = $this->app->database()->delete_post( $id );
181
-            if ( is_wp_error( $result ) ) {
180
+            $result = $this->app->database()->delete_post($id);
181
+            if (is_wp_error($result)) {
182 182
                 /* @var WP_Error $result */
183 183
                 return $result;
184 184
             }
185 185
             return true;
186 186
         }
187 187
 
188
-        $post = $this->blob_to_post( $blob );
188
+        $post = $this->blob_to_post($blob);
189 189
 
190
-        if ( ! $post instanceof Writing_On_GitHub_Post ) {
190
+        if ( ! $post instanceof Writing_On_GitHub_Post) {
191 191
             return false;
192 192
         }
193 193
 
194
-        $result = $this->app->database()->save_post( $post );
195
-        if ( is_wp_error( $result ) ) {
194
+        $result = $this->app->database()->save_post($post);
195
+        if (is_wp_error($result)) {
196 196
             /** @var WP_Error $result */
197 197
             return $result;
198 198
         }
199 199
 
200
-        if ( $post->is_new() ) {
201
-            $result = $this->app->export()->new_posts( $new );
200
+        if ($post->is_new()) {
201
+            $result = $this->app->export()->new_posts($new);
202 202
 
203
-            if ( is_wp_error( $result ) ) {
203
+            if (is_wp_error($result)) {
204 204
                 /** @var WP_Error $result */
205 205
                 return $result;
206 206
             }
@@ -214,13 +214,13 @@  discard block
 block discarded – undo
214 214
      * @param  Writing_On_GitHub_Blob $blob
215 215
      * @return bool
216 216
      */
217
-    protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) {
218
-        if ( $blob->has_frontmatter() ) {
217
+    protected function importable_raw_file(Writing_On_GitHub_Blob $blob) {
218
+        if ($blob->has_frontmatter()) {
219 219
             return false;
220 220
         }
221 221
 
222 222
         // only images
223
-        if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) {
223
+        if (strncasecmp($blob->path(), 'images/', strlen('images/')) != 0) {
224 224
             return false;
225 225
         }
226 226
 
@@ -232,16 +232,16 @@  discard block
 block discarded – undo
232 232
      * @param  Writing_On_GitHub_Blob $blob
233 233
      * @param  bool                   $is_remove
234 234
      */
235
-    protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) {
235
+    protected function import_raw_file(Writing_On_GitHub_Blob $blob, $is_remove) {
236 236
         $arr = wp_upload_dir();
237 237
         $path = $arr['basedir'] . '/writing-on-github/' . $blob->path();
238
-        if ( $is_remove ) {
239
-            if ( file_exists($path) ) {
238
+        if ($is_remove) {
239
+            if (file_exists($path)) {
240 240
                 unlink($path);
241 241
             }
242 242
         } else {
243 243
             $dirname = dirname($path);
244
-            if ( ! file_exists($dirname) ) {
244
+            if ( ! file_exists($dirname)) {
245 245
                 wp_mkdir_p($dirname);
246 246
             }
247 247
 
@@ -256,57 +256,57 @@  discard block
 block discarded – undo
256 256
      *
257 257
      * @return Writing_On_GitHub_Post|false
258 258
      */
259
-    protected function blob_to_post( Writing_On_GitHub_Blob $blob ) {
260
-        $args = array( 'post_content' => $blob->content_import() );
259
+    protected function blob_to_post(Writing_On_GitHub_Blob $blob) {
260
+        $args = array('post_content' => $blob->content_import());
261 261
         $meta = $blob->meta();
262 262
 
263 263
         $id = false;
264 264
 
265
-        if ( ! empty( $meta ) ) {
266
-            if ( array_key_exists( 'layout', $meta ) ) {
265
+        if ( ! empty($meta)) {
266
+            if (array_key_exists('layout', $meta)) {
267 267
                 $args['post_type'] = $meta['layout'];
268
-                unset( $meta['layout'] );
268
+                unset($meta['layout']);
269 269
             }
270 270
 
271
-            if ( array_key_exists( 'published', $meta ) ) {
271
+            if (array_key_exists('published', $meta)) {
272 272
                 $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft';
273
-                unset( $meta['published'] );
273
+                unset($meta['published']);
274 274
             }
275 275
 
276
-            if ( array_key_exists( 'post_title', $meta ) ) {
276
+            if (array_key_exists('post_title', $meta)) {
277 277
                 $args['post_title'] = $meta['post_title'];
278
-                unset( $meta['post_title'] );
278
+                unset($meta['post_title']);
279 279
             }
280 280
 
281
-            if ( array_key_exists( 'post_name', $meta ) ) {
281
+            if (array_key_exists('post_name', $meta)) {
282 282
                 $args['post_name'] = $meta['post_name'];
283
-                unset( $meta['post_name'] );
283
+                unset($meta['post_name']);
284 284
             }
285 285
 
286
-            if ( array_key_exists( 'ID', $meta ) ) {
286
+            if (array_key_exists('ID', $meta)) {
287 287
                 $id = $args['ID'] = $meta['ID'];
288 288
                 $blob->set_id($id);
289
-                unset( $meta['ID'] );
289
+                unset($meta['ID']);
290 290
             }
291 291
         }
292 292
 
293 293
         $meta['_wogh_sha'] = $blob->sha();
294 294
 
295
-        if ( $id ) {
296
-            $old_sha = get_post_meta( $id, '_wogh_sha', true );
297
-            $old_github_path = get_post_meta( $id, '_wogh_github_path', true );
295
+        if ($id) {
296
+            $old_sha = get_post_meta($id, '_wogh_sha', true);
297
+            $old_github_path = get_post_meta($id, '_wogh_github_path', true);
298 298
 
299 299
             // dont save post when has same sha
300
-            if ( $old_sha  && $old_sha == $meta['_wogh_sha'] &&
301
-                 $old_github_path && $old_github_path == $blob->path() ) {
300
+            if ($old_sha && $old_sha == $meta['_wogh_sha'] &&
301
+                 $old_github_path && $old_github_path == $blob->path()) {
302 302
                 return false;
303 303
             }
304 304
         }
305 305
 
306
-        $post = new Writing_On_GitHub_Post( $args, $this->app->api() );
307
-        $post->set_old_github_path( $blob->path() );
308
-        $post->set_meta( $meta );
309
-        $blob->set_id( $post->id() );
306
+        $post = new Writing_On_GitHub_Post($args, $this->app->api());
307
+        $post->set_old_github_path($blob->path());
308
+        $post->set_meta($meta);
309
+        $blob->set_id($post->id());
310 310
 
311 311
         return $post;
312 312
     }
Please login to merge, or discard this patch.