Skip to content

Commit 36b541c

Browse files
authored
Merge pull request #312 from indieweb/fix/242-mf2-json-properties
Fix q=source to return valid MF2 JSON for all properties
2 parents a316c1b + dddd70d commit 36b541c

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

includes/functions.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,15 @@ function micropub_get_mf2( $post_id = null ) {
325325
if ( 'mf2_type' === $field ) {
326326
$mf2['type'] = $val;
327327
} elseif ( 'mf2_' === substr( $field, 0, 4 ) ) {
328+
// Ensure property values are always arrays per MF2 spec.
329+
// Skip empty strings as they indicate unset properties.
330+
if ( '' === $val ) {
331+
continue;
332+
}
333+
// Wrap non-array values in an array.
334+
if ( ! is_array( $val ) ) {
335+
$val = array( $val );
336+
}
328337
$mf2['properties'][ substr( $field, 4 ) ] = $val;
329338
}
330339
}

tests/test_functions.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,47 @@ function test_mp_filter() {
2626
)
2727
);
2828
}
29+
30+
function test_micropub_get_mf2_skips_empty_string_properties() {
31+
$post_id = $this->factory->post->create();
32+
33+
// Add an empty string property (simulates unset property).
34+
update_post_meta( $post_id, 'mf2_location', '' );
35+
// Add a valid property.
36+
update_post_meta( $post_id, 'mf2_category', array( 'test' ) );
37+
38+
$mf2 = micropub_get_mf2( $post_id );
39+
40+
// Empty string property should be skipped.
41+
$this->assertArrayNotHasKey( 'location', $mf2['properties'] );
42+
// Valid property should exist.
43+
$this->assertArrayHasKey( 'category', $mf2['properties'] );
44+
}
45+
46+
function test_micropub_get_mf2_wraps_non_array_values() {
47+
$post_id = $this->factory->post->create();
48+
49+
// Add a non-array property value (string).
50+
update_post_meta( $post_id, 'mf2_syndication', 'https://twitter.com/example/123' );
51+
52+
$mf2 = micropub_get_mf2( $post_id );
53+
54+
// Property value should be wrapped in an array.
55+
$this->assertIsArray( $mf2['properties']['syndication'] );
56+
$this->assertEquals( array( 'https://twitter.com/example/123' ), $mf2['properties']['syndication'] );
57+
}
58+
59+
function test_micropub_get_mf2_preserves_array_values() {
60+
$post_id = $this->factory->post->create();
61+
62+
// Add an array property value.
63+
$categories = array( 'tech', 'indieweb' );
64+
update_post_meta( $post_id, 'mf2_category', $categories );
65+
66+
$mf2 = micropub_get_mf2( $post_id );
67+
68+
// Property value should remain as array.
69+
$this->assertIsArray( $mf2['properties']['category'] );
70+
$this->assertEquals( $categories, $mf2['properties']['category'] );
71+
}
2972
}

0 commit comments

Comments
 (0)