| 1 |
<?php |
| 2 |
namespace Forumax\WpWidgets; |
| 3 |
use WP_Widget; |
| 4 |
use WP_Query; |
| 5 |
|
| 6 |
// Creating the widget |
| 7 |
class Forumax_Topic_Info extends WP_Widget |
| 8 |
{ |
| 9 |
function __construct() |
| 10 |
{ |
| 11 |
parent::__construct( |
| 12 |
'bbpress_forum_topic_info_widget', // Base ID of your widget |
| 13 |
__('(Forumax) Forum Topic Info', 'forumax'), // Widget name |
| 14 |
array('description' => __('Displays information about a Forumax forum topic', 'forumax'), ) // Widget description |
| 15 |
); |
| 16 |
} |
| 17 |
|
| 18 |
// Creating widget front-end |
| 19 |
public function widget($args, $instance) |
| 20 |
{ |
| 21 |
global $post; |
| 22 |
|
| 23 |
if (!$post || !isset($post->ID) || $post->post_type != 'topic') { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
$topic_id = $post->ID; |
| 28 |
$topic = get_post($topic_id); |
| 29 |
|
| 30 |
if (empty($topic) || $topic->post_type != 'topic') { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
$forum_id = get_post_meta($topic_id, '_bbp_forum_id', true); |
| 35 |
$forum_url = get_permalink($forum_id); |
| 36 |
$forum_title = get_the_title($forum_id); |
| 37 |
$status = bbp_get_topic_status($topic_id); |
| 38 |
$replies = bbp_get_topic_reply_count($topic_id); |
| 39 |
$voices = bbp_get_topic_voice_count($topic_id); |
| 40 |
|
| 41 |
echo wp_kses_post($args['before_widget']); |
| 42 |
|
| 43 |
if (!empty($instance['title'])) { |
| 44 |
echo wp_kses_post($args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title']); |
| 45 |
} |
| 46 |
|
| 47 |
echo '<ul class="forumax-topic-info-widget">'; |
| 48 |
echo '<li><span class="forumax-topic-info-label">' . esc_html__('Forum:', 'forumax') . '</span><span class="forumax-topic-info-value"><a href="' . esc_url($forum_url) . '">' . esc_html($forum_title) . '</a></span></li>'; |
| 49 |
echo '<li><span class="forumax-topic-info-label">' . esc_html__('Topic:', 'forumax') . '</span><span class="forumax-topic-info-value">' . esc_html($topic->post_title) . '</span></li>'; |
| 50 |
echo '<li><span class="forumax-topic-info-label">' . esc_html__('Author:', 'forumax') . '</span><span class="forumax-topic-info-value">' . esc_html(get_the_author_meta('display_name', $topic->post_author)) . '</span></li>'; |
| 51 |
echo '<li><span class="forumax-topic-info-label">' . esc_html__('Date:', 'forumax') . '</span><span class="forumax-topic-info-value">' . esc_html(get_the_date('', $topic->ID)) . '</span></li>'; |
| 52 |
echo '<li><span class="forumax-topic-info-label">' . esc_html__('Status:', 'forumax') . '</span><span class="forumax-topic-info-value">' . esc_html($status) . '</span></li>'; |
| 53 |
echo '<li><span class="forumax-topic-info-label">' . esc_html__('Replies:', 'forumax') . '</span><span class="forumax-topic-info-value">' . esc_html($replies) . '</span></li>'; |
| 54 |
echo '<li><span class="forumax-topic-info-label">' . esc_html__('Voices:', 'forumax') . '</span><span class="forumax-topic-info-value">' . esc_html($voices) . '</span></li>'; |
| 55 |
echo '</ul>'; |
| 56 |
|
| 57 |
echo wp_kses_post($args['after_widget']); |
| 58 |
} |
| 59 |
|
| 60 |
// Widget Backend |
| 61 |
public function form($instance) |
| 62 |
{ |
| 63 |
$title = $instance['title'] ?? ''; |
| 64 |
$topic_id = $instance['topic_id'] ?? ''; |
| 65 |
// Widget admin form |
| 66 |
?> |
| 67 |
<p> |
| 68 |
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"> |
| 69 |
<?php esc_html_e('Title:', 'forumax'); ?> |
| 70 |
</label> |
| 71 |
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" |
| 72 |
name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" |
| 73 |
value="<?php echo esc_attr($title); ?>" /> |
| 74 |
</p> |
| 75 |
<p> |
| 76 |
<label for="<?php echo esc_attr($this->get_field_id('topic_id')); ?>"> |
| 77 |
<?php esc_html_e('Topic ID:', 'forumax'); ?> |
| 78 |
</label> |
| 79 |
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('topic_id')); ?>" |
| 80 |
name="<?php echo esc_attr($this->get_field_name('topic_id')); ?>" type="text" |
| 81 |
value="<?php echo esc_attr($topic_id); ?>" /> |
| 82 |
</p> |
| 83 |
<?php |
| 84 |
} |
| 85 |
|
| 86 |
// Updating widget replacing old instances with new |
| 87 |
public function update($new_instance, $old_instance) |
| 88 |
{ |
| 89 |
$instance = array(); |
| 90 |
$instance['title'] = isset($new_instance['title']) ? sanitize_text_field($new_instance['title']) : ''; |
| 91 |
$instance['topic_id'] = isset($new_instance['topic_id']) ? absint($new_instance['topic_id']) : ''; |
| 92 |
|
| 93 |
return $instance; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|