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