Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
2e295de
Remove dependence on pings_open for whether a webmention was open.
dshanske Jul 22, 2024
525549c
Switch from using the ping list in the post to a separate list that o…
dshanske Jul 22, 2024
cff074e
Remove ping usage from sender
dshanske Jul 22, 2024
1b24c81
Merge branch 'main' into webmentionopen
pfefferle Jul 22, 2024
c3da954
Restore update punged
dshanske Aug 24, 2024
25bbcee
Merge branch 'main' into webmentionopen
dshanske Aug 24, 2024
3da5d65
Merge branch 'main' into webmentionopen
pfefferle Sep 13, 2024
ab67e07
Merge branch 'main' into webmentionopen
pfefferle Sep 14, 2024
0681b06
Simplify webmentions_open and add better comment
dshanske Nov 14, 2024
f140c1a
remove the "webmentions_closed" meta value if the post is updated
pfefferle Nov 14, 2024
9d5522c
add block-editor settings to disable webmentions
pfefferle Nov 14, 2024
18c89ce
Merge branch 'main' into webmentionopen
pfefferle Nov 14, 2024
d743ab0
fix phpcs
pfefferle Nov 14, 2024
e08d739
check if site supports blocks before loading them
pfefferle Nov 14, 2024
e0d7cfd
Merge branch 'main' into webmentionopen
dshanske Nov 17, 2024
8b11396
re-add block-editor dependencies
pfefferle Nov 17, 2024
3b26a5e
Merge branch 'main' into webmentionopen
pfefferle Nov 25, 2024
ed657c6
small change
pfefferle Dec 8, 2024
43ecf97
phpunit
pfefferle Dec 13, 2024
21a469c
Merge branch 'main' into webmentionopen
pfefferle Feb 23, 2025
b46e514
fix test
pfefferle Feb 23, 2025
9f2106e
fix tests
pfefferle Feb 23, 2025
b0d1085
change name
pfefferle Feb 23, 2025
a888261
use simpler name
pfefferle Feb 23, 2025
ef3e59e
update name
pfefferle Feb 23, 2025
84abba8
clear _mentionme after sending the mentions
pfefferle Feb 23, 2025
858506d
revert
pfefferle Feb 23, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .distignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/node_modules
/bin
/sass
/src
/vendor
/tests
/config
Expand Down
4 changes: 0 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,3 @@ charset = utf-8
[*.php]
indent_style = tab
indent_size = 4

[*.{js,json}]
indent_style = space
indent_size = 2
8 changes: 8 additions & 0 deletions build/editor-plugin/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "editor-plugin",
"title": "Editor Plugin: not a block, but block.json is very useful.",
"category": "widgets",
"icon": "admin-comments",
"keywords": [],
"editorScript": "file:./plugin.js"
}
1 change: 1 addition & 0 deletions build/editor-plugin/plugin.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array('react', 'wp-components', 'wp-core-data', 'wp-data', 'wp-editor', 'wp-i18n', 'wp-plugins'), 'version' => '03845d869ccb1b1eb23c');
1 change: 1 addition & 0 deletions build/editor-plugin/plugin.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions includes/class-block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Webmention;

class Block {
/**
* Initialize the class, registering WordPress hooks.
*/
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',
)
);
}
}

/**
* Enqueue the block editor assets.
*/
public static function enqueue_editor_assets() {
// Check for our supported post types.
$current_screen = \get_current_screen();
$ap_post_types = \get_post_types_by_support( 'webmentions' );
if ( ! $current_screen || ! in_array( $current_screen->post_type, $ap_post_types, true ) ) {
return;
}
$asset_data = include WEBMENTION_PLUGIN_DIR . 'build/editor-plugin/plugin.asset.php';
$plugin_url = plugins_url( 'build/editor-plugin/plugin.js', WEBMENTION_PLUGIN_FILE );
wp_enqueue_script( 'webmention-block-editor', $plugin_url, $asset_data['dependencies'], $asset_data['version'], true );
}
}
2 changes: 1 addition & 1 deletion includes/class-receiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public static function post( $request ) {

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

$post = get_post( $comment_post_id );
Expand Down
44 changes: 21 additions & 23 deletions includes/class-sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,15 @@ public static function send_webmentions( $post_id ) {
$urls = webmention_extract_urls( $post->post_content, $support_media_urls );

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

// Find previously sent Webmentions and send them one last time.
$deletes = array_diff( $pung, $targets );
$deletes = array_diff( $mentioned, $targets );

$ping = array();
$mentions = array();

foreach ( $targets as $target ) {
// send Webmention
Expand All @@ -225,16 +226,13 @@ public static function send_webmentions( $post_id ) {
continue;
}

// check response
if (
! is_wp_error( $response ) &&
wp_remote_retrieve_response_code( $response ) < 400
) {
$ping[] = $target;
}
$code = wp_remote_retrieve_response_code( $response );

// reschedule if server responds with a http error 5xx
if ( wp_remote_retrieve_response_code( $response ) >= 500 ) {
// check response
if ( ! is_wp_error( $response ) && $code < 400 ) {
$mentions[] = $target;
} elseif ( $code >= 500 ) {
// reschedule if server responds with a http error 5xx
self::reschedule( $post_id );
}
}
Expand All @@ -246,23 +244,23 @@ public static function send_webmentions( $post_id ) {
// reschedule if server responds with a http error 5xx
if ( wp_remote_retrieve_response_code( $response ) >= 500 ) {
self::reschedule( $post_id );
$ping[] = $deleted;
$mentions[] = $deleted;
}
}

if ( ! empty( $mentions ) ) {
update_post_meta( $post_id, '_webmentioned', $mentions );
}

$pung = get_pung( $post );

if ( ! empty( $ping ) ) {
self::update_ping( $post, $ping );
self::update_ping( $post, array_merge( $pung, $ping ) );
}

return $ping;
return $mentions;
}

/*
* Update the Pinged List as Opposed to Adding to It.
*
* @param int|WP_Post $post_id Post.
* @param array $pinged Array of URLs
*/
public static function update_ping( $post_id, $pinged ) {
global $wpdb;
$post = get_post( $post_id );
Expand Down
80 changes: 43 additions & 37 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,18 +550,22 @@ function ifset( &$var, $return = false ) { // phpcs:ignore Universal.NamingConve
* @return boolean if webmentions are open
*/
function webmentions_open( $post = null ) {
$_post = get_post( $post );
$post_id = $_post ? $_post->ID : 0;

// If the post type does not support Webmentions do not even check further
if ( ! post_type_supports( get_post_type( $post_id ), 'webmentions' ) ) {
return false;
$post = get_post( $post );
$post_id = $post ? $post->ID : 0;
$open = false;
if ( $post ) {
// Always consider the home mention link page to be open.
if ( get_option( 'webmention_home_mentions' ) === $post->ID ) {
$open = true;
} elseif ( ! post_type_supports( get_post_type( $post ), 'webmentions' ) ) {
// If the post type does not support Webmentions do not even check further.
$open = false;
} else {
// If the webmentions_disabled meta key exists then consider webmentions closed. Otherwise consider them open.
$open = ! ( metadata_exists( 'post', $post_id, 'webmentions_disabled' ) ); // Invert the result, as exists is closed and not exists is open.
}
}

if ( get_option( 'webmention_home_mentions' ) === $post_id ) {
return true;
}
$open = ( $_post && ( pings_open( $post ) ) );
/**
* Filters whether the current post is open for webmentions.
*
Expand All @@ -572,23 +576,6 @@ function webmentions_open( $post = null ) {
return apply_filters( 'webmentions_open', $open, $post_id );
}

/**
* Return enabled status of Homepage Webmentions.
*
* @since 3.8.9
*
* @param bool $open Whether the current post is open for pings.
* @param int $post_id The post ID.
* @return boolean if pings are open
*/
function webmention_pings_open( $open, $post_id ) {
if ( get_option( 'webmention_home_mentions' ) === $post_id ) {
return true;
}

return $open;
}

/**
* Retrieve the default comment status for a given post type.
*
Expand All @@ -602,16 +589,7 @@ function webmention_pings_open( $open, $post_id ) {
* @return string
*/
function webmention_get_default_comment_status( $status, $post_type, $comment_type ) {
if ( 'webmention' === $comment_type ) {
return post_type_supports( $post_type, 'webmentions' ) ? 'open' : 'closed';
}

// Since support for the pingback comment type is used to keep pings open...
if ( ( 'pingback' === $comment_type ) ) {
return ( post_type_supports( $post_type, 'webmentions' ) ? 'open' : $status );
}

return $status;
return is_registered_webmention_comment_type( $comment_type ) ? 'open' : $status;
}

/**
Expand Down Expand Up @@ -707,3 +685,31 @@ function is_html( $string ) { // phpcs:ignore Universal.NamingConventions.NoRese
return ( wp_strip_all_tags( $string ) !== $string );
}
}

/**
* Check if a site supports the block editor.
*
* @return boolean True if the site supports the block editor, false otherwise.
*/
function site_supports_blocks() {
$return = true;

if ( \version_compare( \get_bloginfo( 'version' ), '5.9', '<' ) ) {
$return = false;
} elseif ( \function_exists( 'classicpress_version' ) ) {
$return = false;
} elseif (
! \function_exists( 'register_block_type_from_metadata' ) ||
! \function_exists( 'do_blocks' )
) {
$return = false;
}

/**
* Allow plugins to disable block editor support,
* thus disabling blocks registered by the Webmentions plugin.
*
* @param boolean $supports_blocks True if the site supports the block editor, false otherwise.
*/
return apply_filters( 'webmention_site_supports_blocks', $return );
}
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"description": "Webmention support for WordPress posts",
"main": "webmention.php",
"devDependencies": {
"@wordpress/components": "^28.0.0",
"@wordpress/data": "^10.0.0",
"@wordpress/env": "^10.10.0",
"@wordpress/icons": "^10.10.0"
},
"repository": {
"type": "git",
Expand All @@ -19,5 +23,12 @@
"bugs": {
"url": "https://github.com/pfefferle/wordpress-webmention/issues"
},
"homepage": "https://github.com/pfefferle/wordpress-webmention"
"homepage": "https://github.com/pfefferle/wordpress-webmention",
"scripts": {
"dev": "wp-scripts start",
"build": "wp-scripts build",
"readme": "grunt wp_readme_to_markdown",
"env-start": "wp-env start",
"env-stop": "wp-env stop"
}
}
8 changes: 8 additions & 0 deletions src/editor-plugin/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "editor-plugin",
"title": "Editor Plugin: not a block, but block.json is very useful.",
"category": "widgets",
"icon": "admin-comments",
"keywords": [],
"editorScript": "file:./plugin.js"
}
34 changes: 34 additions & 0 deletions src/editor-plugin/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { PluginDocumentSettingPanel } from '@wordpress/editor';
import { registerPlugin } from '@wordpress/plugins';
import { CheckboxControl } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';
import { __ } from '@wordpress/i18n';


const EditorPlugin = () => {
const postType = useSelect(
( select ) => select( 'core/editor' ).getCurrentPostType(),
[]
);
const [ meta, setMeta ] = useEntityProp( 'postType', postType, 'meta' );

return (
<PluginDocumentSettingPanel
name="webmention"
title={ __( 'Webmentions', 'webmention' ) }
>
<CheckboxControl
__nextHasNoMarginBottom
label={ __( 'Disable Webmentions', 'webmention' ) }
help={ __( 'Do not accept incoming Webmentions for this post.', 'webmention' ) }
checked={ meta.webmentions_disabled }
onChange={ ( value ) => {
setMeta( { ...meta, webmentions_disabled: value } );
} }
/>
</PluginDocumentSettingPanel>
);
}

registerPlugin( 'webmention-editor-plugin', { render: EditorPlugin } );
Loading