@@ -8,10 +8,10 @@ discard block |
||
| 8 | 8 | * @return WP_Error |
| 9 | 9 | */ |
| 10 | 10 | function wogh_append_error( $error, $error2 ) { |
| 11 | - if ( is_wp_error( $error ) ) { |
|
| 12 | - $error->add( $error2->get_error_code(), $error2->get_error_message() ); |
|
| 13 | - } |
|
| 14 | - return $error2; |
|
| 11 | + if ( is_wp_error( $error ) ) { |
|
| 12 | + $error->add( $error2->get_error_code(), $error2->get_error_message() ); |
|
| 13 | + } |
|
| 14 | + return $error2; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | /** |
@@ -21,9 +21,9 @@ discard block |
||
| 21 | 21 | * @return bool |
| 22 | 22 | */ |
| 23 | 23 | function wogh_equal_front_matter( $post, $blob ) { |
| 24 | - $str1 = $post->front_matter(); |
|
| 25 | - $str2 = $blob->front_matter(); |
|
| 26 | - return trim($str1) === trim($str2); |
|
| 24 | + $str1 = $post->front_matter(); |
|
| 25 | + $str2 = $blob->front_matter(); |
|
| 26 | + return trim($str1) === trim($str2); |
|
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | /** |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * @return bool |
| 32 | 32 | */ |
| 33 | 33 | function wogh_is_dont_export_content() { |
| 34 | - return 'yes' === get_option( 'wogh_dont_export_content' ); |
|
| 34 | + return 'yes' === get_option( 'wogh_dont_export_content' ); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /** |
@@ -41,8 +41,8 @@ discard block |
||
| 41 | 41 | * @return string |
| 42 | 42 | */ |
| 43 | 43 | function wogh_git_sha( $contnet ) { |
| 44 | - // $header = "blob $len\0" |
|
| 45 | - // sha1($header . $content) |
|
| 46 | - $len = strlen( $content ); |
|
| 47 | - return sha1( "blob $len\0$content" ); |
|
| 44 | + // $header = "blob $len\0" |
|
| 45 | + // sha1($header . $content) |
|
| 46 | + $len = strlen( $content ); |
|
| 47 | + return sha1( "blob $len\0$content" ); |
|
| 48 | 48 | } |
@@ -7,9 +7,9 @@ discard block |
||
| 7 | 7 | * @param WP_Error $error2 |
| 8 | 8 | * @return WP_Error |
| 9 | 9 | */ |
| 10 | -function wogh_append_error( $error, $error2 ) { |
|
| 11 | - if ( is_wp_error( $error ) ) { |
|
| 12 | - $error->add( $error2->get_error_code(), $error2->get_error_message() ); |
|
| 10 | +function wogh_append_error($error, $error2) { |
|
| 11 | + if (is_wp_error($error)) { |
|
| 12 | + $error->add($error2->get_error_code(), $error2->get_error_message()); |
|
| 13 | 13 | } |
| 14 | 14 | return $error2; |
| 15 | 15 | } |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | * @param Writing_On_GitHub_Blob $blob |
| 21 | 21 | * @return bool |
| 22 | 22 | */ |
| 23 | -function wogh_equal_front_matter( $post, $blob ) { |
|
| 23 | +function wogh_equal_front_matter($post, $blob) { |
|
| 24 | 24 | $str1 = $post->front_matter(); |
| 25 | 25 | $str2 = $blob->front_matter(); |
| 26 | 26 | return trim($str1) === trim($str2); |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * @return bool |
| 32 | 32 | */ |
| 33 | 33 | function wogh_is_dont_export_content() { |
| 34 | - return 'yes' === get_option( 'wogh_dont_export_content' ); |
|
| 34 | + return 'yes' === get_option('wogh_dont_export_content'); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /** |
@@ -40,9 +40,9 @@ discard block |
||
| 40 | 40 | * @param string $contnet |
| 41 | 41 | * @return string |
| 42 | 42 | */ |
| 43 | -function wogh_git_sha( $contnet ) { |
|
| 43 | +function wogh_git_sha($contnet) { |
|
| 44 | 44 | // $header = "blob $len\0" |
| 45 | 45 | // sha1($header . $content) |
| 46 | - $len = strlen( $content ); |
|
| 47 | - return sha1( "blob $len\0$content" ); |
|
| 46 | + $len = strlen($content); |
|
| 47 | + return sha1("blob $len\0$content"); |
|
| 48 | 48 | } |
@@ -10,275 +10,275 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class Writing_On_GitHub_Export { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * Application container. |
|
| 15 | - * |
|
| 16 | - * @var Writing_On_GitHub |
|
| 17 | - */ |
|
| 18 | - protected $app; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Initializes a new export 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 | - * Updates all of the current posts in the database on master. |
|
| 31 | - * |
|
| 32 | - * @param bool $force |
|
| 33 | - * |
|
| 34 | - * @return string|WP_Error |
|
| 35 | - */ |
|
| 36 | - public function full( $force = false ) { |
|
| 37 | - $posts = $this->app->database()->fetch_all_supported( $force ); |
|
| 38 | - |
|
| 39 | - if ( is_wp_error( $posts ) ) { |
|
| 40 | - /* @var WP_Error $posts */ |
|
| 41 | - return $posts; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - $error = ''; |
|
| 45 | - |
|
| 46 | - foreach ( $posts as $post ) { |
|
| 47 | - $result = $this->update( $post->id() ); |
|
| 48 | - if ( is_wp_error( $result ) ) { |
|
| 49 | - /* @var WP_Error $result */ |
|
| 50 | - $error = wogh_append_error( $error, $result ); |
|
| 51 | - } |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - if ( is_wp_error( $error ) ) { |
|
| 55 | - /* @var WP_Error $error */ |
|
| 56 | - return $error; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Check if it exists in github |
|
| 65 | - * @param int $post_id |
|
| 66 | - * @return boolean |
|
| 67 | - */ |
|
| 68 | - protected function github_path( $post_id ) { |
|
| 69 | - $github_path = get_post_meta( $post_id, '_wogh_github_path', true ); |
|
| 70 | - |
|
| 71 | - if ( $github_path && $this->app->api()->fetch()->exists( $github_path ) ) { |
|
| 72 | - return $github_path; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - return false; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Updates the provided post ID in master. |
|
| 80 | - * |
|
| 81 | - * @param int $post_id Post ID to update. |
|
| 82 | - * |
|
| 83 | - * @return string|WP_Error |
|
| 84 | - */ |
|
| 85 | - public function update( $post_id ) { |
|
| 86 | - $post = $this->app->database()->fetch_by_id( $post_id ); |
|
| 87 | - |
|
| 88 | - if ( is_wp_error( $post ) ) { |
|
| 89 | - /* @var WP_Error $post */ |
|
| 90 | - return $post; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - if ( 'trash' === $post->status() ) { |
|
| 94 | - return $this->delete( $post_id ); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - if ( $old_github_path = $this->github_path( $post->id() ) ) { |
|
| 98 | - error_log("old_github_path: $old_github_path"); |
|
| 99 | - $post->set_old_github_path($old_github_path); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - $result = $this->export_post( $post ); |
|
| 103 | - |
|
| 104 | - if ( is_wp_error( $result ) ) { |
|
| 105 | - /* @var WP_Error $result */ |
|
| 106 | - return $result; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Post to blob |
|
| 114 | - * @param Writing_On_GitHub_Post $post |
|
| 115 | - * @return WP_Error|Writing_On_GitHub_Blob |
|
| 116 | - */ |
|
| 117 | - protected function post_to_blob( Writing_On_GitHub_Post $post ) { |
|
| 118 | - if ( ! $post->get_blob() |
|
| 119 | - && $post->old_github_path() |
|
| 120 | - && wogh_is_dont_export_content() ) { |
|
| 121 | - |
|
| 122 | - |
|
| 123 | - $blob = $this->app->api()->fetch()->blob_by_path( $post->old_github_path() ); |
|
| 124 | - |
|
| 125 | - if ( is_wp_error( $blob ) ) { |
|
| 126 | - /** @var WP_Error $blob */ |
|
| 127 | - return $blob; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - $post->set_blob( $blob ); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - return $post->to_blob(); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Export post to github |
|
| 138 | - * @param Writing_On_GitHub_Post $post |
|
| 139 | - * @return WP_Error|true |
|
| 140 | - */ |
|
| 141 | - public function export_post( Writing_On_GitHub_Post $post ) { |
|
| 142 | - // check blob |
|
| 143 | - $blob = $this->post_to_blob( $post ); |
|
| 144 | - if ( is_wp_error( $blob ) ) { |
|
| 145 | - /** @var WP_Error $blob */ |
|
| 146 | - return $blob; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - $result = false; |
|
| 150 | - |
|
| 151 | - $persist = $this->app->api()->persist(); |
|
| 152 | - $github_path = $post->github_path(); |
|
| 153 | - $old_github_path = $post->old_github_path(); |
|
| 154 | - |
|
| 155 | - if ( $old_github_path && $old_github_path != $github_path ) { |
|
| 156 | - // rename |
|
| 157 | - $message = apply_filters( |
|
| 158 | - 'wogh_commit_msg_move_post', |
|
| 159 | - sprintf( |
|
| 160 | - 'Move %s to %s via WordPress at %s (%s)', |
|
| 161 | - $old_github_path, $github_path, |
|
| 162 | - site_url(), |
|
| 163 | - get_bloginfo( 'name' ) |
|
| 164 | - ) |
|
| 165 | - ) . $this->get_commit_msg_tag(); |
|
| 166 | - |
|
| 167 | - $result = $persist->delete_file( $post->old_github_path(), $blob->sha(), $message ); |
|
| 168 | - if ( is_wp_error( $result ) ) { |
|
| 169 | - return $result; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - $result = $persist->create_file( $blob, $message ); |
|
| 173 | - if ( is_wp_error( $result ) ) { |
|
| 174 | - return $result; |
|
| 175 | - } |
|
| 176 | - } elseif ( ! $old_github_path ) { |
|
| 177 | - // create new |
|
| 178 | - $message = apply_filters( |
|
| 179 | - 'wogh_commit_msg_new_post', |
|
| 180 | - sprintf( |
|
| 181 | - 'Create new post %s from WordPress at %s (%s)', |
|
| 182 | - $github_path, |
|
| 183 | - site_url(), |
|
| 184 | - get_bloginfo( 'name' ) |
|
| 185 | - ) |
|
| 186 | - ) . $this->get_commit_msg_tag(); |
|
| 187 | - $result = $persist->create_file( $blob, $message ); |
|
| 188 | - if ( is_wp_error( $result ) ) { |
|
| 189 | - return $result; |
|
| 190 | - } |
|
| 191 | - } elseif ( $old_github_path && $old_github_path == $github_path ) { |
|
| 192 | - // update |
|
| 193 | - $sha = wogh_git_sha( $blob->content() ); |
|
| 194 | - if ( $sha === $blob->sha() ) { |
|
| 195 | - // don't export when has not changed |
|
| 196 | - return true; |
|
| 197 | - } |
|
| 198 | - $message = apply_filters( |
|
| 199 | - 'wogh_commit_msg_update_post', |
|
| 200 | - sprintf( |
|
| 201 | - 'Update post %s from WordPress at %s (%s)', |
|
| 202 | - $github_path, |
|
| 203 | - site_url(), |
|
| 204 | - get_bloginfo( 'name' ) |
|
| 205 | - ) |
|
| 206 | - ) . $this->get_commit_msg_tag(); |
|
| 207 | - $result = $persist->update_file( $blob, $message ); |
|
| 208 | - if ( is_wp_error( $result ) ) { |
|
| 209 | - return $result; |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - $sha = $result->content->sha; |
|
| 214 | - $post->set_sha( $sha ); |
|
| 215 | - $post->set_old_github_path( $github_path ); |
|
| 216 | - |
|
| 217 | - return true; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Deletes a provided post ID from master. |
|
| 222 | - * |
|
| 223 | - * @param int $post_id Post ID to delete. |
|
| 224 | - * |
|
| 225 | - * @return string|WP_Error |
|
| 226 | - */ |
|
| 227 | - public function delete( $post_id ) { |
|
| 228 | - $post = $this->app->database()->fetch_by_id( $post_id ); |
|
| 229 | - |
|
| 230 | - if ( is_wp_error( $post ) ) { |
|
| 231 | - /* @var WP_Error $post */ |
|
| 232 | - return $post; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - $github_path = get_post_meta( $post_id, '_wogh_github_path', true ); |
|
| 236 | - |
|
| 237 | - $message = apply_filters( |
|
| 238 | - 'wogh_commit_msg_delete', |
|
| 239 | - sprintf( |
|
| 240 | - 'Deleting %s via WordPress at %s (%s)', |
|
| 241 | - $github_path, |
|
| 242 | - site_url(), |
|
| 243 | - get_bloginfo( 'name' ) |
|
| 244 | - ), |
|
| 245 | - $post |
|
| 246 | - ) . $this->get_commit_msg_tag(); |
|
| 247 | - |
|
| 248 | - $result = $this->app->api()->persist()->delete_file( $github_path, $post->sha(), $message ); |
|
| 249 | - |
|
| 250 | - if ( is_wp_error( $result ) ) { |
|
| 251 | - /* @var WP_Error $result */ |
|
| 252 | - return $result; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * Saves the export user to the database. |
|
| 261 | - * |
|
| 262 | - * @param int $user_id User ID to export with. |
|
| 263 | - * |
|
| 264 | - * @return bool |
|
| 265 | - */ |
|
| 266 | - public function set_user( $user_id ) { |
|
| 267 | - return update_option( self::EXPORT_USER_OPTION, (int) $user_id ); |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * Gets the commit message tag. |
|
| 272 | - * |
|
| 273 | - * @return string |
|
| 274 | - */ |
|
| 275 | - protected function get_commit_msg_tag() { |
|
| 276 | - $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' ); |
|
| 277 | - |
|
| 278 | - if ( ! $tag ) { |
|
| 279 | - throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - return ' - ' . $tag; |
|
| 283 | - } |
|
| 13 | + /** |
|
| 14 | + * Application container. |
|
| 15 | + * |
|
| 16 | + * @var Writing_On_GitHub |
|
| 17 | + */ |
|
| 18 | + protected $app; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Initializes a new export 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 | + * Updates all of the current posts in the database on master. |
|
| 31 | + * |
|
| 32 | + * @param bool $force |
|
| 33 | + * |
|
| 34 | + * @return string|WP_Error |
|
| 35 | + */ |
|
| 36 | + public function full( $force = false ) { |
|
| 37 | + $posts = $this->app->database()->fetch_all_supported( $force ); |
|
| 38 | + |
|
| 39 | + if ( is_wp_error( $posts ) ) { |
|
| 40 | + /* @var WP_Error $posts */ |
|
| 41 | + return $posts; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + $error = ''; |
|
| 45 | + |
|
| 46 | + foreach ( $posts as $post ) { |
|
| 47 | + $result = $this->update( $post->id() ); |
|
| 48 | + if ( is_wp_error( $result ) ) { |
|
| 49 | + /* @var WP_Error $result */ |
|
| 50 | + $error = wogh_append_error( $error, $result ); |
|
| 51 | + } |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + if ( is_wp_error( $error ) ) { |
|
| 55 | + /* @var WP_Error $error */ |
|
| 56 | + return $error; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Check if it exists in github |
|
| 65 | + * @param int $post_id |
|
| 66 | + * @return boolean |
|
| 67 | + */ |
|
| 68 | + protected function github_path( $post_id ) { |
|
| 69 | + $github_path = get_post_meta( $post_id, '_wogh_github_path', true ); |
|
| 70 | + |
|
| 71 | + if ( $github_path && $this->app->api()->fetch()->exists( $github_path ) ) { |
|
| 72 | + return $github_path; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + return false; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Updates the provided post ID in master. |
|
| 80 | + * |
|
| 81 | + * @param int $post_id Post ID to update. |
|
| 82 | + * |
|
| 83 | + * @return string|WP_Error |
|
| 84 | + */ |
|
| 85 | + public function update( $post_id ) { |
|
| 86 | + $post = $this->app->database()->fetch_by_id( $post_id ); |
|
| 87 | + |
|
| 88 | + if ( is_wp_error( $post ) ) { |
|
| 89 | + /* @var WP_Error $post */ |
|
| 90 | + return $post; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + if ( 'trash' === $post->status() ) { |
|
| 94 | + return $this->delete( $post_id ); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + if ( $old_github_path = $this->github_path( $post->id() ) ) { |
|
| 98 | + error_log("old_github_path: $old_github_path"); |
|
| 99 | + $post->set_old_github_path($old_github_path); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + $result = $this->export_post( $post ); |
|
| 103 | + |
|
| 104 | + if ( is_wp_error( $result ) ) { |
|
| 105 | + /* @var WP_Error $result */ |
|
| 106 | + return $result; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Post to blob |
|
| 114 | + * @param Writing_On_GitHub_Post $post |
|
| 115 | + * @return WP_Error|Writing_On_GitHub_Blob |
|
| 116 | + */ |
|
| 117 | + protected function post_to_blob( Writing_On_GitHub_Post $post ) { |
|
| 118 | + if ( ! $post->get_blob() |
|
| 119 | + && $post->old_github_path() |
|
| 120 | + && wogh_is_dont_export_content() ) { |
|
| 121 | + |
|
| 122 | + |
|
| 123 | + $blob = $this->app->api()->fetch()->blob_by_path( $post->old_github_path() ); |
|
| 124 | + |
|
| 125 | + if ( is_wp_error( $blob ) ) { |
|
| 126 | + /** @var WP_Error $blob */ |
|
| 127 | + return $blob; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + $post->set_blob( $blob ); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + return $post->to_blob(); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Export post to github |
|
| 138 | + * @param Writing_On_GitHub_Post $post |
|
| 139 | + * @return WP_Error|true |
|
| 140 | + */ |
|
| 141 | + public function export_post( Writing_On_GitHub_Post $post ) { |
|
| 142 | + // check blob |
|
| 143 | + $blob = $this->post_to_blob( $post ); |
|
| 144 | + if ( is_wp_error( $blob ) ) { |
|
| 145 | + /** @var WP_Error $blob */ |
|
| 146 | + return $blob; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + $result = false; |
|
| 150 | + |
|
| 151 | + $persist = $this->app->api()->persist(); |
|
| 152 | + $github_path = $post->github_path(); |
|
| 153 | + $old_github_path = $post->old_github_path(); |
|
| 154 | + |
|
| 155 | + if ( $old_github_path && $old_github_path != $github_path ) { |
|
| 156 | + // rename |
|
| 157 | + $message = apply_filters( |
|
| 158 | + 'wogh_commit_msg_move_post', |
|
| 159 | + sprintf( |
|
| 160 | + 'Move %s to %s via WordPress at %s (%s)', |
|
| 161 | + $old_github_path, $github_path, |
|
| 162 | + site_url(), |
|
| 163 | + get_bloginfo( 'name' ) |
|
| 164 | + ) |
|
| 165 | + ) . $this->get_commit_msg_tag(); |
|
| 166 | + |
|
| 167 | + $result = $persist->delete_file( $post->old_github_path(), $blob->sha(), $message ); |
|
| 168 | + if ( is_wp_error( $result ) ) { |
|
| 169 | + return $result; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + $result = $persist->create_file( $blob, $message ); |
|
| 173 | + if ( is_wp_error( $result ) ) { |
|
| 174 | + return $result; |
|
| 175 | + } |
|
| 176 | + } elseif ( ! $old_github_path ) { |
|
| 177 | + // create new |
|
| 178 | + $message = apply_filters( |
|
| 179 | + 'wogh_commit_msg_new_post', |
|
| 180 | + sprintf( |
|
| 181 | + 'Create new post %s from WordPress at %s (%s)', |
|
| 182 | + $github_path, |
|
| 183 | + site_url(), |
|
| 184 | + get_bloginfo( 'name' ) |
|
| 185 | + ) |
|
| 186 | + ) . $this->get_commit_msg_tag(); |
|
| 187 | + $result = $persist->create_file( $blob, $message ); |
|
| 188 | + if ( is_wp_error( $result ) ) { |
|
| 189 | + return $result; |
|
| 190 | + } |
|
| 191 | + } elseif ( $old_github_path && $old_github_path == $github_path ) { |
|
| 192 | + // update |
|
| 193 | + $sha = wogh_git_sha( $blob->content() ); |
|
| 194 | + if ( $sha === $blob->sha() ) { |
|
| 195 | + // don't export when has not changed |
|
| 196 | + return true; |
|
| 197 | + } |
|
| 198 | + $message = apply_filters( |
|
| 199 | + 'wogh_commit_msg_update_post', |
|
| 200 | + sprintf( |
|
| 201 | + 'Update post %s from WordPress at %s (%s)', |
|
| 202 | + $github_path, |
|
| 203 | + site_url(), |
|
| 204 | + get_bloginfo( 'name' ) |
|
| 205 | + ) |
|
| 206 | + ) . $this->get_commit_msg_tag(); |
|
| 207 | + $result = $persist->update_file( $blob, $message ); |
|
| 208 | + if ( is_wp_error( $result ) ) { |
|
| 209 | + return $result; |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + $sha = $result->content->sha; |
|
| 214 | + $post->set_sha( $sha ); |
|
| 215 | + $post->set_old_github_path( $github_path ); |
|
| 216 | + |
|
| 217 | + return true; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Deletes a provided post ID from master. |
|
| 222 | + * |
|
| 223 | + * @param int $post_id Post ID to delete. |
|
| 224 | + * |
|
| 225 | + * @return string|WP_Error |
|
| 226 | + */ |
|
| 227 | + public function delete( $post_id ) { |
|
| 228 | + $post = $this->app->database()->fetch_by_id( $post_id ); |
|
| 229 | + |
|
| 230 | + if ( is_wp_error( $post ) ) { |
|
| 231 | + /* @var WP_Error $post */ |
|
| 232 | + return $post; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + $github_path = get_post_meta( $post_id, '_wogh_github_path', true ); |
|
| 236 | + |
|
| 237 | + $message = apply_filters( |
|
| 238 | + 'wogh_commit_msg_delete', |
|
| 239 | + sprintf( |
|
| 240 | + 'Deleting %s via WordPress at %s (%s)', |
|
| 241 | + $github_path, |
|
| 242 | + site_url(), |
|
| 243 | + get_bloginfo( 'name' ) |
|
| 244 | + ), |
|
| 245 | + $post |
|
| 246 | + ) . $this->get_commit_msg_tag(); |
|
| 247 | + |
|
| 248 | + $result = $this->app->api()->persist()->delete_file( $github_path, $post->sha(), $message ); |
|
| 249 | + |
|
| 250 | + if ( is_wp_error( $result ) ) { |
|
| 251 | + /* @var WP_Error $result */ |
|
| 252 | + return $result; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * Saves the export user to the database. |
|
| 261 | + * |
|
| 262 | + * @param int $user_id User ID to export with. |
|
| 263 | + * |
|
| 264 | + * @return bool |
|
| 265 | + */ |
|
| 266 | + public function set_user( $user_id ) { |
|
| 267 | + return update_option( self::EXPORT_USER_OPTION, (int) $user_id ); |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * Gets the commit message tag. |
|
| 272 | + * |
|
| 273 | + * @return string |
|
| 274 | + */ |
|
| 275 | + protected function get_commit_msg_tag() { |
|
| 276 | + $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' ); |
|
| 277 | + |
|
| 278 | + if ( ! $tag ) { |
|
| 279 | + throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + return ' - ' . $tag; |
|
| 283 | + } |
|
| 284 | 284 | } |
@@ -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 | |
@@ -33,30 +33,30 @@ discard block |
||
| 33 | 33 | * |
| 34 | 34 | * @return string|WP_Error |
| 35 | 35 | */ |
| 36 | - public function full( $force = false ) { |
|
| 37 | - $posts = $this->app->database()->fetch_all_supported( $force ); |
|
| 36 | + public function full($force = false) { |
|
| 37 | + $posts = $this->app->database()->fetch_all_supported($force); |
|
| 38 | 38 | |
| 39 | - if ( is_wp_error( $posts ) ) { |
|
| 39 | + if (is_wp_error($posts)) { |
|
| 40 | 40 | /* @var WP_Error $posts */ |
| 41 | 41 | return $posts; |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | $error = ''; |
| 45 | 45 | |
| 46 | - foreach ( $posts as $post ) { |
|
| 47 | - $result = $this->update( $post->id() ); |
|
| 48 | - if ( is_wp_error( $result ) ) { |
|
| 46 | + foreach ($posts as $post) { |
|
| 47 | + $result = $this->update($post->id()); |
|
| 48 | + if (is_wp_error($result)) { |
|
| 49 | 49 | /* @var WP_Error $result */ |
| 50 | - $error = wogh_append_error( $error, $result ); |
|
| 50 | + $error = wogh_append_error($error, $result); |
|
| 51 | 51 | } |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | - if ( is_wp_error( $error ) ) { |
|
| 54 | + if (is_wp_error($error)) { |
|
| 55 | 55 | /* @var WP_Error $error */ |
| 56 | 56 | return $error; |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | - return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
| 59 | + return __('Export to GitHub completed successfully.', 'writing-on-github'); |
|
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | |
@@ -65,10 +65,10 @@ discard block |
||
| 65 | 65 | * @param int $post_id |
| 66 | 66 | * @return boolean |
| 67 | 67 | */ |
| 68 | - protected function github_path( $post_id ) { |
|
| 69 | - $github_path = get_post_meta( $post_id, '_wogh_github_path', true ); |
|
| 68 | + protected function github_path($post_id) { |
|
| 69 | + $github_path = get_post_meta($post_id, '_wogh_github_path', true); |
|
| 70 | 70 | |
| 71 | - if ( $github_path && $this->app->api()->fetch()->exists( $github_path ) ) { |
|
| 71 | + if ($github_path && $this->app->api()->fetch()->exists($github_path)) { |
|
| 72 | 72 | return $github_path; |
| 73 | 73 | } |
| 74 | 74 | |
@@ -82,31 +82,31 @@ discard block |
||
| 82 | 82 | * |
| 83 | 83 | * @return string|WP_Error |
| 84 | 84 | */ |
| 85 | - public function update( $post_id ) { |
|
| 86 | - $post = $this->app->database()->fetch_by_id( $post_id ); |
|
| 85 | + public function update($post_id) { |
|
| 86 | + $post = $this->app->database()->fetch_by_id($post_id); |
|
| 87 | 87 | |
| 88 | - if ( is_wp_error( $post ) ) { |
|
| 88 | + if (is_wp_error($post)) { |
|
| 89 | 89 | /* @var WP_Error $post */ |
| 90 | 90 | return $post; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - if ( 'trash' === $post->status() ) { |
|
| 94 | - return $this->delete( $post_id ); |
|
| 93 | + if ('trash' === $post->status()) { |
|
| 94 | + return $this->delete($post_id); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - if ( $old_github_path = $this->github_path( $post->id() ) ) { |
|
| 97 | + if ($old_github_path = $this->github_path($post->id())) { |
|
| 98 | 98 | error_log("old_github_path: $old_github_path"); |
| 99 | 99 | $post->set_old_github_path($old_github_path); |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | - $result = $this->export_post( $post ); |
|
| 102 | + $result = $this->export_post($post); |
|
| 103 | 103 | |
| 104 | - if ( is_wp_error( $result ) ) { |
|
| 104 | + if (is_wp_error($result)) { |
|
| 105 | 105 | /* @var WP_Error $result */ |
| 106 | 106 | return $result; |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
| 109 | + return __('Export to GitHub completed successfully.', 'writing-on-github'); |
|
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
@@ -114,20 +114,20 @@ discard block |
||
| 114 | 114 | * @param Writing_On_GitHub_Post $post |
| 115 | 115 | * @return WP_Error|Writing_On_GitHub_Blob |
| 116 | 116 | */ |
| 117 | - protected function post_to_blob( Writing_On_GitHub_Post $post ) { |
|
| 117 | + protected function post_to_blob(Writing_On_GitHub_Post $post) { |
|
| 118 | 118 | if ( ! $post->get_blob() |
| 119 | 119 | && $post->old_github_path() |
| 120 | - && wogh_is_dont_export_content() ) { |
|
| 120 | + && wogh_is_dont_export_content()) { |
|
| 121 | 121 | |
| 122 | 122 | |
| 123 | - $blob = $this->app->api()->fetch()->blob_by_path( $post->old_github_path() ); |
|
| 123 | + $blob = $this->app->api()->fetch()->blob_by_path($post->old_github_path()); |
|
| 124 | 124 | |
| 125 | - if ( is_wp_error( $blob ) ) { |
|
| 125 | + if (is_wp_error($blob)) { |
|
| 126 | 126 | /** @var WP_Error $blob */ |
| 127 | 127 | return $blob; |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | - $post->set_blob( $blob ); |
|
| 130 | + $post->set_blob($blob); |
|
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | return $post->to_blob(); |
@@ -138,10 +138,10 @@ discard block |
||
| 138 | 138 | * @param Writing_On_GitHub_Post $post |
| 139 | 139 | * @return WP_Error|true |
| 140 | 140 | */ |
| 141 | - public function export_post( Writing_On_GitHub_Post $post ) { |
|
| 141 | + public function export_post(Writing_On_GitHub_Post $post) { |
|
| 142 | 142 | // check blob |
| 143 | - $blob = $this->post_to_blob( $post ); |
|
| 144 | - if ( is_wp_error( $blob ) ) { |
|
| 143 | + $blob = $this->post_to_blob($post); |
|
| 144 | + if (is_wp_error($blob)) { |
|
| 145 | 145 | /** @var WP_Error $blob */ |
| 146 | 146 | return $blob; |
| 147 | 147 | } |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | $github_path = $post->github_path(); |
| 153 | 153 | $old_github_path = $post->old_github_path(); |
| 154 | 154 | |
| 155 | - if ( $old_github_path && $old_github_path != $github_path ) { |
|
| 155 | + if ($old_github_path && $old_github_path != $github_path) { |
|
| 156 | 156 | // rename |
| 157 | 157 | $message = apply_filters( |
| 158 | 158 | 'wogh_commit_msg_move_post', |
@@ -160,20 +160,20 @@ discard block |
||
| 160 | 160 | 'Move %s to %s via WordPress at %s (%s)', |
| 161 | 161 | $old_github_path, $github_path, |
| 162 | 162 | site_url(), |
| 163 | - get_bloginfo( 'name' ) |
|
| 163 | + get_bloginfo('name') |
|
| 164 | 164 | ) |
| 165 | 165 | ) . $this->get_commit_msg_tag(); |
| 166 | 166 | |
| 167 | - $result = $persist->delete_file( $post->old_github_path(), $blob->sha(), $message ); |
|
| 168 | - if ( is_wp_error( $result ) ) { |
|
| 167 | + $result = $persist->delete_file($post->old_github_path(), $blob->sha(), $message); |
|
| 168 | + if (is_wp_error($result)) { |
|
| 169 | 169 | return $result; |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | - $result = $persist->create_file( $blob, $message ); |
|
| 173 | - if ( is_wp_error( $result ) ) { |
|
| 172 | + $result = $persist->create_file($blob, $message); |
|
| 173 | + if (is_wp_error($result)) { |
|
| 174 | 174 | return $result; |
| 175 | 175 | } |
| 176 | - } elseif ( ! $old_github_path ) { |
|
| 176 | + } elseif ( ! $old_github_path) { |
|
| 177 | 177 | // create new |
| 178 | 178 | $message = apply_filters( |
| 179 | 179 | 'wogh_commit_msg_new_post', |
@@ -181,17 +181,17 @@ discard block |
||
| 181 | 181 | 'Create new post %s from WordPress at %s (%s)', |
| 182 | 182 | $github_path, |
| 183 | 183 | site_url(), |
| 184 | - get_bloginfo( 'name' ) |
|
| 184 | + get_bloginfo('name') |
|
| 185 | 185 | ) |
| 186 | 186 | ) . $this->get_commit_msg_tag(); |
| 187 | - $result = $persist->create_file( $blob, $message ); |
|
| 188 | - if ( is_wp_error( $result ) ) { |
|
| 187 | + $result = $persist->create_file($blob, $message); |
|
| 188 | + if (is_wp_error($result)) { |
|
| 189 | 189 | return $result; |
| 190 | 190 | } |
| 191 | - } elseif ( $old_github_path && $old_github_path == $github_path ) { |
|
| 191 | + } elseif ($old_github_path && $old_github_path == $github_path) { |
|
| 192 | 192 | // update |
| 193 | - $sha = wogh_git_sha( $blob->content() ); |
|
| 194 | - if ( $sha === $blob->sha() ) { |
|
| 193 | + $sha = wogh_git_sha($blob->content()); |
|
| 194 | + if ($sha === $blob->sha()) { |
|
| 195 | 195 | // don't export when has not changed |
| 196 | 196 | return true; |
| 197 | 197 | } |
@@ -201,18 +201,18 @@ discard block |
||
| 201 | 201 | 'Update post %s from WordPress at %s (%s)', |
| 202 | 202 | $github_path, |
| 203 | 203 | site_url(), |
| 204 | - get_bloginfo( 'name' ) |
|
| 204 | + get_bloginfo('name') |
|
| 205 | 205 | ) |
| 206 | 206 | ) . $this->get_commit_msg_tag(); |
| 207 | - $result = $persist->update_file( $blob, $message ); |
|
| 208 | - if ( is_wp_error( $result ) ) { |
|
| 207 | + $result = $persist->update_file($blob, $message); |
|
| 208 | + if (is_wp_error($result)) { |
|
| 209 | 209 | return $result; |
| 210 | 210 | } |
| 211 | 211 | } |
| 212 | 212 | |
| 213 | 213 | $sha = $result->content->sha; |
| 214 | - $post->set_sha( $sha ); |
|
| 215 | - $post->set_old_github_path( $github_path ); |
|
| 214 | + $post->set_sha($sha); |
|
| 215 | + $post->set_old_github_path($github_path); |
|
| 216 | 216 | |
| 217 | 217 | return true; |
| 218 | 218 | } |
@@ -224,15 +224,15 @@ discard block |
||
| 224 | 224 | * |
| 225 | 225 | * @return string|WP_Error |
| 226 | 226 | */ |
| 227 | - public function delete( $post_id ) { |
|
| 228 | - $post = $this->app->database()->fetch_by_id( $post_id ); |
|
| 227 | + public function delete($post_id) { |
|
| 228 | + $post = $this->app->database()->fetch_by_id($post_id); |
|
| 229 | 229 | |
| 230 | - if ( is_wp_error( $post ) ) { |
|
| 230 | + if (is_wp_error($post)) { |
|
| 231 | 231 | /* @var WP_Error $post */ |
| 232 | 232 | return $post; |
| 233 | 233 | } |
| 234 | 234 | |
| 235 | - $github_path = get_post_meta( $post_id, '_wogh_github_path', true ); |
|
| 235 | + $github_path = get_post_meta($post_id, '_wogh_github_path', true); |
|
| 236 | 236 | |
| 237 | 237 | $message = apply_filters( |
| 238 | 238 | 'wogh_commit_msg_delete', |
@@ -240,19 +240,19 @@ discard block |
||
| 240 | 240 | 'Deleting %s via WordPress at %s (%s)', |
| 241 | 241 | $github_path, |
| 242 | 242 | site_url(), |
| 243 | - get_bloginfo( 'name' ) |
|
| 243 | + get_bloginfo('name') |
|
| 244 | 244 | ), |
| 245 | 245 | $post |
| 246 | 246 | ) . $this->get_commit_msg_tag(); |
| 247 | 247 | |
| 248 | - $result = $this->app->api()->persist()->delete_file( $github_path, $post->sha(), $message ); |
|
| 248 | + $result = $this->app->api()->persist()->delete_file($github_path, $post->sha(), $message); |
|
| 249 | 249 | |
| 250 | - if ( is_wp_error( $result ) ) { |
|
| 250 | + if (is_wp_error($result)) { |
|
| 251 | 251 | /* @var WP_Error $result */ |
| 252 | 252 | return $result; |
| 253 | 253 | } |
| 254 | 254 | |
| 255 | - return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
| 255 | + return __('Export to GitHub completed successfully.', 'writing-on-github'); |
|
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | |
@@ -263,8 +263,8 @@ discard block |
||
| 263 | 263 | * |
| 264 | 264 | * @return bool |
| 265 | 265 | */ |
| 266 | - public function set_user( $user_id ) { |
|
| 267 | - return update_option( self::EXPORT_USER_OPTION, (int) $user_id ); |
|
| 266 | + public function set_user($user_id) { |
|
| 267 | + return update_option(self::EXPORT_USER_OPTION, (int) $user_id); |
|
| 268 | 268 | } |
| 269 | 269 | |
| 270 | 270 | /** |
@@ -273,10 +273,10 @@ discard block |
||
| 273 | 273 | * @return string |
| 274 | 274 | */ |
| 275 | 275 | protected function get_commit_msg_tag() { |
| 276 | - $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' ); |
|
| 276 | + $tag = apply_filters('wogh_commit_msg_tag', 'wogh'); |
|
| 277 | 277 | |
| 278 | - if ( ! $tag ) { |
|
| 279 | - throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
|
| 278 | + if ( ! $tag) { |
|
| 279 | + throw new Exception(__('Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github')); |
|
| 280 | 280 | } |
| 281 | 281 | |
| 282 | 282 | return ' - ' . $tag; |