| @@ -7,8 +7,9 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace Activitypub; |
| 9 | 9 | |
| 10 | 10 | use Activitypub\Collection\Actors; |
| 11 | +use Activitypub\Collection\Posts; | |
| 11 | 12 | |
| 12 | 13 | /** |
| 13 | 14 | * Mailer Class. |
| 14 | 15 | */ |
| @@ -58,9 +59,9 @@ | ||
| 58 | 59 | |
| 59 | 60 | $post = \get_post( $comment->comment_post_ID ); |
| 60 | 61 | |
| 61 | 62 | /* translators: 1: Blog name, 2: Like or Repost, 3: 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 ) ); | |
| 63 | + return \sprintf( \esc_html__( '[%1$s] %2$s: %3$s', 'activitypub' ), \esc_html( get_option( 'blogname' ) ), \esc_html( $singular ), \esc_html( $post->post_title ) ); | |
| 63 | 64 | } |
| 64 | 65 | |
| 65 | 66 | /** |
| 66 | 67 | * Filter the notification text for Like and Announce notifications. |
| @@ -100,9 +101,9 @@ | ||
| 100 | 101 | // Check if this is a reaction to a post or a comment. |
| 101 | 102 | if ( 0 === (int) $comment->comment_parent ) { |
| 102 | 103 | $notify_message = \sprintf( |
| 103 | 104 | /* translators: 1: Comment type, 2: Post title */ |
| 104 | - \html_entity_decode( \esc_html__( 'New %1$s on your post “%2$s”.', 'activitypub' ) ), | |
| 105 | + \html_entity_decode( esc_html__( 'New %1$s on your post “%2$s”.', 'activitypub' ) ), | |
| 105 | 106 | \esc_html( $comment_type['singular'] ), |
| 106 | 107 | \esc_html( $post->post_title ) |
| 107 | 108 | ) . PHP_EOL . PHP_EOL; |
| 108 | 109 | |
| @@ -109,9 +110,9 @@ | ||
| 109 | 110 | } else { |
| 110 | 111 | $parent_comment = \get_comment( $comment->comment_parent ); |
| 111 | 112 | $notify_message = \sprintf( |
| 112 | 113 | /* translators: 1: Comment type, 2: Post title, 3: Parent comment author */ |
| 113 | - \html_entity_decode( \esc_html__( 'New %1$s on your post “%2$s” in reply to %3$s’s comment.', 'activitypub' ) ), | |
| 114 | + \html_entity_decode( esc_html__( 'New %1$s on your post “%2$s” in reply to %3$s’s comment.', 'activitypub' ) ), | |
| 114 | 115 | \esc_html( $comment_type['singular'] ), |
| 115 | 116 | \esc_html( $post->post_title ), |
| 116 | 117 | \esc_html( $parent_comment->comment_author ) |
| 117 | 118 | ) . PHP_EOL . PHP_EOL; |
| @@ -119,21 +120,9 @@ | ||
| 119 | 120 | |
| 120 | 121 | /* translators: 1: Website name, 2: Website IP address, 3: Website hostname. */ |
| 121 | 122 | $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"; |
| 122 | 123 | /* translators: Reaction author URL. */ |
| 123 | - $notify_message .= \sprintf( \esc_html__( 'URL: %s', 'activitypub' ), \esc_url( $comment->comment_author_url ) ) . "\r\n"; | |
| 124 | - | |
| 125 | - // For quotes, link to the quoting post itself so the author can review and respond. | |
| 126 | - if ( 'quote' === $comment->comment_type ) { | |
| 127 | - $quote_url = Comment::get_source_url( $comment->comment_ID ); | |
| 128 | - | |
| 129 | - if ( $quote_url ) { | |
| 130 | - /* translators: Quoting post URL. */ | |
| 131 | - $notify_message .= \sprintf( \esc_html__( 'Quoting post: %s', 'activitypub' ), \esc_url( $quote_url ) ) . "\r\n"; | |
| 132 | - } | |
| 133 | - } | |
| 134 | - | |
| 135 | - $notify_message .= "\r\n"; | |
| 124 | + $notify_message .= \sprintf( \esc_html__( 'URL: %s', 'activitypub' ), \esc_url( $comment->comment_author_url ) ) . "\r\n\r\n"; | |
| 136 | 125 | /* translators: Comment type label */ |
| 137 | 126 | $notify_message .= \sprintf( \esc_html__( 'You can see all %s on this post here:', 'activitypub' ), \esc_html( $comment_type['label'] ) ) . "\r\n"; |
| 138 | 127 | $notify_message .= \get_permalink( $comment->comment_post_ID ) . '#' . \esc_attr( $comment_type['type'] ) . "\r\n\r\n"; |
| 139 | 128 | |
| @@ -155,8 +144,13 @@ | ||
| 155 | 144 | |
| 156 | 145 | // Extract the user ID (follows are always for a single user). |
| 157 | 146 | $user_id = \is_array( $user_ids ) ? \reset( $user_ids ) : $user_ids; |
| 158 | 147 | |
| 148 | + // Do not send notifications to the Application user. | |
| 149 | + if ( Actors::APPLICATION_USER_ID === $user_id ) { | |
| 150 | + return; | |
| 151 | + } | |
| 152 | + | |
| 159 | 153 | if ( $user_id > Actors::BLOG_USER_ID ) { |
| 160 | 154 | if ( ! \get_user_option( 'activitypub_mailer_new_follower', $user_id ) ) { |
| 161 | 155 | return; |
| 162 | 156 | } |
| @@ -178,17 +172,9 @@ | ||
| 178 | 172 | } |
| 179 | 173 | |
| 180 | 174 | $actor = self::normalize_actor( $actor ); |
| 181 | 175 | |
| 182 | - // Replace emoji in actor name and summary. | |
| 183 | - if ( ! empty( $actor['name'] ) ) { | |
| 184 | - $actor['name'] = Emoji::replace_for_actor( $actor['name'], $actor['url'] ); | |
| 185 | - } | |
| 186 | - if ( ! empty( $actor['summary'] ) ) { | |
| 187 | - $actor['summary'] = Emoji::replace_for_actor( $actor['summary'], $actor['url'] ); | |
| 188 | - } | |
| 189 | - | |
| 190 | - $template_args = \array_merge( | |
| 176 | + $template_args = array_merge( | |
| 191 | 177 | $actor, |
| 192 | 178 | array( |
| 193 | 179 | 'admin_url' => $admin_url, |
| 194 | 180 | 'user_id' => $user_id, |
| @@ -204,9 +190,9 @@ | ||
| 204 | 190 | if ( empty( $actor[ $field ] ) ) { |
| 205 | 191 | continue; |
| 206 | 192 | } |
| 207 | 193 | |
| 208 | - $result = Http::get( $actor[ $field ], array(), true ); | |
| 194 | + $result = Http::get( $actor[ $field ], true ); | |
| 209 | 195 | if ( 200 === \wp_remote_retrieve_response_code( $result ) ) { |
| 210 | 196 | $body = \json_decode( \wp_remote_retrieve_body( $result ), true ); |
| 211 | 197 | if ( isset( $body['totalItems'] ) ) { |
| 212 | 198 | $template_args['stats'][ $field ] = $body['totalItems']; |
| @@ -220,9 +206,9 @@ | ||
| 220 | 206 | \ob_start(); |
| 221 | 207 | \load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/emails/new-follower.php', false, $template_args ); |
| 222 | 208 | $html_message = \ob_get_clean(); |
| 223 | 209 | |
| 224 | - $alt_function = static function ( $mailer ) use ( $actor, $admin_url ) { | |
| 210 | + $alt_function = function ( $mailer ) use ( $actor, $admin_url ) { | |
| 225 | 211 | /* translators: 1: Follower name */ |
| 226 | 212 | $message = \sprintf( \__( 'New Follower: %1$s.', 'activitypub' ), $actor['name'] ) . "\r\n\r\n"; |
| 227 | 213 | /* translators: Follower URL */ |
| 228 | 214 | $message .= \sprintf( \__( 'URL: %s', 'activitypub' ), \esc_url( $actor['url'] ) ) . "\r\n\r\n"; |
| @@ -308,12 +294,12 @@ | ||
| 308 | 294 | \ob_start(); |
| 309 | 295 | \load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/emails/new-dm.php', false, $template_args ); |
| 310 | 296 | $html_message = \ob_get_clean(); |
| 311 | 297 | |
| 312 | - $alt_function = static function ( $mailer ) use ( $actor, $activity ) { | |
| 298 | + $alt_function = function ( $mailer ) use ( $actor, $activity ) { | |
| 313 | 299 | $content = \html_entity_decode( |
| 314 | 300 | \wp_strip_all_tags( |
| 315 | - \str_replace( '</p>', PHP_EOL . PHP_EOL, $activity['object']['content'] ) | |
| 301 | + str_replace( '</p>', PHP_EOL . PHP_EOL, $activity['object']['content'] ) | |
| 316 | 302 | ), |
| 317 | 303 | ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 |
| 318 | 304 | ); |
| 319 | 305 | |
| @@ -351,10 +337,10 @@ | ||
| 351 | 337 | return; |
| 352 | 338 | } |
| 353 | 339 | |
| 354 | 340 | $recipients = array(); |
| 355 | - $mentions = \wp_list_filter( (array) $activity['object']['tag'], array( 'type' => 'Mention' ) ); | |
| 356 | - $mentions = \array_map( '\Activitypub\object_to_uri', $mentions ); | |
| 341 | + $mentions = wp_list_filter( (array) $activity['object']['tag'], array( 'type' => 'Mention' ) ); | |
| 342 | + $mentions = array_map( '\Activitypub\object_to_uri', $mentions ); | |
| 357 | 343 | foreach ( (array) $user_ids as $user_id ) { |
| 358 | 344 | $actor = Actors::get_by_id( $user_id ); |
| 359 | 345 | if ( \is_wp_error( $actor ) ) { |
| 360 | 346 | continue; |
| @@ -408,12 +394,12 @@ | ||
| 408 | 394 | \ob_start(); |
| 409 | 395 | \load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/emails/new-mention.php', false, $template_args ); |
| 410 | 396 | $html_message = \ob_get_clean(); |
| 411 | 397 | |
| 412 | - $alt_function = static function ( $mailer ) use ( $actor, $activity ) { | |
| 398 | + $alt_function = function ( $mailer ) use ( $actor, $activity ) { | |
| 413 | 399 | $content = \html_entity_decode( |
| 414 | 400 | \wp_strip_all_tags( |
| 415 | - \str_replace( '</p>', PHP_EOL . PHP_EOL, $activity['object']['content'] ) | |
| 401 | + str_replace( '</p>', PHP_EOL . PHP_EOL, $activity['object']['content'] ) | |
| 416 | 402 | ), |
| 417 | 403 | ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 |
| 418 | 404 | ); |
| 419 | 405 | |
| @@ -434,68 +420,8 @@ | ||
| 434 | 420 | } |
| 435 | 421 | } |
| 436 | 422 | |
| 437 | 423 | /** |
| 438 | - * Send a templated email to a user. | |
| 439 | - * | |
| 440 | - * @param int $user_id The user ID (or BLOG_USER_ID for blog actor). | |
| 441 | - * @param string $subject The email subject. | |
| 442 | - * @param string $template The template name (without path/extension). | |
| 443 | - * @param array $args Template arguments. | |
| 444 | - * @param string $alt_body Optional plain text alternative. Auto-generated from HTML if empty. | |
| 445 | - * | |
| 446 | - * @return bool True if email was sent, false otherwise. | |
| 447 | - */ | |
| 448 | - public static function send( $user_id, $subject, $template, $args = array(), $alt_body = '' ) { | |
| 449 | - // Get the recipient email address. | |
| 450 | - if ( $user_id > Actors::BLOG_USER_ID ) { | |
| 451 | - $user = \get_userdata( $user_id ); | |
| 452 | - if ( ! $user || empty( $user->user_email ) ) { | |
| 453 | - return false; | |
| 454 | - } | |
| 455 | - $email = $user->user_email; | |
| 456 | - } else { | |
| 457 | - $email = \get_option( 'admin_email' ); | |
| 458 | - } | |
| 459 | - | |
| 460 | - // Load the HTML template. | |
| 461 | - $template_file = ACTIVITYPUB_PLUGIN_DIR . 'templates/emails/' . \sanitize_file_name( $template ) . '.php'; | |
| 462 | - | |
| 463 | - /** | |
| 464 | - * Filter the email template file path. | |
| 465 | - * | |
| 466 | - * @param string $template_file The template file path. | |
| 467 | - * @param string $template The template name. | |
| 468 | - * @param int $user_id The user ID. | |
| 469 | - * @param array $args Template arguments. | |
| 470 | - */ | |
| 471 | - $template_file = \apply_filters( 'activitypub_email_template', $template_file, $template, $user_id, $args ); | |
| 472 | - | |
| 473 | - if ( ! \file_exists( $template_file ) ) { | |
| 474 | - return false; | |
| 475 | - } | |
| 476 | - | |
| 477 | - \ob_start(); | |
| 478 | - \load_template( $template_file, false, $args ); | |
| 479 | - $html_message = \ob_get_clean(); | |
| 480 | - | |
| 481 | - // Build plain text alternative from HTML if not provided. | |
| 482 | - if ( empty( $alt_body ) ) { | |
| 483 | - $alt_body = \wp_strip_all_tags( $html_message ); | |
| 484 | - } | |
| 485 | - $alt_function = static function ( $mailer ) use ( $alt_body ) { | |
| 486 | - $mailer->{'AltBody'} = $alt_body; | |
| 487 | - }; | |
| 488 | - \add_action( 'phpmailer_init', $alt_function ); | |
| 489 | - | |
| 490 | - $result = \wp_mail( $email, $subject, $html_message, array( 'Content-type: text/html' ) ); | |
| 491 | - | |
| 492 | - \remove_action( 'phpmailer_init', $alt_function ); | |
| 493 | - | |
| 494 | - return $result; | |
| 495 | - } | |
| 496 | - | |
| 497 | - /** | |
| 498 | 424 | * Apply defaults to the actor object. |
| 499 | 425 | * |
| 500 | 426 | * Ensure that the actor object has a name, url, and webfinger. |
| 501 | 427 | * |
| @@ -536,8 +462,13 @@ | ||
| 536 | 462 | if ( ! $maybe_notify ) { |
| 537 | 463 | return $maybe_notify; |
| 538 | 464 | } |
| 539 | 465 | |
| 466 | + // Only prevent if the create_posts option is enabled. | |
| 467 | + if ( '1' !== \get_option( 'activitypub_create_posts', false ) ) { | |
| 468 | + return $maybe_notify; | |
| 469 | + } | |
| 470 | + | |
| 540 | 471 | $comment = \get_comment( $comment_id ); |
| 541 | 472 | if ( ! $comment ) { |
| 542 | 473 | return $maybe_notify; |
| 543 | 474 | } |
| @@ -547,9 +478,9 @@ | ||
| 547 | 478 | return $maybe_notify; |
| 548 | 479 | } |
| 549 | 480 | |
| 550 | 481 | // Prevent notifications for comments on ap_post. |
| 551 | - if ( is_ap_post( $post ) ) { | |
| 482 | + if ( Posts::POST_TYPE === $post->post_type ) { | |
| 552 | 483 | return false; |
| 553 | 484 | } |
| 554 | 485 | |
| 555 | 486 | return $maybe_notify; |