@@ -10,270 +10,270 @@ |
||
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|string |
|
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 | - $message = apply_filters( |
|
194 | - 'wogh_commit_msg_update_post', |
|
195 | - sprintf( |
|
196 | - 'Update post %s from WordPress at %s (%s)', |
|
197 | - $github_path, |
|
198 | - site_url(), |
|
199 | - get_bloginfo( 'name' ) |
|
200 | - ) |
|
201 | - ) . $this->get_commit_msg_tag(); |
|
202 | - $result = $persist->update_file( $blob, $message ); |
|
203 | - if ( is_wp_error( $result ) ) { |
|
204 | - return $result; |
|
205 | - } |
|
206 | - } |
|
207 | - |
|
208 | - $sha = $result->content->sha; |
|
209 | - $post->set_sha($sha); |
|
210 | - $post->set_old_github_path($github_path); |
|
211 | - |
|
212 | - return true; |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * Deletes a provided post ID from master. |
|
217 | - * |
|
218 | - * @param int $post_id Post ID to delete. |
|
219 | - * |
|
220 | - * @return string|WP_Error |
|
221 | - */ |
|
222 | - public function delete( $post_id ) { |
|
223 | - $post = $this->app->database()->fetch_by_id( $post_id ); |
|
224 | - |
|
225 | - if ( is_wp_error( $post ) ) { |
|
226 | - /* @var WP_Error $post */ |
|
227 | - return $post; |
|
228 | - } |
|
229 | - |
|
230 | - $github_path = get_post_meta( $post_id, '_wogh_github_path', true ); |
|
231 | - |
|
232 | - $message = apply_filters( |
|
233 | - 'wogh_commit_msg_delete', |
|
234 | - sprintf( |
|
235 | - 'Deleting %s via WordPress at %s (%s)', |
|
236 | - $github_path, |
|
237 | - site_url(), |
|
238 | - get_bloginfo( 'name' ) |
|
239 | - ), |
|
240 | - $post |
|
241 | - ) . $this->get_commit_msg_tag(); |
|
242 | - |
|
243 | - $result = $this->app->api()->persist()->delete_file( $github_path, $post->sha(), $message ); |
|
244 | - |
|
245 | - if ( is_wp_error( $result ) ) { |
|
246 | - /* @var WP_Error $result */ |
|
247 | - return $result; |
|
248 | - } |
|
249 | - |
|
250 | - return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
251 | - } |
|
252 | - |
|
253 | - |
|
254 | - /** |
|
255 | - * Saves the export user to the database. |
|
256 | - * |
|
257 | - * @param int $user_id User ID to export with. |
|
258 | - * |
|
259 | - * @return bool |
|
260 | - */ |
|
261 | - public function set_user( $user_id ) { |
|
262 | - return update_option( self::EXPORT_USER_OPTION, (int) $user_id ); |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Gets the commit message tag. |
|
267 | - * |
|
268 | - * @return string |
|
269 | - */ |
|
270 | - protected function get_commit_msg_tag() { |
|
271 | - $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' ); |
|
272 | - |
|
273 | - if ( ! $tag ) { |
|
274 | - throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
|
275 | - } |
|
276 | - |
|
277 | - return ' - ' . $tag; |
|
278 | - } |
|
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|string |
|
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 | + $message = apply_filters( |
|
194 | + 'wogh_commit_msg_update_post', |
|
195 | + sprintf( |
|
196 | + 'Update post %s from WordPress at %s (%s)', |
|
197 | + $github_path, |
|
198 | + site_url(), |
|
199 | + get_bloginfo( 'name' ) |
|
200 | + ) |
|
201 | + ) . $this->get_commit_msg_tag(); |
|
202 | + $result = $persist->update_file( $blob, $message ); |
|
203 | + if ( is_wp_error( $result ) ) { |
|
204 | + return $result; |
|
205 | + } |
|
206 | + } |
|
207 | + |
|
208 | + $sha = $result->content->sha; |
|
209 | + $post->set_sha($sha); |
|
210 | + $post->set_old_github_path($github_path); |
|
211 | + |
|
212 | + return true; |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * Deletes a provided post ID from master. |
|
217 | + * |
|
218 | + * @param int $post_id Post ID to delete. |
|
219 | + * |
|
220 | + * @return string|WP_Error |
|
221 | + */ |
|
222 | + public function delete( $post_id ) { |
|
223 | + $post = $this->app->database()->fetch_by_id( $post_id ); |
|
224 | + |
|
225 | + if ( is_wp_error( $post ) ) { |
|
226 | + /* @var WP_Error $post */ |
|
227 | + return $post; |
|
228 | + } |
|
229 | + |
|
230 | + $github_path = get_post_meta( $post_id, '_wogh_github_path', true ); |
|
231 | + |
|
232 | + $message = apply_filters( |
|
233 | + 'wogh_commit_msg_delete', |
|
234 | + sprintf( |
|
235 | + 'Deleting %s via WordPress at %s (%s)', |
|
236 | + $github_path, |
|
237 | + site_url(), |
|
238 | + get_bloginfo( 'name' ) |
|
239 | + ), |
|
240 | + $post |
|
241 | + ) . $this->get_commit_msg_tag(); |
|
242 | + |
|
243 | + $result = $this->app->api()->persist()->delete_file( $github_path, $post->sha(), $message ); |
|
244 | + |
|
245 | + if ( is_wp_error( $result ) ) { |
|
246 | + /* @var WP_Error $result */ |
|
247 | + return $result; |
|
248 | + } |
|
249 | + |
|
250 | + return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
251 | + } |
|
252 | + |
|
253 | + |
|
254 | + /** |
|
255 | + * Saves the export user to the database. |
|
256 | + * |
|
257 | + * @param int $user_id User ID to export with. |
|
258 | + * |
|
259 | + * @return bool |
|
260 | + */ |
|
261 | + public function set_user( $user_id ) { |
|
262 | + return update_option( self::EXPORT_USER_OPTION, (int) $user_id ); |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Gets the commit message tag. |
|
267 | + * |
|
268 | + * @return string |
|
269 | + */ |
|
270 | + protected function get_commit_msg_tag() { |
|
271 | + $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' ); |
|
272 | + |
|
273 | + if ( ! $tag ) { |
|
274 | + throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
|
275 | + } |
|
276 | + |
|
277 | + return ' - ' . $tag; |
|
278 | + } |
|
279 | 279 | } |
@@ -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|string |
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,14 +181,14 @@ 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 | 193 | $message = apply_filters( |
194 | 194 | 'wogh_commit_msg_update_post', |
@@ -196,11 +196,11 @@ discard block |
||
196 | 196 | 'Update post %s from WordPress at %s (%s)', |
197 | 197 | $github_path, |
198 | 198 | site_url(), |
199 | - get_bloginfo( 'name' ) |
|
199 | + get_bloginfo('name') |
|
200 | 200 | ) |
201 | 201 | ) . $this->get_commit_msg_tag(); |
202 | - $result = $persist->update_file( $blob, $message ); |
|
203 | - if ( is_wp_error( $result ) ) { |
|
202 | + $result = $persist->update_file($blob, $message); |
|
203 | + if (is_wp_error($result)) { |
|
204 | 204 | return $result; |
205 | 205 | } |
206 | 206 | } |
@@ -219,15 +219,15 @@ discard block |
||
219 | 219 | * |
220 | 220 | * @return string|WP_Error |
221 | 221 | */ |
222 | - public function delete( $post_id ) { |
|
223 | - $post = $this->app->database()->fetch_by_id( $post_id ); |
|
222 | + public function delete($post_id) { |
|
223 | + $post = $this->app->database()->fetch_by_id($post_id); |
|
224 | 224 | |
225 | - if ( is_wp_error( $post ) ) { |
|
225 | + if (is_wp_error($post)) { |
|
226 | 226 | /* @var WP_Error $post */ |
227 | 227 | return $post; |
228 | 228 | } |
229 | 229 | |
230 | - $github_path = get_post_meta( $post_id, '_wogh_github_path', true ); |
|
230 | + $github_path = get_post_meta($post_id, '_wogh_github_path', true); |
|
231 | 231 | |
232 | 232 | $message = apply_filters( |
233 | 233 | 'wogh_commit_msg_delete', |
@@ -235,19 +235,19 @@ discard block |
||
235 | 235 | 'Deleting %s via WordPress at %s (%s)', |
236 | 236 | $github_path, |
237 | 237 | site_url(), |
238 | - get_bloginfo( 'name' ) |
|
238 | + get_bloginfo('name') |
|
239 | 239 | ), |
240 | 240 | $post |
241 | 241 | ) . $this->get_commit_msg_tag(); |
242 | 242 | |
243 | - $result = $this->app->api()->persist()->delete_file( $github_path, $post->sha(), $message ); |
|
243 | + $result = $this->app->api()->persist()->delete_file($github_path, $post->sha(), $message); |
|
244 | 244 | |
245 | - if ( is_wp_error( $result ) ) { |
|
245 | + if (is_wp_error($result)) { |
|
246 | 246 | /* @var WP_Error $result */ |
247 | 247 | return $result; |
248 | 248 | } |
249 | 249 | |
250 | - return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
250 | + return __('Export to GitHub completed successfully.', 'writing-on-github'); |
|
251 | 251 | } |
252 | 252 | |
253 | 253 | |
@@ -258,8 +258,8 @@ discard block |
||
258 | 258 | * |
259 | 259 | * @return bool |
260 | 260 | */ |
261 | - public function set_user( $user_id ) { |
|
262 | - return update_option( self::EXPORT_USER_OPTION, (int) $user_id ); |
|
261 | + public function set_user($user_id) { |
|
262 | + return update_option(self::EXPORT_USER_OPTION, (int) $user_id); |
|
263 | 263 | } |
264 | 264 | |
265 | 265 | /** |
@@ -268,10 +268,10 @@ discard block |
||
268 | 268 | * @return string |
269 | 269 | */ |
270 | 270 | protected function get_commit_msg_tag() { |
271 | - $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' ); |
|
271 | + $tag = apply_filters('wogh_commit_msg_tag', 'wogh'); |
|
272 | 272 | |
273 | - if ( ! $tag ) { |
|
274 | - throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
|
273 | + if ( ! $tag) { |
|
274 | + throw new Exception(__('Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github')); |
|
275 | 275 | } |
276 | 276 | |
277 | 277 | return ' - ' . $tag; |
@@ -10,317 +10,317 @@ |
||
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 | - if ( $is_remove ) { |
|
83 | - $result = $this->delete_post( $blob ); |
|
84 | - } else { |
|
85 | - $result = $this->import_post( $blob ); |
|
86 | - } |
|
87 | - } |
|
88 | - |
|
89 | - if ( is_wp_error( $result ) ) { |
|
90 | - /* @var WP_Error $result */ |
|
91 | - $error = wogh_append_error( $error, $result ); |
|
92 | - } |
|
93 | - } |
|
94 | - |
|
95 | - return $error; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Imports the latest commit on the master branch. |
|
100 | - * |
|
101 | - * @return string|WP_Error |
|
102 | - */ |
|
103 | - public function master() { |
|
104 | - $result = $this->app->api()->fetch()->tree_recursive(); |
|
105 | - |
|
106 | - if ( is_wp_error( $result ) ) { |
|
107 | - /* @var WP_Error $result */ |
|
108 | - return $result; |
|
109 | - } |
|
110 | - |
|
111 | - if ( is_array( $result ) ) { |
|
112 | - $result = $this->import_files( $result ); |
|
113 | - } |
|
114 | - |
|
115 | - if ( is_wp_error( $result ) ) { |
|
116 | - /* @var WP_Error $result */ |
|
117 | - return $result; |
|
118 | - } |
|
119 | - |
|
120 | - return __( 'Payload processed', 'writing-on-github' ); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Checks whether the provided blob should be imported. |
|
125 | - * |
|
126 | - * @param Writing_On_GitHub_File_Info $file |
|
127 | - * |
|
128 | - * @return bool |
|
129 | - */ |
|
130 | - protected function importable_file( Writing_On_GitHub_File_Info $file ) { |
|
131 | - |
|
132 | - $path = $file->path; |
|
133 | - |
|
134 | - // only _pages, _posts and images |
|
135 | - $prefixs = array( '_pages/', '_posts/', 'images/'); |
|
136 | - foreach ($prefixs as $prefix) { |
|
137 | - if ( ! strncasecmp($path, $prefix, strlen( $prefix ) ) ) { |
|
138 | - return true; |
|
139 | - } |
|
140 | - } |
|
141 | - return false; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Checks whether the provided blob should be imported. |
|
146 | - * |
|
147 | - * @param Writing_On_GitHub_Blob $blob Blob to validate. |
|
148 | - * |
|
149 | - * @return bool |
|
150 | - */ |
|
151 | - protected function importable_post( Writing_On_GitHub_Blob $blob ) { |
|
152 | - // global $wpdb; |
|
153 | - |
|
154 | - // // Skip the repo's readme. |
|
155 | - // if ( 'readme' === strtolower( substr( $blob->path(), 0, 6 ) ) ) { |
|
156 | - // return false; |
|
157 | - // } |
|
158 | - |
|
159 | - // // If the blob sha already matches a post, then move on. |
|
160 | - // if ( ! is_wp_error( $this->app->database()->fetch_by_sha( $blob->sha() ) ) ) { |
|
161 | - // return false; |
|
162 | - // } |
|
163 | - |
|
164 | - if ( ! $blob->has_frontmatter() ) { |
|
165 | - return false; |
|
166 | - } |
|
167 | - |
|
168 | - return true; |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Delete post |
|
173 | - * @param Writing_On_GitHub_Blob $blob |
|
174 | - * @return WP_Error|bool |
|
175 | - */ |
|
176 | - protected function delete_post( Writing_On_GitHub_Blob $blob ) { |
|
177 | - $id = $blob->id(); |
|
178 | - if ( empty( $id ) ) { |
|
179 | - return false; |
|
180 | - } |
|
181 | - $result = $this->app->database()->delete_post( $id ); |
|
182 | - if ( is_wp_error( $result ) ) { |
|
183 | - /* @var WP_Error $result */ |
|
184 | - return $result; |
|
185 | - } |
|
186 | - return true; |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Imports a post into wordpress |
|
191 | - * @param Writing_On_GitHub_Blob $blob |
|
192 | - * @return WP_Error|bool |
|
193 | - */ |
|
194 | - protected function import_post( Writing_On_GitHub_Blob $blob ) { |
|
195 | - $post = $this->blob_to_post( $blob ); |
|
196 | - |
|
197 | - if ( ! $post instanceof Writing_On_GitHub_Post ) { |
|
198 | - return false; |
|
199 | - } |
|
200 | - |
|
201 | - $result = $this->app->database()->save_post( $post ); |
|
202 | - if ( is_wp_error( $result ) ) { |
|
203 | - /** @var WP_Error $result */ |
|
204 | - return $result; |
|
205 | - } |
|
206 | - |
|
207 | - if ( $post->is_new() || |
|
208 | - ! wogh_equal_front_matter( $post, $blob ) ) { |
|
209 | - |
|
210 | - $result = $this->app->export()->export_post( $post ); |
|
211 | - |
|
212 | - if ( is_wp_error( $result ) ) { |
|
213 | - /** @var WP_Error $result */ |
|
214 | - return $result; |
|
215 | - } |
|
216 | - } |
|
217 | - |
|
218 | - clean_post_cache( $post->id() ); |
|
219 | - |
|
220 | - return true; |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * import raw file |
|
225 | - * @param Writing_On_GitHub_Blob $blob |
|
226 | - * @return bool |
|
227 | - */ |
|
228 | - protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { |
|
229 | - if ( $blob->has_frontmatter() ) { |
|
230 | - return false; |
|
231 | - } |
|
232 | - |
|
233 | - // only images |
|
234 | - if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) { |
|
235 | - return false; |
|
236 | - } |
|
237 | - |
|
238 | - return true; |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * Imports a raw file content into file system. |
|
243 | - * @param Writing_On_GitHub_Blob $blob |
|
244 | - * @param bool $is_remove |
|
245 | - */ |
|
246 | - protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { |
|
247 | - $arr = wp_upload_dir(); |
|
248 | - $path = $arr['basedir'] . '/writing-on-github/' . $blob->path(); |
|
249 | - if ( $is_remove ) { |
|
250 | - if ( file_exists($path) ) { |
|
251 | - unlink($path); |
|
252 | - } |
|
253 | - } else { |
|
254 | - $dirname = dirname($path); |
|
255 | - if ( ! file_exists($dirname) ) { |
|
256 | - wp_mkdir_p($dirname); |
|
257 | - } |
|
258 | - |
|
259 | - file_put_contents($path, $blob->content()); |
|
260 | - } |
|
261 | - return true; |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * Imports a single blob content into matching post. |
|
266 | - * |
|
267 | - * @param Writing_On_GitHub_Blob $blob Blob to transform into a Post. |
|
268 | - * |
|
269 | - * @return Writing_On_GitHub_Post|false |
|
270 | - */ |
|
271 | - protected function blob_to_post( Writing_On_GitHub_Blob $blob ) { |
|
272 | - $args = array( 'post_content' => $blob->content_import() ); |
|
273 | - $meta = $blob->meta(); |
|
274 | - |
|
275 | - $id = false; |
|
276 | - |
|
277 | - if ( ! empty( $meta ) ) { |
|
278 | - if ( array_key_exists( 'layout', $meta ) ) { |
|
279 | - $args['post_type'] = $meta['layout']; |
|
280 | - unset( $meta['layout'] ); |
|
281 | - } |
|
282 | - |
|
283 | - if ( array_key_exists( 'published', $meta ) ) { |
|
284 | - $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; |
|
285 | - unset( $meta['published'] ); |
|
286 | - } |
|
287 | - |
|
288 | - if ( array_key_exists( 'post_title', $meta ) ) { |
|
289 | - $args['post_title'] = $meta['post_title']; |
|
290 | - unset( $meta['post_title'] ); |
|
291 | - } |
|
292 | - |
|
293 | - if ( array_key_exists( 'post_name', $meta ) ) { |
|
294 | - $args['post_name'] = $meta['post_name']; |
|
295 | - unset( $meta['post_name'] ); |
|
296 | - } |
|
297 | - |
|
298 | - if ( array_key_exists( 'ID', $meta ) ) { |
|
299 | - $id = $args['ID'] = $meta['ID']; |
|
300 | - $blob->set_id($id); |
|
301 | - unset( $meta['ID'] ); |
|
302 | - } |
|
303 | - } |
|
304 | - |
|
305 | - $meta['_wogh_sha'] = $blob->sha(); |
|
306 | - |
|
307 | - if ( $id ) { |
|
308 | - $old_sha = get_post_meta( $id, '_wogh_sha', true ); |
|
309 | - $old_github_path = get_post_meta( $id, '_wogh_github_path', true ); |
|
310 | - |
|
311 | - // dont save post when has same sha |
|
312 | - if ( $old_sha && $old_sha == $meta['_wogh_sha'] && |
|
313 | - $old_github_path && $old_github_path == $blob->path() ) { |
|
314 | - return false; |
|
315 | - } |
|
316 | - } |
|
317 | - |
|
318 | - $post = new Writing_On_GitHub_Post( $args, $this->app->api() ); |
|
319 | - $post->set_old_github_path( $blob->path() ); |
|
320 | - $post->set_meta( $meta ); |
|
321 | - $post->set_blob( $blob ); |
|
322 | - $blob->set_id( $post->id() ); |
|
323 | - |
|
324 | - return $post; |
|
325 | - } |
|
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 | + if ( $is_remove ) { |
|
83 | + $result = $this->delete_post( $blob ); |
|
84 | + } else { |
|
85 | + $result = $this->import_post( $blob ); |
|
86 | + } |
|
87 | + } |
|
88 | + |
|
89 | + if ( is_wp_error( $result ) ) { |
|
90 | + /* @var WP_Error $result */ |
|
91 | + $error = wogh_append_error( $error, $result ); |
|
92 | + } |
|
93 | + } |
|
94 | + |
|
95 | + return $error; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Imports the latest commit on the master branch. |
|
100 | + * |
|
101 | + * @return string|WP_Error |
|
102 | + */ |
|
103 | + public function master() { |
|
104 | + $result = $this->app->api()->fetch()->tree_recursive(); |
|
105 | + |
|
106 | + if ( is_wp_error( $result ) ) { |
|
107 | + /* @var WP_Error $result */ |
|
108 | + return $result; |
|
109 | + } |
|
110 | + |
|
111 | + if ( is_array( $result ) ) { |
|
112 | + $result = $this->import_files( $result ); |
|
113 | + } |
|
114 | + |
|
115 | + if ( is_wp_error( $result ) ) { |
|
116 | + /* @var WP_Error $result */ |
|
117 | + return $result; |
|
118 | + } |
|
119 | + |
|
120 | + return __( 'Payload processed', 'writing-on-github' ); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Checks whether the provided blob should be imported. |
|
125 | + * |
|
126 | + * @param Writing_On_GitHub_File_Info $file |
|
127 | + * |
|
128 | + * @return bool |
|
129 | + */ |
|
130 | + protected function importable_file( Writing_On_GitHub_File_Info $file ) { |
|
131 | + |
|
132 | + $path = $file->path; |
|
133 | + |
|
134 | + // only _pages, _posts and images |
|
135 | + $prefixs = array( '_pages/', '_posts/', 'images/'); |
|
136 | + foreach ($prefixs as $prefix) { |
|
137 | + if ( ! strncasecmp($path, $prefix, strlen( $prefix ) ) ) { |
|
138 | + return true; |
|
139 | + } |
|
140 | + } |
|
141 | + return false; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Checks whether the provided blob should be imported. |
|
146 | + * |
|
147 | + * @param Writing_On_GitHub_Blob $blob Blob to validate. |
|
148 | + * |
|
149 | + * @return bool |
|
150 | + */ |
|
151 | + protected function importable_post( Writing_On_GitHub_Blob $blob ) { |
|
152 | + // global $wpdb; |
|
153 | + |
|
154 | + // // Skip the repo's readme. |
|
155 | + // if ( 'readme' === strtolower( substr( $blob->path(), 0, 6 ) ) ) { |
|
156 | + // return false; |
|
157 | + // } |
|
158 | + |
|
159 | + // // If the blob sha already matches a post, then move on. |
|
160 | + // if ( ! is_wp_error( $this->app->database()->fetch_by_sha( $blob->sha() ) ) ) { |
|
161 | + // return false; |
|
162 | + // } |
|
163 | + |
|
164 | + if ( ! $blob->has_frontmatter() ) { |
|
165 | + return false; |
|
166 | + } |
|
167 | + |
|
168 | + return true; |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Delete post |
|
173 | + * @param Writing_On_GitHub_Blob $blob |
|
174 | + * @return WP_Error|bool |
|
175 | + */ |
|
176 | + protected function delete_post( Writing_On_GitHub_Blob $blob ) { |
|
177 | + $id = $blob->id(); |
|
178 | + if ( empty( $id ) ) { |
|
179 | + return false; |
|
180 | + } |
|
181 | + $result = $this->app->database()->delete_post( $id ); |
|
182 | + if ( is_wp_error( $result ) ) { |
|
183 | + /* @var WP_Error $result */ |
|
184 | + return $result; |
|
185 | + } |
|
186 | + return true; |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Imports a post into wordpress |
|
191 | + * @param Writing_On_GitHub_Blob $blob |
|
192 | + * @return WP_Error|bool |
|
193 | + */ |
|
194 | + protected function import_post( Writing_On_GitHub_Blob $blob ) { |
|
195 | + $post = $this->blob_to_post( $blob ); |
|
196 | + |
|
197 | + if ( ! $post instanceof Writing_On_GitHub_Post ) { |
|
198 | + return false; |
|
199 | + } |
|
200 | + |
|
201 | + $result = $this->app->database()->save_post( $post ); |
|
202 | + if ( is_wp_error( $result ) ) { |
|
203 | + /** @var WP_Error $result */ |
|
204 | + return $result; |
|
205 | + } |
|
206 | + |
|
207 | + if ( $post->is_new() || |
|
208 | + ! wogh_equal_front_matter( $post, $blob ) ) { |
|
209 | + |
|
210 | + $result = $this->app->export()->export_post( $post ); |
|
211 | + |
|
212 | + if ( is_wp_error( $result ) ) { |
|
213 | + /** @var WP_Error $result */ |
|
214 | + return $result; |
|
215 | + } |
|
216 | + } |
|
217 | + |
|
218 | + clean_post_cache( $post->id() ); |
|
219 | + |
|
220 | + return true; |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * import raw file |
|
225 | + * @param Writing_On_GitHub_Blob $blob |
|
226 | + * @return bool |
|
227 | + */ |
|
228 | + protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { |
|
229 | + if ( $blob->has_frontmatter() ) { |
|
230 | + return false; |
|
231 | + } |
|
232 | + |
|
233 | + // only images |
|
234 | + if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) { |
|
235 | + return false; |
|
236 | + } |
|
237 | + |
|
238 | + return true; |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * Imports a raw file content into file system. |
|
243 | + * @param Writing_On_GitHub_Blob $blob |
|
244 | + * @param bool $is_remove |
|
245 | + */ |
|
246 | + protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { |
|
247 | + $arr = wp_upload_dir(); |
|
248 | + $path = $arr['basedir'] . '/writing-on-github/' . $blob->path(); |
|
249 | + if ( $is_remove ) { |
|
250 | + if ( file_exists($path) ) { |
|
251 | + unlink($path); |
|
252 | + } |
|
253 | + } else { |
|
254 | + $dirname = dirname($path); |
|
255 | + if ( ! file_exists($dirname) ) { |
|
256 | + wp_mkdir_p($dirname); |
|
257 | + } |
|
258 | + |
|
259 | + file_put_contents($path, $blob->content()); |
|
260 | + } |
|
261 | + return true; |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * Imports a single blob content into matching post. |
|
266 | + * |
|
267 | + * @param Writing_On_GitHub_Blob $blob Blob to transform into a Post. |
|
268 | + * |
|
269 | + * @return Writing_On_GitHub_Post|false |
|
270 | + */ |
|
271 | + protected function blob_to_post( Writing_On_GitHub_Blob $blob ) { |
|
272 | + $args = array( 'post_content' => $blob->content_import() ); |
|
273 | + $meta = $blob->meta(); |
|
274 | + |
|
275 | + $id = false; |
|
276 | + |
|
277 | + if ( ! empty( $meta ) ) { |
|
278 | + if ( array_key_exists( 'layout', $meta ) ) { |
|
279 | + $args['post_type'] = $meta['layout']; |
|
280 | + unset( $meta['layout'] ); |
|
281 | + } |
|
282 | + |
|
283 | + if ( array_key_exists( 'published', $meta ) ) { |
|
284 | + $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; |
|
285 | + unset( $meta['published'] ); |
|
286 | + } |
|
287 | + |
|
288 | + if ( array_key_exists( 'post_title', $meta ) ) { |
|
289 | + $args['post_title'] = $meta['post_title']; |
|
290 | + unset( $meta['post_title'] ); |
|
291 | + } |
|
292 | + |
|
293 | + if ( array_key_exists( 'post_name', $meta ) ) { |
|
294 | + $args['post_name'] = $meta['post_name']; |
|
295 | + unset( $meta['post_name'] ); |
|
296 | + } |
|
297 | + |
|
298 | + if ( array_key_exists( 'ID', $meta ) ) { |
|
299 | + $id = $args['ID'] = $meta['ID']; |
|
300 | + $blob->set_id($id); |
|
301 | + unset( $meta['ID'] ); |
|
302 | + } |
|
303 | + } |
|
304 | + |
|
305 | + $meta['_wogh_sha'] = $blob->sha(); |
|
306 | + |
|
307 | + if ( $id ) { |
|
308 | + $old_sha = get_post_meta( $id, '_wogh_sha', true ); |
|
309 | + $old_github_path = get_post_meta( $id, '_wogh_github_path', true ); |
|
310 | + |
|
311 | + // dont save post when has same sha |
|
312 | + if ( $old_sha && $old_sha == $meta['_wogh_sha'] && |
|
313 | + $old_github_path && $old_github_path == $blob->path() ) { |
|
314 | + return false; |
|
315 | + } |
|
316 | + } |
|
317 | + |
|
318 | + $post = new Writing_On_GitHub_Post( $args, $this->app->api() ); |
|
319 | + $post->set_old_github_path( $blob->path() ); |
|
320 | + $post->set_meta( $meta ); |
|
321 | + $post->set_blob( $blob ); |
|
322 | + $blob->set_id( $post->id() ); |
|
323 | + |
|
324 | + return $post; |
|
325 | + } |
|
326 | 326 | } |
@@ -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,37 +58,37 @@ 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 | - if ( $is_remove ) { |
|
83 | - $result = $this->delete_post( $blob ); |
|
79 | + if ($this->importable_raw_file($blob)) { |
|
80 | + $result = $this->import_raw_file($blob, $is_remove); |
|
81 | + } elseif ($this->importable_post($blob)) { |
|
82 | + if ($is_remove) { |
|
83 | + $result = $this->delete_post($blob); |
|
84 | 84 | } else { |
85 | - $result = $this->import_post( $blob ); |
|
85 | + $result = $this->import_post($blob); |
|
86 | 86 | } |
87 | 87 | } |
88 | 88 | |
89 | - if ( is_wp_error( $result ) ) { |
|
89 | + if (is_wp_error($result)) { |
|
90 | 90 | /* @var WP_Error $result */ |
91 | - $error = wogh_append_error( $error, $result ); |
|
91 | + $error = wogh_append_error($error, $result); |
|
92 | 92 | } |
93 | 93 | } |
94 | 94 | |
@@ -103,21 +103,21 @@ discard block |
||
103 | 103 | public function master() { |
104 | 104 | $result = $this->app->api()->fetch()->tree_recursive(); |
105 | 105 | |
106 | - if ( is_wp_error( $result ) ) { |
|
106 | + if (is_wp_error($result)) { |
|
107 | 107 | /* @var WP_Error $result */ |
108 | 108 | return $result; |
109 | 109 | } |
110 | 110 | |
111 | - if ( is_array( $result ) ) { |
|
112 | - $result = $this->import_files( $result ); |
|
111 | + if (is_array($result)) { |
|
112 | + $result = $this->import_files($result); |
|
113 | 113 | } |
114 | 114 | |
115 | - if ( is_wp_error( $result ) ) { |
|
115 | + if (is_wp_error($result)) { |
|
116 | 116 | /* @var WP_Error $result */ |
117 | 117 | return $result; |
118 | 118 | } |
119 | 119 | |
120 | - return __( 'Payload processed', 'writing-on-github' ); |
|
120 | + return __('Payload processed', 'writing-on-github'); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | /** |
@@ -127,14 +127,14 @@ discard block |
||
127 | 127 | * |
128 | 128 | * @return bool |
129 | 129 | */ |
130 | - protected function importable_file( Writing_On_GitHub_File_Info $file ) { |
|
130 | + protected function importable_file(Writing_On_GitHub_File_Info $file) { |
|
131 | 131 | |
132 | 132 | $path = $file->path; |
133 | 133 | |
134 | 134 | // only _pages, _posts and images |
135 | - $prefixs = array( '_pages/', '_posts/', 'images/'); |
|
135 | + $prefixs = array('_pages/', '_posts/', 'images/'); |
|
136 | 136 | foreach ($prefixs as $prefix) { |
137 | - if ( ! strncasecmp($path, $prefix, strlen( $prefix ) ) ) { |
|
137 | + if ( ! strncasecmp($path, $prefix, strlen($prefix))) { |
|
138 | 138 | return true; |
139 | 139 | } |
140 | 140 | } |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | * |
149 | 149 | * @return bool |
150 | 150 | */ |
151 | - protected function importable_post( Writing_On_GitHub_Blob $blob ) { |
|
151 | + protected function importable_post(Writing_On_GitHub_Blob $blob) { |
|
152 | 152 | // global $wpdb; |
153 | 153 | |
154 | 154 | // // Skip the repo's readme. |
@@ -161,7 +161,7 @@ discard block |
||
161 | 161 | // return false; |
162 | 162 | // } |
163 | 163 | |
164 | - if ( ! $blob->has_frontmatter() ) { |
|
164 | + if ( ! $blob->has_frontmatter()) { |
|
165 | 165 | return false; |
166 | 166 | } |
167 | 167 | |
@@ -173,13 +173,13 @@ discard block |
||
173 | 173 | * @param Writing_On_GitHub_Blob $blob |
174 | 174 | * @return WP_Error|bool |
175 | 175 | */ |
176 | - protected function delete_post( Writing_On_GitHub_Blob $blob ) { |
|
176 | + protected function delete_post(Writing_On_GitHub_Blob $blob) { |
|
177 | 177 | $id = $blob->id(); |
178 | - if ( empty( $id ) ) { |
|
178 | + if (empty($id)) { |
|
179 | 179 | return false; |
180 | 180 | } |
181 | - $result = $this->app->database()->delete_post( $id ); |
|
182 | - if ( is_wp_error( $result ) ) { |
|
181 | + $result = $this->app->database()->delete_post($id); |
|
182 | + if (is_wp_error($result)) { |
|
183 | 183 | /* @var WP_Error $result */ |
184 | 184 | return $result; |
185 | 185 | } |
@@ -191,31 +191,31 @@ discard block |
||
191 | 191 | * @param Writing_On_GitHub_Blob $blob |
192 | 192 | * @return WP_Error|bool |
193 | 193 | */ |
194 | - protected function import_post( Writing_On_GitHub_Blob $blob ) { |
|
195 | - $post = $this->blob_to_post( $blob ); |
|
194 | + protected function import_post(Writing_On_GitHub_Blob $blob) { |
|
195 | + $post = $this->blob_to_post($blob); |
|
196 | 196 | |
197 | - if ( ! $post instanceof Writing_On_GitHub_Post ) { |
|
197 | + if ( ! $post instanceof Writing_On_GitHub_Post) { |
|
198 | 198 | return false; |
199 | 199 | } |
200 | 200 | |
201 | - $result = $this->app->database()->save_post( $post ); |
|
202 | - if ( is_wp_error( $result ) ) { |
|
201 | + $result = $this->app->database()->save_post($post); |
|
202 | + if (is_wp_error($result)) { |
|
203 | 203 | /** @var WP_Error $result */ |
204 | 204 | return $result; |
205 | 205 | } |
206 | 206 | |
207 | - if ( $post->is_new() || |
|
208 | - ! wogh_equal_front_matter( $post, $blob ) ) { |
|
207 | + if ($post->is_new() || |
|
208 | + ! wogh_equal_front_matter($post, $blob)) { |
|
209 | 209 | |
210 | - $result = $this->app->export()->export_post( $post ); |
|
210 | + $result = $this->app->export()->export_post($post); |
|
211 | 211 | |
212 | - if ( is_wp_error( $result ) ) { |
|
212 | + if (is_wp_error($result)) { |
|
213 | 213 | /** @var WP_Error $result */ |
214 | 214 | return $result; |
215 | 215 | } |
216 | 216 | } |
217 | 217 | |
218 | - clean_post_cache( $post->id() ); |
|
218 | + clean_post_cache($post->id()); |
|
219 | 219 | |
220 | 220 | return true; |
221 | 221 | } |
@@ -225,13 +225,13 @@ discard block |
||
225 | 225 | * @param Writing_On_GitHub_Blob $blob |
226 | 226 | * @return bool |
227 | 227 | */ |
228 | - protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { |
|
229 | - if ( $blob->has_frontmatter() ) { |
|
228 | + protected function importable_raw_file(Writing_On_GitHub_Blob $blob) { |
|
229 | + if ($blob->has_frontmatter()) { |
|
230 | 230 | return false; |
231 | 231 | } |
232 | 232 | |
233 | 233 | // only images |
234 | - if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) { |
|
234 | + if (strncasecmp($blob->path(), 'images/', strlen('images/')) != 0) { |
|
235 | 235 | return false; |
236 | 236 | } |
237 | 237 | |
@@ -243,16 +243,16 @@ discard block |
||
243 | 243 | * @param Writing_On_GitHub_Blob $blob |
244 | 244 | * @param bool $is_remove |
245 | 245 | */ |
246 | - protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { |
|
246 | + protected function import_raw_file(Writing_On_GitHub_Blob $blob, $is_remove) { |
|
247 | 247 | $arr = wp_upload_dir(); |
248 | 248 | $path = $arr['basedir'] . '/writing-on-github/' . $blob->path(); |
249 | - if ( $is_remove ) { |
|
250 | - if ( file_exists($path) ) { |
|
249 | + if ($is_remove) { |
|
250 | + if (file_exists($path)) { |
|
251 | 251 | unlink($path); |
252 | 252 | } |
253 | 253 | } else { |
254 | 254 | $dirname = dirname($path); |
255 | - if ( ! file_exists($dirname) ) { |
|
255 | + if ( ! file_exists($dirname)) { |
|
256 | 256 | wp_mkdir_p($dirname); |
257 | 257 | } |
258 | 258 | |
@@ -268,58 +268,58 @@ discard block |
||
268 | 268 | * |
269 | 269 | * @return Writing_On_GitHub_Post|false |
270 | 270 | */ |
271 | - protected function blob_to_post( Writing_On_GitHub_Blob $blob ) { |
|
272 | - $args = array( 'post_content' => $blob->content_import() ); |
|
271 | + protected function blob_to_post(Writing_On_GitHub_Blob $blob) { |
|
272 | + $args = array('post_content' => $blob->content_import()); |
|
273 | 273 | $meta = $blob->meta(); |
274 | 274 | |
275 | 275 | $id = false; |
276 | 276 | |
277 | - if ( ! empty( $meta ) ) { |
|
278 | - if ( array_key_exists( 'layout', $meta ) ) { |
|
277 | + if ( ! empty($meta)) { |
|
278 | + if (array_key_exists('layout', $meta)) { |
|
279 | 279 | $args['post_type'] = $meta['layout']; |
280 | - unset( $meta['layout'] ); |
|
280 | + unset($meta['layout']); |
|
281 | 281 | } |
282 | 282 | |
283 | - if ( array_key_exists( 'published', $meta ) ) { |
|
283 | + if (array_key_exists('published', $meta)) { |
|
284 | 284 | $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; |
285 | - unset( $meta['published'] ); |
|
285 | + unset($meta['published']); |
|
286 | 286 | } |
287 | 287 | |
288 | - if ( array_key_exists( 'post_title', $meta ) ) { |
|
288 | + if (array_key_exists('post_title', $meta)) { |
|
289 | 289 | $args['post_title'] = $meta['post_title']; |
290 | - unset( $meta['post_title'] ); |
|
290 | + unset($meta['post_title']); |
|
291 | 291 | } |
292 | 292 | |
293 | - if ( array_key_exists( 'post_name', $meta ) ) { |
|
293 | + if (array_key_exists('post_name', $meta)) { |
|
294 | 294 | $args['post_name'] = $meta['post_name']; |
295 | - unset( $meta['post_name'] ); |
|
295 | + unset($meta['post_name']); |
|
296 | 296 | } |
297 | 297 | |
298 | - if ( array_key_exists( 'ID', $meta ) ) { |
|
298 | + if (array_key_exists('ID', $meta)) { |
|
299 | 299 | $id = $args['ID'] = $meta['ID']; |
300 | 300 | $blob->set_id($id); |
301 | - unset( $meta['ID'] ); |
|
301 | + unset($meta['ID']); |
|
302 | 302 | } |
303 | 303 | } |
304 | 304 | |
305 | 305 | $meta['_wogh_sha'] = $blob->sha(); |
306 | 306 | |
307 | - if ( $id ) { |
|
308 | - $old_sha = get_post_meta( $id, '_wogh_sha', true ); |
|
309 | - $old_github_path = get_post_meta( $id, '_wogh_github_path', true ); |
|
307 | + if ($id) { |
|
308 | + $old_sha = get_post_meta($id, '_wogh_sha', true); |
|
309 | + $old_github_path = get_post_meta($id, '_wogh_github_path', true); |
|
310 | 310 | |
311 | 311 | // dont save post when has same sha |
312 | - if ( $old_sha && $old_sha == $meta['_wogh_sha'] && |
|
313 | - $old_github_path && $old_github_path == $blob->path() ) { |
|
312 | + if ($old_sha && $old_sha == $meta['_wogh_sha'] && |
|
313 | + $old_github_path && $old_github_path == $blob->path()) { |
|
314 | 314 | return false; |
315 | 315 | } |
316 | 316 | } |
317 | 317 | |
318 | - $post = new Writing_On_GitHub_Post( $args, $this->app->api() ); |
|
319 | - $post->set_old_github_path( $blob->path() ); |
|
320 | - $post->set_meta( $meta ); |
|
321 | - $post->set_blob( $blob ); |
|
322 | - $blob->set_id( $post->id() ); |
|
318 | + $post = new Writing_On_GitHub_Post($args, $this->app->api()); |
|
319 | + $post->set_old_github_path($blob->path()); |
|
320 | + $post->set_meta($meta); |
|
321 | + $post->set_blob($blob); |
|
322 | + $blob->set_id($post->id()); |
|
323 | 323 | |
324 | 324 | return $post; |
325 | 325 | } |
@@ -9,474 +9,474 @@ |
||
9 | 9 | */ |
10 | 10 | class Writing_On_GitHub_Post { |
11 | 11 | |
12 | - /** |
|
13 | - * Api object |
|
14 | - * |
|
15 | - * @var Writing_On_GitHub_Api |
|
16 | - */ |
|
17 | - public $api; |
|
18 | - |
|
19 | - /** |
|
20 | - * Post ID |
|
21 | - * @var integer |
|
22 | - */ |
|
23 | - public $id = 0; |
|
24 | - |
|
25 | - /** |
|
26 | - * Blob object |
|
27 | - * @var Writing_On_GitHub_Blob |
|
28 | - */ |
|
29 | - public $blob; |
|
30 | - |
|
31 | - /** |
|
32 | - * Post object |
|
33 | - * @var WP_Post |
|
34 | - */ |
|
35 | - public $post; |
|
36 | - |
|
37 | - /** |
|
38 | - * Post args. |
|
39 | - * |
|
40 | - * @var array |
|
41 | - */ |
|
42 | - protected $args; |
|
43 | - |
|
44 | - /** |
|
45 | - * Post meta. |
|
46 | - * |
|
47 | - * @var array |
|
48 | - */ |
|
49 | - protected $meta; |
|
50 | - |
|
51 | - /** |
|
52 | - * Whether the post has been saved. |
|
53 | - * |
|
54 | - * @var bool |
|
55 | - */ |
|
56 | - protected $new = true; |
|
57 | - |
|
58 | - |
|
59 | - protected $old_github_path; |
|
60 | - |
|
61 | - /** |
|
62 | - * Instantiates a new Post object |
|
63 | - * |
|
64 | - * @param int|array $id_or_args Either a post ID or an array of arguments. |
|
65 | - * @param Writing_On_GitHub_Api $api API object. |
|
66 | - * |
|
67 | - * @todo remove database operations from this method |
|
68 | - */ |
|
69 | - public function __construct( $id_or_args, Writing_On_GitHub_Api $api ) { |
|
70 | - $this->api = $api; |
|
71 | - |
|
72 | - if ( is_numeric( $id_or_args ) ) { |
|
73 | - $this->id = (int) $id_or_args; |
|
74 | - $this->post = get_post( $this->id ); |
|
75 | - $this->new = false; |
|
76 | - } |
|
77 | - |
|
78 | - if ( is_array( $id_or_args ) ) { |
|
79 | - $this->args = $id_or_args; |
|
80 | - |
|
81 | - if ( isset( $this->args['ID'] ) ) { |
|
82 | - $this->post = get_post( $this->args['ID'] ); |
|
83 | - |
|
84 | - if ( $this->post ) { |
|
85 | - $this->id = $this->post->ID; |
|
86 | - $this->new = false; |
|
87 | - } else { |
|
88 | - unset( $this->args['ID'] ); |
|
89 | - } |
|
90 | - } |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - public function id() { |
|
95 | - return $this->id; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Returns the post type |
|
100 | - */ |
|
101 | - public function type() { |
|
102 | - return $this->post->post_type; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Returns the post type |
|
107 | - */ |
|
108 | - public function status() { |
|
109 | - return $this->post->post_status; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Returns the post name |
|
114 | - */ |
|
115 | - public function name() { |
|
116 | - return $this->post->post_name; |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Returns true if the post has a password |
|
121 | - * @return bool |
|
122 | - */ |
|
123 | - public function has_password() { |
|
124 | - return ! empty( $this->post->post_password ); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Combines the 2 content parts for GitHub |
|
129 | - */ |
|
130 | - public function github_content() { |
|
131 | - $content = $this->front_matter() . $this->post_content(); |
|
132 | - $ending = apply_filters( 'wogh_line_endings', "\n" ); |
|
133 | - |
|
134 | - return preg_replace( '~(*BSR_ANYCRLF)\R~', $ending, $content ); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * The post's YAML frontmatter |
|
139 | - * |
|
140 | - * Returns String the YAML frontmatter, ready to be written to the file |
|
141 | - */ |
|
142 | - public function front_matter() { |
|
143 | - return "---\n" . spyc_dump( $this->meta() ) . "---\n"; |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Returns the post_content |
|
148 | - * |
|
149 | - * Markdownify's the content if applicable |
|
150 | - */ |
|
151 | - public function post_content() { |
|
152 | - $content = $this->post->post_content; |
|
153 | - |
|
154 | - if ( function_exists( 'wpmarkdown_html_to_markdown' ) ) { |
|
155 | - $content = wpmarkdown_html_to_markdown( $content ); |
|
156 | - } else if ( class_exists( 'WPCom_Markdown' ) ) { |
|
157 | - if ( WPCom_Markdown::get_instance()->is_markdown( $this->post->ID ) ) { |
|
158 | - $content = $this->post->post_content_filtered; |
|
159 | - } |
|
160 | - } |
|
161 | - |
|
162 | - return apply_filters( 'wogh_content_export', $content, $this ); |
|
163 | - } |
|
164 | - |
|
165 | - public function old_github_path() { |
|
166 | - return $this->old_github_path; |
|
167 | - } |
|
168 | - |
|
169 | - public function set_old_github_path( $path ) { |
|
170 | - $this->old_github_path = $path; |
|
171 | - update_post_meta( $this->id, '_wogh_github_path', $path ); |
|
172 | - } |
|
173 | - |
|
174 | - |
|
175 | - /** |
|
176 | - * Retrieves or calculates the proper GitHub path for a given post |
|
177 | - * |
|
178 | - * Returns (string) the path relative to repo root |
|
179 | - */ |
|
180 | - public function github_path() { |
|
181 | - $path = $this->github_directory() . $this->github_filename(); |
|
182 | - |
|
183 | - return $path; |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Get GitHub directory based on post |
|
188 | - * |
|
189 | - * @return string |
|
190 | - */ |
|
191 | - public function github_directory() { |
|
192 | - if ( 'publish' !== $this->status() ) { |
|
193 | - return apply_filters( 'wogh_directory_unpublished', '_drafts/', $this ); |
|
194 | - } |
|
195 | - |
|
196 | - $name = ''; |
|
197 | - |
|
198 | - switch ( $this->type() ) { |
|
199 | - case 'post': |
|
200 | - $name = 'posts'; |
|
201 | - break; |
|
202 | - case 'page': |
|
203 | - $name = 'pages'; |
|
204 | - break; |
|
205 | - default: |
|
206 | - $obj = get_post_type_object( $this->type() ); |
|
207 | - |
|
208 | - if ( $obj ) { |
|
209 | - $name = strtolower( $obj->labels->name ); |
|
210 | - } |
|
211 | - |
|
212 | - if ( ! $name ) { |
|
213 | - $name = ''; |
|
214 | - } |
|
215 | - } |
|
216 | - |
|
217 | - if ( $name ) { |
|
218 | - $name = '_' . $name . '/'; |
|
219 | - } |
|
220 | - |
|
221 | - return apply_filters( 'wogh_directory_published', $name, $this ); |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * Build GitHub filename based on post |
|
226 | - */ |
|
227 | - public function github_filename() { |
|
228 | - if ( 'post' === $this->type() ) { |
|
229 | - $filename = get_the_time( 'Y-m-d-', $this->id ) . $this->get_name() . '.md'; |
|
230 | - } else { |
|
231 | - $filename = $this->get_name() . '.md'; |
|
232 | - } |
|
233 | - |
|
234 | - return apply_filters( 'wogh_filename', $filename, $this ); |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * Returns a post slug we can use in the GitHub filename |
|
239 | - * |
|
240 | - * @return string |
|
241 | - */ |
|
242 | - protected function get_name() { |
|
243 | - if ( '' !== $this->name() ) { |
|
244 | - return $this->name(); |
|
245 | - } |
|
246 | - |
|
247 | - return sanitize_title( get_the_title( $this->post ) ); |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * is put on github |
|
252 | - * @return boolean |
|
253 | - */ |
|
254 | - public function is_on_github() { |
|
255 | - $sha = get_post_meta( $this->id, '_wogh_sha', true ); |
|
256 | - $github_path = get_post_meta( $this->id, '_wogh_github_path', true ); |
|
257 | - if ( $sha && $github_path ) { |
|
258 | - return true; |
|
259 | - } |
|
260 | - return false; |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * Returns the URL for the post on GitHub. |
|
265 | - * |
|
266 | - * @return string |
|
267 | - */ |
|
268 | - public function github_view_url() { |
|
269 | - $github_path = get_post_meta( $this->id, '_wogh_github_path', true ); |
|
270 | - $repository = $this->api->fetch()->repository(); |
|
271 | - $branch = $this->api->fetch()->branch(); |
|
272 | - |
|
273 | - return "https://github.com/$repository/blob/$branch/$github_path"; |
|
274 | - } |
|
275 | - |
|
276 | - /** |
|
277 | - * Returns the URL for the post on GitHub. |
|
278 | - * |
|
279 | - * @return string |
|
280 | - */ |
|
281 | - public function github_edit_url() { |
|
282 | - $github_path = get_post_meta( $this->id, '_wogh_github_path', true ); |
|
283 | - $repository = $this->api->fetch()->repository(); |
|
284 | - $branch = $this->api->fetch()->branch(); |
|
285 | - |
|
286 | - return "https://github.com/$repository/edit/$branch/$github_path"; |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * Retrieve post type directory from blob path. |
|
291 | - * |
|
292 | - * @param string $path Path string. |
|
293 | - * |
|
294 | - * @return string |
|
295 | - */ |
|
296 | - public function get_directory_from_path( $path ) { |
|
297 | - $directory = explode( '/', $path ); |
|
298 | - $directory = count( $directory ) > 0 ? $directory[0] : ''; |
|
299 | - |
|
300 | - return $directory; |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Determines the last author to modify the post |
|
305 | - * |
|
306 | - * Returns Array an array containing the author name and email |
|
307 | - */ |
|
308 | - public function last_modified_author() { |
|
309 | - if ( $last_id = get_post_meta( $this->id, '_edit_last', true ) ) { |
|
310 | - $user = get_userdata( $last_id ); |
|
311 | - |
|
312 | - if ( $user ) { |
|
313 | - return array( 'name' => $user->display_name, 'email' => $user->user_email ); |
|
314 | - } |
|
315 | - } |
|
316 | - |
|
317 | - return array(); |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * The post's sha |
|
322 | - * Cached as post meta, or will make a live call if need be |
|
323 | - * |
|
324 | - * Returns String the sha1 hash |
|
325 | - */ |
|
326 | - public function sha() { |
|
327 | - $sha = get_post_meta( $this->id, '_wogh_sha', true ); |
|
328 | - |
|
329 | - // If we've done a full export and we have no sha |
|
330 | - // then we should try a live check to see if it exists. |
|
331 | - // if ( ! $sha && 'yes' === get_option( '_wogh_fully_exported' ) ) { |
|
332 | - |
|
333 | - // // @todo could we eliminate this by calling down the full tree and searching it |
|
334 | - // $data = $this->api->fetch()->remote_contents( $this ); |
|
335 | - |
|
336 | - // if ( ! is_wp_error( $data ) ) { |
|
337 | - // update_post_meta( $this->id, '_wogh_sha', $data->sha ); |
|
338 | - // $sha = $data->sha; |
|
339 | - // } |
|
340 | - // } |
|
341 | - |
|
342 | - // if the sha still doesn't exist, then it's empty |
|
343 | - if ( ! $sha || is_wp_error( $sha ) ) { |
|
344 | - $sha = ''; |
|
345 | - } |
|
346 | - |
|
347 | - return $sha; |
|
348 | - } |
|
349 | - |
|
350 | - /** |
|
351 | - * Save the sha to post |
|
352 | - * |
|
353 | - * @param string $sha |
|
354 | - */ |
|
355 | - public function set_sha( $sha ) { |
|
356 | - update_post_meta( $this->id, '_wogh_sha', $sha ); |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * The post's metadata |
|
361 | - * |
|
362 | - * Returns Array the post's metadata |
|
363 | - */ |
|
364 | - public function meta() { |
|
365 | - $meta = array( |
|
366 | - 'ID' => $this->id, |
|
367 | - 'post_title' => get_the_title( $this->post ), |
|
368 | - 'post_name' => $this->post->post_name, |
|
369 | - 'author' => ( $author = get_userdata( $this->post->post_author ) ) ? $author->display_name : '', |
|
370 | - 'post_date' => $this->post->post_date, |
|
371 | - 'post_excerpt' => $this->post->post_excerpt, |
|
372 | - 'layout' => get_post_type( $this->post ), |
|
373 | - 'link' => get_permalink( $this->post ), |
|
374 | - 'published' => 'publish' === $this->status() ? true : false, |
|
375 | - 'tags' => wp_get_post_tags( $this->id, array( 'fields' => 'names' ) ), |
|
376 | - 'categories' => wp_get_post_categories( $this->id, array( 'fields' => 'names' ) ) |
|
377 | - ); |
|
378 | - if ( empty($this->post->post_name) ) { |
|
379 | - unset($meta['post_name']); |
|
380 | - } |
|
381 | - if ( empty($this->post->post_excerpt) ) { |
|
382 | - unset($meta['post_excerpt']); |
|
383 | - } |
|
384 | - if ( 'yes' == get_option('wogh_ignore_author') ) { |
|
385 | - unset($meta['author']); |
|
386 | - } |
|
387 | - |
|
388 | - //convert traditional post_meta values, hide hidden values, skip already populated values |
|
389 | - // foreach ( get_post_custom( $this->id ) as $key => $value ) { |
|
390 | - |
|
391 | - // if ( '_' === substr( $key, 0, 1 ) || isset( $meta[ $key ] ) ) { |
|
392 | - // continue; |
|
393 | - // } |
|
394 | - |
|
395 | - // $meta[ $key ] = $value; |
|
396 | - |
|
397 | - // } |
|
398 | - |
|
399 | - return apply_filters( 'wogh_post_meta', $meta, $this ); |
|
400 | - } |
|
401 | - |
|
402 | - /** |
|
403 | - * Returns whether the Post has been saved in the DB yet. |
|
404 | - * |
|
405 | - * @return bool |
|
406 | - */ |
|
407 | - public function is_new() { |
|
408 | - return $this->new; |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * Sets the Post's meta. |
|
413 | - * |
|
414 | - * @param array $meta |
|
415 | - */ |
|
416 | - public function set_meta( $meta ) { |
|
417 | - $this->meta = $meta; |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * Returns the Post's arguments. |
|
422 | - * |
|
423 | - * @return array |
|
424 | - */ |
|
425 | - public function get_args() { |
|
426 | - return $this->args; |
|
427 | - } |
|
428 | - |
|
429 | - /** |
|
430 | - * Returns the Post's meta. |
|
431 | - * |
|
432 | - * @return array |
|
433 | - */ |
|
434 | - public function get_meta() { |
|
435 | - return $this->meta; |
|
436 | - } |
|
437 | - |
|
438 | - /** |
|
439 | - * Get the blob |
|
440 | - * @return Writing_On_GitHub_Blob |
|
441 | - */ |
|
442 | - public function get_blob() { |
|
443 | - return $this->blob; |
|
444 | - } |
|
445 | - |
|
446 | - /** |
|
447 | - * Set the blob |
|
448 | - * @param Writing_On_GitHub_Blob $blob |
|
449 | - */ |
|
450 | - public function set_blob( Writing_On_GitHub_Blob $blob ) { |
|
451 | - $this->blob = $blob; |
|
452 | - } |
|
453 | - |
|
454 | - /** |
|
455 | - * Sets the Post's WP_Post object. |
|
456 | - * |
|
457 | - * @param WP_Post $post |
|
458 | - * |
|
459 | - * @return $this |
|
460 | - */ |
|
461 | - public function set_post( WP_Post $post ) { |
|
462 | - $this->post = $post; |
|
463 | - $this->id = $post->ID; |
|
464 | - |
|
465 | - return $this; |
|
466 | - } |
|
467 | - |
|
468 | - /** |
|
469 | - * Transforms the Post into a Blob. |
|
470 | - * |
|
471 | - * @return Writing_On_GitHub_Blob |
|
472 | - */ |
|
473 | - public function to_blob() { |
|
474 | - $data = new stdClass; |
|
475 | - |
|
476 | - $data->path = $this->github_path(); |
|
477 | - $data->content = $this->github_content(); |
|
478 | - $data->sha = $this->sha(); |
|
479 | - |
|
480 | - return new Writing_On_GitHub_Blob( $data ); |
|
481 | - } |
|
12 | + /** |
|
13 | + * Api object |
|
14 | + * |
|
15 | + * @var Writing_On_GitHub_Api |
|
16 | + */ |
|
17 | + public $api; |
|
18 | + |
|
19 | + /** |
|
20 | + * Post ID |
|
21 | + * @var integer |
|
22 | + */ |
|
23 | + public $id = 0; |
|
24 | + |
|
25 | + /** |
|
26 | + * Blob object |
|
27 | + * @var Writing_On_GitHub_Blob |
|
28 | + */ |
|
29 | + public $blob; |
|
30 | + |
|
31 | + /** |
|
32 | + * Post object |
|
33 | + * @var WP_Post |
|
34 | + */ |
|
35 | + public $post; |
|
36 | + |
|
37 | + /** |
|
38 | + * Post args. |
|
39 | + * |
|
40 | + * @var array |
|
41 | + */ |
|
42 | + protected $args; |
|
43 | + |
|
44 | + /** |
|
45 | + * Post meta. |
|
46 | + * |
|
47 | + * @var array |
|
48 | + */ |
|
49 | + protected $meta; |
|
50 | + |
|
51 | + /** |
|
52 | + * Whether the post has been saved. |
|
53 | + * |
|
54 | + * @var bool |
|
55 | + */ |
|
56 | + protected $new = true; |
|
57 | + |
|
58 | + |
|
59 | + protected $old_github_path; |
|
60 | + |
|
61 | + /** |
|
62 | + * Instantiates a new Post object |
|
63 | + * |
|
64 | + * @param int|array $id_or_args Either a post ID or an array of arguments. |
|
65 | + * @param Writing_On_GitHub_Api $api API object. |
|
66 | + * |
|
67 | + * @todo remove database operations from this method |
|
68 | + */ |
|
69 | + public function __construct( $id_or_args, Writing_On_GitHub_Api $api ) { |
|
70 | + $this->api = $api; |
|
71 | + |
|
72 | + if ( is_numeric( $id_or_args ) ) { |
|
73 | + $this->id = (int) $id_or_args; |
|
74 | + $this->post = get_post( $this->id ); |
|
75 | + $this->new = false; |
|
76 | + } |
|
77 | + |
|
78 | + if ( is_array( $id_or_args ) ) { |
|
79 | + $this->args = $id_or_args; |
|
80 | + |
|
81 | + if ( isset( $this->args['ID'] ) ) { |
|
82 | + $this->post = get_post( $this->args['ID'] ); |
|
83 | + |
|
84 | + if ( $this->post ) { |
|
85 | + $this->id = $this->post->ID; |
|
86 | + $this->new = false; |
|
87 | + } else { |
|
88 | + unset( $this->args['ID'] ); |
|
89 | + } |
|
90 | + } |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + public function id() { |
|
95 | + return $this->id; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Returns the post type |
|
100 | + */ |
|
101 | + public function type() { |
|
102 | + return $this->post->post_type; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Returns the post type |
|
107 | + */ |
|
108 | + public function status() { |
|
109 | + return $this->post->post_status; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Returns the post name |
|
114 | + */ |
|
115 | + public function name() { |
|
116 | + return $this->post->post_name; |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Returns true if the post has a password |
|
121 | + * @return bool |
|
122 | + */ |
|
123 | + public function has_password() { |
|
124 | + return ! empty( $this->post->post_password ); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Combines the 2 content parts for GitHub |
|
129 | + */ |
|
130 | + public function github_content() { |
|
131 | + $content = $this->front_matter() . $this->post_content(); |
|
132 | + $ending = apply_filters( 'wogh_line_endings', "\n" ); |
|
133 | + |
|
134 | + return preg_replace( '~(*BSR_ANYCRLF)\R~', $ending, $content ); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * The post's YAML frontmatter |
|
139 | + * |
|
140 | + * Returns String the YAML frontmatter, ready to be written to the file |
|
141 | + */ |
|
142 | + public function front_matter() { |
|
143 | + return "---\n" . spyc_dump( $this->meta() ) . "---\n"; |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Returns the post_content |
|
148 | + * |
|
149 | + * Markdownify's the content if applicable |
|
150 | + */ |
|
151 | + public function post_content() { |
|
152 | + $content = $this->post->post_content; |
|
153 | + |
|
154 | + if ( function_exists( 'wpmarkdown_html_to_markdown' ) ) { |
|
155 | + $content = wpmarkdown_html_to_markdown( $content ); |
|
156 | + } else if ( class_exists( 'WPCom_Markdown' ) ) { |
|
157 | + if ( WPCom_Markdown::get_instance()->is_markdown( $this->post->ID ) ) { |
|
158 | + $content = $this->post->post_content_filtered; |
|
159 | + } |
|
160 | + } |
|
161 | + |
|
162 | + return apply_filters( 'wogh_content_export', $content, $this ); |
|
163 | + } |
|
164 | + |
|
165 | + public function old_github_path() { |
|
166 | + return $this->old_github_path; |
|
167 | + } |
|
168 | + |
|
169 | + public function set_old_github_path( $path ) { |
|
170 | + $this->old_github_path = $path; |
|
171 | + update_post_meta( $this->id, '_wogh_github_path', $path ); |
|
172 | + } |
|
173 | + |
|
174 | + |
|
175 | + /** |
|
176 | + * Retrieves or calculates the proper GitHub path for a given post |
|
177 | + * |
|
178 | + * Returns (string) the path relative to repo root |
|
179 | + */ |
|
180 | + public function github_path() { |
|
181 | + $path = $this->github_directory() . $this->github_filename(); |
|
182 | + |
|
183 | + return $path; |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Get GitHub directory based on post |
|
188 | + * |
|
189 | + * @return string |
|
190 | + */ |
|
191 | + public function github_directory() { |
|
192 | + if ( 'publish' !== $this->status() ) { |
|
193 | + return apply_filters( 'wogh_directory_unpublished', '_drafts/', $this ); |
|
194 | + } |
|
195 | + |
|
196 | + $name = ''; |
|
197 | + |
|
198 | + switch ( $this->type() ) { |
|
199 | + case 'post': |
|
200 | + $name = 'posts'; |
|
201 | + break; |
|
202 | + case 'page': |
|
203 | + $name = 'pages'; |
|
204 | + break; |
|
205 | + default: |
|
206 | + $obj = get_post_type_object( $this->type() ); |
|
207 | + |
|
208 | + if ( $obj ) { |
|
209 | + $name = strtolower( $obj->labels->name ); |
|
210 | + } |
|
211 | + |
|
212 | + if ( ! $name ) { |
|
213 | + $name = ''; |
|
214 | + } |
|
215 | + } |
|
216 | + |
|
217 | + if ( $name ) { |
|
218 | + $name = '_' . $name . '/'; |
|
219 | + } |
|
220 | + |
|
221 | + return apply_filters( 'wogh_directory_published', $name, $this ); |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * Build GitHub filename based on post |
|
226 | + */ |
|
227 | + public function github_filename() { |
|
228 | + if ( 'post' === $this->type() ) { |
|
229 | + $filename = get_the_time( 'Y-m-d-', $this->id ) . $this->get_name() . '.md'; |
|
230 | + } else { |
|
231 | + $filename = $this->get_name() . '.md'; |
|
232 | + } |
|
233 | + |
|
234 | + return apply_filters( 'wogh_filename', $filename, $this ); |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * Returns a post slug we can use in the GitHub filename |
|
239 | + * |
|
240 | + * @return string |
|
241 | + */ |
|
242 | + protected function get_name() { |
|
243 | + if ( '' !== $this->name() ) { |
|
244 | + return $this->name(); |
|
245 | + } |
|
246 | + |
|
247 | + return sanitize_title( get_the_title( $this->post ) ); |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * is put on github |
|
252 | + * @return boolean |
|
253 | + */ |
|
254 | + public function is_on_github() { |
|
255 | + $sha = get_post_meta( $this->id, '_wogh_sha', true ); |
|
256 | + $github_path = get_post_meta( $this->id, '_wogh_github_path', true ); |
|
257 | + if ( $sha && $github_path ) { |
|
258 | + return true; |
|
259 | + } |
|
260 | + return false; |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * Returns the URL for the post on GitHub. |
|
265 | + * |
|
266 | + * @return string |
|
267 | + */ |
|
268 | + public function github_view_url() { |
|
269 | + $github_path = get_post_meta( $this->id, '_wogh_github_path', true ); |
|
270 | + $repository = $this->api->fetch()->repository(); |
|
271 | + $branch = $this->api->fetch()->branch(); |
|
272 | + |
|
273 | + return "https://github.com/$repository/blob/$branch/$github_path"; |
|
274 | + } |
|
275 | + |
|
276 | + /** |
|
277 | + * Returns the URL for the post on GitHub. |
|
278 | + * |
|
279 | + * @return string |
|
280 | + */ |
|
281 | + public function github_edit_url() { |
|
282 | + $github_path = get_post_meta( $this->id, '_wogh_github_path', true ); |
|
283 | + $repository = $this->api->fetch()->repository(); |
|
284 | + $branch = $this->api->fetch()->branch(); |
|
285 | + |
|
286 | + return "https://github.com/$repository/edit/$branch/$github_path"; |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * Retrieve post type directory from blob path. |
|
291 | + * |
|
292 | + * @param string $path Path string. |
|
293 | + * |
|
294 | + * @return string |
|
295 | + */ |
|
296 | + public function get_directory_from_path( $path ) { |
|
297 | + $directory = explode( '/', $path ); |
|
298 | + $directory = count( $directory ) > 0 ? $directory[0] : ''; |
|
299 | + |
|
300 | + return $directory; |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Determines the last author to modify the post |
|
305 | + * |
|
306 | + * Returns Array an array containing the author name and email |
|
307 | + */ |
|
308 | + public function last_modified_author() { |
|
309 | + if ( $last_id = get_post_meta( $this->id, '_edit_last', true ) ) { |
|
310 | + $user = get_userdata( $last_id ); |
|
311 | + |
|
312 | + if ( $user ) { |
|
313 | + return array( 'name' => $user->display_name, 'email' => $user->user_email ); |
|
314 | + } |
|
315 | + } |
|
316 | + |
|
317 | + return array(); |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * The post's sha |
|
322 | + * Cached as post meta, or will make a live call if need be |
|
323 | + * |
|
324 | + * Returns String the sha1 hash |
|
325 | + */ |
|
326 | + public function sha() { |
|
327 | + $sha = get_post_meta( $this->id, '_wogh_sha', true ); |
|
328 | + |
|
329 | + // If we've done a full export and we have no sha |
|
330 | + // then we should try a live check to see if it exists. |
|
331 | + // if ( ! $sha && 'yes' === get_option( '_wogh_fully_exported' ) ) { |
|
332 | + |
|
333 | + // // @todo could we eliminate this by calling down the full tree and searching it |
|
334 | + // $data = $this->api->fetch()->remote_contents( $this ); |
|
335 | + |
|
336 | + // if ( ! is_wp_error( $data ) ) { |
|
337 | + // update_post_meta( $this->id, '_wogh_sha', $data->sha ); |
|
338 | + // $sha = $data->sha; |
|
339 | + // } |
|
340 | + // } |
|
341 | + |
|
342 | + // if the sha still doesn't exist, then it's empty |
|
343 | + if ( ! $sha || is_wp_error( $sha ) ) { |
|
344 | + $sha = ''; |
|
345 | + } |
|
346 | + |
|
347 | + return $sha; |
|
348 | + } |
|
349 | + |
|
350 | + /** |
|
351 | + * Save the sha to post |
|
352 | + * |
|
353 | + * @param string $sha |
|
354 | + */ |
|
355 | + public function set_sha( $sha ) { |
|
356 | + update_post_meta( $this->id, '_wogh_sha', $sha ); |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * The post's metadata |
|
361 | + * |
|
362 | + * Returns Array the post's metadata |
|
363 | + */ |
|
364 | + public function meta() { |
|
365 | + $meta = array( |
|
366 | + 'ID' => $this->id, |
|
367 | + 'post_title' => get_the_title( $this->post ), |
|
368 | + 'post_name' => $this->post->post_name, |
|
369 | + 'author' => ( $author = get_userdata( $this->post->post_author ) ) ? $author->display_name : '', |
|
370 | + 'post_date' => $this->post->post_date, |
|
371 | + 'post_excerpt' => $this->post->post_excerpt, |
|
372 | + 'layout' => get_post_type( $this->post ), |
|
373 | + 'link' => get_permalink( $this->post ), |
|
374 | + 'published' => 'publish' === $this->status() ? true : false, |
|
375 | + 'tags' => wp_get_post_tags( $this->id, array( 'fields' => 'names' ) ), |
|
376 | + 'categories' => wp_get_post_categories( $this->id, array( 'fields' => 'names' ) ) |
|
377 | + ); |
|
378 | + if ( empty($this->post->post_name) ) { |
|
379 | + unset($meta['post_name']); |
|
380 | + } |
|
381 | + if ( empty($this->post->post_excerpt) ) { |
|
382 | + unset($meta['post_excerpt']); |
|
383 | + } |
|
384 | + if ( 'yes' == get_option('wogh_ignore_author') ) { |
|
385 | + unset($meta['author']); |
|
386 | + } |
|
387 | + |
|
388 | + //convert traditional post_meta values, hide hidden values, skip already populated values |
|
389 | + // foreach ( get_post_custom( $this->id ) as $key => $value ) { |
|
390 | + |
|
391 | + // if ( '_' === substr( $key, 0, 1 ) || isset( $meta[ $key ] ) ) { |
|
392 | + // continue; |
|
393 | + // } |
|
394 | + |
|
395 | + // $meta[ $key ] = $value; |
|
396 | + |
|
397 | + // } |
|
398 | + |
|
399 | + return apply_filters( 'wogh_post_meta', $meta, $this ); |
|
400 | + } |
|
401 | + |
|
402 | + /** |
|
403 | + * Returns whether the Post has been saved in the DB yet. |
|
404 | + * |
|
405 | + * @return bool |
|
406 | + */ |
|
407 | + public function is_new() { |
|
408 | + return $this->new; |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * Sets the Post's meta. |
|
413 | + * |
|
414 | + * @param array $meta |
|
415 | + */ |
|
416 | + public function set_meta( $meta ) { |
|
417 | + $this->meta = $meta; |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * Returns the Post's arguments. |
|
422 | + * |
|
423 | + * @return array |
|
424 | + */ |
|
425 | + public function get_args() { |
|
426 | + return $this->args; |
|
427 | + } |
|
428 | + |
|
429 | + /** |
|
430 | + * Returns the Post's meta. |
|
431 | + * |
|
432 | + * @return array |
|
433 | + */ |
|
434 | + public function get_meta() { |
|
435 | + return $this->meta; |
|
436 | + } |
|
437 | + |
|
438 | + /** |
|
439 | + * Get the blob |
|
440 | + * @return Writing_On_GitHub_Blob |
|
441 | + */ |
|
442 | + public function get_blob() { |
|
443 | + return $this->blob; |
|
444 | + } |
|
445 | + |
|
446 | + /** |
|
447 | + * Set the blob |
|
448 | + * @param Writing_On_GitHub_Blob $blob |
|
449 | + */ |
|
450 | + public function set_blob( Writing_On_GitHub_Blob $blob ) { |
|
451 | + $this->blob = $blob; |
|
452 | + } |
|
453 | + |
|
454 | + /** |
|
455 | + * Sets the Post's WP_Post object. |
|
456 | + * |
|
457 | + * @param WP_Post $post |
|
458 | + * |
|
459 | + * @return $this |
|
460 | + */ |
|
461 | + public function set_post( WP_Post $post ) { |
|
462 | + $this->post = $post; |
|
463 | + $this->id = $post->ID; |
|
464 | + |
|
465 | + return $this; |
|
466 | + } |
|
467 | + |
|
468 | + /** |
|
469 | + * Transforms the Post into a Blob. |
|
470 | + * |
|
471 | + * @return Writing_On_GitHub_Blob |
|
472 | + */ |
|
473 | + public function to_blob() { |
|
474 | + $data = new stdClass; |
|
475 | + |
|
476 | + $data->path = $this->github_path(); |
|
477 | + $data->content = $this->github_content(); |
|
478 | + $data->sha = $this->sha(); |
|
479 | + |
|
480 | + return new Writing_On_GitHub_Blob( $data ); |
|
481 | + } |
|
482 | 482 | } |
@@ -66,26 +66,26 @@ discard block |
||
66 | 66 | * |
67 | 67 | * @todo remove database operations from this method |
68 | 68 | */ |
69 | - public function __construct( $id_or_args, Writing_On_GitHub_Api $api ) { |
|
69 | + public function __construct($id_or_args, Writing_On_GitHub_Api $api) { |
|
70 | 70 | $this->api = $api; |
71 | 71 | |
72 | - if ( is_numeric( $id_or_args ) ) { |
|
72 | + if (is_numeric($id_or_args)) { |
|
73 | 73 | $this->id = (int) $id_or_args; |
74 | - $this->post = get_post( $this->id ); |
|
74 | + $this->post = get_post($this->id); |
|
75 | 75 | $this->new = false; |
76 | 76 | } |
77 | 77 | |
78 | - if ( is_array( $id_or_args ) ) { |
|
78 | + if (is_array($id_or_args)) { |
|
79 | 79 | $this->args = $id_or_args; |
80 | 80 | |
81 | - if ( isset( $this->args['ID'] ) ) { |
|
82 | - $this->post = get_post( $this->args['ID'] ); |
|
81 | + if (isset($this->args['ID'])) { |
|
82 | + $this->post = get_post($this->args['ID']); |
|
83 | 83 | |
84 | - if ( $this->post ) { |
|
84 | + if ($this->post) { |
|
85 | 85 | $this->id = $this->post->ID; |
86 | 86 | $this->new = false; |
87 | 87 | } else { |
88 | - unset( $this->args['ID'] ); |
|
88 | + unset($this->args['ID']); |
|
89 | 89 | } |
90 | 90 | } |
91 | 91 | } |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | * @return bool |
122 | 122 | */ |
123 | 123 | public function has_password() { |
124 | - return ! empty( $this->post->post_password ); |
|
124 | + return ! empty($this->post->post_password); |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | /** |
@@ -129,9 +129,9 @@ discard block |
||
129 | 129 | */ |
130 | 130 | public function github_content() { |
131 | 131 | $content = $this->front_matter() . $this->post_content(); |
132 | - $ending = apply_filters( 'wogh_line_endings', "\n" ); |
|
132 | + $ending = apply_filters('wogh_line_endings', "\n"); |
|
133 | 133 | |
134 | - return preg_replace( '~(*BSR_ANYCRLF)\R~', $ending, $content ); |
|
134 | + return preg_replace('~(*BSR_ANYCRLF)\R~', $ending, $content); |
|
135 | 135 | } |
136 | 136 | |
137 | 137 | /** |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | * Returns String the YAML frontmatter, ready to be written to the file |
141 | 141 | */ |
142 | 142 | public function front_matter() { |
143 | - return "---\n" . spyc_dump( $this->meta() ) . "---\n"; |
|
143 | + return "---\n" . spyc_dump($this->meta()) . "---\n"; |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | /** |
@@ -151,24 +151,24 @@ discard block |
||
151 | 151 | public function post_content() { |
152 | 152 | $content = $this->post->post_content; |
153 | 153 | |
154 | - if ( function_exists( 'wpmarkdown_html_to_markdown' ) ) { |
|
155 | - $content = wpmarkdown_html_to_markdown( $content ); |
|
156 | - } else if ( class_exists( 'WPCom_Markdown' ) ) { |
|
157 | - if ( WPCom_Markdown::get_instance()->is_markdown( $this->post->ID ) ) { |
|
154 | + if (function_exists('wpmarkdown_html_to_markdown')) { |
|
155 | + $content = wpmarkdown_html_to_markdown($content); |
|
156 | + } else if (class_exists('WPCom_Markdown')) { |
|
157 | + if (WPCom_Markdown::get_instance()->is_markdown($this->post->ID)) { |
|
158 | 158 | $content = $this->post->post_content_filtered; |
159 | 159 | } |
160 | 160 | } |
161 | 161 | |
162 | - return apply_filters( 'wogh_content_export', $content, $this ); |
|
162 | + return apply_filters('wogh_content_export', $content, $this); |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | public function old_github_path() { |
166 | 166 | return $this->old_github_path; |
167 | 167 | } |
168 | 168 | |
169 | - public function set_old_github_path( $path ) { |
|
169 | + public function set_old_github_path($path) { |
|
170 | 170 | $this->old_github_path = $path; |
171 | - update_post_meta( $this->id, '_wogh_github_path', $path ); |
|
171 | + update_post_meta($this->id, '_wogh_github_path', $path); |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | |
@@ -189,13 +189,13 @@ discard block |
||
189 | 189 | * @return string |
190 | 190 | */ |
191 | 191 | public function github_directory() { |
192 | - if ( 'publish' !== $this->status() ) { |
|
193 | - return apply_filters( 'wogh_directory_unpublished', '_drafts/', $this ); |
|
192 | + if ('publish' !== $this->status()) { |
|
193 | + return apply_filters('wogh_directory_unpublished', '_drafts/', $this); |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | $name = ''; |
197 | 197 | |
198 | - switch ( $this->type() ) { |
|
198 | + switch ($this->type()) { |
|
199 | 199 | case 'post': |
200 | 200 | $name = 'posts'; |
201 | 201 | break; |
@@ -203,35 +203,35 @@ discard block |
||
203 | 203 | $name = 'pages'; |
204 | 204 | break; |
205 | 205 | default: |
206 | - $obj = get_post_type_object( $this->type() ); |
|
206 | + $obj = get_post_type_object($this->type()); |
|
207 | 207 | |
208 | - if ( $obj ) { |
|
209 | - $name = strtolower( $obj->labels->name ); |
|
208 | + if ($obj) { |
|
209 | + $name = strtolower($obj->labels->name); |
|
210 | 210 | } |
211 | 211 | |
212 | - if ( ! $name ) { |
|
212 | + if ( ! $name) { |
|
213 | 213 | $name = ''; |
214 | 214 | } |
215 | 215 | } |
216 | 216 | |
217 | - if ( $name ) { |
|
217 | + if ($name) { |
|
218 | 218 | $name = '_' . $name . '/'; |
219 | 219 | } |
220 | 220 | |
221 | - return apply_filters( 'wogh_directory_published', $name, $this ); |
|
221 | + return apply_filters('wogh_directory_published', $name, $this); |
|
222 | 222 | } |
223 | 223 | |
224 | 224 | /** |
225 | 225 | * Build GitHub filename based on post |
226 | 226 | */ |
227 | 227 | public function github_filename() { |
228 | - if ( 'post' === $this->type() ) { |
|
229 | - $filename = get_the_time( 'Y-m-d-', $this->id ) . $this->get_name() . '.md'; |
|
228 | + if ('post' === $this->type()) { |
|
229 | + $filename = get_the_time('Y-m-d-', $this->id) . $this->get_name() . '.md'; |
|
230 | 230 | } else { |
231 | 231 | $filename = $this->get_name() . '.md'; |
232 | 232 | } |
233 | 233 | |
234 | - return apply_filters( 'wogh_filename', $filename, $this ); |
|
234 | + return apply_filters('wogh_filename', $filename, $this); |
|
235 | 235 | } |
236 | 236 | |
237 | 237 | /** |
@@ -240,11 +240,11 @@ discard block |
||
240 | 240 | * @return string |
241 | 241 | */ |
242 | 242 | protected function get_name() { |
243 | - if ( '' !== $this->name() ) { |
|
243 | + if ('' !== $this->name()) { |
|
244 | 244 | return $this->name(); |
245 | 245 | } |
246 | 246 | |
247 | - return sanitize_title( get_the_title( $this->post ) ); |
|
247 | + return sanitize_title(get_the_title($this->post)); |
|
248 | 248 | } |
249 | 249 | |
250 | 250 | /** |
@@ -252,9 +252,9 @@ discard block |
||
252 | 252 | * @return boolean |
253 | 253 | */ |
254 | 254 | public function is_on_github() { |
255 | - $sha = get_post_meta( $this->id, '_wogh_sha', true ); |
|
256 | - $github_path = get_post_meta( $this->id, '_wogh_github_path', true ); |
|
257 | - if ( $sha && $github_path ) { |
|
255 | + $sha = get_post_meta($this->id, '_wogh_sha', true); |
|
256 | + $github_path = get_post_meta($this->id, '_wogh_github_path', true); |
|
257 | + if ($sha && $github_path) { |
|
258 | 258 | return true; |
259 | 259 | } |
260 | 260 | return false; |
@@ -266,7 +266,7 @@ discard block |
||
266 | 266 | * @return string |
267 | 267 | */ |
268 | 268 | public function github_view_url() { |
269 | - $github_path = get_post_meta( $this->id, '_wogh_github_path', true ); |
|
269 | + $github_path = get_post_meta($this->id, '_wogh_github_path', true); |
|
270 | 270 | $repository = $this->api->fetch()->repository(); |
271 | 271 | $branch = $this->api->fetch()->branch(); |
272 | 272 | |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | * @return string |
280 | 280 | */ |
281 | 281 | public function github_edit_url() { |
282 | - $github_path = get_post_meta( $this->id, '_wogh_github_path', true ); |
|
282 | + $github_path = get_post_meta($this->id, '_wogh_github_path', true); |
|
283 | 283 | $repository = $this->api->fetch()->repository(); |
284 | 284 | $branch = $this->api->fetch()->branch(); |
285 | 285 | |
@@ -293,9 +293,9 @@ discard block |
||
293 | 293 | * |
294 | 294 | * @return string |
295 | 295 | */ |
296 | - public function get_directory_from_path( $path ) { |
|
297 | - $directory = explode( '/', $path ); |
|
298 | - $directory = count( $directory ) > 0 ? $directory[0] : ''; |
|
296 | + public function get_directory_from_path($path) { |
|
297 | + $directory = explode('/', $path); |
|
298 | + $directory = count($directory) > 0 ? $directory[0] : ''; |
|
299 | 299 | |
300 | 300 | return $directory; |
301 | 301 | } |
@@ -306,11 +306,11 @@ discard block |
||
306 | 306 | * Returns Array an array containing the author name and email |
307 | 307 | */ |
308 | 308 | public function last_modified_author() { |
309 | - if ( $last_id = get_post_meta( $this->id, '_edit_last', true ) ) { |
|
310 | - $user = get_userdata( $last_id ); |
|
309 | + if ($last_id = get_post_meta($this->id, '_edit_last', true)) { |
|
310 | + $user = get_userdata($last_id); |
|
311 | 311 | |
312 | - if ( $user ) { |
|
313 | - return array( 'name' => $user->display_name, 'email' => $user->user_email ); |
|
312 | + if ($user) { |
|
313 | + return array('name' => $user->display_name, 'email' => $user->user_email); |
|
314 | 314 | } |
315 | 315 | } |
316 | 316 | |
@@ -324,7 +324,7 @@ discard block |
||
324 | 324 | * Returns String the sha1 hash |
325 | 325 | */ |
326 | 326 | public function sha() { |
327 | - $sha = get_post_meta( $this->id, '_wogh_sha', true ); |
|
327 | + $sha = get_post_meta($this->id, '_wogh_sha', true); |
|
328 | 328 | |
329 | 329 | // If we've done a full export and we have no sha |
330 | 330 | // then we should try a live check to see if it exists. |
@@ -340,7 +340,7 @@ discard block |
||
340 | 340 | // } |
341 | 341 | |
342 | 342 | // if the sha still doesn't exist, then it's empty |
343 | - if ( ! $sha || is_wp_error( $sha ) ) { |
|
343 | + if ( ! $sha || is_wp_error($sha)) { |
|
344 | 344 | $sha = ''; |
345 | 345 | } |
346 | 346 | |
@@ -352,8 +352,8 @@ discard block |
||
352 | 352 | * |
353 | 353 | * @param string $sha |
354 | 354 | */ |
355 | - public function set_sha( $sha ) { |
|
356 | - update_post_meta( $this->id, '_wogh_sha', $sha ); |
|
355 | + public function set_sha($sha) { |
|
356 | + update_post_meta($this->id, '_wogh_sha', $sha); |
|
357 | 357 | } |
358 | 358 | |
359 | 359 | /** |
@@ -364,24 +364,24 @@ discard block |
||
364 | 364 | public function meta() { |
365 | 365 | $meta = array( |
366 | 366 | 'ID' => $this->id, |
367 | - 'post_title' => get_the_title( $this->post ), |
|
367 | + 'post_title' => get_the_title($this->post), |
|
368 | 368 | 'post_name' => $this->post->post_name, |
369 | - 'author' => ( $author = get_userdata( $this->post->post_author ) ) ? $author->display_name : '', |
|
369 | + 'author' => ($author = get_userdata($this->post->post_author)) ? $author->display_name : '', |
|
370 | 370 | 'post_date' => $this->post->post_date, |
371 | 371 | 'post_excerpt' => $this->post->post_excerpt, |
372 | - 'layout' => get_post_type( $this->post ), |
|
373 | - 'link' => get_permalink( $this->post ), |
|
372 | + 'layout' => get_post_type($this->post), |
|
373 | + 'link' => get_permalink($this->post), |
|
374 | 374 | 'published' => 'publish' === $this->status() ? true : false, |
375 | - 'tags' => wp_get_post_tags( $this->id, array( 'fields' => 'names' ) ), |
|
376 | - 'categories' => wp_get_post_categories( $this->id, array( 'fields' => 'names' ) ) |
|
375 | + 'tags' => wp_get_post_tags($this->id, array('fields' => 'names')), |
|
376 | + 'categories' => wp_get_post_categories($this->id, array('fields' => 'names')) |
|
377 | 377 | ); |
378 | - if ( empty($this->post->post_name) ) { |
|
378 | + if (empty($this->post->post_name)) { |
|
379 | 379 | unset($meta['post_name']); |
380 | 380 | } |
381 | - if ( empty($this->post->post_excerpt) ) { |
|
381 | + if (empty($this->post->post_excerpt)) { |
|
382 | 382 | unset($meta['post_excerpt']); |
383 | 383 | } |
384 | - if ( 'yes' == get_option('wogh_ignore_author') ) { |
|
384 | + if ('yes' == get_option('wogh_ignore_author')) { |
|
385 | 385 | unset($meta['author']); |
386 | 386 | } |
387 | 387 | |
@@ -396,7 +396,7 @@ discard block |
||
396 | 396 | |
397 | 397 | // } |
398 | 398 | |
399 | - return apply_filters( 'wogh_post_meta', $meta, $this ); |
|
399 | + return apply_filters('wogh_post_meta', $meta, $this); |
|
400 | 400 | } |
401 | 401 | |
402 | 402 | /** |
@@ -413,7 +413,7 @@ discard block |
||
413 | 413 | * |
414 | 414 | * @param array $meta |
415 | 415 | */ |
416 | - public function set_meta( $meta ) { |
|
416 | + public function set_meta($meta) { |
|
417 | 417 | $this->meta = $meta; |
418 | 418 | } |
419 | 419 | |
@@ -447,7 +447,7 @@ discard block |
||
447 | 447 | * Set the blob |
448 | 448 | * @param Writing_On_GitHub_Blob $blob |
449 | 449 | */ |
450 | - public function set_blob( Writing_On_GitHub_Blob $blob ) { |
|
450 | + public function set_blob(Writing_On_GitHub_Blob $blob) { |
|
451 | 451 | $this->blob = $blob; |
452 | 452 | } |
453 | 453 | |
@@ -458,7 +458,7 @@ discard block |
||
458 | 458 | * |
459 | 459 | * @return $this |
460 | 460 | */ |
461 | - public function set_post( WP_Post $post ) { |
|
461 | + public function set_post(WP_Post $post) { |
|
462 | 462 | $this->post = $post; |
463 | 463 | $this->id = $post->ID; |
464 | 464 | |
@@ -477,6 +477,6 @@ discard block |
||
477 | 477 | $data->content = $this->github_content(); |
478 | 478 | $data->sha = $this->sha(); |
479 | 479 | |
480 | - return new Writing_On_GitHub_Blob( $data ); |
|
480 | + return new Writing_On_GitHub_Blob($data); |
|
481 | 481 | } |
482 | 482 | } |
@@ -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,5 +31,5 @@ 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 | } |
@@ -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,5 +31,5 @@ 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 | } |
@@ -9,118 +9,118 @@ |
||
9 | 9 | */ |
10 | 10 | class Writing_On_GitHub_Fetch_Client extends Writing_On_GitHub_Base_Client { |
11 | 11 | |
12 | - /** |
|
13 | - * Compare a commit by sha with master from the GitHub API |
|
14 | - * |
|
15 | - * @param string $sha Sha for commit to retrieve. |
|
16 | - * |
|
17 | - * @return Writing_On_GitHub_File_Info[]|WP_Error |
|
18 | - */ |
|
19 | - public function compare( $sha ) { |
|
20 | - // https://api.github.com/repos/litefeel/testwpsync/compare/861f87e8851b8debb78db548269d29f8da4d94ac...master |
|
21 | - $endpoint = $this->compare_endpoint(); |
|
22 | - $branch = $this->branch(); |
|
23 | - $data = $this->call( 'GET', "$endpoint/$sha...$branch" ); |
|
24 | - |
|
25 | - if ( is_wp_error( $data ) ) { |
|
26 | - return $data; |
|
27 | - } |
|
28 | - |
|
29 | - $files = array(); |
|
30 | - foreach ($data->files as $file) { |
|
31 | - $file->path = $file->filename; |
|
32 | - $files[] = new Writing_On_GitHub_File_Info($file); |
|
33 | - } |
|
34 | - |
|
35 | - return $files; |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * Calls the content API to get the post's contents and metadata |
|
40 | - * |
|
41 | - * Returns Object the response from the API |
|
42 | - * |
|
43 | - * @param Writing_On_GitHub_Post $post Post to retrieve remote contents for. |
|
44 | - * |
|
45 | - * @return mixed |
|
46 | - */ |
|
47 | - public function remote_contents( $post ) { |
|
48 | - return $this->call( 'GET', $this->content_endpoint( $post->github_path() ) ); |
|
49 | - } |
|
50 | - |
|
51 | - |
|
52 | - |
|
53 | - public function exists( $path ) { |
|
54 | - $result = $this->call( 'GET', $this->content_endpoint( $path ) ); |
|
55 | - if ( is_wp_error( $result ) ) { |
|
56 | - return false; |
|
57 | - } |
|
58 | - return true; |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Retrieves a tree by sha recursively from the GitHub API |
|
63 | - * |
|
64 | - * @param string $sha Commit sha to retrieve tree from. |
|
65 | - * |
|
66 | - * @return Writing_On_GitHub_File_Info[]|WP_Error |
|
67 | - */ |
|
68 | - public function tree_recursive( $sha = '_default' ) { |
|
69 | - |
|
70 | - if ( '_default' === $sha ) { |
|
71 | - $sha = $this->branch(); |
|
72 | - } |
|
73 | - |
|
74 | - $data = $this->call( 'GET', $this->tree_endpoint() . '/' . $sha . '?recursive=1' ); |
|
75 | - |
|
76 | - if ( is_wp_error( $data ) ) { |
|
77 | - return $data; |
|
78 | - } |
|
79 | - |
|
80 | - $files = array(); |
|
81 | - |
|
82 | - foreach ( $data->tree as $index => $thing ) { |
|
83 | - // We need to remove the trees because |
|
84 | - // the recursive tree includes both |
|
85 | - // the subtrees as well the subtrees' blobs. |
|
86 | - if ( 'blob' === $thing->type ) { |
|
87 | - $thing->status = ''; |
|
88 | - $files[] = new Writing_On_GitHub_File_Info( $thing ); |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - return $files; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Retrieves the blob data for a given sha |
|
97 | - * |
|
98 | - * @param Writing_On_GitHub_File_Info $fileinfo |
|
99 | - * |
|
100 | - * @return Writing_On_GitHub_Blob|WP_Error |
|
101 | - */ |
|
102 | - public function blob( Writing_On_GitHub_File_Info $fileinfo ) { |
|
103 | - $data = $this->call( 'GET', $this->blob_endpoint() . '/' . $fileinfo->sha ); |
|
104 | - |
|
105 | - if ( is_wp_error( $data ) ) { |
|
106 | - return $data; |
|
107 | - } |
|
108 | - |
|
109 | - $data->path = $fileinfo->path; |
|
110 | - return new Writing_On_GitHub_Blob( $data ); |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Get blob by path |
|
115 | - * @param string $path |
|
116 | - * @return Writing_On_GitHub_Blob|WP_Error |
|
117 | - */ |
|
118 | - public function blob_by_path( $path ) { |
|
119 | - $result = $this->call( 'GET', $this->content_endpoint( $path ) ); |
|
120 | - if ( is_wp_error( $result ) ) { |
|
121 | - return $result; |
|
122 | - } |
|
123 | - |
|
124 | - return new Writing_On_GitHub_Blob( $result ); |
|
125 | - } |
|
12 | + /** |
|
13 | + * Compare a commit by sha with master from the GitHub API |
|
14 | + * |
|
15 | + * @param string $sha Sha for commit to retrieve. |
|
16 | + * |
|
17 | + * @return Writing_On_GitHub_File_Info[]|WP_Error |
|
18 | + */ |
|
19 | + public function compare( $sha ) { |
|
20 | + // https://api.github.com/repos/litefeel/testwpsync/compare/861f87e8851b8debb78db548269d29f8da4d94ac...master |
|
21 | + $endpoint = $this->compare_endpoint(); |
|
22 | + $branch = $this->branch(); |
|
23 | + $data = $this->call( 'GET', "$endpoint/$sha...$branch" ); |
|
24 | + |
|
25 | + if ( is_wp_error( $data ) ) { |
|
26 | + return $data; |
|
27 | + } |
|
28 | + |
|
29 | + $files = array(); |
|
30 | + foreach ($data->files as $file) { |
|
31 | + $file->path = $file->filename; |
|
32 | + $files[] = new Writing_On_GitHub_File_Info($file); |
|
33 | + } |
|
34 | + |
|
35 | + return $files; |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * Calls the content API to get the post's contents and metadata |
|
40 | + * |
|
41 | + * Returns Object the response from the API |
|
42 | + * |
|
43 | + * @param Writing_On_GitHub_Post $post Post to retrieve remote contents for. |
|
44 | + * |
|
45 | + * @return mixed |
|
46 | + */ |
|
47 | + public function remote_contents( $post ) { |
|
48 | + return $this->call( 'GET', $this->content_endpoint( $post->github_path() ) ); |
|
49 | + } |
|
50 | + |
|
51 | + |
|
52 | + |
|
53 | + public function exists( $path ) { |
|
54 | + $result = $this->call( 'GET', $this->content_endpoint( $path ) ); |
|
55 | + if ( is_wp_error( $result ) ) { |
|
56 | + return false; |
|
57 | + } |
|
58 | + return true; |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Retrieves a tree by sha recursively from the GitHub API |
|
63 | + * |
|
64 | + * @param string $sha Commit sha to retrieve tree from. |
|
65 | + * |
|
66 | + * @return Writing_On_GitHub_File_Info[]|WP_Error |
|
67 | + */ |
|
68 | + public function tree_recursive( $sha = '_default' ) { |
|
69 | + |
|
70 | + if ( '_default' === $sha ) { |
|
71 | + $sha = $this->branch(); |
|
72 | + } |
|
73 | + |
|
74 | + $data = $this->call( 'GET', $this->tree_endpoint() . '/' . $sha . '?recursive=1' ); |
|
75 | + |
|
76 | + if ( is_wp_error( $data ) ) { |
|
77 | + return $data; |
|
78 | + } |
|
79 | + |
|
80 | + $files = array(); |
|
81 | + |
|
82 | + foreach ( $data->tree as $index => $thing ) { |
|
83 | + // We need to remove the trees because |
|
84 | + // the recursive tree includes both |
|
85 | + // the subtrees as well the subtrees' blobs. |
|
86 | + if ( 'blob' === $thing->type ) { |
|
87 | + $thing->status = ''; |
|
88 | + $files[] = new Writing_On_GitHub_File_Info( $thing ); |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + return $files; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Retrieves the blob data for a given sha |
|
97 | + * |
|
98 | + * @param Writing_On_GitHub_File_Info $fileinfo |
|
99 | + * |
|
100 | + * @return Writing_On_GitHub_Blob|WP_Error |
|
101 | + */ |
|
102 | + public function blob( Writing_On_GitHub_File_Info $fileinfo ) { |
|
103 | + $data = $this->call( 'GET', $this->blob_endpoint() . '/' . $fileinfo->sha ); |
|
104 | + |
|
105 | + if ( is_wp_error( $data ) ) { |
|
106 | + return $data; |
|
107 | + } |
|
108 | + |
|
109 | + $data->path = $fileinfo->path; |
|
110 | + return new Writing_On_GitHub_Blob( $data ); |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Get blob by path |
|
115 | + * @param string $path |
|
116 | + * @return Writing_On_GitHub_Blob|WP_Error |
|
117 | + */ |
|
118 | + public function blob_by_path( $path ) { |
|
119 | + $result = $this->call( 'GET', $this->content_endpoint( $path ) ); |
|
120 | + if ( is_wp_error( $result ) ) { |
|
121 | + return $result; |
|
122 | + } |
|
123 | + |
|
124 | + return new Writing_On_GitHub_Blob( $result ); |
|
125 | + } |
|
126 | 126 | } |
@@ -16,13 +16,13 @@ discard block |
||
16 | 16 | * |
17 | 17 | * @return Writing_On_GitHub_File_Info[]|WP_Error |
18 | 18 | */ |
19 | - public function compare( $sha ) { |
|
19 | + public function compare($sha) { |
|
20 | 20 | // https://api.github.com/repos/litefeel/testwpsync/compare/861f87e8851b8debb78db548269d29f8da4d94ac...master |
21 | 21 | $endpoint = $this->compare_endpoint(); |
22 | 22 | $branch = $this->branch(); |
23 | - $data = $this->call( 'GET', "$endpoint/$sha...$branch" ); |
|
23 | + $data = $this->call('GET', "$endpoint/$sha...$branch"); |
|
24 | 24 | |
25 | - if ( is_wp_error( $data ) ) { |
|
25 | + if (is_wp_error($data)) { |
|
26 | 26 | return $data; |
27 | 27 | } |
28 | 28 | |
@@ -44,15 +44,15 @@ discard block |
||
44 | 44 | * |
45 | 45 | * @return mixed |
46 | 46 | */ |
47 | - public function remote_contents( $post ) { |
|
48 | - return $this->call( 'GET', $this->content_endpoint( $post->github_path() ) ); |
|
47 | + public function remote_contents($post) { |
|
48 | + return $this->call('GET', $this->content_endpoint($post->github_path())); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | |
52 | 52 | |
53 | - public function exists( $path ) { |
|
54 | - $result = $this->call( 'GET', $this->content_endpoint( $path ) ); |
|
55 | - if ( is_wp_error( $result ) ) { |
|
53 | + public function exists($path) { |
|
54 | + $result = $this->call('GET', $this->content_endpoint($path)); |
|
55 | + if (is_wp_error($result)) { |
|
56 | 56 | return false; |
57 | 57 | } |
58 | 58 | return true; |
@@ -65,27 +65,27 @@ discard block |
||
65 | 65 | * |
66 | 66 | * @return Writing_On_GitHub_File_Info[]|WP_Error |
67 | 67 | */ |
68 | - public function tree_recursive( $sha = '_default' ) { |
|
68 | + public function tree_recursive($sha = '_default') { |
|
69 | 69 | |
70 | - if ( '_default' === $sha ) { |
|
70 | + if ('_default' === $sha) { |
|
71 | 71 | $sha = $this->branch(); |
72 | 72 | } |
73 | 73 | |
74 | - $data = $this->call( 'GET', $this->tree_endpoint() . '/' . $sha . '?recursive=1' ); |
|
74 | + $data = $this->call('GET', $this->tree_endpoint() . '/' . $sha . '?recursive=1'); |
|
75 | 75 | |
76 | - if ( is_wp_error( $data ) ) { |
|
76 | + if (is_wp_error($data)) { |
|
77 | 77 | return $data; |
78 | 78 | } |
79 | 79 | |
80 | 80 | $files = array(); |
81 | 81 | |
82 | - foreach ( $data->tree as $index => $thing ) { |
|
82 | + foreach ($data->tree as $index => $thing) { |
|
83 | 83 | // We need to remove the trees because |
84 | 84 | // the recursive tree includes both |
85 | 85 | // the subtrees as well the subtrees' blobs. |
86 | - if ( 'blob' === $thing->type ) { |
|
86 | + if ('blob' === $thing->type) { |
|
87 | 87 | $thing->status = ''; |
88 | - $files[] = new Writing_On_GitHub_File_Info( $thing ); |
|
88 | + $files[] = new Writing_On_GitHub_File_Info($thing); |
|
89 | 89 | } |
90 | 90 | } |
91 | 91 | |
@@ -99,15 +99,15 @@ discard block |
||
99 | 99 | * |
100 | 100 | * @return Writing_On_GitHub_Blob|WP_Error |
101 | 101 | */ |
102 | - public function blob( Writing_On_GitHub_File_Info $fileinfo ) { |
|
103 | - $data = $this->call( 'GET', $this->blob_endpoint() . '/' . $fileinfo->sha ); |
|
102 | + public function blob(Writing_On_GitHub_File_Info $fileinfo) { |
|
103 | + $data = $this->call('GET', $this->blob_endpoint() . '/' . $fileinfo->sha); |
|
104 | 104 | |
105 | - if ( is_wp_error( $data ) ) { |
|
105 | + if (is_wp_error($data)) { |
|
106 | 106 | return $data; |
107 | 107 | } |
108 | 108 | |
109 | 109 | $data->path = $fileinfo->path; |
110 | - return new Writing_On_GitHub_Blob( $data ); |
|
110 | + return new Writing_On_GitHub_Blob($data); |
|
111 | 111 | } |
112 | 112 | |
113 | 113 | /** |
@@ -115,12 +115,12 @@ discard block |
||
115 | 115 | * @param string $path |
116 | 116 | * @return Writing_On_GitHub_Blob|WP_Error |
117 | 117 | */ |
118 | - public function blob_by_path( $path ) { |
|
119 | - $result = $this->call( 'GET', $this->content_endpoint( $path ) ); |
|
120 | - if ( is_wp_error( $result ) ) { |
|
118 | + public function blob_by_path($path) { |
|
119 | + $result = $this->call('GET', $this->content_endpoint($path)); |
|
120 | + if (is_wp_error($result)) { |
|
121 | 121 | return $result; |
122 | 122 | } |
123 | 123 | |
124 | - return new Writing_On_GitHub_Blob( $result ); |
|
124 | + return new Writing_On_GitHub_Blob($result); |
|
125 | 125 | } |
126 | 126 | } |