defaults = array( 'title' => esc_html__( 'Trending Articles', 'digimag-lite' ), 'tag_name' => '', 'type' => 'slide', 'post_number' => 6, 'speed' => 20, ); parent::__construct( 'digimag-trending', esc_html__( 'Digimag: Trending Articles', 'digimag-lite' ), array( 'classname' => 'digimag-trending to-be-replaced', // This will be replaced by the type. 'description' => esc_html__( 'A widget that displays your trending articles', 'digimag-lite' ), ) ); } /** * How to display the widget on the screen. * * @param array $args Widget parameters. * @param array $instance Widget instance. */ public function widget( $args, $instance ) { $instance = wp_parse_args( $instance, $this->defaults ); $post_number = $instance['post_number']; $title = $instance['title']; $type = $instance['type']; $speed = $instance['speed']; $type = ( 'rotate' === $type ) ? 'rotate is-hidden' : 'slide'; $args['before_widget'] = str_replace( 'to-be-replaced', 'digimag-trending-' . $type, $args['before_widget'] ); $tag = $instance['tag_name']; if ( empty( $tag ) ) { return; } $term = get_term_by( 'name', $tag, 'post_tag' ); if ( $term ) { $tag_id = $term->term_id; } else { $new_tag = wp_insert_term( $tag, 'post_tag' ); $tag_id = ( isset( $new_tag['term_id'] ) ) ? $new_tag['term_id'] : ''; } $arguments = array( 'posts_per_page' => $post_number, 'tag_id' => $tag_id, ); if ( 'slide' === $type ) { $js_class = 'js-trending-slide'; $data_speed = 'data-speed="' . esc_attr( $speed ) . '"'; $button = ''; } else { $js_class = 'js-trending-rotate'; $data_speed = ''; $button = ''; } $query = new WP_Query( $arguments ); if ( $query->have_posts() ) { echo $args['before_widget']; // WPCS: XSS OK. echo $args['before_title'] . $title . $args['after_title']; // WPCS: XSS OK. ?> defaults ); $title = $instance['title']; $tag_name = $instance['tag_name']; $type = $instance['type']; $post_number = absint( $instance['post_number'] ); $speed = absint( $instance['speed'] ); ?>