Advertisement
Tyrsdei

widget plugin

May 27th, 2018
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.80 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Forum Display
  4. Plugin URI: http://thehost.ninja/
  5. Description: This plugin adds a custom widget.
  6. Version: 1.0
  7. Author: Modified by Jason Cook/Written by bbPress
  8. Author URI: http://bbpress.org/
  9. License: GPL2
  10. */
  11. // Exit if accessed directly
  12. if ( !defined( 'ABSPATH' ) ) exit;
  13. // The widget class
  14. /**
  15.  * bbPress Forum Widget
  16.  *
  17.  * Adds a widget which displays the forum list
  18.  *
  19.  * @since bbPress (r2653)
  20.  *
  21.  * @uses WP_Widget
  22.  */
  23. class forumdisplay_widget extends WP_Widget {
  24.  
  25.     /**
  26.      * bbPress Forum Widget
  27.      *
  28.      * Registers the forum widget
  29.      *
  30.      * @since bbPress (r2653)
  31.      *
  32.      * @uses apply_filters() Calls 'bbp_forums_widget_options' with the
  33.      *                        widget options
  34.      */
  35.     public function __construct() {
  36.         $widget_ops = apply_filters( 'forumdisplay_widget_options', array(
  37.             'classname'   => 'widget_display_forums',
  38.             'description' => __( 'A list of forums with an option to set the parent.', 'bbpress' )
  39.         ) );
  40.  
  41.         parent::__construct( false, __( 'Forum Display', 'bbpress' ), $widget_ops );
  42.     }
  43.  
  44.     /**
  45.      * Register the widget
  46.      *
  47.      * @since bbPress (r3389)
  48.      *
  49.      * @uses register_widget()
  50.      */
  51.     public static function register_widget() {
  52.         register_widget( 'forumdisplay_widget' );
  53.     }
  54.  
  55.     /**
  56.      * Displays the output, the forum list
  57.      *
  58.      * @since bbPress (r2653)
  59.      *
  60.      * @param mixed $args Arguments
  61.      * @param array $instance Instance
  62.      * @uses apply_filters() Calls 'bbp_forum_widget_title' with the title
  63.      * @uses get_option() To get the forums per page option
  64.      * @uses current_user_can() To check if the current user can read
  65.      *                           private() To resety name
  66.      * @uses bbp_has_forums() The main forum loop
  67.      * @uses bbp_forums() To check whether there are more forums available
  68.      *                     in the loop
  69.      * @uses bbp_the_forum() Loads up the current forum in the loop
  70.      * @uses bbp_forum_permalink() To display the forum permalink
  71.      * @uses bbp_forum_title() To display the forum title
  72.      */
  73.     public function widget( $args, $instance ) {
  74.  
  75.         // Get widget settings
  76.         $settings = $this->parse_settings( $instance );
  77.  
  78.         // Typical WordPress filter
  79.         $settings['title'] = apply_filters( 'widget_title',           $settings['title'], $instance, $this->id_base );
  80.  
  81.         // bbPress filter
  82.         $settings['title'] = apply_filters( 'forumdisplay_widget_title', $settings['title'], $instance, $this->id_base );
  83.  
  84.         // Note: private and hidden forums will be excluded via the
  85.         // bbp_pre_get_posts_normalize_forum_visibility action and function.
  86.         $widget_query = new WP_Query( array(
  87.             'post_type'           => bbp_get_forum_post_type(),
  88.             'post_parent'         => $settings['parent_forum'],
  89.             'post_status'         => bbp_get_public_status_id(),
  90.             'posts_per_page'      => get_option( '_bbp_forums_per_page', 50 ),
  91.             'ignore_sticky_posts' => true,
  92.             'no_found_rows'       => true,
  93.             'orderby'             => 'menu_order title',
  94.             'order'               => 'ASC'
  95.         ) );
  96.  
  97.         // Bail if no posts
  98.         if ( ! $widget_query->have_posts() ) {
  99.             return;
  100.         }
  101.  
  102.         echo $args['before_widget'];
  103.  
  104.         if ( !empty( $settings['title'] ) ) {
  105.             echo $args['before_title'] . $settings['title'] . $args['after_title'];
  106.         } ?>
  107.  
  108.         <ul>
  109.  
  110.             <?php while ( $widget_query->have_posts() ) : $widget_query->the_post(); ?>
  111.  
  112.                 <li><a class="bbp-forum-title" href="<?php bbp_forum_permalink( $widget_query->post->ID ); ?>"><?php bbp_forum_title( $widget_query->post->ID ); ?></a></li>
  113.  
  114.             <?php endwhile; ?>
  115.  
  116.         </ul>
  117.  
  118.         <?php echo $args['after_widget'];
  119.  
  120.         // Reset the $post global
  121.         wp_reset_postdata();
  122.     }
  123.  
  124.     /**
  125.      * Update the forum widget options
  126.      *
  127.      * @since bbPress (r2653)
  128.      *
  129.      * @param array $new_instance The new instance options
  130.      * @param array $old_instance The old instance options
  131.      */
  132.     public function update( $new_instance, $old_instance ) {
  133.         $instance                 = $old_instance;
  134.         $instance['title']        = strip_tags( $new_instance['title'] );
  135.         $instance['parent_forum'] = sanitize_text_field( $new_instance['parent_forum'] );
  136.  
  137.         // Force to any
  138.         if ( !empty( $instance['parent_forum'] ) && !is_numeric( $instance['parent_forum'] ) ) {
  139.             $instance['parent_forum'] = 'any';
  140.         }
  141.  
  142.         return $instance;
  143.     }
  144.  
  145.     /**
  146.      * Output the forum widget options form
  147.      *
  148.      * @since bbPress (r2653)
  149.      *
  150.      * @param $instance Instance
  151.      * @uses BBP_Forums_Widget::get_field_id() To output the field id
  152.      * @uses BBP_Forums_Widget::get_field_name() To output the field name
  153.      */
  154.     public function form( $instance ) {
  155.  
  156.         // Get widget settings
  157.         $settings = $this->parse_settings( $instance ); ?>
  158.  
  159.         <p>
  160.             <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'bbpress' ); ?>
  161.                 <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( $settings['title'] ); ?>" />
  162.             </label>
  163.         </p>
  164.  
  165.         <p>
  166.             <label for="<?php echo $this->get_field_id( 'parent_forum' ); ?>"><?php _e( 'Parent Forum ID:', 'bbpress' ); ?>
  167.                 <input class="widefat" id="<?php echo $this->get_field_id( 'parent_forum' ); ?>" name="<?php echo $this->get_field_name( 'parent_forum' ); ?>" type="text" value="<?php echo esc_attr( $settings['parent_forum'] ); ?>" />
  168.             </label>
  169.  
  170.             <br />
  171.  
  172.             <small><?php _e( '"0" to show only root - "any" to show all', 'bbpress' ); ?></small>
  173.         </p>
  174.  
  175.         <?php
  176.     }
  177.  
  178.     /**
  179.      * Merge the widget settings into defaults array.
  180.      *
  181.      * @since bbPress (r4802)
  182.      *
  183.      * @param $instance Instance
  184.      * @uses bbp_parse_args() To merge widget settings into defaults
  185.      */
  186.     public function parse_settings( $instance = array() ) {
  187.         return bbp_parse_args( $instance, array(
  188.             'title'        => __( 'Forums', 'bbpress' ),
  189.             'parent_forum' => 0
  190.         ), 'forum_widget_settings' );
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement