Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content warnings! #900

Merged
merged 9 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

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
30 changes: 30 additions & 0 deletions includes/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,36 @@ 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',
)
);
}
}

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
26 changes: 26 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,11 @@ function generate_post_summary( $post, $length = 500 ) {
return '';
}

$content_warning = get_content_warning( $post->ID );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you set it in the transformer, you do not also have to add it here.

I quite like the idea to set it in the transformer only, this we can still generate the summary!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we would keep it in there, it would show the "content warning" also in the content field if someone would use the shortcode [ap_excerpt] for the content field.

if ( $content_warning ) {
return $content_warning;
}

pfefferle marked this conversation as resolved.
Show resolved Hide resolved
$content = \sanitize_post_field( 'post_excerpt', $post->post_excerpt, $post->ID );

if ( $content ) {
Expand Down Expand Up @@ -1215,3 +1220,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;
pfefferle marked this conversation as resolved.
Show resolved Hide resolved
}
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 } );
Loading