| 1 |
<?php |
| 2 |
namespace Forumax\WpWidgets; |
| 3 |
use WP_Widget; |
| 4 |
use WP_Query; |
| 5 |
|
| 6 |
/** |
| 7 |
* Class Forumax_Recent_Replies |
| 8 |
* A list of the most recent replies. |
| 9 |
* |
| 10 |
* @package Forumax\WpWidgets |
| 11 |
*/ |
| 12 |
class Forumax_Recent_Replies extends WP_Widget |
| 13 |
{ |
| 14 |
/** |
| 15 |
* Forumax Recent Replies Widget |
| 16 |
* |
| 17 |
* Registers the replies widget |
| 18 |
*/ |
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
$widget_ops = apply_filters('forumax_replies_widget_options', array( |
| 22 |
'classname' => 'forumax_recent_replies_widget widget_display_replies', |
| 23 |
'description' => esc_html__('A list of the most recent replies.', 'forumax'), |
| 24 |
'customize_selective_refresh' => true |
| 25 |
)); |
| 26 |
|
| 27 |
parent::__construct('forumax_recent_replies', esc_html__('(Forumax) Recent Replies', 'forumax'), $widget_ops); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Register the widget |
| 32 |
*/ |
| 33 |
public static function register_widget() |
| 34 |
{ |
| 35 |
register_widget(__CLASS__); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Displays the output, the replies list |
| 40 |
* |
| 41 |
* @param array $args Arguments |
| 42 |
* @param array $instance Instance |
| 43 |
*/ |
| 44 |
public function widget($args = array(), $instance = array()) |
| 45 |
{ |
| 46 |
|
| 47 |
// Get widget settings |
| 48 |
$settings = $this->parse_settings($instance); |
| 49 |
|
| 50 |
// Typical WordPress filter |
| 51 |
$settings['title'] = apply_filters('widget_title', $settings['title'], $instance, $this->id_base); |
| 52 |
|
| 53 |
// Forumax filter |
| 54 |
$settings['title'] = apply_filters('forumax_replies_widget_title', $settings['title'], $instance, $this->id_base); |
| 55 |
|
| 56 |
// Note: private and hidden forums will be excluded via the |
| 57 |
// bbp_pre_get_posts_normalize_forum_visibility action and function. |
| 58 |
$widget_query = new WP_Query(array( |
| 59 |
'post_type' => bbp_get_reply_post_type(), |
| 60 |
'post_status' => bbp_get_public_reply_statuses(), |
| 61 |
'posts_per_page' => (int) $settings['max_shown'], |
| 62 |
'ignore_sticky_posts' => true, |
| 63 |
'no_found_rows' => true, |
| 64 |
'update_post_term_cache' => false, |
| 65 |
'update_post_meta_cache' => false |
| 66 |
)); |
| 67 |
|
| 68 |
// Bail if no replies are found |
| 69 |
if (!$widget_query->have_posts()) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
echo $args['before_widget']; |
| 74 |
|
| 75 |
if (!empty($settings['title'])) { |
| 76 |
echo $args['before_title'] . $settings['title'] . $args['after_title']; |
| 77 |
} ?> |
| 78 |
|
| 79 |
<ul class="bbp-replies-widget frmx-list-unstyled"> |
| 80 |
|
| 81 |
<?php while ($widget_query->have_posts()): |
| 82 |
$widget_query->the_post(); ?> |
| 83 |
|
| 84 |
<li> |
| 85 |
|
| 86 |
<?php |
| 87 |
// Verify the reply ID |
| 88 |
$reply_id = bbp_get_reply_id($widget_query->post->ID); |
| 89 |
$reply_link = '<a class="bbp-reply-topic-title" href="' . esc_url(bbp_get_reply_url($reply_id)) . '" title="' . esc_attr(bbp_get_reply_excerpt($reply_id, 50)) . '">' . esc_html(bbp_get_reply_topic_title($reply_id)) . '</a>'; |
| 90 |
$time = get_the_time('U', $reply_id); |
| 91 |
$show_date = '<time datetime="' . gmdate('Y-m-d H:i:s', $time) . '">' . esc_html(bbp_get_time_since($time)) . '</time>'; |
| 92 |
|
| 93 |
// Only query user if showing them |
| 94 |
if (!empty($settings['show_user'])): |
| 95 |
$author_link = bbp_get_reply_author_link(array('post_id' => $reply_id, 'type' => 'both', 'size' => 14)); |
| 96 |
else: |
| 97 |
$author_link = false; |
| 98 |
endif; |
| 99 |
|
| 100 |
// Reply author, link, and timestamp |
| 101 |
if (!empty($settings['show_date']) && !empty($author_link)): |
| 102 |
|
| 103 |
// translators: 1: reply author, 2: reply link, 3: reply timestamp |
| 104 |
printf(esc_html_x('%1$s on %2$s %3$s', 'widgets', 'bbpress'), $author_link, $reply_link, $show_date); |
| 105 |
|
| 106 |
// Reply link and timestamp |
| 107 |
elseif (!empty($settings['show_date'])): |
| 108 |
echo $reply_link . ' ' . $show_date; |
| 109 |
|
| 110 |
// Reply author and title |
| 111 |
elseif (!empty($author_link)): |
| 112 |
|
| 113 |
// translators: 1: reply author, 2: reply link |
| 114 |
printf(esc_html_x('%1$s on %2$s', 'widgets', 'bbpress'), $author_link, $reply_link); |
| 115 |
|
| 116 |
// Only the reply title |
| 117 |
else: |
| 118 |
echo $reply_link; |
| 119 |
endif; |
| 120 |
?> |
| 121 |
|
| 122 |
</li> |
| 123 |
|
| 124 |
<?php endwhile; ?> |
| 125 |
|
| 126 |
</ul> |
| 127 |
|
| 128 |
<?php echo $args['after_widget']; |
| 129 |
|
| 130 |
// Reset the $post global |
| 131 |
wp_reset_postdata(); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Update the reply widget options |
| 136 |
* |
| 137 |
* @param array $new_instance The new instance options |
| 138 |
* @param array $old_instance The old instance options |
| 139 |
* @return array Updated instance |
| 140 |
*/ |
| 141 |
public function update($new_instance = array(), $old_instance = array()) |
| 142 |
{ |
| 143 |
$instance = $old_instance; |
| 144 |
$instance['title'] = strip_tags($new_instance['title']); |
| 145 |
$instance['max_shown'] = (int) $new_instance['max_shown']; |
| 146 |
|
| 147 |
// Date |
| 148 |
$instance['show_date'] = isset($new_instance['show_date']) |
| 149 |
? (bool) $new_instance['show_date'] |
| 150 |
: false; |
| 151 |
|
| 152 |
// Author |
| 153 |
$instance['show_user'] = isset($new_instance['show_user']) |
| 154 |
? (bool) $new_instance['show_user'] |
| 155 |
: false; |
| 156 |
|
| 157 |
return $instance; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Output the reply widget options form |
| 162 |
* |
| 163 |
* @param array $instance Instance |
| 164 |
*/ |
| 165 |
public function form($instance = array()) |
| 166 |
{ |
| 167 |
|
| 168 |
// Get widget settings |
| 169 |
$settings = $this->parse_settings($instance); ?> |
| 170 |
|
| 171 |
<p> |
| 172 |
<label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('Title:', 'forumax'); ?> |
| 173 |
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" |
| 174 |
name="<?php echo $this->get_field_name('title'); ?>" type="text" |
| 175 |
value="<?php echo esc_attr($settings['title']); ?>" /> |
| 176 |
</label> |
| 177 |
</p> |
| 178 |
|
| 179 |
<p> |
| 180 |
<label |
| 181 |
for="<?php echo $this->get_field_id('max_shown'); ?>"><?php esc_html_e('Maximum replies to show:', 'forumax'); ?> |
| 182 |
<input class="widefat" id="<?php echo $this->get_field_id('max_shown'); ?>" |
| 183 |
name="<?php echo $this->get_field_name('max_shown'); ?>" type="text" |
| 184 |
value="<?php echo esc_attr($settings['max_shown']); ?>" /> |
| 185 |
</label> |
| 186 |
</p> |
| 187 |
|
| 188 |
<p> |
| 189 |
<label for="<?php echo $this->get_field_id('show_date'); ?>"> |
| 190 |
<input type="checkbox" id="<?php echo $this->get_field_id('show_date'); ?>" |
| 191 |
name="<?php echo $this->get_field_name('show_date'); ?>" <?php checked(true, $settings['show_date']); ?> |
| 192 |
value="1" /> |
| 193 |
<?php esc_html_e('Show post date', 'forumax'); ?> |
| 194 |
</label> |
| 195 |
</p> |
| 196 |
|
| 197 |
<p> |
| 198 |
<label for="<?php echo $this->get_field_id('show_user'); ?>"> |
| 199 |
<input type="checkbox" id="<?php echo $this->get_field_id('show_user'); ?>" |
| 200 |
name="<?php echo $this->get_field_name('show_user'); ?>" <?php checked(true, $settings['show_user']); ?> |
| 201 |
value="1" /> |
| 202 |
<?php esc_html_e('Show reply author', 'forumax'); ?> |
| 203 |
</label> |
| 204 |
</p> |
| 205 |
|
| 206 |
<?php |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Merge the widget settings into defaults array. |
| 211 |
* |
| 212 |
* @param array $instance Instance |
| 213 |
* @return array Parsed settings |
| 214 |
*/ |
| 215 |
public function parse_settings($instance = array()) |
| 216 |
{ |
| 217 |
return bbp_parse_args($instance, array( |
| 218 |
'title' => esc_html__('Recent Replies', 'forumax'), |
| 219 |
'max_shown' => 5, |
| 220 |
'show_date' => true, |
| 221 |
'show_user' => true |
| 222 |
), 'forumax_replies_widget_settings'); |
| 223 |
} |
| 224 |
} |
| 225 |
|