@@ -10,307 +10,307 @@ |
||
10 | 10 | */ |
11 | 11 | class Writing_On_GitHub_Import { |
12 | 12 | |
13 | - /** |
|
14 | - * Application container. |
|
15 | - * |
|
16 | - * @var Writing_On_GitHub |
|
17 | - */ |
|
18 | - protected $app; |
|
19 | - |
|
20 | - /** |
|
21 | - * Initializes a new import manager. |
|
22 | - * |
|
23 | - * @param Writing_On_GitHub $app Application container. |
|
24 | - */ |
|
25 | - public function __construct( Writing_On_GitHub $app ) { |
|
26 | - $this->app = $app; |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * Imports a payload. |
|
31 | - * @param Writing_On_GitHub_Payload $payload |
|
32 | - * |
|
33 | - * @return string|WP_Error |
|
34 | - */ |
|
35 | - public function payload( Writing_On_GitHub_Payload $payload ) { |
|
36 | - |
|
37 | - $result = $this->app->api()->fetch()->compare( $payload->get_before_commit_id() ); |
|
38 | - |
|
39 | - if ( is_wp_error( $result ) ) { |
|
40 | - /* @var WP_Error $result */ |
|
41 | - return $result; |
|
42 | - } |
|
43 | - |
|
44 | - if ( is_array( $result ) ) { |
|
45 | - $result = $this->import_files( $result ); |
|
46 | - } |
|
47 | - |
|
48 | - if ( is_wp_error( $result ) ) { |
|
49 | - return $files; |
|
50 | - } |
|
51 | - |
|
52 | - return __( 'Payload processed', 'writing-on-github' ); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * import blob by files |
|
57 | - * @param Writing_On_GitHub_File_Info[] $files |
|
58 | - * |
|
59 | - * @return true|WP_Error |
|
60 | - */ |
|
61 | - protected function import_files( $files ) { |
|
62 | - |
|
63 | - $error = true; |
|
64 | - |
|
65 | - foreach ( $files as $file ) { |
|
66 | - if ( ! $this->importable_file( $file ) ) { |
|
67 | - continue; |
|
68 | - } |
|
69 | - |
|
70 | - $blob = $this->app->api()->fetch()->blob( $file ); |
|
71 | - // network error ? |
|
72 | - if ( ! $blob instanceof Writing_On_GitHub_Blob ) { |
|
73 | - continue; |
|
74 | - } |
|
75 | - |
|
76 | - $is_remove = 'removed' == $file->status; |
|
77 | - |
|
78 | - $result = false; |
|
79 | - if ( $this->importable_raw_file( $blob ) ) { |
|
80 | - $result = $this->import_raw_file( $blob, $is_remove ); |
|
81 | - } elseif ( $this->importable_post( $blob ) ) { |
|
82 | - $result = $this->import_post( $blob, $is_remove ); |
|
83 | - } |
|
84 | - |
|
85 | - if ( is_wp_error( $result ) ) { |
|
86 | - /* @var WP_Error $result */ |
|
87 | - $error = wogh_append_error( $error, $result ); |
|
88 | - } |
|
89 | - } |
|
90 | - |
|
91 | - return $error; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * Imports the latest commit on the master branch. |
|
96 | - * |
|
97 | - * @return string|WP_Error |
|
98 | - */ |
|
99 | - public function master() { |
|
100 | - $result = $this->app->api()->fetch()->tree_recursive(); |
|
101 | - |
|
102 | - if ( is_wp_error( $result ) ) { |
|
103 | - /* @var WP_Error $result */ |
|
104 | - return $result; |
|
105 | - } |
|
106 | - |
|
107 | - if ( is_array( $result ) ) { |
|
108 | - $result = $this->import_files( $result ); |
|
109 | - } |
|
110 | - |
|
111 | - if ( is_wp_error( $result ) ) { |
|
112 | - /* @var WP_Error $result */ |
|
113 | - return $result; |
|
114 | - } |
|
115 | - |
|
116 | - return __( 'Payload processed', 'writing-on-github' ); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Checks whether the provided blob should be imported. |
|
121 | - * |
|
122 | - * @param Writing_On_GitHub_File_Info $file |
|
123 | - * |
|
124 | - * @return bool |
|
125 | - */ |
|
126 | - protected function importable_file( Writing_On_GitHub_File_Info $file ) { |
|
127 | - |
|
128 | - $path = $file->path; |
|
129 | - |
|
130 | - // only _pages, _posts and images |
|
131 | - $prefixs = array( '_pages/', '_posts/', 'images/'); |
|
132 | - foreach ($prefixs as $prefix) { |
|
133 | - if ( ! strncasecmp($path, $prefix, strlen( $prefix ) ) ) { |
|
134 | - return true; |
|
135 | - } |
|
136 | - } |
|
137 | - return false; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Checks whether the provided blob should be imported. |
|
142 | - * |
|
143 | - * @param Writing_On_GitHub_Blob $blob Blob to validate. |
|
144 | - * |
|
145 | - * @return bool |
|
146 | - */ |
|
147 | - protected function importable_post( Writing_On_GitHub_Blob $blob ) { |
|
148 | - // global $wpdb; |
|
149 | - |
|
150 | - // // Skip the repo's readme. |
|
151 | - // if ( 'readme' === strtolower( substr( $blob->path(), 0, 6 ) ) ) { |
|
152 | - // return false; |
|
153 | - // } |
|
154 | - |
|
155 | - // // If the blob sha already matches a post, then move on. |
|
156 | - // if ( ! is_wp_error( $this->app->database()->fetch_by_sha( $blob->sha() ) ) ) { |
|
157 | - // return false; |
|
158 | - // } |
|
159 | - |
|
160 | - if ( ! $blob->has_frontmatter() ) { |
|
161 | - return false; |
|
162 | - } |
|
163 | - |
|
164 | - return true; |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Imports a post into wordpress |
|
169 | - * @param Writing_On_GitHub_Blob $blob |
|
170 | - * @param bool $is_remove |
|
171 | - * @return WP_Error|bool |
|
172 | - */ |
|
173 | - protected function import_post( Writing_On_GitHub_Blob $blob, $is_remove ) { |
|
174 | - |
|
175 | - if ( $is_remove ) { |
|
176 | - $id = $blob->id(); |
|
177 | - if ( empty( $id ) ) { |
|
178 | - return false; |
|
179 | - } |
|
180 | - $result = $this->app->database()->delete_post( $id ); |
|
181 | - if ( is_wp_error( $result ) ) { |
|
182 | - /* @var WP_Error $result */ |
|
183 | - return $result; |
|
184 | - } |
|
185 | - return true; |
|
186 | - } |
|
187 | - |
|
188 | - $post = $this->blob_to_post( $blob ); |
|
189 | - |
|
190 | - if ( ! $post instanceof Writing_On_GitHub_Post ) { |
|
191 | - return false; |
|
192 | - } |
|
193 | - |
|
194 | - $result = $this->app->database()->save_post( $post ); |
|
195 | - if ( is_wp_error( $result ) ) { |
|
196 | - /** @var WP_Error $result */ |
|
197 | - return $result; |
|
198 | - } |
|
199 | - |
|
200 | - if ( $post->is_new() || |
|
201 | - ! wogh_equal_front_matter( $post, $blob ) ) { |
|
202 | - |
|
203 | - $result = $this->app->export()->new_posts( array( $post ) ); |
|
204 | - |
|
205 | - if ( is_wp_error( $result ) ) { |
|
206 | - /** @var WP_Error $result */ |
|
207 | - return $result; |
|
208 | - } |
|
209 | - } |
|
210 | - |
|
211 | - return true; |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * import raw file |
|
216 | - * @param Writing_On_GitHub_Blob $blob |
|
217 | - * @return bool |
|
218 | - */ |
|
219 | - protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { |
|
220 | - if ( $blob->has_frontmatter() ) { |
|
221 | - return false; |
|
222 | - } |
|
223 | - |
|
224 | - // only images |
|
225 | - if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) { |
|
226 | - return false; |
|
227 | - } |
|
228 | - |
|
229 | - return true; |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * Imports a raw file content into file system. |
|
234 | - * @param Writing_On_GitHub_Blob $blob |
|
235 | - * @param bool $is_remove |
|
236 | - */ |
|
237 | - protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { |
|
238 | - $arr = wp_upload_dir(); |
|
239 | - $path = $arr['basedir'] . '/writing-on-github/' . $blob->path(); |
|
240 | - if ( $is_remove ) { |
|
241 | - if ( file_exists($path) ) { |
|
242 | - unlink($path); |
|
243 | - } |
|
244 | - } else { |
|
245 | - $dirname = dirname($path); |
|
246 | - if ( ! file_exists($dirname) ) { |
|
247 | - wp_mkdir_p($dirname); |
|
248 | - } |
|
249 | - |
|
250 | - file_put_contents($path, $blob->content()); |
|
251 | - } |
|
252 | - return true; |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * Imports a single blob content into matching post. |
|
257 | - * |
|
258 | - * @param Writing_On_GitHub_Blob $blob Blob to transform into a Post. |
|
259 | - * |
|
260 | - * @return Writing_On_GitHub_Post|false |
|
261 | - */ |
|
262 | - protected function blob_to_post( Writing_On_GitHub_Blob $blob ) { |
|
263 | - $args = array( 'post_content' => $blob->content_import() ); |
|
264 | - $meta = $blob->meta(); |
|
265 | - |
|
266 | - $id = false; |
|
267 | - |
|
268 | - if ( ! empty( $meta ) ) { |
|
269 | - if ( array_key_exists( 'layout', $meta ) ) { |
|
270 | - $args['post_type'] = $meta['layout']; |
|
271 | - unset( $meta['layout'] ); |
|
272 | - } |
|
273 | - |
|
274 | - if ( array_key_exists( 'published', $meta ) ) { |
|
275 | - $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; |
|
276 | - unset( $meta['published'] ); |
|
277 | - } |
|
278 | - |
|
279 | - if ( array_key_exists( 'post_title', $meta ) ) { |
|
280 | - $args['post_title'] = $meta['post_title']; |
|
281 | - unset( $meta['post_title'] ); |
|
282 | - } |
|
283 | - |
|
284 | - if ( array_key_exists( 'post_name', $meta ) ) { |
|
285 | - $args['post_name'] = $meta['post_name']; |
|
286 | - unset( $meta['post_name'] ); |
|
287 | - } |
|
288 | - |
|
289 | - if ( array_key_exists( 'ID', $meta ) ) { |
|
290 | - $id = $args['ID'] = $meta['ID']; |
|
291 | - $blob->set_id($id); |
|
292 | - unset( $meta['ID'] ); |
|
293 | - } |
|
294 | - } |
|
295 | - |
|
296 | - $meta['_wogh_sha'] = $blob->sha(); |
|
297 | - |
|
298 | - if ( $id ) { |
|
299 | - $old_sha = get_post_meta( $id, '_wogh_sha', true ); |
|
300 | - $old_github_path = get_post_meta( $id, '_wogh_github_path', true ); |
|
301 | - |
|
302 | - // dont save post when has same sha |
|
303 | - if ( $old_sha && $old_sha == $meta['_wogh_sha'] && |
|
304 | - $old_github_path && $old_github_path == $blob->path() ) { |
|
305 | - return false; |
|
306 | - } |
|
307 | - } |
|
308 | - |
|
309 | - $post = new Writing_On_GitHub_Post( $args, $this->app->api() ); |
|
310 | - $post->set_old_github_path( $blob->path() ); |
|
311 | - $post->set_meta( $meta ); |
|
312 | - $blob->set_id( $post->id() ); |
|
313 | - |
|
314 | - return $post; |
|
315 | - } |
|
13 | + /** |
|
14 | + * Application container. |
|
15 | + * |
|
16 | + * @var Writing_On_GitHub |
|
17 | + */ |
|
18 | + protected $app; |
|
19 | + |
|
20 | + /** |
|
21 | + * Initializes a new import manager. |
|
22 | + * |
|
23 | + * @param Writing_On_GitHub $app Application container. |
|
24 | + */ |
|
25 | + public function __construct( Writing_On_GitHub $app ) { |
|
26 | + $this->app = $app; |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * Imports a payload. |
|
31 | + * @param Writing_On_GitHub_Payload $payload |
|
32 | + * |
|
33 | + * @return string|WP_Error |
|
34 | + */ |
|
35 | + public function payload( Writing_On_GitHub_Payload $payload ) { |
|
36 | + |
|
37 | + $result = $this->app->api()->fetch()->compare( $payload->get_before_commit_id() ); |
|
38 | + |
|
39 | + if ( is_wp_error( $result ) ) { |
|
40 | + /* @var WP_Error $result */ |
|
41 | + return $result; |
|
42 | + } |
|
43 | + |
|
44 | + if ( is_array( $result ) ) { |
|
45 | + $result = $this->import_files( $result ); |
|
46 | + } |
|
47 | + |
|
48 | + if ( is_wp_error( $result ) ) { |
|
49 | + return $files; |
|
50 | + } |
|
51 | + |
|
52 | + return __( 'Payload processed', 'writing-on-github' ); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * import blob by files |
|
57 | + * @param Writing_On_GitHub_File_Info[] $files |
|
58 | + * |
|
59 | + * @return true|WP_Error |
|
60 | + */ |
|
61 | + protected function import_files( $files ) { |
|
62 | + |
|
63 | + $error = true; |
|
64 | + |
|
65 | + foreach ( $files as $file ) { |
|
66 | + if ( ! $this->importable_file( $file ) ) { |
|
67 | + continue; |
|
68 | + } |
|
69 | + |
|
70 | + $blob = $this->app->api()->fetch()->blob( $file ); |
|
71 | + // network error ? |
|
72 | + if ( ! $blob instanceof Writing_On_GitHub_Blob ) { |
|
73 | + continue; |
|
74 | + } |
|
75 | + |
|
76 | + $is_remove = 'removed' == $file->status; |
|
77 | + |
|
78 | + $result = false; |
|
79 | + if ( $this->importable_raw_file( $blob ) ) { |
|
80 | + $result = $this->import_raw_file( $blob, $is_remove ); |
|
81 | + } elseif ( $this->importable_post( $blob ) ) { |
|
82 | + $result = $this->import_post( $blob, $is_remove ); |
|
83 | + } |
|
84 | + |
|
85 | + if ( is_wp_error( $result ) ) { |
|
86 | + /* @var WP_Error $result */ |
|
87 | + $error = wogh_append_error( $error, $result ); |
|
88 | + } |
|
89 | + } |
|
90 | + |
|
91 | + return $error; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * Imports the latest commit on the master branch. |
|
96 | + * |
|
97 | + * @return string|WP_Error |
|
98 | + */ |
|
99 | + public function master() { |
|
100 | + $result = $this->app->api()->fetch()->tree_recursive(); |
|
101 | + |
|
102 | + if ( is_wp_error( $result ) ) { |
|
103 | + /* @var WP_Error $result */ |
|
104 | + return $result; |
|
105 | + } |
|
106 | + |
|
107 | + if ( is_array( $result ) ) { |
|
108 | + $result = $this->import_files( $result ); |
|
109 | + } |
|
110 | + |
|
111 | + if ( is_wp_error( $result ) ) { |
|
112 | + /* @var WP_Error $result */ |
|
113 | + return $result; |
|
114 | + } |
|
115 | + |
|
116 | + return __( 'Payload processed', 'writing-on-github' ); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Checks whether the provided blob should be imported. |
|
121 | + * |
|
122 | + * @param Writing_On_GitHub_File_Info $file |
|
123 | + * |
|
124 | + * @return bool |
|
125 | + */ |
|
126 | + protected function importable_file( Writing_On_GitHub_File_Info $file ) { |
|
127 | + |
|
128 | + $path = $file->path; |
|
129 | + |
|
130 | + // only _pages, _posts and images |
|
131 | + $prefixs = array( '_pages/', '_posts/', 'images/'); |
|
132 | + foreach ($prefixs as $prefix) { |
|
133 | + if ( ! strncasecmp($path, $prefix, strlen( $prefix ) ) ) { |
|
134 | + return true; |
|
135 | + } |
|
136 | + } |
|
137 | + return false; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Checks whether the provided blob should be imported. |
|
142 | + * |
|
143 | + * @param Writing_On_GitHub_Blob $blob Blob to validate. |
|
144 | + * |
|
145 | + * @return bool |
|
146 | + */ |
|
147 | + protected function importable_post( Writing_On_GitHub_Blob $blob ) { |
|
148 | + // global $wpdb; |
|
149 | + |
|
150 | + // // Skip the repo's readme. |
|
151 | + // if ( 'readme' === strtolower( substr( $blob->path(), 0, 6 ) ) ) { |
|
152 | + // return false; |
|
153 | + // } |
|
154 | + |
|
155 | + // // If the blob sha already matches a post, then move on. |
|
156 | + // if ( ! is_wp_error( $this->app->database()->fetch_by_sha( $blob->sha() ) ) ) { |
|
157 | + // return false; |
|
158 | + // } |
|
159 | + |
|
160 | + if ( ! $blob->has_frontmatter() ) { |
|
161 | + return false; |
|
162 | + } |
|
163 | + |
|
164 | + return true; |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Imports a post into wordpress |
|
169 | + * @param Writing_On_GitHub_Blob $blob |
|
170 | + * @param bool $is_remove |
|
171 | + * @return WP_Error|bool |
|
172 | + */ |
|
173 | + protected function import_post( Writing_On_GitHub_Blob $blob, $is_remove ) { |
|
174 | + |
|
175 | + if ( $is_remove ) { |
|
176 | + $id = $blob->id(); |
|
177 | + if ( empty( $id ) ) { |
|
178 | + return false; |
|
179 | + } |
|
180 | + $result = $this->app->database()->delete_post( $id ); |
|
181 | + if ( is_wp_error( $result ) ) { |
|
182 | + /* @var WP_Error $result */ |
|
183 | + return $result; |
|
184 | + } |
|
185 | + return true; |
|
186 | + } |
|
187 | + |
|
188 | + $post = $this->blob_to_post( $blob ); |
|
189 | + |
|
190 | + if ( ! $post instanceof Writing_On_GitHub_Post ) { |
|
191 | + return false; |
|
192 | + } |
|
193 | + |
|
194 | + $result = $this->app->database()->save_post( $post ); |
|
195 | + if ( is_wp_error( $result ) ) { |
|
196 | + /** @var WP_Error $result */ |
|
197 | + return $result; |
|
198 | + } |
|
199 | + |
|
200 | + if ( $post->is_new() || |
|
201 | + ! wogh_equal_front_matter( $post, $blob ) ) { |
|
202 | + |
|
203 | + $result = $this->app->export()->new_posts( array( $post ) ); |
|
204 | + |
|
205 | + if ( is_wp_error( $result ) ) { |
|
206 | + /** @var WP_Error $result */ |
|
207 | + return $result; |
|
208 | + } |
|
209 | + } |
|
210 | + |
|
211 | + return true; |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * import raw file |
|
216 | + * @param Writing_On_GitHub_Blob $blob |
|
217 | + * @return bool |
|
218 | + */ |
|
219 | + protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { |
|
220 | + if ( $blob->has_frontmatter() ) { |
|
221 | + return false; |
|
222 | + } |
|
223 | + |
|
224 | + // only images |
|
225 | + if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) { |
|
226 | + return false; |
|
227 | + } |
|
228 | + |
|
229 | + return true; |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * Imports a raw file content into file system. |
|
234 | + * @param Writing_On_GitHub_Blob $blob |
|
235 | + * @param bool $is_remove |
|
236 | + */ |
|
237 | + protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { |
|
238 | + $arr = wp_upload_dir(); |
|
239 | + $path = $arr['basedir'] . '/writing-on-github/' . $blob->path(); |
|
240 | + if ( $is_remove ) { |
|
241 | + if ( file_exists($path) ) { |
|
242 | + unlink($path); |
|
243 | + } |
|
244 | + } else { |
|
245 | + $dirname = dirname($path); |
|
246 | + if ( ! file_exists($dirname) ) { |
|
247 | + wp_mkdir_p($dirname); |
|
248 | + } |
|
249 | + |
|
250 | + file_put_contents($path, $blob->content()); |
|
251 | + } |
|
252 | + return true; |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * Imports a single blob content into matching post. |
|
257 | + * |
|
258 | + * @param Writing_On_GitHub_Blob $blob Blob to transform into a Post. |
|
259 | + * |
|
260 | + * @return Writing_On_GitHub_Post|false |
|
261 | + */ |
|
262 | + protected function blob_to_post( Writing_On_GitHub_Blob $blob ) { |
|
263 | + $args = array( 'post_content' => $blob->content_import() ); |
|
264 | + $meta = $blob->meta(); |
|
265 | + |
|
266 | + $id = false; |
|
267 | + |
|
268 | + if ( ! empty( $meta ) ) { |
|
269 | + if ( array_key_exists( 'layout', $meta ) ) { |
|
270 | + $args['post_type'] = $meta['layout']; |
|
271 | + unset( $meta['layout'] ); |
|
272 | + } |
|
273 | + |
|
274 | + if ( array_key_exists( 'published', $meta ) ) { |
|
275 | + $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; |
|
276 | + unset( $meta['published'] ); |
|
277 | + } |
|
278 | + |
|
279 | + if ( array_key_exists( 'post_title', $meta ) ) { |
|
280 | + $args['post_title'] = $meta['post_title']; |
|
281 | + unset( $meta['post_title'] ); |
|
282 | + } |
|
283 | + |
|
284 | + if ( array_key_exists( 'post_name', $meta ) ) { |
|
285 | + $args['post_name'] = $meta['post_name']; |
|
286 | + unset( $meta['post_name'] ); |
|
287 | + } |
|
288 | + |
|
289 | + if ( array_key_exists( 'ID', $meta ) ) { |
|
290 | + $id = $args['ID'] = $meta['ID']; |
|
291 | + $blob->set_id($id); |
|
292 | + unset( $meta['ID'] ); |
|
293 | + } |
|
294 | + } |
|
295 | + |
|
296 | + $meta['_wogh_sha'] = $blob->sha(); |
|
297 | + |
|
298 | + if ( $id ) { |
|
299 | + $old_sha = get_post_meta( $id, '_wogh_sha', true ); |
|
300 | + $old_github_path = get_post_meta( $id, '_wogh_github_path', true ); |
|
301 | + |
|
302 | + // dont save post when has same sha |
|
303 | + if ( $old_sha && $old_sha == $meta['_wogh_sha'] && |
|
304 | + $old_github_path && $old_github_path == $blob->path() ) { |
|
305 | + return false; |
|
306 | + } |
|
307 | + } |
|
308 | + |
|
309 | + $post = new Writing_On_GitHub_Post( $args, $this->app->api() ); |
|
310 | + $post->set_old_github_path( $blob->path() ); |
|
311 | + $post->set_meta( $meta ); |
|
312 | + $blob->set_id( $post->id() ); |
|
313 | + |
|
314 | + return $post; |
|
315 | + } |
|
316 | 316 | } |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | * |
23 | 23 | * @param Writing_On_GitHub $app Application container. |
24 | 24 | */ |
25 | - public function __construct( Writing_On_GitHub $app ) { |
|
25 | + public function __construct(Writing_On_GitHub $app) { |
|
26 | 26 | $this->app = $app; |
27 | 27 | } |
28 | 28 | |
@@ -32,24 +32,24 @@ discard block |
||
32 | 32 | * |
33 | 33 | * @return string|WP_Error |
34 | 34 | */ |
35 | - public function payload( Writing_On_GitHub_Payload $payload ) { |
|
35 | + public function payload(Writing_On_GitHub_Payload $payload) { |
|
36 | 36 | |
37 | - $result = $this->app->api()->fetch()->compare( $payload->get_before_commit_id() ); |
|
37 | + $result = $this->app->api()->fetch()->compare($payload->get_before_commit_id()); |
|
38 | 38 | |
39 | - if ( is_wp_error( $result ) ) { |
|
39 | + if (is_wp_error($result)) { |
|
40 | 40 | /* @var WP_Error $result */ |
41 | 41 | return $result; |
42 | 42 | } |
43 | 43 | |
44 | - if ( is_array( $result ) ) { |
|
45 | - $result = $this->import_files( $result ); |
|
44 | + if (is_array($result)) { |
|
45 | + $result = $this->import_files($result); |
|
46 | 46 | } |
47 | 47 | |
48 | - if ( is_wp_error( $result ) ) { |
|
48 | + if (is_wp_error($result)) { |
|
49 | 49 | return $files; |
50 | 50 | } |
51 | 51 | |
52 | - return __( 'Payload processed', 'writing-on-github' ); |
|
52 | + return __('Payload processed', 'writing-on-github'); |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | /** |
@@ -58,33 +58,33 @@ discard block |
||
58 | 58 | * |
59 | 59 | * @return true|WP_Error |
60 | 60 | */ |
61 | - protected function import_files( $files ) { |
|
61 | + protected function import_files($files) { |
|
62 | 62 | |
63 | 63 | $error = true; |
64 | 64 | |
65 | - foreach ( $files as $file ) { |
|
66 | - if ( ! $this->importable_file( $file ) ) { |
|
65 | + foreach ($files as $file) { |
|
66 | + if ( ! $this->importable_file($file)) { |
|
67 | 67 | continue; |
68 | 68 | } |
69 | 69 | |
70 | - $blob = $this->app->api()->fetch()->blob( $file ); |
|
70 | + $blob = $this->app->api()->fetch()->blob($file); |
|
71 | 71 | // network error ? |
72 | - if ( ! $blob instanceof Writing_On_GitHub_Blob ) { |
|
72 | + if ( ! $blob instanceof Writing_On_GitHub_Blob) { |
|
73 | 73 | continue; |
74 | 74 | } |
75 | 75 | |
76 | 76 | $is_remove = 'removed' == $file->status; |
77 | 77 | |
78 | 78 | $result = false; |
79 | - if ( $this->importable_raw_file( $blob ) ) { |
|
80 | - $result = $this->import_raw_file( $blob, $is_remove ); |
|
81 | - } elseif ( $this->importable_post( $blob ) ) { |
|
82 | - $result = $this->import_post( $blob, $is_remove ); |
|
79 | + if ($this->importable_raw_file($blob)) { |
|
80 | + $result = $this->import_raw_file($blob, $is_remove); |
|
81 | + } elseif ($this->importable_post($blob)) { |
|
82 | + $result = $this->import_post($blob, $is_remove); |
|
83 | 83 | } |
84 | 84 | |
85 | - if ( is_wp_error( $result ) ) { |
|
85 | + if (is_wp_error($result)) { |
|
86 | 86 | /* @var WP_Error $result */ |
87 | - $error = wogh_append_error( $error, $result ); |
|
87 | + $error = wogh_append_error($error, $result); |
|
88 | 88 | } |
89 | 89 | } |
90 | 90 | |
@@ -99,21 +99,21 @@ discard block |
||
99 | 99 | public function master() { |
100 | 100 | $result = $this->app->api()->fetch()->tree_recursive(); |
101 | 101 | |
102 | - if ( is_wp_error( $result ) ) { |
|
102 | + if (is_wp_error($result)) { |
|
103 | 103 | /* @var WP_Error $result */ |
104 | 104 | return $result; |
105 | 105 | } |
106 | 106 | |
107 | - if ( is_array( $result ) ) { |
|
108 | - $result = $this->import_files( $result ); |
|
107 | + if (is_array($result)) { |
|
108 | + $result = $this->import_files($result); |
|
109 | 109 | } |
110 | 110 | |
111 | - if ( is_wp_error( $result ) ) { |
|
111 | + if (is_wp_error($result)) { |
|
112 | 112 | /* @var WP_Error $result */ |
113 | 113 | return $result; |
114 | 114 | } |
115 | 115 | |
116 | - return __( 'Payload processed', 'writing-on-github' ); |
|
116 | + return __('Payload processed', 'writing-on-github'); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | /** |
@@ -123,14 +123,14 @@ discard block |
||
123 | 123 | * |
124 | 124 | * @return bool |
125 | 125 | */ |
126 | - protected function importable_file( Writing_On_GitHub_File_Info $file ) { |
|
126 | + protected function importable_file(Writing_On_GitHub_File_Info $file) { |
|
127 | 127 | |
128 | 128 | $path = $file->path; |
129 | 129 | |
130 | 130 | // only _pages, _posts and images |
131 | - $prefixs = array( '_pages/', '_posts/', 'images/'); |
|
131 | + $prefixs = array('_pages/', '_posts/', 'images/'); |
|
132 | 132 | foreach ($prefixs as $prefix) { |
133 | - if ( ! strncasecmp($path, $prefix, strlen( $prefix ) ) ) { |
|
133 | + if ( ! strncasecmp($path, $prefix, strlen($prefix))) { |
|
134 | 134 | return true; |
135 | 135 | } |
136 | 136 | } |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | * |
145 | 145 | * @return bool |
146 | 146 | */ |
147 | - protected function importable_post( Writing_On_GitHub_Blob $blob ) { |
|
147 | + protected function importable_post(Writing_On_GitHub_Blob $blob) { |
|
148 | 148 | // global $wpdb; |
149 | 149 | |
150 | 150 | // // Skip the repo's readme. |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | // return false; |
158 | 158 | // } |
159 | 159 | |
160 | - if ( ! $blob->has_frontmatter() ) { |
|
160 | + if ( ! $blob->has_frontmatter()) { |
|
161 | 161 | return false; |
162 | 162 | } |
163 | 163 | |
@@ -170,39 +170,39 @@ discard block |
||
170 | 170 | * @param bool $is_remove |
171 | 171 | * @return WP_Error|bool |
172 | 172 | */ |
173 | - protected function import_post( Writing_On_GitHub_Blob $blob, $is_remove ) { |
|
173 | + protected function import_post(Writing_On_GitHub_Blob $blob, $is_remove) { |
|
174 | 174 | |
175 | - if ( $is_remove ) { |
|
175 | + if ($is_remove) { |
|
176 | 176 | $id = $blob->id(); |
177 | - if ( empty( $id ) ) { |
|
177 | + if (empty($id)) { |
|
178 | 178 | return false; |
179 | 179 | } |
180 | - $result = $this->app->database()->delete_post( $id ); |
|
181 | - if ( is_wp_error( $result ) ) { |
|
180 | + $result = $this->app->database()->delete_post($id); |
|
181 | + if (is_wp_error($result)) { |
|
182 | 182 | /* @var WP_Error $result */ |
183 | 183 | return $result; |
184 | 184 | } |
185 | 185 | return true; |
186 | 186 | } |
187 | 187 | |
188 | - $post = $this->blob_to_post( $blob ); |
|
188 | + $post = $this->blob_to_post($blob); |
|
189 | 189 | |
190 | - if ( ! $post instanceof Writing_On_GitHub_Post ) { |
|
190 | + if ( ! $post instanceof Writing_On_GitHub_Post) { |
|
191 | 191 | return false; |
192 | 192 | } |
193 | 193 | |
194 | - $result = $this->app->database()->save_post( $post ); |
|
195 | - if ( is_wp_error( $result ) ) { |
|
194 | + $result = $this->app->database()->save_post($post); |
|
195 | + if (is_wp_error($result)) { |
|
196 | 196 | /** @var WP_Error $result */ |
197 | 197 | return $result; |
198 | 198 | } |
199 | 199 | |
200 | - if ( $post->is_new() || |
|
201 | - ! wogh_equal_front_matter( $post, $blob ) ) { |
|
200 | + if ($post->is_new() || |
|
201 | + ! wogh_equal_front_matter($post, $blob)) { |
|
202 | 202 | |
203 | - $result = $this->app->export()->new_posts( array( $post ) ); |
|
203 | + $result = $this->app->export()->new_posts(array($post)); |
|
204 | 204 | |
205 | - if ( is_wp_error( $result ) ) { |
|
205 | + if (is_wp_error($result)) { |
|
206 | 206 | /** @var WP_Error $result */ |
207 | 207 | return $result; |
208 | 208 | } |
@@ -216,13 +216,13 @@ discard block |
||
216 | 216 | * @param Writing_On_GitHub_Blob $blob |
217 | 217 | * @return bool |
218 | 218 | */ |
219 | - protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { |
|
220 | - if ( $blob->has_frontmatter() ) { |
|
219 | + protected function importable_raw_file(Writing_On_GitHub_Blob $blob) { |
|
220 | + if ($blob->has_frontmatter()) { |
|
221 | 221 | return false; |
222 | 222 | } |
223 | 223 | |
224 | 224 | // only images |
225 | - if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) { |
|
225 | + if (strncasecmp($blob->path(), 'images/', strlen('images/')) != 0) { |
|
226 | 226 | return false; |
227 | 227 | } |
228 | 228 | |
@@ -234,16 +234,16 @@ discard block |
||
234 | 234 | * @param Writing_On_GitHub_Blob $blob |
235 | 235 | * @param bool $is_remove |
236 | 236 | */ |
237 | - protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { |
|
237 | + protected function import_raw_file(Writing_On_GitHub_Blob $blob, $is_remove) { |
|
238 | 238 | $arr = wp_upload_dir(); |
239 | 239 | $path = $arr['basedir'] . '/writing-on-github/' . $blob->path(); |
240 | - if ( $is_remove ) { |
|
241 | - if ( file_exists($path) ) { |
|
240 | + if ($is_remove) { |
|
241 | + if (file_exists($path)) { |
|
242 | 242 | unlink($path); |
243 | 243 | } |
244 | 244 | } else { |
245 | 245 | $dirname = dirname($path); |
246 | - if ( ! file_exists($dirname) ) { |
|
246 | + if ( ! file_exists($dirname)) { |
|
247 | 247 | wp_mkdir_p($dirname); |
248 | 248 | } |
249 | 249 | |
@@ -259,57 +259,57 @@ discard block |
||
259 | 259 | * |
260 | 260 | * @return Writing_On_GitHub_Post|false |
261 | 261 | */ |
262 | - protected function blob_to_post( Writing_On_GitHub_Blob $blob ) { |
|
263 | - $args = array( 'post_content' => $blob->content_import() ); |
|
262 | + protected function blob_to_post(Writing_On_GitHub_Blob $blob) { |
|
263 | + $args = array('post_content' => $blob->content_import()); |
|
264 | 264 | $meta = $blob->meta(); |
265 | 265 | |
266 | 266 | $id = false; |
267 | 267 | |
268 | - if ( ! empty( $meta ) ) { |
|
269 | - if ( array_key_exists( 'layout', $meta ) ) { |
|
268 | + if ( ! empty($meta)) { |
|
269 | + if (array_key_exists('layout', $meta)) { |
|
270 | 270 | $args['post_type'] = $meta['layout']; |
271 | - unset( $meta['layout'] ); |
|
271 | + unset($meta['layout']); |
|
272 | 272 | } |
273 | 273 | |
274 | - if ( array_key_exists( 'published', $meta ) ) { |
|
274 | + if (array_key_exists('published', $meta)) { |
|
275 | 275 | $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; |
276 | - unset( $meta['published'] ); |
|
276 | + unset($meta['published']); |
|
277 | 277 | } |
278 | 278 | |
279 | - if ( array_key_exists( 'post_title', $meta ) ) { |
|
279 | + if (array_key_exists('post_title', $meta)) { |
|
280 | 280 | $args['post_title'] = $meta['post_title']; |
281 | - unset( $meta['post_title'] ); |
|
281 | + unset($meta['post_title']); |
|
282 | 282 | } |
283 | 283 | |
284 | - if ( array_key_exists( 'post_name', $meta ) ) { |
|
284 | + if (array_key_exists('post_name', $meta)) { |
|
285 | 285 | $args['post_name'] = $meta['post_name']; |
286 | - unset( $meta['post_name'] ); |
|
286 | + unset($meta['post_name']); |
|
287 | 287 | } |
288 | 288 | |
289 | - if ( array_key_exists( 'ID', $meta ) ) { |
|
289 | + if (array_key_exists('ID', $meta)) { |
|
290 | 290 | $id = $args['ID'] = $meta['ID']; |
291 | 291 | $blob->set_id($id); |
292 | - unset( $meta['ID'] ); |
|
292 | + unset($meta['ID']); |
|
293 | 293 | } |
294 | 294 | } |
295 | 295 | |
296 | 296 | $meta['_wogh_sha'] = $blob->sha(); |
297 | 297 | |
298 | - if ( $id ) { |
|
299 | - $old_sha = get_post_meta( $id, '_wogh_sha', true ); |
|
300 | - $old_github_path = get_post_meta( $id, '_wogh_github_path', true ); |
|
298 | + if ($id) { |
|
299 | + $old_sha = get_post_meta($id, '_wogh_sha', true); |
|
300 | + $old_github_path = get_post_meta($id, '_wogh_github_path', true); |
|
301 | 301 | |
302 | 302 | // dont save post when has same sha |
303 | - if ( $old_sha && $old_sha == $meta['_wogh_sha'] && |
|
304 | - $old_github_path && $old_github_path == $blob->path() ) { |
|
303 | + if ($old_sha && $old_sha == $meta['_wogh_sha'] && |
|
304 | + $old_github_path && $old_github_path == $blob->path()) { |
|
305 | 305 | return false; |
306 | 306 | } |
307 | 307 | } |
308 | 308 | |
309 | - $post = new Writing_On_GitHub_Post( $args, $this->app->api() ); |
|
310 | - $post->set_old_github_path( $blob->path() ); |
|
311 | - $post->set_meta( $meta ); |
|
312 | - $blob->set_id( $post->id() ); |
|
309 | + $post = new Writing_On_GitHub_Post($args, $this->app->api()); |
|
310 | + $post->set_old_github_path($blob->path()); |
|
311 | + $post->set_meta($meta); |
|
312 | + $blob->set_id($post->id()); |
|
313 | 313 | |
314 | 314 | return $post; |
315 | 315 | } |
@@ -9,226 +9,226 @@ |
||
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 | - * Instantiates a new Blob object. |
|
55 | - * |
|
56 | - * @param stdClass $data Raw blob data. |
|
57 | - */ |
|
58 | - public function __construct( stdClass $data ) { |
|
59 | - $this->interpret_data( $data ); |
|
60 | - } |
|
61 | - |
|
62 | - public function id() { |
|
63 | - return $this->id; |
|
64 | - } |
|
65 | - |
|
66 | - public function set_id($id) { |
|
67 | - $this->id = $id; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Returns the raw blob content. |
|
72 | - * |
|
73 | - * @return string |
|
74 | - */ |
|
75 | - public function content() { |
|
76 | - return $this->content; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Set's the blob's content. |
|
81 | - * |
|
82 | - * @param string $content Raw blob content. |
|
83 | - * @param bool $base64 Whether the content is base64 encoded. |
|
84 | - * |
|
85 | - * @return $this |
|
86 | - */ |
|
87 | - public function set_content( $content, $base64 = false ) { |
|
88 | - if ( $base64 ) { |
|
89 | - $content = base64_decode( $content ); |
|
90 | - } |
|
91 | - |
|
92 | - $this->frontmatter = '---' === substr( $this->content = $content, 0, 3 ); |
|
93 | - |
|
94 | - return $this; |
|
95 | - } |
|
96 | - /** |
|
97 | - * Returns the blob sha. |
|
98 | - * |
|
99 | - * @return string |
|
100 | - */ |
|
101 | - public function sha() { |
|
102 | - return $this->sha; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Return's the blob path. |
|
107 | - * |
|
108 | - * @return string |
|
109 | - */ |
|
110 | - public function path() { |
|
111 | - return $this->path; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Whether the blob has frontmatter. |
|
116 | - * |
|
117 | - * @return bool |
|
118 | - */ |
|
119 | - public function has_frontmatter() { |
|
120 | - return $this->frontmatter; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * The front matter of github post |
|
125 | - * @return string |
|
126 | - */ |
|
127 | - public function front_matter() { |
|
128 | - return $this->front_matter; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Returns the formatted/filtered blob content used for import. |
|
133 | - * |
|
134 | - * @return string |
|
135 | - */ |
|
136 | - public function content_import() { |
|
137 | - $content = $this->content(); |
|
138 | - |
|
139 | - if ( $this->has_frontmatter() ) { |
|
140 | - // Break out content. |
|
141 | - preg_match( '/(^---(.*?)---$)?(.*)/ms', $content, $matches ); |
|
142 | - $this->front_matter = $matches[1]; |
|
143 | - $content = array_pop( $matches ); |
|
144 | - } |
|
145 | - |
|
146 | - if ( function_exists( 'wpmarkdown_markdown_to_html' ) ) { |
|
147 | - $content = wpmarkdown_markdown_to_html( $content ); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Filters the content for import. |
|
152 | - */ |
|
153 | - return apply_filters( 'wogh_content_import', trim( $content ) ); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Returns the blob meta. |
|
158 | - * |
|
159 | - * @return array |
|
160 | - */ |
|
161 | - public function meta() { |
|
162 | - $meta = array(); |
|
163 | - |
|
164 | - if ( $this->has_frontmatter() ) { |
|
165 | - // Break out meta, if present. |
|
166 | - preg_match( '/(^---(.*?)---$)?(.*)/ms', $this->content(), $matches ); |
|
167 | - array_pop( $matches ); |
|
168 | - |
|
169 | - $meta = spyc_load( $matches[2] ); |
|
170 | - if ( 'yes' == get_option('wogh_ignore_author') ) { |
|
171 | - unset($meta['author']); |
|
172 | - } |
|
173 | - // if ( isset( $meta['link'] ) ) { |
|
174 | - // $meta['link'] = str_replace( home_url(), '', $meta['link'] ); |
|
175 | - // } |
|
176 | - } |
|
177 | - |
|
178 | - return $meta; |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Formats the blob into an API call body. |
|
183 | - * |
|
184 | - * @return stdClass |
|
185 | - */ |
|
186 | - // public function to_body() { |
|
187 | - // $data = new stdClass; |
|
188 | - |
|
189 | - // $data->mode = '100644'; |
|
190 | - // $data->type = 'blob'; |
|
191 | - |
|
192 | - // $data->path = $this->path(); |
|
193 | - |
|
194 | - // if ( $this->sha() ) { |
|
195 | - // $data->sha = $this->sha(); |
|
196 | - // } else { |
|
197 | - // $data->content = $this->content(); |
|
198 | - // } |
|
199 | - |
|
200 | - // return $data; |
|
201 | - // } |
|
202 | - |
|
203 | - |
|
204 | - /** |
|
205 | - * Formats the blob into an API call body. |
|
206 | - * |
|
207 | - * @return stdClass |
|
208 | - */ |
|
209 | - public function to_body() { |
|
210 | - $data = new stdClass; |
|
211 | - |
|
212 | - // $data->mode = '100644'; |
|
213 | - // $data->type = 'blob'; |
|
214 | - |
|
215 | - $data->path = $this->path(); |
|
216 | - $data->content = base64_encode( $this->content() ); |
|
217 | - $data->sha = $this->sha; |
|
218 | - |
|
219 | - return $data; |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * Interprets the blob's data into properties. |
|
224 | - */ |
|
225 | - protected function interpret_data( $data ) { |
|
226 | - $this->sha = isset( $data->sha ) ? $data->sha : ''; |
|
227 | - $this->path = isset( $data->path ) ? $data->path : ''; |
|
228 | - |
|
229 | - $this->set_content( |
|
230 | - isset( $data->content ) ? trim( $data->content ) : '', |
|
231 | - isset( $data->encoding ) && 'base64' === $data->encoding ? true : false |
|
232 | - ); |
|
233 | - } |
|
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 | + * Instantiates a new Blob object. |
|
55 | + * |
|
56 | + * @param stdClass $data Raw blob data. |
|
57 | + */ |
|
58 | + public function __construct( stdClass $data ) { |
|
59 | + $this->interpret_data( $data ); |
|
60 | + } |
|
61 | + |
|
62 | + public function id() { |
|
63 | + return $this->id; |
|
64 | + } |
|
65 | + |
|
66 | + public function set_id($id) { |
|
67 | + $this->id = $id; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Returns the raw blob content. |
|
72 | + * |
|
73 | + * @return string |
|
74 | + */ |
|
75 | + public function content() { |
|
76 | + return $this->content; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Set's the blob's content. |
|
81 | + * |
|
82 | + * @param string $content Raw blob content. |
|
83 | + * @param bool $base64 Whether the content is base64 encoded. |
|
84 | + * |
|
85 | + * @return $this |
|
86 | + */ |
|
87 | + public function set_content( $content, $base64 = false ) { |
|
88 | + if ( $base64 ) { |
|
89 | + $content = base64_decode( $content ); |
|
90 | + } |
|
91 | + |
|
92 | + $this->frontmatter = '---' === substr( $this->content = $content, 0, 3 ); |
|
93 | + |
|
94 | + return $this; |
|
95 | + } |
|
96 | + /** |
|
97 | + * Returns the blob sha. |
|
98 | + * |
|
99 | + * @return string |
|
100 | + */ |
|
101 | + public function sha() { |
|
102 | + return $this->sha; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Return's the blob path. |
|
107 | + * |
|
108 | + * @return string |
|
109 | + */ |
|
110 | + public function path() { |
|
111 | + return $this->path; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Whether the blob has frontmatter. |
|
116 | + * |
|
117 | + * @return bool |
|
118 | + */ |
|
119 | + public function has_frontmatter() { |
|
120 | + return $this->frontmatter; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * The front matter of github post |
|
125 | + * @return string |
|
126 | + */ |
|
127 | + public function front_matter() { |
|
128 | + return $this->front_matter; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Returns the formatted/filtered blob content used for import. |
|
133 | + * |
|
134 | + * @return string |
|
135 | + */ |
|
136 | + public function content_import() { |
|
137 | + $content = $this->content(); |
|
138 | + |
|
139 | + if ( $this->has_frontmatter() ) { |
|
140 | + // Break out content. |
|
141 | + preg_match( '/(^---(.*?)---$)?(.*)/ms', $content, $matches ); |
|
142 | + $this->front_matter = $matches[1]; |
|
143 | + $content = array_pop( $matches ); |
|
144 | + } |
|
145 | + |
|
146 | + if ( function_exists( 'wpmarkdown_markdown_to_html' ) ) { |
|
147 | + $content = wpmarkdown_markdown_to_html( $content ); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Filters the content for import. |
|
152 | + */ |
|
153 | + return apply_filters( 'wogh_content_import', trim( $content ) ); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Returns the blob meta. |
|
158 | + * |
|
159 | + * @return array |
|
160 | + */ |
|
161 | + public function meta() { |
|
162 | + $meta = array(); |
|
163 | + |
|
164 | + if ( $this->has_frontmatter() ) { |
|
165 | + // Break out meta, if present. |
|
166 | + preg_match( '/(^---(.*?)---$)?(.*)/ms', $this->content(), $matches ); |
|
167 | + array_pop( $matches ); |
|
168 | + |
|
169 | + $meta = spyc_load( $matches[2] ); |
|
170 | + if ( 'yes' == get_option('wogh_ignore_author') ) { |
|
171 | + unset($meta['author']); |
|
172 | + } |
|
173 | + // if ( isset( $meta['link'] ) ) { |
|
174 | + // $meta['link'] = str_replace( home_url(), '', $meta['link'] ); |
|
175 | + // } |
|
176 | + } |
|
177 | + |
|
178 | + return $meta; |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Formats the blob into an API call body. |
|
183 | + * |
|
184 | + * @return stdClass |
|
185 | + */ |
|
186 | + // public function to_body() { |
|
187 | + // $data = new stdClass; |
|
188 | + |
|
189 | + // $data->mode = '100644'; |
|
190 | + // $data->type = 'blob'; |
|
191 | + |
|
192 | + // $data->path = $this->path(); |
|
193 | + |
|
194 | + // if ( $this->sha() ) { |
|
195 | + // $data->sha = $this->sha(); |
|
196 | + // } else { |
|
197 | + // $data->content = $this->content(); |
|
198 | + // } |
|
199 | + |
|
200 | + // return $data; |
|
201 | + // } |
|
202 | + |
|
203 | + |
|
204 | + /** |
|
205 | + * Formats the blob into an API call body. |
|
206 | + * |
|
207 | + * @return stdClass |
|
208 | + */ |
|
209 | + public function to_body() { |
|
210 | + $data = new stdClass; |
|
211 | + |
|
212 | + // $data->mode = '100644'; |
|
213 | + // $data->type = 'blob'; |
|
214 | + |
|
215 | + $data->path = $this->path(); |
|
216 | + $data->content = base64_encode( $this->content() ); |
|
217 | + $data->sha = $this->sha; |
|
218 | + |
|
219 | + return $data; |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * Interprets the blob's data into properties. |
|
224 | + */ |
|
225 | + protected function interpret_data( $data ) { |
|
226 | + $this->sha = isset( $data->sha ) ? $data->sha : ''; |
|
227 | + $this->path = isset( $data->path ) ? $data->path : ''; |
|
228 | + |
|
229 | + $this->set_content( |
|
230 | + isset( $data->content ) ? trim( $data->content ) : '', |
|
231 | + isset( $data->encoding ) && 'base64' === $data->encoding ? true : false |
|
232 | + ); |
|
233 | + } |
|
234 | 234 | } |
@@ -55,8 +55,8 @@ discard block |
||
55 | 55 | * |
56 | 56 | * @param stdClass $data Raw blob data. |
57 | 57 | */ |
58 | - public function __construct( stdClass $data ) { |
|
59 | - $this->interpret_data( $data ); |
|
58 | + public function __construct(stdClass $data) { |
|
59 | + $this->interpret_data($data); |
|
60 | 60 | } |
61 | 61 | |
62 | 62 | public function id() { |
@@ -84,12 +84,12 @@ discard block |
||
84 | 84 | * |
85 | 85 | * @return $this |
86 | 86 | */ |
87 | - public function set_content( $content, $base64 = false ) { |
|
88 | - if ( $base64 ) { |
|
89 | - $content = base64_decode( $content ); |
|
87 | + public function set_content($content, $base64 = false) { |
|
88 | + if ($base64) { |
|
89 | + $content = base64_decode($content); |
|
90 | 90 | } |
91 | 91 | |
92 | - $this->frontmatter = '---' === substr( $this->content = $content, 0, 3 ); |
|
92 | + $this->frontmatter = '---' === substr($this->content = $content, 0, 3); |
|
93 | 93 | |
94 | 94 | return $this; |
95 | 95 | } |
@@ -136,21 +136,21 @@ discard block |
||
136 | 136 | public function content_import() { |
137 | 137 | $content = $this->content(); |
138 | 138 | |
139 | - if ( $this->has_frontmatter() ) { |
|
139 | + if ($this->has_frontmatter()) { |
|
140 | 140 | // Break out content. |
141 | - preg_match( '/(^---(.*?)---$)?(.*)/ms', $content, $matches ); |
|
141 | + preg_match('/(^---(.*?)---$)?(.*)/ms', $content, $matches); |
|
142 | 142 | $this->front_matter = $matches[1]; |
143 | - $content = array_pop( $matches ); |
|
143 | + $content = array_pop($matches); |
|
144 | 144 | } |
145 | 145 | |
146 | - if ( function_exists( 'wpmarkdown_markdown_to_html' ) ) { |
|
147 | - $content = wpmarkdown_markdown_to_html( $content ); |
|
146 | + if (function_exists('wpmarkdown_markdown_to_html')) { |
|
147 | + $content = wpmarkdown_markdown_to_html($content); |
|
148 | 148 | } |
149 | 149 | |
150 | 150 | /** |
151 | 151 | * Filters the content for import. |
152 | 152 | */ |
153 | - return apply_filters( 'wogh_content_import', trim( $content ) ); |
|
153 | + return apply_filters('wogh_content_import', trim($content)); |
|
154 | 154 | } |
155 | 155 | |
156 | 156 | /** |
@@ -161,13 +161,13 @@ discard block |
||
161 | 161 | public function meta() { |
162 | 162 | $meta = array(); |
163 | 163 | |
164 | - if ( $this->has_frontmatter() ) { |
|
164 | + if ($this->has_frontmatter()) { |
|
165 | 165 | // Break out meta, if present. |
166 | - preg_match( '/(^---(.*?)---$)?(.*)/ms', $this->content(), $matches ); |
|
167 | - array_pop( $matches ); |
|
166 | + preg_match('/(^---(.*?)---$)?(.*)/ms', $this->content(), $matches); |
|
167 | + array_pop($matches); |
|
168 | 168 | |
169 | - $meta = spyc_load( $matches[2] ); |
|
170 | - if ( 'yes' == get_option('wogh_ignore_author') ) { |
|
169 | + $meta = spyc_load($matches[2]); |
|
170 | + if ('yes' == get_option('wogh_ignore_author')) { |
|
171 | 171 | unset($meta['author']); |
172 | 172 | } |
173 | 173 | // if ( isset( $meta['link'] ) ) { |
@@ -213,7 +213,7 @@ discard block |
||
213 | 213 | // $data->type = 'blob'; |
214 | 214 | |
215 | 215 | $data->path = $this->path(); |
216 | - $data->content = base64_encode( $this->content() ); |
|
216 | + $data->content = base64_encode($this->content()); |
|
217 | 217 | $data->sha = $this->sha; |
218 | 218 | |
219 | 219 | return $data; |
@@ -222,13 +222,13 @@ discard block |
||
222 | 222 | /** |
223 | 223 | * Interprets the blob's data into properties. |
224 | 224 | */ |
225 | - protected function interpret_data( $data ) { |
|
226 | - $this->sha = isset( $data->sha ) ? $data->sha : ''; |
|
227 | - $this->path = isset( $data->path ) ? $data->path : ''; |
|
225 | + protected function interpret_data($data) { |
|
226 | + $this->sha = isset($data->sha) ? $data->sha : ''; |
|
227 | + $this->path = isset($data->path) ? $data->path : ''; |
|
228 | 228 | |
229 | 229 | $this->set_content( |
230 | - isset( $data->content ) ? trim( $data->content ) : '', |
|
231 | - isset( $data->encoding ) && 'base64' === $data->encoding ? true : false |
|
230 | + isset($data->content) ? trim($data->content) : '', |
|
231 | + isset($data->encoding) && 'base64' === $data->encoding ? true : false |
|
232 | 232 | ); |
233 | 233 | } |
234 | 234 | } |
@@ -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,7 +21,7 @@ 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 | } |
@@ -7,9 +7,9 @@ discard block |
||
7 | 7 | * @param WP_Error $error2 |
8 | 8 | * @return WP_Error |
9 | 9 | */ |
10 | -function wogh_append_error( $error, $error2 ) { |
|
11 | - if ( is_wp_error( $error ) ) { |
|
12 | - $error->add( $error2->get_error_code(), $error2->get_error_message() ); |
|
10 | +function wogh_append_error($error, $error2) { |
|
11 | + if (is_wp_error($error)) { |
|
12 | + $error->add($error2->get_error_code(), $error2->get_error_message()); |
|
13 | 13 | } |
14 | 14 | return $error2; |
15 | 15 | } |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | * @param Writing_On_GitHub_Blob $blob |
21 | 21 | * @return bool |
22 | 22 | */ |
23 | -function wogh_equal_front_matter( $post, $blob ) { |
|
23 | +function wogh_equal_front_matter($post, $blob) { |
|
24 | 24 | $str1 = $post->front_matter(); |
25 | 25 | $str2 = $blob->front_matter(); |
26 | 26 | return trim($str1) === trim($str2); |