diff --git a/classes/comment-moderation-message.php b/classes/comment-moderation-message.php new file mode 100644 index 0000000..c5aed85 --- /dev/null +++ b/classes/comment-moderation-message.php @@ -0,0 +1,187 @@ + $time ) { + $this->entries[] = $this->get_single_message( get_comment( $comment ), $time ); + } + } + + /** + * Get comment moderation section message. + * + * @return string The section message. + */ + public function get_message() { + $processed_count = count( $this->entries ) - count( array_filter( $this->entries ) ); + + $message = '
' . __( 'Pending Comments', 'digest' ) . '
'; + $message .= ''; + $message .= sprintf( + _n( + 'There is %s new comment waiting for approval.', + 'There are %s new comments waiting for approval.', + count( $this->entries ), + 'digest' + ), + number_format_i18n( count( $this->entries ) ) + ); + if ( 0 < $processed_count ) { + $message .= ' '; + $message .= sprintf( + _n( + '%s comment was already moderated.', + '%s comments were already moderated.', + $processed_count, + 'digest' + ), + number_format_i18n( $processed_count ) + ); + } + $message .= '
'; + $message .= implode( '', $this->entries ); + $message .= sprintf( + '' . __( 'Please visit the moderation panel.', 'digest' ) . '
', + admin_url( 'edit-comments.php?comment_status=moderated' ) + ); + + return $message; + } + + /** + * Get the comment moderation message. + * + * @param WP_Comment $comment The comment object. + * @param int $time The timestamp when the comment was written. + * @return string The comment moderation message. + */ + protected function get_single_message( $comment, $time ) { + if ( null === $comment || '0' !== $comment->comment_approved ) { + return ''; + } + + $message = $this->get_single_comment_content( $comment, $time ); + + $actions = array( + 'view' => __( 'Permalink', 'digest' ), + ); + + if ( $this->user && user_can( $this->user, 'edit_comment' ) || $this->user && $this->user->user_email === get_option( 'admin_email' ) ) { + $actions['approve'] = __( 'Approve', 'digest' ); + + if ( EMPTY_TRASH_DAYS ) { + $actions['trash'] = _x( 'Trash', 'verb', 'digest' ); + } else { + $actions['delete'] = __( 'Delete', 'digest' ); + } + $actions['spam'] = _x( 'Spam', 'verb', 'digest' ); + } + + if ( ! empty( $actions ) ) { + $message .= '' . $this->get_comment_action_links( $actions, $comment ) . '
'; + } + + return $message; + } + + /** + * Get the comment message. + * + * @param WP_Comment $comment The comment object. + * @param int $time The timestamp when the comment was written. + * @return string The comment message. + */ + protected function get_single_comment_content( $comment, $time ) { + $post_link = '' . get_the_title( $comment->comment_post_ID ) . ''; + + $message = ''; + + switch ( $comment->comment_type ) { + case 'trackback': + case 'pingback': + if ( 'pingback' === $comment->comment_type ) { + $message .= sprintf( __( 'Pingback on %1$s %2$s ago:', 'digest' ), $post_link, human_time_diff( $time, current_time( 'timestamp' ) ) ) . '' . __( 'New Comments', 'digest' ) . '
'; + $message .= ''; + $message .= sprintf( + _n( + 'There was %s new comment.', + 'There were %s new comments.', + count( $this->entries ), + 'digest' + ), + number_format_i18n( count( $this->entries ) ) + ); + if ( 0 < $processed_count ) { + $message .= ' '; + $message .= sprintf( + _n( + '%s comment was already moderated.', + '%s comments were already moderated.', + $processed_count, + 'digest' + ), + number_format_i18n( $processed_count ) + ); + } + $message .= '
'; + $message .= implode( '', $this->entries ); + $message .= sprintf( + '' . __( 'Please visit the moderation panel.', 'digest' ) . '
', + admin_url( 'edit-comments.php?comment_status=moderated' ) + ); + + return $message; + } + + /** + * Get the comment moderation message. + * + * @param WP_Comment $comment The comment object. + * @param int $time The timestamp when the comment was written. + * @return string The comment moderation message. + */ + protected function get_single_message( $comment, $time ) { + if ( null === $comment ) { + return ''; + } + + $message = $this->get_single_comment_content( $comment, $time ); + + $actions = array( + 'view' => __( 'Permalink', 'digest' ), + ); + + if ( $this->user && user_can( $this->user, 'edit_comment' ) || $this->user && $this->user->user_email === get_option( 'admin_email' ) ) { + if ( EMPTY_TRASH_DAYS ) { + $actions['trash'] = _x( 'Trash', 'verb', 'digest' ); + } else { + $actions['delete'] = __( 'Delete', 'digest' ); + } + $actions['spam'] = _x( 'Spam', 'verb', 'digest' ); + } + + if ( ! empty( $actions ) ) { + $message .= '' . $this->get_comment_action_links( $actions, $comment ) . '
'; + } + + return $message; + } +} diff --git a/classes/core-update-message.php b/classes/core-update-message.php new file mode 100644 index 0000000..0b33d00 --- /dev/null +++ b/classes/core-update-message.php @@ -0,0 +1,120 @@ + $time ) { + $this->entries[] = $this->get_single_message( $version, $time, $event ); + } + } + + /** + * Get core update section message. + * + * @return string The section message. + */ + public function get_message() { + return implode( '', $this->entries ); + } + + /** + * Get the single core update/failure message. + * + * @param string $version The version WordPress was updated to. + * @param int $time The timestamp when the update happened. + * @param string $event The current event. + * @return string The core update message. + */ + protected function get_single_message( $version, $time, $event ) { + if ( 'core_update_success' === $event ) { + return $this->get_core_update_success_message( $version, $time ); + } else { + return $this->get_core_update_fail_message( $version, $time ); + } + } + + /** + * Get the message for a successful core update. + * + * @param string $version The version WordPress was updated to. + * @param int $time The timestamp when the update happened. + * + * @return string The core update message. + */ + protected function get_core_update_success_message( $version, $time ) { + $message = sprintf( + '' . __( 'Your site at %2$s has been updated automatically to WordPress %3$s %4$s ago.', 'digest' ) . '
', + esc_url( home_url() ), + esc_html( str_replace( array( 'http://', 'https://' ), '', home_url() ) ), + esc_html( $version ), + human_time_diff( $time, current_time( 'timestamp' ) ) + ); + + // Can only reference the About screen if their update was successful. + list( $about_version ) = explode( '-', $version, 2 ); + + $message .= sprintf( + '' . __( 'For more on version %1$s, see the About WordPress screen.', 'digest' ) . '
', + esc_html( $about_version ), + esc_url( admin_url( 'about.php' ) ) + ); + + return $message; + } + + /** + * Get the message for a failed core update. + * + * @param string $version The version WordPress was updated to. + * @param int $time The timestamp when the update attempt happened. + * + * @return string The core update message. + */ + protected function get_core_update_fail_message( $version, $time ) { + global $wp_version; + + // Check if WordPress hasn't already been updated. + if ( version_compare( $wp_version, $version, '>=' ) ) { + return ''; + } + + $message = sprintf( + '' . __( 'Please update your site at %2$s to WordPress %3$s. Updating is easy and only takes a few moments.', 'digest' ) . '
', + esc_url( home_url() ), + esc_html( str_replace( array( 'http://', 'https://' ), '', home_url() ) ), + esc_html( $version ), + human_time_diff( $time, current_time( 'timestamp' ) ) + ); + + $message .= '' . sprintf( '%s', network_admin_url( 'update-core.php' ), __( 'Update now', 'digest' ) ) . '
'; + + return $message; + } +} diff --git a/classes/cron.php b/classes/cron.php index 4bc23b6..c1bfb72 100644 --- a/classes/cron.php +++ b/classes/cron.php @@ -93,7 +93,6 @@ protected static function run() { * Filter the digest subject. * * @param string $subject The digest's subject line. - * * @return string The filtered subject. */ $subject = apply_filters( 'digest_cron_email_subject', sprintf( $subject, get_bloginfo( 'name' ) ) ); @@ -107,8 +106,6 @@ protected static function run() { * * @param string $message The message to be sent. * @param string $recipient The recipient's email address. - * - * @return string The filtered message. */ $message = apply_filters( 'digest_cron_email_message', $message->get_message(), $recipient ); @@ -120,5 +117,3 @@ protected static function run() { WP_Digest_Queue::clear(); } } - -add_action( 'digest_event', array( 'WP_Digest_Cron', 'init' ) ); diff --git a/classes/message.php b/classes/message.php index 3e062e2..088582f 100644 --- a/classes/message.php +++ b/classes/message.php @@ -36,7 +36,8 @@ class WP_Digest_Message { */ public function __construct( $recipient, array $items ) { // Load the user with this email address if it exists. - $this->user = get_user_by( 'email', $recipient ); + $this->user = get_user_by( 'email', $recipient ); + $this->events = $this->process_event_items( $items ); } @@ -51,29 +52,7 @@ protected function process_event_items( $items ) { $events = array(); foreach ( $items as $item ) { - $method = 'get_' . $item[1] . '_message'; - - if ( in_array( $item[1], array( 'core_update_success', 'core_update_fail', 'core_update_manual' ) ) ) { - $item[1] = 'core_update'; - } - - $message = ''; - - if ( method_exists( $this, $method ) ) { - $message = $this->$method( $item[2], $item[0] ); - } - - /** - * Filter the single event message. - * - * @param string $message The message. - * @param array $item The event item. - * - * @return string The filtered message. - */ - $message = apply_filters( 'digest_event_message', $message, $item ); - - $events[ $item[1] ][] = $message; + $events[ $item[1] ][ $item[2] ] = $item[0]; } return $events; @@ -90,7 +69,8 @@ public function get_message() { // Loop through the processed events in manual order. foreach ( array( - 'core_update', + 'core_update_success', + 'core_update_failure', 'comment_notification', 'comment_moderation', 'new_user_notification', @@ -98,8 +78,15 @@ public function get_message() { ) as $event ) { if ( isset( $this->events[ $event ] ) && 0 < count( array_filter( $this->events[ $event ] ) ) ) { - // Add some text before and after the entries. - $message .= $this->get_event_section( $event, $this->events[ $event ] ); + /** + * Filter the message section + * + * @param string $message The message. + * @param array $entries The event items. + * @param object $user The current user. + * @param string $event The current event. + */ + $message .= apply_filters( 'digest_message_section_' . $event, '', $this->events[ $event ], $this->user, $event ); } } @@ -113,446 +100,6 @@ public function get_message() { return $salutation . $message . $valediction; } - - /** - * Get the content for a specific event by adding some text before and after the entries. - * - * @param string $section The type of event, e.g. comment notification or core update. - * @param array $entries The entries for this event. - * - * @return string The section's content. - */ - protected function get_event_section( $section, array $entries ) { - $method = 'get_' . $section . '_section_message'; - - $message = '' . __( 'Others', 'digest' ) . '
'; - $message .= implode( '', $entries ); - - if ( method_exists( $this, $method ) ) { - $message = $this->$method( $entries ); - } - - /** - * Filter the event section message. - * - * @param string $message The message. - * @param string $section The event section name. - * @param array $entries The event entries. - * - * @return string The filtered message. - */ - $message = apply_filters( 'digest_event_section_message', $message, $section, $entries ); - - return $message; - } - - /** - * Get comment notification section message. - * - * @param array $entries The comment notification entries. - * - * @return string The section message. - */ - protected function get_comment_notification_section_message( array $entries ) { - $processed_count = count( $entries ) - count( array_filter( $entries ) ); - - $message = '' . __( 'New Comments', 'digest' ) . '
'; - $message .= ''; - $message .= sprintf( - _n( - 'There was %s new comment.', - 'There were %s new comments.', - count( $entries ), - 'digest' - ), - number_format_i18n( count( $entries ) ) - ); - if ( 0 < $processed_count ) { - $message .= ' '; - $message .= sprintf( - _n( - '%s comment was already moderated.', - '%s comments were already moderated.', - $processed_count, - 'digest' - ), - number_format_i18n( $processed_count ) - ); - } - $message .= '
'; - $message .= implode( '', $entries ); - - return $message; - } - - /** - * Get comment moderation section message. - * - * @param array $entries The comment moderation entries. - * - * @return string The section message. - */ - protected function get_comment_moderation_section_message( array $entries ) { - $processed_count = count( $entries ) - count( array_filter( $entries ) ); - - $message = '' . __( 'Pending Comments', 'digest' ) . '
'; - $message .= ''; - $message .= sprintf( - _n( - 'There is %s new comment waiting for approval.', - 'There are %s new comments waiting for approval.', - count( $entries ), - 'digest' - ), - number_format_i18n( count( $entries ) ) - ); - if ( 0 < $processed_count ) { - $message .= ' '; - $message .= sprintf( - _n( - '%s comment was already moderated.', - '%s comments were already moderated.', - $processed_count, - 'digest' - ), - number_format_i18n( $processed_count ) - ); - } - $message .= '
'; - $message .= implode( '', $entries ); - $message .= sprintf( - '' . __( 'Please visit the moderation panel.', 'digest' ) . '
', - admin_url( 'edit-comments.php?comment_status=moderated' ) - ); - - return $message; - } - - /** - * Get new user notification section message. - * - * @param array $entries The new user notification entries. - * - * @return string The section message. - */ - protected function get_new_user_notification_section_message( array $entries ) { - $message = '' . __( 'New User Sign-ups', 'digest' ) . '
'; - $message .= '' . _n( 'The following user signed up on your site:', 'The following users signed up on your site:', count( $entries ), 'digest' ) . '
'; - $message .= '' . __( 'Password Changes', 'digest' ) . '
'; - $message .= '' . _n( 'The following user lost and changed his password:', 'The following users lost and changed their passwords:', count( $entries ), 'digest' ) . '
'; - $message .= '' . __( 'Core Updates', 'digest' ) . '
'; - $message .= implode( '', $entries ); - - return $message; - } - - /** - * Get the comment notification message. - * - * @param int $comment_id The comment ID. - * @param int $time The timestamp when the comment was written. - * - * @return string The comment moderation message. - */ - protected function get_comment_notification_message( $comment_id, $time ) { - /** - * Current comment object. - * - * @var object $comment - */ - $comment = get_comment( $comment_id ); - - if ( null === $comment || '1' !== $comment->comment_approved ) { - return ''; - } - - $message = $this->comment_message( $comment, $time ); - - $actions = array( - 'view' => __( 'Permalink', 'digest' ), - ); - - if ( $this->user && user_can( $this->user, 'edit_comment' ) ) { - if ( EMPTY_TRASH_DAYS ) { - $actions['trash'] = _x( 'Trash', 'verb', 'digest' ); - } else { - $actions['delete'] = __( 'Delete', 'digest' ); - } - $actions['spam'] = _x( 'Spam', 'verb', 'digest' ); - } - - if ( ! empty( $actions ) ) { - $message .= '' . $this->comment_action_links( $actions, $comment_id ) . '
'; - } - - return $message; - } - - /** - * Get the comment moderation message. - * - * @param int $comment_id The comment ID. - * @param int $time The timestamp when the comment was written. - * - * @return string The comment moderation message. - */ - protected function get_comment_moderation_message( $comment_id, $time ) { - /** - * Current comment object. - * - * @var object $comment - */ - $comment = get_comment( $comment_id ); - - if ( null === $comment || '0' !== $comment->comment_approved ) { - return ''; - } - - $message = $this->comment_message( $comment, $time ); - - $actions = array( - 'view' => __( 'Permalink', 'digest' ), - ); - - if ( $this->user && user_can( $this->user, 'edit_comment' ) ) { - $actions['approve'] = __( 'Approve', 'digest' ); - - if ( EMPTY_TRASH_DAYS ) { - $actions['trash'] = _x( 'Trash', 'verb', 'digest' ); - } else { - $actions['delete'] = __( 'Delete', 'digest' ); - } - $actions['spam'] = _x( 'Spam', 'verb', 'digest' ); - } - - if ( ! empty( $actions ) ) { - $message .= '' . $this->comment_action_links( $actions, $comment_id ) . '
'; - } - - return $message; - } - - /** - * Get the new user notification message. - * - * @param int $user_id The user ID. - * @param int $time The timestamp when the user signed up. - * - * @return string The new user notification message. - */ - protected function get_new_user_notification_message( $user_id, $time ) { - $user = new WP_User( $user_id ); - - if ( 0 === $user->ID ) { - return ''; - } - - return sprintf( - '' . __( 'Your site at %2$s has been updated automatically to WordPress %3$s %4$s ago.', 'digest' ) . '
', - esc_url( home_url() ), - esc_html( str_replace( array( 'http://', 'https://' ), '', home_url() ) ), - esc_html( $version ), - human_time_diff( $time, current_time( 'timestamp' ) ) - ); - - // Can only reference the About screen if their update was successful. - list( $about_version ) = explode( '-', $version, 2 ); - - $message .= sprintf( - '' . __( 'For more on version %1$s, see the About WordPress screen.', 'digest' ) . '
', - esc_html( $about_version ), - esc_url( admin_url( 'about.php' ) ) - ); - - return $message; - } - - /** - * Get the message for a failed core update. - * - * @param string $version The version WordPress was updated to. - * @param int $time The timestamp when the update attempt happened. - * - * @return string The core update message. - */ - protected function get_core_update_fail_message( $version, $time ) { - global $wp_version; - - // Check if WordPress hasn't already been updated. - if ( version_compare( $wp_version, $version, '>=' ) ) { - return ''; - } - - $message = sprintf( - '' . __( 'Please update your site at %2$s to WordPress %3$s. Updating is easy and only takes a few moments.', 'digest' ) . '
', - esc_url( home_url() ), - esc_html( str_replace( array( 'http://', 'https://' ), '', home_url() ) ), - esc_html( $version ), - human_time_diff( $time, current_time( 'timestamp' ) ) - ); - - $message .= '' . sprintf( '%s', network_admin_url( 'update-core.php' ), __( 'Update now', 'digest' ) ) . '
'; - - return $message; - } - - /** - * Get the message for an available core update that can't be installed automatically. - * - * @param string $version The version WordPress was updated to. - * @param int $time The timestamp when the update notification got in. - * - * @return string The core update message. - */ - protected function get_core_update_manual_message( $version, $time ) { - return $this->get_core_update_fail_message( $version, $time ); - } - - /** - * Get the comment message. - * - * @param object $comment The comment object. - * @param int $time The timestamp when the comment was written. - * - * @return string The comment message. - */ - protected function comment_message( $comment, $time ) { - $post_link = '' . get_the_title( $comment->comment_post_ID ) . ''; - - $message = ''; - - switch ( $comment->comment_type ) { - case 'trackback': - $message .= sprintf( __( 'Trackback on %1$s %2$s ago:', 'digest' ), $post_link, human_time_diff( $time, current_time( 'timestamp' ) ) ) . '' . __( 'Password Changes', 'digest' ) . '
'; + if ( 1 === count( $this->entries ) ) { + $message .= '' . __( 'The following user lost and changed his password:', 'digest' ) . '
'; + } else { + $message .= '' . __( 'The following users lost and changed their passwords:', 'digest' ) . '
'; + } + $message .= '' . __( 'Core Updates', 'digest' ) . '
'; + } + + return $content . $message->get_message(); +}, 10, 4 ); + +add_filter( 'digest_message_section_comment_moderation', function ( $content, $entries, $user ) { + $message = new WP_Digest_Comment_Moderation_Message( $entries, $user ); + + return $content . $message->get_message(); +}, 10, 3 ); + +add_filter( 'digest_message_section_comment_notification', function ( $content, $entries, $user ) { + $message = new WP_Digest_Comment_Notification_Message( $entries, $user ); + + return $content . $message->get_message(); +}, 10, 3 ); + +add_filter( 'digest_message_section_new_user_notification', function ( $content, $entries, $user ) { + $message = new WP_Digest_User_Notification_Message( $entries, $user ); + + return $content . $message->get_message(); +}, 10, 3 ); + +add_filter( 'digest_message_section_password_change_notification', function ( $content, $entries, $user ) { + $message = new WP_Digest_Password_Change_Notification_Message( $entries, $user ); + + return $content . $message->get_message(); +}, 10, 3 ); diff --git a/includes/functions.php b/includes/functions.php new file mode 100644 index 0000000..e62db6d --- /dev/null +++ b/includes/functions.php @@ -0,0 +1,35 @@ +