S()->settings->getOption( 'analytics_enabled' ) === 'on' ) { $stats = new Recorder(); $stats->listen(); } } /** * Get search results via ajax * * @param string $phrase Search phrase. * @param bool $return Whether to return the results. * @param string $context Search context: 'autocomplete' or 'product-ids'. * * @return mixed|void */ public function getSearchResults( $phrase = '', $return = false, $context = 'autocomplete' ) { if ( $context === 'all-results' ) { $context = 'product-ids'; } $start = microtime( true ); $lang = ''; $hits = 0; if ( Multilingual::isMultilingual() ) { $lang = Multilingual::getCurrentLanguage(); } if ( !defined( 'DGWT_WCAS_AJAX' ) ) { define( 'DGWT_WCAS_AJAX', true ); } $this->groups = $this->searchResultsGroups(); $this->flexibleLimits = apply_filters( 'dgwt/wcas/flexible_limits', true ); $this->showHeadings = DGWT_WCAS()->settings->getOption( 'show_grouped_results' ) === 'on'; if ( $this->flexibleLimits ) { $totalLimit = DGWT_WCAS()->settings->getOption( 'suggestions_limit', 'int', 7 ); $this->totalLimit = ( $totalLimit === -1 ? $this->calcFreeSlots() : $totalLimit ); } $output = array(); $results = array(); $keyword = ''; if ( $return ) { $keyword = sanitize_text_field( $phrase ); } else { // Compatible with v1.1.7 if ( !empty($_REQUEST['dgwt_wcas_keyword']) ) { $keyword = sanitize_text_field( $_REQUEST['dgwt_wcas_keyword'] ); } if ( !empty($_REQUEST['s']) ) { $keyword = sanitize_text_field( $_REQUEST['s'] ); } } $keyword = apply_filters( 'dgwt/wcas/phrase', $keyword ); // Break early if keyword contains blacklisted phrase. if ( Helpers::phraseContainsBlacklistedTerm( $keyword ) ) { if ( $return ) { return $this->getEmptyOutput(); } else { echo json_encode( Helpers::noResultsSuggestion( $this->getEmptyOutput() ) ) ; die; } } /* SEARCH IN WOO CATEGORIES */ if ( $context === 'autocomplete' && array_key_exists( 'tax_product_cat', $this->groups ) ) { $limit = ( $this->flexibleLimits ? $this->totalLimit : $this->groups['tax_product_cat']['limit'] ); $categories = $this->getCategories( $keyword, $limit ); $this->groups['tax_product_cat']['results'] = $categories['items']; $hits += $categories['total']; } /* SEARCH IN WOO TAGS */ if ( $context === 'autocomplete' && array_key_exists( 'tax_product_tag', $this->groups ) ) { $limit = ( $this->flexibleLimits ? $this->totalLimit : $this->groups['tax_product_tag']['limit'] ); $tags = $this->getTags( $keyword, $limit ); $this->groups['tax_product_tag']['results'] = $tags['items']; $hits += $tags['total']; } /* SEARCH IN PRODUCTS */ $totalProducts = 0; if ( apply_filters( 'dgwt/wcas/search_in_products', true ) ) { $args = array( 's' => $keyword, 'posts_per_page' => -1, 'post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'order' => 'DESC', 'suppress_filters' => false, ); // Backward compatibility WC < 3.0 if ( Helpers::compareWcVersion( '3.0', '<' ) ) { $args['meta_query'] = $this->getMetaQuery(); } else { $args['tax_query'] = $this->getTaxQuery(); } $args = apply_filters( 'dgwt/wcas/search_query/args', $args ); $products = get_posts( $args ); $products = apply_filters( 'dgwt/wcas/search_results/products_raw', $products ); $totalProducts = count( $products ); $hits += $totalProducts; do_action( 'dgwt/wcas/analytics/after_searching', $keyword, $hits, $lang ); if ( !empty($products) ) { $orderedProducts = array(); $i = 0; foreach ( $products as $post ) { if ( $context === 'product-ids' ) { $orderedProducts[$i] = new \stdClass(); $orderedProducts[$i]->ID = $post->ID; } else { $orderedProducts[$i] = $post; } $score = Helpers::calcScore( $keyword, $post->post_title ); $orderedProducts[$i]->score = apply_filters( 'dgwt/wcas/search_results/product/score', $score, $keyword, $post->ID, $post ); $i++; } // Sort by relevance usort( $orderedProducts, array( 'DgoraWcas\\Helpers', 'cmpSimilarity' ) ); // Response that returns all results. if ( $context === 'product-ids' ) { $output['suggestions'] = $orderedProducts; $output['time'] = number_format( microtime( true ) - $start, 2, '.', '' ) . ' sec'; $result = apply_filters( 'dgwt/wcas/page_search_results/output', $output ); if ( $return ) { return $result; } else { echo json_encode( $result ) ; die; } } $productsSlots = ( $this->flexibleLimits ? $this->totalLimit : $this->groups['product']['limit'] ); $fields = []; if ( DGWT_WCAS()->settings->getOption( 'show_product_image' ) === 'on' ) { $fields[] = 'thumb_html'; } if ( DGWT_WCAS()->settings->getOption( 'show_product_price' ) === 'on' ) { $fields[] = 'price'; } if ( DGWT_WCAS()->settings->getOption( 'show_product_sku' ) === 'on' ) { $fields[] = 'sku'; } $relevantProducts = $this->getProductsData( $orderedProducts, $productsSlots, $fields ); } wp_reset_postdata(); } /* END SEARCH IN PRODUCTS */ if ( !empty($relevantProducts) ) { $this->groups['product']['results'] = $relevantProducts; } if ( $this->hasResults() ) { if ( $this->flexibleLimits ) { $this->applyFlexibleLimits(); } $results = $this->convertGroupsToSuggestions(); // Show more if ( !empty($this->groups['product']['results']) && count( $this->groups['product']['results'] ) < $totalProducts ) { $results[] = array( 'value' => '', 'total' => $totalProducts, 'url' => add_query_arg( array( 's' => $keyword, 'post_type' => 'product', 'dgwt_wcas' => '1', ), home_url() ), 'type' => 'more_products', ); } } else { if ( $context === 'product-ids' ) { $emptyResult = new \stdClass(); $emptyResult->ID = 0; $results[] = $emptyResult; } else { $results[] = array( 'value' => '', 'type' => 'no-results', ); } } $output['suggestions'] = $results; $output['total'] = $hits; $output['time'] = number_format( microtime( true ) - $start, 2, '.', '' ) . ' sec'; $output['engine'] = 'free'; $output['v'] = DGWT_WCAS_VERSION; $result = apply_filters( 'dgwt/wcas/search_results/output', $output ); if ( $return ) { return $result; } else { echo json_encode( $result ) ; die; } } public function getProductsData( $orderedProducts, $limit = -1, $fields = array() ) { $relevantProducts = array(); foreach ( $orderedProducts as $post ) { $product = new Product( $post ); if ( !$product->isCorrect() ) { continue; } // Strip