Нет описания

functions.wp-notify.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. /** phpcs:disable Squiz.Commenting.FileComment.MissingPackageTag,Generic.Commenting.DocComment.MissingShort
  3. *
  4. * Declare two functions to handle notification emails to authors and moderators.
  5. *
  6. * These functions are hooked into filters to short circuit the regular flow and send the emails.
  7. * Code was copied from the original pluggable functions and slightly modified (modifications are commented).
  8. *
  9. * In the past, we used to overwrite the whole pluggable function, but we started using filters to avoid having
  10. * to check for Jetpack::is_active() too early in the load flow.
  11. */
  12. use Automattic\Jetpack\Connection\Manager as Connection_Manager;
  13. use Automattic\Jetpack\Redirect;
  14. // phpcs:disable WordPress.WP.I18n.MissingArgDomain --reason: WP Core string.
  15. /**
  16. * Short circuits the {@see `wp_notify_postauthor`} function via the `comment_notification_recipients` filter.
  17. *
  18. * Notify an author (and/or others) of a comment/trackback/pingback on a post.
  19. *
  20. * @since 5.8.0
  21. * @since 9.3.0 Switched from pluggable function to filter callback
  22. *
  23. * @param array $emails List of recipients.
  24. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
  25. * @return array Empty array to shortcircuit wp_notify_postauthor execution. $emails if we want to disable the filter.
  26. */
  27. function jetpack_notify_postauthor( $emails, $comment_id ) {
  28. // Don't do anything if Jetpack isn't connected.
  29. if ( ! Jetpack::is_connection_ready() || empty( $emails ) ) {
  30. return $emails;
  31. }
  32. // Original function modified: Code before the comment_notification_recipients filter removed.
  33. $comment = get_comment( $comment_id );
  34. $post = get_post( $comment->comment_post_ID );
  35. $author = get_userdata( $post->post_author );
  36. // Facilitate unsetting below without knowing the keys.
  37. $emails = array_flip( $emails );
  38. /** This filter is documented in core/src/wp-includes/pluggable.php */
  39. $notify_author = apply_filters( 'comment_notification_notify_author', false, $comment->comment_ID );
  40. // The comment was left by the author.
  41. if ( $author && ! $notify_author && $comment->user_id == $post->post_author ) {
  42. unset( $emails[ $author->user_email ] );
  43. }
  44. // The author moderated a comment on their own post.
  45. if ( $author && ! $notify_author && get_current_user_id() == $post->post_author ) {
  46. unset( $emails[ $author->user_email ] );
  47. }
  48. // The post author is no longer a member of the blog.
  49. if ( $author && ! $notify_author && ! user_can( $post->post_author, 'read_post', $post->ID ) ) {
  50. unset( $emails[ $author->user_email ] );
  51. }
  52. // If there's no email to send the comment to, bail, otherwise flip array back around for use below.
  53. if ( ! count( $emails ) ) {
  54. return array(); // Original function modified. Return empty array instead of false.
  55. } else {
  56. $emails = array_flip( $emails );
  57. }
  58. $switched_locale = switch_to_locale( get_locale() );
  59. $comment_author_domain = '';
  60. if ( WP_Http::is_ip_address( $comment->comment_author_IP ) ) {
  61. $comment_author_domain = gethostbyaddr( $comment->comment_author_IP );
  62. }
  63. // The blogname option is escaped with esc_html on the way into the database in sanitize_option
  64. // we want to reverse this for the plain text arena of emails.
  65. $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
  66. $comment_content = wp_specialchars_decode( $comment->comment_content );
  67. // Original function modified.
  68. $moderate_on_wpcom = ! in_array( false, array_map( 'jetpack_notify_is_user_connected_by_email', $emails ) );
  69. switch ( $comment->comment_type ) {
  70. case 'trackback':
  71. /* translators: 1: Post title */
  72. $notify_message = sprintf( __( 'New trackback on your post "%s"' ), $post->post_title ) . "\r\n";
  73. /* translators: 1: Trackback/pingback website name, 2: website IP address, 3: website hostname */
  74. $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  75. /* translators: %s: Site URL */
  76. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  77. /* translators: %s: Comment Content */
  78. $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
  79. $notify_message .= __( 'You can see all trackbacks on this post here:' ) . "\r\n";
  80. /* translators: 1: blog name, 2: post title */
  81. $subject = sprintf( __( '[%1$s] Trackback: "%2$s"' ), $blogname, $post->post_title );
  82. break;
  83. case 'pingback':
  84. /* translators: 1: Post title */
  85. $notify_message = sprintf( __( 'New pingback on your post "%s"' ), $post->post_title ) . "\r\n";
  86. /* translators: 1: Trackback/pingback website name, 2: website IP address, 3: website hostname */
  87. $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  88. /* translators: %s: Site URL */
  89. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  90. /* translators: %s: Comment Content */
  91. $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
  92. $notify_message .= __( 'You can see all pingbacks on this post here:' ) . "\r\n";
  93. /* translators: 1: blog name, 2: post title */
  94. $subject = sprintf( __( '[%1$s] Pingback: "%2$s"' ), $blogname, $post->post_title );
  95. break;
  96. default: // Comments.
  97. /* translators: 1: Post title */
  98. $notify_message = sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "\r\n";
  99. /* translators: 1: comment author, 2: comment author's IP address, 3: comment author's hostname */
  100. $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  101. /* translators: %s: Email address */
  102. $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n";
  103. /* translators: %s: Site URL */
  104. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  105. /* translators: %s: Comment Content */
  106. $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
  107. $notify_message .= __( 'You can see all comments on this post here:' ) . "\r\n";
  108. /* translators: 1: blog name, 2: post title */
  109. $subject = sprintf( __( '[%1$s] Comment: "%2$s"' ), $blogname, $post->post_title );
  110. break;
  111. }
  112. // Original function modified: Consider $moderate_on_wpcom when building $notify_message.
  113. $notify_message .= $moderate_on_wpcom
  114. ? Redirect::get_url(
  115. 'calypso-comments-all',
  116. array(
  117. 'path' => $comment->comment_post_ID,
  118. )
  119. ) . "/\r\n\r\n"
  120. : get_permalink( $comment->comment_post_ID ) . "#comments\r\n\r\n";
  121. /* translators: %s: URL */
  122. $notify_message .= sprintf( __( 'Permalink: %s' ), get_comment_link( $comment ) ) . "\r\n";
  123. $base_wpcom_edit_comment_url = Redirect::get_url(
  124. 'calypso-edit-comment',
  125. array(
  126. 'path' => $comment_id,
  127. 'query' => 'action=__action__', // __action__ will be replaced by the actual action.
  128. )
  129. );
  130. // Original function modified: Consider $moderate_on_wpcom when building $notify_message.
  131. if ( user_can( $post->post_author, 'edit_comment', $comment->comment_ID ) ) {
  132. if ( EMPTY_TRASH_DAYS ) {
  133. $notify_message .= sprintf(
  134. /* translators: Placeholder is the edit URL */
  135. __( 'Trash it: %s' ),
  136. $moderate_on_wpcom
  137. ? str_replace( '__action__', 'trash', $base_wpcom_edit_comment_url )
  138. : admin_url( "comment.php?action=trash&c={$comment->comment_ID}#wpbody-content" )
  139. ) . "\r\n";
  140. } else {
  141. $notify_message .= sprintf(
  142. /* translators: Placeholder is the edit URL */
  143. __( 'Delete it: %s' ),
  144. $moderate_on_wpcom
  145. ? str_replace( '__action__', 'delete', $base_wpcom_edit_comment_url )
  146. : admin_url( "comment.php?action=delete&c={$comment->comment_ID}#wpbody-content" )
  147. ) . "\r\n";
  148. }
  149. $notify_message .= sprintf(
  150. /* translators: Placeholder is the edit URL */
  151. __( 'Spam it: %s' ),
  152. $moderate_on_wpcom
  153. ? str_replace( '__action__', 'spam', $base_wpcom_edit_comment_url )
  154. : admin_url( "comment.php?action=spam&c={$comment->comment_ID}#wpbody-content" )
  155. ) . "\r\n";
  156. }
  157. $wp_email = 'wordpress@' . preg_replace( '#^www\.#', '', strtolower( $_SERVER['SERVER_NAME'] ) );
  158. if ( '' == $comment->comment_author ) {
  159. $from = "From: \"$blogname\" <$wp_email>";
  160. if ( '' != $comment->comment_author_email ) {
  161. $reply_to = "Reply-To: $comment->comment_author_email";
  162. }
  163. } else {
  164. $from = "From: \"$comment->comment_author\" <$wp_email>";
  165. if ( '' != $comment->comment_author_email ) {
  166. $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>";
  167. }
  168. }
  169. $message_headers = "$from\n"
  170. . 'Content-Type: text/plain; charset="' . get_option( 'blog_charset' ) . "\"\n";
  171. if ( isset( $reply_to ) ) {
  172. $message_headers .= $reply_to . "\n";
  173. }
  174. /** This filter is documented in core/src/wp-includes/pluggable.php */
  175. $notify_message = apply_filters( 'comment_notification_text', $notify_message, $comment->comment_ID );
  176. /** This filter is documented in core/src/wp-includes/pluggable.php */
  177. $subject = apply_filters( 'comment_notification_subject', $subject, $comment->comment_ID );
  178. /** This filter is documented in core/src/wp-includes/pluggable.php */
  179. $message_headers = apply_filters( 'comment_notification_headers', $message_headers, $comment->comment_ID );
  180. foreach ( $emails as $email ) {
  181. wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers );
  182. }
  183. if ( $switched_locale ) {
  184. restore_previous_locale();
  185. }
  186. return array();
  187. }
  188. /**
  189. * Short circuits the {@see `wp_notify_moderator`} function via the `notify_moderator` filter.
  190. *
  191. * Notifies the moderator of the site about a new comment that is awaiting approval.
  192. *
  193. * @since 5.8.0
  194. * @since 9.2.0 Switched from pluggable function to filter callback
  195. * @since 9.5.0 Updated the passing condition to call get_option( 'moderation_notify' ); directly.
  196. *
  197. * @global wpdb $wpdb WordPress database abstraction object.
  198. *
  199. * @param string $notify_moderator The value of the moderation_notify option OR if the comment is awaiting moderation.
  200. * @param int $comment_id Comment ID.
  201. * @return boolean Returns false to shortcircuit the execution of wp_notify_moderator
  202. */
  203. function jetpack_notify_moderator( $notify_moderator, $comment_id ) {
  204. /*
  205. * $notify_moderator is a tricky one. This filter is called in two places in Core. One is just to pass if a comment
  206. * is being held for moderation. See https://core.trac.wordpress.org/browser/tags/5.6/src/wp-includes/comment.php#L2296
  207. *
  208. * So we can't just assume that a true value here is what we need. The second time the filter is called, it checks
  209. * the option -- which is what we expected here. See https://core.trac.wordpress.org/browser/tags/5.6/src/wp-includes/pluggable.php#L1737
  210. *
  211. * It's possible another plugin would be filtering this value to true despite the option setting; however, since we're running at priority 1,
  212. * they can still do that. They'll just get the Core flow instead of this one.
  213. */
  214. // If Jetpack is not active, or if Notify moderators options is not set, let the default flow go on.
  215. if ( ! $notify_moderator || ! get_option( 'moderation_notify' ) || ! Jetpack::is_connection_ready() ) {
  216. return $notify_moderator;
  217. }
  218. // Original function modified: Removed code before the notify_moderator filter.
  219. global $wpdb;
  220. $comment = get_comment( $comment_id );
  221. $post = get_post( $comment->comment_post_ID );
  222. $user = get_userdata( $post->post_author );
  223. // Send to the administration and to the post author if the author can modify the comment.
  224. $emails = array( get_option( 'admin_email' ) );
  225. if ( $user && user_can( $user->ID, 'edit_comment', $comment_id ) && ! empty( $user->user_email ) ) {
  226. if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) {
  227. $emails[] = $user->user_email;
  228. }
  229. }
  230. $switched_locale = switch_to_locale( get_locale() );
  231. $comment_author_domain = '';
  232. if ( WP_Http::is_ip_address( $comment->comment_author_IP ) ) {
  233. $comment_author_domain = gethostbyaddr( $comment->comment_author_IP );
  234. }
  235. $comments_waiting = $wpdb->get_var( "SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'" );
  236. // The blogname option is escaped with esc_html on the way into the database in sanitize_option
  237. // we want to reverse this for the plain text arena of emails.
  238. $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
  239. $comment_content = wp_specialchars_decode( $comment->comment_content );
  240. switch ( $comment->comment_type ) {
  241. case 'trackback':
  242. /* translators: 1: Post title */
  243. $notify_message = sprintf( __( 'A new trackback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n";
  244. $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n";
  245. /* translators: 1: Trackback/pingback website name, 2: website IP address, 3: website hostname */
  246. $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  247. /* translators: 1: Trackback/pingback/comment author URL */
  248. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  249. $notify_message .= __( 'Trackback excerpt: ' ) . "\r\n" . $comment_content . "\r\n\r\n";
  250. break;
  251. case 'pingback':
  252. /* translators: 1: Post title */
  253. $notify_message = sprintf( __( 'A new pingback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n";
  254. $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n";
  255. /* translators: 1: Trackback/pingback website name, 2: website IP address, 3: website hostname */
  256. $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  257. /* translators: 1: Trackback/pingback/comment author URL */
  258. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  259. $notify_message .= __( 'Pingback excerpt: ' ) . "\r\n" . $comment_content . "\r\n\r\n";
  260. break;
  261. default: // Comments.
  262. /* translators: 1: Post title */
  263. $notify_message = sprintf( __( 'A new comment on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n";
  264. $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n";
  265. /* translators: 1: Comment author name, 2: comment author's IP address, 3: comment author's hostname */
  266. $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
  267. /* translators: 1: Comment author URL */
  268. $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n";
  269. /* translators: 1: Trackback/pingback/comment author URL */
  270. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
  271. /* translators: 1: Comment text */
  272. $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
  273. break;
  274. }
  275. /** This filter is documented in core/src/wp-includes/pluggable.php */
  276. $emails = apply_filters( 'comment_moderation_recipients', $emails, $comment_id );
  277. // Original function modified.
  278. $moderate_on_wpcom = ! in_array( false, array_map( 'jetpack_notify_is_user_connected_by_email', $emails ) );
  279. $base_wpcom_edit_comment_url = Redirect::get_url(
  280. 'calypso-edit-comment',
  281. array(
  282. 'path' => $comment_id,
  283. 'query' => 'action=__action__', // __action__ will be replaced by the actual action.
  284. )
  285. );
  286. // Original function modified: Consider $moderate_on_wpcom when building $notify_message.
  287. $notify_message .= sprintf(
  288. /* translators: Comment moderation. 1: Comment action URL */
  289. __( 'Approve it: %s' ),
  290. $moderate_on_wpcom
  291. ? str_replace( '__action__', 'approve', $base_wpcom_edit_comment_url )
  292. : admin_url( "comment.php?action=approve&c={$comment_id}#wpbody-content" )
  293. ) . "\r\n";
  294. if ( EMPTY_TRASH_DAYS ) {
  295. $notify_message .= sprintf(
  296. /* translators: Comment moderation. 1: Comment action URL */
  297. __( 'Trash it: %s' ),
  298. $moderate_on_wpcom
  299. ? str_replace( '__action__', 'trash', $base_wpcom_edit_comment_url )
  300. : admin_url( "comment.php?action=trash&c={$comment_id}#wpbody-content" )
  301. ) . "\r\n";
  302. } else {
  303. $notify_message .= sprintf(
  304. /* translators: Comment moderation. 1: Comment action URL */
  305. __( 'Delete it: %s' ),
  306. $moderate_on_wpcom
  307. ? str_replace( '__action__', 'delete', $base_wpcom_edit_comment_url )
  308. : admin_url( "comment.php?action=delete&c={$comment_id}#wpbody-content" )
  309. ) . "\r\n";
  310. }
  311. $notify_message .= sprintf(
  312. /* translators: Comment moderation. 1: Comment action URL */
  313. __( 'Spam it: %s' ),
  314. $moderate_on_wpcom
  315. ? str_replace( '__action__', 'spam', $base_wpcom_edit_comment_url )
  316. : admin_url( "comment.php?action=spam&c={$comment_id}#wpbody-content" )
  317. ) . "\r\n";
  318. $notify_message .= sprintf(
  319. /* translators: Comment moderation. 1: Number of comments awaiting approval */
  320. _n(
  321. 'Currently %s comment is waiting for approval. Please visit the moderation panel:',
  322. 'Currently %s comments are waiting for approval. Please visit the moderation panel:',
  323. $comments_waiting
  324. ),
  325. number_format_i18n( $comments_waiting )
  326. ) . "\r\n";
  327. $notify_message .= $moderate_on_wpcom
  328. ? Redirect::get_url( 'calypso-comments-pending' )
  329. : admin_url( 'edit-comments.php?comment_status=moderated#wpbody-content' ) . "\r\n";
  330. /* translators: Comment moderation notification email subject. 1: Site name, 2: Post title */
  331. $subject = sprintf( __( '[%1$s] Please moderate: "%2$s"' ), $blogname, $post->post_title );
  332. $message_headers = '';
  333. /** This filter is documented in core/src/wp-includes/pluggable.php */
  334. $notify_message = apply_filters( 'comment_moderation_text', $notify_message, $comment_id );
  335. /** This filter is documented in core/src/wp-includes/pluggable.php */
  336. $subject = apply_filters( 'comment_moderation_subject', $subject, $comment_id );
  337. /** This filter is documented in core/src/wp-includes/pluggable.php */
  338. $message_headers = apply_filters( 'comment_moderation_headers', $message_headers, $comment_id );
  339. foreach ( $emails as $email ) {
  340. wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers );
  341. }
  342. if ( $switched_locale ) {
  343. restore_previous_locale();
  344. }
  345. return false;
  346. }
  347. /**
  348. * Gets an user by email and verify if it's connected
  349. *
  350. * @param string $email The user email.
  351. * @return boolean
  352. */
  353. function jetpack_notify_is_user_connected_by_email( $email ) {
  354. $user = get_user_by( 'email', $email );
  355. return ( new Connection_Manager( 'jetpack' ) )->is_user_connected( $user->ID );
  356. }