| 1 |
<?php |
| 2 |
/** |
| 3 |
* Recent coments widget |
| 4 |
* |
| 5 |
* @package wpstream-theme |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! class_exists( 'Wpstream_Recent_Comments_Widget' ) ) { |
| 14 |
/** |
| 15 |
* Widget class for displaying recent comments. |
| 16 |
* |
| 17 |
* This widget displays the most recent comments on the site. |
| 18 |
* |
| 19 |
* @since 2.8.0 |
| 20 |
*/ |
| 21 |
class Wpstream_Recent_Comments_Widget extends Wpstream_Widget_Base { |
| 22 |
/** |
| 23 |
* Sets up a new widget instance. |
| 24 |
* |
| 25 |
* @since 2.8.0 |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
$widget_ops = array( |
| 29 |
'description' => __( 'Your site’s most recent comments.', 'hello-wpstream' ), |
| 30 |
); |
| 31 |
parent::__construct( 'wpstream-recent-comments', __( 'Wpstream Recent Comments', 'hello-wpstream' ), $widget_ops ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Outputs the content for the current widget instance. |
| 36 |
* |
| 37 |
* @param array $args Display arguments including 'before_title', 'after_title', |
| 38 |
* 'before_widget', and 'after_widget'. |
| 39 |
* @param array $instance Settings for the current widget instance. |
| 40 |
* |
| 41 |
* @since 2.8.0 |
| 42 |
* @since 5.4.0 Creates a unique HTML ID for the `<ul>` element |
| 43 |
* if more than one instance is displayed on the page. |
| 44 |
*/ |
| 45 |
public function widget( $args, $instance ) { |
| 46 |
static $first_instance = true; |
| 47 |
|
| 48 |
$default_title = __( 'Recent Comments', 'hello-wpstream' ); |
| 49 |
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : $default_title; |
| 50 |
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 3; |
| 51 |
|
| 52 |
$comments = get_comments( |
| 53 |
apply_filters( |
| 54 |
'widget_comments_args', |
| 55 |
array( |
| 56 |
'number' => $number, |
| 57 |
'status' => 'approve', |
| 58 |
'post_status' => 'publish', |
| 59 |
), |
| 60 |
$instance |
| 61 |
) |
| 62 |
); |
| 63 |
|
| 64 |
if ( ! is_array( $comments ) || empty( $comments ) ) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
echo wp_kses_post($args['before_widget']); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 69 |
|
| 70 |
if ( $title ) { |
| 71 |
echo wp_kses_post($args['before_title']); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 72 |
echo esc_html( $title ); |
| 73 |
echo wp_kses_post($args['after_title']); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 74 |
} |
| 75 |
|
| 76 |
$recent_comments_id = ( $first_instance ) ? 'wpstream-recent-comments' : "wpstream-recent-comments-{$this->number}"; |
| 77 |
$first_instance = false; |
| 78 |
|
| 79 |
echo '<ul id="' . esc_attr( $recent_comments_id ) . '" class="wpstream-recent-comments d-flex flex-column">'; |
| 80 |
|
| 81 |
foreach ( (array) $comments as $comment ) { ?> |
| 82 |
<li class="wpstream-recent-comment d-flex align-items-start"> |
| 83 |
<img class="wpstream-recent-comment-author-img rounded-circle object-fit-cover" |
| 84 |
src="<?php echo esc_url( wpstream_get_author_profile_image_url_by_author_id( $comment->user_id, 48 ) ); ?>" |
| 85 |
alt="<?php echo esc_attr( get_comment_author( $comment ) ); ?>"> |
| 86 |
|
| 87 |
<div class="wpstream-recent-comment-body"> |
| 88 |
<p class="d-flex flex-wrap m-0"> |
| 89 |
<span class="wpstream-recent-comment-author "><?php echo esc_html( get_comment_author( $comment ) ); ?></span> |
| 90 |
<span class="wpstream-recent-comment-date text-gray"><?php echo esc_html( wpstream_get_published_duration_by_date_time( $comment->comment_date ) ); ?></span> |
| 91 |
</p> |
| 92 |
<p class="wpstream-recent-comment-text m-0"> |
| 93 |
<?php echo esc_html( get_comment_text( $comment ) ); ?> |
| 94 |
</p> |
| 95 |
<p class="m-0"> |
| 96 |
<span class="text-gray"><?php echo esc_html_x( 'In', 'widget', 'hello-wpstream' ); ?></span> |
| 97 |
<a class="" href="<?php echo esc_url( get_comment_link( $comment ) ); ?>"> |
| 98 |
<?php echo esc_html( get_the_title( $comment->comment_post_ID ) ); ?> |
| 99 |
</a> |
| 100 |
</p> |
| 101 |
</div> |
| 102 |
</li> |
| 103 |
|
| 104 |
<?php |
| 105 |
} |
| 106 |
|
| 107 |
echo '</ul>'; |
| 108 |
|
| 109 |
echo wp_kses_post ( $args['after_widget']); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Handles updating settings for the current widget instance. |
| 114 |
* |
| 115 |
* @param array $new_instance New settings for this instance as input by the user via WP_Widget::form(). |
| 116 |
* @param array $old_instance Old settings for this instance. |
| 117 |
* |
| 118 |
* @return array Updated settings to save. |
| 119 |
* @since 2.8.0 |
| 120 |
*/ |
| 121 |
public function update( $new_instance, $old_instance ) { |
| 122 |
$instance = $old_instance; |
| 123 |
$instance['title'] = sanitize_text_field( $new_instance['title'] ); |
| 124 |
$instance['number'] = absint( $new_instance['number'] ); |
| 125 |
|
| 126 |
return $instance; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Outputs the settings form for the widget. |
| 131 |
* |
| 132 |
* @param array $instance Current settings. |
| 133 |
* |
| 134 |
* @since 2.8.0 |
| 135 |
*/ |
| 136 |
public function form( $instance ) { |
| 137 |
$title = isset( $instance['title'] ) ? $instance['title'] : ''; |
| 138 |
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 3; |
| 139 |
?> |
| 140 |
<p> |
| 141 |
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'hello-wpstream' ); ?></label> |
| 142 |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" |
| 143 |
name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" |
| 144 |
value="<?php echo esc_attr( $title ); ?>"/> |
| 145 |
</p> |
| 146 |
|
| 147 |
<p> |
| 148 |
<label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php esc_html_e( 'Number of comments to show:', 'hello-wpstream' ); ?></label> |
| 149 |
<input class="tiny-text" |
| 150 |
id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" |
| 151 |
name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" type="number" step="1" min="1" |
| 152 |
value="<?php echo absint( $number ); ?>" size="3"/> |
| 153 |
</p> |
| 154 |
<?php |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|