@@ -26,12 +26,15 @@ public static function init() {
26
26
$ post_types = get_post_types_by_support ( 'webmentions ' );
27
27
foreach ( $ post_types as $ post_type ) {
28
28
add_action ( 'publish_ ' . $ post_type , array ( static ::class, 'publish_hook ' ), 3 );
29
+ add_action ( 'trashed_ ' . $ post_type , array ( static ::class, 'trash_hook ' ) );
29
30
}
30
31
32
+ add_action ( 'wp_trash_post ' , array ( self ::class, 'trash_post ' ), 1 );
33
+ add_action ( 'untrash_post ' , array ( self ::class, 'untrash_post ' ), 1 );
34
+
31
35
add_action ( 'comment_post ' , array ( static ::class, 'comment_post ' ) );
32
36
33
37
// remote delete posts
34
- add_action ( 'trashed_post ' , array ( static ::class, 'trash_hook ' ) );
35
38
add_action ( 'webmention_delete ' , array ( static ::class, 'send_webmentions ' ) );
36
39
}
37
40
@@ -84,6 +87,33 @@ public static function trash_hook( $post_id ) {
84
87
wp_schedule_single_event ( time () + wp_rand ( 0 , 120 ), 'webmention_delete ' , array ( $ post_id ) );
85
88
}
86
89
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
+
87
117
/**
88
118
* Send Webmentions.
89
119
*
@@ -158,11 +188,11 @@ public static function send_webmention( $source, $target, $post_id = null ) {
158
188
* @return array|bool array of results or false if failed.
159
189
*/
160
190
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 );
163
192
164
- // remove `__trashed` from the url
165
- $ source = str_replace ( '__trashed ' , '' , $ source );
193
+ if ( ! $ source ) {
194
+ $ source = get_permalink ( $ post_id );
195
+ }
166
196
167
197
// get post
168
198
$ post = get_post ( $ post_id );
0 commit comments