|
4 | 4 | use WP_Comment; |
5 | 5 | use WP_Comment_Query; |
6 | 6 |
|
| 7 | +use Activitypub\Webfinger; |
7 | 8 | use Activitypub\Comment as Comment_Utils; |
8 | 9 | use Activitypub\Model\Blog_User; |
9 | 10 | use Activitypub\Collection\Users; |
@@ -178,31 +179,7 @@ protected function get_cc() { |
178 | 179 | } |
179 | 180 | } |
180 | 181 |
|
181 | | - $comment_query = new WP_Comment_Query( |
182 | | - array( |
183 | | - 'post_id' => $this->wp_object->comment_post_ID, |
184 | | - // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
185 | | - 'meta_query' => array( |
186 | | - array( |
187 | | - 'key' => 'source_id', |
188 | | - 'compare' => 'EXISTS', |
189 | | - ), |
190 | | - ), |
191 | | - ) |
192 | | - ); |
193 | | - |
194 | | - if ( $comment_query->comments ) { |
195 | | - foreach ( $comment_query->comments as $comment ) { |
196 | | - if ( empty( $comment->comment_author_url ) ) { |
197 | | - continue; |
198 | | - } |
199 | | - $cc[] = \esc_url( $comment->comment_author_url ); |
200 | | - } |
201 | | - } |
202 | | - |
203 | | - $cc = \array_unique( $cc ); |
204 | | - |
205 | | - return $cc; |
| 182 | + return array_unique( $cc ); |
206 | 183 | } |
207 | 184 |
|
208 | 185 | /** |
@@ -236,9 +213,64 @@ protected function get_tag() { |
236 | 213 | * @return array The list of @-Mentions. |
237 | 214 | */ |
238 | 215 | protected function get_mentions() { |
| 216 | + \add_filter( 'activitypub_extract_mentions', array( $this, 'extract_reply_context' ) ); |
| 217 | + |
239 | 218 | return apply_filters( 'activitypub_extract_mentions', array(), $this->wp_object->comment_content, $this->wp_object ); |
240 | 219 | } |
241 | 220 |
|
| 221 | + /** |
| 222 | + * Collect all other Users that participated in this comment-thread |
| 223 | + * to send them a notification about the new reply. |
| 224 | + * |
| 225 | + * @param array $mentions The already mentioned ActivityPub users |
| 226 | + * |
| 227 | + * @return array The list of all Repliers. |
| 228 | + */ |
| 229 | + public function extract_reply_context( $mentions ) { |
| 230 | + // Check if `$this->wp_object` is a WP_Comment |
| 231 | + if ( 'WP_Comment' !== get_class( $this->wp_object ) ) { |
| 232 | + return $mentions; |
| 233 | + } |
| 234 | + |
| 235 | + $comment_query = new WP_Comment_Query( |
| 236 | + array( |
| 237 | + 'post_id' => $this->wp_object->comment_post_ID, |
| 238 | + // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 239 | + 'meta_query' => array( |
| 240 | + array( |
| 241 | + 'key' => 'protocol', |
| 242 | + 'value' => 'activitypub', |
| 243 | + ), |
| 244 | + ), |
| 245 | + ) |
| 246 | + ); |
| 247 | + |
| 248 | + if ( $comment_query->comments ) { |
| 249 | + foreach ( $comment_query->comments as $comment ) { |
| 250 | + $id = null; |
| 251 | + $comment_meta = \get_comment_meta( $comment->comment_ID ); |
| 252 | + |
| 253 | + if ( ! empty( $comment_meta['source_id'][0] ) ) { |
| 254 | + $id = \esc_url( $comment_meta['source_id'][0] ); |
| 255 | + } elseif ( ! empty( $comment_meta['source_url'][0] ) ) { |
| 256 | + $id = \esc_url( $comment_meta['source_url'][0] ); |
| 257 | + } elseif ( ! empty( $comment->comment_author_url ) ) { |
| 258 | + $id = $comment->comment_author_url; |
| 259 | + } |
| 260 | + |
| 261 | + if ( $id ) { |
| 262 | + $acct = Webfinger::uri_to_acct( $id ); |
| 263 | + if ( $acct && ! is_wp_error( $acct ) ) { |
| 264 | + $acct = str_replace( 'acct:', '@', $acct ); |
| 265 | + $mentions[ $acct ] = $id; |
| 266 | + } |
| 267 | + } |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + return $mentions; |
| 272 | + } |
| 273 | + |
242 | 274 | /** |
243 | 275 | * Returns the locale of the post. |
244 | 276 | * |
|
0 commit comments