defaults = [ 'title' => '', 'post_type' => '', 'taxonomy' => '', 'terms' => [], 'post_ids' => '', 'pages' => [], 'number' => 5, 'orderby' => 'date', 'order' => 'DESC', 'styles' => 'grid-view1', 'grid_columns' => 1, ]; // Set the options for orderby. $this->orderby = [ 'date' => esc_html__( 'Publish Date', 'bayleaf' ), 'modified' => esc_html__( 'Modified Date', 'bayleaf' ), 'title' => esc_html__( 'Title', 'bayleaf' ), 'author' => esc_html__( 'Author', 'bayleaf' ), 'comment_count' => esc_html__( 'Comment Count', 'bayleaf' ), 'rand' => esc_html__( 'Random', 'bayleaf' ), ]; // Set the widget options. $widget_ops = [ 'classname' => 'display_posts', 'description' => esc_html__( 'Create a display posts widget.', 'bayleaf' ), 'customize_selective_refresh' => true, ]; parent::__construct( 'bayleaf_display_posts', esc_html__( 'Display Posts', 'bayleaf' ), $widget_ops ); } /** * Outputs the content for the current widget instance. * * @since 2.8.0 * * @param array $args Display arguments including 'before_title', 'after_title', * 'before_widget', and 'after_widget'. * @param array $instance Settings for the current widget instance. */ public function widget( $args, $instance ) { $args['widget_id'] = isset( $args['widget_id'] ) ? $args['widget_id'] : $this->id; // Merge with defaults. $instance = wp_parse_args( (array) $instance, $this->defaults ); $title = ! empty( $instance['title'] ) ? $instance['title'] : ''; /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); $wrapper_class = apply_filters( 'bayleaf_dp_wrapper_classes', [ $instance['styles'] ], $instance, $this ); $wrapper_class = array_map( 'esc_attr', $wrapper_class ); $after_title = apply_filters( 'bayleaf_after_dp_widget_title', $args['after_title'], $instance ); echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped if ( $title ) { echo $args['before_title'] . $title . $after_title; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } // Prepare the query. $query_args = []; if ( ! $instance['post_type'] ) { return; } elseif ( 'page' === $instance['post_type'] ) { $query_args = [ 'post_type' => 'page', 'post__in' => $instance['pages'], 'post_status' => 'publish', 'ignore_sticky_posts' => true, 'no_found_rows' => true, ]; } else { $query_args = [ 'post_type' => $instance['post_type'], 'post_status' => 'publish', 'ignore_sticky_posts' => true, 'no_found_rows' => true, 'posts_per_page' => $instance['number'], 'orderby' => $instance['orderby'], 'order' => $instance['order'], ]; if ( $instance['taxonomy'] ) { $query_args['tax_query'] = [ [ 'taxonomy' => $instance['taxonomy'], 'field' => 'slug', 'terms' => $instance['terms'], ], ]; } if ( $instance['post_ids'] ) { $query_args['post__in'] = explode( ',', $instance['post_ids'] ); } } $query_args = apply_filters( 'bayleaf_display_posts_args', $query_args, $instance, $this ); $post_query = new \WP_Query( $query_args ); if ( $post_query->have_posts() ) : $action_args = [ 'instance' => $instance, 'query' => $post_query, ]; ?>
label( 'title', esc_html__( 'Title:', 'bayleaf' ) ); ?>
post_types, 'label', 'name' ); $post_type = array_merge( [ '' => esc_html__( 'None', 'bayleaf' ) ], $post_type ); $this->label( 'post_type', esc_html__( 'Select Post Type', 'bayleaf' ) ); $this->select( 'post_type', $post_type, $instance['post_type'] ); ?>
post_types ) ) { // Get the registered post types. $this->post_types = get_post_types( [ 'public' => true ], 'objects' ); } $valid_post_types = wp_list_pluck( $this->post_types, 'name' ); $instance['post_type'] = in_array( $new_instance['post_type'], $valid_post_types, true ) ? $new_instance['post_type'] : ''; if ( 'page' === $instance['post_type'] ) { // Get list of all pages. $pages = get_pages( [ 'exclude' => get_option( 'page_for_posts' ) ] ); $valid_pages = wp_list_pluck( $pages, 'ID' ); $instance['pages'] = array_intersect( $new_instance['pages'], $valid_pages ); $instance['taxonomy'] = ''; } else { $instance['pages'] = []; } if ( $instance['post_type'] && 'page' !== $instance['post_type'] && $new_instance['post_ids'] ) { $post_ids = array_map( 'absint', explode( ',', $new_instance['post_ids'] ) ); $instance['post_ids'] = implode( ',', $post_ids ); } else { $instance['post_ids'] = ''; } if ( $instance['post_type'] && 'page' !== $instance['post_type'] && $new_instance['taxonomy'] ) { // Get list of all taxonomies for a post type. $taxonomies = get_object_taxonomies( $instance['post_type'], 'objects' ); $taxonomies = wp_list_pluck( $taxonomies, 'label', 'name' ); $instance['taxonomy'] = array_key_exists( $new_instance['taxonomy'], $taxonomies ) ? $new_instance['taxonomy'] : ''; } else { $instance['taxonomy'] = ''; } if ( $instance['taxonomy'] && $new_instance['terms'] ) { // Get list of all terms. $terms = get_terms( [ 'taxonomy' => $instance['taxonomy'] ] ); $terms = wp_list_pluck( $terms, 'name', 'slug' ); $valid_terms = array_keys( $terms ); $instance['terms'] = array_intersect( $new_instance['terms'], $valid_terms ); } else { $instance['terms'] = []; } $instance['number'] = absint( $new_instance['number'] ); $instance['orderby'] = ( array_key_exists( $new_instance['orderby'], $this->orderby ) ) ? $new_instance['orderby'] : 'date'; $instance['order'] = ( 'DESC' === $new_instance['order'] ) ? 'DESC' : 'ASC'; $valid_styles = apply_filters( 'bayleaf_dp_styles', '', $new_instance ); $instance['styles'] = array_key_exists( $new_instance['styles'], $valid_styles ) ? $new_instance['styles'] : ''; return $instance; } /** * Prints a checkbox list of all pages. * * @param array $selected_pages Checked pages. * @return void */ public function pages_checklist( $selected_pages ) { // Get list of all pages. $pages = get_pages( [ 'exclude' => get_option( 'page_for_posts' ) ] ); $pages = wp_list_pluck( $pages, 'post_title', 'ID' ); $this->label( 'pages', esc_html__( 'Select Pages', 'bayleaf' ) ); $this->mu_checkbox( 'pages', $pages, $selected_pages ); } /** * Prints a checkbox list of all terms for a taxonomy. * * @param str $taxonomy Selected Taxonomy. * @param array $selected_terms Selected Terms. * @return void */ public function terms_checklist( $taxonomy, $selected_terms = [] ) { // Get list of all registered terms. $terms = get_terms(); // Get 'checkbox' options as value => label. $options = wp_list_pluck( $terms, 'name', 'slug' ); // Get HTML classes for checkbox options. $classes = wp_list_pluck( $terms, 'taxonomy', 'slug' ); if ( $taxonomy ) { foreach ( $classes as $slug => $taxon ) { if ( $taxonomy !== $taxon ) { $classes[ $slug ] .= ' bayleaf-hidden'; } } } // Terms Checkbox markup. $this->label( 'terms', esc_html__( 'Select Terms', 'bayleaf' ) ); $this->mu_checkbox( 'terms', $options, $selected_terms, $classes ); } /** * Prints select list of all taxonomies for a post type. * * @param str $post_type Selected post type. * @param array $selected Selected taxonomy in widget form. * @return void */ public function taxonomies_select( $post_type, $selected = [] ) { // Get list of all registered taxonomies. $taxonomies = get_taxonomies( [], 'objects' ); // Get 'select' options as value => label. $options = wp_list_pluck( $taxonomies, 'label', 'name' ); $options = array_merge( [ '' => esc_html__( 'Ignore Taxonomy', 'bayleaf' ) ], $options ); // Get HTML classes for select options. $classes = wp_list_pluck( $taxonomies, 'object_type', 'name' ); if ( $post_type && 'page' !== $post_type ) { foreach ( $classes as $name => $type ) { $type = (array) $type; if ( ! in_array( $post_type, $type, true ) ) { $type[] = 'bayleaf-hidden'; $classes[ $name ] = $type; } } } $classes[''] = 'always-visible'; // Taxonomy Select markup. $this->label( 'taxonomy', esc_html__( 'Select Taxonomy', 'bayleaf' ) ); $this->select( 'taxonomy', $options, $selected, $classes ); } /** * Markup for 'label' for widget input options. * * @param str $for Label for which ID. * @param str $text Label text. * @param bool $echo Display or Return. * @return void|string */ public function label( $for, $text, $echo = true ) { $label = sprintf( '', esc_attr( $this->get_field_id( $for ) ), esc_html( $text ) ); if ( $echo ) { echo $label; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } else { return $label; } } /** * Markup for Select dropdown lists for widget options. * * @param str $for Select for which ID. * @param array $options Select options as 'value => label' pair. * @param str $selected selected option. * @param array $classes Options HTML classes. * @param bool $echo Display or return. * @return void|string */ public function select( $for, $options, $selected, $classes = [], $echo = true ) { $select = ''; $final_class = ''; foreach ( $options as $value => $label ) { if ( isset( $classes[ $value ] ) ) { $option_classes = (array) $classes[ $value ]; $option_classes = array_map( 'esc_attr', $option_classes ); $final_class = 'class="' . join( ' ', $option_classes ) . '"'; } $select .= sprintf( '', esc_attr( $value ), $final_class, selected( $value, $selected, false ), esc_html( $label ) ); } $select = sprintf( '', esc_attr( $this->get_field_id( $for ) ), esc_attr( $this->get_field_name( $for ) ), esc_attr( str_replace( '_', '-', $for ) ), $select ); if ( $echo ) { echo $select; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } else { return $select; } } /** * Markup for multiple checkbox for widget options. * * @param str $for Select for which ID. * @param array $options Select options as 'value => label' pair. * @param str $selected selected option. * @param array $classes Checkbox input HTML classes. * @param bool $echo Display or return. * @return void|string */ public function mu_checkbox( $for, $options, $selected, $classes = [], $echo = true ) { $final_class = ''; $mu_checkbox = '