Skip to content

Commit

Permalink
Content warnings! (#900)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
mattwiebe and pfefferle authored Sep 25, 2024
1 parent cc7a6cc commit de85cca
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 4 deletions.
8 changes: 8 additions & 0 deletions build/editor-plugin/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "editor-plugin",
"title": "Editor Plugin: not a block, but block.json is very useful.",
"category": "widgets",
"icon": "admin-comments",
"keywords": [],
"editorScript": "file:./plugin.js"
}
1 change: 1 addition & 0 deletions build/editor-plugin/plugin.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array('react', 'wp-components', 'wp-core-data', 'wp-data', 'wp-editor', 'wp-i18n', 'wp-plugins'), 'version' => '88603987940fec29730d');
1 change: 1 addition & 0 deletions build/editor-plugin/plugin.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions includes/activity/class-actor.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,15 @@ class Actor extends Base_Object {
* @var boolean
*/
protected $manually_approves_followers = false;

/**
* Used to mark an object as containing sensitive content.
* Mastodon displays a content warning, requiring users to click
* through to view the content.
*
* @see https://docs.joinmastodon.org/spec/activitypub/#sensitive
*
* @var boolean
*/
protected $sensitive = null;
}
14 changes: 13 additions & 1 deletion includes/activity/class-base-object.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class Base_Object {
const JSON_LD_CONTEXT = array(
'https://www.w3.org/ns/activitystreams',
array(
'Hashtag' => 'as:Hashtag',
'Hashtag' => 'as:Hashtag',
'sensitive' => 'as:sensitive',
),
);

Expand Down Expand Up @@ -445,6 +446,17 @@ class Base_Object {
*/
protected $replies;

/**
* Used to mark an object as containing sensitive content.
* Mastodon displays a content warning, requiring users to click
* through to view the content.
*
* @see https://docs.joinmastodon.org/spec/activitypub/#sensitive
*
* @var boolean
*/
protected $sensitive = false;

/**
* Magic function to implement getter and setter
*
Expand Down
31 changes: 31 additions & 0 deletions includes/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,37 @@ public static function init() {
\add_action( 'wp_enqueue_scripts', array( self::class, 'add_data' ) );
\add_action( 'enqueue_block_editor_assets', array( self::class, 'add_data' ) );
\add_action( 'load-post-new.php', array( self::class, 'handle_in_reply_to_get_param' ) );
// Add editor plugin
\add_action( 'enqueue_block_editor_assets', array( self::class, 'enqueue_editor_assets' ) );
\add_action( 'init', array( self::class, 'register_postmeta' ), 11 );
}

public static function register_postmeta() {
$ap_post_types = \get_post_types_by_support( 'activitypub' );
foreach ( $ap_post_types as $post_type ) {
\register_post_meta(
$post_type,
'activitypub_content_warning',
array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
)
);
}
}

public static function enqueue_editor_assets() {
// check for our supported post types
$current_screen = \get_current_screen();
$ap_post_types = \get_post_types_by_support( 'activitypub' );
if ( ! $current_screen || ! in_array( $current_screen->post_type, $ap_post_types, true ) ) {
return;
}
$asset_data = include ACTIVITYPUB_PLUGIN_DIR . 'build/editor-plugin/plugin.asset.php';
$plugin_url = plugins_url( 'build/editor-plugin/plugin.js', ACTIVITYPUB_PLUGIN_FILE );
wp_enqueue_script( 'activitypub-block-editor', $plugin_url, $asset_data['dependencies'], $asset_data['version'], true );
}

/**
Expand Down
21 changes: 21 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1215,3 +1215,24 @@ function generate_post_summary( $post, $length = 500 ) {
*/
return $content;
}

/**
* Get the content warning of a post.
*
* @param int $post_id The post ID.
*
* @return string|false The content warning or false if not found.
*/
function get_content_warning( $post_id ) {
$post = get_post( $post_id );
if ( ! $post ) {
return false;
}

$warning = get_post_meta( $post->ID, 'activitypub_content_warning', true );
if ( empty( $warning ) ) {
return false;
}

return $warning;
}
7 changes: 7 additions & 0 deletions includes/transformer/class-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use function Activitypub\get_rest_url_by_path;
use function Activitypub\is_user_type_disabled;
use function Activitypub\generate_post_summary;
use function Activitypub\get_content_warning;

/**
* WordPress Post Transformer
Expand Down Expand Up @@ -87,6 +88,12 @@ public function to_object() {
)
);

$content_warning = get_content_warning( $post );
if ( ! empty( $content_warning ) ) {
$object->set_sensitive( true );
$object->set_summary( $content_warning );
}

return $object;
}

Expand Down
9 changes: 9 additions & 0 deletions src/editor-plugin/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "editor-plugin",
"title": "Editor Plugin: not a block, but block.json is very useful.",
"category": "widgets",
"icon": "admin-comments",
"keywords": [
],
"editorScript": "file:./plugin.js"
}
33 changes: 33 additions & 0 deletions src/editor-plugin/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { PluginDocumentSettingPanel } from '@wordpress/editor';
import { registerPlugin } from '@wordpress/plugins';
import { TextControl } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';
import { __ } from '@wordpress/i18n';


const EditorPlugin = () => {
const postType = useSelect(
( select ) => select( 'core/editor' ).getCurrentPostType(),
[]
);
const [ meta, setMeta ] = useEntityProp( 'postType', postType, 'meta' );

return (
<PluginDocumentSettingPanel
name="activitypub"
title={ __( 'Fediverse', 'activitypub' ) }
>
<TextControl
label={ __( 'Content Warning', 'activitypub' ) }
value={ meta?.activitypub_content_warning }
onChange={ ( value ) => {
setMeta( { ...meta, activitypub_content_warning: value } );
} }
placeholder={ __( 'Optional content warning', 'activitypub' ) }
/>
</PluginDocumentSettingPanel>
);
}

registerPlugin( 'activitypub-editor-plugin', { render: EditorPlugin } );
7 changes: 4 additions & 3 deletions tests/test-class-activitypub-activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ function ( $mentions ) {

public function test_object_transformation() {
$test_array = array(
'id' => 'https://example.com/post/123',
'type' => 'Note',
'content' => 'Hello world!',
'id' => 'https://example.com/post/123',
'type' => 'Note',
'content' => 'Hello world!',
'sensitive' => false,
);

$object = \Activitypub\Activity\Base_Object::init_from_array( $test_array );
Expand Down

0 comments on commit de85cca

Please sign in to comment.