label = __( 'Checkboxes', 'fwp' ); $this->fields = [ 'parent_term', 'modifiers', 'hierarchical', 'show_expanded', 'ghosts', 'operator', 'orderby', 'count', 'soft_limit' ]; } /** * Load the available choices */ function load_values( $params ) { global $wpdb; $facet = $params['facet']; $from_clause = $wpdb->prefix . 'facetwp_index f'; $where_clause = $params['where_clause']; // Orderby $orderby = $this->get_orderby( $facet ); // Limit $limit = $this->get_limit( $facet ); // Use "OR" mode when necessary $is_single = FWP()->helper->facet_is( $facet, 'multiple', 'no' ); $using_or = FWP()->helper->facet_is( $facet, 'operator', 'or' ); // Facet in "OR" mode if ( $is_single || $using_or ) { $where_clause = $this->get_where_clause( $facet ); } $orderby = apply_filters( 'facetwp_facet_orderby', $orderby, $facet ); $from_clause = apply_filters( 'facetwp_facet_from', $from_clause, $facet ); $where_clause = apply_filters( 'facetwp_facet_where', $where_clause, $facet ); $sql = " SELECT f.facet_value, f.facet_display_value, f.term_id, f.parent_id, f.depth, COUNT(DISTINCT f.post_id) AS counter FROM $from_clause WHERE f.facet_name = '{$facet['name']}' $where_clause GROUP BY f.facet_value ORDER BY $orderby LIMIT $limit"; $output = $wpdb->get_results( $sql, ARRAY_A ); // Show "ghost" facet choices // For performance gains, only run if facets are in use $show_ghosts = FWP()->helper->facet_is( $facet, 'ghosts', 'yes' ); if ( $show_ghosts && FWP()->is_filtered ) { $raw_post_ids = implode( ',', FWP()->unfiltered_post_ids ); $sql = " SELECT f.facet_value, f.facet_display_value, f.term_id, f.parent_id, f.depth, 0 AS counter FROM $from_clause WHERE f.facet_name = '{$facet['name']}' AND post_id IN ($raw_post_ids) GROUP BY f.facet_value ORDER BY $orderby LIMIT $limit"; $ghost_output = $wpdb->get_results( $sql, ARRAY_A ); $tmp = []; $preserve_ghosts = FWP()->helper->facet_is( $facet, 'preserve_ghosts', 'yes' ); $orderby_count = FWP()->helper->facet_is( $facet, 'orderby', 'count' ); // Keep the facet placement intact if ( $preserve_ghosts && ! $orderby_count ) { foreach ( $ghost_output as $row ) { $tmp[ $row['facet_value'] . ' ' ] = $row; } foreach ( $output as $row ) { $tmp[ $row['facet_value'] . ' ' ] = $row; } $output = $tmp; } else { // Make the array key equal to the facet_value (for easy lookup) foreach ( $output as $row ) { $tmp[ $row['facet_value'] . ' ' ] = $row; // Force a string array key } $output = $tmp; foreach ( $ghost_output as $row ) { $facet_value = $row['facet_value']; if ( ! isset( $output[ "$facet_value " ] ) ) { $output[ "$facet_value " ] = $row; } } } $output = array_splice( $output, 0, $limit ); $output = array_values( $output ); } return $output; } /** * Generate the facet HTML */ function render( $params ) { $facet = $params['facet']; if ( FWP()->helper->facet_is( $facet, 'hierarchical', 'yes' ) ) { return $this->render_hierarchy( $params ); } $output = ''; $values = (array) $params['values']; $soft_limit = empty( $facet['soft_limit'] ) ? 0 : (int) $facet['soft_limit']; $key = 0; foreach ( $values as $key => $row ) { if ( 0 < $soft_limit && $key == $soft_limit ) { $output .= '