|
| 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 | +} |
0 commit comments