widget_cssclass = 'widget_bloglex_grid_posts'; $this->widget_description = __("Displays grid posts with an image", 'bloglex'); $this->widget_id = 'bloglex_grid_posts'; $this->widget_name = __('Bloglex: Grid Posts', 'bloglex'); $this->settings = $this->get_widget_settings(); parent::__construct(); } /** * Define widget settings. */ protected function get_widget_settings() { return array( 'title' => array( 'type' => 'text', 'label' => __('Title', 'bloglex'), 'std' => __('Grid Posts', 'bloglex'), ), 'style' => array( 'type' => 'select', 'label' => __('Style', 'bloglex'), 'options' => array( 'wpi-post-regular' => __('Regular View', 'bloglex'), 'wpi-post-list' => __('List View', 'bloglex'), 'wpi-post-card' => __('Card View', 'bloglex'), ), 'std' => 'wpi-post-list', ), 'category' => array( 'type' => 'dropdown-taxonomies', 'label' => __('Select Category', 'bloglex'), 'args' => array( 'taxonomy' => 'category', 'class' => 'widefat', 'hierarchical' => true, 'show_count' => 1, 'show_option_all' => __('— Select —', 'bloglex'), ), ), 'number' => array( 'type' => 'number', 'step' => 1, 'min' => 1, 'std' => 5, 'label' => __('Number of posts to show', 'bloglex'), ), 'offset' => array( 'type' => 'number', 'step' => 1, 'min' => 0, 'max' => '', 'std' => '', 'label' => __( 'Offset', 'bloglex' ), 'desc' => __( 'Offsets are used to skip a certain number of WordPress posts before starting output. Set it to 0 if you do not wish to use this feature.', 'bloglex' ), ), 'orderby' => array( 'type' => 'select', 'std' => 'date', 'label' => __('Order by', 'bloglex'), 'options' => array( 'date' => __('Date', 'bloglex'), 'ID' => __('ID', 'bloglex'), 'title' => __('Title', 'bloglex'), 'rand' => __('Random', 'bloglex'), ), ), 'order' => array( 'type' => 'select', 'std' => 'desc', 'label' => __('Order', 'bloglex'), 'options' => array( 'asc' => __('ASC', 'bloglex'), 'desc' => __('DESC', 'bloglex'), ), ), 'show_image' => array( 'type' => 'checkbox', 'label' => __('Show Image', 'bloglex'), 'std' => true, ), 'image_size' => array( 'type' => 'select', 'label' => __('Image Size', 'bloglex'), 'options' => array( 'thumbnail' => __('Thumbnail', 'bloglex'), 'medium' => __('Medium', 'bloglex'), 'medium_large' => __('Medium Large', 'bloglex'), 'large' => __('Large', 'bloglex'), 'full' => __('Full', 'bloglex'), ), 'std' => 'medium', ), 'show_date' => array( 'type' => 'checkbox', 'label' => __('Show Date', 'bloglex'), 'std' => true, ), 'date_format' => array( 'type' => 'select', 'label' => __('Date Format', 'bloglex'), 'options' => array( 'format_1' => __('Format 1', 'bloglex'), 'format_2' => __('Format 2', 'bloglex'), ), 'std' => 'format_1', ), ); } /** * Query the posts and return them. */ protected function get_posts($args, $instance) { $query_args = array( 'posts_per_page' => !empty($instance['number']) ? absint($instance['number']) : $this->settings['number']['std'], 'post_status' => 'publish', 'no_found_rows' => 1, 'orderby' => !empty($instance['orderby']) ? sanitize_text_field($instance['orderby']) : $this->settings['orderby']['std'], 'order' => !empty($instance['order']) ? sanitize_text_field($instance['order']) : $this->settings['order']['std'], 'offset' => !empty($instance['offset']) ? sanitize_text_field($instance['offset']) : $this->settings['offset']['std'], 'ignore_sticky_posts' => 1 ); if ( isset($instance['offset']) && absint($instance['offset']) != 0 ) { $query_args['offset'] = absint($instance['offset']); } if (!empty($instance['category']) && -1 != $instance['category'] && 0 != $instance['category']) { $query_args['tax_query'][] = array( 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => $instance['category'], ); } return new WP_Query(apply_filters('bloglex_grid_posts_query_args', $query_args)); } /** * Output widget content. */ public function widget($args, $instance) { $posts = $this->get_posts($args, $instance); if (!$posts->have_posts()) { return; } echo $args['before_widget']; do_action('bloglex_before_grid_posts_with_image'); if (!empty($instance['title'])) { echo $args['before_title'] . esc_html($instance['title']) . $args['after_title']; } echo '
'; do_action('bloglex_after_grid_posts_with_image'); echo $args['after_widget']; } /** * Render a single post item. */ protected function render_post($instance) { $style = !empty($instance['style']) ? $instance['style'] : $this->settings['style']['std']; $image_size = !empty($instance['image_size']) ? $instance['image_size'] : $this->settings['image_size']['std']; ?>