Skip to content

Commit

Permalink
Split up WP_Digest_Message in smaller chunks. See #3
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed Oct 6, 2015
1 parent 9a3fbd7 commit 19c9899
Show file tree
Hide file tree
Showing 11 changed files with 697 additions and 477 deletions.
187 changes: 187 additions & 0 deletions classes/comment-moderation-message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php
/**
* This file holds the WP_Digest_Comment_Moderation_Message class.
*
* @package WP_Digest
*/

defined( 'WPINC' ) or die;

/**
* WP_Digest_Comment_Moderation_Message class.
*
* Responsible for creating the comment moderation section
*/
class WP_Digest_Comment_Moderation_Message extends WP_Digest_Section_Message {
/**
* Constructor.
*
* @param array $entries The comment moder ation entries.
* @param WP_User $user The current user.
*/
public function __construct( $entries, $user ) {
parent::__construct( $user );

foreach ( $entries as $comment => $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 = '<p><b>' . __( 'Pending Comments', 'digest' ) . '</b></p>';
$message .= '<p>';
$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 .= '</p>';
$message .= implode( '', $this->entries );
$message .= sprintf(
'<p>' . __( 'Please visit the <a href="%s">moderation panel</a>.', 'digest' ) . '</p>',
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 .= '<p>' . $this->get_comment_action_links( $actions, $comment ) . '</p>';
}

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 = '<a href="' . esc_url( get_permalink( $comment->comment_post_ID ) ) . '">' . get_the_title( $comment->comment_post_ID ) . '</a>';

$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' ) ) ) . '<br />';
} else {
$message .= sprintf( __( 'Trackback on %1$s %2$s ago:', 'digest' ), $post_link, human_time_diff( $time, current_time( 'timestamp' ) ) ) . '<br />';
}
$message .= sprintf( __( 'Website: %s', 'digest' ), '<a href="' . esc_url( $comment->comment_author_url ) . '">' . esc_html( $comment->comment_author ) . '</a>' ) . '<br />';
$message .= sprintf( __( 'Excerpt: %s', 'digest' ), '<br />' . $this->get_comment_text( $comment->comment_ID ) );
break;
default: // Comments.
$author = sprintf( __( 'Author: %s', 'digest' ), esc_html( $comment->comment_author ) );
if ( $comment->comment_author_url ) {
$author = sprintf( __( 'Author: %s', 'digest' ), '<a href="' . esc_url( $comment->comment_author_url ) . '">' . esc_html( $comment->comment_author ) . '</a>' );
}
$message = sprintf( __( 'Comment on %1$s %2$s ago:', 'digest' ), $post_link, human_time_diff( $time, current_time( 'timestamp' ) ) ) . '<br />';
$message .= $author . '<br />';
if ( $comment->comment_author_email ) {
$message .= sprintf( __( 'Email: %s', 'digest' ), '<a href="mailto:' . esc_attr( $comment->comment_author_email ) . '">' . esc_html( $comment->comment_author_email ) . '</a>' ) . '<br />';
}
$message .= sprintf( __( 'Comment: %s', 'digest' ), '<br />' . $this->get_comment_text( $comment->comment_ID ) );
break;
}

return $message;
}

/**
* Get the comment text, which is already filtered by WordPress.
*
* @param int $comment_id The comment ID.
* @return string The filtered comment text
*/
protected function get_comment_text( $comment_id ) {
ob_start();

comment_text( $comment_id );

return ob_get_clean();
}

/**
* Add action links to the message
*
* @param array $actions Actions for that comment.
* @param WP_Comment $comment The comment object.
* @return string The comment action links.
*/
protected function get_comment_action_links( array $actions, $comment ) {
$links = array();

foreach ( $actions as $action => $label ) {
$url = admin_url( sprintf( 'comment.php?action=%s&c=%d', $action, $comment->comment_ID ) );

if ( 'view' === $action ) {
$url = get_comment_link( $comment );
}

$links[] = sprintf(
'<a href="%1$s">%2$s</a>',
esc_url( $url ),
esc_html( $label )
);
}

return implode( ' | ', $links );
}
}
105 changes: 105 additions & 0 deletions classes/comment-notification-message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* This file holds the WP_Digest_Comment_Notification_Message class.
*
* @package WP_Digest
*/

defined( 'WPINC' ) or die;

/**
* WP_Digest_Comment_Notification_Message class.
*
* Responsible for creating the comment notification section
*/
class WP_Digest_Comment_Notification_Message extends WP_Digest_Comment_Moderation_Message {
/**
* Constructor.
*
* @param array $entries The comment moder ation entries.
* @param WP_User $user The current user.
*/
public function __construct( $entries, $user ) {
parent::__construct( $entries, $user );

$this->entries = array();
foreach ( $entries as $comment => $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 = '<p><b>' . __( 'New Comments', 'digest' ) . '</b></p>';
$message .= '<p>';
$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 .= '</p>';
$message .= implode( '', $this->entries );
$message .= sprintf(
'<p>' . __( 'Please visit the <a href="%s">moderation panel</a>.', 'digest' ) . '</p>',
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 .= '<p>' . $this->get_comment_action_links( $actions, $comment ) . '</p>';
}

return $message;
}
}
Loading

0 comments on commit 19c9899

Please sign in to comment.