@@ -10,329 +10,329 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class Writing_On_GitHub_Import { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * Application container. |
|
| 15 | - * |
|
| 16 | - * @var Writing_On_GitHub |
|
| 17 | - */ |
|
| 18 | - protected $app; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Initializes a new import manager. |
|
| 22 | - * |
|
| 23 | - * @param Writing_On_GitHub $app Application container. |
|
| 24 | - */ |
|
| 25 | - public function __construct( Writing_On_GitHub $app ) { |
|
| 26 | - $this->app = $app; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Imports a payload. |
|
| 31 | - * @param Writing_On_GitHub_Payload $payload |
|
| 32 | - * |
|
| 33 | - * @return string|WP_Error |
|
| 34 | - */ |
|
| 35 | - public function payload( Writing_On_GitHub_Payload $payload ) { |
|
| 36 | - |
|
| 37 | - $result = $this->app->api()->fetch()->compare( $payload->get_before_commit_id() ); |
|
| 38 | - |
|
| 39 | - if ( is_wp_error( $result ) ) { |
|
| 40 | - /* @var WP_Error $result */ |
|
| 41 | - return $result; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - if ( is_array( $result ) ) { |
|
| 45 | - $result = $this->import_files( $result ); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - if ( is_wp_error( $result ) ) { |
|
| 49 | - return $files; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - return __( 'Payload processed', 'writing-on-github' ); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * import blob by files |
|
| 57 | - * @param Writing_On_GitHub_File_Info[] $files |
|
| 58 | - * |
|
| 59 | - * @return string|WP_Error |
|
| 60 | - */ |
|
| 61 | - protected function import_files( $files ) { |
|
| 62 | - |
|
| 63 | - $error = false; |
|
| 64 | - $delete_ids = false; |
|
| 65 | - |
|
| 66 | - $result = $this->compare( $files, $delete_ids ); |
|
| 67 | - |
|
| 68 | - if ( is_wp_error( $result ) ) { |
|
| 69 | - return $result; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - if ( $delete_ids ) { |
|
| 73 | - foreach ($delete_ids as $id) { |
|
| 74 | - $result = $this->app->database()->delete_post( $id ); |
|
| 75 | - if ( is_wp_error( $result ) ) { |
|
| 76 | - /* @var WP_Error $result */ |
|
| 77 | - $error = wogh_append_error( $error, $result ); |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - return $error; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Imports the latest commit on the master branch. |
|
| 87 | - * |
|
| 88 | - * @return string|WP_Error |
|
| 89 | - */ |
|
| 90 | - public function master() { |
|
| 91 | - $result = $this->app->api()->fetch()->tree_recursive(); |
|
| 92 | - |
|
| 93 | - if ( is_wp_error( $result ) ) { |
|
| 94 | - /* @var WP_Error $result */ |
|
| 95 | - return $result; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - if ( is_array( $result ) ) { |
|
| 99 | - $result = $this->import_files( $result ); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - if ( is_wp_error( $result ) ) { |
|
| 103 | - /* @var WP_Error $result */ |
|
| 104 | - return $result; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - return __( 'Payload processed', 'writing-on-github' ); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Do compare |
|
| 112 | - * @param Writing_On_GitHub_File_Info[]|WP_Error $files |
|
| 113 | - * @param int[] &$delete_ids |
|
| 114 | - * |
|
| 115 | - * @return string|WP_Error |
|
| 116 | - */ |
|
| 117 | - protected function compare( $files, &$delete_ids ) { |
|
| 118 | - if ( is_wp_error( $files ) ) { |
|
| 119 | - /* @var WP_Error $files */ |
|
| 120 | - return $files; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - $posts = array(); |
|
| 124 | - $new = array(); |
|
| 125 | - |
|
| 126 | - $idsmap = array(); |
|
| 127 | - |
|
| 128 | - foreach ( $files as $file ) { |
|
| 129 | - if ( ! $this->importable_file( $file ) ) { |
|
| 130 | - continue; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - $blob = $this->app->api()->fetch()->blob( $file ); |
|
| 134 | - // network error ? |
|
| 135 | - if ( ! $blob instanceof Writing_On_GitHub_Blob ) { |
|
| 136 | - continue; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - if ( $this->importable_raw_file( $blob ) ) { |
|
| 140 | - $this->import_raw_file( $blob, $file->status == 'removed' ); |
|
| 141 | - continue; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - if ( ! $this->importable_blob( $blob ) ) { |
|
| 145 | - continue; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - $post = $this->blob_to_post( $blob ); |
|
| 149 | - |
|
| 150 | - if ( $file->status == 'removed' ) { |
|
| 151 | - if ( $blob->id() ) { |
|
| 152 | - $idsmap[$blob->id()] = true; |
|
| 153 | - } |
|
| 154 | - } elseif ( $post != false ) { |
|
| 155 | - $posts[] = $post; |
|
| 156 | - if ( $post->is_new() ) { |
|
| 157 | - $new[] = $post; |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - foreach ( $posts as $post ) { |
|
| 163 | - if ( $post->id() && isset( $idsmap[ $post->id() ] ) ) { |
|
| 164 | - unset( $idsmap[ $post->id() ] ); |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - $delete_ids = array(); |
|
| 168 | - foreach ( $idsmap as $id => $value ) { |
|
| 169 | - $delete_ids[] = $id; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - // $this->app->database()->save_posts( $posts, $commit->author_email() ); |
|
| 173 | - |
|
| 174 | - $result = $this->app->database()->save_posts( $posts ); |
|
| 175 | - |
|
| 176 | - if ( is_wp_error( $result ) ) { |
|
| 177 | - return $result; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - if ( ! empty( $new ) ) { |
|
| 181 | - $result = $this->app->export()->new_posts( $new ); |
|
| 182 | - |
|
| 183 | - if ( is_wp_error( $result ) ) { |
|
| 184 | - return $result; |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - return $posts; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Checks whether the provided blob should be imported. |
|
| 193 | - * |
|
| 194 | - * @param Writing_On_GitHub_File_Info $file |
|
| 195 | - * |
|
| 196 | - * @return bool |
|
| 197 | - */ |
|
| 198 | - protected function importable_file( Writing_On_GitHub_File_Info $file ) { |
|
| 199 | - |
|
| 200 | - // only _pages and _posts |
|
| 201 | - if ( strncasecmp($file->path, '_pages/', strlen('_pages/') ) != 0 && |
|
| 202 | - strncasecmp($file->path, '_posts/', strlen('_posts/') ) != 0 && |
|
| 203 | - strncasecmp($file->path, 'images/', strlen('images/') ) != 0 ) { |
|
| 204 | - return false; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - |
|
| 208 | - // if ( ! $file->has_frontmatter() ) { |
|
| 209 | - // return false; |
|
| 210 | - // } |
|
| 211 | - |
|
| 212 | - return true; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * Checks whether the provided blob should be imported. |
|
| 217 | - * |
|
| 218 | - * @param Writing_On_GitHub_Blob $blob Blob to validate. |
|
| 219 | - * |
|
| 220 | - * @return bool |
|
| 221 | - */ |
|
| 222 | - protected function importable_blob( Writing_On_GitHub_Blob $blob ) { |
|
| 223 | - // global $wpdb; |
|
| 224 | - |
|
| 225 | - // // Skip the repo's readme. |
|
| 226 | - // if ( 'readme' === strtolower( substr( $blob->path(), 0, 6 ) ) ) { |
|
| 227 | - // return false; |
|
| 228 | - // } |
|
| 229 | - |
|
| 230 | - // // If the blob sha already matches a post, then move on. |
|
| 231 | - // if ( ! is_wp_error( $this->app->database()->fetch_by_sha( $blob->sha() ) ) ) { |
|
| 232 | - // return false; |
|
| 233 | - // } |
|
| 234 | - |
|
| 235 | - if ( ! $blob->has_frontmatter() ) { |
|
| 236 | - return false; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - return true; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { |
|
| 243 | - if ( $blob->has_frontmatter() ) { |
|
| 244 | - return false; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - // only images |
|
| 248 | - if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) { |
|
| 249 | - return false; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - return true; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Imports a raw file content into file system. |
|
| 257 | - * @param Writing_On_GitHub_Blob $blob |
|
| 258 | - * @param bool $is_remove |
|
| 259 | - */ |
|
| 260 | - protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { |
|
| 261 | - $arr = wp_upload_dir(); |
|
| 262 | - $path = $arr['basedir'] . '/writing-on-github/' . $blob->path(); |
|
| 263 | - if ( $is_remove ) { |
|
| 264 | - if ( file_exists($path) ) { |
|
| 265 | - unlink($path); |
|
| 266 | - } |
|
| 267 | - } else { |
|
| 268 | - $dirname = dirname($path); |
|
| 269 | - if ( ! file_exists($dirname) ) { |
|
| 270 | - wp_mkdir_p($dirname); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - file_put_contents($path, $blob->content()); |
|
| 274 | - } |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Imports a single blob content into matching post. |
|
| 279 | - * |
|
| 280 | - * @param Writing_On_GitHub_Blob $blob Blob to transform into a Post. |
|
| 281 | - * |
|
| 282 | - * @return Writing_On_GitHub_Post|false |
|
| 283 | - */ |
|
| 284 | - protected function blob_to_post( Writing_On_GitHub_Blob $blob ) { |
|
| 285 | - $args = array( 'post_content' => $blob->content_import() ); |
|
| 286 | - $meta = $blob->meta(); |
|
| 287 | - |
|
| 288 | - $id = false; |
|
| 289 | - |
|
| 290 | - if ( ! empty( $meta ) ) { |
|
| 291 | - if ( array_key_exists( 'layout', $meta ) ) { |
|
| 292 | - $args['post_type'] = $meta['layout']; |
|
| 293 | - unset( $meta['layout'] ); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - if ( array_key_exists( 'published', $meta ) ) { |
|
| 297 | - $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; |
|
| 298 | - unset( $meta['published'] ); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - if ( array_key_exists( 'post_title', $meta ) ) { |
|
| 302 | - $args['post_title'] = $meta['post_title']; |
|
| 303 | - unset( $meta['post_title'] ); |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - if ( array_key_exists( 'post_name', $meta ) ) { |
|
| 307 | - $args['post_name'] = $meta['post_name']; |
|
| 308 | - unset( $meta['post_name'] ); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - if ( array_key_exists( 'ID', $meta ) ) { |
|
| 312 | - $id = $args['ID'] = $meta['ID']; |
|
| 313 | - $blob->set_id($id); |
|
| 314 | - unset( $meta['ID'] ); |
|
| 315 | - } |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - $meta['_wogh_sha'] = $blob->sha(); |
|
| 319 | - |
|
| 320 | - if ( $id ) { |
|
| 321 | - $old_sha = get_post_meta( $id, '_wogh_sha', true ); |
|
| 322 | - $old_github_path = get_post_meta( $id, '_wogh_github_path', true ); |
|
| 323 | - |
|
| 324 | - // dont save post when has same sha |
|
| 325 | - if ( $old_sha && $old_sha == $meta['_wogh_sha'] && |
|
| 326 | - $old_github_path && $old_github_path == $blob->path() ) { |
|
| 327 | - return false; |
|
| 328 | - } |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - $post = new Writing_On_GitHub_Post( $args, $this->app->api() ); |
|
| 332 | - $post->set_old_github_path( $blob->path() ); |
|
| 333 | - $post->set_meta( $meta ); |
|
| 334 | - $blob->set_id( $post->id() ); |
|
| 335 | - |
|
| 336 | - return $post; |
|
| 337 | - } |
|
| 13 | + /** |
|
| 14 | + * Application container. |
|
| 15 | + * |
|
| 16 | + * @var Writing_On_GitHub |
|
| 17 | + */ |
|
| 18 | + protected $app; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Initializes a new import manager. |
|
| 22 | + * |
|
| 23 | + * @param Writing_On_GitHub $app Application container. |
|
| 24 | + */ |
|
| 25 | + public function __construct( Writing_On_GitHub $app ) { |
|
| 26 | + $this->app = $app; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Imports a payload. |
|
| 31 | + * @param Writing_On_GitHub_Payload $payload |
|
| 32 | + * |
|
| 33 | + * @return string|WP_Error |
|
| 34 | + */ |
|
| 35 | + public function payload( Writing_On_GitHub_Payload $payload ) { |
|
| 36 | + |
|
| 37 | + $result = $this->app->api()->fetch()->compare( $payload->get_before_commit_id() ); |
|
| 38 | + |
|
| 39 | + if ( is_wp_error( $result ) ) { |
|
| 40 | + /* @var WP_Error $result */ |
|
| 41 | + return $result; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + if ( is_array( $result ) ) { |
|
| 45 | + $result = $this->import_files( $result ); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + if ( is_wp_error( $result ) ) { |
|
| 49 | + return $files; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + return __( 'Payload processed', 'writing-on-github' ); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * import blob by files |
|
| 57 | + * @param Writing_On_GitHub_File_Info[] $files |
|
| 58 | + * |
|
| 59 | + * @return string|WP_Error |
|
| 60 | + */ |
|
| 61 | + protected function import_files( $files ) { |
|
| 62 | + |
|
| 63 | + $error = false; |
|
| 64 | + $delete_ids = false; |
|
| 65 | + |
|
| 66 | + $result = $this->compare( $files, $delete_ids ); |
|
| 67 | + |
|
| 68 | + if ( is_wp_error( $result ) ) { |
|
| 69 | + return $result; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + if ( $delete_ids ) { |
|
| 73 | + foreach ($delete_ids as $id) { |
|
| 74 | + $result = $this->app->database()->delete_post( $id ); |
|
| 75 | + if ( is_wp_error( $result ) ) { |
|
| 76 | + /* @var WP_Error $result */ |
|
| 77 | + $error = wogh_append_error( $error, $result ); |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + return $error; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Imports the latest commit on the master branch. |
|
| 87 | + * |
|
| 88 | + * @return string|WP_Error |
|
| 89 | + */ |
|
| 90 | + public function master() { |
|
| 91 | + $result = $this->app->api()->fetch()->tree_recursive(); |
|
| 92 | + |
|
| 93 | + if ( is_wp_error( $result ) ) { |
|
| 94 | + /* @var WP_Error $result */ |
|
| 95 | + return $result; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + if ( is_array( $result ) ) { |
|
| 99 | + $result = $this->import_files( $result ); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + if ( is_wp_error( $result ) ) { |
|
| 103 | + /* @var WP_Error $result */ |
|
| 104 | + return $result; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + return __( 'Payload processed', 'writing-on-github' ); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Do compare |
|
| 112 | + * @param Writing_On_GitHub_File_Info[]|WP_Error $files |
|
| 113 | + * @param int[] &$delete_ids |
|
| 114 | + * |
|
| 115 | + * @return string|WP_Error |
|
| 116 | + */ |
|
| 117 | + protected function compare( $files, &$delete_ids ) { |
|
| 118 | + if ( is_wp_error( $files ) ) { |
|
| 119 | + /* @var WP_Error $files */ |
|
| 120 | + return $files; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + $posts = array(); |
|
| 124 | + $new = array(); |
|
| 125 | + |
|
| 126 | + $idsmap = array(); |
|
| 127 | + |
|
| 128 | + foreach ( $files as $file ) { |
|
| 129 | + if ( ! $this->importable_file( $file ) ) { |
|
| 130 | + continue; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + $blob = $this->app->api()->fetch()->blob( $file ); |
|
| 134 | + // network error ? |
|
| 135 | + if ( ! $blob instanceof Writing_On_GitHub_Blob ) { |
|
| 136 | + continue; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + if ( $this->importable_raw_file( $blob ) ) { |
|
| 140 | + $this->import_raw_file( $blob, $file->status == 'removed' ); |
|
| 141 | + continue; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + if ( ! $this->importable_blob( $blob ) ) { |
|
| 145 | + continue; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + $post = $this->blob_to_post( $blob ); |
|
| 149 | + |
|
| 150 | + if ( $file->status == 'removed' ) { |
|
| 151 | + if ( $blob->id() ) { |
|
| 152 | + $idsmap[$blob->id()] = true; |
|
| 153 | + } |
|
| 154 | + } elseif ( $post != false ) { |
|
| 155 | + $posts[] = $post; |
|
| 156 | + if ( $post->is_new() ) { |
|
| 157 | + $new[] = $post; |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + foreach ( $posts as $post ) { |
|
| 163 | + if ( $post->id() && isset( $idsmap[ $post->id() ] ) ) { |
|
| 164 | + unset( $idsmap[ $post->id() ] ); |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + $delete_ids = array(); |
|
| 168 | + foreach ( $idsmap as $id => $value ) { |
|
| 169 | + $delete_ids[] = $id; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + // $this->app->database()->save_posts( $posts, $commit->author_email() ); |
|
| 173 | + |
|
| 174 | + $result = $this->app->database()->save_posts( $posts ); |
|
| 175 | + |
|
| 176 | + if ( is_wp_error( $result ) ) { |
|
| 177 | + return $result; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + if ( ! empty( $new ) ) { |
|
| 181 | + $result = $this->app->export()->new_posts( $new ); |
|
| 182 | + |
|
| 183 | + if ( is_wp_error( $result ) ) { |
|
| 184 | + return $result; |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + return $posts; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Checks whether the provided blob should be imported. |
|
| 193 | + * |
|
| 194 | + * @param Writing_On_GitHub_File_Info $file |
|
| 195 | + * |
|
| 196 | + * @return bool |
|
| 197 | + */ |
|
| 198 | + protected function importable_file( Writing_On_GitHub_File_Info $file ) { |
|
| 199 | + |
|
| 200 | + // only _pages and _posts |
|
| 201 | + if ( strncasecmp($file->path, '_pages/', strlen('_pages/') ) != 0 && |
|
| 202 | + strncasecmp($file->path, '_posts/', strlen('_posts/') ) != 0 && |
|
| 203 | + strncasecmp($file->path, 'images/', strlen('images/') ) != 0 ) { |
|
| 204 | + return false; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + |
|
| 208 | + // if ( ! $file->has_frontmatter() ) { |
|
| 209 | + // return false; |
|
| 210 | + // } |
|
| 211 | + |
|
| 212 | + return true; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * Checks whether the provided blob should be imported. |
|
| 217 | + * |
|
| 218 | + * @param Writing_On_GitHub_Blob $blob Blob to validate. |
|
| 219 | + * |
|
| 220 | + * @return bool |
|
| 221 | + */ |
|
| 222 | + protected function importable_blob( Writing_On_GitHub_Blob $blob ) { |
|
| 223 | + // global $wpdb; |
|
| 224 | + |
|
| 225 | + // // Skip the repo's readme. |
|
| 226 | + // if ( 'readme' === strtolower( substr( $blob->path(), 0, 6 ) ) ) { |
|
| 227 | + // return false; |
|
| 228 | + // } |
|
| 229 | + |
|
| 230 | + // // If the blob sha already matches a post, then move on. |
|
| 231 | + // if ( ! is_wp_error( $this->app->database()->fetch_by_sha( $blob->sha() ) ) ) { |
|
| 232 | + // return false; |
|
| 233 | + // } |
|
| 234 | + |
|
| 235 | + if ( ! $blob->has_frontmatter() ) { |
|
| 236 | + return false; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + return true; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { |
|
| 243 | + if ( $blob->has_frontmatter() ) { |
|
| 244 | + return false; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + // only images |
|
| 248 | + if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) { |
|
| 249 | + return false; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + return true; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Imports a raw file content into file system. |
|
| 257 | + * @param Writing_On_GitHub_Blob $blob |
|
| 258 | + * @param bool $is_remove |
|
| 259 | + */ |
|
| 260 | + protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { |
|
| 261 | + $arr = wp_upload_dir(); |
|
| 262 | + $path = $arr['basedir'] . '/writing-on-github/' . $blob->path(); |
|
| 263 | + if ( $is_remove ) { |
|
| 264 | + if ( file_exists($path) ) { |
|
| 265 | + unlink($path); |
|
| 266 | + } |
|
| 267 | + } else { |
|
| 268 | + $dirname = dirname($path); |
|
| 269 | + if ( ! file_exists($dirname) ) { |
|
| 270 | + wp_mkdir_p($dirname); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + file_put_contents($path, $blob->content()); |
|
| 274 | + } |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Imports a single blob content into matching post. |
|
| 279 | + * |
|
| 280 | + * @param Writing_On_GitHub_Blob $blob Blob to transform into a Post. |
|
| 281 | + * |
|
| 282 | + * @return Writing_On_GitHub_Post|false |
|
| 283 | + */ |
|
| 284 | + protected function blob_to_post( Writing_On_GitHub_Blob $blob ) { |
|
| 285 | + $args = array( 'post_content' => $blob->content_import() ); |
|
| 286 | + $meta = $blob->meta(); |
|
| 287 | + |
|
| 288 | + $id = false; |
|
| 289 | + |
|
| 290 | + if ( ! empty( $meta ) ) { |
|
| 291 | + if ( array_key_exists( 'layout', $meta ) ) { |
|
| 292 | + $args['post_type'] = $meta['layout']; |
|
| 293 | + unset( $meta['layout'] ); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + if ( array_key_exists( 'published', $meta ) ) { |
|
| 297 | + $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; |
|
| 298 | + unset( $meta['published'] ); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + if ( array_key_exists( 'post_title', $meta ) ) { |
|
| 302 | + $args['post_title'] = $meta['post_title']; |
|
| 303 | + unset( $meta['post_title'] ); |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + if ( array_key_exists( 'post_name', $meta ) ) { |
|
| 307 | + $args['post_name'] = $meta['post_name']; |
|
| 308 | + unset( $meta['post_name'] ); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + if ( array_key_exists( 'ID', $meta ) ) { |
|
| 312 | + $id = $args['ID'] = $meta['ID']; |
|
| 313 | + $blob->set_id($id); |
|
| 314 | + unset( $meta['ID'] ); |
|
| 315 | + } |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + $meta['_wogh_sha'] = $blob->sha(); |
|
| 319 | + |
|
| 320 | + if ( $id ) { |
|
| 321 | + $old_sha = get_post_meta( $id, '_wogh_sha', true ); |
|
| 322 | + $old_github_path = get_post_meta( $id, '_wogh_github_path', true ); |
|
| 323 | + |
|
| 324 | + // dont save post when has same sha |
|
| 325 | + if ( $old_sha && $old_sha == $meta['_wogh_sha'] && |
|
| 326 | + $old_github_path && $old_github_path == $blob->path() ) { |
|
| 327 | + return false; |
|
| 328 | + } |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + $post = new Writing_On_GitHub_Post( $args, $this->app->api() ); |
|
| 332 | + $post->set_old_github_path( $blob->path() ); |
|
| 333 | + $post->set_meta( $meta ); |
|
| 334 | + $blob->set_id( $post->id() ); |
|
| 335 | + |
|
| 336 | + return $post; |
|
| 337 | + } |
|
| 338 | 338 | } |
@@ -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 | 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 ) ) { |
|
| 74 | + $result = $this->app->database()->delete_post($id); |
|
| 75 | + if (is_wp_error($result)) { |
|
| 76 | 76 | /* @var WP_Error $result */ |
| 77 | - $error = wogh_append_error( $error, $result ); |
|
| 77 | + $error = wogh_append_error($error, $result); |
|
| 78 | 78 | } |
| 79 | 79 | } |
| 80 | 80 | } |
@@ -90,21 +90,21 @@ discard block |
||
| 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 | /* @var WP_Error $result */ |
| 95 | 95 | return $result; |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - if ( is_array( $result ) ) { |
|
| 99 | - $result = $this->import_files( $result ); |
|
| 98 | + if (is_array($result)) { |
|
| 99 | + $result = $this->import_files($result); |
|
| 100 | 100 | } |
| 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 | - return __( 'Payload processed', 'writing-on-github' ); |
|
| 107 | + return __('Payload processed', 'writing-on-github'); |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | /** |
@@ -114,8 +114,8 @@ discard block |
||
| 114 | 114 | * |
| 115 | 115 | * @return string|WP_Error |
| 116 | 116 | */ |
| 117 | - protected function compare( $files, &$delete_ids ) { |
|
| 118 | - if ( is_wp_error( $files ) ) { |
|
| 117 | + protected function compare($files, &$delete_ids) { |
|
| 118 | + if (is_wp_error($files)) { |
|
| 119 | 119 | /* @var WP_Error $files */ |
| 120 | 120 | return $files; |
| 121 | 121 | } |
@@ -125,62 +125,62 @@ discard block |
||
| 125 | 125 | |
| 126 | 126 | $idsmap = array(); |
| 127 | 127 | |
| 128 | - foreach ( $files as $file ) { |
|
| 129 | - if ( ! $this->importable_file( $file ) ) { |
|
| 128 | + foreach ($files as $file) { |
|
| 129 | + if ( ! $this->importable_file($file)) { |
|
| 130 | 130 | continue; |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | - $blob = $this->app->api()->fetch()->blob( $file ); |
|
| 133 | + $blob = $this->app->api()->fetch()->blob($file); |
|
| 134 | 134 | // network error ? |
| 135 | - if ( ! $blob instanceof Writing_On_GitHub_Blob ) { |
|
| 135 | + if ( ! $blob instanceof Writing_On_GitHub_Blob) { |
|
| 136 | 136 | continue; |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | - if ( $this->importable_raw_file( $blob ) ) { |
|
| 140 | - $this->import_raw_file( $blob, $file->status == 'removed' ); |
|
| 139 | + if ($this->importable_raw_file($blob)) { |
|
| 140 | + $this->import_raw_file($blob, $file->status == 'removed'); |
|
| 141 | 141 | continue; |
| 142 | 142 | } |
| 143 | 143 | |
| 144 | - if ( ! $this->importable_blob( $blob ) ) { |
|
| 144 | + if ( ! $this->importable_blob($blob)) { |
|
| 145 | 145 | continue; |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | - $post = $this->blob_to_post( $blob ); |
|
| 148 | + $post = $this->blob_to_post($blob); |
|
| 149 | 149 | |
| 150 | - if ( $file->status == 'removed' ) { |
|
| 151 | - if ( $blob->id() ) { |
|
| 150 | + if ($file->status == 'removed') { |
|
| 151 | + if ($blob->id()) { |
|
| 152 | 152 | $idsmap[$blob->id()] = true; |
| 153 | 153 | } |
| 154 | - } elseif ( $post != false ) { |
|
| 154 | + } elseif ($post != false) { |
|
| 155 | 155 | $posts[] = $post; |
| 156 | - if ( $post->is_new() ) { |
|
| 156 | + if ($post->is_new()) { |
|
| 157 | 157 | $new[] = $post; |
| 158 | 158 | } |
| 159 | 159 | } |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | - foreach ( $posts as $post ) { |
|
| 163 | - if ( $post->id() && isset( $idsmap[ $post->id() ] ) ) { |
|
| 164 | - unset( $idsmap[ $post->id() ] ); |
|
| 162 | + foreach ($posts as $post) { |
|
| 163 | + if ($post->id() && isset($idsmap[$post->id()])) { |
|
| 164 | + unset($idsmap[$post->id()]); |
|
| 165 | 165 | } |
| 166 | 166 | } |
| 167 | 167 | $delete_ids = array(); |
| 168 | - foreach ( $idsmap as $id => $value ) { |
|
| 168 | + foreach ($idsmap as $id => $value) { |
|
| 169 | 169 | $delete_ids[] = $id; |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | // $this->app->database()->save_posts( $posts, $commit->author_email() ); |
| 173 | 173 | |
| 174 | - $result = $this->app->database()->save_posts( $posts ); |
|
| 174 | + $result = $this->app->database()->save_posts($posts); |
|
| 175 | 175 | |
| 176 | - if ( is_wp_error( $result ) ) { |
|
| 176 | + if (is_wp_error($result)) { |
|
| 177 | 177 | return $result; |
| 178 | 178 | } |
| 179 | 179 | |
| 180 | - if ( ! empty( $new ) ) { |
|
| 181 | - $result = $this->app->export()->new_posts( $new ); |
|
| 180 | + if ( ! empty($new)) { |
|
| 181 | + $result = $this->app->export()->new_posts($new); |
|
| 182 | 182 | |
| 183 | - if ( is_wp_error( $result ) ) { |
|
| 183 | + if (is_wp_error($result)) { |
|
| 184 | 184 | return $result; |
| 185 | 185 | } |
| 186 | 186 | } |
@@ -195,12 +195,12 @@ discard block |
||
| 195 | 195 | * |
| 196 | 196 | * @return bool |
| 197 | 197 | */ |
| 198 | - protected function importable_file( Writing_On_GitHub_File_Info $file ) { |
|
| 198 | + protected function importable_file(Writing_On_GitHub_File_Info $file) { |
|
| 199 | 199 | |
| 200 | 200 | // only _pages and _posts |
| 201 | - if ( strncasecmp($file->path, '_pages/', strlen('_pages/') ) != 0 && |
|
| 202 | - strncasecmp($file->path, '_posts/', strlen('_posts/') ) != 0 && |
|
| 203 | - strncasecmp($file->path, 'images/', strlen('images/') ) != 0 ) { |
|
| 201 | + if (strncasecmp($file->path, '_pages/', strlen('_pages/')) != 0 && |
|
| 202 | + strncasecmp($file->path, '_posts/', strlen('_posts/')) != 0 && |
|
| 203 | + strncasecmp($file->path, 'images/', strlen('images/')) != 0) { |
|
| 204 | 204 | return false; |
| 205 | 205 | } |
| 206 | 206 | |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | * |
| 220 | 220 | * @return bool |
| 221 | 221 | */ |
| 222 | - protected function importable_blob( Writing_On_GitHub_Blob $blob ) { |
|
| 222 | + protected function importable_blob(Writing_On_GitHub_Blob $blob) { |
|
| 223 | 223 | // global $wpdb; |
| 224 | 224 | |
| 225 | 225 | // // Skip the repo's readme. |
@@ -232,20 +232,20 @@ discard block |
||
| 232 | 232 | // return false; |
| 233 | 233 | // } |
| 234 | 234 | |
| 235 | - if ( ! $blob->has_frontmatter() ) { |
|
| 235 | + if ( ! $blob->has_frontmatter()) { |
|
| 236 | 236 | return false; |
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | return true; |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | - protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { |
|
| 243 | - if ( $blob->has_frontmatter() ) { |
|
| 242 | + protected function importable_raw_file(Writing_On_GitHub_Blob $blob) { |
|
| 243 | + if ($blob->has_frontmatter()) { |
|
| 244 | 244 | return false; |
| 245 | 245 | } |
| 246 | 246 | |
| 247 | 247 | // only images |
| 248 | - if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) { |
|
| 248 | + if (strncasecmp($blob->path(), 'images/', strlen('images/')) != 0) { |
|
| 249 | 249 | return false; |
| 250 | 250 | } |
| 251 | 251 | |
@@ -257,16 +257,16 @@ discard block |
||
| 257 | 257 | * @param Writing_On_GitHub_Blob $blob |
| 258 | 258 | * @param bool $is_remove |
| 259 | 259 | */ |
| 260 | - protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { |
|
| 260 | + protected function import_raw_file(Writing_On_GitHub_Blob $blob, $is_remove) { |
|
| 261 | 261 | $arr = wp_upload_dir(); |
| 262 | 262 | $path = $arr['basedir'] . '/writing-on-github/' . $blob->path(); |
| 263 | - if ( $is_remove ) { |
|
| 264 | - if ( file_exists($path) ) { |
|
| 263 | + if ($is_remove) { |
|
| 264 | + if (file_exists($path)) { |
|
| 265 | 265 | unlink($path); |
| 266 | 266 | } |
| 267 | 267 | } else { |
| 268 | 268 | $dirname = dirname($path); |
| 269 | - if ( ! file_exists($dirname) ) { |
|
| 269 | + if ( ! file_exists($dirname)) { |
|
| 270 | 270 | wp_mkdir_p($dirname); |
| 271 | 271 | } |
| 272 | 272 | |
@@ -281,57 +281,57 @@ discard block |
||
| 281 | 281 | * |
| 282 | 282 | * @return Writing_On_GitHub_Post|false |
| 283 | 283 | */ |
| 284 | - protected function blob_to_post( Writing_On_GitHub_Blob $blob ) { |
|
| 285 | - $args = array( 'post_content' => $blob->content_import() ); |
|
| 284 | + protected function blob_to_post(Writing_On_GitHub_Blob $blob) { |
|
| 285 | + $args = array('post_content' => $blob->content_import()); |
|
| 286 | 286 | $meta = $blob->meta(); |
| 287 | 287 | |
| 288 | 288 | $id = false; |
| 289 | 289 | |
| 290 | - if ( ! empty( $meta ) ) { |
|
| 291 | - if ( array_key_exists( 'layout', $meta ) ) { |
|
| 290 | + if ( ! empty($meta)) { |
|
| 291 | + if (array_key_exists('layout', $meta)) { |
|
| 292 | 292 | $args['post_type'] = $meta['layout']; |
| 293 | - unset( $meta['layout'] ); |
|
| 293 | + unset($meta['layout']); |
|
| 294 | 294 | } |
| 295 | 295 | |
| 296 | - if ( array_key_exists( 'published', $meta ) ) { |
|
| 296 | + if (array_key_exists('published', $meta)) { |
|
| 297 | 297 | $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; |
| 298 | - unset( $meta['published'] ); |
|
| 298 | + unset($meta['published']); |
|
| 299 | 299 | } |
| 300 | 300 | |
| 301 | - if ( array_key_exists( 'post_title', $meta ) ) { |
|
| 301 | + if (array_key_exists('post_title', $meta)) { |
|
| 302 | 302 | $args['post_title'] = $meta['post_title']; |
| 303 | - unset( $meta['post_title'] ); |
|
| 303 | + unset($meta['post_title']); |
|
| 304 | 304 | } |
| 305 | 305 | |
| 306 | - if ( array_key_exists( 'post_name', $meta ) ) { |
|
| 306 | + if (array_key_exists('post_name', $meta)) { |
|
| 307 | 307 | $args['post_name'] = $meta['post_name']; |
| 308 | - unset( $meta['post_name'] ); |
|
| 308 | + unset($meta['post_name']); |
|
| 309 | 309 | } |
| 310 | 310 | |
| 311 | - if ( array_key_exists( 'ID', $meta ) ) { |
|
| 311 | + if (array_key_exists('ID', $meta)) { |
|
| 312 | 312 | $id = $args['ID'] = $meta['ID']; |
| 313 | 313 | $blob->set_id($id); |
| 314 | - unset( $meta['ID'] ); |
|
| 314 | + unset($meta['ID']); |
|
| 315 | 315 | } |
| 316 | 316 | } |
| 317 | 317 | |
| 318 | 318 | $meta['_wogh_sha'] = $blob->sha(); |
| 319 | 319 | |
| 320 | - if ( $id ) { |
|
| 321 | - $old_sha = get_post_meta( $id, '_wogh_sha', true ); |
|
| 322 | - $old_github_path = get_post_meta( $id, '_wogh_github_path', true ); |
|
| 320 | + if ($id) { |
|
| 321 | + $old_sha = get_post_meta($id, '_wogh_sha', true); |
|
| 322 | + $old_github_path = get_post_meta($id, '_wogh_github_path', true); |
|
| 323 | 323 | |
| 324 | 324 | // dont save post when has same sha |
| 325 | - if ( $old_sha && $old_sha == $meta['_wogh_sha'] && |
|
| 326 | - $old_github_path && $old_github_path == $blob->path() ) { |
|
| 325 | + if ($old_sha && $old_sha == $meta['_wogh_sha'] && |
|
| 326 | + $old_github_path && $old_github_path == $blob->path()) { |
|
| 327 | 327 | return false; |
| 328 | 328 | } |
| 329 | 329 | } |
| 330 | 330 | |
| 331 | - $post = new Writing_On_GitHub_Post( $args, $this->app->api() ); |
|
| 332 | - $post->set_old_github_path( $blob->path() ); |
|
| 333 | - $post->set_meta( $meta ); |
|
| 334 | - $blob->set_id( $post->id() ); |
|
| 331 | + $post = new Writing_On_GitHub_Post($args, $this->app->api()); |
|
| 332 | + $post->set_old_github_path($blob->path()); |
|
| 333 | + $post->set_meta($meta); |
|
| 334 | + $blob->set_id($post->id()); |
|
| 335 | 335 | |
| 336 | 336 | return $post; |
| 337 | 337 | } |