|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Plugin Name: Notify WordCamp Central on pending posts |
| 4 | + * Plugin URI: http://wordcamp.org |
| 5 | + * Description: Send email notification to WordCamp Central when post status becomes pending. |
| 6 | + * Version: 1.0 |
| 7 | + * |
| 8 | + * Heavily inspired from Pending Submission Notifications plugin by Razvan Horeanga. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Notify_Central_Pending_Posts; |
| 12 | + |
| 13 | +if ( ! defined( 'ABSPATH' ) ) { |
| 14 | + die( 'Invalid request.' ); |
| 15 | +} |
| 16 | + |
| 17 | +add_action( 'transition_post_status', __NAMESPACE__ . '\send_notification_email', 10, 3 ); |
| 18 | + |
| 19 | +/** |
| 20 | + * Send the notification email. |
| 21 | + * |
| 22 | + * @param string $new_status New post status. |
| 23 | + * @param string $old_status Old post status. |
| 24 | + * @param WP_Post $post Post object. |
| 25 | + */ |
| 26 | +function send_notification_email( $new_status, $old_status, $post ) { |
| 27 | + if ( 'pending' === $new_status && user_can( $post->post_author, 'edit_posts' ) ) { |
| 28 | + // Prevent many emails from the same post. |
| 29 | + $sent = get_post_meta( $post->ID, '_ncpp_sent', true ); |
| 30 | + if ( ! empty( $sent ) ) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + $edit_link = get_edit_post_link( $post->ID, '' ); |
| 35 | + $preview_link = get_permalink( $post->ID ) . '&preview=true'; |
| 36 | + |
| 37 | + $username = get_userdata( $post->post_author ); |
| 38 | + $username_last_edit = get_the_modified_author(); |
| 39 | + |
| 40 | + $subject = __( 'New post on WordCamp Central pending review', 'wordcamporg' ) . ": {$post->post_title}"; |
| 41 | + |
| 42 | + $message = __( 'Hello team! A new post on WordCamp Central is pending review.', 'wordcamporg' ); |
| 43 | + $message .= "\r\n\r\n"; |
| 44 | + $message .= __( 'Title' ) . ": {$post->post_title}\r\n"; |
| 45 | + $message .= __( 'Author' ) . ": {$username->user_login}\r\n"; |
| 46 | + $message .= "\r\n\r\n"; |
| 47 | + $message .= __( 'Edit' ) . ": {$edit_link}\r\n"; |
| 48 | + $message .= __( 'Preview' ) . ": {$preview_link}"; |
| 49 | + |
| 50 | + wp_mail( '[email protected]', $subject, $message ); |
| 51 | + |
| 52 | + // Save a pointer that notification has been sent. |
| 53 | + update_post_meta( $post->ID, '_ncpp_sent', wp_date( 'Y-m-d H:i:s' ) ); |
| 54 | + } |
| 55 | +} |
0 commit comments