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