width' => 'auto', 'show_details_panel' => false, 'show_images' => false, 'show_price' => false, 'show_desc' => false, 'show_sale_badge' => false, 'show_featured_badge' => false, 'dynamic_prices' => false, 'is_rtl' => ( is_rtl() == true ? true : false ), 'show_preloader' => false, 'show_headings' => false, 'preloader_url' => '', 'taxonomy_brands' => '', 'img_url' => DGWT_WCAS_URL . 'assets/img/', 'is_premium' => dgoraAsfwFs()->is_premium(), 'layout_breakpoint' => $layout->layout_breakpoint, 'mobile_overlay_breakpoint' => $layout->mobile_overlay_breakpoint, 'mobile_overlay_wrapper' => $layout->mobile_overlay_wrapper, 'mobile_overlay_delay' => apply_filters( 'dgwt/wcas/scripts/overlay_delay_ms', 0 ), 'debounce_wait_ms' => apply_filters( 'dgwt/wcas/scripts/debounce_wait_ms', 400 ), 'send_ga_events' => apply_filters( 'dgwt/wcas/scripts/send_ga_events', true ), 'enable_ga_site_search_module' => apply_filters( 'dgwt/wcas/scripts/enable_ga_site_search_module', false ), 'magnifier_icon' => self::getMagnifierIco( '' ), 'close_icon' => self::getIcon( 'close' ), 'back_icon' => self::getIcon( 'arrow-left' ), 'preloader_icon' => self::getIcon( 'preloader' ), 'voice_search_inactive_icon' => self::getIcon( ( $layout->style === 'pirx' ? 'voice-search-inactive-pirx' : 'voice-search-inactive' ), 'dgwt-wcas-voice-search-mic-inactive' ), 'voice_search_active_icon' => self::getIcon( ( $layout->style === 'pirx' ? 'voice-search-active-pirx' : 'voice-search-active' ), 'dgwt-wcas-voice-search-mic-active' ), 'voice_search_disabled_icon' => self::getIcon( ( $layout->style === 'pirx' ? 'voice-search-disabled-pirx' : 'voice-search-disabled' ), 'dgwt-wcas-voice-search-mic-disabled' ), 'custom_params' => (object) apply_filters( 'dgwt/wcas/scripts/custom_params', array() ), 'convert_html' => true, 'suggestions_wrapper' => apply_filters( 'dgwt/wcas/scripts/suggestions_wrapper', 'body' ), 'show_product_vendor' => dgoraAsfwFs()->is_premium() && class_exists( 'DgoraWcas\\Integrations\\Marketplace\\Marketplace' ) && DGWT_WCAS()->marketplace->showProductVendor(), 'disable_hits' => apply_filters( 'dgwt/wcas/scripts/disable_hits', false ), 'disable_submit' => apply_filters( 'dgwt/wcas/scripts/disable_submit', false ), 'fixer' => apply_filters( 'dgwt/wcas/scripts/fixer', array( 'broken_search_ui' => true, 'broken_search_ui_ajax' => true, 'broken_search_ui_hard' => false, 'broken_search_elementor_popups' => true, 'broken_search_browsers_back_arrow' => true, 'force_refresh_checkout' => true, ) ), 'voice_search_enabled' => defined( 'DGWT_WCAS_VOICE_SEARCH_ENABLE' ) && DGWT_WCAS_VOICE_SEARCH_ENABLE, 'voice_search_lang' => apply_filters( 'dgwt/wcas/scripts/voice_search_lang', get_bloginfo( 'language' ) ), ); if ( Multilingual::isMultilingual() ) { $localize['current_lang'] = Multilingual::getCurrentLanguage(); } // Min characters $min_chars = DGWT_WCAS()->settings->getOption( 'min_chars' ); if ( !empty($min_chars) && is_numeric( $min_chars ) ) { $localize['min_chars'] = absint( $min_chars ); } $sug_width = DGWT_WCAS()->settings->getOption( 'sug_width' ); if ( !empty($sug_width) && is_numeric( $sug_width ) && $sug_width > 100 ) { $localize['sug_width'] = absint( $sug_width ); } // Show/hide Details panel if ( DGWT_WCAS()->settings->getOption( 'show_details_box' ) === 'on' ) { $localize['show_details_panel'] = true; } // Show/hide images if ( DGWT_WCAS()->settings->getOption( 'show_product_image' ) === 'on' ) { $localize['show_images'] = true; } // Show/hide price if ( DGWT_WCAS()->settings->getOption( 'show_product_price' ) === 'on' ) { $localize['show_price'] = true; } // Show/hide description if ( DGWT_WCAS()->settings->getOption( 'show_product_desc' ) === 'on' ) { $localize['show_desc'] = true; } // Show/hide description if ( DGWT_WCAS()->settings->getOption( 'show_product_sku' ) === 'on' ) { $localize['show_sku'] = true; } // Show/hide sale badge if ( DGWT_WCAS()->settings->getOption( 'show_sale_badge' ) === 'on' ) { $localize['show_sale_badge'] = true; } // Show/hide featured badge if ( DGWT_WCAS()->settings->getOption( 'show_featured_badge' ) === 'on' ) { $localize['show_featured_badge'] = true; } // Set preloader if ( DGWT_WCAS()->settings->getOption( 'show_preloader' ) === 'on' ) { $localize['show_preloader'] = true; $localize['preloader_url'] = esc_url( trim( DGWT_WCAS()->settings->getOption( 'preloader_url' ) ) ); } // Show/hide autocomplete headings if ( DGWT_WCAS()->settings->getOption( 'show_grouped_results' ) === 'on' ) { $localize['show_headings'] = true; } return apply_filters( 'dgwt/wcas/scripts/localize', $localize ); } /** * Get endpoint URL * * @param string $type * * @return string */ public static function getEndpointUrl( $type = '' ) { $url = ''; if ( !in_array( $type, array( 'search', 'details', 'prices' ) ) ) { return $url; } switch ( $type ) { case 'search': $url = \WC_AJAX::get_endpoint( DGWT_WCAS_SEARCH_ACTION ); break; case 'details': $url = \WC_AJAX::get_endpoint( DGWT_WCAS_RESULT_DETAILS_ACTION ); break; case 'prices': $url = \WC_AJAX::get_endpoint( DGWT_WCAS_GET_PRICES_ACTION ); break; default: break; } return apply_filters( "dgwt/wcas/endpoint/{$type}", $url ); } /** * Checking the current code is run by the object of the given class * * @param string $class_name Class name * @param int $backtrace_limit The number of stack frames that is tested backwards. * * @return bool */ public static function is_running_inside_class( $class_name, $backtrace_limit = 10 ) { if ( empty($class_name) ) { return false; } if ( intval( $backtrace_limit ) <= 0 ) { $backtrace_limit = 10; } $result = false; $backtrace = self::debugBacktrace( 0, $backtrace_limit ); if ( !empty($backtrace) ) { foreach ( $backtrace as $item ) { if ( isset( $item['class'] ) && $item['class'] === $class_name ) { $result = true; break; } } } return $result; } private static function debugBacktrace( $options, $limit ) { return debug_backtrace( $options, $limit ); } /** * Search products with native engine * * @param $phrase * * @return int[] */ public static function searchProducts( $phrase ) { $postIn = array(); $urlPhrase = str_replace( "\\'", "'", $phrase ); $urlPhrase = str_replace( '\\"', '"', $urlPhrase ); $args = array( 's' => urlencode( $urlPhrase ), 'remote' => 1, ); if ( Multilingual::isMultilingual() ) { $args['l'] = Multilingual::getCurrentLanguage(); } $url = add_query_arg( $args, Helpers::getAjaxSearchEndpointUrl() ); $r = wp_remote_retrieve_body( wp_remote_get( $url, array( 'timeout' => 120, ) ) ); $decR = json_decode( $r ); if ( json_last_error() == JSON_ERROR_NONE ) { if ( is_object( $decR ) && property_exists( $decR, 'suggestions' ) && is_array( $decR->suggestions ) ) { $postIn = wp_list_pluck( $decR->suggestions, 'ID' ); } } return $postIn; } /** * Get all post types used in search * * @param string $filter 'no-products' returns post types not related to products * 'only-products' returns post types related to products * * @return array */ public static function getAllowedPostTypes( $filter = '' ) { $types = array(); if ( $filter !== 'no-products' ) { $types[] = 'product'; $types[] = 'product-variation'; } if ( $filter !== 'only-products' ) { if ( DGWT_WCAS()->settings->getOption( 'show_matching_posts' ) === 'on' ) { $types[] = 'post'; } if ( DGWT_WCAS()->settings->getOption( 'show_matching_pages' ) === 'on' ) { $types[] = 'page'; } } return apply_filters( 'dgwt/wcas/allowed_post_types', $types, $filter ); } /** * Get Basic Auth header from dedicated constants or from current request * * @return string */ public static function getBasicAuthHeader() { $authorization = ''; if ( defined( 'DGWT_WCAS_BA_USERNAME' ) && defined( 'DGWT_WCAS_BA_PASSWORD' ) ) { $authorization = 'Basic ' . base64_encode( wp_unslash( DGWT_WCAS_BA_USERNAME ) . ':' . wp_unslash( DGWT_WCAS_BA_PASSWORD ) ); } elseif ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) { $authorization = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) ); } return $authorization; } /** * Get AJAX search endpoint URL * * @param null $scheme * * @return string * * @see \WC_AJAX::get_endpoint() - Almost the same, but you can choose a scheme */ public static function getAjaxSearchEndpointUrl( $scheme = null ) { return esc_url_raw( apply_filters( 'woocommerce_ajax_get_endpoint', add_query_arg( 'wc-ajax', DGWT_WCAS_SEARCH_ACTION, remove_query_arg( array( 'remove_item', 'add-to-cart', 'added-to-cart', 'order_again', '_wpnonce' ), home_url( '/', $scheme ) ) ), DGWT_WCAS_SEARCH_ACTION ) ); } /** * Check that the AMP version of the page is displayed * * @return bool */ public static function isAMPEndpoint() { return function_exists( 'is_amp_endpoint' ) && is_amp_endpoint(); } /** * Get the path to the fibo directory in the theme or child theme * * @param string $path * @param array $vars * * @return void */ public static function loadTemplate( $template = '', $vars = array() ) { $path = ''; // Load default partials from the plugin $file = DGWT_WCAS_DIR . 'partials/' . $template; if ( file_exists( $file ) ) { $path = $file; } // Load a partial if it is localized in the child-theme $file = get_stylesheet_directory() . '/fibosearch/' . $template; if ( file_exists( $file ) ) { $path = $file; } $path = apply_filters( 'dgwt/wcas/template', $path, $template, $vars ); if ( file_exists( $path ) ) { include $path; } } /** * Add "No results" if suggestions are empty * * @param array $output * * @return array */ public static function noResultsSuggestion( $output ) { if ( empty($output['suggestions']) ) { $output['suggestions'][] = array( 'value' => '', 'type' => 'no-results', ); } return $output; } /** * Get default collate * * @param string $context * * @return string */ public static function getCollate( $context = '' ) { global $wpdb ; $sql = ''; $collate = ''; $charset = ''; if ( $wpdb->has_cap( 'collation' ) ) { if ( !empty($wpdb->charset) ) { $charset = $wpdb->charset; } if ( !empty($wpdb->collate) ) { $collate = $wpdb->collate; } } $charset = apply_filters( 'dgwt/wcas/db/charset', $charset, $context ); $collate = apply_filters( 'dgwt/wcas/db/collation', $collate, $context ); if ( !empty($charset) ) { $sql .= " DEFAULT CHARACTER SET " . $charset; } if ( !empty($collate) ) { $sql .= " COLLATE " . $collate; } return apply_filters( 'dgwt/wcas/db/collation/sql', $sql, $context ); } /** * Check if string ends with another string * * @param string $haystack * @param string $needle * * @return bool */ public static function endsWith( $haystack, $needle ) { $length = strlen( $needle ); return ( $length > 0 ? substr( $haystack, -$length ) === $needle : true ); } /** * Get table info * * @return float[] */ public static function getTableInfo( $table = '' ) { global $wpdb ; if ( !defined( 'DB_NAME' ) || empty($table) ) { return array( 'data' => 0.0, 'index' => 0.0, ); } $info = $wpdb->get_row( $wpdb->prepare( "SELECT\n\t\t\t\t\t round( ( data_length / 1024 / 1024 ), 2 ) 'data',\n\t\t\t\t\t round( ( index_length / 1024 / 1024 ), 2 ) 'index'\n\t\t\t\t\tFROM information_schema.TABLES\n\t\t\t\t\tWHERE table_schema = %s\n\t\t\t\t\tAND table_name = %s;", DB_NAME, $table ), ARRAY_A ); if ( !isset( $info['data'] ) || !isset( $info['index'] ) ) { return array( 'data' => 0.0, 'index' => 0.0, ); } $info['data'] = floatval( $info['data'] ); $info['index'] = floatval( $info['index'] ); return $info; } /** * Get names of all FiboSearch options * * @return array */ public static function getAllOptionNames() { global $wpdb ; $options = array(); $res = $wpdb->get_col( "SELECT SQL_NO_CACHE option_name FROM {$wpdb->options} WHERE option_name LIKE 'dgwt_wcas_%'" ); if ( !empty($res) && is_array( $res ) ) { $options = $res; } return $options; } }