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