1 | <?php |
||
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 ) { |
||
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 ) { |
||
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() { |
||
283 | } |
||
284 | } |
||
285 |