defaults = [ 'title' => '', 'post_type' => '', 'taxonomy' => '', 'terms' => [], 'pages' => [], 'number' => 5, 'orderby' => 'date', 'order' => 'DESC', 'styles' => 'list_l', ]; // Get the registered post types. $this->post_types = get_post_types( [ 'public' => true ], 'objects' ); // Set the options for orderby. $this->orderby = [ 'date' => esc_html__( 'Publish Date', 'aamla' ), 'modified' => esc_html__( 'Modified Date', 'aamla' ), 'title' => esc_html__( 'Title', 'aamla' ), 'author' => esc_html__( 'Author', 'aamla' ), 'comment_count' => esc_html__( 'Comment Count', 'aamla' ), 'rand' => esc_html__( 'Random', 'aamla' ), ]; // Setup our AJAX callback. add_action( 'wp_ajax_aamla_display_posts', array( $this, 'ajax_callback' ) ); // Set the widget options. $widget_ops = [ 'classname' => 'display_posts', 'description' => esc_html__( 'Create a display posts widget.', 'aamla' ), 'customize_selective_refresh' => true, ]; parent::__construct( 'aamla_display_posts', esc_html__( 'Display Posts', 'aamla' ), $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 ); $entry_class = apply_filters( 'aamla_dp_classes', $instance['styles'], $instance, $this ); echo $args['before_widget']; // WPCS xss ok. Contains HTML. if ( $title ) { echo $args['before_title'] . $title . $args['after_title']; // WPCS xss ok. Contains HTML. } // 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'], ], ]; } } $query_args = apply_filters( 'aamla_display_posts_args', $query_args, $instance, $this ); $post_query = new \WP_Query( $query_args ); if ( $post_query->have_posts() ) : ?>
label( 'title', esc_html__( 'Title:', 'aamla' ) ); ?>
post_types, 'label', 'name' ); $post_type = array_merge( [ '' => esc_html__( 'None', 'aamla' ) ], $post_type ); $this->label( 'post_type', esc_html__( 'Select Post Type', 'aamla' ) ); $this->select( 'post_type', $post_type, $instance['post_type'] ); ?>
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['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( 'aamla_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', 'aamla' ) ); $this->mu_checkbox( 'pages', $pages, $selected_pages ); } /** * Prints a checkbox list of all terms for a taxonomy. * * @param str $taxonomy Selected taxonomy in widget form. * @param array $selected_terms Selected Terms. * @return void */ public function terms_checklist( $taxonomy, $selected_terms ) { // Get list of all terms for a taxonomy. $terms = get_terms( [ 'taxonomy' => $taxonomy ] ); $terms = wp_list_pluck( $terms, 'name', 'slug' ); // Terms Checkbox markup. $this->label( 'terms', esc_html__( 'Select Terms', 'aamla' ) ); $this->mu_checkbox( 'terms', $terms, $selected_terms ); } /** * Prints select list of all taxonomies for a post type. * * @param str $post_type Selected post type in widget form. * @param str $taxonomy Selected taxonomy in widget form. * @return void */ public function taxonomies_select( $post_type, $taxonomy ) { // Get list of all taxonomies for a post type. $taxonomies = get_object_taxonomies( $post_type, 'objects' ); $taxonomies = wp_list_pluck( $taxonomies, 'label', 'name' ); $taxonomies = array_merge( [ '' => esc_html__( 'Ignore Taxonomy', 'aamla' ) ], $taxonomies ); // Taxonomy Select markup. $this->label( 'taxonomy', esc_html__( 'Select Taxonomy', 'aamla' ) ); $this->select( 'taxonomy', $taxonomies, $taxonomy ); } /** * Setup Ajax callback for this widget. * * @return void */ public function ajax_callback() { // Nounce verification of Ajax request. check_ajax_referer( 'display_posts' ); if ( isset( $_POST['query_context'] ) ) { $queried_context = sanitize_text_field( $_POST['query_context'] ); } else { $queried_context = ''; } if ( isset( $_POST['query_val'] ) ) { $queried_item = sanitize_text_field( $_POST['query_val'] ); } else { $queried_item = ''; } if ( isset( $_POST['widget_id'] ) ) { $widget_id = sanitize_text_field( $_POST['widget_id'] ); } else { $widget_id = ''; } if ( '' === $queried_item || '' === $queried_context ) { echo false; wp_die(); } $widget_id = explode( '_aamla_display_posts-', $widget_id ); $id = $widget_id[1]; $instances = $this->get_settings(); $instance = array_key_exists( $id, $instances ) ? $instances[ $id ] : []; if ( 'posttype' === $queried_context ) { if ( 'page' === $queried_item ) { $pages = isset( $instance['pages'] ) ? $instance['pages'] : []; $this->pages_checklist( $pages ); } elseif ( $queried_item ) { $taxonomy = isset( $instance['taxonomy'] ) ? $instance['taxonomy'] : []; $this->taxonomies_select( $queried_item, $instance['taxonomy'] ); } } elseif ( 'taxonomy' === $queried_context ) { if ( $queried_item ) { $terms = isset( $instance['terms'] ) ? $instance['terms'] : []; $this->terms_checklist( $queried_item, $instance['terms'] ); } } wp_die(); } /** * 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; // WPCS xss ok. Contains HTML. Other values escaped. } 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 bool $echo Display or return. * @return void|string */ public function select( $for, $options, $selected, $echo = true ) { $select = ''; foreach ( $options as $value => $label ) { $select .= sprintf( '', esc_attr( $value ), 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; // WPCS xss ok. Contains HTML. Other values escaped. } 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 bool $echo Display or return. * @return void|string */ public function mu_checkbox( $for, $options, $selected, $echo = true ) { $mu_checkbox = '