diff --git a/includes/class-admin.php b/includes/class-admin.php index 1dd7107..fa4536a 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -56,7 +56,7 @@ public static function discussion_settings() { * * @param object $object The comment object. */ - public static function meta_boxes( $object ) { + public static function comment_metabox( $object ) { wp_nonce_field( 'webmention_comment_metabox', 'webmention_comment_nonce' ); if ( ! $object instanceof WP_Comment ) { @@ -65,6 +65,16 @@ public static function meta_boxes( $object ) { load_template( __DIR__ . '/../templates/webmention-edit-comment-form.php' ); } + /** + * Add Webmention settings meta box to the Classic editor screen. + * + * @param object $object The comment object. + */ + public static function post_metabox( $object ) { + wp_nonce_field( 'webmention_post_metabox', 'webmention_post_nonce' ); + load_template( __DIR__ . '/../templates/webmention-edit-post-form.php' ); + } + /** * Add comment-type as column in WP-Admin * @@ -162,11 +172,23 @@ public static function add_meta_boxes() { add_meta_box( 'webmention-meta', esc_html__( 'Webmention Data', 'webmention' ), - array( static::class, 'meta_boxes' ), + array( static::class, 'comment_metabox' ), 'comment', 'normal', 'default' ); + add_meta_box( + 'webmention-meta', + esc_html__( 'Webmention Settings', 'webmention' ), + array( static::class, 'post_metabox' ), + get_option( 'webmention_support_post_types', array( 'post', 'page' ) ), + 'side', + 'default', + array( + '__block_editor_compatible_meta_box' => true, + '__back_compat_meta_box' => true, // This should only be used in the Classic Editor. + ) + ); } /** diff --git a/includes/class-block.php b/includes/class-block.php index a5105e0..08630e8 100644 --- a/includes/class-block.php +++ b/includes/class-block.php @@ -9,34 +9,6 @@ class Block { public static function init() { // Add editor plugin. \add_action( 'enqueue_block_editor_assets', array( self::class, 'enqueue_editor_assets' ) ); - \add_action( 'init', array( self::class, 'register_postmeta' ), 11 ); - } - - /** - * Register post meta - */ - public static function register_postmeta() { - $post_types = \get_post_types_by_support( 'webmentions' ); - foreach ( $post_types as $post_type ) { - \register_post_meta( - $post_type, - 'webmentions_disabled', - array( - 'show_in_rest' => true, - 'single' => true, - 'type' => 'boolean', - ) - ); - \register_post_meta( - $post_type, - 'webmentions_disabled_pings', - array( - 'show_in_rest' => true, - 'single' => true, - 'type' => 'boolean', - ) - ); - } } /** diff --git a/includes/class-receiver.php b/includes/class-receiver.php index 431b988..6700da3 100755 --- a/includes/class-receiver.php +++ b/includes/class-receiver.php @@ -43,6 +43,11 @@ public static function init() { // Add scheduler for async processing add_action( 'webmention_process_schedule', array( static::class, 'process' ) ); + $post_types = get_post_types_by_support( 'webmentions' ); + foreach ( $post_types as $post_type ) { + add_action( 'save_' . $post_type, array( static::class, 'save_hook' ), 3 ); + } + self::register_meta(); } @@ -50,6 +55,18 @@ public static function init() { * This is more to lay out the data structure than anything else. */ public static function register_meta() { + $post_types = \get_post_types_by_support( 'webmentions' ); + foreach ( $post_types as $post_type ) { + \register_post_meta( + $post_type, + 'webmentions_disabled', + array( + 'show_in_rest' => true, + 'single' => true, + 'type' => 'boolean', + ) + ); + } $args = array( 'type' => 'string', 'description' => esc_html__( 'Protocol Used to Receive', 'webmention' ), @@ -127,6 +144,22 @@ public static function register_meta() { register_meta( 'comment', 'avatar', $args ); } + /** + * Saves optional settings on save + * + * @param int $post_id Post ID. + */ + public static function save_hook( $post_id ) { + if ( array_key_exists( 'webmentions_disabled', $_POST ) ) { + if ( 1 === intval( $_POST['webmentions_disabled'] ) ) { + \add_post_meta( $post_id, 'webmentions_disabled', '1', true ); + } else { + \delete_post_meta( $post_id, 'webmentions_disabled' ); + + } + } + } + /** * Register the Route. */ diff --git a/includes/class-sender.php b/includes/class-sender.php index 4bb78ad..430097f 100755 --- a/includes/class-sender.php +++ b/includes/class-sender.php @@ -25,6 +25,7 @@ public static function init() { // Send Webmentions from Every Type that Declared Webmention Support $post_types = get_post_types_by_support( 'webmentions' ); foreach ( $post_types as $post_type ) { + add_action( 'save_' . $post_type, array( static::class, 'save_hook' ), 3 ); add_action( 'publish_' . $post_type, array( static::class, 'publish_hook' ), 3 ); add_action( 'trashed_' . $post_type, array( static::class, 'trash_hook' ) ); } @@ -36,6 +37,41 @@ public static function init() { // remote delete posts add_action( 'webmention_delete', array( static::class, 'send_webmentions' ) ); + self::register_postmeta(); + } + + /** + * Register post meta + */ + public static function register_meta() { + $post_types = \get_post_types_by_support( 'webmentions' ); + foreach ( $post_types as $post_type ) { + \register_post_meta( + $post_type, + 'webmentions_disabled_pings', + array( + 'show_in_rest' => true, + 'single' => true, + 'type' => 'boolean', + ) + ); + } + } + + /** + * Saves optional settings on save + * + * @param int $post_id Post ID. + */ + public static function save_hook( $post_id ) { + if ( array_key_exists( 'webmentions_disabled_pings', $_POST ) ) { + if ( 1 === intval( $_POST['webmentions_disabled_pings'] ) ) { + \add_post_meta( $post_id, 'webmentions_disabled_pings', '1', true ); + } else { + \delete_post_meta( $post_id, 'webmentions_disabled_pings' ); + + } + } } /** diff --git a/templates/webmention-edit-post-form.php b/templates/webmention-edit-post-form.php new file mode 100644 index 0000000..7fe64bb --- /dev/null +++ b/templates/webmention-edit-post-form.php @@ -0,0 +1,13 @@ + +