'italystrap_posts_widget', 'description' => __( 'Displays list of posts with an array of options (DEPRECATED)', 'italystrap' ), ); $control_options = array( 'width' => 450, ); parent::__construct( 'sticky-posts', __( 'ItalyStrap Posts Widget', 'italystrap' ), $widget_options, $control_options ); $this->alt_option_name = 'italystrap_posts_widget'; add_action( 'save_post', array( &$this, 'flush_widget_cache' ) ); add_action( 'deleted_post', array( &$this, 'flush_widget_cache' ) ); add_action( 'switch_theme', array( &$this, 'flush_widget_cache' ) ); // add_action( 'admin_enqueue_scripts', array( &$this, 'enqueue_admin_scripts' ) ); // add_action( 'admin_enqueue_scripts', array( $this, 'upload_scripts' ) ); /** * Valutare se inserire uno stile predefinito. * if (apply_filters('italystrap_enqueue_styles', true) && !is_admin()) { * add_action('wp_enqueue_scripts', array(&$this, 'enqueue_theme_scripts')); * } */ } /** * Widget method * @param array $args Array of widget's arguments. * @param array $instance Array with widget value. */ function widget( $args, $instance ) { global $post; $current_post_id = is_object( $post ) ? $post->ID : ''; $cache = wp_cache_get( 'italystrap_posts_widget', 'widget' ); if ( ! is_array( $cache ) ) $cache = array(); if ( isset( $cache[ $args['widget_id'] ] ) ) { echo $cache[ $args['widget_id'] ]; return; } /** * Da vedere se serve, ma non credo. * ob_start(); */ ob_start(); foreach ( $args as $key => $value) $$key = $value; $class = $instance['class']; $number = empty( $instance['number'] ) ? -1 : $instance['number']; $types = empty( $instance['types'] ) ? 'any' : explode( ',', $instance['types'] ); $cats = empty( $instance['cats'] ) ? '' : explode( ',', $instance['cats'] ); $tags = empty( $instance['tags'] ) ? '' : explode( ',', $instance['tags'] ); $atcat = $instance['atcat'] ? true : false; $thumb_size = $instance['thumb_size']; $attag = $instance['attag'] ? true : false; $excerpt_length = $instance['excerpt_length']; $excerpt_readmore = $instance['excerpt_readmore']; $sticky = $instance['sticky']; $order = $instance['order']; $orderby = $instance['orderby']; $meta_key = $instance['meta_key']; $custom_fields = $instance['custom_fields']; /** * Sticky posts. */ if ( 'only' === $sticky ) { $sticky_query = array( 'post__in' => get_option( 'sticky_posts' ) ); } elseif ( 'hide' === $sticky ) { $sticky_query = array( 'post__not_in' => get_option( 'sticky_posts' ) ); } else { $sticky_query = null; } /** * If $atcat true and in category */ if ( $atcat && is_category() ) { $cats = get_query_var( 'cat' ); } /** * If $atcat true and is single post */ if ( $atcat && is_single() ) { $cats = ''; foreach ( get_the_category() as $catt ) { $cats .= $catt->term_id.' '; } $cats = str_replace( ' ', ',', trim( $cats ) ); } /** * If $attag true and in tag */ if ( $attag && is_tag() ) { $tags = get_query_var( 'tag_id' ); } /** * If $attag true and is single post */ if ( $attag && is_single() ) { $tags = ''; $thetags = get_the_tags(); if ( $thetags ) { foreach ( $thetags as $tagg ) { $tags .= $tagg->term_id . ' '; } } $tags = str_replace( ' ', ',', trim( $tags ) ); } /** * Excerpt more filter * @var function */ $new_excerpt_more = create_function( '$more', 'return "...";' ); add_filter( 'excerpt_more', $new_excerpt_more ); // Excerpt length filter $new_excerpt_length = create_function( '$length', 'return ' . $excerpt_length . ';' ); if ( $instance['excerpt_length'] > 0 ) add_filter( 'excerpt_length', $new_excerpt_length ); if ( $class ) { $before_widget = str_replace( 'class="', 'class="'. $class . ' ', $before_widget ); } echo $before_widget; $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); if ( $title && $instance['title_link'] ) echo $before_title . apply_filters( 'italystrap_widget_title_link', '' . esc_attr( $title ) . '', $instance['title_link'], $title ) . $after_title; elseif ( $title && ! $instance['title_link'] ) echo $before_title . esc_attr( $title ) . $after_title; /** * Arguments for WP_Query * @var array */ $args = array( 'posts_per_page' => $number, 'order' => $order, 'orderby' => $orderby, 'category__in' => $cats, 'tag__in' => $tags, 'post_type' => $types, ); if ( 'meta_value' === $orderby ) { $args['meta_key'] = $meta_key; } if ( ! empty( $sticky_query ) ) { $args[ key( $sticky_query ) ] = reset( $sticky_query ); } $args = apply_filters( 'italystrap_wp_query_args', $args, $instance, $this->id_base ); $widget_post_query = new WP_Query( $args ); if ( 'custom' === $instance['template'] ) { $custom_template_path = apply_filters( 'italystrap_custom_template_path', '/templates/' . $instance['template_custom'] . '.php', $instance, $this->id_base ); if ( locate_template( $custom_template_path ) ) { include get_stylesheet_directory() . $custom_template_path; } else { include 'templates/standard.php'; } } elseif ( 'standard' === $instance['template'] ) { // include( ITALYSTRAP_PLUGIN_PATH . 'templates/standard.php' ); include( ITALYSTRAP_PLUGIN_PATH . 'templates/legacy.php' ); // include get_template( '/templates/content-post.php' ); } else { include 'templates/legacy.php'; } // Reset the global $the_post as this query will have stomped on it. wp_reset_postdata(); echo $after_widget; if ( $cache ) { $cache[ $args['widget_id'] ] = ob_get_flush(); } wp_cache_set( 'italystrap_posts_widget', $cache, 'widget' ); } /** * Update method * @param array $new_instance Array of value. * @param array $old_instance Array with old value. * @return array Return the array updated */ function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = strip_tags( $new_instance['title'] ); $instance['class'] = strip_tags( $new_instance['class'] ); $instance['title_link'] = strip_tags( $new_instance['title_link'] ); $instance['number'] = strip_tags( $new_instance['number'] ); $instance['types'] = (isset( $new_instance['types'] )) ? implode( ',', (array) $new_instance['types'] ) : ''; $instance['cats'] = (isset( $new_instance['cats'] )) ? implode( ',', (array) $new_instance['cats'] ) : ''; $instance['tags'] = (isset( $new_instance['tags'] )) ? implode( ',', (array) $new_instance['tags'] ) : ''; $instance['atcat'] = isset( $new_instance['atcat'] ); $instance['attag'] = isset( $new_instance['attag'] ); $instance['show_excerpt'] = isset( $new_instance['show_excerpt'] ); $instance['show_content'] = isset( $new_instance['show_content'] ); $instance['show_thumbnail'] = isset( $new_instance['show_thumbnail'] ); $instance['show_date'] = isset( $new_instance['show_date'] ); $instance['date_format'] = strip_tags( $new_instance['date_format'] ); $instance['show_title'] = isset( $new_instance['show_title'] ); $instance['show_author'] = isset( $new_instance['show_author'] ); $instance['show_comments'] = isset( $new_instance['show_comments'] ); $instance['thumb_size'] = strip_tags( $new_instance['thumb_size'] ); $instance['show_readmore'] = isset( $new_instance['show_readmore'] ); $instance['excerpt_length'] = strip_tags( $new_instance['excerpt_length'] ); $instance['excerpt_readmore'] = strip_tags( $new_instance['excerpt_readmore'] ); $instance['sticky'] = $new_instance['sticky']; $instance['order'] = $new_instance['order']; $instance['orderby'] = $new_instance['orderby']; $instance['meta_key'] = $new_instance['meta_key']; $instance['show_cats'] = isset( $new_instance['show_cats'] ); $instance['show_tags'] = isset( $new_instance['show_tags'] ); $instance['custom_fields'] = strip_tags( $new_instance['custom_fields'] ); $instance['template'] = strip_tags( $new_instance['template'] ); $instance['template_custom'] = strip_tags( $new_instance['template_custom'] ); $instance['thumb_url'] = sanitize_text_field( $new_instance['thumb_url'] ); if ( current_user_can( 'unfiltered_html' ) ) { $instance['before_posts'] = $new_instance['before_posts']; $instance['after_posts'] = $new_instance['after_posts']; } else { $instance['before_posts'] = wp_filter_post_kses( $new_instance['before_posts'] ); $instance['after_posts'] = wp_filter_post_kses( $new_instance['after_posts'] ); } $this->flush_widget_cache(); $alloptions = wp_cache_get( 'alloptions', 'options' ); if ( isset( $alloptions['italystrap_posts_widget'] ) ) delete_option( 'italystrap_posts_widget' ); return $instance; } /** * Widget from for admin area * @param array $instance Array of value */ function form( $instance ) { /** * Set default arguments * @var array */ $instance = wp_parse_args( (array) $instance, array( 'title' => __( 'ItalyStrap Widget Posts', 'italystrap' ), 'class' => '', 'title_link' => '', 'number' => '5', 'types' => 'post', 'cats' => '', 'tags' => '', 'atcat' => false, 'thumb_size' => 'thumbnail', 'attag' => false, 'excerpt_length' => 10, 'excerpt_readmore' => __( 'Read more →', 'italystrap' ), 'order' => 'DESC', 'orderby' => 'date', 'meta_key' => '', 'sticky' => 'show', 'show_cats' => false, 'show_tags' => false, 'show_title' => true, 'show_date' => true, 'date_format' => get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), 'show_author' => true, 'show_comments' => false, 'show_excerpt' => true, 'show_content' => false, 'show_readmore' => true, 'show_thumbnail' => true, 'thumb_url' => '', 'custom_fields' => '', // Set template to 'legacy' if field from UPW < 2.0 is set. 'template' => empty( $instance['morebutton_text'] ) ? 'standard' : 'legacy', 'template_custom' => '', 'before_posts' => '', 'after_posts' => '', ) ); // Or use the instance $title = strip_tags( $instance['title'] ); $class = strip_tags( $instance['class'] ); $title_link = strip_tags( $instance['title_link'] ); $number = strip_tags( $instance['number'] ); $types = $instance['types']; $cats = $instance['cats']; $tags = $instance['tags']; $atcat = $instance['atcat']; $thumb_size = $instance['thumb_size']; $attag = $instance['attag']; $excerpt_length = strip_tags( $instance['excerpt_length'] ); $excerpt_readmore = strip_tags( $instance['excerpt_readmore'] ); $order = $instance['order']; $orderby = $instance['orderby']; $meta_key = $instance['meta_key']; $sticky = $instance['sticky']; $show_cats = $instance['show_cats']; $show_tags = $instance['show_tags']; $show_title = $instance['show_title']; $show_date = $instance['show_date']; $date_format = $instance['date_format']; $show_author = $instance['show_author']; $show_comments = $instance['show_comments']; $show_excerpt = $instance['show_excerpt']; $show_content = $instance['show_content']; $show_readmore = $instance['show_readmore']; $show_thumbnail = $instance['show_thumbnail']; $thumb_url = $instance['thumb_url']; $custom_fields = strip_tags( $instance['custom_fields'] ); $template = $instance['template']; $template_custom = strip_tags( $instance['template_custom'] ); $before_posts = format_to_edit( $instance['before_posts'] ); $after_posts = format_to_edit( $instance['after_posts'] ); // Let's turn $types, $cats, and $tags into an array if they are set. if ( ! empty( $types )) $types = explode( ',', $types ); if ( ! empty( $cats )) $cats = explode( ',', $cats ); if ( ! empty( $tags )) $tags = explode( ',', $tags ); // Count number of post types for select box sizing. $cpt_types = get_post_types( array( 'public' => true ), 'names' ); if ( $cpt_types ) { foreach ( $cpt_types as $cpt ) { $cpt_ar[] = $cpt; } $n = count( $cpt_ar ); if ( $n > 6 ) { $n = 6; } } else { $n = 3; } // Count number of categories for select box sizing. $cat_list = get_categories( 'hide_empty=0' ); if ( $cat_list ) { foreach ( $cat_list as $cat ) { $cat_ar[] = $cat; } $c = count( $cat_ar ); if ( $c > 6 ) { $c = 6; } } else { $c = 3; } // Count number of tags for select box sizing. $tag_list = get_tags( 'hide_empty=0' ); if ( $tag_list ) { foreach ( $tag_list as $tag ) { $tag_ar[] = $tag; } $t = count( $tag_ar ); if ( $t > 6 ) { $t = 6; } } else { $t = 3; } ?>

>

/>

/>

>

/>

/>

/>

>

/>

>

>

/>

>

/>

/>

/>

/>

>