$largestGroupCount = $thisGroupTotal; $largestGroupKey = $key; } } } $last = count( $this->groups[ $largestGroupKey ]['results'] ) - 1; if ( isset( $this->groups[ $largestGroupKey ]['results'][ $last ] ) ) { unset( $this->groups[ $largestGroupKey ]['results'][ $last ] ); } } } } /** * Prepare suggestions based on groups * * @return array */ public function convertGroupsToSuggestions() { $suggestions = array(); $totalHeadlines = 0; foreach ( $this->groups as $key => $group ) { if ( ! empty( $group['results'] ) ) { if ( $this->showHeadings ) { $suggestions[] = $this->headlineBody( $key ); $totalHeadlines ++; } foreach ( $group['results'] as $result ) { $suggestions[] = $result; } } } // Remove products headline when there are only product type suggestion if ( $totalHeadlines === 1 ) { $i = 0; $unset = false; foreach ( $suggestions as $key => $suggestion ) { if ( ! empty( $suggestion['type'] ) && $suggestion['type'] === 'headline' && $suggestion['value'] === 'product' ) { unset( $suggestions[ $i ] ); $unset = true; break; } $i ++; } if ( $unset ) { $suggestions = array_values( $suggestions ); } } return $suggestions; } /** * Order of the search resutls groups * * @return array */ public function searchResultsGroups() { $groups = array(); if ( DGWT_WCAS()->settings->getOption( 'show_product_tax_product_cat' ) === 'on' ) { $groups['tax_product_cat'] = array( 'limit' => 3 ); } if ( DGWT_WCAS()->settings->getOption( 'show_product_tax_product_tag' ) === 'on' ) { $groups['tax_product_tag'] = array( 'limit' => 3 ); } $groups['product'] = array( 'limit' => 7 ); return apply_filters( 'dgwt/wcas/search_groups', $groups ); } /** * Allow to get the ID of products that have been found * * @param integer[] $postsIDs * * @return mixed */ public function getProductIds( $postsIDs ) { if ( $this->postsIDsBuffer !== null ) { return $this->postsIDsBuffer; } return $postsIDs; } /** * Add taxonomies labels * * @param array $labels Labels used at frontend * * @return array */ public function setTaxonomiesLabels( $labels ) { $labels['tax_product_cat_plu'] = __( 'Categories', 'woocommerce' ); $labels['tax_product_cat'] = __( 'Category', 'woocommerce' ); $labels['tax_product_tag_plu'] = __( 'Tags' ); $labels['tax_product_tag'] = __( 'Tag' ); return $labels; } /** * Backward compatibility for labels * * Full taxonomy names for categories and tags. All with prefix 'tax_'. * * @param array $labels Labels used at frontend * * @return array */ public function fixTaxonomiesLabels( $labels ) { // Product category. Old: 'category', 'product_cat_plu'. if ( isset( $labels['category'] ) ) { $labels['tax_product_cat'] = $labels['category']; unset( $labels['category'] ); } if ( isset( $labels['product_cat_plu'] ) ) { $labels['tax_product_cat_plu'] = $labels['product_cat_plu']; unset( $labels['product_cat_plu'] ); } // Product tag. Old: 'tag', 'product_tag_plu'. if ( isset( $labels['tag'] ) ) { $labels['tax_product_tag'] = $labels['tag']; unset( $labels['tag'] ); } if ( isset( $labels['product_tag_plu'] ) ) { $labels['tax_product_tag_plu'] = $labels['product_tag_plu']; unset( $labels['product_tag_plu'] ); } return $labels; } /** * Add language to WC endpoint if Polylang is active * * @param $url * @param $request * * @return string * @see polylang-wc/frontend/frontend.php:306 */ public function fixPolylangWooEndpoint( $url, $request ) { if ( PLL() instanceof \PLL_Frontend ) { // Remove wc-ajax to avoid the value %%endpoint%% to be encoded by add_query_arg (used in plain permalinks). $url = remove_query_arg( 'wc-ajax', $url ); $url = PLL()->links_model->switch_language_in_link( $url, PLL()->curlang ); return add_query_arg( 'wc-ajax', $request, $url ); } return $url; } /** * Get empty search output * * @return array */ private function getEmptyOutput() { $output = array( 'engine' => 'free', 'suggestions' => array(), 'time' => '0 sec', 'total' => 0, 'v' => DGWT_WCAS_VERSION, ); return $output; } }