widget_cssclass = 'widget_blogmarks_recent_posts'; $this->widget_description = __( 'Displays recent posts with an image', 'blogmarks' ); $this->widget_id = 'blogmarks_recent_posts_with_image'; $this->widget_name = __( 'Blogmarks: Recent Posts', 'blogmarks' ); $this->settings = array( 'title' => array( 'type' => 'text', 'label' => __( 'Title', 'blogmarks' ), ), 'post_settings_heading' => array( 'type' => 'heading', 'label' => __( 'Post Settings', 'blogmarks' ), ), 'category' => array( 'type' => 'dropdown-taxonomies', 'label' => __( 'Select Category', 'blogmarks' ), 'desc' => __( 'Leave empty if you don\'t want the posts to be category specific', 'blogmarks' ), 'args' => array( 'taxonomy' => 'category', 'class' => 'widefat', 'hierarchical' => true, 'show_count' => 1, 'show_option_all' => __( '— Select —', 'blogmarks' ), ), ), 'number' => array( 'type' => 'number', 'step' => 1, 'min' => 1, 'max' => '', 'std' => 3, 'label' => __( 'Number of posts to show', 'blogmarks' ), ), 'offset' => array( 'type' => 'number', 'step' => 1, 'min' => 0, 'max' => '', 'std' => '', 'label' => __( 'Offset', 'blogmarks' ), 'desc' => __( 'Can be useful if you want to skip certain number of posts. Leave as 0 if you do not want to use it.', 'blogmarks' ), ), 'orderby' => array( 'type' => 'select', 'std' => 'date', 'label' => __( 'Order by', 'blogmarks' ), 'options' => array( 'date' => __( 'Date', 'blogmarks' ), 'ID' => __( 'ID', 'blogmarks' ), 'title' => __( 'Title', 'blogmarks' ), 'rand' => __( 'Random', 'blogmarks' ), ), ), 'order' => array( 'type' => 'select', 'std' => 'desc', 'label' => __( 'Order', 'blogmarks' ), 'options' => array( 'asc' => __( 'ASC', 'blogmarks' ), 'desc' => __( 'DESC', 'blogmarks' ), ), ), 'meta_settings_heading' => array( 'type' => 'heading', 'label' => __( 'Post Meta Settings', 'blogmarks' ), ), 'post_meta' => array( 'type' => 'multi-checkbox', 'label' => __( 'Post Meta', 'blogmarks' ), 'options' => array( 'author' => __( 'Author', 'blogmarks' ), 'read_time' => __( 'Post Read Time', 'blogmarks' ), 'date' => __( 'Date', 'blogmarks' ), 'comment' => __( 'Comment', 'blogmarks' ), ), 'std' => array( 'date' ), ), 'post_meta_icon' => array( 'type' => 'checkbox', 'label' => __( 'Show Post Meta Icon', 'blogmarks' ), 'desc' => __( 'Some Icons may show up regardless to provide better info.', 'blogmarks' ), 'std' => true, ), 'date_format' => array( 'type' => 'select', 'label' => __( 'Date Format', 'blogmarks' ), 'desc' => __( 'Make sure to select Date from above for this to work.', 'blogmarks' ), 'options' => array( 'format_1' => __( 'Times Ago', 'blogmarks' ), 'format_2' => __( 'Default Format', 'blogmarks' ), ), 'std' => 'format_1', ), 'author_image' => array( 'type' => 'checkbox', 'label' => __( 'Show Author Image', 'blogmarks' ), 'desc' => __( 'Make sure to select Author from above for this to work.', 'blogmarks' ), 'std' => false, ), 'widget_settings_heading' => array( 'type' => 'heading', 'label' => __( 'Widget Settings', 'blogmarks' ), ), 'style' => array( 'type' => 'select', 'label' => __( 'Style', 'blogmarks' ), 'options' => array( 'style_1' => __( 'Default', 'blogmarks' ), 'style_2' => __( 'Circled Image', 'blogmarks' ), ), 'std' => 'style_1', ), 'enable_post_format_icon' => array( 'type' => 'checkbox', 'label' => __( 'Enable Post Format Icon', 'blogmarks' ), 'std' => false, ), 'invert_list_post' => array( 'type' => 'checkbox', 'label' => __( 'Invert List Post', 'blogmarks' ), 'std' => false, ), 'inverted_block_color' => array( 'type' => 'checkbox', 'label' => __( 'Inverted Color', 'blogmarks' ), 'desc' => __( 'Can be used if you have dark background and want lighter color on the text.', 'blogmarks' ), 'std' => false, ), 'title_limit' => array( 'type' => 'select', 'label' => __( 'Title Line Limit', 'blogmarks' ), 'options' => blogmarks_get_title_limit_choices(), 'std' => '', ), 'enable_item_separator' => array( 'type' => 'checkbox', 'label' => __( 'Enable Item Separator', 'blogmarks' ), 'std' => false, ), 'hide_list_image' => array( 'type' => 'checkbox', 'label' => __( 'Hide List Post Image', 'blogmarks' ), 'std' => false, ), ); parent::__construct(); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) ); } /** * Query the posts and return them. * * @param array $args * @param array $instance * @return WP_Query */ public function get_posts( $args, $instance ) { $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std']; $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']; $query_args = array( 'posts_per_page' => $number, 'post_status' => 'publish', 'no_found_rows' => 1, 'orderby' => $orderby, 'order' => $order, 'ignore_sticky_posts' => 1, ); if ( $offset && 0 != $offset ) { $query_args['offset'] = absint( $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( 'blogmarks_recent_posts_query_args', $query_args ) ); } /** * Output widget. * * @see WP_Widget * * @param array $args * @param array $instance */ public function widget( $args, $instance ) { ob_start(); if ( ( $posts = $this->get_posts( $args, $instance ) ) && $posts->have_posts() ) { $this->widget_start( $args, $instance ); do_action( 'blogmarks_before_recent_posts_with_image' ); $class = ''; $style = isset( $instance['style'] ) ? $instance['style'] : $this->settings['style']['std']; $widget_class = $style; $enable_post_format_icon = isset( $instance['enable_post_format_icon'] ) ? $instance['enable_post_format_icon'] : $this->settings['enable_post_format_icon']['std']; $title_limit = isset( $instance['title_limit'] ) ? $instance['title_limit'] : $this->settings['title_limit']['std']; $enabled_post_meta = isset( $instance['post_meta'] ) ? $instance['post_meta'] : $this->settings['post_meta']['std']; $meta_settings['date_format'] = isset( $instance['date_format'] ) ? $instance['date_format'] : $this->settings['date_format']['std']; $meta_settings['author_image'] = isset( $instance['author_image'] ) ? $instance['author_image'] : $this->settings['author_image']['std']; $meta_settings['show_icons'] = isset( $instance['post_meta_icon'] ) ? $instance['post_meta_icon'] : $this->settings['post_meta_icon']['std']; // Inverted Item. $invert_list_post = isset( $instance['invert_list_post'] ) ? $instance['invert_list_post'] : $this->settings['invert_list_post']['std']; if ( $invert_list_post ) { $widget_class .= ' has-inverted-items'; } // Item Separator. $item_separator = isset( $instance['enable_item_separator'] ) ? $instance['enable_item_separator'] : $this->settings['enable_item_separator']['std']; if ( $item_separator ) { $widget_class .= ' wpintf-item-sep'; } $inverted_block_color = isset( $instance['inverted_block_color'] ) ? $instance['inverted_block_color'] : $this->settings['inverted_block_color']['std']; // Hide List Image. $show_image = true; $hide_list_image = isset( $instance['hide_list_image'] ) ? $instance['hide_list_image'] : $this->settings['hide_list_image']['std']; if ( $hide_list_image ) { $widget_class .= ' wpintf-hidden-post-image'; $show_image = false; } if ( 'style_1' === $style ) { $class = ' image-hover-zoom'; } // Inverted Color. if ( $inverted_block_color ) { $widget_class .= ' widget-inverted-scheme'; } ?>
widget_end( $args ); } echo ob_get_clean(); } public function enqueue_assets() { if ( is_active_widget( false, false, $this->id_base ) ) { $file_prefix = is_rtl() ? '-rtl' : ''; $css_file = get_template_directory() . '/include/widgets/assets/recent-posts' . $file_prefix . '.css'; if ( file_exists( $css_file ) ) { $styles = wp_strip_all_tags( file_get_contents( $css_file ) ); wp_add_inline_style( 'blogmarks-style', $styles ); } } } }