Skip to content

Commit de85cca

Browse files
mattwiebepfefferle
andauthored
Content warnings! (#900)
* Allow content warning * build files * added missing namespace * fix tests * actors does not support sensitive (yet). * Update includes/functions.php * added `sanitize_callback` --------- Co-authored-by: Matthias Pfefferle <[email protected]>
1 parent cc7a6cc commit de85cca

File tree

11 files changed

+139
-4
lines changed

11 files changed

+139
-4
lines changed

build/editor-plugin/block.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "editor-plugin",
3+
"title": "Editor Plugin: not a block, but block.json is very useful.",
4+
"category": "widgets",
5+
"icon": "admin-comments",
6+
"keywords": [],
7+
"editorScript": "file:./plugin.js"
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php return array('dependencies' => array('react', 'wp-components', 'wp-core-data', 'wp-data', 'wp-editor', 'wp-i18n', 'wp-plugins'), 'version' => '88603987940fec29730d');

build/editor-plugin/plugin.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/activity/class-actor.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,15 @@ class Actor extends Base_Object {
169169
* @var boolean
170170
*/
171171
protected $manually_approves_followers = false;
172+
173+
/**
174+
* Used to mark an object as containing sensitive content.
175+
* Mastodon displays a content warning, requiring users to click
176+
* through to view the content.
177+
*
178+
* @see https://docs.joinmastodon.org/spec/activitypub/#sensitive
179+
*
180+
* @var boolean
181+
*/
182+
protected $sensitive = null;
172183
}

includes/activity/class-base-object.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class Base_Object {
3030
const JSON_LD_CONTEXT = array(
3131
'https://www.w3.org/ns/activitystreams',
3232
array(
33-
'Hashtag' => 'as:Hashtag',
33+
'Hashtag' => 'as:Hashtag',
34+
'sensitive' => 'as:sensitive',
3435
),
3536
);
3637

@@ -445,6 +446,17 @@ class Base_Object {
445446
*/
446447
protected $replies;
447448

449+
/**
450+
* Used to mark an object as containing sensitive content.
451+
* Mastodon displays a content warning, requiring users to click
452+
* through to view the content.
453+
*
454+
* @see https://docs.joinmastodon.org/spec/activitypub/#sensitive
455+
*
456+
* @var boolean
457+
*/
458+
protected $sensitive = false;
459+
448460
/**
449461
* Magic function to implement getter and setter
450462
*

includes/class-blocks.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,37 @@ public static function init() {
1414
\add_action( 'wp_enqueue_scripts', array( self::class, 'add_data' ) );
1515
\add_action( 'enqueue_block_editor_assets', array( self::class, 'add_data' ) );
1616
\add_action( 'load-post-new.php', array( self::class, 'handle_in_reply_to_get_param' ) );
17+
// Add editor plugin
18+
\add_action( 'enqueue_block_editor_assets', array( self::class, 'enqueue_editor_assets' ) );
19+
\add_action( 'init', array( self::class, 'register_postmeta' ), 11 );
20+
}
21+
22+
public static function register_postmeta() {
23+
$ap_post_types = \get_post_types_by_support( 'activitypub' );
24+
foreach ( $ap_post_types as $post_type ) {
25+
\register_post_meta(
26+
$post_type,
27+
'activitypub_content_warning',
28+
array(
29+
'show_in_rest' => true,
30+
'single' => true,
31+
'type' => 'string',
32+
'sanitize_callback' => 'sanitize_text_field',
33+
)
34+
);
35+
}
36+
}
37+
38+
public static function enqueue_editor_assets() {
39+
// check for our supported post types
40+
$current_screen = \get_current_screen();
41+
$ap_post_types = \get_post_types_by_support( 'activitypub' );
42+
if ( ! $current_screen || ! in_array( $current_screen->post_type, $ap_post_types, true ) ) {
43+
return;
44+
}
45+
$asset_data = include ACTIVITYPUB_PLUGIN_DIR . 'build/editor-plugin/plugin.asset.php';
46+
$plugin_url = plugins_url( 'build/editor-plugin/plugin.js', ACTIVITYPUB_PLUGIN_FILE );
47+
wp_enqueue_script( 'activitypub-block-editor', $plugin_url, $asset_data['dependencies'], $asset_data['version'], true );
1748
}
1849

1950
/**

includes/functions.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,3 +1215,24 @@ function generate_post_summary( $post, $length = 500 ) {
12151215
*/
12161216
return $content;
12171217
}
1218+
1219+
/**
1220+
* Get the content warning of a post.
1221+
*
1222+
* @param int $post_id The post ID.
1223+
*
1224+
* @return string|false The content warning or false if not found.
1225+
*/
1226+
function get_content_warning( $post_id ) {
1227+
$post = get_post( $post_id );
1228+
if ( ! $post ) {
1229+
return false;
1230+
}
1231+
1232+
$warning = get_post_meta( $post->ID, 'activitypub_content_warning', true );
1233+
if ( empty( $warning ) ) {
1234+
return false;
1235+
}
1236+
1237+
return $warning;
1238+
}

includes/transformer/class-post.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use function Activitypub\get_rest_url_by_path;
1515
use function Activitypub\is_user_type_disabled;
1616
use function Activitypub\generate_post_summary;
17+
use function Activitypub\get_content_warning;
1718

1819
/**
1920
* WordPress Post Transformer
@@ -87,6 +88,12 @@ public function to_object() {
8788
)
8889
);
8990

91+
$content_warning = get_content_warning( $post );
92+
if ( ! empty( $content_warning ) ) {
93+
$object->set_sensitive( true );
94+
$object->set_summary( $content_warning );
95+
}
96+
9097
return $object;
9198
}
9299

src/editor-plugin/block.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "editor-plugin",
3+
"title": "Editor Plugin: not a block, but block.json is very useful.",
4+
"category": "widgets",
5+
"icon": "admin-comments",
6+
"keywords": [
7+
],
8+
"editorScript": "file:./plugin.js"
9+
}

src/editor-plugin/plugin.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { PluginDocumentSettingPanel } from '@wordpress/editor';
2+
import { registerPlugin } from '@wordpress/plugins';
3+
import { TextControl } from '@wordpress/components';
4+
import { useSelect } from '@wordpress/data';
5+
import { useEntityProp } from '@wordpress/core-data';
6+
import { __ } from '@wordpress/i18n';
7+
8+
9+
const EditorPlugin = () => {
10+
const postType = useSelect(
11+
( select ) => select( 'core/editor' ).getCurrentPostType(),
12+
[]
13+
);
14+
const [ meta, setMeta ] = useEntityProp( 'postType', postType, 'meta' );
15+
16+
return (
17+
<PluginDocumentSettingPanel
18+
name="activitypub"
19+
title={ __( 'Fediverse', 'activitypub' ) }
20+
>
21+
<TextControl
22+
label={ __( 'Content Warning', 'activitypub' ) }
23+
value={ meta?.activitypub_content_warning }
24+
onChange={ ( value ) => {
25+
setMeta( { ...meta, activitypub_content_warning: value } );
26+
} }
27+
placeholder={ __( 'Optional content warning', 'activitypub' ) }
28+
/>
29+
</PluginDocumentSettingPanel>
30+
);
31+
}
32+
33+
registerPlugin( 'activitypub-editor-plugin', { render: EditorPlugin } );

0 commit comments

Comments
 (0)