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