false ), 'magnifier_icon' => self::getMagnifierIco( '' ), 'magnifier_icon_pirx' => self::getMagnifierIco( '', 'magnifier-pirx' ), 'history_icon' => self::getIcon( 'history' ), 'close_icon' => self::getIcon( 'close' ), 'back_icon' => self::getIcon( 'arrow-left' ), 'preloader_icon' => self::getIcon( 'preloader' ), 'voice_search_inactive_icon' => self::getIcon( ( in_array( $layout->style, array('pirx', 'pirx-compact') ) ? 'voice-search-inactive-pirx' : 'voice-search-inactive' ), 'dgwt-wcas-voice-search-mic-inactive' ), 'voice_search_active_icon' => self::getIcon( ( in_array( $layout->style, array('pirx', 'pirx-compact') ) ? 'voice-search-active-pirx' : 'voice-search-active' ), 'dgwt-wcas-voice-search-mic-active' ), 'voice_search_disabled_icon' => self::getIcon( ( in_array( $layout->style, array('pirx', 'pirx-compact') ) ? '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_jet_mobile_menu' => 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' ) ), 'show_recently_searched_products' => false, 'show_recently_searched_phrases' => false, ); // User search history if ( DGWT_WCAS()->settings->getOption( 'show_user_history' ) === 'on' ) { $localize['show_recently_searched_products'] = apply_filters( 'dgwt/wcas/scripts/show_recently_searched_products', true ); $localize['show_recently_searched_phrases'] = apply_filters( 'dgwt/wcas/scripts/show_recently_searched_phrases', true ); } 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; } /** * Checking the current code is run inside specified function * * @param string $function_name Function name * @param int $backtrace_limit The number of stack frames that is tested backwards. * * @return bool */ public static function isRunningInsideFunction( $function_name, $backtrace_limit = 10 ) { if ( empty( $function_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['function'] ) && $item['function'] === $function_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 = []; $results = DGWT_WCAS()->nativeSearch->getSearchResults( $phrase, true, 'product-ids' ); if ( isset( $results['suggestions'] ) && is_array( $results['suggestions'] ) ) { $postIn = wp_list_pluck( $results['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; } /** * 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; } /** * Does the "Shop manager" role have access to the plugin settings? * * @return bool */ public static function shopManagerHasAccess() { return defined( 'DGWT_WCAS_ALLOW_SHOP_MANAGER_ACCESS' ) && DGWT_WCAS_ALLOW_SHOP_MANAGER_ACCESS; } /** * Clear phrase before processing regex expression. * Some user inputs might contain special characters which should be escaped. * * @return string */ public static function escPhraseForRegex( $phrase ) { $phrase = preg_replace_callback( "/([!@#\$&()\\-\\[\\]{}\\`.+,\\/\"\\'])/", function ( $matches ) { return '\\' . $matches[0]; }, $phrase ); return $phrase; } /** * Esc not allowed HTML tags for No Results text * * @return string */ public static function ksesNoResults( $content ) { $content = wp_kses( $content, array( 'div' => array( 'class' => array(), ), 'span' => array( 'class' => array(), ), 'a' => array( 'href' => array(), ), 'br' => array(), 'p' => array(), 'em' => array(), 'b' => array(), 'ol' => array(), 'ul' => array(), 'li' => array(), 'h1' => array(), 'h2' => array(), 'h3' => array(), 'h4' => array(), 'h5' => array(), 'h6' => array(), ) ); return $content; } /** * Remove Greek accents * * @param string $text The text to process. * * @return string */ public static function removeGreekAccents( $text ) { $chars = array( 'Ά' => 'Α', 'ά' => 'α', 'Έ' => 'Ε', 'έ' => 'ε', 'Ί' => 'Ι', 'ί' => 'ι', 'ΐ' => 'ϊ', 'Ύ' => 'Υ', 'ύ' => 'υ', 'ΰ' => 'ϋ', 'Ή' => 'Η', 'ή' => 'η', 'Ό' => 'Ο', 'ό' => 'ο', 'Ώ' => 'Ω', 'ώ' => 'ω', ); return strtr( $text, $chars ); } /** * Test if phrase contains blacklisted term * * @param string $phrase Search phrase. * * @return bool */ public static function phraseContainsBlacklistedTerm( $phrase ) { $blacklistedTerms = apply_filters( 'dgwt/wcas/blacklisted_terms', array() ); if ( is_array( $blacklistedTerms ) ) { foreach ( $blacklistedTerms as $term ) { if ( mb_stripos( $phrase, $term ) !== false ) { return true; } } } if ( apply_filters( 'dgwt/wcas/blacklisted_terms/check_js', true ) && self::containsJsScript( $phrase ) ) { return true; } return false; } /** * Get specific label of the post type * * @param string|\WP_Post_Type $postType * @param string $label * * @return string */ public static function getPostTypeLabel( $postType, $label ) { $text = ''; $obj = null; $label = sanitize_key( $label ); if ( is_string( $postType ) ) { $obj = get_post_type_object( $postType ); } if ( is_object( $postType ) && is_a( $postType, 'WP_Post_Type' ) ) { $obj = $postType; } if ( !empty( $obj->labels ) && !empty( $obj->labels->{$label} ) ) { $text = $obj->labels->{$label}; } return $text; } /** * Check if the string contains JS script * * @param string $text * * @return bool */ public static function containsJsScript( $text ) { return !empty( $text ) && preg_match( '/]*?>/', $text ); } /** * Remove content of