@@ -26,24 +26,24 @@ discard block |
||
| 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() ); |
| 38 | 38 | |
| 39 | 39 | if ( is_wp_error( $result ) ) { |
| 40 | - /* @var WP_Error $result */ |
|
| 40 | + /* @var WP_Error $result */ |
|
| 41 | 41 | return $result; |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - if ( is_array( $result ) ) { |
|
| 45 | - $result = $this->import_files( $result ); |
|
| 46 | - } |
|
| 44 | + if ( is_array( $result ) ) { |
|
| 45 | + $result = $this->import_files( $result ); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | 48 | if ( is_wp_error( $result ) ) { |
| 49 | 49 | return $files; |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | /** |
| 56 | 56 | * import blob by files |
| 57 | 57 | * @param Writing_On_GitHub_File_Info[] $files |
| 58 | - * |
|
| 58 | + * |
|
| 59 | 59 | * @return string|WP_Error |
| 60 | 60 | */ |
| 61 | 61 | protected function import_files( $files ) { |
@@ -94,32 +94,32 @@ discard block |
||
| 94 | 94 | $result = $this->app->api()->fetch()->tree_recursive(); |
| 95 | 95 | |
| 96 | 96 | if ( is_wp_error( $result ) ) { |
| 97 | - /* @var WP_Error $result */ |
|
| 97 | + /* @var WP_Error $result */ |
|
| 98 | 98 | return $result; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | - if ( is_array( $result ) ) { |
|
| 102 | - $result = $this->import_files( $result ); |
|
| 103 | - } |
|
| 101 | + if ( is_array( $result ) ) { |
|
| 102 | + $result = $this->import_files( $result ); |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | 105 | if ( is_wp_error( $result ) ) { |
| 106 | - /* @var WP_Error $result */ |
|
| 106 | + /* @var WP_Error $result */ |
|
| 107 | 107 | return $result; |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | return __( 'Payload processed', 'writing-on-github' ); |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * Do compare |
|
| 115 | - * @param Writing_On_GitHub_File_Info[]|WP_Error $files |
|
| 116 | - * @param int[] &$delete_ids |
|
| 117 | - * |
|
| 118 | - * @return string|WP_Error |
|
| 119 | - */ |
|
| 113 | + /** |
|
| 114 | + * Do compare |
|
| 115 | + * @param Writing_On_GitHub_File_Info[]|WP_Error $files |
|
| 116 | + * @param int[] &$delete_ids |
|
| 117 | + * |
|
| 118 | + * @return string|WP_Error |
|
| 119 | + */ |
|
| 120 | 120 | protected function compare( $files, &$delete_ids ) { |
| 121 | 121 | if ( is_wp_error( $files ) ) { |
| 122 | - /* @var WP_Error $files */ |
|
| 122 | + /* @var WP_Error $files */ |
|
| 123 | 123 | return $files; |
| 124 | 124 | } |
| 125 | 125 | |
@@ -22,7 +22,7 @@ discard block |
||
| 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 |
||
| 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,23 +58,23 @@ discard block |
||
| 58 | 58 | * |
| 59 | 59 | * @return string|WP_Error |
| 60 | 60 | */ |
| 61 | - protected function import_files( $files ) { |
|
| 61 | + protected function import_files($files) { |
|
| 62 | 62 | |
| 63 | - $error = false; |
|
| 63 | + $error = false; |
|
| 64 | 64 | $delete_ids = false; |
| 65 | 65 | |
| 66 | - $result = $this->compare( $files, $delete_ids ); |
|
| 66 | + $result = $this->compare($files, $delete_ids); |
|
| 67 | 67 | |
| 68 | - if ( is_wp_error( $result ) ) { |
|
| 68 | + if (is_wp_error($result)) { |
|
| 69 | 69 | return $result; |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | - if ( $delete_ids ) { |
|
| 72 | + if ($delete_ids) { |
|
| 73 | 73 | foreach ($delete_ids as $id) { |
| 74 | - $result = $this->app->database()->delete_post( $id ); |
|
| 75 | - if ( is_wp_error( $result ) ) { |
|
| 76 | - if ( $error ) { |
|
| 77 | - $error->add( $result->get_error_code(), $result->get_error_message() ); |
|
| 74 | + $result = $this->app->database()->delete_post($id); |
|
| 75 | + if (is_wp_error($result)) { |
|
| 76 | + if ($error) { |
|
| 77 | + $error->add($result->get_error_code(), $result->get_error_message()); |
|
| 78 | 78 | } else { |
| 79 | 79 | $error = $result; |
| 80 | 80 | } |
@@ -93,21 +93,21 @@ discard block |
||
| 93 | 93 | public function master() { |
| 94 | 94 | $result = $this->app->api()->fetch()->tree_recursive(); |
| 95 | 95 | |
| 96 | - if ( is_wp_error( $result ) ) { |
|
| 96 | + if (is_wp_error($result)) { |
|
| 97 | 97 | /* @var WP_Error $result */ |
| 98 | 98 | return $result; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | - if ( is_array( $result ) ) { |
|
| 102 | - $result = $this->import_files( $result ); |
|
| 101 | + if (is_array($result)) { |
|
| 102 | + $result = $this->import_files($result); |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - if ( is_wp_error( $result ) ) { |
|
| 105 | + if (is_wp_error($result)) { |
|
| 106 | 106 | /* @var WP_Error $result */ |
| 107 | 107 | return $result; |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | - return __( 'Payload processed', 'writing-on-github' ); |
|
| 110 | + return __('Payload processed', 'writing-on-github'); |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | /** |
@@ -117,8 +117,8 @@ discard block |
||
| 117 | 117 | * |
| 118 | 118 | * @return string|WP_Error |
| 119 | 119 | */ |
| 120 | - protected function compare( $files, &$delete_ids ) { |
|
| 121 | - if ( is_wp_error( $files ) ) { |
|
| 120 | + protected function compare($files, &$delete_ids) { |
|
| 121 | + if (is_wp_error($files)) { |
|
| 122 | 122 | /* @var WP_Error $files */ |
| 123 | 123 | return $files; |
| 124 | 124 | } |
@@ -128,62 +128,62 @@ discard block |
||
| 128 | 128 | |
| 129 | 129 | $idsmap = array(); |
| 130 | 130 | |
| 131 | - foreach ( $files as $file ) { |
|
| 132 | - if ( ! $this->importable_file( $file ) ) { |
|
| 131 | + foreach ($files as $file) { |
|
| 132 | + if ( ! $this->importable_file($file)) { |
|
| 133 | 133 | continue; |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | - $blob = $this->app->api()->fetch()->blob( $file ); |
|
| 136 | + $blob = $this->app->api()->fetch()->blob($file); |
|
| 137 | 137 | // network error ? |
| 138 | - if ( ! $blob instanceof Writing_On_GitHub_Blob ) { |
|
| 138 | + if ( ! $blob instanceof Writing_On_GitHub_Blob) { |
|
| 139 | 139 | continue; |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | - if ( $this->importable_raw_file( $blob ) ) { |
|
| 143 | - $this->import_raw_file( $blob, $file->status == 'removed' ); |
|
| 142 | + if ($this->importable_raw_file($blob)) { |
|
| 143 | + $this->import_raw_file($blob, $file->status == 'removed'); |
|
| 144 | 144 | continue; |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | - if ( ! $this->importable_blob( $blob ) ) { |
|
| 147 | + if ( ! $this->importable_blob($blob)) { |
|
| 148 | 148 | continue; |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | - $post = $this->blob_to_post( $blob ); |
|
| 151 | + $post = $this->blob_to_post($blob); |
|
| 152 | 152 | |
| 153 | - if ( $file->status == 'removed' ) { |
|
| 154 | - if ( $blob->id() ) { |
|
| 153 | + if ($file->status == 'removed') { |
|
| 154 | + if ($blob->id()) { |
|
| 155 | 155 | $idsmap[$blob->id()] = true; |
| 156 | 156 | } |
| 157 | - } elseif ( $post != false ) { |
|
| 157 | + } elseif ($post != false) { |
|
| 158 | 158 | $posts[] = $post; |
| 159 | - if ( $post->is_new() ) { |
|
| 159 | + if ($post->is_new()) { |
|
| 160 | 160 | $new[] = $post; |
| 161 | 161 | } |
| 162 | 162 | } |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | - foreach ( $posts as $post ) { |
|
| 166 | - if ( $post->id() && isset( $idsmap[ $post->id() ] ) ) { |
|
| 167 | - unset( $idsmap[ $post->id() ] ); |
|
| 165 | + foreach ($posts as $post) { |
|
| 166 | + if ($post->id() && isset($idsmap[$post->id()])) { |
|
| 167 | + unset($idsmap[$post->id()]); |
|
| 168 | 168 | } |
| 169 | 169 | } |
| 170 | 170 | $delete_ids = array(); |
| 171 | - foreach ( $idsmap as $id => $value ) { |
|
| 171 | + foreach ($idsmap as $id => $value) { |
|
| 172 | 172 | $delete_ids[] = $id; |
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | // $this->app->database()->save_posts( $posts, $commit->author_email() ); |
| 176 | 176 | |
| 177 | - $result = $this->app->database()->save_posts( $posts ); |
|
| 177 | + $result = $this->app->database()->save_posts($posts); |
|
| 178 | 178 | |
| 179 | - if ( is_wp_error( $result ) ) { |
|
| 179 | + if (is_wp_error($result)) { |
|
| 180 | 180 | return $result; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | - if ( ! empty( $new ) ) { |
|
| 184 | - $result = $this->app->export()->new_posts( $new ); |
|
| 183 | + if ( ! empty($new)) { |
|
| 184 | + $result = $this->app->export()->new_posts($new); |
|
| 185 | 185 | |
| 186 | - if ( is_wp_error( $result ) ) { |
|
| 186 | + if (is_wp_error($result)) { |
|
| 187 | 187 | return $result; |
| 188 | 188 | } |
| 189 | 189 | } |
@@ -198,12 +198,12 @@ discard block |
||
| 198 | 198 | * |
| 199 | 199 | * @return bool |
| 200 | 200 | */ |
| 201 | - protected function importable_file( Writing_On_GitHub_File_Info $file ) { |
|
| 201 | + protected function importable_file(Writing_On_GitHub_File_Info $file) { |
|
| 202 | 202 | |
| 203 | 203 | // only _pages and _posts |
| 204 | - if ( strncasecmp($file->path, '_pages/', strlen('_pages/') ) != 0 && |
|
| 205 | - strncasecmp($file->path, '_posts/', strlen('_posts/') ) != 0 && |
|
| 206 | - strncasecmp($file->path, 'images/', strlen('images/') ) != 0 ) { |
|
| 204 | + if (strncasecmp($file->path, '_pages/', strlen('_pages/')) != 0 && |
|
| 205 | + strncasecmp($file->path, '_posts/', strlen('_posts/')) != 0 && |
|
| 206 | + strncasecmp($file->path, 'images/', strlen('images/')) != 0) { |
|
| 207 | 207 | return false; |
| 208 | 208 | } |
| 209 | 209 | |
@@ -222,7 +222,7 @@ discard block |
||
| 222 | 222 | * |
| 223 | 223 | * @return bool |
| 224 | 224 | */ |
| 225 | - protected function importable_blob( Writing_On_GitHub_Blob $blob ) { |
|
| 225 | + protected function importable_blob(Writing_On_GitHub_Blob $blob) { |
|
| 226 | 226 | // global $wpdb; |
| 227 | 227 | |
| 228 | 228 | // // Skip the repo's readme. |
@@ -235,20 +235,20 @@ discard block |
||
| 235 | 235 | // return false; |
| 236 | 236 | // } |
| 237 | 237 | |
| 238 | - if ( ! $blob->has_frontmatter() ) { |
|
| 238 | + if ( ! $blob->has_frontmatter()) { |
|
| 239 | 239 | return false; |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | return true; |
| 243 | 243 | } |
| 244 | 244 | |
| 245 | - protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { |
|
| 246 | - if ( $blob->has_frontmatter() ) { |
|
| 245 | + protected function importable_raw_file(Writing_On_GitHub_Blob $blob) { |
|
| 246 | + if ($blob->has_frontmatter()) { |
|
| 247 | 247 | return false; |
| 248 | 248 | } |
| 249 | 249 | |
| 250 | 250 | // only images |
| 251 | - if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) { |
|
| 251 | + if (strncasecmp($blob->path(), 'images/', strlen('images/')) != 0) { |
|
| 252 | 252 | return false; |
| 253 | 253 | } |
| 254 | 254 | |
@@ -260,16 +260,16 @@ discard block |
||
| 260 | 260 | * @param Writing_On_GitHub_Blob $blob |
| 261 | 261 | * @param bool $is_remove |
| 262 | 262 | */ |
| 263 | - protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { |
|
| 263 | + protected function import_raw_file(Writing_On_GitHub_Blob $blob, $is_remove) { |
|
| 264 | 264 | $arr = wp_upload_dir(); |
| 265 | 265 | $path = $arr['basedir'] . '/writing-on-github/' . $blob->path(); |
| 266 | - if ( $is_remove ) { |
|
| 267 | - if ( file_exists($path) ) { |
|
| 266 | + if ($is_remove) { |
|
| 267 | + if (file_exists($path)) { |
|
| 268 | 268 | unlink($path); |
| 269 | 269 | } |
| 270 | 270 | } else { |
| 271 | 271 | $dirname = dirname($path); |
| 272 | - if ( ! file_exists($dirname) ) { |
|
| 272 | + if ( ! file_exists($dirname)) { |
|
| 273 | 273 | wp_mkdir_p($dirname); |
| 274 | 274 | } |
| 275 | 275 | |
@@ -284,57 +284,57 @@ discard block |
||
| 284 | 284 | * |
| 285 | 285 | * @return Writing_On_GitHub_Post|false |
| 286 | 286 | */ |
| 287 | - protected function blob_to_post( Writing_On_GitHub_Blob $blob ) { |
|
| 288 | - $args = array( 'post_content' => $blob->content_import() ); |
|
| 287 | + protected function blob_to_post(Writing_On_GitHub_Blob $blob) { |
|
| 288 | + $args = array('post_content' => $blob->content_import()); |
|
| 289 | 289 | $meta = $blob->meta(); |
| 290 | 290 | |
| 291 | 291 | $id = false; |
| 292 | 292 | |
| 293 | - if ( ! empty( $meta ) ) { |
|
| 294 | - if ( array_key_exists( 'layout', $meta ) ) { |
|
| 293 | + if ( ! empty($meta)) { |
|
| 294 | + if (array_key_exists('layout', $meta)) { |
|
| 295 | 295 | $args['post_type'] = $meta['layout']; |
| 296 | - unset( $meta['layout'] ); |
|
| 296 | + unset($meta['layout']); |
|
| 297 | 297 | } |
| 298 | 298 | |
| 299 | - if ( array_key_exists( 'published', $meta ) ) { |
|
| 299 | + if (array_key_exists('published', $meta)) { |
|
| 300 | 300 | $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; |
| 301 | - unset( $meta['published'] ); |
|
| 301 | + unset($meta['published']); |
|
| 302 | 302 | } |
| 303 | 303 | |
| 304 | - if ( array_key_exists( 'post_title', $meta ) ) { |
|
| 304 | + if (array_key_exists('post_title', $meta)) { |
|
| 305 | 305 | $args['post_title'] = $meta['post_title']; |
| 306 | - unset( $meta['post_title'] ); |
|
| 306 | + unset($meta['post_title']); |
|
| 307 | 307 | } |
| 308 | 308 | |
| 309 | - if ( array_key_exists( 'post_name', $meta ) ) { |
|
| 309 | + if (array_key_exists('post_name', $meta)) { |
|
| 310 | 310 | $args['post_name'] = $meta['post_name']; |
| 311 | - unset( $meta['post_name'] ); |
|
| 311 | + unset($meta['post_name']); |
|
| 312 | 312 | } |
| 313 | 313 | |
| 314 | - if ( array_key_exists( 'ID', $meta ) ) { |
|
| 314 | + if (array_key_exists('ID', $meta)) { |
|
| 315 | 315 | $id = $args['ID'] = $meta['ID']; |
| 316 | 316 | $blob->set_id($id); |
| 317 | - unset( $meta['ID'] ); |
|
| 317 | + unset($meta['ID']); |
|
| 318 | 318 | } |
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | $meta['_wogh_sha'] = $blob->sha(); |
| 322 | 322 | |
| 323 | - if ( $id ) { |
|
| 324 | - $old_sha = get_post_meta( $id, '_wogh_sha', true ); |
|
| 325 | - $old_github_path = get_post_meta( $id, '_wogh_github_path', true ); |
|
| 323 | + if ($id) { |
|
| 324 | + $old_sha = get_post_meta($id, '_wogh_sha', true); |
|
| 325 | + $old_github_path = get_post_meta($id, '_wogh_github_path', true); |
|
| 326 | 326 | |
| 327 | 327 | // dont save post when has same sha |
| 328 | - if ( $old_sha && $old_sha == $meta['_wogh_sha'] && |
|
| 329 | - $old_github_path && $old_github_path == $blob->path() ) { |
|
| 328 | + if ($old_sha && $old_sha == $meta['_wogh_sha'] && |
|
| 329 | + $old_github_path && $old_github_path == $blob->path()) { |
|
| 330 | 330 | return false; |
| 331 | 331 | } |
| 332 | 332 | } |
| 333 | 333 | |
| 334 | - $post = new Writing_On_GitHub_Post( $args, $this->app->api() ); |
|
| 335 | - $post->set_old_github_path( $blob->path() ); |
|
| 336 | - $post->set_meta( $meta ); |
|
| 337 | - $blob->set_id( $post->id() ); |
|
| 334 | + $post = new Writing_On_GitHub_Post($args, $this->app->api()); |
|
| 335 | + $post->set_old_github_path($blob->path()); |
|
| 336 | + $post->set_meta($meta); |
|
| 337 | + $blob->set_id($post->id()); |
|
| 338 | 338 | |
| 339 | 339 | return $post; |
| 340 | 340 | } |