Skip to content

Commit 1a331ce

Browse files
committed
first pass YAML front matter parsing
1 parent 305e74e commit 1a331ce

File tree

3 files changed

+1241
-3
lines changed

3 files changed

+1241
-3
lines changed

inc/class-importer.php

+27-3
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,20 @@ protected function update_post_from_markdown_source( $post_id ) {
281281
$etag = wp_remote_retrieve_header( $response, 'etag' );
282282

283283
$markdown = wp_remote_retrieve_body( $response );
284-
// Strip YAML doc from the header
285-
$markdown = preg_replace( '#^---(.+)---#Us', '', $markdown );
284+
285+
// Get YAML doc from the header
286+
preg_match( '#^---(.+)---#Us', $markdown, $yaml );
287+
if ( $yaml ) {
288+
$yaml_parser = new Yaml();
289+
$yaml = $yaml_parser->loadString( $yaml[1] );
290+
// Strip YAML doc from the header
291+
$markdown = preg_replace( '#^---(.+)---#Us', '', $markdown );
292+
}
286293

287294
$title = null;
288-
if ( preg_match( '/^#\s(.+)/', $markdown, $matches ) ) {
295+
if ( isset( $yaml['title'] ) ) {
296+
$title = $yaml['title'];
297+
} elseif ( preg_match( '/^#\s(.+)/', $markdown, $matches ) ) {
289298
$title = $matches[1];
290299
$markdown = preg_replace( '/^#\swp\s(.+)/', '', $markdown );
291300
}
@@ -313,9 +322,24 @@ protected function update_post_from_markdown_source( $post_id ) {
313322
}
314323
wp_update_post( $post_data );
315324

325+
// Add meta data from YAML front matter.
326+
if ( isset( $yaml['meta'] ) && is_array( $yaml['meta'] ) ) {
327+
foreach ( $yaml['meta'] as $key => $value ) {
328+
update_post_meta( $post_id, $key, $value );
329+
}
330+
}
331+
316332
// Set ETag for future updates.
317333
update_post_meta( $post_id, $this->etag_meta_key, wp_slash( $etag ) );
318334

335+
/**
336+
* Action for any post processing after post update.
337+
*
338+
* @param int $post_id The post's ID.
339+
* @param array|bool $yaml The YAML front matter as an array.
340+
*/
341+
do_action( 'wordpressdotorg.markdown.update_post', $post_id, $yaml );
342+
319343
return true;
320344
}
321345

0 commit comments

Comments
 (0)