Skip to content

Commit c256f57

Browse files
authored
Introduce general namespace support, using dcterms as an example (#1893)
1 parent 23b04c6 commit c256f57

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: added
3+
4+
Added support for namespaced attributes and the dcterms:subject field (FEP-b2b8), as a first step toward phasing out summary-based content warnings.

includes/activity/class-base-object.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Base_Object extends Generic_Object {
3232
array(
3333
'Hashtag' => 'as:Hashtag',
3434
'sensitive' => 'as:sensitive',
35+
'dcterms' => 'http://purl.org/dc/terms/',
3536
),
3637
);
3738

@@ -416,6 +417,16 @@ class Base_Object extends Generic_Object {
416417
*/
417418
protected $sensitive;
418419

420+
/**
421+
* The dcterms namespace.
422+
*
423+
* @see https://codeberg.org/fediverse/fep/src/branch/main/fep/b2b8/fep-b2b8.md#sensitive
424+
* @see https://www.dublincore.org/specifications/dublin-core/dcmi-terms/
425+
*
426+
* @var array
427+
*/
428+
protected $dcterms;
429+
419430
/**
420431
* Generic getter.
421432
*

includes/activity/class-generic-object.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,11 @@ public function to_array( $include_json_ld_context = true ) {
297297
$value = $value->to_array( false );
298298
}
299299

300-
// If value is still empty, ignore it for the array and continue.
301-
if ( isset( $value ) ) {
300+
if ( is_array( $value ) && $this->is_namespaced( $key ) ) {
301+
foreach ( $value as $sub_key => $sub_value ) {
302+
$array[ snake_to_camel_case( $key ) . ':' . snake_to_camel_case( $sub_key ) ] = $sub_value;
303+
}
304+
} elseif ( isset( $value ) ) {
302305
$array[ snake_to_camel_case( $key ) ] = $value;
303306
}
304307
}
@@ -373,4 +376,23 @@ public function get_object_var_keys() {
373376
public function get_json_ld_context() {
374377
return static::JSON_LD_CONTEXT;
375378
}
379+
380+
/**
381+
* Checks if an attribute is in a namespace.
382+
*
383+
* @param string $attribute The attribute to check.
384+
*
385+
* @return bool Whether the attribute is namespaced.
386+
*/
387+
private function is_namespaced( $attribute ) {
388+
$namespaces = array();
389+
390+
foreach ( static::JSON_LD_CONTEXT as $context ) {
391+
if ( is_array( $context ) ) {
392+
$namespaces = \array_merge( $namespaces, $context );
393+
}
394+
}
395+
396+
return isset( $namespaces[ $attribute ] ) && \wp_http_validate_url( $namespaces[ $attribute ] );
397+
}
376398
}

includes/transformer/class-post.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function to_object() {
5757
$object->set_sensitive( true );
5858
$object->set_summary( $content_warning );
5959
$object->set_summary_map( null );
60+
$object->set_dcterms( array( 'subject' => $content_warning ) );
6061
}
6162

6263
return $object;

tests/includes/collection/class-test-outbox.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function activity_object_provider() {
115115
),
116116
'Create',
117117
1,
118-
'{"@context":["https:\/\/www.w3.org\/ns\/activitystreams",{"Hashtag":"as:Hashtag","sensitive":"as:sensitive"}],"actor":"http:\/\/example.org\/?author=1","id":"http:\/\/example.org\/?post_type=ap_outbox\u0026p=351","type":"Create","to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"object":{"id":"https:\/\/example.com\/1","type":"Note","content":"\u003Cp\u003EThis is a note\u003C\/p\u003E","contentMap":{"en":"\u003Cp\u003EThis is a note\u003C\/p\u003E"},"tag":[],"to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"mediaType":"text\/html"}}',
118+
'{"@context":["https:\/\/www.w3.org\/ns\/activitystreams",{"Hashtag":"as:Hashtag","sensitive":"as:sensitive","dcterms":"http:\/\/purl.org\/dc\/terms\/"}],"actor":"http:\/\/example.org\/?author=1","id":"http:\/\/example.org\/?post_type=ap_outbox\u0026p=351","type":"Create","to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"object":{"id":"https:\/\/example.com\/1","type":"Note","content":"\u003Cp\u003EThis is a note\u003C\/p\u003E","contentMap":{"en":"\u003Cp\u003EThis is a note\u003C\/p\u003E"},"tag":[],"to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"mediaType":"text\/html"}}',
119119
),
120120
array(
121121
array(
@@ -126,7 +126,7 @@ public function activity_object_provider() {
126126
),
127127
'Create',
128128
2,
129-
'{"@context":["https:\/\/www.w3.org\/ns\/activitystreams",{"Hashtag":"as:Hashtag","sensitive":"as:sensitive"}],"actor":"http:\/\/example.org\/?author=0","id":"http:\/\/example.org\/?post_type=ap_outbox\u0026p=352","type":"Create","to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"object":{"id":"https:\/\/example.com\/2","type":"Note","content":"\u003Cp\u003EThis is another note\u003C\/p\u003E","contentMap":{"en":"\u003Cp\u003EThis is another note\u003C\/p\u003E"},"tag":[],"to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"mediaType":"text\/html"}}',
129+
'{"@context":["https:\/\/www.w3.org\/ns\/activitystreams",{"Hashtag":"as:Hashtag","sensitive":"as:sensitive","dcterms":"http:\/\/purl.org\/dc\/terms\/"}],"actor":"http:\/\/example.org\/?author=0","id":"http:\/\/example.org\/?post_type=ap_outbox\u0026p=352","type":"Create","to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"object":{"id":"https:\/\/example.com\/2","type":"Note","content":"\u003Cp\u003EThis is another note\u003C\/p\u003E","contentMap":{"en":"\u003Cp\u003EThis is another note\u003C\/p\u003E"},"tag":[],"to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"mediaType":"text\/html"}}',
130130
),
131131
array(
132132
Event::init_from_array(

0 commit comments

Comments
 (0)