@@ -281,11 +281,20 @@ protected function update_post_from_markdown_source( $post_id ) {
281
281
$ etag = wp_remote_retrieve_header ( $ response , 'etag ' );
282
282
283
283
$ 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
+ }
286
293
287
294
$ 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 ) ) {
289
298
$ title = $ matches [1 ];
290
299
$ markdown = preg_replace ( '/^#\swp\s(.+)/ ' , '' , $ markdown );
291
300
}
@@ -313,9 +322,24 @@ protected function update_post_from_markdown_source( $post_id ) {
313
322
}
314
323
wp_update_post ( $ post_data );
315
324
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
+
316
332
// Set ETag for future updates.
317
333
update_post_meta ( $ post_id , $ this ->etag_meta_key , wp_slash ( $ etag ) );
318
334
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
+
319
343
return true ;
320
344
}
321
345
0 commit comments