$lang ) { if ( $lang['locale'] === $locale ) { $default = $code; break; } } if ( 'en' === $default && 0 !== strpos( $locale, 'en' ) && Languages::exists( substr( $locale, 0, 2 ) ) ) { $default = substr( $locale, 0, 2 ); } return array( 'default_language' => $default, 'languages' => array(), 'url_mode' => 'subdirectory', 'custom_domains' => array(), 'switcher' => array( 'style' => 'dropdown_flags', 'position' => 'bottom_right', 'native_names' => true, 'show_flags' => true, 'flag_shape' => 'rect', 'theme' => 'light', 'menu_location' => '', 'flags' => array(), // Language => another flag it shows (Languages::ALT_FLAGS). ), 'seo' => array( 'hreflang' => true, 'x_default' => true, 'translate_slugs' => false, 'sitemap' => true, 'noindex_incomplete' => true, ), 'auto_redirect' => false, 'exclusions' => array( 'paths' => array(), 'selectors' => array(), ), 'glossary' => array(), ); } /** * Entitlements used while the site is not connected: nothing is translated. */ public static function disconnected_entitlements() { return array( 'max_languages' => 0, 'seo_indexing' => false, 'edit_translations' => false, 'url_translation' => false, 'custom_domains' => false, 'woocommerce_email' => false, 'chars_per_year' => 0, ); } /** * Stored config merged over defaults (not yet constrained by entitlements). */ public static function config() { $stored = get_option( self::CONFIG_OPTION, array() ); return self::sanitize_config( is_array( $stored ) ? $stored : array() ); } public static function config_version() { $conn = self::connection(); return (int) ( $conn['config_version'] ?? 0 ); } public static function save_config( array $config, $version = null ) { update_option( self::CONFIG_OPTION, self::sanitize_config( $config ), true ); if ( null !== $version ) { self::update_connection( array( 'config_version' => (int) $version ) ); } self::$effective = null; } /** * Config as it applies right now: sanitized and limited by the plan. */ public static function effective() { if ( null === self::$effective ) { self::$effective = self::apply_entitlements( self::config(), self::entitlements() ); } return self::$effective; } public static function connection() { $conn = get_option( self::CONNECTION_OPTION, array() ); return is_array( $conn ) ? $conn : array(); } public static function update_connection( array $changes ) { $conn = array_merge( self::connection(), $changes ); update_option( self::CONNECTION_OPTION, $conn, true ); self::$effective = null; return $conn; } public static function clear_connection() { $conn = self::connection(); // Keep the cloud URL so reconnecting goes to the same place. update_option( self::CONNECTION_OPTION, array( 'cloud_url' => $conn['cloud_url'] ?? '' ), true ); self::$effective = null; } public static function is_connected() { $conn = self::connection(); return ! empty( $conn['api_key'] ); } /** * The cloud connection carries the API key, so it must be HTTPS except when * developing locally (localhost, *.local, *.test or a local/development WP environment). */ public static function is_allowed_cloud_url( $url ) { $parts = wp_parse_url( (string) $url ); if ( empty( $parts['scheme'] ) || empty( $parts['host'] ) ) { return false; } if ( 'https' === strtolower( $parts['scheme'] ) ) { return true; } if ( 'http' !== strtolower( $parts['scheme'] ) ) { return false; } $host = strtolower( $parts['host'] ); return in_array( $host, array( 'localhost', '127.0.0.1', '[::1]', '::1' ), true ) || (bool) preg_match( '/\.(local|test|localhost)$/', $host ) || in_array( wp_get_environment_type(), array( 'local', 'development' ), true ); } public static function cloud_url() { if ( defined( 'LANGFERRY_CLOUD_URL' ) && LANGFERRY_CLOUD_URL ) { return untrailingslashit( LANGFERRY_CLOUD_URL ); } $conn = self::connection(); $url = ! empty( $conn['cloud_url'] ) ? $conn['cloud_url'] : LANGFERRY_DEFAULT_CLOUD_URL; return untrailingslashit( $url ); } public static function entitlements() { $conn = self::connection(); if ( empty( $conn['api_key'] ) || empty( $conn['entitlements'] ) || ! is_array( $conn['entitlements'] ) ) { return self::disconnected_entitlements(); } return array_merge( self::disconnected_entitlements(), $conn['entitlements'] ); } public static function can( $entitlement ) { $ent = self::entitlements(); return ! empty( $ent[ $entitlement ] ); } /** * Whether the front end should translate anything at all. */ public static function is_active() { $config = self::effective(); return self::is_connected() && ! empty( $config['languages'] ); } /** * The ticked languages in the order chosen on the settings page. The checkboxes arrive in the * grid's alphabetical order, which means nothing, so the order comes from $order (the chips' * comma-separated languages_order field). It can only rearrange ticked languages; anything else * in it is ignored. Ticked languages it leaves out, or all of them when there's no order (no * JavaScript), keep the place they already have, and newly ticked ones go last. * * @param string[] $current The languages saved now, in order. * @param mixed $ticked The ticked codes as submitted; anything that isn't text is ignored. * @param mixed $order The languages_order field, if any. * @return string[] */ public static function keep_order( array $current, $ticked, $order = null ) { $ticked = array_values( array_filter( (array) $ticked, 'is_string' ) ); $kept = array_values( array_unique( array_merge( array_intersect( $current, $ticked ), $ticked ) ) ); if ( ! is_string( $order ) || '' === trim( $order ) ) { return $kept; } $chosen = array_intersect( array_map( 'trim', explode( ',', substr( $order, 0, 4000 ) ) ), $kept ); return array_values( array_unique( array_merge( $chosen, $kept ) ) ); } /** * Default language + target languages. */ public static function all_languages() { $config = self::effective(); return array_values( array_unique( array_merge( array( $config['default_language'] ), $config['languages'] ) ) ); } /** * Coerce any input into a well-formed config. Unknown keys are dropped. */ public static function sanitize_config( array $in ) { $defaults = self::default_config(); $out = $defaults; if ( isset( $in['default_language'] ) && Languages::exists( $in['default_language'] ) ) { $out['default_language'] = $in['default_language']; } $langs = array(); foreach ( (array) ( $in['languages'] ?? array() ) as $code ) { if ( Languages::exists( $code ) && $code !== $out['default_language'] && ! in_array( $code, $langs, true ) ) { $langs[] = $code; } } $out['languages'] = $langs; if ( isset( $in['url_mode'] ) && in_array( $in['url_mode'], self::URL_MODES, true ) ) { $out['url_mode'] = $in['url_mode']; } $domains = array(); foreach ( (array) ( $in['custom_domains'] ?? array() ) as $code => $host ) { $host = strtolower( trim( (string) $host ) ); $host = preg_replace( '#^https?://#', '', $host ); $host = trim( (string) $host, '/' ); if ( Languages::exists( $code ) && '' !== $host && preg_match( '/^[a-z0-9.-]+(:\d+)?$/', $host ) ) { $domains[ $code ] = $host; } } $out['custom_domains'] = $domains; $sw = (array) ( $in['switcher'] ?? array() ); if ( isset( $sw['style'] ) && in_array( $sw['style'], self::SWITCHER_STYLES, true ) ) { $out['switcher']['style'] = $sw['style']; } if ( isset( $sw['position'] ) && in_array( $sw['position'], self::SWITCHER_POSITIONS, true ) ) { $out['switcher']['position'] = $sw['position']; } foreach ( array( 'native_names', 'show_flags' ) as $bool ) { if ( isset( $sw[ $bool ] ) ) { $out['switcher'][ $bool ] = (bool) $sw[ $bool ]; } } if ( isset( $sw['flag_shape'] ) && in_array( $sw['flag_shape'], array( 'rect', 'round' ), true ) ) { $out['switcher']['flag_shape'] = $sw['flag_shape']; } if ( isset( $sw['theme'] ) && in_array( $sw['theme'], array( 'light', 'dark', 'auto' ), true ) ) { $out['switcher']['theme'] = $sw['theme']; } $flags = array(); foreach ( is_array( $sw['flags'] ?? null ) ? $sw['flags'] : array() as $lang => $flag ) { if ( is_string( $flag ) && in_array( $flag, Languages::ALT_FLAGS[ $lang ] ?? array(), true ) ) { $flags[ $lang ] = $flag; } } $out['switcher']['flags'] = $flags; if ( isset( $sw['menu_location'] ) ) { $out['switcher']['menu_location'] = sanitize_key( $sw['menu_location'] ); } $seo = (array) ( $in['seo'] ?? array() ); foreach ( array_keys( $defaults['seo'] ) as $key ) { if ( isset( $seo[ $key ] ) ) { $out['seo'][ $key ] = (bool) $seo[ $key ]; } } if ( isset( $in['auto_redirect'] ) ) { $out['auto_redirect'] = (bool) $in['auto_redirect']; } $ex = (array) ( $in['exclusions'] ?? array() ); $out['exclusions']['paths'] = self::clean_list( $ex['paths'] ?? array(), 'path' ); $out['exclusions']['selectors'] = self::clean_list( $ex['selectors'] ?? array(), 'selector' ); $out['glossary'] = self::clean_list( $in['glossary'] ?? array(), 'text' ); return $out; } /** * Constrain a config to what the plan allows (mirrors the cloud's normalize_config). */ public static function apply_entitlements( array $config, array $ent ) { if ( isset( $ent['max_languages'] ) ) { // null = unlimited. $config['languages'] = array_slice( $config['languages'], 0, max( 0, (int) $ent['max_languages'] ) ); } // Address mode and the slug switch are plugin features on every plan; Langferry Cloud // decides which slugs it machine-translates (purpose "slug"). return $config; } /** * Accept arrays or newline-separated strings. */ private static function clean_list( $value, $type ) { if ( is_string( $value ) ) { $value = preg_split( '/\r\n|\r|\n/', $value ); } $out = array(); foreach ( (array) $value as $item ) { $item = trim( wp_strip_all_tags( (string) $item ) ); if ( '' === $item ) { continue; } if ( 'path' === $type ) { $item = '/' . ltrim( $item, '/' ); } if ( 'selector' === $type && ! preg_match( '/^[\w\s.#\-\[\]="\':>*,]+$/u', $item ) ) { continue; } if ( ! in_array( $item, $out, true ) ) { $out[] = mb_substr( $item, 0, 200 ); } } return array_slice( $out, 0, 200 ); } }