| @@ -7,9 +7,8 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace Activitypub; |
| 9 | 9 | |
| 10 | 10 | use Activitypub\Collection\Actors; |
| 11 | -use Activitypub\Comment; | |
| 12 | 11 | |
| 13 | 12 | /** |
| 14 | 13 | * Mailer Class. |
| 15 | 14 | */ |
| @@ -26,9 +25,8 @@ | ||
| 26 | 25 | \add_action( 'activitypub_inbox_create', array( self::class, 'direct_message' ), 10, 2 ); |
| 27 | 26 | \add_action( 'activitypub_inbox_create', array( self::class, 'mention' ), 20, 2 ); /** After @see \Activitypub\Handler\Create::handle_create() */ |
| 28 | 27 | |
| 29 | 28 | \add_filter( 'notify_post_author', array( self::class, 'maybe_prevent_comment_notification' ), 10, 2 ); |
| 30 | - \add_filter( 'notify_post_author', array( self::class, 'maybe_prevent_reaction_notification' ), 10, 2 ); | |
| 31 | 29 | \add_filter( 'notify_moderator', array( self::class, 'maybe_prevent_comment_notification' ), 10, 2 ); |
| 32 | 30 | } |
| 33 | 31 | |
| 34 | 32 | /** |
| @@ -60,9 +58,9 @@ | ||
| 60 | 58 | |
| 61 | 59 | $post = \get_post( $comment->comment_post_ID ); |
| 62 | 60 | |
| 63 | 61 | /* translators: 1: Blog name, 2: Like or Repost, 3: Post title */ |
| 64 | - return \sprintf( \esc_html__( '[%1$s] %2$s: %3$s', 'activitypub' ), \esc_html( \get_option( 'blogname' ) ), \esc_html( $singular ), \esc_html( $post->post_title ) ); | |
| 62 | + return \sprintf( \esc_html__( '[%1$s] %2$s: %3$s', 'activitypub' ), \esc_html( get_option( 'blogname' ) ), \esc_html( $singular ), \esc_html( $post->post_title ) ); | |
| 65 | 63 | } |
| 66 | 64 | |
| 67 | 65 | /** |
| 68 | 66 | * Filter the notification text for Like and Announce notifications. |
| @@ -102,9 +100,9 @@ | ||
| 102 | 100 | // Check if this is a reaction to a post or a comment. |
| 103 | 101 | if ( 0 === (int) $comment->comment_parent ) { |
| 104 | 102 | $notify_message = \sprintf( |
| 105 | 103 | /* translators: 1: Comment type, 2: Post title */ |
| 106 | - \html_entity_decode( \esc_html__( 'New %1$s on your post “%2$s”.', 'activitypub' ) ), | |
| 104 | + \html_entity_decode( esc_html__( 'New %1$s on your post “%2$s”.', 'activitypub' ) ), | |
| 107 | 105 | \esc_html( $comment_type['singular'] ), |
| 108 | 106 | \esc_html( $post->post_title ) |
| 109 | 107 | ) . PHP_EOL . PHP_EOL; |
| 110 | 108 | |
| @@ -111,9 +109,9 @@ | ||
| 111 | 109 | } else { |
| 112 | 110 | $parent_comment = \get_comment( $comment->comment_parent ); |
| 113 | 111 | $notify_message = \sprintf( |
| 114 | 112 | /* translators: 1: Comment type, 2: Post title, 3: Parent comment author */ |
| 115 | - \html_entity_decode( \esc_html__( 'New %1$s on your post “%2$s” in reply to %3$s’s comment.', 'activitypub' ) ), | |
| 113 | + \html_entity_decode( esc_html__( 'New %1$s on your post “%2$s” in reply to %3$s’s comment.', 'activitypub' ) ), | |
| 116 | 114 | \esc_html( $comment_type['singular'] ), |
| 117 | 115 | \esc_html( $post->post_title ), |
| 118 | 116 | \esc_html( $parent_comment->comment_author ) |
| 119 | 117 | ) . PHP_EOL . PHP_EOL; |
| @@ -121,21 +119,9 @@ | ||
| 121 | 119 | |
| 122 | 120 | /* translators: 1: Website name, 2: Website IP address, 3: Website hostname. */ |
| 123 | 121 | $notify_message .= \sprintf( \esc_html__( 'From: %1$s (IP address: %2$s, %3$s)', 'activitypub' ), \esc_html( $comment->comment_author ), \esc_html( $comment->comment_author_IP ), \esc_html( $comment_author_domain ) ) . "\r\n"; |
| 124 | 122 | /* translators: Reaction author URL. */ |
| 125 | - $notify_message .= \sprintf( \esc_html__( 'URL: %s', 'activitypub' ), \esc_url( $comment->comment_author_url ) ) . "\r\n"; | |
| 126 | - | |
| 127 | - // For quotes, link to the quoting post itself so the author can review and respond. | |
| 128 | - if ( 'quote' === $comment->comment_type ) { | |
| 129 | - $quote_url = Comment::get_source_url( $comment->comment_ID ); | |
| 130 | - | |
| 131 | - if ( $quote_url ) { | |
| 132 | - /* translators: Quoting post URL. */ | |
| 133 | - $notify_message .= \sprintf( \esc_html__( 'Quoting post: %s', 'activitypub' ), \esc_url( $quote_url ) ) . "\r\n"; | |
| 134 | - } | |
| 135 | - } | |
| 136 | - | |
| 137 | - $notify_message .= "\r\n"; | |
| 123 | + $notify_message .= \sprintf( \esc_html__( 'URL: %s', 'activitypub' ), \esc_url( $comment->comment_author_url ) ) . "\r\n\r\n"; | |
| 138 | 124 | /* translators: Comment type label */ |
| 139 | 125 | $notify_message .= \sprintf( \esc_html__( 'You can see all %s on this post here:', 'activitypub' ), \esc_html( $comment_type['label'] ) ) . "\r\n"; |
| 140 | 126 | $notify_message .= \get_permalink( $comment->comment_post_ID ) . '#' . \esc_attr( $comment_type['type'] ) . "\r\n\r\n"; |
| 141 | 127 | |
| @@ -157,8 +143,13 @@ | ||
| 157 | 143 | |
| 158 | 144 | // Extract the user ID (follows are always for a single user). |
| 159 | 145 | $user_id = \is_array( $user_ids ) ? \reset( $user_ids ) : $user_ids; |
| 160 | 146 | |
| 147 | + // Do not send notifications to the Application user. | |
| 148 | + if ( Actors::APPLICATION_USER_ID === $user_id ) { | |
| 149 | + return; | |
| 150 | + } | |
| 151 | + | |
| 161 | 152 | if ( $user_id > Actors::BLOG_USER_ID ) { |
| 162 | 153 | if ( ! \get_user_option( 'activitypub_mailer_new_follower', $user_id ) ) { |
| 163 | 154 | return; |
| 164 | 155 | } |
| @@ -188,9 +179,9 @@ | ||
| 188 | 179 | if ( ! empty( $actor['summary'] ) ) { |
| 189 | 180 | $actor['summary'] = Emoji::replace_for_actor( $actor['summary'], $actor['url'] ); |
| 190 | 181 | } |
| 191 | 182 | |
| 192 | - $template_args = \array_merge( | |
| 183 | + $template_args = array_merge( | |
| 193 | 184 | $actor, |
| 194 | 185 | array( |
| 195 | 186 | 'admin_url' => $admin_url, |
| 196 | 187 | 'user_id' => $user_id, |
| @@ -313,9 +304,9 @@ | ||
| 313 | 304 | |
| 314 | 305 | $alt_function = static function ( $mailer ) use ( $actor, $activity ) { |
| 315 | 306 | $content = \html_entity_decode( |
| 316 | 307 | \wp_strip_all_tags( |
| 317 | - \str_replace( '</p>', PHP_EOL . PHP_EOL, $activity['object']['content'] ) | |
| 308 | + str_replace( '</p>', PHP_EOL . PHP_EOL, $activity['object']['content'] ) | |
| 318 | 309 | ), |
| 319 | 310 | ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 |
| 320 | 311 | ); |
| 321 | 312 | |
| @@ -353,10 +344,10 @@ | ||
| 353 | 344 | return; |
| 354 | 345 | } |
| 355 | 346 | |
| 356 | 347 | $recipients = array(); |
| 357 | - $mentions = \wp_list_filter( (array) $activity['object']['tag'], array( 'type' => 'Mention' ) ); | |
| 358 | - $mentions = \array_map( '\Activitypub\object_to_uri', $mentions ); | |
| 348 | + $mentions = wp_list_filter( (array) $activity['object']['tag'], array( 'type' => 'Mention' ) ); | |
| 349 | + $mentions = array_map( '\Activitypub\object_to_uri', $mentions ); | |
| 359 | 350 | foreach ( (array) $user_ids as $user_id ) { |
| 360 | 351 | $actor = Actors::get_by_id( $user_id ); |
| 361 | 352 | if ( \is_wp_error( $actor ) ) { |
| 362 | 353 | continue; |
| @@ -413,9 +404,9 @@ | ||
| 413 | 404 | |
| 414 | 405 | $alt_function = static function ( $mailer ) use ( $actor, $activity ) { |
| 415 | 406 | $content = \html_entity_decode( |
| 416 | 407 | \wp_strip_all_tags( |
| 417 | - \str_replace( '</p>', PHP_EOL . PHP_EOL, $activity['object']['content'] ) | |
| 408 | + str_replace( '</p>', PHP_EOL . PHP_EOL, $activity['object']['content'] ) | |
| 418 | 409 | ), |
| 419 | 410 | ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 |
| 420 | 411 | ); |
| 421 | 412 | |
| @@ -436,68 +427,8 @@ | ||
| 436 | 427 | } |
| 437 | 428 | } |
| 438 | 429 | |
| 439 | 430 | /** |
| 440 | - * Send a templated email to a user. | |
| 441 | - * | |
| 442 | - * @param int $user_id The user ID (or BLOG_USER_ID for blog actor). | |
| 443 | - * @param string $subject The email subject. | |
| 444 | - * @param string $template The template name (without path/extension). | |
| 445 | - * @param array $args Template arguments. | |
| 446 | - * @param string $alt_body Optional plain text alternative. Auto-generated from HTML if empty. | |
| 447 | - * | |
| 448 | - * @return bool True if email was sent, false otherwise. | |
| 449 | - */ | |
| 450 | - public static function send( $user_id, $subject, $template, $args = array(), $alt_body = '' ) { | |
| 451 | - // Get the recipient email address. | |
| 452 | - if ( $user_id > Actors::BLOG_USER_ID ) { | |
| 453 | - $user = \get_userdata( $user_id ); | |
| 454 | - if ( ! $user || empty( $user->user_email ) ) { | |
| 455 | - return false; | |
| 456 | - } | |
| 457 | - $email = $user->user_email; | |
| 458 | - } else { | |
| 459 | - $email = \get_option( 'admin_email' ); | |
| 460 | - } | |
| 461 | - | |
| 462 | - // Load the HTML template. | |
| 463 | - $template_file = ACTIVITYPUB_PLUGIN_DIR . 'templates/emails/' . \sanitize_file_name( $template ) . '.php'; | |
| 464 | - | |
| 465 | - /** | |
| 466 | - * Filter the email template file path. | |
| 467 | - * | |
| 468 | - * @param string $template_file The template file path. | |
| 469 | - * @param string $template The template name. | |
| 470 | - * @param int $user_id The user ID. | |
| 471 | - * @param array $args Template arguments. | |
| 472 | - */ | |
| 473 | - $template_file = \apply_filters( 'activitypub_email_template', $template_file, $template, $user_id, $args ); | |
| 474 | - | |
| 475 | - if ( ! \file_exists( $template_file ) ) { | |
| 476 | - return false; | |
| 477 | - } | |
| 478 | - | |
| 479 | - \ob_start(); | |
| 480 | - \load_template( $template_file, false, $args ); | |
| 481 | - $html_message = \ob_get_clean(); | |
| 482 | - | |
| 483 | - // Build plain text alternative from HTML if not provided. | |
| 484 | - if ( empty( $alt_body ) ) { | |
| 485 | - $alt_body = \wp_strip_all_tags( $html_message ); | |
| 486 | - } | |
| 487 | - $alt_function = static function ( $mailer ) use ( $alt_body ) { | |
| 488 | - $mailer->{'AltBody'} = $alt_body; | |
| 489 | - }; | |
| 490 | - \add_action( 'phpmailer_init', $alt_function ); | |
| 491 | - | |
| 492 | - $result = \wp_mail( $email, $subject, $html_message, array( 'Content-type: text/html' ) ); | |
| 493 | - | |
| 494 | - \remove_action( 'phpmailer_init', $alt_function ); | |
| 495 | - | |
| 496 | - return $result; | |
| 497 | - } | |
| 498 | - | |
| 499 | - /** | |
| 500 | 431 | * Apply defaults to the actor object. |
| 501 | 432 | * |
| 502 | 433 | * Ensure that the actor object has a name, url, and webfinger. |
| 503 | 434 | * |
| @@ -554,40 +485,6 @@ | ||
| 554 | 485 | return false; |
| 555 | 486 | } |
| 556 | 487 | |
| 557 | 488 | return $maybe_notify; |
| 558 | - } | |
| 559 | - | |
| 560 | - /** | |
| 561 | - * Let the post author mute email about reactions to their post. | |
| 562 | - * | |
| 563 | - * Likes, reposts, and quotes are stored as comments, so WordPress emails the post author about | |
| 564 | - * them like any other comment. This is hooked on `notify_post_author` only, so it never affects | |
| 565 | - * the moderator notification, and it targets the plugin's own reaction comment types so pingbacks, | |
| 566 | - * trackbacks, and plain replies keep notifying as usual. The preference defaults to on. | |
| 567 | - * | |
| 568 | - * @since 9.3.0 | |
| 569 | - * | |
| 570 | - * @param bool $maybe_notify Whether to send the notification. | |
| 571 | - * @param int $comment_id The comment ID. | |
| 572 | - * | |
| 573 | - * @return bool Whether to send the notification. | |
| 574 | - */ | |
| 575 | - public static function maybe_prevent_reaction_notification( $maybe_notify, $comment_id ) { | |
| 576 | - // If already disabled, respect that. | |
| 577 | - if ( ! $maybe_notify ) { | |
| 578 | - return $maybe_notify; | |
| 579 | - } | |
| 580 | - | |
| 581 | - $comment = \get_comment( $comment_id ); | |
| 582 | - if ( ! $comment || ! \in_array( \get_comment_type( $comment ), Comment::get_comment_type_slugs(), true ) ) { | |
| 583 | - return $maybe_notify; | |
| 584 | - } | |
| 585 | - | |
| 586 | - $post = \get_post( $comment->comment_post_ID ); | |
| 587 | - if ( ! $post ) { | |
| 588 | - return $maybe_notify; | |
| 589 | - } | |
| 590 | - | |
| 591 | - return (bool) \get_user_option( 'activitypub_mailer_new_reaction', $post->post_author ); | |
| 592 | 489 | } |
| 593 | 490 | } |