| 1360 | /** |
| 1361 | * Sends emails for moderated forum posts to moderators |
| 1362 | * |
| 1363 | * @since 2.7.x bbPress (rXXX) |
| 1364 | * |
| 1365 | * @param int $post_id bbPress topic or reply ID |
| 1366 | * @return bool True on success, false on failure |
| 1367 | */ |
| 1368 | function bbp_notify_forum_moderators( $post_id = 0 ) { |
| 1369 | // Bail if importing |
| 1370 | if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
| 1371 | return false; |
| 1372 | } |
| 1373 | |
| 1374 | // If post isn't pending, bail |
| 1375 | if ( bbp_get_pending_status_id() !== get_post_status( $post_id ) ) { |
| 1376 | return false; |
| 1377 | } |
| 1378 | |
| 1379 | // Check if this is a bbPress topic or reply |
| 1380 | if ( ! bbp_is_topic( $post_id ) && ! bbp_is_reply( $post_id ) ) { |
| 1381 | return false; |
| 1382 | } |
| 1383 | |
| 1384 | // Determine bbPress type |
| 1385 | if ( bbp_is_reply( $post_id ) ) { |
| 1386 | $type = 'reply'; |
| 1387 | $forum_id = bbp_get_reply_forum_id( $post_id ); |
| 1388 | $author_name = bbp_get_reply_author_display_name( $post_id ); |
| 1389 | $content = bbp_get_reply_content( $post_id ); |
| 1390 | $title = bbp_get_reply_topic_title( $post_id ); |
| 1391 | $url = bbp_get_reply_url( $post_id ); |
| 1392 | } else { |
| 1393 | $type = 'topic'; |
| 1394 | $forum_id = bbp_get_topic_forum_id( $post_id ); |
| 1395 | $author_name = bbp_get_topic_author_display_name( $post_id ); |
| 1396 | $content = bbp_get_topic_content( $post_id ); |
| 1397 | $title = bbp_get_topic_title( $post_id ); |
| 1398 | $url = bbp_get_topic_permalink( $post_id ); |
| 1399 | } |
| 1400 | |
| 1401 | // Get moderator user IDs |
| 1402 | $user_ids = bbp_get_moderator_ids( $forum_id ); |
| 1403 | |
| 1404 | // Remove author from the list |
| 1405 | $author_key = array_search( (int) get_post( $post_id )->post_author, $user_ids, true ); |
| 1406 | if ( false !== $author_key ) { |
| 1407 | unset( $user_ids[ $author_key ] ); |
| 1408 | } |
| 1409 | |
| 1410 | /** |
| 1411 | * User IDs filter to send moderation emails to. |
| 1412 | * |
| 1413 | * @since 2.7.x |
| 1414 | * |
| 1415 | * @param array $user_ids Array of user IDs |
| 1416 | * @param int $forum_id Forum ID |
| 1417 | */ |
| 1418 | $user_ids = (array) apply_filters( 'bbp_forum_moderation_user_ids', $user_ids, $forum_id ); |
| 1419 | |
| 1420 | // Bail if no one to notify |
| 1421 | if ( empty( $user_ids ) ) { |
| 1422 | return false; |
| 1423 | } |
| 1424 | |
| 1425 | // Get email addresses, bail if empty |
| 1426 | $email_addresses = bbp_get_email_addresses_from_user_ids( $user_ids ); |
| 1427 | if ( empty( $email_addresses ) ) { |
| 1428 | return false; |
| 1429 | } |
| 1430 | |
| 1431 | /** Mail ******************************************************************/ |
| 1432 | |
| 1433 | // Remove content filters for email display usage |
| 1434 | bbp_remove_all_filters( "bbp_get_{$type}_content" ); |
| 1435 | bbp_remove_all_filters( "bbp_get_{$type}_title" ); |
| 1436 | bbp_remove_all_filters( 'the_title' ); |
| 1437 | |
| 1438 | // Email variables |
| 1439 | $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); |
| 1440 | $title = wp_specialchars_decode( strip_tags( $title ), ENT_QUOTES ); |
| 1441 | $author_name = wp_specialchars_decode( strip_tags( $author_name ), ENT_QUOTES ); |
| 1442 | $forum_name = wp_specialchars_decode( strip_tags( bbp_get_forum_title( $forum_id ) ), ENT_QUOTES ); |
| 1443 | $content = wp_specialchars_decode( strip_tags( $content ), ENT_QUOTES ); |
| 1444 | $url = bbp_add_view_all( $url, true ); |
| 1445 | |
| 1446 | $message = sprintf( esc_html__( '%1$s wrote: |
| 1447 | |
| 1448 | %2$s |
| 1449 | |
| 1450 | ----------- |
| 1451 | |
| 1452 | To approve, trash or mark this post as spam, click here: |
| 1453 | %3$s |
| 1454 | |
| 1455 | You are receiving this email because you are a moderator for the %4$s forum.', 'bbpress' ), |
| 1456 | |
| 1457 | $author_name, |
| 1458 | $content, |
| 1459 | $url, |
| 1460 | $forum_name |
| 1461 | ); |
| 1462 | |
| 1463 | /** |
| 1464 | * Filters email message for moderation email. |
| 1465 | * |
| 1466 | * @since 2.7.0 |
| 1467 | * |
| 1468 | * @param string $message Email content |
| 1469 | * @param int $post_id Moderated post ID |
| 1470 | * @param int $forum_id Forum ID containing the moderated post. |
| 1471 | */ |
| 1472 | $message = apply_filters( 'bbp_forum_moderation_mail_message', $message, $post_id, $forum_id ); |
| 1473 | if ( empty( $message ) ) { |
| 1474 | return; |
| 1475 | } |
| 1476 | |
| 1477 | /* translators: bbPress moderation email subject. 1: Site title, 2: Topic title, 3: Forum title */ |
| 1478 | $subject = sprintf( __( '[%1$s] Please moderate: "%2$s" from the forum "%3$s"', 'bbpress' ), $blog_name, $title, $forum_name ); |
| 1479 | |
| 1480 | /** |
| 1481 | * Filters email subject for moderation email. |
| 1482 | * |
| 1483 | * @since 2.7.0 |
| 1484 | * |
| 1485 | * @param string $title Email subject |
| 1486 | * @param int $post_id Moderated post ID |
| 1487 | * @param int $forum_id Forum ID containing the moderated post |
| 1488 | */ |
| 1489 | $subject = apply_filters( 'bbp_forum_moderation_mail_subject', $subject, $post_id, $forum_id ); |
| 1490 | if ( empty( $subject ) ) { |
| 1491 | return; |
| 1492 | } |
| 1493 | |
| 1494 | /** Headers ***************************************************************/ |
| 1495 | |
| 1496 | // Default bbPress X-header |
| 1497 | $headers = array( bbp_get_email_header() ); |
| 1498 | |
| 1499 | // Get the noreply@ address |
| 1500 | $no_reply = bbp_get_do_not_reply_address(); |
| 1501 | |
| 1502 | // Setup "From" email address |
| 1503 | $from_email = apply_filters( 'bbp_moderation_from_email', $no_reply ); |
| 1504 | |
| 1505 | // Setup the From header |
| 1506 | $headers[] = 'From: ' . get_bloginfo( 'name' ) . ' <' . $from_email . '>'; |
| 1507 | |
| 1508 | // Loop through addresses |
| 1509 | foreach ( (array) $email_addresses as $address ) { |
| 1510 | $headers[] = 'Bcc: ' . $address; |
| 1511 | } |
| 1512 | |
| 1513 | /** Send it ***************************************************************/ |
| 1514 | |
| 1515 | /** |
| 1516 | * Filters mail headers for the email sent to moderators. |
| 1517 | * |
| 1518 | * @since 2.7.0 |
| 1519 | * |
| 1520 | * @param array $headers Email headers |
| 1521 | */ |
| 1522 | $headers = apply_filters( 'bbp_moderation_mail_headers', $headers ); |
| 1523 | |
| 1524 | /** |
| 1525 | * Filters "To" email address for the email sent to moderators. |
| 1526 | * |
| 1527 | * @since 2.7.0 |
| 1528 | * |
| 1529 | * @param string $email Email address. Defaults to no-reply email address. |
| 1530 | */ |
| 1531 | $to_email = apply_filters( 'bbp_moderation_to_email', $no_reply ); |
| 1532 | |
| 1533 | |
| 1534 | /** |
| 1535 | * Hook to do something before email is sent to moderators. |
| 1536 | * |
| 1537 | * @since 2.7.0 |
| 1538 | * |
| 1539 | * @param int $post_id Moderated post ID |
| 1540 | * @param int $forum_id Forum ID containing moderated post |
| 1541 | * @param array $user_ids User IDs to send email to. |
| 1542 | */ |
| 1543 | do_action( 'bbp_pre_notify_forum_moderators', $post_id, $forum_id, $user_ids ); |
| 1544 | |
| 1545 | // Send notification email |
| 1546 | wp_mail( $to_email, $subject, $message, $headers ); |
| 1547 | |
| 1548 | /** |
| 1549 | * Hook to do something after email is sent to moderators. |
| 1550 | * |
| 1551 | * @since 2.7.0 |
| 1552 | * |
| 1553 | * @param int $post_id Moderated post ID |
| 1554 | * @param int $forum_id Forum ID containing moderated post |
| 1555 | * @param array $user_ids User IDs to send email to. |
| 1556 | */ |
| 1557 | do_action( 'bbp_post_notify_forum_moderators', $post_id, $forum_id, $user_ids ); |
| 1558 | |
| 1559 | // Restore previously removed filters |
| 1560 | bbp_restore_all_filters( "bbp_get_{$type}_content" ); |
| 1561 | bbp_restore_all_filters( "bbp_get_{$type}_title" ); |
| 1562 | bbp_restore_all_filters( 'the_title' ); |
| 1563 | |
| 1564 | return true; |
| 1565 | } |
| 1566 | |