'DESC', 'name' => __( 'DESC', 'ultimate-post' ), ), array( 'id' => 'ASC', 'name' => __( 'ASC', 'ultimate-post' ), ), ); } /** * Order by Value for select * * @var array */ public $order_by = array( array( 'id' => 'date', 'name' => 'Created Date', ), array( 'id' => 'modified', 'name' => 'Date Modified', ), array( 'id' => 'title', 'name' => 'Title', ), array( 'id' => 'menu_order', 'name' => 'Menu Order', ), array( 'id' => 'rand', 'name' => 'Random', ), // array( // 'id' => 'post__in', // 'name' => 'Post In', // ), array( 'id' => 'comment_count', 'name' => 'Number of Comments', ), ); /** * Filter_select_options * * @param MIXED | $options (ARRAY). * @param mixed | $filter (ARRAY). * @return array */ private function filter_select_options( $options, $filter ) { $res = array(); $map = array(); foreach ( $options as $option ) { $map[ $option['id'] ] = $option['name']; } foreach ( $filter as $f ) { if ( isset( $map[ $f ] ) ) { $res[] = array( 'id' => strtolower( $f ), 'name' => $map[ $f ], ); } } return $res; } /** * Advanced_Filter constructor. */ public function __construct() { // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped add_action( 'init', array( $this, 'register' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** * Get_adv_filter_options * * @return array */ private function get_adv_filter_options() { return array( array( 'id' => 'popular_post_1_day_view', 'name' => __( 'Trending Today', 'ultimate-post' ), ), array( 'id' => 'popular_post_7_days_view', 'name' => __( 'This Week’s Popular Posts', 'ultimate-post' ), ), array( 'id' => 'popular_post_30_days_view', 'name' => __( 'Top Posts of the Month', 'ultimate-post' ), ), array( 'id' => 'popular_post_all_times_view', 'name' => __( 'All-Time Favorites', 'ultimate-post' ), ), array( 'id' => 'random_post', 'name' => __( 'Random Posts', 'ultimate-post' ), ), array( 'id' => 'random_post_7_days', 'name' => __( 'Random Posts (7 Days)', 'ultimate-post' ), ), array( 'id' => 'random_post_30_days', 'name' => __( 'Random Posts (30 Days)', 'ultimate-post' ), ), array( 'id' => 'latest_post_published', 'name' => __( 'Latest Posts - Published Date', 'ultimate-post' ), ), array( 'id' => 'latest_post_modified', 'name' => __( 'Latest Posts - Last Modified Date', 'ultimate-post' ), ), array( 'id' => 'oldest_post_published', 'name' => __( 'Oldest Posts - Published Date', 'ultimate-post' ), ), array( 'id' => 'oldest_post_modified', 'name' => __( 'Oldest Posts - Last Modified Date', 'ultimate-post' ), ), array( 'id' => 'alphabet_asc', 'name' => __( 'Alphabetical ASC', 'ultimate-post' ), ), array( 'id' => 'alphabet_desc', 'name' => __( 'Alphabetical DESC', 'ultimate-post' ), ), array( 'id' => 'sticky_posts', 'name' => __( 'Sticky Post', 'ultimate-post' ), ), array( 'id' => 'most_comment', 'name' => __( 'Most Comments', 'ultimate-post' ), ), array( 'id' => 'most_comment_1_day', 'name' => __( 'Most Comments (1 Day)', 'ultimate-post' ), ), array( 'id' => 'most_comment_7_days', 'name' => __( 'Most Comments (7 Days)', 'ultimate-post' ), ), array( 'id' => 'most_comment_30_days', 'name' => __( 'Most Comments (30 Days)', 'ultimate-post' ), ), ); } /** * Get_select_attributes * * @return array */ public function get_select_attributes() { return array( 'advanceId' => '', 'blockId' => '', 'advanceCss' => '', 'filterStyle' => 'dropdown', 'filterValues' => '["_all"]', 'dropdownOptionsType' => 'all', 'dropdownOptions' => '["_all"]', 'allText' => 'All', 'sPlaceholderText' => 'Search...', 'searchEnabled' => false, 'inlineMultiSelect' => true, ); } /** * Get_search_attributes * * @return array */ public function get_search_attributes() { return array( 'advanceId' => '', 'blockId' => '', 'advanceCss' => '', 'placeholder' => 'Search...', ); } /** * Get_clear_attributes * * @return array */ public function get_clear_attributes() { return array( 'advanceId' => '', 'blockId' => '', 'clearButtonText' => 'Clear Filters', 'clearButtonPosition' => 'normal', 'cAlign' => 'margin-bottom: inherit', ); } public function register() { register_block_type( 'ultimate-post/filter-select', array( 'editor_script' => 'ultp-blocks-editor-script', 'editor_style' => 'ultp-blocks-editor-css', 'render_callback' => array( $this, 'select_content' ), ) ); register_block_type( 'ultimate-post/filter-search-adv', array( 'editor_script' => 'ultp-blocks-editor-script', 'editor_style' => 'ultp-blocks-editor-css', 'render_callback' => array( $this, 'search_content' ), ) ); register_block_type( 'ultimate-post/filter-clear', array( 'editor_script' => 'ultp-blocks-editor-script', 'editor_style' => 'ultp-blocks-editor-css', 'render_callback' => array( $this, 'clear_content' ), ) ); } /** * Get_button_data * * @param mixed $type string. * @param mixed $ids string. * @param mixed $post_types string. * @param mixed $allText string. * @param mixed $option string. * @param mixed $order array. * @return array */ public function get_button_data( $type, $data_ids, $post_types = '', $allText = 'All', $option = '', $order = array() ) { $res = array(); if ( in_array( '_all', $data_ids ) ) { $res['_all'] = $allText; // $ids = array_filter($ids, function ($item) { // return $item != '_all'; // }); } $ids = implode( ',', $data_ids ); $adv_sort = $this->get_adv_filter_options(); switch ( $type ) { case 'adv_sort': $adv_sort_id = explode( ',', $ids ); foreach ( $adv_sort as $adv ) { foreach ( $adv_sort_id as $id ) { if ( $adv['id'] === $id ) { $res[ $id ] = $adv['name']; } } } break; case 'category': $args = array( 'taxonomy' => 'category', 'hide_empty' => false, ); if ( 'all' !== $option && ! empty( $ids ) && '_all' !== $ids ) { $args['include'] = wp_parse_id_list( $ids ); $args['orderby'] = 'include'; } $categories = get_terms( $args ); if ( ! is_wp_error( $categories ) && ! empty( $categories ) ) { foreach ( $categories as $category ) { $res['_all'] = $allText; $res[ $category->slug ] = $category->name; } } break; case 'tags': $args = array( 'taxonomy' => 'post_tag', 'hide_empty' => false, ); if ( 'all' !== $option && ! empty( $ids ) && $ids !== '_all' ) { $args['include'] = wp_parse_id_list( $ids ); $args['orderby'] = 'include'; } $tags = get_terms( $args ); if ( ! is_wp_error( $tags ) && ! empty( $tags ) ) { foreach ( $tags as $tag ) { $res[ $tag->slug ] = $tag->name; } } break; case 'author': if ( ! empty( $ids ) && $ids !== '_all' ) { $authors = get_users( array( 'per_page' => -1, 'role__in' => array( 'administrator', 'editor', 'author', 'contributor' ), 'include' => $ids, ) ); foreach ( $authors as $author ) { $res[ $author->ID ] = $author->display_name; } } break; case 'order': foreach ( $this->order as $order ) { $res[ $order['id'] ] = $order['name']; } break; case 'order_by': $orders = explode( ',', $ids ); foreach ( $this->order_by as $order_by ) { foreach ( $orders as $order ) { if ( $order_by['id'] === $order ) { $res[ $order ] = $order_by['name']; } } } break; case 'custom_tax': if ( '' === $post_types ) { break; } foreach ( $post_types as $post_type ) { $taxonomies = get_object_taxonomies( $post_type ); foreach ( $taxonomies as $taxonomy ) { if ( 'category' !== $taxonomy && 'post_tag' !== $taxonomy ) { $terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false, ) ); foreach ( $terms as $term ) { $should_add = true; if ( 'specific' === $option ) { $should_add = false; if ( is_array( $data_ids ) ) { $should_add = in_array( $term->slug, $data_ids, true ); } elseif ( is_string( $data_ids ) ) { $data_ids_arr = array_filter( array_map( 'trim', explode( ',', $data_ids ) ), 'strlen' ); $should_add = in_array( $term->slug, $data_ids_arr, true ); } } if ( $should_add ) { $res[ $term->slug ] = array( 'name' => $term->name, 'taxonomy' => $taxonomy, ); } } } } } default: break; } return $res; } /** * Inserts all option depending on condition * * @param array $arr array. * @param array $item all item. * @param int $idx all item idx. * @param string $mode mode. * @return void */ private function maybe_insert_all_option( &$arr, &$item, $idx, $mode ) { if ( 'all' === $mode ) { array_unshift( $arr, $item ); } elseif ( ! empty( $item ) && false !== $idx ) { array_splice( $arr, $idx, 0, array( $item ) ); } } public function get_select_data( $type, $all_text, $post_types = '', $specific = array(), $mode = 'all', $order = array() ) { $all = null; $all_idx = array_search( '_all', $specific, true ); $filtered_specific = $specific; if ( false !== $all_idx || 'all' === $mode ) { $all = array( 'id' => '_all', 'name' => $all_text, ); $filtered_specific = array_filter( $specific, function ( $item ) { return '_all' !== $item; } ); } $adv_sort = $this->get_adv_filter_options(); switch ( $type ) { case 'category': $categories = array(); if ( 'all' === $mode || ( 'specific' === $mode && count( $filtered_specific ) > 0 ) ) { $categories = get_categories( array( 'per_page' => -1, 'include' => $filtered_specific, 'orderby' => 'include', ) ); $categories = array_map( function ( $category ) { return array( 'id' => $category->slug, 'name' => $category->name, ); }, $categories ); } $this->maybe_insert_all_option( $categories, $all, $all_idx, $mode ); return $categories; case 'tags': $tags = array(); if ( 'all' === $mode || ( 'specific' === $mode && count( $filtered_specific ) > 0 ) ) { $tags = get_tags( array( 'per_page' => -1, 'include' => $filtered_specific, ) ); $tags = array_map( function ( \WP_Term $tag ) { return array( 'id' => $tag->slug, 'name' => $tag->name, ); }, $tags ); } $this->maybe_insert_all_option( $tags, $all, $all_idx, $mode ); return $tags; case 'author': $authors = array(); if ( 'all' === $mode || ( 'specific' === $mode && count( $filtered_specific ) > 0 ) ) { $authors = get_users( array( 'per_page' => -1, 'include' => $filtered_specific, 'role__not_in' => array( 'subscriber', 'translator' ), ) ); $authors = array_map( function ( $author ) { return array( 'id' => $author->ID, 'name' => $author->display_name, ); }, $authors ); } $this->maybe_insert_all_option( $authors, $all, $all_idx, $mode ); return $authors; case 'order': $filtered_specific = array_map( 'strtoupper', $filtered_specific ); return 'specific' === $mode ? $this->filter_select_options( $order, $filtered_specific ) : $order; case 'order_by': return 'specific' === $mode ? $this->filter_select_options( $this->order_by, $filtered_specific ) : $this->order_by; case 'adv_sort': $data = 'specific' === $mode ? $this->filter_select_options( $adv_sort, $filtered_specific ) : $adv_sort; $this->maybe_insert_all_option( $data, $all, $all_idx, $mode ); return $data; case 'custom_tax': if ( '' === $post_types ) { return array(); } $data = array(); foreach ( $post_types as $post_type ) { $taxonomies = get_object_taxonomies( sanitize_text_field( $post_type ) ); foreach ( $taxonomies as $taxonomy ) { if ( 'category' !== $taxonomy && 'post_tag' !== $taxonomy ) { $terms = array(); if ( 'specific' === $mode && count( $filtered_specific ) > 0 ) { foreach ( $filtered_specific as $s ) { $res = get_term_by( 'slug', $s, $taxonomy ); if ( ! empty( $res ) ) { $terms[] = $res; } } } else { $terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false, ) ); } foreach ( $terms as $term ) { $existing_ids = array_column( $data, 'id' ); if ( ! in_array( $term->slug, $existing_ids, true ) ) { $data[] = array( 'id' => $term->slug, 'name' => $term->name, 'taxonomy' => $taxonomy, ); } } } } } $this->maybe_insert_all_option( $data, $all, $all_idx, $mode ); return $data; default: return array(); } } public function select_content( $attr ) { $block_name = 'filter-select'; $is_active = ultimate_post()->is_lc_active(); $attr = wp_parse_args( $attr, $this->get_select_attributes() ); if ( ! $is_active && in_array( $attr['type'], $this->pro_select_types ) ) { return ''; } $post_types = isset( $attr['postTypes'] ) ? $attr['postTypes'] : ''; if ( is_string( $post_types ) ) { $post_types = json_decode( $post_types, true ); } if ( is_array( $post_types ) && in_array( 'customPostType', $post_types, true ) ) { $post_types = json_decode( $attr['queryPostType'][0] ?? '[]', true ); if ( is_string( $post_types ) ) { $post_types = json_decode( $post_types, true ); } } $attr['blockId'] = ultimate_post()->sanitize_attr( $attr, 'blockId', 'sanitize_html_class', 'missing_block_id' ); $attr['allText'] = ultimate_post()->sanitize_attr( $attr, 'allText', 'sanitize_text_field', 'missing_block_id' ); if ( 'inline' === $attr['filterStyle'] ) { $inline_values = json_decode( $attr['filterValues'], true ); if ( ! is_array( $inline_values ) ) { return ''; } $data = $this->get_button_data( $attr['type'], $inline_values, $post_types, $attr['allText'], $attr['dropdownOptionsType'], $this->get_order_options() ); $btn_wrapper_attrs = get_block_wrapper_attributes( array( 'class' => 'ultp-block-' . $attr['blockId'] . ' ultp-filter-button', 'role' => 'button', 'data-blockId' => $attr['blockId'], 'data-is-active' => 'false', 'data-builder' => ultimate_post()->get_builder_attr( 'archiveBuilderFilter' ), 'data-current-postid' => get_the_ID(), 'data-filter-label' => isset( $attr['allText'] ) ? $attr['allText'] : 'All', ) ); ob_start(); ?>