Snippi
A super awesome snippet tool.
- 1.
<?php
- 2.
// $latest_activity = dwqa_get_latest_activity_info( get_the_ID() );
- 3.
// $user_id = get_post_field( 'post_author', get_the_ID() ) ? get_post_field( 'post_author', get_the_ID() );
- 4.
- 5.
class DWQA_Widgets_Popular_Question extends WP_Widget {
- 6.
- 7.
- 8.
private $default_args = array();
- 9.
/**
- 10.
* Constructor
- 11.
*
- 12.
* @return void
- 13.
**/
- 14.
function __construct() {
- 15.
- 16.
$this->default_args = array(
- 17.
'title' => __( 'Popular Questions', 'dwqa' ),
- 18.
'number' => 5,
- 19.
'hide_date' => 0,
- 20.
'hide_user' => 0,
- 21.
);
- 22.
- 23.
$widget_ops = array(
- 24.
'classname' => 'dwqa-widget dwqa-popular-question',
- 25.
'description' => __( 'Show a list of popular questions.', 'dwqa' )
- 26.
);
- 27.
parent::__construct( 'dwqa-popular-question', __( 'DWQA Popular Questions', 'dwqa' ), $widget_ops );
- 28.
}
- 29.
- 30.
function widget( $args, $instance ) {
- 31.
extract( $args, EXTR_SKIP );
- 32.
$instance = wp_parse_args( $instance, $this->default_args);
- 33.
- 34.
echo $before_widget;
- 35.
echo $before_title;
- 36.
echo $instance['title'];
- 37.
echo $after_title;
- 38.
- 39.
$args = array(
- 40.
'posts_per_page' => $instance['number'],
- 41.
'order' => 'DESC',
- 42.
'orderby' => 'meta_value_num',
- 43.
'meta_key' => '_dwqa_views',
- 44.
'post_type' => 'dwqa-question',
- 45.
'suppress_filters' => false,
- 46.
);
- 47.
$questions = new WP_Query( $args );
- 48.
$user_id = get_post_field( 'post_author', get_the_ID() );
- 49.
if ( $questions->have_posts() ) {
- 50.
echo '<div class="dwqa-popular-questions">';
- 51.
echo '<ul>';
- 52.
while ( $questions->have_posts() ) { $questions->the_post();
- 53.
echo '<li><a href="'.get_permalink().'" class="question-title">'.get_the_title().'</a>';
- 54.
if(!$instance['hide_user']){
- 55.
- 56.
echo '<li><a href="'.dwqa_get_author_link( $user_id ).'" class="question-author">'.get_the_author().'</a>';
- 57.
}
- 58.
if(!$instance['hide_date']){
- 59.
echo ', ' . sprintf( esc_html__( '%s ago', 'dwqa' ), human_time_diff( get_post_time('U', true) ) );
- 60.
}
- 61.
echo '</li>';
- 62.
}
- 63.
echo '</ul>';
- 64.
echo '</div>';
- 65.
}
- 66.
wp_reset_query();
- 67.
wp_reset_postdata();
- 68.
echo $after_widget;
- 69.
}
- 70.
- 71.
function update( $new_instance, $old_instance ) {
- 72.
- 73.
// update logic goes here
- 74.
$updated_instance = $new_instance;
- 75.
return $updated_instance;
- 76.
}
- 77.
- 78.
function form( $instance ) {
- 79.
$instance = wp_parse_args( $instance, $this->default_args);
- 80.
- 81.
?>
- 82.
<p><label for="<?php echo $this->get_field_id( 'title' ) ?>"><?php _e( 'Widget title', 'dwqa' ) ?></label>
- 83.
<input type="text" name="<?php echo $this->get_field_name( 'title' ) ?>" id="<?php echo $this->get_field_id( 'title' ) ?>" value="<?php echo $instance['title'] ?>" class="widefat">
- 84.
</p>
- 85.
<p><label for="<?php echo $this->get_field_id( 'number' ) ?>"><?php _e( 'Number of posts', 'dwqa' ) ?></label>
- 86.
<input type="text" name="<?php echo $this->get_field_name( 'number' ) ?>" id="<?php echo $this->get_field_id( 'number' ) ?>" value="<?php echo $instance['number'] ?>" class="widefat">
- 87.
</p>
- 88.
<p>
- 89.
<input id="<?php echo $this->get_field_id( 'hide_user' ) ?>" name="<?php echo $this->get_field_name( 'hide_user' ) ?>" type="checkbox" <?php echo $instance['hide_user']?'checked':''; ?>> <label for="<?php echo $this->get_field_id( 'hide_user' ) ?>"><?php _e( 'Hide author', 'dwqa' ) ?></label>
- 90.
</p>
- 91.
<p>
- 92.
<input id="<?php echo $this->get_field_id( 'hide_date' ) ?>" name="<?php echo $this->get_field_name( 'hide_date' ) ?>" type="checkbox" <?php echo $instance['hide_date']?'checked':''; ?>> <label for="<?php echo $this->get_field_id( 'hide_date' ) ?>"><?php _e( 'Hide date', 'dwqa' ) ?></label>
- 93.
</p>
- 94.
<?php
- 95.
}
- 96.
}
- 97.
- 98.
?>