Skip to content

Commit 9d5522c

Browse files
committed
add block-editor settings to disable webmentions
thanks so much @mattwiebe for all your help with the block editor ❤️
1 parent f140c1a commit 9d5522c

File tree

10 files changed

+120
-6
lines changed

10 files changed

+120
-6
lines changed

.distignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/node_modules
55
/bin
66
/sass
7+
/src
78
/vendor
89
/tests
910
/config

.editorconfig

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,3 @@ charset = utf-8
1111
[*.php]
1212
indent_style = tab
1313
indent_size = 4
14-
15-
[*.{js,json}]
16-
indent_style = space
17-
indent_size = 2

build/editor-plugin/block.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "editor-plugin",
3+
"title": "Editor Plugin: not a block, but block.json is very useful.",
4+
"category": "widgets",
5+
"icon": "admin-comments",
6+
"keywords": [],
7+
"editorScript": "file:./plugin.js"
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php return array('dependencies' => array('react', 'wp-components', 'wp-core-data', 'wp-data', 'wp-editor', 'wp-i18n', 'wp-plugins'), 'version' => '48d7b75f27b9d4f0a5e9');

build/editor-plugin/plugin.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/class-block.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Webmention;
4+
5+
class Block {
6+
/**
7+
* Initialize the class, registering WordPress hooks.
8+
*/
9+
public static function init() {
10+
// Add editor plugin.
11+
\add_action( 'enqueue_block_editor_assets', array( self::class, 'enqueue_editor_assets' ) );
12+
\add_action( 'init', array( self::class, 'register_postmeta' ), 11 );
13+
}
14+
15+
/**
16+
* Register post meta
17+
*/
18+
public static function register_postmeta() {
19+
$post_types = \get_post_types_by_support( 'webmentions' );
20+
foreach ( $post_types as $post_type ) {
21+
\register_post_meta(
22+
$post_type,
23+
'webmentions_closed',
24+
array(
25+
'show_in_rest' => true,
26+
'single' => true,
27+
'type' => 'boolean',
28+
)
29+
);
30+
}
31+
}
32+
33+
/**
34+
* Enqueue the block editor assets.
35+
*/
36+
public static function enqueue_editor_assets() {
37+
// Check for our supported post types.
38+
$current_screen = \get_current_screen();
39+
$ap_post_types = \get_post_types_by_support( 'webmentions' );
40+
if ( ! $current_screen || ! in_array( $current_screen->post_type, $ap_post_types, true ) ) {
41+
return;
42+
}
43+
$asset_data = include WEBMENTION_PLUGIN_DIR . 'build/editor-plugin/plugin.asset.php';
44+
$plugin_url = plugins_url( 'build/editor-plugin/plugin.js', WEBMENTION_PLUGIN_FILE );
45+
wp_enqueue_script( 'webmention-block-editor', $plugin_url, $asset_data['dependencies'], $asset_data['version'], true );
46+
}
47+
}

package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"description": "Webmention support for WordPress posts",
44
"main": "webmention.php",
55
"devDependencies": {
6+
"@wordpress/components": "^28.0.0",
7+
"@wordpress/data": "^10.0.0",
8+
"@wordpress/env": "^10.10.0",
9+
"@wordpress/icons": "^10.10.0",
610
"grunt": "^1.6.1",
711
"grunt-checktextdomain": "^1.0.1",
812
"grunt-wp-i18n": "^1.0.3",
@@ -23,5 +27,12 @@
2327
"bugs": {
2428
"url": "https://github.com/pfefferle/wordpress-webmention/issues"
2529
},
26-
"homepage": "https://github.com/pfefferle/wordpress-webmention"
30+
"homepage": "https://github.com/pfefferle/wordpress-webmention",
31+
"scripts": {
32+
"dev": "wp-scripts start",
33+
"build": "wp-scripts build",
34+
"readme": "grunt wp_readme_to_markdown",
35+
"env-start": "wp-env start",
36+
"env-stop": "wp-env stop"
37+
}
2738
}

src/editor-plugin/block.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "editor-plugin",
3+
"title": "Editor Plugin: not a block, but block.json is very useful.",
4+
"category": "widgets",
5+
"icon": "admin-comments",
6+
"keywords": [
7+
],
8+
"editorScript": "file:./plugin.js"
9+
}

src/editor-plugin/plugin.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { PluginDocumentSettingPanel } from '@wordpress/editor';
2+
import { registerPlugin } from '@wordpress/plugins';
3+
import { CheckboxControl } from '@wordpress/components';
4+
import { useSelect } from '@wordpress/data';
5+
import { useEntityProp } from '@wordpress/core-data';
6+
import { __ } from '@wordpress/i18n';
7+
8+
9+
const EditorPlugin = () => {
10+
const postType = useSelect(
11+
( select ) => select( 'core/editor' ).getCurrentPostType(),
12+
[]
13+
);
14+
const [ meta, setMeta ] = useEntityProp( 'postType', postType, 'meta' );
15+
16+
return (
17+
<PluginDocumentSettingPanel
18+
name="webmention"
19+
title={ __( 'Webmentions', 'webmention' ) }
20+
>
21+
<CheckboxControl
22+
__nextHasNoMarginBottom
23+
label={ __( 'Close Webmentions', 'webmention' ) }
24+
help={ __( 'Do not accept incoming Webmentions for this post.', 'webmention' ) }
25+
checked={ meta.webmentions_closed }
26+
onChange={ ( value ) => {
27+
setMeta( { ...meta, webmentions_closed: value } );
28+
} }
29+
/>
30+
</PluginDocumentSettingPanel>
31+
);
32+
}
33+
34+
registerPlugin( 'webmention-editor-plugin', { render: EditorPlugin } );

webmention.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ function init() {
120120
require_once __DIR__ . '/includes/class-discovery.php';
121121
add_action( 'init', array( '\Webmention\Discovery', 'init' ) );
122122

123+
// initialize Webmention Bloks.
124+
require_once __DIR__ . '/includes/class-block.php';
125+
add_action( 'init', array( '\Webmention\Block', 'init' ) );
126+
123127
// load local avatar store.
124128
if ( WEBMENTION_LOCAL_AVATAR_STORE ) {
125129
require_once __DIR__ . '/includes/class-avatar-store.php';
@@ -158,7 +162,9 @@ function ( $meta_id, $object_id, $meta_key, $meta_value ) {
158162
if ( 'webmentions_closed' === $meta_key && empty( $meta_value ) ) {
159163
\delete_post_meta( $object_id, 'webmentions_closed' );
160164
}
161-
}
165+
},
166+
10,
167+
4
162168
);
163169
}
164170
add_action( 'plugins_loaded', '\Webmention\init' );

0 commit comments

Comments
 (0)