@@ -10,275 +10,275 @@ |
||
10 | 10 | */ |
11 | 11 | class Writing_On_GitHub_Export { |
12 | 12 | |
13 | - /** |
|
14 | - * Application container. |
|
15 | - * |
|
16 | - * @var Writing_On_GitHub |
|
17 | - */ |
|
18 | - protected $app; |
|
19 | - |
|
20 | - /** |
|
21 | - * Initializes a new export manager. |
|
22 | - * |
|
23 | - * @param Writing_On_GitHub $app Application container. |
|
24 | - */ |
|
25 | - public function __construct( Writing_On_GitHub $app ) { |
|
26 | - $this->app = $app; |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * Updates all of the current posts in the database on master. |
|
31 | - * |
|
32 | - * @param bool $force |
|
33 | - * |
|
34 | - * @return string|WP_Error |
|
35 | - */ |
|
36 | - public function full( $force = false ) { |
|
37 | - $posts = $this->app->database()->fetch_all_supported( $force ); |
|
38 | - |
|
39 | - if ( is_wp_error( $posts ) ) { |
|
40 | - /* @var WP_Error $posts */ |
|
41 | - return $posts; |
|
42 | - } |
|
43 | - |
|
44 | - $error = ''; |
|
45 | - |
|
46 | - foreach ( $posts as $post ) { |
|
47 | - $result = $this->update( $post->id() ); |
|
48 | - if ( is_wp_error( $result ) ) { |
|
49 | - /* @var WP_Error $result */ |
|
50 | - $error = wogh_append_error( $error, $result ); |
|
51 | - } |
|
52 | - } |
|
53 | - |
|
54 | - if ( is_wp_error( $error ) ) { |
|
55 | - /* @var WP_Error $error */ |
|
56 | - return $error; |
|
57 | - } |
|
58 | - |
|
59 | - return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
60 | - } |
|
61 | - |
|
62 | - |
|
63 | - /** |
|
64 | - * Check if it exists in github |
|
65 | - * @param int $post_id |
|
66 | - * @return boolean |
|
67 | - */ |
|
68 | - protected function github_path( $post_id ) { |
|
69 | - $github_path = get_post_meta( $post_id, '_wogh_github_path', true ); |
|
70 | - |
|
71 | - if ( $github_path && $this->app->api()->fetch()->exists( $github_path ) ) { |
|
72 | - return $github_path; |
|
73 | - } |
|
74 | - |
|
75 | - return false; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Updates the provided post ID in master. |
|
80 | - * |
|
81 | - * @param int $post_id Post ID to update. |
|
82 | - * |
|
83 | - * @return string|WP_Error |
|
84 | - */ |
|
85 | - public function update( $post_id ) { |
|
86 | - $post = $this->app->database()->fetch_by_id( $post_id ); |
|
87 | - |
|
88 | - if ( is_wp_error( $post ) ) { |
|
89 | - /* @var WP_Error $post */ |
|
90 | - return $post; |
|
91 | - } |
|
92 | - |
|
93 | - if ( 'trash' === $post->status() ) { |
|
94 | - return $this->delete( $post_id ); |
|
95 | - } |
|
96 | - |
|
97 | - if ( $old_github_path = $this->github_path( $post->id() ) ) { |
|
98 | - error_log("old_github_path: $old_github_path"); |
|
99 | - $post->set_old_github_path($old_github_path); |
|
100 | - } |
|
101 | - |
|
102 | - $result = $this->export_post( $post ); |
|
103 | - |
|
104 | - if ( is_wp_error( $result ) ) { |
|
105 | - /* @var WP_Error $result */ |
|
106 | - return $result; |
|
107 | - } |
|
108 | - |
|
109 | - return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Post to blob |
|
114 | - * @param Writing_On_GitHub_Post $post |
|
115 | - * @return WP_Error|Writing_On_GitHub_Blob |
|
116 | - */ |
|
117 | - protected function post_to_blob( Writing_On_GitHub_Post $post ) { |
|
118 | - if ( ! $post->get_blob() |
|
119 | - && $post->old_github_path() |
|
120 | - && wogh_is_dont_export_content() ) { |
|
121 | - |
|
122 | - |
|
123 | - $blob = $this->app->api()->fetch()->blob_by_path( $post->old_github_path() ); |
|
124 | - |
|
125 | - if ( is_wp_error( $blob ) ) { |
|
126 | - /** @var WP_Error $blob */ |
|
127 | - return $blob; |
|
128 | - } |
|
129 | - |
|
130 | - $post->set_blob( $blob ); |
|
131 | - } |
|
132 | - |
|
133 | - return $post->to_blob(); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Export post to github |
|
138 | - * @param Writing_On_GitHub_Post $post |
|
139 | - * @return WP_Error|true |
|
140 | - */ |
|
141 | - public function export_post( Writing_On_GitHub_Post $post ) { |
|
142 | - // check blob |
|
143 | - $blob = $this->post_to_blob( $post ); |
|
144 | - if ( is_wp_error( $blob ) ) { |
|
145 | - /** @var WP_Error $blob */ |
|
146 | - return $blob; |
|
147 | - } |
|
148 | - |
|
149 | - $result = false; |
|
150 | - |
|
151 | - $persist = $this->app->api()->persist(); |
|
152 | - $github_path = $post->github_path(); |
|
153 | - $old_github_path = $post->old_github_path(); |
|
154 | - |
|
155 | - if ( $old_github_path && $old_github_path != $github_path ) { |
|
156 | - // rename |
|
157 | - $message = apply_filters( |
|
158 | - 'wogh_commit_msg_move_post', |
|
159 | - sprintf( |
|
160 | - 'Move %s to %s via WordPress at %s (%s)', |
|
161 | - $old_github_path, $github_path, |
|
162 | - site_url(), |
|
163 | - get_bloginfo( 'name' ) |
|
164 | - ) |
|
165 | - ) . $this->get_commit_msg_tag(); |
|
166 | - |
|
167 | - $result = $persist->delete_file( $post->old_github_path(), $blob->sha(), $message ); |
|
168 | - if ( is_wp_error( $result ) ) { |
|
169 | - return $result; |
|
170 | - } |
|
171 | - |
|
172 | - $result = $persist->create_file( $blob, $message ); |
|
173 | - if ( is_wp_error( $result ) ) { |
|
174 | - return $result; |
|
175 | - } |
|
176 | - } elseif ( ! $old_github_path ) { |
|
177 | - // create new |
|
178 | - $message = apply_filters( |
|
179 | - 'wogh_commit_msg_new_post', |
|
180 | - sprintf( |
|
181 | - 'Create new post %s from WordPress at %s (%s)', |
|
182 | - $github_path, |
|
183 | - site_url(), |
|
184 | - get_bloginfo( 'name' ) |
|
185 | - ) |
|
186 | - ) . $this->get_commit_msg_tag(); |
|
187 | - $result = $persist->create_file( $blob, $message ); |
|
188 | - if ( is_wp_error( $result ) ) { |
|
189 | - return $result; |
|
190 | - } |
|
191 | - } elseif ( $old_github_path && $old_github_path == $github_path ) { |
|
192 | - // update |
|
193 | - $sha = wogh_git_sha( $blob->content() ); |
|
194 | - if ( $sha === $blob->sha() ) { |
|
195 | - // don't export when has not changed |
|
196 | - return true; |
|
197 | - } |
|
198 | - $message = apply_filters( |
|
199 | - 'wogh_commit_msg_update_post', |
|
200 | - sprintf( |
|
201 | - 'Update post %s from WordPress at %s (%s)', |
|
202 | - $github_path, |
|
203 | - site_url(), |
|
204 | - get_bloginfo( 'name' ) |
|
205 | - ) |
|
206 | - ) . $this->get_commit_msg_tag(); |
|
207 | - $result = $persist->update_file( $blob, $message ); |
|
208 | - if ( is_wp_error( $result ) ) { |
|
209 | - return $result; |
|
210 | - } |
|
211 | - } |
|
212 | - |
|
213 | - $sha = $result->content->sha; |
|
214 | - $post->set_sha( $sha ); |
|
215 | - $post->set_old_github_path( $github_path ); |
|
216 | - |
|
217 | - return true; |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * Deletes a provided post ID from master. |
|
222 | - * |
|
223 | - * @param int $post_id Post ID to delete. |
|
224 | - * |
|
225 | - * @return string|WP_Error |
|
226 | - */ |
|
227 | - public function delete( $post_id ) { |
|
228 | - $post = $this->app->database()->fetch_by_id( $post_id ); |
|
229 | - |
|
230 | - if ( is_wp_error( $post ) ) { |
|
231 | - /* @var WP_Error $post */ |
|
232 | - return $post; |
|
233 | - } |
|
234 | - |
|
235 | - $github_path = get_post_meta( $post_id, '_wogh_github_path', true ); |
|
236 | - |
|
237 | - $message = apply_filters( |
|
238 | - 'wogh_commit_msg_delete', |
|
239 | - sprintf( |
|
240 | - 'Deleting %s via WordPress at %s (%s)', |
|
241 | - $github_path, |
|
242 | - site_url(), |
|
243 | - get_bloginfo( 'name' ) |
|
244 | - ), |
|
245 | - $post |
|
246 | - ) . $this->get_commit_msg_tag(); |
|
247 | - |
|
248 | - $result = $this->app->api()->persist()->delete_file( $github_path, $post->sha(), $message ); |
|
249 | - |
|
250 | - if ( is_wp_error( $result ) ) { |
|
251 | - /* @var WP_Error $result */ |
|
252 | - return $result; |
|
253 | - } |
|
254 | - |
|
255 | - return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
256 | - } |
|
257 | - |
|
258 | - |
|
259 | - /** |
|
260 | - * Saves the export user to the database. |
|
261 | - * |
|
262 | - * @param int $user_id User ID to export with. |
|
263 | - * |
|
264 | - * @return bool |
|
265 | - */ |
|
266 | - public function set_user( $user_id ) { |
|
267 | - return update_option( self::EXPORT_USER_OPTION, (int) $user_id ); |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * Gets the commit message tag. |
|
272 | - * |
|
273 | - * @return string |
|
274 | - */ |
|
275 | - protected function get_commit_msg_tag() { |
|
276 | - $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' ); |
|
277 | - |
|
278 | - if ( ! $tag ) { |
|
279 | - throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
|
280 | - } |
|
281 | - |
|
282 | - return ' - ' . $tag; |
|
283 | - } |
|
13 | + /** |
|
14 | + * Application container. |
|
15 | + * |
|
16 | + * @var Writing_On_GitHub |
|
17 | + */ |
|
18 | + protected $app; |
|
19 | + |
|
20 | + /** |
|
21 | + * Initializes a new export manager. |
|
22 | + * |
|
23 | + * @param Writing_On_GitHub $app Application container. |
|
24 | + */ |
|
25 | + public function __construct( Writing_On_GitHub $app ) { |
|
26 | + $this->app = $app; |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * Updates all of the current posts in the database on master. |
|
31 | + * |
|
32 | + * @param bool $force |
|
33 | + * |
|
34 | + * @return string|WP_Error |
|
35 | + */ |
|
36 | + public function full( $force = false ) { |
|
37 | + $posts = $this->app->database()->fetch_all_supported( $force ); |
|
38 | + |
|
39 | + if ( is_wp_error( $posts ) ) { |
|
40 | + /* @var WP_Error $posts */ |
|
41 | + return $posts; |
|
42 | + } |
|
43 | + |
|
44 | + $error = ''; |
|
45 | + |
|
46 | + foreach ( $posts as $post ) { |
|
47 | + $result = $this->update( $post->id() ); |
|
48 | + if ( is_wp_error( $result ) ) { |
|
49 | + /* @var WP_Error $result */ |
|
50 | + $error = wogh_append_error( $error, $result ); |
|
51 | + } |
|
52 | + } |
|
53 | + |
|
54 | + if ( is_wp_error( $error ) ) { |
|
55 | + /* @var WP_Error $error */ |
|
56 | + return $error; |
|
57 | + } |
|
58 | + |
|
59 | + return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
60 | + } |
|
61 | + |
|
62 | + |
|
63 | + /** |
|
64 | + * Check if it exists in github |
|
65 | + * @param int $post_id |
|
66 | + * @return boolean |
|
67 | + */ |
|
68 | + protected function github_path( $post_id ) { |
|
69 | + $github_path = get_post_meta( $post_id, '_wogh_github_path', true ); |
|
70 | + |
|
71 | + if ( $github_path && $this->app->api()->fetch()->exists( $github_path ) ) { |
|
72 | + return $github_path; |
|
73 | + } |
|
74 | + |
|
75 | + return false; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Updates the provided post ID in master. |
|
80 | + * |
|
81 | + * @param int $post_id Post ID to update. |
|
82 | + * |
|
83 | + * @return string|WP_Error |
|
84 | + */ |
|
85 | + public function update( $post_id ) { |
|
86 | + $post = $this->app->database()->fetch_by_id( $post_id ); |
|
87 | + |
|
88 | + if ( is_wp_error( $post ) ) { |
|
89 | + /* @var WP_Error $post */ |
|
90 | + return $post; |
|
91 | + } |
|
92 | + |
|
93 | + if ( 'trash' === $post->status() ) { |
|
94 | + return $this->delete( $post_id ); |
|
95 | + } |
|
96 | + |
|
97 | + if ( $old_github_path = $this->github_path( $post->id() ) ) { |
|
98 | + error_log("old_github_path: $old_github_path"); |
|
99 | + $post->set_old_github_path($old_github_path); |
|
100 | + } |
|
101 | + |
|
102 | + $result = $this->export_post( $post ); |
|
103 | + |
|
104 | + if ( is_wp_error( $result ) ) { |
|
105 | + /* @var WP_Error $result */ |
|
106 | + return $result; |
|
107 | + } |
|
108 | + |
|
109 | + return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Post to blob |
|
114 | + * @param Writing_On_GitHub_Post $post |
|
115 | + * @return WP_Error|Writing_On_GitHub_Blob |
|
116 | + */ |
|
117 | + protected function post_to_blob( Writing_On_GitHub_Post $post ) { |
|
118 | + if ( ! $post->get_blob() |
|
119 | + && $post->old_github_path() |
|
120 | + && wogh_is_dont_export_content() ) { |
|
121 | + |
|
122 | + |
|
123 | + $blob = $this->app->api()->fetch()->blob_by_path( $post->old_github_path() ); |
|
124 | + |
|
125 | + if ( is_wp_error( $blob ) ) { |
|
126 | + /** @var WP_Error $blob */ |
|
127 | + return $blob; |
|
128 | + } |
|
129 | + |
|
130 | + $post->set_blob( $blob ); |
|
131 | + } |
|
132 | + |
|
133 | + return $post->to_blob(); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Export post to github |
|
138 | + * @param Writing_On_GitHub_Post $post |
|
139 | + * @return WP_Error|true |
|
140 | + */ |
|
141 | + public function export_post( Writing_On_GitHub_Post $post ) { |
|
142 | + // check blob |
|
143 | + $blob = $this->post_to_blob( $post ); |
|
144 | + if ( is_wp_error( $blob ) ) { |
|
145 | + /** @var WP_Error $blob */ |
|
146 | + return $blob; |
|
147 | + } |
|
148 | + |
|
149 | + $result = false; |
|
150 | + |
|
151 | + $persist = $this->app->api()->persist(); |
|
152 | + $github_path = $post->github_path(); |
|
153 | + $old_github_path = $post->old_github_path(); |
|
154 | + |
|
155 | + if ( $old_github_path && $old_github_path != $github_path ) { |
|
156 | + // rename |
|
157 | + $message = apply_filters( |
|
158 | + 'wogh_commit_msg_move_post', |
|
159 | + sprintf( |
|
160 | + 'Move %s to %s via WordPress at %s (%s)', |
|
161 | + $old_github_path, $github_path, |
|
162 | + site_url(), |
|
163 | + get_bloginfo( 'name' ) |
|
164 | + ) |
|
165 | + ) . $this->get_commit_msg_tag(); |
|
166 | + |
|
167 | + $result = $persist->delete_file( $post->old_github_path(), $blob->sha(), $message ); |
|
168 | + if ( is_wp_error( $result ) ) { |
|
169 | + return $result; |
|
170 | + } |
|
171 | + |
|
172 | + $result = $persist->create_file( $blob, $message ); |
|
173 | + if ( is_wp_error( $result ) ) { |
|
174 | + return $result; |
|
175 | + } |
|
176 | + } elseif ( ! $old_github_path ) { |
|
177 | + // create new |
|
178 | + $message = apply_filters( |
|
179 | + 'wogh_commit_msg_new_post', |
|
180 | + sprintf( |
|
181 | + 'Create new post %s from WordPress at %s (%s)', |
|
182 | + $github_path, |
|
183 | + site_url(), |
|
184 | + get_bloginfo( 'name' ) |
|
185 | + ) |
|
186 | + ) . $this->get_commit_msg_tag(); |
|
187 | + $result = $persist->create_file( $blob, $message ); |
|
188 | + if ( is_wp_error( $result ) ) { |
|
189 | + return $result; |
|
190 | + } |
|
191 | + } elseif ( $old_github_path && $old_github_path == $github_path ) { |
|
192 | + // update |
|
193 | + $sha = wogh_git_sha( $blob->content() ); |
|
194 | + if ( $sha === $blob->sha() ) { |
|
195 | + // don't export when has not changed |
|
196 | + return true; |
|
197 | + } |
|
198 | + $message = apply_filters( |
|
199 | + 'wogh_commit_msg_update_post', |
|
200 | + sprintf( |
|
201 | + 'Update post %s from WordPress at %s (%s)', |
|
202 | + $github_path, |
|
203 | + site_url(), |
|
204 | + get_bloginfo( 'name' ) |
|
205 | + ) |
|
206 | + ) . $this->get_commit_msg_tag(); |
|
207 | + $result = $persist->update_file( $blob, $message ); |
|
208 | + if ( is_wp_error( $result ) ) { |
|
209 | + return $result; |
|
210 | + } |
|
211 | + } |
|
212 | + |
|
213 | + $sha = $result->content->sha; |
|
214 | + $post->set_sha( $sha ); |
|
215 | + $post->set_old_github_path( $github_path ); |
|
216 | + |
|
217 | + return true; |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * Deletes a provided post ID from master. |
|
222 | + * |
|
223 | + * @param int $post_id Post ID to delete. |
|
224 | + * |
|
225 | + * @return string|WP_Error |
|
226 | + */ |
|
227 | + public function delete( $post_id ) { |
|
228 | + $post = $this->app->database()->fetch_by_id( $post_id ); |
|
229 | + |
|
230 | + if ( is_wp_error( $post ) ) { |
|
231 | + /* @var WP_Error $post */ |
|
232 | + return $post; |
|
233 | + } |
|
234 | + |
|
235 | + $github_path = get_post_meta( $post_id, '_wogh_github_path', true ); |
|
236 | + |
|
237 | + $message = apply_filters( |
|
238 | + 'wogh_commit_msg_delete', |
|
239 | + sprintf( |
|
240 | + 'Deleting %s via WordPress at %s (%s)', |
|
241 | + $github_path, |
|
242 | + site_url(), |
|
243 | + get_bloginfo( 'name' ) |
|
244 | + ), |
|
245 | + $post |
|
246 | + ) . $this->get_commit_msg_tag(); |
|
247 | + |
|
248 | + $result = $this->app->api()->persist()->delete_file( $github_path, $post->sha(), $message ); |
|
249 | + |
|
250 | + if ( is_wp_error( $result ) ) { |
|
251 | + /* @var WP_Error $result */ |
|
252 | + return $result; |
|
253 | + } |
|
254 | + |
|
255 | + return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
256 | + } |
|
257 | + |
|
258 | + |
|
259 | + /** |
|
260 | + * Saves the export user to the database. |
|
261 | + * |
|
262 | + * @param int $user_id User ID to export with. |
|
263 | + * |
|
264 | + * @return bool |
|
265 | + */ |
|
266 | + public function set_user( $user_id ) { |
|
267 | + return update_option( self::EXPORT_USER_OPTION, (int) $user_id ); |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * Gets the commit message tag. |
|
272 | + * |
|
273 | + * @return string |
|
274 | + */ |
|
275 | + protected function get_commit_msg_tag() { |
|
276 | + $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' ); |
|
277 | + |
|
278 | + if ( ! $tag ) { |
|
279 | + throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
|
280 | + } |
|
281 | + |
|
282 | + return ' - ' . $tag; |
|
283 | + } |
|
284 | 284 | } |
@@ -9,247 +9,247 @@ |
||
9 | 9 | */ |
10 | 10 | class Writing_On_GitHub_Blob { |
11 | 11 | |
12 | - /** |
|
13 | - * Complete blob content. |
|
14 | - * |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - protected $content; |
|
18 | - |
|
19 | - /** |
|
20 | - * Blob sha. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - protected $sha; |
|
25 | - |
|
26 | - /** |
|
27 | - * Blob path. |
|
28 | - * |
|
29 | - * @var string |
|
30 | - */ |
|
31 | - protected $path; |
|
32 | - |
|
33 | - /** |
|
34 | - * Post id. |
|
35 | - * |
|
36 | - * @var int |
|
37 | - */ |
|
38 | - protected $id; |
|
39 | - |
|
40 | - /** |
|
41 | - * Whether the blob has frontmatter. |
|
42 | - * |
|
43 | - * @var boolean |
|
44 | - */ |
|
45 | - protected $frontmatter = false; |
|
46 | - |
|
47 | - /** |
|
48 | - * The front matter of github post |
|
49 | - * @var string |
|
50 | - */ |
|
51 | - protected $front_matter = ''; |
|
52 | - |
|
53 | - /** |
|
54 | - * Content without front matter |
|
55 | - * @var string |
|
56 | - */ |
|
57 | - protected $post_content; |
|
58 | - |
|
59 | - /** |
|
60 | - * Instantiates a new Blob object. |
|
61 | - * |
|
62 | - * @param stdClass $data Raw blob data. |
|
63 | - */ |
|
64 | - public function __construct( stdClass $data ) { |
|
65 | - $this->interpret_data( $data ); |
|
66 | - } |
|
67 | - |
|
68 | - public function id() { |
|
69 | - return $this->id; |
|
70 | - } |
|
71 | - |
|
72 | - public function set_id($id) { |
|
73 | - $this->id = $id; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Returns the raw blob content. |
|
78 | - * |
|
79 | - * @return string |
|
80 | - */ |
|
81 | - public function content() { |
|
82 | - return $this->content; |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Set's the blob's content. |
|
87 | - * |
|
88 | - * @param string $content Raw blob content. |
|
89 | - * @param bool $base64 Whether the content is base64 encoded. |
|
90 | - * |
|
91 | - * @return $this |
|
92 | - */ |
|
93 | - public function set_content( $content, $base64 = false ) { |
|
94 | - if ( $base64 ) { |
|
95 | - $content = base64_decode( $content ); |
|
96 | - } |
|
97 | - |
|
98 | - // remove whitespace from the beginning of content, |
|
99 | - // To prevent blank lines before yml |
|
100 | - $this->content = ltrim( $content ); |
|
101 | - |
|
102 | - $this->frontmatter = '---' === substr( $this->content, 0, 3 ); |
|
103 | - |
|
104 | - return $this; |
|
105 | - } |
|
106 | - /** |
|
107 | - * Returns the blob sha. |
|
108 | - * |
|
109 | - * @return string |
|
110 | - */ |
|
111 | - public function sha() { |
|
112 | - return $this->sha; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Return's the blob path. |
|
117 | - * |
|
118 | - * @return string |
|
119 | - */ |
|
120 | - public function path() { |
|
121 | - return $this->path; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Whether the blob has frontmatter. |
|
126 | - * |
|
127 | - * @return bool |
|
128 | - */ |
|
129 | - public function has_frontmatter() { |
|
130 | - return $this->frontmatter; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * The front matter of github post |
|
135 | - * @return string |
|
136 | - */ |
|
137 | - public function front_matter() { |
|
138 | - return $this->front_matter; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Content without front matter |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - public function post_content() { |
|
146 | - if ( ! $this->post_content ) { |
|
147 | - $this->content_import(); |
|
148 | - } |
|
149 | - return $this->post_content; |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Returns the formatted/filtered blob content used for import. |
|
154 | - * |
|
155 | - * @return string |
|
156 | - */ |
|
157 | - public function content_import() { |
|
158 | - $this->post_content = $content = $this->content(); |
|
159 | - |
|
160 | - if ( $this->has_frontmatter() ) { |
|
161 | - // Break out content. |
|
162 | - preg_match( '/(^---(.*?)---$(\r\n|\n|\r)?)?(.*)/ms', $content, $matches ); |
|
163 | - $this->front_matter = $matches[1]; |
|
164 | - $this->post_content = $content = array_pop( $matches ); |
|
165 | - } |
|
166 | - |
|
167 | - if ( function_exists( 'wpmarkdown_markdown_to_html' ) ) { |
|
168 | - $content = wpmarkdown_markdown_to_html( $content ); |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Filters the content for import. |
|
173 | - */ |
|
174 | - return apply_filters( 'wogh_content_import', trim( $content ) ); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Returns the blob meta. |
|
179 | - * |
|
180 | - * @return array |
|
181 | - */ |
|
182 | - public function meta() { |
|
183 | - $meta = array(); |
|
184 | - |
|
185 | - if ( $this->has_frontmatter() ) { |
|
186 | - // Break out meta, if present. |
|
187 | - preg_match( '/(^---(.*?)---$)?(.*)/ms', $this->content(), $matches ); |
|
188 | - array_pop( $matches ); |
|
189 | - |
|
190 | - $meta = spyc_load( $matches[2] ); |
|
191 | - if ( 'yes' == get_option('wogh_ignore_author') ) { |
|
192 | - unset($meta['author']); |
|
193 | - } |
|
194 | - // if ( isset( $meta['link'] ) ) { |
|
195 | - // $meta['link'] = str_replace( home_url(), '', $meta['link'] ); |
|
196 | - // } |
|
197 | - } |
|
198 | - |
|
199 | - return $meta; |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Formats the blob into an API call body. |
|
204 | - * |
|
205 | - * @return stdClass |
|
206 | - */ |
|
207 | - // public function to_body() { |
|
208 | - // $data = new stdClass; |
|
209 | - |
|
210 | - // $data->mode = '100644'; |
|
211 | - // $data->type = 'blob'; |
|
212 | - |
|
213 | - // $data->path = $this->path(); |
|
214 | - |
|
215 | - // if ( $this->sha() ) { |
|
216 | - // $data->sha = $this->sha(); |
|
217 | - // } else { |
|
218 | - // $data->content = $this->content(); |
|
219 | - // } |
|
220 | - |
|
221 | - // return $data; |
|
222 | - // } |
|
223 | - |
|
224 | - |
|
225 | - /** |
|
226 | - * Formats the blob into an API call body. |
|
227 | - * |
|
228 | - * @return stdClass |
|
229 | - */ |
|
230 | - public function to_body() { |
|
231 | - $data = new stdClass; |
|
232 | - |
|
233 | - // $data->mode = '100644'; |
|
234 | - // $data->type = 'blob'; |
|
235 | - |
|
236 | - $data->path = $this->path(); |
|
237 | - $data->content = base64_encode( $this->content() ); |
|
238 | - $data->sha = $this->sha; |
|
239 | - |
|
240 | - return $data; |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Interprets the blob's data into properties. |
|
245 | - */ |
|
246 | - protected function interpret_data( $data ) { |
|
247 | - $this->sha = isset( $data->sha ) ? $data->sha : ''; |
|
248 | - $this->path = isset( $data->path ) ? $data->path : ''; |
|
249 | - |
|
250 | - $this->set_content( |
|
251 | - isset( $data->content ) ? $data->content : '', |
|
252 | - isset( $data->encoding ) && 'base64' === $data->encoding ? true : false |
|
253 | - ); |
|
254 | - } |
|
12 | + /** |
|
13 | + * Complete blob content. |
|
14 | + * |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + protected $content; |
|
18 | + |
|
19 | + /** |
|
20 | + * Blob sha. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + protected $sha; |
|
25 | + |
|
26 | + /** |
|
27 | + * Blob path. |
|
28 | + * |
|
29 | + * @var string |
|
30 | + */ |
|
31 | + protected $path; |
|
32 | + |
|
33 | + /** |
|
34 | + * Post id. |
|
35 | + * |
|
36 | + * @var int |
|
37 | + */ |
|
38 | + protected $id; |
|
39 | + |
|
40 | + /** |
|
41 | + * Whether the blob has frontmatter. |
|
42 | + * |
|
43 | + * @var boolean |
|
44 | + */ |
|
45 | + protected $frontmatter = false; |
|
46 | + |
|
47 | + /** |
|
48 | + * The front matter of github post |
|
49 | + * @var string |
|
50 | + */ |
|
51 | + protected $front_matter = ''; |
|
52 | + |
|
53 | + /** |
|
54 | + * Content without front matter |
|
55 | + * @var string |
|
56 | + */ |
|
57 | + protected $post_content; |
|
58 | + |
|
59 | + /** |
|
60 | + * Instantiates a new Blob object. |
|
61 | + * |
|
62 | + * @param stdClass $data Raw blob data. |
|
63 | + */ |
|
64 | + public function __construct( stdClass $data ) { |
|
65 | + $this->interpret_data( $data ); |
|
66 | + } |
|
67 | + |
|
68 | + public function id() { |
|
69 | + return $this->id; |
|
70 | + } |
|
71 | + |
|
72 | + public function set_id($id) { |
|
73 | + $this->id = $id; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Returns the raw blob content. |
|
78 | + * |
|
79 | + * @return string |
|
80 | + */ |
|
81 | + public function content() { |
|
82 | + return $this->content; |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Set's the blob's content. |
|
87 | + * |
|
88 | + * @param string $content Raw blob content. |
|
89 | + * @param bool $base64 Whether the content is base64 encoded. |
|
90 | + * |
|
91 | + * @return $this |
|
92 | + */ |
|
93 | + public function set_content( $content, $base64 = false ) { |
|
94 | + if ( $base64 ) { |
|
95 | + $content = base64_decode( $content ); |
|
96 | + } |
|
97 | + |
|
98 | + // remove whitespace from the beginning of content, |
|
99 | + // To prevent blank lines before yml |
|
100 | + $this->content = ltrim( $content ); |
|
101 | + |
|
102 | + $this->frontmatter = '---' === substr( $this->content, 0, 3 ); |
|
103 | + |
|
104 | + return $this; |
|
105 | + } |
|
106 | + /** |
|
107 | + * Returns the blob sha. |
|
108 | + * |
|
109 | + * @return string |
|
110 | + */ |
|
111 | + public function sha() { |
|
112 | + return $this->sha; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Return's the blob path. |
|
117 | + * |
|
118 | + * @return string |
|
119 | + */ |
|
120 | + public function path() { |
|
121 | + return $this->path; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Whether the blob has frontmatter. |
|
126 | + * |
|
127 | + * @return bool |
|
128 | + */ |
|
129 | + public function has_frontmatter() { |
|
130 | + return $this->frontmatter; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * The front matter of github post |
|
135 | + * @return string |
|
136 | + */ |
|
137 | + public function front_matter() { |
|
138 | + return $this->front_matter; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Content without front matter |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + public function post_content() { |
|
146 | + if ( ! $this->post_content ) { |
|
147 | + $this->content_import(); |
|
148 | + } |
|
149 | + return $this->post_content; |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Returns the formatted/filtered blob content used for import. |
|
154 | + * |
|
155 | + * @return string |
|
156 | + */ |
|
157 | + public function content_import() { |
|
158 | + $this->post_content = $content = $this->content(); |
|
159 | + |
|
160 | + if ( $this->has_frontmatter() ) { |
|
161 | + // Break out content. |
|
162 | + preg_match( '/(^---(.*?)---$(\r\n|\n|\r)?)?(.*)/ms', $content, $matches ); |
|
163 | + $this->front_matter = $matches[1]; |
|
164 | + $this->post_content = $content = array_pop( $matches ); |
|
165 | + } |
|
166 | + |
|
167 | + if ( function_exists( 'wpmarkdown_markdown_to_html' ) ) { |
|
168 | + $content = wpmarkdown_markdown_to_html( $content ); |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Filters the content for import. |
|
173 | + */ |
|
174 | + return apply_filters( 'wogh_content_import', trim( $content ) ); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Returns the blob meta. |
|
179 | + * |
|
180 | + * @return array |
|
181 | + */ |
|
182 | + public function meta() { |
|
183 | + $meta = array(); |
|
184 | + |
|
185 | + if ( $this->has_frontmatter() ) { |
|
186 | + // Break out meta, if present. |
|
187 | + preg_match( '/(^---(.*?)---$)?(.*)/ms', $this->content(), $matches ); |
|
188 | + array_pop( $matches ); |
|
189 | + |
|
190 | + $meta = spyc_load( $matches[2] ); |
|
191 | + if ( 'yes' == get_option('wogh_ignore_author') ) { |
|
192 | + unset($meta['author']); |
|
193 | + } |
|
194 | + // if ( isset( $meta['link'] ) ) { |
|
195 | + // $meta['link'] = str_replace( home_url(), '', $meta['link'] ); |
|
196 | + // } |
|
197 | + } |
|
198 | + |
|
199 | + return $meta; |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Formats the blob into an API call body. |
|
204 | + * |
|
205 | + * @return stdClass |
|
206 | + */ |
|
207 | + // public function to_body() { |
|
208 | + // $data = new stdClass; |
|
209 | + |
|
210 | + // $data->mode = '100644'; |
|
211 | + // $data->type = 'blob'; |
|
212 | + |
|
213 | + // $data->path = $this->path(); |
|
214 | + |
|
215 | + // if ( $this->sha() ) { |
|
216 | + // $data->sha = $this->sha(); |
|
217 | + // } else { |
|
218 | + // $data->content = $this->content(); |
|
219 | + // } |
|
220 | + |
|
221 | + // return $data; |
|
222 | + // } |
|
223 | + |
|
224 | + |
|
225 | + /** |
|
226 | + * Formats the blob into an API call body. |
|
227 | + * |
|
228 | + * @return stdClass |
|
229 | + */ |
|
230 | + public function to_body() { |
|
231 | + $data = new stdClass; |
|
232 | + |
|
233 | + // $data->mode = '100644'; |
|
234 | + // $data->type = 'blob'; |
|
235 | + |
|
236 | + $data->path = $this->path(); |
|
237 | + $data->content = base64_encode( $this->content() ); |
|
238 | + $data->sha = $this->sha; |
|
239 | + |
|
240 | + return $data; |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Interprets the blob's data into properties. |
|
245 | + */ |
|
246 | + protected function interpret_data( $data ) { |
|
247 | + $this->sha = isset( $data->sha ) ? $data->sha : ''; |
|
248 | + $this->path = isset( $data->path ) ? $data->path : ''; |
|
249 | + |
|
250 | + $this->set_content( |
|
251 | + isset( $data->content ) ? $data->content : '', |
|
252 | + isset( $data->encoding ) && 'base64' === $data->encoding ? true : false |
|
253 | + ); |
|
254 | + } |
|
255 | 255 | } |
@@ -8,10 +8,10 @@ discard block |
||
8 | 8 | * @return WP_Error |
9 | 9 | */ |
10 | 10 | function wogh_append_error( $error, $error2 ) { |
11 | - if ( is_wp_error( $error ) ) { |
|
12 | - $error->add( $error2->get_error_code(), $error2->get_error_message() ); |
|
13 | - } |
|
14 | - return $error2; |
|
11 | + if ( is_wp_error( $error ) ) { |
|
12 | + $error->add( $error2->get_error_code(), $error2->get_error_message() ); |
|
13 | + } |
|
14 | + return $error2; |
|
15 | 15 | } |
16 | 16 | |
17 | 17 | /** |
@@ -21,9 +21,9 @@ discard block |
||
21 | 21 | * @return bool |
22 | 22 | */ |
23 | 23 | function wogh_equal_front_matter( $post, $blob ) { |
24 | - $str1 = $post->front_matter(); |
|
25 | - $str2 = $blob->front_matter(); |
|
26 | - return trim($str1) === trim($str2); |
|
24 | + $str1 = $post->front_matter(); |
|
25 | + $str2 = $blob->front_matter(); |
|
26 | + return trim($str1) === trim($str2); |
|
27 | 27 | } |
28 | 28 | |
29 | 29 | /** |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | * @return bool |
32 | 32 | */ |
33 | 33 | function wogh_is_dont_export_content() { |
34 | - return 'yes' === get_option( 'wogh_dont_export_content' ); |
|
34 | + return 'yes' === get_option( 'wogh_dont_export_content' ); |
|
35 | 35 | } |
36 | 36 | |
37 | 37 | /** |
@@ -41,8 +41,8 @@ discard block |
||
41 | 41 | * @return string |
42 | 42 | */ |
43 | 43 | function wogh_git_sha( $content ) { |
44 | - // $header = "blob $len\0" |
|
45 | - // sha1($header . $content) |
|
46 | - $len = strlen( $content ); |
|
47 | - return sha1( "blob $len\0$content" ); |
|
44 | + // $header = "blob $len\0" |
|
45 | + // sha1($header . $content) |
|
46 | + $len = strlen( $content ); |
|
47 | + return sha1( "blob $len\0$content" ); |
|
48 | 48 | } |
@@ -14,391 +14,391 @@ |
||
14 | 14 | // This fixes function duplication during unit testing. |
15 | 15 | $path = dirname( __FILE__ ) . '/vendor/autoload.php'; |
16 | 16 | if ( file_exists( $path ) ) { |
17 | - require_once( $path ); |
|
17 | + require_once( $path ); |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | add_action( 'plugins_loaded', array( new Writing_On_GitHub, 'boot' ) ); |
21 | 21 | |
22 | 22 | class Writing_On_GitHub { |
23 | 23 | |
24 | - /** |
|
25 | - * Object instance |
|
26 | - * @var self |
|
27 | - */ |
|
28 | - public static $instance; |
|
29 | - |
|
30 | - /** |
|
31 | - * Language text domain |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - public static $text_domain = 'writing-on-github'; |
|
35 | - |
|
36 | - /** |
|
37 | - * Controller object |
|
38 | - * @var Writing_On_GitHub_Controller |
|
39 | - */ |
|
40 | - public $controller; |
|
41 | - |
|
42 | - /** |
|
43 | - * Controller object |
|
44 | - * @var Writing_On_GitHub_Admin |
|
45 | - */ |
|
46 | - public $admin; |
|
47 | - |
|
48 | - /** |
|
49 | - * CLI object. |
|
50 | - * |
|
51 | - * @var Writing_On_GitHub_CLI |
|
52 | - */ |
|
53 | - protected $cli; |
|
54 | - |
|
55 | - /** |
|
56 | - * Request object. |
|
57 | - * |
|
58 | - * @var Writing_On_GitHub_Request |
|
59 | - */ |
|
60 | - protected $request; |
|
61 | - |
|
62 | - /** |
|
63 | - * Response object. |
|
64 | - * |
|
65 | - * @var Writing_On_GitHub_Response |
|
66 | - */ |
|
67 | - protected $response; |
|
68 | - |
|
69 | - /** |
|
70 | - * Api object. |
|
71 | - * |
|
72 | - * @var Writing_On_GitHub_Api |
|
73 | - */ |
|
74 | - protected $api; |
|
75 | - |
|
76 | - /** |
|
77 | - * Import object. |
|
78 | - * |
|
79 | - * @var Writing_On_GitHub_Import |
|
80 | - */ |
|
81 | - protected $import; |
|
82 | - |
|
83 | - /** |
|
84 | - * Export object. |
|
85 | - * |
|
86 | - * @var Writing_On_GitHub_Export |
|
87 | - */ |
|
88 | - protected $export; |
|
89 | - |
|
90 | - /** |
|
91 | - * Semaphore object. |
|
92 | - * |
|
93 | - * @var Writing_On_GitHub_Semaphore |
|
94 | - */ |
|
95 | - protected $semaphore; |
|
96 | - |
|
97 | - /** |
|
98 | - * Database object. |
|
99 | - * |
|
100 | - * @var Writing_On_GitHub_Database |
|
101 | - */ |
|
102 | - protected $database; |
|
103 | - |
|
104 | - /** |
|
105 | - * Called at load time, hooks into WP core |
|
106 | - */ |
|
107 | - public function __construct() { |
|
108 | - self::$instance = $this; |
|
109 | - |
|
110 | - if ( is_admin() ) { |
|
111 | - $this->admin = new Writing_On_GitHub_Admin( plugin_basename( __FILE__ ) ); |
|
112 | - } |
|
113 | - |
|
114 | - $this->controller = new Writing_On_GitHub_Controller( $this ); |
|
115 | - |
|
116 | - if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
117 | - WP_CLI::add_command( 'wogh', $this->cli() ); |
|
118 | - } |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Attaches the plugin's hooks into WordPress. |
|
123 | - */ |
|
124 | - public function boot() { |
|
125 | - register_activation_hook( __FILE__, array( $this, 'activate' ) ); |
|
126 | - add_action( 'admin_notices', array( $this, 'activation_notice' ) ); |
|
127 | - |
|
128 | - add_action( 'init', array( $this, 'l10n' ) ); |
|
129 | - |
|
130 | - // Controller actions. |
|
131 | - add_action( 'save_post', array( $this->controller, 'export_post' ) ); |
|
132 | - add_action( 'delete_post', array( $this->controller, 'delete_post' ) ); |
|
133 | - add_action( 'wp_ajax_nopriv_wogh_push_request', array( $this->controller, 'pull_posts' ) ); |
|
134 | - add_action( 'wogh_export', array( $this->controller, 'export_all' ), 10, 2 ); |
|
135 | - add_action( 'wogh_import', array( $this->controller, 'import_master' ), 10, 2 ); |
|
136 | - add_filter( 'get_edit_post_link', array( $this, 'edit_post_link' ), 10, 3 ); |
|
137 | - |
|
138 | - // add_filter( 'wogh_post_meta', array( $this, 'ignore_post_meta' ), 10, 1 ); |
|
139 | - // add_filter( 'wogh_pre_import_meta', array( $this, 'ignore_post_meta' ), 10, 1 ); |
|
140 | - add_filter( 'the_content', array( $this, 'the_content' ) ); |
|
141 | - |
|
142 | - do_action( 'wogh_boot', $this ); |
|
143 | - } |
|
144 | - |
|
145 | - public function edit_post_link($link, $postID, $context) { |
|
146 | - if ( ! wp_is_post_revision( $postID ) ) { |
|
147 | - $post = new Writing_On_GitHub_Post( $postID, Writing_On_GitHub::$instance->api() ); |
|
148 | - if ( $post->is_on_github() ) { |
|
149 | - return $post->github_edit_url(); |
|
150 | - } |
|
151 | - } |
|
152 | - |
|
153 | - return $link; |
|
154 | - } |
|
155 | - |
|
156 | - public function ignore_post_meta($meta) { |
|
157 | - $ignore_meta_keys = get_option('wogh_ignore_metas'); |
|
158 | - if (empty($ignore_meta_keys)) { |
|
159 | - return $meta; |
|
160 | - } |
|
161 | - |
|
162 | - $keys = preg_split("/\\r\\n|\\r|\\n/", $ignore_meta_keys); |
|
163 | - if (empty($keys)) { |
|
164 | - return $meta; |
|
165 | - } |
|
166 | - foreach ($keys as $key => $value) { |
|
167 | - $keys[$key] = trim($value); |
|
168 | - } |
|
169 | - |
|
170 | - foreach ($meta as $key => $value) { |
|
171 | - if (in_array($key, $keys)) { |
|
172 | - unset($meta[$key]); |
|
173 | - } |
|
174 | - } |
|
175 | - |
|
176 | - return $meta; |
|
177 | - } |
|
178 | - |
|
179 | - public function the_content($content) { |
|
180 | - $arr = wp_upload_dir(); |
|
181 | - $baseurl = $arr['baseurl'] . '/writing-on-github'; |
|
182 | - |
|
183 | - $content = preg_replace_callback( |
|
184 | - '/(<img [^>]*?src=[\'"])\s*(\/images\/[^\s#]\S+)\s*([\'"][^>]*?>)/', |
|
185 | - function($matchs) use ($baseurl) { |
|
186 | - $url = $baseurl . $matchs[2]; |
|
187 | - return "${matchs[1]}$url${matchs[3]}"; |
|
188 | - }, |
|
189 | - $content |
|
190 | - ); |
|
191 | - |
|
192 | - $content = preg_replace_callback( |
|
193 | - '/(<a [^>]*?href=[\'"])\s*(\/images\/[^\s#]\S+)\s*([\'"][^>]*?>)/', |
|
194 | - function($matchs) use ($baseurl) { |
|
195 | - $url = $baseurl . $matchs[2]; |
|
196 | - return "${matchs[1]}$url${matchs[3]}"; |
|
197 | - }, |
|
198 | - $content |
|
199 | - ); |
|
200 | - return $content; |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * Init i18n files |
|
205 | - */ |
|
206 | - public function l10n() { |
|
207 | - load_plugin_textdomain( self::$text_domain ); |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * Sets and kicks off the export cronjob |
|
212 | - */ |
|
213 | - public function start_export( $force = false ) { |
|
214 | - $this->start_cron( 'export', $force ); |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Sets and kicks off the import cronjob |
|
219 | - */ |
|
220 | - public function start_import( $force = false ) { |
|
221 | - $this->start_cron( 'import', $force ); |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * Enables the admin notice on initial activation |
|
226 | - */ |
|
227 | - public function activate() { |
|
228 | - if ( 'yes' !== get_option( '_wogh_fully_exported' ) ) { |
|
229 | - set_transient( '_wogh_activated', 'yes' ); |
|
230 | - } |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Displays the activation admin notice |
|
235 | - */ |
|
236 | - public function activation_notice() { |
|
237 | - if ( ! get_transient( '_wogh_activated' ) ) { |
|
238 | - return; |
|
239 | - } |
|
240 | - |
|
241 | - delete_transient( '_wogh_activated' ); |
|
242 | - |
|
243 | - ?><div class="updated"> |
|
24 | + /** |
|
25 | + * Object instance |
|
26 | + * @var self |
|
27 | + */ |
|
28 | + public static $instance; |
|
29 | + |
|
30 | + /** |
|
31 | + * Language text domain |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + public static $text_domain = 'writing-on-github'; |
|
35 | + |
|
36 | + /** |
|
37 | + * Controller object |
|
38 | + * @var Writing_On_GitHub_Controller |
|
39 | + */ |
|
40 | + public $controller; |
|
41 | + |
|
42 | + /** |
|
43 | + * Controller object |
|
44 | + * @var Writing_On_GitHub_Admin |
|
45 | + */ |
|
46 | + public $admin; |
|
47 | + |
|
48 | + /** |
|
49 | + * CLI object. |
|
50 | + * |
|
51 | + * @var Writing_On_GitHub_CLI |
|
52 | + */ |
|
53 | + protected $cli; |
|
54 | + |
|
55 | + /** |
|
56 | + * Request object. |
|
57 | + * |
|
58 | + * @var Writing_On_GitHub_Request |
|
59 | + */ |
|
60 | + protected $request; |
|
61 | + |
|
62 | + /** |
|
63 | + * Response object. |
|
64 | + * |
|
65 | + * @var Writing_On_GitHub_Response |
|
66 | + */ |
|
67 | + protected $response; |
|
68 | + |
|
69 | + /** |
|
70 | + * Api object. |
|
71 | + * |
|
72 | + * @var Writing_On_GitHub_Api |
|
73 | + */ |
|
74 | + protected $api; |
|
75 | + |
|
76 | + /** |
|
77 | + * Import object. |
|
78 | + * |
|
79 | + * @var Writing_On_GitHub_Import |
|
80 | + */ |
|
81 | + protected $import; |
|
82 | + |
|
83 | + /** |
|
84 | + * Export object. |
|
85 | + * |
|
86 | + * @var Writing_On_GitHub_Export |
|
87 | + */ |
|
88 | + protected $export; |
|
89 | + |
|
90 | + /** |
|
91 | + * Semaphore object. |
|
92 | + * |
|
93 | + * @var Writing_On_GitHub_Semaphore |
|
94 | + */ |
|
95 | + protected $semaphore; |
|
96 | + |
|
97 | + /** |
|
98 | + * Database object. |
|
99 | + * |
|
100 | + * @var Writing_On_GitHub_Database |
|
101 | + */ |
|
102 | + protected $database; |
|
103 | + |
|
104 | + /** |
|
105 | + * Called at load time, hooks into WP core |
|
106 | + */ |
|
107 | + public function __construct() { |
|
108 | + self::$instance = $this; |
|
109 | + |
|
110 | + if ( is_admin() ) { |
|
111 | + $this->admin = new Writing_On_GitHub_Admin( plugin_basename( __FILE__ ) ); |
|
112 | + } |
|
113 | + |
|
114 | + $this->controller = new Writing_On_GitHub_Controller( $this ); |
|
115 | + |
|
116 | + if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
117 | + WP_CLI::add_command( 'wogh', $this->cli() ); |
|
118 | + } |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Attaches the plugin's hooks into WordPress. |
|
123 | + */ |
|
124 | + public function boot() { |
|
125 | + register_activation_hook( __FILE__, array( $this, 'activate' ) ); |
|
126 | + add_action( 'admin_notices', array( $this, 'activation_notice' ) ); |
|
127 | + |
|
128 | + add_action( 'init', array( $this, 'l10n' ) ); |
|
129 | + |
|
130 | + // Controller actions. |
|
131 | + add_action( 'save_post', array( $this->controller, 'export_post' ) ); |
|
132 | + add_action( 'delete_post', array( $this->controller, 'delete_post' ) ); |
|
133 | + add_action( 'wp_ajax_nopriv_wogh_push_request', array( $this->controller, 'pull_posts' ) ); |
|
134 | + add_action( 'wogh_export', array( $this->controller, 'export_all' ), 10, 2 ); |
|
135 | + add_action( 'wogh_import', array( $this->controller, 'import_master' ), 10, 2 ); |
|
136 | + add_filter( 'get_edit_post_link', array( $this, 'edit_post_link' ), 10, 3 ); |
|
137 | + |
|
138 | + // add_filter( 'wogh_post_meta', array( $this, 'ignore_post_meta' ), 10, 1 ); |
|
139 | + // add_filter( 'wogh_pre_import_meta', array( $this, 'ignore_post_meta' ), 10, 1 ); |
|
140 | + add_filter( 'the_content', array( $this, 'the_content' ) ); |
|
141 | + |
|
142 | + do_action( 'wogh_boot', $this ); |
|
143 | + } |
|
144 | + |
|
145 | + public function edit_post_link($link, $postID, $context) { |
|
146 | + if ( ! wp_is_post_revision( $postID ) ) { |
|
147 | + $post = new Writing_On_GitHub_Post( $postID, Writing_On_GitHub::$instance->api() ); |
|
148 | + if ( $post->is_on_github() ) { |
|
149 | + return $post->github_edit_url(); |
|
150 | + } |
|
151 | + } |
|
152 | + |
|
153 | + return $link; |
|
154 | + } |
|
155 | + |
|
156 | + public function ignore_post_meta($meta) { |
|
157 | + $ignore_meta_keys = get_option('wogh_ignore_metas'); |
|
158 | + if (empty($ignore_meta_keys)) { |
|
159 | + return $meta; |
|
160 | + } |
|
161 | + |
|
162 | + $keys = preg_split("/\\r\\n|\\r|\\n/", $ignore_meta_keys); |
|
163 | + if (empty($keys)) { |
|
164 | + return $meta; |
|
165 | + } |
|
166 | + foreach ($keys as $key => $value) { |
|
167 | + $keys[$key] = trim($value); |
|
168 | + } |
|
169 | + |
|
170 | + foreach ($meta as $key => $value) { |
|
171 | + if (in_array($key, $keys)) { |
|
172 | + unset($meta[$key]); |
|
173 | + } |
|
174 | + } |
|
175 | + |
|
176 | + return $meta; |
|
177 | + } |
|
178 | + |
|
179 | + public function the_content($content) { |
|
180 | + $arr = wp_upload_dir(); |
|
181 | + $baseurl = $arr['baseurl'] . '/writing-on-github'; |
|
182 | + |
|
183 | + $content = preg_replace_callback( |
|
184 | + '/(<img [^>]*?src=[\'"])\s*(\/images\/[^\s#]\S+)\s*([\'"][^>]*?>)/', |
|
185 | + function($matchs) use ($baseurl) { |
|
186 | + $url = $baseurl . $matchs[2]; |
|
187 | + return "${matchs[1]}$url${matchs[3]}"; |
|
188 | + }, |
|
189 | + $content |
|
190 | + ); |
|
191 | + |
|
192 | + $content = preg_replace_callback( |
|
193 | + '/(<a [^>]*?href=[\'"])\s*(\/images\/[^\s#]\S+)\s*([\'"][^>]*?>)/', |
|
194 | + function($matchs) use ($baseurl) { |
|
195 | + $url = $baseurl . $matchs[2]; |
|
196 | + return "${matchs[1]}$url${matchs[3]}"; |
|
197 | + }, |
|
198 | + $content |
|
199 | + ); |
|
200 | + return $content; |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * Init i18n files |
|
205 | + */ |
|
206 | + public function l10n() { |
|
207 | + load_plugin_textdomain( self::$text_domain ); |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * Sets and kicks off the export cronjob |
|
212 | + */ |
|
213 | + public function start_export( $force = false ) { |
|
214 | + $this->start_cron( 'export', $force ); |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Sets and kicks off the import cronjob |
|
219 | + */ |
|
220 | + public function start_import( $force = false ) { |
|
221 | + $this->start_cron( 'import', $force ); |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * Enables the admin notice on initial activation |
|
226 | + */ |
|
227 | + public function activate() { |
|
228 | + if ( 'yes' !== get_option( '_wogh_fully_exported' ) ) { |
|
229 | + set_transient( '_wogh_activated', 'yes' ); |
|
230 | + } |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Displays the activation admin notice |
|
235 | + */ |
|
236 | + public function activation_notice() { |
|
237 | + if ( ! get_transient( '_wogh_activated' ) ) { |
|
238 | + return; |
|
239 | + } |
|
240 | + |
|
241 | + delete_transient( '_wogh_activated' ); |
|
242 | + |
|
243 | + ?><div class="updated"> |
|
244 | 244 | <p> |
245 | 245 | <?php |
246 | - printf( |
|
247 | - __( 'To set up your site to sync with GitHub, update your <a href="%s">settings</a> and click "Export to GitHub."', 'writing-on-github' ), |
|
248 | - admin_url( 'options-general.php?page=' . static::$text_domain) |
|
249 | - ); |
|
250 | - ?> |
|
246 | + printf( |
|
247 | + __( 'To set up your site to sync with GitHub, update your <a href="%s">settings</a> and click "Export to GitHub."', 'writing-on-github' ), |
|
248 | + admin_url( 'options-general.php?page=' . static::$text_domain) |
|
249 | + ); |
|
250 | + ?> |
|
251 | 251 | </p> |
252 | 252 | </div><?php |
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * Get the Controller object. |
|
257 | - * |
|
258 | - * @return Writing_On_GitHub_Controller |
|
259 | - */ |
|
260 | - public function controller() { |
|
261 | - return $this->controller; |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * Lazy-load the CLI object. |
|
266 | - * |
|
267 | - * @return Writing_On_GitHub_CLI |
|
268 | - */ |
|
269 | - public function cli() { |
|
270 | - if ( ! $this->cli ) { |
|
271 | - $this->cli = new Writing_On_GitHub_CLI; |
|
272 | - } |
|
273 | - |
|
274 | - return $this->cli; |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * Lazy-load the Request object. |
|
279 | - * |
|
280 | - * @return Writing_On_GitHub_Request |
|
281 | - */ |
|
282 | - public function request() { |
|
283 | - if ( ! $this->request ) { |
|
284 | - $this->request = new Writing_On_GitHub_Request( $this ); |
|
285 | - } |
|
286 | - |
|
287 | - return $this->request; |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Lazy-load the Response object. |
|
292 | - * |
|
293 | - * @return Writing_On_GitHub_Response |
|
294 | - */ |
|
295 | - public function response() { |
|
296 | - if ( ! $this->response ) { |
|
297 | - $this->response = new Writing_On_GitHub_Response( $this ); |
|
298 | - } |
|
299 | - |
|
300 | - return $this->response; |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Lazy-load the Api object. |
|
305 | - * |
|
306 | - * @return Writing_On_GitHub_Api |
|
307 | - */ |
|
308 | - public function api() { |
|
309 | - if ( ! $this->api ) { |
|
310 | - $this->api = new Writing_On_GitHub_Api( $this ); |
|
311 | - } |
|
312 | - |
|
313 | - return $this->api; |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * Lazy-load the Import object. |
|
318 | - * |
|
319 | - * @return Writing_On_GitHub_Import |
|
320 | - */ |
|
321 | - public function import() { |
|
322 | - if ( ! $this->import ) { |
|
323 | - $this->import = new Writing_On_GitHub_Import( $this ); |
|
324 | - } |
|
325 | - |
|
326 | - return $this->import; |
|
327 | - } |
|
328 | - |
|
329 | - /** |
|
330 | - * Lazy-load the Export object. |
|
331 | - * |
|
332 | - * @return Writing_On_GitHub_Export |
|
333 | - */ |
|
334 | - public function export() { |
|
335 | - if ( ! $this->export ) { |
|
336 | - $this->export = new Writing_On_GitHub_Export( $this ); |
|
337 | - } |
|
338 | - |
|
339 | - return $this->export; |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * Lazy-load the Semaphore object. |
|
344 | - * |
|
345 | - * @return Writing_On_GitHub_Semaphore |
|
346 | - */ |
|
347 | - public function semaphore() { |
|
348 | - if ( ! $this->semaphore ) { |
|
349 | - $this->semaphore = new Writing_On_GitHub_Semaphore; |
|
350 | - } |
|
351 | - |
|
352 | - return $this->semaphore; |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * Lazy-load the Database object. |
|
357 | - * |
|
358 | - * @return Writing_On_GitHub_Database |
|
359 | - */ |
|
360 | - public function database() { |
|
361 | - if ( ! $this->database ) { |
|
362 | - $this->database = new Writing_On_GitHub_Database( $this ); |
|
363 | - } |
|
364 | - |
|
365 | - return $this->database; |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Print to WP_CLI if in CLI environment or |
|
370 | - * write to debug.log if WP_DEBUG is enabled |
|
371 | - * @source http://www.stumiller.me/sending-output-to-the-wordpress-debug-log/ |
|
372 | - * |
|
373 | - * @param mixed $msg |
|
374 | - * @param string $write |
|
375 | - */ |
|
376 | - public static function write_log( $msg, $write = 'line' ) { |
|
377 | - if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
378 | - if ( is_array( $msg ) || is_object( $msg ) ) { |
|
379 | - WP_CLI::print_value( $msg ); |
|
380 | - } else { |
|
381 | - WP_CLI::$write( $msg ); |
|
382 | - } |
|
383 | - } elseif ( true === WP_DEBUG ) { |
|
384 | - if ( is_array( $msg ) || is_object( $msg ) ) { |
|
385 | - error_log( print_r( $msg, true ) ); |
|
386 | - } else { |
|
387 | - error_log( $msg ); |
|
388 | - } |
|
389 | - } |
|
390 | - } |
|
391 | - |
|
392 | - /** |
|
393 | - * Kicks of an import or export cronjob. |
|
394 | - * |
|
395 | - * @param bool $force |
|
396 | - * @param string $type |
|
397 | - */ |
|
398 | - protected function start_cron( $type, $force = false ) { |
|
399 | - update_option( '_wogh_' . $type . '_started', 'yes' ); |
|
400 | - $user_id = get_current_user_id(); |
|
401 | - wp_schedule_single_event( time(), 'wogh_' . $type . '', array( $user_id, $force ) ); |
|
402 | - spawn_cron(); |
|
403 | - } |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * Get the Controller object. |
|
257 | + * |
|
258 | + * @return Writing_On_GitHub_Controller |
|
259 | + */ |
|
260 | + public function controller() { |
|
261 | + return $this->controller; |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * Lazy-load the CLI object. |
|
266 | + * |
|
267 | + * @return Writing_On_GitHub_CLI |
|
268 | + */ |
|
269 | + public function cli() { |
|
270 | + if ( ! $this->cli ) { |
|
271 | + $this->cli = new Writing_On_GitHub_CLI; |
|
272 | + } |
|
273 | + |
|
274 | + return $this->cli; |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * Lazy-load the Request object. |
|
279 | + * |
|
280 | + * @return Writing_On_GitHub_Request |
|
281 | + */ |
|
282 | + public function request() { |
|
283 | + if ( ! $this->request ) { |
|
284 | + $this->request = new Writing_On_GitHub_Request( $this ); |
|
285 | + } |
|
286 | + |
|
287 | + return $this->request; |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Lazy-load the Response object. |
|
292 | + * |
|
293 | + * @return Writing_On_GitHub_Response |
|
294 | + */ |
|
295 | + public function response() { |
|
296 | + if ( ! $this->response ) { |
|
297 | + $this->response = new Writing_On_GitHub_Response( $this ); |
|
298 | + } |
|
299 | + |
|
300 | + return $this->response; |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Lazy-load the Api object. |
|
305 | + * |
|
306 | + * @return Writing_On_GitHub_Api |
|
307 | + */ |
|
308 | + public function api() { |
|
309 | + if ( ! $this->api ) { |
|
310 | + $this->api = new Writing_On_GitHub_Api( $this ); |
|
311 | + } |
|
312 | + |
|
313 | + return $this->api; |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * Lazy-load the Import object. |
|
318 | + * |
|
319 | + * @return Writing_On_GitHub_Import |
|
320 | + */ |
|
321 | + public function import() { |
|
322 | + if ( ! $this->import ) { |
|
323 | + $this->import = new Writing_On_GitHub_Import( $this ); |
|
324 | + } |
|
325 | + |
|
326 | + return $this->import; |
|
327 | + } |
|
328 | + |
|
329 | + /** |
|
330 | + * Lazy-load the Export object. |
|
331 | + * |
|
332 | + * @return Writing_On_GitHub_Export |
|
333 | + */ |
|
334 | + public function export() { |
|
335 | + if ( ! $this->export ) { |
|
336 | + $this->export = new Writing_On_GitHub_Export( $this ); |
|
337 | + } |
|
338 | + |
|
339 | + return $this->export; |
|
340 | + } |
|
341 | + |
|
342 | + /** |
|
343 | + * Lazy-load the Semaphore object. |
|
344 | + * |
|
345 | + * @return Writing_On_GitHub_Semaphore |
|
346 | + */ |
|
347 | + public function semaphore() { |
|
348 | + if ( ! $this->semaphore ) { |
|
349 | + $this->semaphore = new Writing_On_GitHub_Semaphore; |
|
350 | + } |
|
351 | + |
|
352 | + return $this->semaphore; |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * Lazy-load the Database object. |
|
357 | + * |
|
358 | + * @return Writing_On_GitHub_Database |
|
359 | + */ |
|
360 | + public function database() { |
|
361 | + if ( ! $this->database ) { |
|
362 | + $this->database = new Writing_On_GitHub_Database( $this ); |
|
363 | + } |
|
364 | + |
|
365 | + return $this->database; |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Print to WP_CLI if in CLI environment or |
|
370 | + * write to debug.log if WP_DEBUG is enabled |
|
371 | + * @source http://www.stumiller.me/sending-output-to-the-wordpress-debug-log/ |
|
372 | + * |
|
373 | + * @param mixed $msg |
|
374 | + * @param string $write |
|
375 | + */ |
|
376 | + public static function write_log( $msg, $write = 'line' ) { |
|
377 | + if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
378 | + if ( is_array( $msg ) || is_object( $msg ) ) { |
|
379 | + WP_CLI::print_value( $msg ); |
|
380 | + } else { |
|
381 | + WP_CLI::$write( $msg ); |
|
382 | + } |
|
383 | + } elseif ( true === WP_DEBUG ) { |
|
384 | + if ( is_array( $msg ) || is_object( $msg ) ) { |
|
385 | + error_log( print_r( $msg, true ) ); |
|
386 | + } else { |
|
387 | + error_log( $msg ); |
|
388 | + } |
|
389 | + } |
|
390 | + } |
|
391 | + |
|
392 | + /** |
|
393 | + * Kicks of an import or export cronjob. |
|
394 | + * |
|
395 | + * @param bool $force |
|
396 | + * @param string $type |
|
397 | + */ |
|
398 | + protected function start_cron( $type, $force = false ) { |
|
399 | + update_option( '_wogh_' . $type . '_started', 'yes' ); |
|
400 | + $user_id = get_current_user_id(); |
|
401 | + wp_schedule_single_event( time(), 'wogh_' . $type . '', array( $user_id, $force ) ); |
|
402 | + spawn_cron(); |
|
403 | + } |
|
404 | 404 | } |
@@ -9,264 +9,264 @@ |
||
9 | 9 | */ |
10 | 10 | class Writing_On_GitHub_Admin { |
11 | 11 | |
12 | - /** |
|
13 | - * plugin file name rel plugin dir. |
|
14 | - * @var string |
|
15 | - */ |
|
16 | - protected $plugin_file; |
|
17 | - |
|
18 | - /** |
|
19 | - * Hook into GitHub API |
|
20 | - */ |
|
21 | - public function __construct( $plugin_file ) { |
|
22 | - $this->plugin_file = $plugin_file; |
|
23 | - |
|
24 | - add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); |
|
25 | - add_action( 'admin_init', array( $this, 'register_settings' ) ); |
|
26 | - add_action( 'current_screen', array( $this, 'trigger_cron' ) ); |
|
27 | - add_filter( 'plugin_action_links', array($this, 'settings_links'), 10, 2 ); |
|
28 | - } |
|
29 | - |
|
30 | - /** |
|
31 | - * settings link |
|
32 | - * @param string[] $links |
|
33 | - * @param string $file |
|
34 | - * @return string[] |
|
35 | - */ |
|
36 | - public function settings_links( $links, $file ) { |
|
37 | - if ( $file != $this->plugin_file ) { |
|
38 | - return $links; |
|
39 | - } |
|
40 | - |
|
41 | - $settings_link = '<a href="options-general.php?page=' . |
|
42 | - Writing_On_GitHub::$text_domain . '">' . __( 'Settings', 'writing-on-github' ) . '</a>'; |
|
43 | - |
|
44 | - array_push( $links, $settings_link ); |
|
45 | - |
|
46 | - return $links; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Callback to render the settings page view |
|
51 | - */ |
|
52 | - public function settings_page() { |
|
53 | - include dirname( dirname( __FILE__ ) ) . '/views/options.php'; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Callback to register the plugin's options |
|
58 | - */ |
|
59 | - public function register_settings() { |
|
60 | - add_settings_section( |
|
61 | - 'general', |
|
62 | - 'General Settings', |
|
63 | - array( $this, 'section_callback' ), |
|
64 | - Writing_On_GitHub::$text_domain |
|
65 | - ); |
|
66 | - |
|
67 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_host' ); |
|
68 | - add_settings_field( 'wogh_host', __( 'GitHub hostname', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
69 | - 'default' => 'https://api.github.com', |
|
70 | - 'name' => 'wogh_host', |
|
71 | - 'help_text' => __( 'The GitHub host to use. This only needs to be changed to support a GitHub Enterprise installation.', 'writing-on-github' ), |
|
72 | - ) |
|
73 | - ); |
|
74 | - |
|
75 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_repository' ); |
|
76 | - add_settings_field( 'wogh_repository', __( 'Repository', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
77 | - 'default' => '', |
|
78 | - 'name' => 'wogh_repository', |
|
79 | - 'help_text' => __( 'The GitHub repository to commit to, with owner (<code>[OWNER]/[REPOSITORY]</code>), e.g., <code>github/hubot.github.com</code>. The repository should contain an initial commit, which is satisfied by including a README when you create the repository on GitHub.', 'writing-on-github' ), |
|
80 | - ) |
|
81 | - ); |
|
82 | - |
|
83 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_branch' ); |
|
84 | - add_settings_field( 'wogh_branch', __( 'Branch', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
85 | - 'default' => 'master', |
|
86 | - 'name' => 'wogh_branch', |
|
87 | - 'help_text' => __( 'The GitHub branch to commit to, default is master.', 'writing-on-github' ), |
|
88 | - ) |
|
89 | - ); |
|
90 | - |
|
91 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_oauth_token' ); |
|
92 | - add_settings_field( 'wogh_oauth_token', __( 'Oauth Token', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
93 | - 'default' => '', |
|
94 | - 'name' => 'wogh_oauth_token', |
|
95 | - 'help_text' => __( "A <a href='https://github.com/settings/tokens/new'>personal oauth token</a> with <code>public_repo</code> scope.", 'writing-on-github' ), |
|
96 | - ) |
|
97 | - ); |
|
98 | - |
|
99 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_secret' ); |
|
100 | - add_settings_field( 'wogh_secret', __( 'Webhook Secret', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
101 | - 'default' => '', |
|
102 | - 'name' => 'wogh_secret', |
|
103 | - 'help_text' => __( "The webhook's secret phrase. This should be password strength, as it is used to verify the webhook's payload.", 'writing-on-github' ), |
|
104 | - ) |
|
105 | - ); |
|
106 | - |
|
107 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_default_user' ); |
|
108 | - add_settings_field( 'wogh_default_user', __( 'Default Import User', 'writing-on-github' ), array( &$this, 'user_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
109 | - 'default' => '', |
|
110 | - 'name' => 'wogh_default_user', |
|
111 | - 'help_text' => __( 'The fallback user for import, in case Writing On GitHub cannot find the committer in the database.', 'writing-on-github' ), |
|
112 | - ) |
|
113 | - ); |
|
114 | - |
|
115 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_ignore_author' ); |
|
116 | - add_settings_field( 'wogh_ignore_author', __( 'Ignore author', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
117 | - 'default' => '', |
|
118 | - 'name' => 'wogh_ignore_author', |
|
119 | - 'help_text' => __( 'Do not export author and do not use author info from GitHub.', 'writing-on-github' ), |
|
120 | - ) |
|
121 | - ); |
|
122 | - |
|
123 | - register_setting( Writing_On_GitHub::$text_domain, 'wogh_dont_export_content' ); |
|
124 | - add_settings_field( 'wogh_dont_export_content', __( 'Don\'t export content', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
125 | - 'default' => '', |
|
126 | - 'name' => 'wogh_dont_export_content', |
|
127 | - 'help_text' => __( 'Do not export post content to github, only export meta.', 'writing-on-github' ), |
|
128 | - ) |
|
129 | - ); |
|
130 | - |
|
131 | - // register_setting( Writing_On_GitHub::$text_domain, 'wogh_ignore_metas' ); |
|
132 | - // add_settings_field( 'wogh_ignore_metas', __( 'Ignore post metas', 'writing-on-github' ), array( &$this, 'textarea_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
133 | - // 'default' => '', |
|
134 | - // 'name' => 'wogh_ignore_metas', |
|
135 | - // 'help_text' => __( 'These meta keys will be ignored and cannot be imported and exported. One meta key per line.', 'writing-on-github' ), |
|
136 | - // ) |
|
137 | - // ); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Callback to render an individual options field |
|
142 | - * |
|
143 | - * @param array $args Field arguments. |
|
144 | - */ |
|
145 | - public function field_callback( $args ) { |
|
146 | - include dirname( dirname( __FILE__ ) ) . '/views/setting-field.php'; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Callback to render the default import user field. |
|
151 | - * |
|
152 | - * @param array $args Field arguments. |
|
153 | - */ |
|
154 | - public function user_field_callback( $args ) { |
|
155 | - include dirname( dirname( __FILE__ ) ) . '/views/user-setting-field.php'; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Callback to render the textarea field. |
|
160 | - * |
|
161 | - * @param array $args Field arguments. |
|
162 | - */ |
|
163 | - public function textarea_field_callback( $args ) { |
|
164 | - include dirname( dirname( __FILE__ ) ) . '/views/textarea-setting-field.php'; |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Callback to render the checkbox field. |
|
169 | - * |
|
170 | - * @param array $args Field arguments. |
|
171 | - */ |
|
172 | - public function checkbox_field_callback( $args ) { |
|
173 | - include dirname( dirname( __FILE__ ) ) . '/views/checkbox-setting-field.php'; |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Displays settings messages from background processes |
|
178 | - */ |
|
179 | - public function section_callback() { |
|
180 | - if ( get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) { |
|
181 | - return; |
|
182 | - } |
|
183 | - |
|
184 | - if ( 'yes' === get_option( '_wogh_export_started' ) ) { ?> |
|
12 | + /** |
|
13 | + * plugin file name rel plugin dir. |
|
14 | + * @var string |
|
15 | + */ |
|
16 | + protected $plugin_file; |
|
17 | + |
|
18 | + /** |
|
19 | + * Hook into GitHub API |
|
20 | + */ |
|
21 | + public function __construct( $plugin_file ) { |
|
22 | + $this->plugin_file = $plugin_file; |
|
23 | + |
|
24 | + add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); |
|
25 | + add_action( 'admin_init', array( $this, 'register_settings' ) ); |
|
26 | + add_action( 'current_screen', array( $this, 'trigger_cron' ) ); |
|
27 | + add_filter( 'plugin_action_links', array($this, 'settings_links'), 10, 2 ); |
|
28 | + } |
|
29 | + |
|
30 | + /** |
|
31 | + * settings link |
|
32 | + * @param string[] $links |
|
33 | + * @param string $file |
|
34 | + * @return string[] |
|
35 | + */ |
|
36 | + public function settings_links( $links, $file ) { |
|
37 | + if ( $file != $this->plugin_file ) { |
|
38 | + return $links; |
|
39 | + } |
|
40 | + |
|
41 | + $settings_link = '<a href="options-general.php?page=' . |
|
42 | + Writing_On_GitHub::$text_domain . '">' . __( 'Settings', 'writing-on-github' ) . '</a>'; |
|
43 | + |
|
44 | + array_push( $links, $settings_link ); |
|
45 | + |
|
46 | + return $links; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Callback to render the settings page view |
|
51 | + */ |
|
52 | + public function settings_page() { |
|
53 | + include dirname( dirname( __FILE__ ) ) . '/views/options.php'; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Callback to register the plugin's options |
|
58 | + */ |
|
59 | + public function register_settings() { |
|
60 | + add_settings_section( |
|
61 | + 'general', |
|
62 | + 'General Settings', |
|
63 | + array( $this, 'section_callback' ), |
|
64 | + Writing_On_GitHub::$text_domain |
|
65 | + ); |
|
66 | + |
|
67 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_host' ); |
|
68 | + add_settings_field( 'wogh_host', __( 'GitHub hostname', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
69 | + 'default' => 'https://api.github.com', |
|
70 | + 'name' => 'wogh_host', |
|
71 | + 'help_text' => __( 'The GitHub host to use. This only needs to be changed to support a GitHub Enterprise installation.', 'writing-on-github' ), |
|
72 | + ) |
|
73 | + ); |
|
74 | + |
|
75 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_repository' ); |
|
76 | + add_settings_field( 'wogh_repository', __( 'Repository', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
77 | + 'default' => '', |
|
78 | + 'name' => 'wogh_repository', |
|
79 | + 'help_text' => __( 'The GitHub repository to commit to, with owner (<code>[OWNER]/[REPOSITORY]</code>), e.g., <code>github/hubot.github.com</code>. The repository should contain an initial commit, which is satisfied by including a README when you create the repository on GitHub.', 'writing-on-github' ), |
|
80 | + ) |
|
81 | + ); |
|
82 | + |
|
83 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_branch' ); |
|
84 | + add_settings_field( 'wogh_branch', __( 'Branch', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
85 | + 'default' => 'master', |
|
86 | + 'name' => 'wogh_branch', |
|
87 | + 'help_text' => __( 'The GitHub branch to commit to, default is master.', 'writing-on-github' ), |
|
88 | + ) |
|
89 | + ); |
|
90 | + |
|
91 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_oauth_token' ); |
|
92 | + add_settings_field( 'wogh_oauth_token', __( 'Oauth Token', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
93 | + 'default' => '', |
|
94 | + 'name' => 'wogh_oauth_token', |
|
95 | + 'help_text' => __( "A <a href='https://github.com/settings/tokens/new'>personal oauth token</a> with <code>public_repo</code> scope.", 'writing-on-github' ), |
|
96 | + ) |
|
97 | + ); |
|
98 | + |
|
99 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_secret' ); |
|
100 | + add_settings_field( 'wogh_secret', __( 'Webhook Secret', 'writing-on-github' ), array( $this, 'field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
101 | + 'default' => '', |
|
102 | + 'name' => 'wogh_secret', |
|
103 | + 'help_text' => __( "The webhook's secret phrase. This should be password strength, as it is used to verify the webhook's payload.", 'writing-on-github' ), |
|
104 | + ) |
|
105 | + ); |
|
106 | + |
|
107 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_default_user' ); |
|
108 | + add_settings_field( 'wogh_default_user', __( 'Default Import User', 'writing-on-github' ), array( &$this, 'user_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
109 | + 'default' => '', |
|
110 | + 'name' => 'wogh_default_user', |
|
111 | + 'help_text' => __( 'The fallback user for import, in case Writing On GitHub cannot find the committer in the database.', 'writing-on-github' ), |
|
112 | + ) |
|
113 | + ); |
|
114 | + |
|
115 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_ignore_author' ); |
|
116 | + add_settings_field( 'wogh_ignore_author', __( 'Ignore author', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
117 | + 'default' => '', |
|
118 | + 'name' => 'wogh_ignore_author', |
|
119 | + 'help_text' => __( 'Do not export author and do not use author info from GitHub.', 'writing-on-github' ), |
|
120 | + ) |
|
121 | + ); |
|
122 | + |
|
123 | + register_setting( Writing_On_GitHub::$text_domain, 'wogh_dont_export_content' ); |
|
124 | + add_settings_field( 'wogh_dont_export_content', __( 'Don\'t export content', 'writing-on-github' ), array( &$this, 'checkbox_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
125 | + 'default' => '', |
|
126 | + 'name' => 'wogh_dont_export_content', |
|
127 | + 'help_text' => __( 'Do not export post content to github, only export meta.', 'writing-on-github' ), |
|
128 | + ) |
|
129 | + ); |
|
130 | + |
|
131 | + // register_setting( Writing_On_GitHub::$text_domain, 'wogh_ignore_metas' ); |
|
132 | + // add_settings_field( 'wogh_ignore_metas', __( 'Ignore post metas', 'writing-on-github' ), array( &$this, 'textarea_field_callback' ), Writing_On_GitHub::$text_domain, 'general', array( |
|
133 | + // 'default' => '', |
|
134 | + // 'name' => 'wogh_ignore_metas', |
|
135 | + // 'help_text' => __( 'These meta keys will be ignored and cannot be imported and exported. One meta key per line.', 'writing-on-github' ), |
|
136 | + // ) |
|
137 | + // ); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Callback to render an individual options field |
|
142 | + * |
|
143 | + * @param array $args Field arguments. |
|
144 | + */ |
|
145 | + public function field_callback( $args ) { |
|
146 | + include dirname( dirname( __FILE__ ) ) . '/views/setting-field.php'; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Callback to render the default import user field. |
|
151 | + * |
|
152 | + * @param array $args Field arguments. |
|
153 | + */ |
|
154 | + public function user_field_callback( $args ) { |
|
155 | + include dirname( dirname( __FILE__ ) ) . '/views/user-setting-field.php'; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Callback to render the textarea field. |
|
160 | + * |
|
161 | + * @param array $args Field arguments. |
|
162 | + */ |
|
163 | + public function textarea_field_callback( $args ) { |
|
164 | + include dirname( dirname( __FILE__ ) ) . '/views/textarea-setting-field.php'; |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Callback to render the checkbox field. |
|
169 | + * |
|
170 | + * @param array $args Field arguments. |
|
171 | + */ |
|
172 | + public function checkbox_field_callback( $args ) { |
|
173 | + include dirname( dirname( __FILE__ ) ) . '/views/checkbox-setting-field.php'; |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Displays settings messages from background processes |
|
178 | + */ |
|
179 | + public function section_callback() { |
|
180 | + if ( get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) { |
|
181 | + return; |
|
182 | + } |
|
183 | + |
|
184 | + if ( 'yes' === get_option( '_wogh_export_started' ) ) { ?> |
|
185 | 185 | <div class="updated"> |
186 | 186 | <p><?php esc_html_e( 'Export to GitHub started.', 'writing-on-github' ); ?></p> |
187 | 187 | </div><?php |
188 | - delete_option( '_wogh_export_started' ); |
|
189 | - } |
|
188 | + delete_option( '_wogh_export_started' ); |
|
189 | + } |
|
190 | 190 | |
191 | - if ( $message = get_option( '_wogh_export_error' ) ) { ?> |
|
191 | + if ( $message = get_option( '_wogh_export_error' ) ) { ?> |
|
192 | 192 | <div class="error"> |
193 | 193 | <p><?php esc_html_e( 'Export to GitHub failed with error:', 'writing-on-github' ); ?> <?php echo esc_html( $message );?></p> |
194 | 194 | </div><?php |
195 | - delete_option( '_wogh_export_error' ); |
|
196 | - } |
|
195 | + delete_option( '_wogh_export_error' ); |
|
196 | + } |
|
197 | 197 | |
198 | - if ( 'yes' === get_option( '_wogh_export_complete' ) ) { ?> |
|
198 | + if ( 'yes' === get_option( '_wogh_export_complete' ) ) { ?> |
|
199 | 199 | <div class="updated"> |
200 | 200 | <p><?php esc_html_e( 'Export to GitHub completed successfully.', 'writing-on-github' );?></p> |
201 | 201 | </div><?php |
202 | - delete_option( '_wogh_export_complete' ); |
|
203 | - } |
|
202 | + delete_option( '_wogh_export_complete' ); |
|
203 | + } |
|
204 | 204 | |
205 | - if ( 'yes' === get_option( '_wogh_import_started' ) ) { ?> |
|
205 | + if ( 'yes' === get_option( '_wogh_import_started' ) ) { ?> |
|
206 | 206 | <div class="updated"> |
207 | 207 | <p><?php esc_html_e( 'Import from GitHub started.', 'writing-on-github' ); ?></p> |
208 | 208 | </div><?php |
209 | - delete_option( '_wogh_import_started' ); |
|
210 | - } |
|
209 | + delete_option( '_wogh_import_started' ); |
|
210 | + } |
|
211 | 211 | |
212 | - if ( $message = get_option( '_wogh_import_error' ) ) { ?> |
|
212 | + if ( $message = get_option( '_wogh_import_error' ) ) { ?> |
|
213 | 213 | <div class="error"> |
214 | 214 | <p><?php esc_html_e( 'Import from GitHub failed with error:', 'writing-on-github' ); ?> <?php echo esc_html( $message );?></p> |
215 | 215 | </div><?php |
216 | - delete_option( '_wogh_import_error' ); |
|
217 | - } |
|
216 | + delete_option( '_wogh_import_error' ); |
|
217 | + } |
|
218 | 218 | |
219 | - if ( 'yes' === get_option( '_wogh_import_complete' ) ) { ?> |
|
219 | + if ( 'yes' === get_option( '_wogh_import_complete' ) ) { ?> |
|
220 | 220 | <div class="updated"> |
221 | 221 | <p><?php esc_html_e( 'Import from GitHub completed successfully.', 'writing-on-github' );?></p> |
222 | 222 | </div><?php |
223 | - delete_option( '_wogh_import_complete' ); |
|
224 | - } |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * Add options menu to admin navbar |
|
229 | - */ |
|
230 | - public function add_admin_menu() { |
|
231 | - add_options_page( |
|
232 | - __( 'Writing On GitHub', 'writing-on-github' ), |
|
233 | - __( 'Writing On GitHub', 'writing-on-github' ), |
|
234 | - 'manage_options', |
|
235 | - Writing_On_GitHub::$text_domain, |
|
236 | - array( $this, 'settings_page' ) |
|
237 | - ); |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * Admin callback to trigger import/export because WordPress admin routing lol |
|
242 | - */ |
|
243 | - public function trigger_cron() { |
|
244 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
245 | - return; |
|
246 | - } |
|
247 | - |
|
248 | - if ( get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) { |
|
249 | - return; |
|
250 | - } |
|
251 | - |
|
252 | - if ( ! isset( $_GET['action'] ) ) { |
|
253 | - return; |
|
254 | - } |
|
255 | - |
|
256 | - if ( 'export' === $_GET['action'] ) { |
|
257 | - Writing_On_GitHub::$instance->start_export(); |
|
258 | - } |
|
259 | - if ( 'force_export' === $_GET['action'] ) { |
|
260 | - Writing_On_GitHub::$instance->start_export(true); |
|
261 | - } |
|
262 | - if ( 'import' === $_GET['action'] ) { |
|
263 | - Writing_On_GitHub::$instance->start_import(); |
|
264 | - } |
|
265 | - if ( 'force_import' === $_GET['action'] ) { |
|
266 | - Writing_On_GitHub::$instance->start_import(true); |
|
267 | - } |
|
268 | - |
|
269 | - wp_redirect( admin_url( 'options-general.php?page=writing-on-github' ) ); |
|
270 | - die; |
|
271 | - } |
|
223 | + delete_option( '_wogh_import_complete' ); |
|
224 | + } |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * Add options menu to admin navbar |
|
229 | + */ |
|
230 | + public function add_admin_menu() { |
|
231 | + add_options_page( |
|
232 | + __( 'Writing On GitHub', 'writing-on-github' ), |
|
233 | + __( 'Writing On GitHub', 'writing-on-github' ), |
|
234 | + 'manage_options', |
|
235 | + Writing_On_GitHub::$text_domain, |
|
236 | + array( $this, 'settings_page' ) |
|
237 | + ); |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * Admin callback to trigger import/export because WordPress admin routing lol |
|
242 | + */ |
|
243 | + public function trigger_cron() { |
|
244 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
245 | + return; |
|
246 | + } |
|
247 | + |
|
248 | + if ( get_current_screen()->id !== 'settings_page_' . Writing_On_GitHub::$text_domain ) { |
|
249 | + return; |
|
250 | + } |
|
251 | + |
|
252 | + if ( ! isset( $_GET['action'] ) ) { |
|
253 | + return; |
|
254 | + } |
|
255 | + |
|
256 | + if ( 'export' === $_GET['action'] ) { |
|
257 | + Writing_On_GitHub::$instance->start_export(); |
|
258 | + } |
|
259 | + if ( 'force_export' === $_GET['action'] ) { |
|
260 | + Writing_On_GitHub::$instance->start_export(true); |
|
261 | + } |
|
262 | + if ( 'import' === $_GET['action'] ) { |
|
263 | + Writing_On_GitHub::$instance->start_import(); |
|
264 | + } |
|
265 | + if ( 'force_import' === $_GET['action'] ) { |
|
266 | + Writing_On_GitHub::$instance->start_import(true); |
|
267 | + } |
|
268 | + |
|
269 | + wp_redirect( admin_url( 'options-general.php?page=writing-on-github' ) ); |
|
270 | + die; |
|
271 | + } |
|
272 | 272 | } |
@@ -9,134 +9,134 @@ |
||
9 | 9 | */ |
10 | 10 | class Writing_On_GitHub_Payload { |
11 | 11 | |
12 | - /** |
|
13 | - * Application container. |
|
14 | - * |
|
15 | - * @var Writing_On_GitHub |
|
16 | - */ |
|
17 | - protected $app; |
|
18 | - |
|
19 | - /** |
|
20 | - * Payload data. |
|
21 | - * |
|
22 | - * @var stdClass |
|
23 | - */ |
|
24 | - protected $data; |
|
25 | - |
|
26 | - /** |
|
27 | - * Writing_On_GitHub_Payload constructor. |
|
28 | - * |
|
29 | - * @param Writing_On_GitHub $app Application container. |
|
30 | - * @param string $raw_data Raw request data. |
|
31 | - */ |
|
32 | - public function __construct( Writing_On_GitHub $app, $raw_data ) { |
|
33 | - $this->app = $app; |
|
34 | - $this->data = json_decode( $raw_data ); |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Returns whether payload should be imported. |
|
39 | - * |
|
40 | - * @return true|WP_Error |
|
41 | - */ |
|
42 | - public function should_import() { |
|
43 | - // @todo how do we get this without importing the whole api object just for this? |
|
44 | - if ( strtolower( $this->data->repository->full_name ) !== strtolower( $this->app->api()->fetch()->repository() ) ) { |
|
45 | - return new WP_Error( |
|
46 | - 'incorrect_repository', |
|
47 | - sprintf( 'Incorrect repository, %s -> %s .', |
|
48 | - $this->data->repository->full_name, |
|
49 | - $this->app->api()->fetch()->repository() |
|
50 | - ) |
|
51 | - ); |
|
52 | - } |
|
53 | - |
|
54 | - // The last term in the ref is the payload_branch name. |
|
55 | - $refs = explode( '/', $this->data->ref ); |
|
56 | - $payload_branch = array_pop( $refs ); |
|
57 | - |
|
58 | - $branch = $this->app->api()->fetch()->branch(); |
|
59 | - |
|
60 | - if ( $branch !== $payload_branch ) { |
|
61 | - return new WP_Error( |
|
62 | - 'incorrect_branch', |
|
63 | - sprintf( 'Incorrect branch, %s -> %s .', |
|
64 | - $payload_branch, |
|
65 | - $branch |
|
66 | - ) |
|
67 | - ); |
|
68 | - } |
|
69 | - |
|
70 | - // We add a tag to commits we push out, so we shouldn't pull them in again. |
|
71 | - $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' ); |
|
72 | - |
|
73 | - if ( ! $tag ) { |
|
74 | - throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
|
75 | - } |
|
76 | - |
|
77 | - if ( $tag === substr( $this->message(), -1 * strlen( $tag ) ) ) { |
|
78 | - return new WP_Error( |
|
79 | - 'skip_import', |
|
80 | - 'Skip import on auto export post.' |
|
81 | - ); |
|
82 | - } |
|
83 | - |
|
84 | - if ( ! $this->get_commit_id() ) { |
|
85 | - return new WP_Error( |
|
86 | - 'invalid_payload', |
|
87 | - "[Missing Commit ID] won't be imported." |
|
88 | - ); |
|
89 | - } |
|
90 | - |
|
91 | - return true; |
|
92 | - } |
|
93 | - |
|
94 | - public function get_before_commit_id() { |
|
95 | - return $this->data->before ? $this->data->before : null; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Returns the sha of the head commit. |
|
100 | - * |
|
101 | - * @return string |
|
102 | - */ |
|
103 | - public function get_commit_id() { |
|
104 | - return $this->data->head_commit ? $this->data->head_commit->id : null; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Returns the email address for the commit author. |
|
109 | - * |
|
110 | - * @return string |
|
111 | - */ |
|
112 | - public function get_author_email() { |
|
113 | - return $this->data->head_commit->author->email; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Returns array commits for the payload. |
|
118 | - * |
|
119 | - * @return array |
|
120 | - */ |
|
121 | - public function get_commits() { |
|
122 | - return $this->data->commits; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Returns the repository's full name. |
|
127 | - * |
|
128 | - * @return string |
|
129 | - */ |
|
130 | - public function get_repository_name() { |
|
131 | - return $this->data->repository->full_name; |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * Returns the payload's commit message. |
|
136 | - * |
|
137 | - * @return string |
|
138 | - */ |
|
139 | - protected function message() { |
|
140 | - return $this->data->head_commit->message; |
|
141 | - } |
|
12 | + /** |
|
13 | + * Application container. |
|
14 | + * |
|
15 | + * @var Writing_On_GitHub |
|
16 | + */ |
|
17 | + protected $app; |
|
18 | + |
|
19 | + /** |
|
20 | + * Payload data. |
|
21 | + * |
|
22 | + * @var stdClass |
|
23 | + */ |
|
24 | + protected $data; |
|
25 | + |
|
26 | + /** |
|
27 | + * Writing_On_GitHub_Payload constructor. |
|
28 | + * |
|
29 | + * @param Writing_On_GitHub $app Application container. |
|
30 | + * @param string $raw_data Raw request data. |
|
31 | + */ |
|
32 | + public function __construct( Writing_On_GitHub $app, $raw_data ) { |
|
33 | + $this->app = $app; |
|
34 | + $this->data = json_decode( $raw_data ); |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Returns whether payload should be imported. |
|
39 | + * |
|
40 | + * @return true|WP_Error |
|
41 | + */ |
|
42 | + public function should_import() { |
|
43 | + // @todo how do we get this without importing the whole api object just for this? |
|
44 | + if ( strtolower( $this->data->repository->full_name ) !== strtolower( $this->app->api()->fetch()->repository() ) ) { |
|
45 | + return new WP_Error( |
|
46 | + 'incorrect_repository', |
|
47 | + sprintf( 'Incorrect repository, %s -> %s .', |
|
48 | + $this->data->repository->full_name, |
|
49 | + $this->app->api()->fetch()->repository() |
|
50 | + ) |
|
51 | + ); |
|
52 | + } |
|
53 | + |
|
54 | + // The last term in the ref is the payload_branch name. |
|
55 | + $refs = explode( '/', $this->data->ref ); |
|
56 | + $payload_branch = array_pop( $refs ); |
|
57 | + |
|
58 | + $branch = $this->app->api()->fetch()->branch(); |
|
59 | + |
|
60 | + if ( $branch !== $payload_branch ) { |
|
61 | + return new WP_Error( |
|
62 | + 'incorrect_branch', |
|
63 | + sprintf( 'Incorrect branch, %s -> %s .', |
|
64 | + $payload_branch, |
|
65 | + $branch |
|
66 | + ) |
|
67 | + ); |
|
68 | + } |
|
69 | + |
|
70 | + // We add a tag to commits we push out, so we shouldn't pull them in again. |
|
71 | + $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' ); |
|
72 | + |
|
73 | + if ( ! $tag ) { |
|
74 | + throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
|
75 | + } |
|
76 | + |
|
77 | + if ( $tag === substr( $this->message(), -1 * strlen( $tag ) ) ) { |
|
78 | + return new WP_Error( |
|
79 | + 'skip_import', |
|
80 | + 'Skip import on auto export post.' |
|
81 | + ); |
|
82 | + } |
|
83 | + |
|
84 | + if ( ! $this->get_commit_id() ) { |
|
85 | + return new WP_Error( |
|
86 | + 'invalid_payload', |
|
87 | + "[Missing Commit ID] won't be imported." |
|
88 | + ); |
|
89 | + } |
|
90 | + |
|
91 | + return true; |
|
92 | + } |
|
93 | + |
|
94 | + public function get_before_commit_id() { |
|
95 | + return $this->data->before ? $this->data->before : null; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Returns the sha of the head commit. |
|
100 | + * |
|
101 | + * @return string |
|
102 | + */ |
|
103 | + public function get_commit_id() { |
|
104 | + return $this->data->head_commit ? $this->data->head_commit->id : null; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Returns the email address for the commit author. |
|
109 | + * |
|
110 | + * @return string |
|
111 | + */ |
|
112 | + public function get_author_email() { |
|
113 | + return $this->data->head_commit->author->email; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Returns array commits for the payload. |
|
118 | + * |
|
119 | + * @return array |
|
120 | + */ |
|
121 | + public function get_commits() { |
|
122 | + return $this->data->commits; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Returns the repository's full name. |
|
127 | + * |
|
128 | + * @return string |
|
129 | + */ |
|
130 | + public function get_repository_name() { |
|
131 | + return $this->data->repository->full_name; |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * Returns the payload's commit message. |
|
136 | + * |
|
137 | + * @return string |
|
138 | + */ |
|
139 | + protected function message() { |
|
140 | + return $this->data->head_commit->message; |
|
141 | + } |
|
142 | 142 | } |
@@ -9,232 +9,232 @@ |
||
9 | 9 | */ |
10 | 10 | class Writing_On_GitHub_Controller { |
11 | 11 | |
12 | - /** |
|
13 | - * Application container. |
|
14 | - * |
|
15 | - * @var Writing_On_GitHub |
|
16 | - */ |
|
17 | - public $app; |
|
18 | - |
|
19 | - /** |
|
20 | - * Instantiates a new Controller object |
|
21 | - * |
|
22 | - * @param Writing_On_GitHub $app Applicatio container. |
|
23 | - */ |
|
24 | - public function __construct( Writing_On_GitHub $app ) { |
|
25 | - $this->app = $app; |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * Webhook callback as triggered from GitHub push. |
|
30 | - * |
|
31 | - * Reads the Webhook payload and syncs posts as necessary. |
|
32 | - * |
|
33 | - * @return boolean |
|
34 | - */ |
|
35 | - public function pull_posts() { |
|
36 | - $this->set_ajax(); |
|
37 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
38 | - return $this->app->response()->error( new WP_Error( |
|
39 | - 'semaphore_locked', |
|
40 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::pull_posts()' ) |
|
41 | - ) ); |
|
42 | - } |
|
43 | - |
|
44 | - if ( ! $this->app->request()->is_secret_valid() ) { |
|
45 | - return $this->app->response()->error( new WP_Error( |
|
46 | - 'invalid_headers', |
|
47 | - __( 'Failed to validate secret.', 'writing-on-github' ) |
|
48 | - ) ); |
|
49 | - } |
|
50 | - |
|
51 | - // ping |
|
52 | - if ( $this->app->request()->is_ping() ) { |
|
53 | - return $this->app->response()->success( __( 'Wordpress is ready.', 'writing-on-github' ) ); |
|
54 | - } |
|
55 | - |
|
56 | - // push |
|
57 | - if ( ! $this->app->request()->is_push() ) { |
|
58 | - return $this->app->response()->error( new WP_Error( |
|
59 | - 'invalid_headers', |
|
60 | - sprintf( 'Failed to validate webhook event: %s.', |
|
61 | - $this->app->request()->webhook_event() ) |
|
62 | - ) ); |
|
63 | - } |
|
64 | - $payload = $this->app->request()->payload(); |
|
65 | - |
|
66 | - $error = $payload->should_import(); |
|
67 | - if ( is_wp_error( $error ) ) { |
|
68 | - /* @var WP_Error $error */ |
|
69 | - return $this->app->response()->error( $error ); |
|
70 | - } |
|
71 | - |
|
72 | - $this->app->semaphore()->lock(); |
|
73 | - remove_action( 'save_post', array( $this, 'export_post' ) ); |
|
74 | - remove_action( 'delete_post', array( $this, 'delete_post' ) ); |
|
75 | - |
|
76 | - $result = $this->app->import()->payload( $payload ); |
|
77 | - |
|
78 | - $this->app->semaphore()->unlock(); |
|
79 | - |
|
80 | - if ( is_wp_error( $result ) ) { |
|
81 | - /* @var WP_Error $result */ |
|
82 | - return $this->app->response()->error( $result ); |
|
83 | - } |
|
84 | - |
|
85 | - return $this->app->response()->success( $result ); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Imports posts from the current master branch. |
|
90 | - * @param integer $user_id |
|
91 | - * @param boolean $force |
|
92 | - * @return boolean |
|
93 | - */ |
|
94 | - public function import_master( $user_id = 0, $force = false ) { |
|
95 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
96 | - return $this->app->response()->error( new WP_Error( |
|
97 | - 'semaphore_locked', |
|
98 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::import_master()' ) |
|
99 | - ) ); |
|
100 | - } |
|
101 | - |
|
102 | - $this->app->semaphore()->lock(); |
|
103 | - remove_action( 'save_post', array( $this, 'export_post' ) ); |
|
104 | - remove_action( 'save_post', array( $this, 'delete_post' ) ); |
|
105 | - |
|
106 | - if ( $user_id ) { |
|
107 | - wp_set_current_user( $user_id ); |
|
108 | - } |
|
109 | - |
|
110 | - $result = $this->app->import()->master( $force ); |
|
111 | - |
|
112 | - $this->app->semaphore()->unlock(); |
|
113 | - |
|
114 | - if ( is_wp_error( $result ) ) { |
|
115 | - /* @var WP_Error $result */ |
|
116 | - update_option( '_wogh_import_error', $result->get_error_message() ); |
|
117 | - |
|
118 | - return $this->app->response()->error( $result ); |
|
119 | - } |
|
120 | - |
|
121 | - update_option( '_wogh_import_complete', 'yes' ); |
|
122 | - |
|
123 | - return $this->app->response()->success( $result ); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Export all the posts in the database to GitHub. |
|
128 | - * |
|
129 | - * @param int $user_id |
|
130 | - * @param boolean $force |
|
131 | - * @return boolean |
|
132 | - */ |
|
133 | - public function export_all( $user_id = 0, $force = false ) { |
|
134 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
135 | - return $this->app->response()->error( new WP_Error( |
|
136 | - 'semaphore_locked', |
|
137 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_all()' ) |
|
138 | - ) ); |
|
139 | - } |
|
140 | - |
|
141 | - $this->app->semaphore()->lock(); |
|
142 | - |
|
143 | - if ( $user_id ) { |
|
144 | - wp_set_current_user( $user_id ); |
|
145 | - } |
|
146 | - |
|
147 | - $result = $this->app->export()->full($force); |
|
148 | - $this->app->semaphore()->unlock(); |
|
149 | - |
|
150 | - // Maybe move option updating out of this class/upgrade message display? |
|
151 | - if ( is_wp_error( $result ) ) { |
|
152 | - /* @var WP_Error $result */ |
|
153 | - update_option( '_wogh_export_error', $result->get_error_message() ); |
|
154 | - |
|
155 | - return $this->app->response()->error( $result ); |
|
156 | - } else { |
|
157 | - update_option( '_wogh_export_complete', 'yes' ); |
|
158 | - update_option( '_wogh_fully_exported', 'yes' ); |
|
159 | - |
|
160 | - return $this->app->response()->success( $result ); |
|
161 | - } |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Exports a single post to GitHub by ID. |
|
166 | - * |
|
167 | - * Called on the save_post hook. |
|
168 | - * |
|
169 | - * @param int $post_id Post ID. |
|
170 | - * |
|
171 | - * @return boolean |
|
172 | - */ |
|
173 | - public function export_post( $post_id ) { |
|
174 | - if ( wp_is_post_revision( $post_id ) ) { |
|
175 | - return; |
|
176 | - } |
|
177 | - |
|
178 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
179 | - return $this->app->response()->error( new WP_Error( |
|
180 | - 'semaphore_locked', |
|
181 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_post()' ) |
|
182 | - ) ); |
|
183 | - } |
|
184 | - |
|
185 | - $this->app->semaphore()->lock(); |
|
186 | - $result = $this->app->export()->update( $post_id ); |
|
187 | - $this->app->semaphore()->unlock(); |
|
188 | - |
|
189 | - if ( is_wp_error( $result ) ) { |
|
190 | - /* @var WP_Error $result */ |
|
191 | - return $this->app->response()->error( $result ); |
|
192 | - } |
|
193 | - |
|
194 | - return $this->app->response()->success( $result ); |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Removes the post from the tree. |
|
199 | - * |
|
200 | - * Called the delete_post hook. |
|
201 | - * |
|
202 | - * @param int $post_id Post ID. |
|
203 | - * |
|
204 | - * @return boolean |
|
205 | - */ |
|
206 | - public function delete_post( $post_id ) { |
|
207 | - if ( wp_is_post_revision( $post_id ) ) { |
|
208 | - return; |
|
209 | - } |
|
210 | - |
|
211 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
212 | - return $this->app->response()->error( new WP_Error( |
|
213 | - 'semaphore_locked', |
|
214 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::delete_post()' ) |
|
215 | - ) ); |
|
216 | - } |
|
217 | - |
|
218 | - $this->app->semaphore()->lock(); |
|
219 | - $result = $this->app->export()->delete( $post_id ); |
|
220 | - $this->app->semaphore()->unlock(); |
|
221 | - |
|
222 | - if ( is_wp_error( $result ) ) { |
|
223 | - /* @var WP_Error $result */ |
|
224 | - return $this->app->response()->error( $result ); |
|
225 | - } |
|
226 | - |
|
227 | - return $this->app->response()->success( $result ); |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Indicates we're running our own AJAX hook |
|
232 | - * and thus should respond with JSON, rather |
|
233 | - * than just returning data. |
|
234 | - */ |
|
235 | - protected function set_ajax() { |
|
236 | - if ( ! defined( 'WOGH_AJAX' ) ) { |
|
237 | - define( 'WOGH_AJAX', true ); |
|
238 | - } |
|
239 | - } |
|
12 | + /** |
|
13 | + * Application container. |
|
14 | + * |
|
15 | + * @var Writing_On_GitHub |
|
16 | + */ |
|
17 | + public $app; |
|
18 | + |
|
19 | + /** |
|
20 | + * Instantiates a new Controller object |
|
21 | + * |
|
22 | + * @param Writing_On_GitHub $app Applicatio container. |
|
23 | + */ |
|
24 | + public function __construct( Writing_On_GitHub $app ) { |
|
25 | + $this->app = $app; |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * Webhook callback as triggered from GitHub push. |
|
30 | + * |
|
31 | + * Reads the Webhook payload and syncs posts as necessary. |
|
32 | + * |
|
33 | + * @return boolean |
|
34 | + */ |
|
35 | + public function pull_posts() { |
|
36 | + $this->set_ajax(); |
|
37 | + if ( ! $this->app->semaphore()->is_open() ) { |
|
38 | + return $this->app->response()->error( new WP_Error( |
|
39 | + 'semaphore_locked', |
|
40 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::pull_posts()' ) |
|
41 | + ) ); |
|
42 | + } |
|
43 | + |
|
44 | + if ( ! $this->app->request()->is_secret_valid() ) { |
|
45 | + return $this->app->response()->error( new WP_Error( |
|
46 | + 'invalid_headers', |
|
47 | + __( 'Failed to validate secret.', 'writing-on-github' ) |
|
48 | + ) ); |
|
49 | + } |
|
50 | + |
|
51 | + // ping |
|
52 | + if ( $this->app->request()->is_ping() ) { |
|
53 | + return $this->app->response()->success( __( 'Wordpress is ready.', 'writing-on-github' ) ); |
|
54 | + } |
|
55 | + |
|
56 | + // push |
|
57 | + if ( ! $this->app->request()->is_push() ) { |
|
58 | + return $this->app->response()->error( new WP_Error( |
|
59 | + 'invalid_headers', |
|
60 | + sprintf( 'Failed to validate webhook event: %s.', |
|
61 | + $this->app->request()->webhook_event() ) |
|
62 | + ) ); |
|
63 | + } |
|
64 | + $payload = $this->app->request()->payload(); |
|
65 | + |
|
66 | + $error = $payload->should_import(); |
|
67 | + if ( is_wp_error( $error ) ) { |
|
68 | + /* @var WP_Error $error */ |
|
69 | + return $this->app->response()->error( $error ); |
|
70 | + } |
|
71 | + |
|
72 | + $this->app->semaphore()->lock(); |
|
73 | + remove_action( 'save_post', array( $this, 'export_post' ) ); |
|
74 | + remove_action( 'delete_post', array( $this, 'delete_post' ) ); |
|
75 | + |
|
76 | + $result = $this->app->import()->payload( $payload ); |
|
77 | + |
|
78 | + $this->app->semaphore()->unlock(); |
|
79 | + |
|
80 | + if ( is_wp_error( $result ) ) { |
|
81 | + /* @var WP_Error $result */ |
|
82 | + return $this->app->response()->error( $result ); |
|
83 | + } |
|
84 | + |
|
85 | + return $this->app->response()->success( $result ); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Imports posts from the current master branch. |
|
90 | + * @param integer $user_id |
|
91 | + * @param boolean $force |
|
92 | + * @return boolean |
|
93 | + */ |
|
94 | + public function import_master( $user_id = 0, $force = false ) { |
|
95 | + if ( ! $this->app->semaphore()->is_open() ) { |
|
96 | + return $this->app->response()->error( new WP_Error( |
|
97 | + 'semaphore_locked', |
|
98 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::import_master()' ) |
|
99 | + ) ); |
|
100 | + } |
|
101 | + |
|
102 | + $this->app->semaphore()->lock(); |
|
103 | + remove_action( 'save_post', array( $this, 'export_post' ) ); |
|
104 | + remove_action( 'save_post', array( $this, 'delete_post' ) ); |
|
105 | + |
|
106 | + if ( $user_id ) { |
|
107 | + wp_set_current_user( $user_id ); |
|
108 | + } |
|
109 | + |
|
110 | + $result = $this->app->import()->master( $force ); |
|
111 | + |
|
112 | + $this->app->semaphore()->unlock(); |
|
113 | + |
|
114 | + if ( is_wp_error( $result ) ) { |
|
115 | + /* @var WP_Error $result */ |
|
116 | + update_option( '_wogh_import_error', $result->get_error_message() ); |
|
117 | + |
|
118 | + return $this->app->response()->error( $result ); |
|
119 | + } |
|
120 | + |
|
121 | + update_option( '_wogh_import_complete', 'yes' ); |
|
122 | + |
|
123 | + return $this->app->response()->success( $result ); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Export all the posts in the database to GitHub. |
|
128 | + * |
|
129 | + * @param int $user_id |
|
130 | + * @param boolean $force |
|
131 | + * @return boolean |
|
132 | + */ |
|
133 | + public function export_all( $user_id = 0, $force = false ) { |
|
134 | + if ( ! $this->app->semaphore()->is_open() ) { |
|
135 | + return $this->app->response()->error( new WP_Error( |
|
136 | + 'semaphore_locked', |
|
137 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_all()' ) |
|
138 | + ) ); |
|
139 | + } |
|
140 | + |
|
141 | + $this->app->semaphore()->lock(); |
|
142 | + |
|
143 | + if ( $user_id ) { |
|
144 | + wp_set_current_user( $user_id ); |
|
145 | + } |
|
146 | + |
|
147 | + $result = $this->app->export()->full($force); |
|
148 | + $this->app->semaphore()->unlock(); |
|
149 | + |
|
150 | + // Maybe move option updating out of this class/upgrade message display? |
|
151 | + if ( is_wp_error( $result ) ) { |
|
152 | + /* @var WP_Error $result */ |
|
153 | + update_option( '_wogh_export_error', $result->get_error_message() ); |
|
154 | + |
|
155 | + return $this->app->response()->error( $result ); |
|
156 | + } else { |
|
157 | + update_option( '_wogh_export_complete', 'yes' ); |
|
158 | + update_option( '_wogh_fully_exported', 'yes' ); |
|
159 | + |
|
160 | + return $this->app->response()->success( $result ); |
|
161 | + } |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Exports a single post to GitHub by ID. |
|
166 | + * |
|
167 | + * Called on the save_post hook. |
|
168 | + * |
|
169 | + * @param int $post_id Post ID. |
|
170 | + * |
|
171 | + * @return boolean |
|
172 | + */ |
|
173 | + public function export_post( $post_id ) { |
|
174 | + if ( wp_is_post_revision( $post_id ) ) { |
|
175 | + return; |
|
176 | + } |
|
177 | + |
|
178 | + if ( ! $this->app->semaphore()->is_open() ) { |
|
179 | + return $this->app->response()->error( new WP_Error( |
|
180 | + 'semaphore_locked', |
|
181 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_post()' ) |
|
182 | + ) ); |
|
183 | + } |
|
184 | + |
|
185 | + $this->app->semaphore()->lock(); |
|
186 | + $result = $this->app->export()->update( $post_id ); |
|
187 | + $this->app->semaphore()->unlock(); |
|
188 | + |
|
189 | + if ( is_wp_error( $result ) ) { |
|
190 | + /* @var WP_Error $result */ |
|
191 | + return $this->app->response()->error( $result ); |
|
192 | + } |
|
193 | + |
|
194 | + return $this->app->response()->success( $result ); |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Removes the post from the tree. |
|
199 | + * |
|
200 | + * Called the delete_post hook. |
|
201 | + * |
|
202 | + * @param int $post_id Post ID. |
|
203 | + * |
|
204 | + * @return boolean |
|
205 | + */ |
|
206 | + public function delete_post( $post_id ) { |
|
207 | + if ( wp_is_post_revision( $post_id ) ) { |
|
208 | + return; |
|
209 | + } |
|
210 | + |
|
211 | + if ( ! $this->app->semaphore()->is_open() ) { |
|
212 | + return $this->app->response()->error( new WP_Error( |
|
213 | + 'semaphore_locked', |
|
214 | + sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::delete_post()' ) |
|
215 | + ) ); |
|
216 | + } |
|
217 | + |
|
218 | + $this->app->semaphore()->lock(); |
|
219 | + $result = $this->app->export()->delete( $post_id ); |
|
220 | + $this->app->semaphore()->unlock(); |
|
221 | + |
|
222 | + if ( is_wp_error( $result ) ) { |
|
223 | + /* @var WP_Error $result */ |
|
224 | + return $this->app->response()->error( $result ); |
|
225 | + } |
|
226 | + |
|
227 | + return $this->app->response()->success( $result ); |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Indicates we're running our own AJAX hook |
|
232 | + * and thus should respond with JSON, rather |
|
233 | + * than just returning data. |
|
234 | + */ |
|
235 | + protected function set_ajax() { |
|
236 | + if ( ! defined( 'WOGH_AJAX' ) ) { |
|
237 | + define( 'WOGH_AJAX', true ); |
|
238 | + } |
|
239 | + } |
|
240 | 240 | } |
@@ -9,139 +9,139 @@ |
||
9 | 9 | */ |
10 | 10 | class Writing_On_GitHub_Request { |
11 | 11 | |
12 | - /** |
|
13 | - * Application container. |
|
14 | - * |
|
15 | - * @var Writing_On_GitHub |
|
16 | - */ |
|
17 | - protected $app; |
|
18 | - |
|
19 | - /** |
|
20 | - * Raw request data. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - protected $raw_data; |
|
25 | - |
|
26 | - /** |
|
27 | - * Headers |
|
28 | - * @var array |
|
29 | - */ |
|
30 | - protected $headers; |
|
31 | - |
|
32 | - /** |
|
33 | - * Writing_On_GitHub_Request constructor. |
|
34 | - * |
|
35 | - * @param Writing_On_GitHub $app Application container. |
|
36 | - */ |
|
37 | - public function __construct( Writing_On_GitHub $app ) { |
|
38 | - $this->app = $app; |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * Validates the header's secret. |
|
43 | - * |
|
44 | - * @return true|WP_Error |
|
45 | - */ |
|
46 | - public function is_secret_valid() { |
|
47 | - $headers = $this->headers(); |
|
48 | - |
|
49 | - $this->raw_data = $this->read_raw_data(); |
|
50 | - |
|
51 | - // Validate request secret. |
|
52 | - $hash = hash_hmac( 'sha1', $this->raw_data, $this->secret() ); |
|
53 | - if ( 'sha1=' . $hash !== $headers['X-Hub-Signature'] ) { |
|
54 | - return false; |
|
55 | - } |
|
56 | - |
|
57 | - // [X-Hub-Signature] => sha1=3cf3da70de401f7dfff053392f60cc534efed3b4 |
|
58 | - // [Content-Type] => application/json |
|
59 | - // [X-Github-Delivery] => b2102500-0acf-11e7-8acb-fd86a3497c2f |
|
60 | - // [X-Github-Event] => ping |
|
61 | - |
|
62 | - return true; |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * Validates the ping event. |
|
67 | - * @return boolean |
|
68 | - */ |
|
69 | - public function is_ping() { |
|
70 | - return 'ping' == $this->webhook_event(); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Validates the push event. |
|
75 | - * @return boolean |
|
76 | - */ |
|
77 | - public function is_push() { |
|
78 | - return 'push' == $this->webhook_event(); |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Return X-Github-Event in headers. |
|
83 | - * @return string |
|
84 | - */ |
|
85 | - public function webhook_event() { |
|
86 | - $headers = $this->headers(); |
|
87 | - return $headers['X-Github-Event']; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Returns a payload object for the given request. |
|
92 | - * |
|
93 | - * @return Writing_On_GitHub_Payload |
|
94 | - */ |
|
95 | - public function payload() { |
|
96 | - return new Writing_On_GitHub_Payload( $this->app, $this->raw_data ); |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Cross-server header support. |
|
101 | - * |
|
102 | - * Returns an array of the request's headers. |
|
103 | - * |
|
104 | - * @return array |
|
105 | - */ |
|
106 | - protected function headers() { |
|
107 | - if ( ! empty( $this->headers ) ) { |
|
108 | - return $this->headers; |
|
109 | - } |
|
110 | - |
|
111 | - if ( function_exists( 'getallheaders' ) ) { |
|
112 | - |
|
113 | - $this->headers = getallheaders(); |
|
114 | - return $this->headers; |
|
115 | - } |
|
116 | - /** |
|
117 | - * Nginx and pre 5.4 workaround. |
|
118 | - * @see http://www.php.net/manual/en/function.getallheaders.php |
|
119 | - */ |
|
120 | - $this->headers = array(); |
|
121 | - foreach ( $_SERVER as $name => $value ) { |
|
122 | - if ( 'HTTP_' === substr( $name, 0, 5 ) ) { |
|
123 | - $this->headers[ str_replace( ' ', '-', ucwords( strtolower( str_replace( '_', ' ', substr( $name, 5 ) ) ) ) ) ] = $value; |
|
124 | - } |
|
125 | - } |
|
126 | - |
|
127 | - return $this->headers; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Reads the raw data from STDIN. |
|
132 | - * |
|
133 | - * @return string |
|
134 | - */ |
|
135 | - protected function read_raw_data() { |
|
136 | - return file_get_contents( 'php://input' ); |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Returns the Webhook secret |
|
141 | - * |
|
142 | - * @return string |
|
143 | - */ |
|
144 | - protected function secret() { |
|
145 | - return get_option( 'wogh_secret' ); |
|
146 | - } |
|
12 | + /** |
|
13 | + * Application container. |
|
14 | + * |
|
15 | + * @var Writing_On_GitHub |
|
16 | + */ |
|
17 | + protected $app; |
|
18 | + |
|
19 | + /** |
|
20 | + * Raw request data. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + protected $raw_data; |
|
25 | + |
|
26 | + /** |
|
27 | + * Headers |
|
28 | + * @var array |
|
29 | + */ |
|
30 | + protected $headers; |
|
31 | + |
|
32 | + /** |
|
33 | + * Writing_On_GitHub_Request constructor. |
|
34 | + * |
|
35 | + * @param Writing_On_GitHub $app Application container. |
|
36 | + */ |
|
37 | + public function __construct( Writing_On_GitHub $app ) { |
|
38 | + $this->app = $app; |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * Validates the header's secret. |
|
43 | + * |
|
44 | + * @return true|WP_Error |
|
45 | + */ |
|
46 | + public function is_secret_valid() { |
|
47 | + $headers = $this->headers(); |
|
48 | + |
|
49 | + $this->raw_data = $this->read_raw_data(); |
|
50 | + |
|
51 | + // Validate request secret. |
|
52 | + $hash = hash_hmac( 'sha1', $this->raw_data, $this->secret() ); |
|
53 | + if ( 'sha1=' . $hash !== $headers['X-Hub-Signature'] ) { |
|
54 | + return false; |
|
55 | + } |
|
56 | + |
|
57 | + // [X-Hub-Signature] => sha1=3cf3da70de401f7dfff053392f60cc534efed3b4 |
|
58 | + // [Content-Type] => application/json |
|
59 | + // [X-Github-Delivery] => b2102500-0acf-11e7-8acb-fd86a3497c2f |
|
60 | + // [X-Github-Event] => ping |
|
61 | + |
|
62 | + return true; |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * Validates the ping event. |
|
67 | + * @return boolean |
|
68 | + */ |
|
69 | + public function is_ping() { |
|
70 | + return 'ping' == $this->webhook_event(); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Validates the push event. |
|
75 | + * @return boolean |
|
76 | + */ |
|
77 | + public function is_push() { |
|
78 | + return 'push' == $this->webhook_event(); |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Return X-Github-Event in headers. |
|
83 | + * @return string |
|
84 | + */ |
|
85 | + public function webhook_event() { |
|
86 | + $headers = $this->headers(); |
|
87 | + return $headers['X-Github-Event']; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Returns a payload object for the given request. |
|
92 | + * |
|
93 | + * @return Writing_On_GitHub_Payload |
|
94 | + */ |
|
95 | + public function payload() { |
|
96 | + return new Writing_On_GitHub_Payload( $this->app, $this->raw_data ); |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Cross-server header support. |
|
101 | + * |
|
102 | + * Returns an array of the request's headers. |
|
103 | + * |
|
104 | + * @return array |
|
105 | + */ |
|
106 | + protected function headers() { |
|
107 | + if ( ! empty( $this->headers ) ) { |
|
108 | + return $this->headers; |
|
109 | + } |
|
110 | + |
|
111 | + if ( function_exists( 'getallheaders' ) ) { |
|
112 | + |
|
113 | + $this->headers = getallheaders(); |
|
114 | + return $this->headers; |
|
115 | + } |
|
116 | + /** |
|
117 | + * Nginx and pre 5.4 workaround. |
|
118 | + * @see http://www.php.net/manual/en/function.getallheaders.php |
|
119 | + */ |
|
120 | + $this->headers = array(); |
|
121 | + foreach ( $_SERVER as $name => $value ) { |
|
122 | + if ( 'HTTP_' === substr( $name, 0, 5 ) ) { |
|
123 | + $this->headers[ str_replace( ' ', '-', ucwords( strtolower( str_replace( '_', ' ', substr( $name, 5 ) ) ) ) ) ] = $value; |
|
124 | + } |
|
125 | + } |
|
126 | + |
|
127 | + return $this->headers; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Reads the raw data from STDIN. |
|
132 | + * |
|
133 | + * @return string |
|
134 | + */ |
|
135 | + protected function read_raw_data() { |
|
136 | + return file_get_contents( 'php://input' ); |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Returns the Webhook secret |
|
141 | + * |
|
142 | + * @return string |
|
143 | + */ |
|
144 | + protected function secret() { |
|
145 | + return get_option( 'wogh_secret' ); |
|
146 | + } |
|
147 | 147 | } |
@@ -10,330 +10,330 @@ |
||
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 | - * @param boolean $force |
|
59 | - * |
|
60 | - * @return true|WP_Error |
|
61 | - */ |
|
62 | - protected function import_files( $files, $force = false ) { |
|
63 | - |
|
64 | - $error = true; |
|
65 | - |
|
66 | - foreach ( $files as $file ) { |
|
67 | - if ( ! $this->importable_file( $file ) ) { |
|
68 | - continue; |
|
69 | - } |
|
70 | - |
|
71 | - $blob = $this->app->api()->fetch()->blob( $file ); |
|
72 | - // network error ? |
|
73 | - if ( ! $blob instanceof Writing_On_GitHub_Blob ) { |
|
74 | - continue; |
|
75 | - } |
|
76 | - |
|
77 | - $is_remove = 'removed' == $file->status; |
|
78 | - |
|
79 | - $result = false; |
|
80 | - if ( $this->importable_raw_file( $blob ) ) { |
|
81 | - $result = $this->import_raw_file( $blob, $is_remove ); |
|
82 | - } elseif ( $this->importable_post( $blob ) ) { |
|
83 | - if ( $is_remove ) { |
|
84 | - $result = $this->delete_post( $blob ); |
|
85 | - } else { |
|
86 | - $result = $this->import_post( $blob, $force ); |
|
87 | - } |
|
88 | - } |
|
89 | - |
|
90 | - if ( is_wp_error( $result ) ) { |
|
91 | - /* @var WP_Error $result */ |
|
92 | - $error = wogh_append_error( $error, $result ); |
|
93 | - } |
|
94 | - } |
|
95 | - |
|
96 | - return $error; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Imports the latest commit on the master branch. |
|
101 | - * |
|
102 | - * @param boolean $force |
|
103 | - * @return string|WP_Error |
|
104 | - */ |
|
105 | - public function master( $force = false ) { |
|
106 | - $result = $this->app->api()->fetch()->tree_recursive(); |
|
107 | - |
|
108 | - if ( is_wp_error( $result ) ) { |
|
109 | - /* @var WP_Error $result */ |
|
110 | - return $result; |
|
111 | - } |
|
112 | - |
|
113 | - if ( is_array( $result ) ) { |
|
114 | - $result = $this->import_files( $result, $force ); |
|
115 | - } |
|
116 | - |
|
117 | - if ( is_wp_error( $result ) ) { |
|
118 | - /* @var WP_Error $result */ |
|
119 | - return $result; |
|
120 | - } |
|
121 | - |
|
122 | - return __( 'Payload processed', 'writing-on-github' ); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Checks whether the provided blob should be imported. |
|
127 | - * |
|
128 | - * @param Writing_On_GitHub_File_Info $file |
|
129 | - * |
|
130 | - * @return bool |
|
131 | - */ |
|
132 | - protected function importable_file( Writing_On_GitHub_File_Info $file ) { |
|
133 | - |
|
134 | - $path = $file->path; |
|
135 | - |
|
136 | - // only _pages, _posts and images |
|
137 | - $prefixs = array( '_pages/', '_posts/', 'images/'); |
|
138 | - foreach ($prefixs as $prefix) { |
|
139 | - if ( ! strncasecmp($path, $prefix, strlen( $prefix ) ) ) { |
|
140 | - return true; |
|
141 | - } |
|
142 | - } |
|
143 | - return false; |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Checks whether the provided blob should be imported. |
|
148 | - * |
|
149 | - * @param Writing_On_GitHub_Blob $blob Blob to validate. |
|
150 | - * |
|
151 | - * @return bool |
|
152 | - */ |
|
153 | - protected function importable_post( Writing_On_GitHub_Blob $blob ) { |
|
154 | - // global $wpdb; |
|
155 | - |
|
156 | - // // Skip the repo's readme. |
|
157 | - // if ( 'readme' === strtolower( substr( $blob->path(), 0, 6 ) ) ) { |
|
158 | - // return false; |
|
159 | - // } |
|
160 | - |
|
161 | - // // If the blob sha already matches a post, then move on. |
|
162 | - // if ( ! is_wp_error( $this->app->database()->fetch_by_sha( $blob->sha() ) ) ) { |
|
163 | - // return false; |
|
164 | - // } |
|
165 | - |
|
166 | - if ( ! $blob->has_frontmatter() ) { |
|
167 | - return false; |
|
168 | - } |
|
169 | - |
|
170 | - return true; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Delete post |
|
175 | - * @param Writing_On_GitHub_Blob $blob |
|
176 | - * @return WP_Error|bool |
|
177 | - */ |
|
178 | - protected function delete_post( Writing_On_GitHub_Blob $blob ) { |
|
179 | - $id = $blob->id(); |
|
180 | - if ( empty( $id ) ) { |
|
181 | - return false; |
|
182 | - } |
|
183 | - $result = $this->app->database()->delete_post( $id ); |
|
184 | - if ( is_wp_error( $result ) ) { |
|
185 | - /* @var WP_Error $result */ |
|
186 | - return $result; |
|
187 | - } |
|
188 | - return true; |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Imports a post into wordpress |
|
193 | - * @param Writing_On_GitHub_Blob $blob |
|
194 | - * @param boolean $force |
|
195 | - * @return WP_Error|bool |
|
196 | - */ |
|
197 | - protected function import_post( Writing_On_GitHub_Blob $blob, $force = false ) { |
|
198 | - $post = $this->blob_to_post( $blob, $force ); |
|
199 | - |
|
200 | - if ( ! $post instanceof Writing_On_GitHub_Post ) { |
|
201 | - return false; |
|
202 | - } |
|
203 | - |
|
204 | - $result = $this->app->database()->save_post( $post ); |
|
205 | - if ( is_wp_error( $result ) ) { |
|
206 | - /** @var WP_Error $result */ |
|
207 | - return $result; |
|
208 | - } |
|
209 | - |
|
210 | - if ( $post->is_new() || |
|
211 | - ! wogh_equal_front_matter( $post, $blob ) ) { |
|
212 | - |
|
213 | - $result = $this->app->export()->export_post( $post ); |
|
214 | - |
|
215 | - if ( is_wp_error( $result ) ) { |
|
216 | - /** @var WP_Error $result */ |
|
217 | - return $result; |
|
218 | - } |
|
219 | - } |
|
220 | - |
|
221 | - clean_post_cache( $post->id() ); |
|
222 | - |
|
223 | - return true; |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * import raw file |
|
228 | - * @param Writing_On_GitHub_Blob $blob |
|
229 | - * @return bool |
|
230 | - */ |
|
231 | - protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { |
|
232 | - if ( $blob->has_frontmatter() ) { |
|
233 | - return false; |
|
234 | - } |
|
235 | - |
|
236 | - // only images |
|
237 | - if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) { |
|
238 | - return false; |
|
239 | - } |
|
240 | - |
|
241 | - return true; |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * Imports a raw file content into file system. |
|
246 | - * @param Writing_On_GitHub_Blob $blob |
|
247 | - * @param bool $is_remove |
|
248 | - */ |
|
249 | - protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { |
|
250 | - $arr = wp_upload_dir(); |
|
251 | - $path = $arr['basedir'] . '/writing-on-github/' . $blob->path(); |
|
252 | - if ( $is_remove ) { |
|
253 | - if ( file_exists($path) ) { |
|
254 | - unlink($path); |
|
255 | - } |
|
256 | - } else { |
|
257 | - $dirname = dirname($path); |
|
258 | - if ( ! file_exists($dirname) ) { |
|
259 | - wp_mkdir_p($dirname); |
|
260 | - } |
|
261 | - |
|
262 | - file_put_contents($path, $blob->content()); |
|
263 | - } |
|
264 | - return true; |
|
265 | - } |
|
266 | - |
|
267 | - /** |
|
268 | - * Imports a single blob content into matching post. |
|
269 | - * |
|
270 | - * @param Writing_On_GitHub_Blob $blob Blob to transform into a Post. |
|
271 | - * @param boolean $force |
|
272 | - * |
|
273 | - * @return Writing_On_GitHub_Post|false |
|
274 | - */ |
|
275 | - protected function blob_to_post( Writing_On_GitHub_Blob $blob, $force = false ) { |
|
276 | - $args = array( 'post_content' => $blob->content_import() ); |
|
277 | - $meta = $blob->meta(); |
|
278 | - |
|
279 | - $id = false; |
|
280 | - |
|
281 | - if ( ! empty( $meta ) ) { |
|
282 | - if ( array_key_exists( 'layout', $meta ) ) { |
|
283 | - $args['post_type'] = $meta['layout']; |
|
284 | - unset( $meta['layout'] ); |
|
285 | - } |
|
286 | - |
|
287 | - if ( array_key_exists( 'published', $meta ) ) { |
|
288 | - $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; |
|
289 | - unset( $meta['published'] ); |
|
290 | - } |
|
291 | - |
|
292 | - if ( array_key_exists( 'post_title', $meta ) ) { |
|
293 | - $args['post_title'] = $meta['post_title']; |
|
294 | - unset( $meta['post_title'] ); |
|
295 | - } |
|
296 | - |
|
297 | - if ( array_key_exists( 'post_name', $meta ) ) { |
|
298 | - $args['post_name'] = $meta['post_name']; |
|
299 | - unset( $meta['post_name'] ); |
|
300 | - } |
|
301 | - |
|
302 | - if ( array_key_exists( 'ID', $meta ) ) { |
|
303 | - $id = $args['ID'] = $meta['ID']; |
|
304 | - $blob->set_id($id); |
|
305 | - unset( $meta['ID'] ); |
|
306 | - } |
|
307 | - |
|
308 | - if ( array_key_exists( 'post_date', $meta ) ) { |
|
309 | - if ( empty( $meta['post_date'] ) ) { |
|
310 | - $meta['post_date'] = current_time( 'mysql' ); |
|
311 | - } |
|
312 | - |
|
313 | - $args['post_date'] = $meta['post_date']; |
|
314 | - unset( $meta['post_date'] ); |
|
315 | - } |
|
316 | - } |
|
317 | - |
|
318 | - $meta['_wogh_sha'] = $blob->sha(); |
|
319 | - |
|
320 | - if ( ! $force && $id ) { |
|
321 | - $old_sha = get_post_meta( $id, '_wogh_sha', true ); |
|
322 | - $old_github_path = get_post_meta( $id, '_wogh_github_path', true ); |
|
323 | - |
|
324 | - // dont save post when has same sha |
|
325 | - if ( $old_sha && $old_sha == $meta['_wogh_sha'] && |
|
326 | - $old_github_path && $old_github_path == $blob->path() ) { |
|
327 | - return false; |
|
328 | - } |
|
329 | - } |
|
330 | - |
|
331 | - $post = new Writing_On_GitHub_Post( $args, $this->app->api() ); |
|
332 | - $post->set_old_github_path( $blob->path() ); |
|
333 | - $post->set_meta( $meta ); |
|
334 | - $post->set_blob( $blob ); |
|
335 | - $blob->set_id( $post->id() ); |
|
336 | - |
|
337 | - return $post; |
|
338 | - } |
|
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 | + * @param boolean $force |
|
59 | + * |
|
60 | + * @return true|WP_Error |
|
61 | + */ |
|
62 | + protected function import_files( $files, $force = false ) { |
|
63 | + |
|
64 | + $error = true; |
|
65 | + |
|
66 | + foreach ( $files as $file ) { |
|
67 | + if ( ! $this->importable_file( $file ) ) { |
|
68 | + continue; |
|
69 | + } |
|
70 | + |
|
71 | + $blob = $this->app->api()->fetch()->blob( $file ); |
|
72 | + // network error ? |
|
73 | + if ( ! $blob instanceof Writing_On_GitHub_Blob ) { |
|
74 | + continue; |
|
75 | + } |
|
76 | + |
|
77 | + $is_remove = 'removed' == $file->status; |
|
78 | + |
|
79 | + $result = false; |
|
80 | + if ( $this->importable_raw_file( $blob ) ) { |
|
81 | + $result = $this->import_raw_file( $blob, $is_remove ); |
|
82 | + } elseif ( $this->importable_post( $blob ) ) { |
|
83 | + if ( $is_remove ) { |
|
84 | + $result = $this->delete_post( $blob ); |
|
85 | + } else { |
|
86 | + $result = $this->import_post( $blob, $force ); |
|
87 | + } |
|
88 | + } |
|
89 | + |
|
90 | + if ( is_wp_error( $result ) ) { |
|
91 | + /* @var WP_Error $result */ |
|
92 | + $error = wogh_append_error( $error, $result ); |
|
93 | + } |
|
94 | + } |
|
95 | + |
|
96 | + return $error; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Imports the latest commit on the master branch. |
|
101 | + * |
|
102 | + * @param boolean $force |
|
103 | + * @return string|WP_Error |
|
104 | + */ |
|
105 | + public function master( $force = false ) { |
|
106 | + $result = $this->app->api()->fetch()->tree_recursive(); |
|
107 | + |
|
108 | + if ( is_wp_error( $result ) ) { |
|
109 | + /* @var WP_Error $result */ |
|
110 | + return $result; |
|
111 | + } |
|
112 | + |
|
113 | + if ( is_array( $result ) ) { |
|
114 | + $result = $this->import_files( $result, $force ); |
|
115 | + } |
|
116 | + |
|
117 | + if ( is_wp_error( $result ) ) { |
|
118 | + /* @var WP_Error $result */ |
|
119 | + return $result; |
|
120 | + } |
|
121 | + |
|
122 | + return __( 'Payload processed', 'writing-on-github' ); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Checks whether the provided blob should be imported. |
|
127 | + * |
|
128 | + * @param Writing_On_GitHub_File_Info $file |
|
129 | + * |
|
130 | + * @return bool |
|
131 | + */ |
|
132 | + protected function importable_file( Writing_On_GitHub_File_Info $file ) { |
|
133 | + |
|
134 | + $path = $file->path; |
|
135 | + |
|
136 | + // only _pages, _posts and images |
|
137 | + $prefixs = array( '_pages/', '_posts/', 'images/'); |
|
138 | + foreach ($prefixs as $prefix) { |
|
139 | + if ( ! strncasecmp($path, $prefix, strlen( $prefix ) ) ) { |
|
140 | + return true; |
|
141 | + } |
|
142 | + } |
|
143 | + return false; |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Checks whether the provided blob should be imported. |
|
148 | + * |
|
149 | + * @param Writing_On_GitHub_Blob $blob Blob to validate. |
|
150 | + * |
|
151 | + * @return bool |
|
152 | + */ |
|
153 | + protected function importable_post( Writing_On_GitHub_Blob $blob ) { |
|
154 | + // global $wpdb; |
|
155 | + |
|
156 | + // // Skip the repo's readme. |
|
157 | + // if ( 'readme' === strtolower( substr( $blob->path(), 0, 6 ) ) ) { |
|
158 | + // return false; |
|
159 | + // } |
|
160 | + |
|
161 | + // // If the blob sha already matches a post, then move on. |
|
162 | + // if ( ! is_wp_error( $this->app->database()->fetch_by_sha( $blob->sha() ) ) ) { |
|
163 | + // return false; |
|
164 | + // } |
|
165 | + |
|
166 | + if ( ! $blob->has_frontmatter() ) { |
|
167 | + return false; |
|
168 | + } |
|
169 | + |
|
170 | + return true; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Delete post |
|
175 | + * @param Writing_On_GitHub_Blob $blob |
|
176 | + * @return WP_Error|bool |
|
177 | + */ |
|
178 | + protected function delete_post( Writing_On_GitHub_Blob $blob ) { |
|
179 | + $id = $blob->id(); |
|
180 | + if ( empty( $id ) ) { |
|
181 | + return false; |
|
182 | + } |
|
183 | + $result = $this->app->database()->delete_post( $id ); |
|
184 | + if ( is_wp_error( $result ) ) { |
|
185 | + /* @var WP_Error $result */ |
|
186 | + return $result; |
|
187 | + } |
|
188 | + return true; |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Imports a post into wordpress |
|
193 | + * @param Writing_On_GitHub_Blob $blob |
|
194 | + * @param boolean $force |
|
195 | + * @return WP_Error|bool |
|
196 | + */ |
|
197 | + protected function import_post( Writing_On_GitHub_Blob $blob, $force = false ) { |
|
198 | + $post = $this->blob_to_post( $blob, $force ); |
|
199 | + |
|
200 | + if ( ! $post instanceof Writing_On_GitHub_Post ) { |
|
201 | + return false; |
|
202 | + } |
|
203 | + |
|
204 | + $result = $this->app->database()->save_post( $post ); |
|
205 | + if ( is_wp_error( $result ) ) { |
|
206 | + /** @var WP_Error $result */ |
|
207 | + return $result; |
|
208 | + } |
|
209 | + |
|
210 | + if ( $post->is_new() || |
|
211 | + ! wogh_equal_front_matter( $post, $blob ) ) { |
|
212 | + |
|
213 | + $result = $this->app->export()->export_post( $post ); |
|
214 | + |
|
215 | + if ( is_wp_error( $result ) ) { |
|
216 | + /** @var WP_Error $result */ |
|
217 | + return $result; |
|
218 | + } |
|
219 | + } |
|
220 | + |
|
221 | + clean_post_cache( $post->id() ); |
|
222 | + |
|
223 | + return true; |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * import raw file |
|
228 | + * @param Writing_On_GitHub_Blob $blob |
|
229 | + * @return bool |
|
230 | + */ |
|
231 | + protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { |
|
232 | + if ( $blob->has_frontmatter() ) { |
|
233 | + return false; |
|
234 | + } |
|
235 | + |
|
236 | + // only images |
|
237 | + if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) { |
|
238 | + return false; |
|
239 | + } |
|
240 | + |
|
241 | + return true; |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * Imports a raw file content into file system. |
|
246 | + * @param Writing_On_GitHub_Blob $blob |
|
247 | + * @param bool $is_remove |
|
248 | + */ |
|
249 | + protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { |
|
250 | + $arr = wp_upload_dir(); |
|
251 | + $path = $arr['basedir'] . '/writing-on-github/' . $blob->path(); |
|
252 | + if ( $is_remove ) { |
|
253 | + if ( file_exists($path) ) { |
|
254 | + unlink($path); |
|
255 | + } |
|
256 | + } else { |
|
257 | + $dirname = dirname($path); |
|
258 | + if ( ! file_exists($dirname) ) { |
|
259 | + wp_mkdir_p($dirname); |
|
260 | + } |
|
261 | + |
|
262 | + file_put_contents($path, $blob->content()); |
|
263 | + } |
|
264 | + return true; |
|
265 | + } |
|
266 | + |
|
267 | + /** |
|
268 | + * Imports a single blob content into matching post. |
|
269 | + * |
|
270 | + * @param Writing_On_GitHub_Blob $blob Blob to transform into a Post. |
|
271 | + * @param boolean $force |
|
272 | + * |
|
273 | + * @return Writing_On_GitHub_Post|false |
|
274 | + */ |
|
275 | + protected function blob_to_post( Writing_On_GitHub_Blob $blob, $force = false ) { |
|
276 | + $args = array( 'post_content' => $blob->content_import() ); |
|
277 | + $meta = $blob->meta(); |
|
278 | + |
|
279 | + $id = false; |
|
280 | + |
|
281 | + if ( ! empty( $meta ) ) { |
|
282 | + if ( array_key_exists( 'layout', $meta ) ) { |
|
283 | + $args['post_type'] = $meta['layout']; |
|
284 | + unset( $meta['layout'] ); |
|
285 | + } |
|
286 | + |
|
287 | + if ( array_key_exists( 'published', $meta ) ) { |
|
288 | + $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; |
|
289 | + unset( $meta['published'] ); |
|
290 | + } |
|
291 | + |
|
292 | + if ( array_key_exists( 'post_title', $meta ) ) { |
|
293 | + $args['post_title'] = $meta['post_title']; |
|
294 | + unset( $meta['post_title'] ); |
|
295 | + } |
|
296 | + |
|
297 | + if ( array_key_exists( 'post_name', $meta ) ) { |
|
298 | + $args['post_name'] = $meta['post_name']; |
|
299 | + unset( $meta['post_name'] ); |
|
300 | + } |
|
301 | + |
|
302 | + if ( array_key_exists( 'ID', $meta ) ) { |
|
303 | + $id = $args['ID'] = $meta['ID']; |
|
304 | + $blob->set_id($id); |
|
305 | + unset( $meta['ID'] ); |
|
306 | + } |
|
307 | + |
|
308 | + if ( array_key_exists( 'post_date', $meta ) ) { |
|
309 | + if ( empty( $meta['post_date'] ) ) { |
|
310 | + $meta['post_date'] = current_time( 'mysql' ); |
|
311 | + } |
|
312 | + |
|
313 | + $args['post_date'] = $meta['post_date']; |
|
314 | + unset( $meta['post_date'] ); |
|
315 | + } |
|
316 | + } |
|
317 | + |
|
318 | + $meta['_wogh_sha'] = $blob->sha(); |
|
319 | + |
|
320 | + if ( ! $force && $id ) { |
|
321 | + $old_sha = get_post_meta( $id, '_wogh_sha', true ); |
|
322 | + $old_github_path = get_post_meta( $id, '_wogh_github_path', true ); |
|
323 | + |
|
324 | + // dont save post when has same sha |
|
325 | + if ( $old_sha && $old_sha == $meta['_wogh_sha'] && |
|
326 | + $old_github_path && $old_github_path == $blob->path() ) { |
|
327 | + return false; |
|
328 | + } |
|
329 | + } |
|
330 | + |
|
331 | + $post = new Writing_On_GitHub_Post( $args, $this->app->api() ); |
|
332 | + $post->set_old_github_path( $blob->path() ); |
|
333 | + $post->set_meta( $meta ); |
|
334 | + $post->set_blob( $blob ); |
|
335 | + $blob->set_id( $post->id() ); |
|
336 | + |
|
337 | + return $post; |
|
338 | + } |
|
339 | 339 | } |