Skip to content

Commit 65d8d74

Browse files
authored
Add quote visibility setting for Classic Editor (#2374)
1 parent a94d7fd commit 65d8d74

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
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+
Add quote visibility setting for Classic Editor users.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Classic Editor integration file.
4+
*
5+
* @package Activitypub
6+
*/
7+
8+
namespace Activitypub\Integration;
9+
10+
/**
11+
* Classic Editor integration class.
12+
*
13+
* Handles compatibility with the Classic Editor plugin and sites without block editor support.
14+
*/
15+
class Classic_Editor {
16+
17+
/**
18+
* Initialize the class, registering WordPress hooks.
19+
*/
20+
public static function init() {
21+
\add_action( 'post_comment_status_meta_box-options', array( self::class, 'add_quote_policy_field' ) );
22+
\add_action( 'save_post', array( self::class, 'save_quote_policy' ) );
23+
}
24+
25+
/**
26+
* Add quote policy field to the Discussion meta box.
27+
*
28+
* @param \WP_Post $post The post object.
29+
*/
30+
public static function add_quote_policy_field( $post ) {
31+
// Only add field for post types that support ActivityPub.
32+
if ( ! \post_type_supports( $post->post_type, 'activitypub' ) ) {
33+
return;
34+
}
35+
36+
// Add nonce for security.
37+
\wp_nonce_field( 'activitypub_quote_policy', 'activitypub_quote_policy_nonce' );
38+
39+
// Get current value.
40+
$quote_interaction_policy = \get_post_meta( $post->ID, 'activitypub_interaction_policy_quote', true ) ?: ACTIVITYPUB_INTERACTION_POLICY_ANYONE; // phpcs:ignore Universal.Operators.DisallowShortTernary
41+
?>
42+
<br class="clear" />
43+
<label for="activitypub_allow_quotes" class="selectit">
44+
<input name="activitypub_allow_quotes" type="checkbox" id="activitypub_allow_quotes" value="1" <?php \checked( $quote_interaction_policy, ACTIVITYPUB_INTERACTION_POLICY_ANYONE ); ?> />
45+
<?php \esc_html_e( 'Allow quotes from the Fediverse', 'activitypub' ); ?>
46+
</label>
47+
<?php
48+
}
49+
50+
/**
51+
* Save quote policy data.
52+
*
53+
* @param int $post_id The post ID.
54+
*/
55+
public static function save_quote_policy( $post_id ) {
56+
// Check if this is an autosave.
57+
if ( \defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
58+
return;
59+
}
60+
61+
// Only process if nonce is set (meaning we're saving from the post editor).
62+
if ( ! isset( $_POST['activitypub_quote_policy_nonce'] ) ) {
63+
return;
64+
}
65+
66+
// Verify nonce.
67+
if ( ! \wp_verify_nonce( \sanitize_text_field( \wp_unslash( $_POST['activitypub_quote_policy_nonce'] ) ), 'activitypub_quote_policy' ) ) {
68+
return;
69+
}
70+
71+
// Only process for post types that support ActivityPub.
72+
if ( ! \post_type_supports( \get_post_type( $post_id ), 'activitypub' ) ) {
73+
return;
74+
}
75+
76+
// Check user permissions.
77+
if ( ! \current_user_can( 'edit_post', $post_id ) ) {
78+
return;
79+
}
80+
81+
// Save quote interaction policy based on checkbox.
82+
if ( '1' === \sanitize_text_field( \wp_unslash( $_POST['activitypub_allow_quotes'] ?? '0' ) ) ) {
83+
// Checked: allow anyone to quote (default value).
84+
\update_post_meta( $post_id, 'activitypub_interaction_policy_quote', ACTIVITYPUB_INTERACTION_POLICY_ANYONE );
85+
} else {
86+
// Unchecked: only allow "just me" to quote.
87+
\update_post_meta( $post_id, 'activitypub_interaction_policy_quote', ACTIVITYPUB_INTERACTION_POLICY_ME );
88+
}
89+
}
90+
}

integration/load.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,26 @@
77

88
namespace Activitypub\Integration;
99

10+
use function Activitypub\site_supports_blocks;
11+
1012
\Activitypub\Autoloader::register_path( __NAMESPACE__, __DIR__ );
1113

1214
/**
1315
* Initialize the ActivityPub integrations.
1416
*/
1517
function plugin_init() {
18+
/**
19+
* Adds Classic Editor support.
20+
*
21+
* This class handles the compatibility with the Classic Editor plugin
22+
* and sites without block editor support.
23+
*
24+
* @see https://wordpress.org/plugins/classic-editor/
25+
*/
26+
if ( class_exists( '\Classic_Editor' ) || ! site_supports_blocks() ) {
27+
Classic_Editor::init();
28+
}
29+
1630
/**
1731
* Adds WebFinger (plugin) support.
1832
*

0 commit comments

Comments
 (0)