post_content, 'woocommerce_checkout' ); } /** * Whether Streetfill loads on this page. For themes that show the checkout * or address forms on other pages. * * @param bool $load Whether the page has address fields. */ return (bool) apply_filters( 'streetfill_is_address_page', $checkout || $account ); } /** * Enqueues the provider script and styles. */ public static function enqueue() { if ( ! Settings::has_key() || ! self::wc_autocomplete_enabled() || ! self::is_address_page() ) { return; } wp_enqueue_script( 'streetfill-provider', STREETFILL_URL . 'assets/js/provider.js', array( self::WC_COMMON, 'streetfill-services', 'wp-hooks' ), STREETFILL_VERSION, true ); /** * Front-end configuration of the provider script. * * @param array $config Configuration. */ $config = apply_filters( 'streetfill_frontend_config', self::config() ); wp_add_inline_script( 'streetfill-provider', 'var streetfillConfig = ' . wp_json_encode( $config ) . ';', 'before' ); // The service's attribution stays whether or not our look is on. wp_enqueue_style( 'streetfill-attribution', STREETFILL_URL . 'assets/css/attribution.css', array(), STREETFILL_VERSION ); if ( 'on' === Settings::get( 'style' ) ) { wp_enqueue_style( 'streetfill', STREETFILL_URL . 'assets/css/streetfill.css', array(), STREETFILL_VERSION ); } } /** * Configuration for provider.js. * * @return array */ public static function config() { $service = Settings::service(); return array( 'providerId' => Plugin::PROVIDER_ID, 'service' => $service, 'mapsUrl' => 'google' === $service ? self::maps_url() : '', 'key' => 'google' === $service ? '' : Settings::key(), 'language' => self::google_language( determine_locale() ), ); } /** * URL of Google's Maps JavaScript API with the shop's key. The browser loads it * only when a shopper first focuses an address field (provider.js). * * @return string */ public static function maps_url() { return add_query_arg( array( 'key' => rawurlencode( (string) Settings::get( 'api_key' ) ), 'v' => 'weekly', 'loading' => 'async', 'language' => rawurlencode( self::google_language( determine_locale() ) ), 'callback' => 'streetfillMapsReady', ), 'https://maps.googleapis.com/maps/api/js' ); } /** * Google's language code for a WordPress locale (de_DE_formal → de, pt_BR → pt-BR). * * @param string $locale WordPress locale. * @return string */ public static function google_language( $locale ) { $locale = (string) $locale; $keep = array( 'pt_BR', 'pt_PT', 'zh_CN', 'zh_TW', 'zh_HK', 'en_GB', 'en_AU', 'fr_CA', 'es_419' ); if ( 1 === preg_match( '/^([a-z]{2,3}_[A-Z0-9]{2,3})/', $locale, $m ) && in_array( $m[1], $keep, true ) ) { return str_replace( '_', '-', $m[1] ); } $primary = strtolower( (string) strtok( $locale, '_' ) ); return 1 === preg_match( '/^[a-z]{2,3}$/', $primary ) ? $primary : 'en'; } }