Skip to content

Commit 381b3f0

Browse files
authored
Merge pull request #466 from pfefferle/fix/delete
Fix delete-webmentions
2 parents 57ecf6d + b3a86bd commit 381b3f0

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

includes/class-sender.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ public static function init() {
2626
$post_types = get_post_types_by_support( 'webmentions' );
2727
foreach ( $post_types as $post_type ) {
2828
add_action( 'publish_' . $post_type, array( static::class, 'publish_hook' ), 3 );
29+
add_action( 'trashed_' . $post_type, array( static::class, 'trash_hook' ) );
2930
}
3031

32+
add_action( 'wp_trash_post', array( self::class, 'trash_post' ), 1 );
33+
add_action( 'untrash_post', array( self::class, 'untrash_post' ), 1 );
34+
3135
add_action( 'comment_post', array( static::class, 'comment_post' ) );
3236

3337
// remote delete posts
34-
add_action( 'trashed_post', array( static::class, 'trash_hook' ) );
3538
add_action( 'webmention_delete', array( static::class, 'send_webmentions' ) );
3639
}
3740

@@ -84,6 +87,33 @@ public static function trash_hook( $post_id ) {
8487
wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'webmention_delete', array( $post_id ) );
8588
}
8689

90+
/**
91+
* Store permalink in meta, to send delete Webmention.
92+
*
93+
* @param string $post_id The Post ID.
94+
*
95+
* @return void
96+
*/
97+
public static function trash_post( $post_id ) {
98+
add_post_meta(
99+
$post_id,
100+
'webmention_canonical_url',
101+
get_permalink( $post_id ),
102+
true
103+
);
104+
}
105+
106+
/**
107+
* Delete permalink from meta
108+
*
109+
* @param string $post_id The Post ID
110+
*
111+
* @return void
112+
*/
113+
public static function untrash_post( $post_id ) {
114+
delete_post_meta( $post_id, 'webmention_canonical_url' );
115+
}
116+
87117
/**
88118
* Send Webmentions.
89119
*
@@ -158,11 +188,11 @@ public static function send_webmention( $source, $target, $post_id = null ) {
158188
* @return array|bool array of results or false if failed.
159189
*/
160190
public static function send_webmentions( $post_id ) {
161-
// get source url
162-
$source = get_permalink( $post_id );
191+
$source = get_post_meta( $post_id, 'webmention_canonical_url', true );
163192

164-
// remove `__trashed` from the url
165-
$source = str_replace( '__trashed', '', $source );
193+
if ( ! $source ) {
194+
$source = get_permalink( $post_id );
195+
}
166196

167197
// get post
168198
$post = get_post( $post_id );

0 commit comments

Comments
 (0)