Skip to content

Commit 6b85a47

Browse files
authored
Merge pull request #485 from pfefferle/webmentionopen
Remove Webmention Dependence on Pings
2 parents 1491c6b + 858506d commit 6b85a47

File tree

15 files changed

+297
-68
lines changed

15 files changed

+297
-68
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' => '03845d869ccb1b1eb23c');

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_disabled',
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+
}

includes/class-receiver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public static function post( $request ) {
242242

243243
// check if webmentions are allowed
244244
if ( ! webmentions_open( $comment_post_id ) ) {
245-
return new WP_Error( 'webmentions_closed', esc_html__( 'Webmentions are disabled for this post', 'webmention' ), array( 'status' => 400 ) );
245+
return new WP_Error( 'webmentions_disabled', esc_html__( 'Webmentions are disabled for this post', 'webmention' ), array( 'status' => 400 ) );
246246
}
247247

248248
$post = get_post( $comment_post_id );

includes/class-sender.php

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,15 @@ public static function send_webmentions( $post_id ) {
208208
$urls = webmention_extract_urls( $post->post_content, $support_media_urls );
209209

210210
// filter links
211-
$targets = apply_filters( 'webmention_links', $urls, $post_id );
212-
$targets = array_unique( $targets );
213-
$pung = get_pung( $post );
211+
$targets = apply_filters( 'webmention_links', $urls, $post_id );
212+
$targets = array_unique( $targets );
213+
$mentioned = get_post_meta( $post->ID, '_webmentioned', true );
214+
$mentioned = empty( $mentioned ) ? array() : $mentioned;
214215

215216
// Find previously sent Webmentions and send them one last time.
216-
$deletes = array_diff( $pung, $targets );
217+
$deletes = array_diff( $mentioned, $targets );
217218

218-
$ping = array();
219+
$mentions = array();
219220

220221
foreach ( $targets as $target ) {
221222
// send Webmention
@@ -225,16 +226,13 @@ public static function send_webmentions( $post_id ) {
225226
continue;
226227
}
227228

228-
// check response
229-
if (
230-
! is_wp_error( $response ) &&
231-
wp_remote_retrieve_response_code( $response ) < 400
232-
) {
233-
$ping[] = $target;
234-
}
229+
$code = wp_remote_retrieve_response_code( $response );
235230

236-
// reschedule if server responds with a http error 5xx
237-
if ( wp_remote_retrieve_response_code( $response ) >= 500 ) {
231+
// check response
232+
if ( ! is_wp_error( $response ) && $code < 400 ) {
233+
$mentions[] = $target;
234+
} elseif ( $code >= 500 ) {
235+
// reschedule if server responds with a http error 5xx
238236
self::reschedule( $post_id );
239237
}
240238
}
@@ -246,23 +244,23 @@ public static function send_webmentions( $post_id ) {
246244
// reschedule if server responds with a http error 5xx
247245
if ( wp_remote_retrieve_response_code( $response ) >= 500 ) {
248246
self::reschedule( $post_id );
249-
$ping[] = $deleted;
247+
$mentions[] = $deleted;
250248
}
251249
}
252250

251+
if ( ! empty( $mentions ) ) {
252+
update_post_meta( $post_id, '_webmentioned', $mentions );
253+
}
254+
255+
$pung = get_pung( $post );
256+
253257
if ( ! empty( $ping ) ) {
254-
self::update_ping( $post, $ping );
258+
self::update_ping( $post, array_merge( $pung, $ping ) );
255259
}
256260

257-
return $ping;
261+
return $mentions;
258262
}
259263

260-
/*
261-
* Update the Pinged List as Opposed to Adding to It.
262-
*
263-
* @param int|WP_Post $post_id Post.
264-
* @param array $pinged Array of URLs
265-
*/
266264
public static function update_ping( $post_id, $pinged ) {
267265
global $wpdb;
268266
$post = get_post( $post_id );

includes/functions.php

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -550,18 +550,22 @@ function ifset( &$var, $return = false ) { // phpcs:ignore Universal.NamingConve
550550
* @return boolean if webmentions are open
551551
*/
552552
function webmentions_open( $post = null ) {
553-
$_post = get_post( $post );
554-
$post_id = $_post ? $_post->ID : 0;
555-
556-
// If the post type does not support Webmentions do not even check further
557-
if ( ! post_type_supports( get_post_type( $post_id ), 'webmentions' ) ) {
558-
return false;
553+
$post = get_post( $post );
554+
$post_id = $post ? $post->ID : 0;
555+
$open = false;
556+
if ( $post ) {
557+
// Always consider the home mention link page to be open.
558+
if ( get_option( 'webmention_home_mentions' ) === $post->ID ) {
559+
$open = true;
560+
} elseif ( ! post_type_supports( get_post_type( $post ), 'webmentions' ) ) {
561+
// If the post type does not support Webmentions do not even check further.
562+
$open = false;
563+
} else {
564+
// If the webmentions_disabled meta key exists then consider webmentions closed. Otherwise consider them open.
565+
$open = ! ( metadata_exists( 'post', $post_id, 'webmentions_disabled' ) ); // Invert the result, as exists is closed and not exists is open.
566+
}
559567
}
560568

561-
if ( get_option( 'webmention_home_mentions' ) === $post_id ) {
562-
return true;
563-
}
564-
$open = ( $_post && ( pings_open( $post ) ) );
565569
/**
566570
* Filters whether the current post is open for webmentions.
567571
*
@@ -572,23 +576,6 @@ function webmentions_open( $post = null ) {
572576
return apply_filters( 'webmentions_open', $open, $post_id );
573577
}
574578

575-
/**
576-
* Return enabled status of Homepage Webmentions.
577-
*
578-
* @since 3.8.9
579-
*
580-
* @param bool $open Whether the current post is open for pings.
581-
* @param int $post_id The post ID.
582-
* @return boolean if pings are open
583-
*/
584-
function webmention_pings_open( $open, $post_id ) {
585-
if ( get_option( 'webmention_home_mentions' ) === $post_id ) {
586-
return true;
587-
}
588-
589-
return $open;
590-
}
591-
592579
/**
593580
* Retrieve the default comment status for a given post type.
594581
*
@@ -602,16 +589,7 @@ function webmention_pings_open( $open, $post_id ) {
602589
* @return string
603590
*/
604591
function webmention_get_default_comment_status( $status, $post_type, $comment_type ) {
605-
if ( 'webmention' === $comment_type ) {
606-
return post_type_supports( $post_type, 'webmentions' ) ? 'open' : 'closed';
607-
}
608-
609-
// Since support for the pingback comment type is used to keep pings open...
610-
if ( ( 'pingback' === $comment_type ) ) {
611-
return ( post_type_supports( $post_type, 'webmentions' ) ? 'open' : $status );
612-
}
613-
614-
return $status;
592+
return is_registered_webmention_comment_type( $comment_type ) ? 'open' : $status;
615593
}
616594

617595
/**
@@ -707,3 +685,31 @@ function is_html( $string ) { // phpcs:ignore Universal.NamingConventions.NoRese
707685
return ( wp_strip_all_tags( $string ) !== $string );
708686
}
709687
}
688+
689+
/**
690+
* Check if a site supports the block editor.
691+
*
692+
* @return boolean True if the site supports the block editor, false otherwise.
693+
*/
694+
function site_supports_blocks() {
695+
$return = true;
696+
697+
if ( \version_compare( \get_bloginfo( 'version' ), '5.9', '<' ) ) {
698+
$return = false;
699+
} elseif ( \function_exists( 'classicpress_version' ) ) {
700+
$return = false;
701+
} elseif (
702+
! \function_exists( 'register_block_type_from_metadata' ) ||
703+
! \function_exists( 'do_blocks' )
704+
) {
705+
$return = false;
706+
}
707+
708+
/**
709+
* Allow plugins to disable block editor support,
710+
* thus disabling blocks registered by the Webmentions plugin.
711+
*
712+
* @param boolean $supports_blocks True if the site supports the block editor, false otherwise.
713+
*/
714+
return apply_filters( 'webmention_site_supports_blocks', $return );
715+
}

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
},
711
"repository": {
812
"type": "git",
@@ -19,5 +23,12 @@
1923
"bugs": {
2024
"url": "https://github.com/pfefferle/wordpress-webmention/issues"
2125
},
22-
"homepage": "https://github.com/pfefferle/wordpress-webmention"
26+
"homepage": "https://github.com/pfefferle/wordpress-webmention",
27+
"scripts": {
28+
"dev": "wp-scripts start",
29+
"build": "wp-scripts build",
30+
"readme": "grunt wp_readme_to_markdown",
31+
"env-start": "wp-env start",
32+
"env-stop": "wp-env stop"
33+
}
2334
}

0 commit comments

Comments
 (0)