'alurra-recent-posts', 'description' => esc_html__( 'Use this widget to list your recent posts with thumbnails.', 'alurra' ), ) ); $this->orderby = array( 'date' => esc_html__( 'date', 'alurra' ), 'comments_num' => esc_html__( 'number of comments', 'alurra' ), 'random' => esc_html__( 'random', 'alurra' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); } /** * Enqueue admin widgets page scripts and styles. * * @access public */ public function admin_enqueue_scripts( $hook ) { if ( 'widgets.php' != $hook ) return; } /** * Output the HTML for this widget. * * @access public * * @param array $args An array of standard parameters for widgets in this theme. * @param array $instance An array of settings for this widget instance. */ public function widget( $args, $instance ) { $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5; $title = ( ! empty( $instance['title'] ) ) ? strip_tags( $instance['title'] ) : esc_html__( 'Alurra Recent Posts', 'alurra' ); $query_args = array( 'order' => 'DESC', 'posts_per_page' => $number, 'no_found_rows' => true, 'post_type' => 'post', 'post_status' => 'publish', 'post__not_in' => get_option( 'sticky_posts' ) ); $query_args['tax_query'] = array(); if( ! empty( $instance['category'] ) ){ $query_args['tax_query'][] = array( 'taxonomy' => 'category', 'terms' => absint( $instance['category'] ), 'field' => 'term_id' ); } if( ! empty( $instance['tags'] ) ) { $tags = explode( ',', $instance['tags'] ); $query_args['tax_query'][] = array( 'taxonomy' => 'post_tag', 'terms' => $tags, 'field' => 'name' ); } if( count( $query_args['tax_query'] ) > 1 ){ $query_args['tax_query']['relation'] = 'AND'; } $orderby = isset( $instance['orderby'] ) ? $instance['orderby'] : 'date'; switch ($orderby) { case 'comments_num': $query_args['orderby'] = 'comment_count'; break; case 'random': $query_args['orderby'] = 'rand'; break; } $recent_posts = new WP_Query( $query_args ); if ( $recent_posts->have_posts() ) : $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false; echo $args['before_widget']; if ( ! empty( $title ) ) { printf( '%s%s%s', $args['before_title'], $title, $args['after_title'] ); } ?> orderby ) ){ $instance['orderby'] = $new_instance['orderby']; } $instance['category'] = isset( $new_instance['category'] ) ? absint( $new_instance['category'] ) : 0; $instance['tags'] = strip_tags( $new_instance['tags'] ); $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false; return $instance; } /** * Display the form for this widget on the Widgets page of the Admin area. * * @param array $instance */ function form( $instance ) { $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'number' => 5, 'orderby' => 'date', 'category' => 0, 'tags' => '', 'show_date' => true ) ); ?>

esc_html__( 'All Categories', 'alurra' ), 'selected' => absint( $instance['category'] ), 'name' => esc_attr( $this->get_field_name( 'category' ) ), 'id' => esc_attr( $this->get_field_id( 'category' ) ), 'class' => 'widefat' ) ); ?>

id="get_field_id( 'show_date' ) ); ?>" name="get_field_name( 'show_date' ) ); ?>" />