litefeel /
writing-on-github
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * GitHub Export Manager. |
||
| 4 | * |
||
| 5 | * @package Writing_On_GitHub |
||
| 6 | */ |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Class Writing_On_GitHub_Export |
||
| 10 | */ |
||
| 11 | class Writing_On_GitHub_Export { |
||
| 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 = false; |
||
| 45 | |||
| 46 | View Code Duplication | foreach ( $posts as $post ) { |
|
| 47 | $result = $this->update( $post->id() ); |
||
| 48 | if ( is_wp_error( $result ) ) { |
||
| 49 | if ( $error ) { |
||
| 50 | $error->add( $result->get_error_code(), $result->get_error_message() ); |
||
| 51 | } else { |
||
| 52 | $error = $result; |
||
| 53 | } |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | if ( is_wp_error( $error ) ) { |
||
| 58 | return $error; |
||
| 59 | } |
||
| 60 | |||
| 61 | return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
||
| 62 | } |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * Check if it exists in github |
||
| 67 | * @param int $post_id |
||
| 68 | * @return boolean |
||
| 69 | */ |
||
| 70 | protected function github_path( $post_id ) { |
||
| 71 | $github_path = get_post_meta( $post_id, '_wogh_github_path', true ); |
||
| 72 | |||
| 73 | if ( $github_path && $this->app->api()->fetch()->exists( $github_path ) ) { |
||
| 74 | return $github_path; |
||
| 75 | } |
||
| 76 | |||
| 77 | return false; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Updates the provided post ID in master. |
||
| 82 | * |
||
| 83 | * @param int $post_id Post ID to update. |
||
| 84 | * |
||
| 85 | * @return string|WP_Error |
||
| 86 | */ |
||
| 87 | public function update( $post_id ) { |
||
| 88 | $post = $this->app->database()->fetch_by_id( $post_id ); |
||
| 89 | |||
| 90 | if ( is_wp_error( $post ) ) { |
||
| 91 | /* @var WP_Error $post */ |
||
| 92 | return $post; |
||
| 93 | } |
||
| 94 | |||
| 95 | if ( 'trash' === $post->status() ) { |
||
| 96 | return $this->delete( $post_id ); |
||
| 97 | } |
||
| 98 | |||
| 99 | if ( $old_github_path = $this->github_path( $post->id() ) ) { |
||
| 100 | error_log("old_github_path: $old_github_path"); |
||
| 101 | $post->set_old_github_path($old_github_path); |
||
| 102 | } |
||
| 103 | |||
| 104 | $result = $this->new_posts( array( $post ) ); |
||
| 105 | |||
| 106 | if ( is_wp_error( $result ) ) { |
||
| 107 | /* @var WP_Error $result */ |
||
| 108 | return $result; |
||
| 109 | } |
||
| 110 | |||
| 111 | return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Updates GitHub-created posts with latest WordPress data. |
||
| 116 | * |
||
| 117 | * @param Writing_On_GitHub_Post[] $posts Array of Posts to create. |
||
| 118 | * |
||
| 119 | * @return string|WP_Error |
||
| 120 | */ |
||
| 121 | public function new_posts( array $posts ) { |
||
| 122 | $error = false; |
||
| 123 | |||
| 124 | $persist = $this->app->api()->persist(); |
||
| 125 | |||
| 126 | View Code Duplication | foreach ( $posts as $post ) { |
|
| 127 | $result = $this->new_post( $post, $persist ); |
||
| 128 | if ( is_wp_error( $result ) ) { |
||
| 129 | if ( $error ) { |
||
| 130 | $error->add( $result->get_error_code(), $result->get_error_message() ); |
||
| 131 | } else { |
||
| 132 | $error = $result; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | if ( is_wp_error( $error ) ) { |
||
| 138 | return $error; |
||
| 139 | } |
||
| 140 | |||
| 141 | return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
||
| 142 | } |
||
| 143 | |||
| 144 | protected function new_post( $post, $persist ) { |
||
|
0 ignored issues
–
show
|
|||
| 145 | $github_path = $post->github_path(); |
||
| 146 | $old_github_path = $post->old_github_path(); |
||
| 147 | $blob = $post->to_blob(); |
||
| 148 | $result = false; |
||
| 149 | |||
| 150 | if ( $old_github_path && $old_github_path != $github_path ) { |
||
| 151 | // rename |
||
| 152 | $message = apply_filters( |
||
| 153 | 'wogh_commit_msg_move_post', |
||
| 154 | sprintf( |
||
| 155 | 'Move %s to %s via WordPress at %s (%s)', |
||
| 156 | $old_github_path, $github_path, |
||
| 157 | site_url(), |
||
| 158 | get_bloginfo( 'name' ) |
||
| 159 | ) |
||
| 160 | ) . $this->get_commit_msg_tag(); |
||
| 161 | |||
| 162 | $result = $persist->delete_file( $post->old_github_path(), $blob->sha(), $message ); |
||
| 163 | if ( is_wp_error( $result ) ) { |
||
| 164 | return $result; |
||
| 165 | } |
||
| 166 | |||
| 167 | $result = $persist->create_file( $blob, $message ); |
||
| 168 | if ( is_wp_error( $result ) ) { |
||
| 169 | return $result; |
||
| 170 | } |
||
| 171 | } elseif ( ! $old_github_path ) { |
||
| 172 | // create new |
||
| 173 | $message = apply_filters( |
||
| 174 | 'wogh_commit_msg_new_post', |
||
| 175 | sprintf( |
||
| 176 | 'Create new post %s from WordPress at %s (%s)', |
||
| 177 | $github_path, |
||
| 178 | site_url(), |
||
| 179 | get_bloginfo( 'name' ) |
||
| 180 | ) |
||
| 181 | ) . $this->get_commit_msg_tag(); |
||
| 182 | $result = $persist->create_file( $blob, $message ); |
||
| 183 | if ( is_wp_error( $result ) ) { |
||
| 184 | return $result; |
||
| 185 | } |
||
| 186 | } elseif ( $old_github_path && $old_github_path == $github_path ) { |
||
| 187 | // update |
||
| 188 | $message = apply_filters( |
||
| 189 | 'wogh_commit_msg_update_post', |
||
| 190 | sprintf( |
||
| 191 | 'Update post %s from WordPress at %s (%s)', |
||
| 192 | $github_path, |
||
| 193 | site_url(), |
||
| 194 | get_bloginfo( 'name' ) |
||
| 195 | ) |
||
| 196 | ) . $this->get_commit_msg_tag(); |
||
| 197 | $result = $persist->update_file( $blob, $message ); |
||
| 198 | if ( is_wp_error( $result ) ) { |
||
| 199 | return $result; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | $sha = $result->content->sha; |
||
| 204 | $post->set_sha($sha); |
||
| 205 | $post->set_old_github_path($github_path); |
||
| 206 | |||
| 207 | return true; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Deletes a provided post ID from master. |
||
| 212 | * |
||
| 213 | * @param int $post_id Post ID to delete. |
||
| 214 | * |
||
| 215 | * @return string|WP_Error |
||
| 216 | */ |
||
| 217 | public function delete( $post_id ) { |
||
| 218 | $post = $this->app->database()->fetch_by_id( $post_id ); |
||
| 219 | |||
| 220 | if ( is_wp_error( $post ) ) { |
||
| 221 | /* @var WP_Error $post */ |
||
| 222 | return $post; |
||
| 223 | } |
||
| 224 | |||
| 225 | $github_path = get_post_meta( $post_id, '_wogh_github_path', true ); |
||
| 226 | |||
| 227 | $message = apply_filters( |
||
| 228 | 'wogh_commit_msg_delete', |
||
| 229 | sprintf( |
||
| 230 | 'Deleting %s via WordPress at %s (%s)', |
||
| 231 | $github_path, |
||
| 232 | site_url(), |
||
| 233 | get_bloginfo( 'name' ) |
||
| 234 | ), |
||
| 235 | $post |
||
| 236 | ) . $this->get_commit_msg_tag(); |
||
| 237 | |||
| 238 | $result = $this->app->api()->persist()->delete_file( $github_path, $post->sha(), $message ); |
||
| 239 | |||
| 240 | if ( is_wp_error( $result ) ) { |
||
| 241 | /* @var WP_Error $result */ |
||
| 242 | return $result; |
||
| 243 | } |
||
| 244 | |||
| 245 | return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
||
| 246 | } |
||
| 247 | |||
| 248 | |||
| 249 | /** |
||
| 250 | * Saves the export user to the database. |
||
| 251 | * |
||
| 252 | * @param int $user_id User ID to export with. |
||
| 253 | * |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | public function set_user( $user_id ) { |
||
| 257 | return update_option( self::EXPORT_USER_OPTION, (int) $user_id ); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Gets the commit message tag. |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | protected function get_commit_msg_tag() { |
||
| 266 | $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' ); |
||
| 267 | |||
| 268 | if ( ! $tag ) { |
||
| 269 | throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
||
| 270 | } |
||
| 271 | |||
| 272 | return ' - ' . $tag; |
||
| 273 | } |
||
| 274 | } |
||
| 275 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.