0 ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's own filter. if ( $code && isset( $langs[ $code ]['default_locale'] ) ) { return (string) $langs[ $code ]['default_locale']; } } $site = get_option( 'WPLANG' ); return $site ? (string) $site : 'en_US'; } /** * Is the page in another language than the shop's main one (the plugin's text language)? */ function dayslot_other_language() { static $memo = array(); if ( ! dayslot_multilingual() || ( is_admin() && ! wp_doing_ajax() ) ) { return false; } $here = determine_locale(); if ( ! isset( $memo[ $here ] ) ) { $memo[ $here ] = dayslot_plugin_locale( $here, 'dayslot' ) !== dayslot_plugin_locale( dayslot_main_locale(), 'dayslot' ); } return $memo[ $here ]; } /** * The plugin's translations in one language: array( English => translation ), empty for English. */ function dayslot_locale_strings( $locale ) { static $memo = array(); $locale = dayslot_plugin_locale( $locale, 'dayslot' ); if ( ! isset( $memo[ $locale ] ) ) { $memo[ $locale ] = array(); foreach ( array( WP_LANG_DIR . '/plugins/dayslot-' . $locale . '.l10n.php', DAYSLOT_DIR . 'languages/dayslot-' . $locale . '.l10n.php' ) as $file ) { if ( 'en_US' !== $locale && is_readable( $file ) ) { $data = include $file; $memo[ $locale ] = is_array( $data ) && isset( $data['messages'] ) ? (array) $data['messages'] : array(); break; } } } return $memo[ $locale ]; } /** * The default settings with the texts in one language: array( English => translation ), empty = English. */ function dayslot_defaults_in( $strings ) { $swap = function ( $translation, $text, $domain ) use ( $strings ) { return 'dayslot' === $domain ? ( isset( $strings[ $text ] ) ? $strings[ $text ] : $text ) : $translation; }; $with_context = function ( $translation, $text, $context, $domain ) use ( $swap ) { return $swap( $translation, $text, $domain ); }; add_filter( 'gettext', $swap, PHP_INT_MAX, 3 ); add_filter( 'gettext_with_context', $with_context, PHP_INT_MAX, 4 ); $defaults = dayslot_defaults(); remove_filter( 'gettext', $swap, PHP_INT_MAX ); remove_filter( 'gettext_with_context', $with_context, PHP_INT_MAX ); return $defaults; } /** * Setting keys that hold a text for customers: array( key => array( English default, main-language default ) ). * A text is a default that changes with the language (not values like "yes", "contact" or colours). */ function dayslot_text_settings() { static $out = null; if ( null === $out ) { $out = array(); $orig = dayslot_defaults_in( array() ); $main = dayslot_defaults_in( dayslot_locale_strings( dayslot_main_locale() ) ); $here = dayslot_defaults_in( dayslot_locale_strings( determine_locale() ) ); foreach ( $orig as $key => $value ) { if ( is_string( $value ) && '' !== $value && isset( $main[ $key ], $here[ $key ] ) && ( $main[ $key ] !== $value || $here[ $key ] !== $value ) ) { $out[ $key ] = array( $value, (string) $main[ $key ] ); } } } return $out; } /** * Lists in the settings with the shop's own texts: delivery kinds and checkout fields. */ function dayslot_text_lists() { return (array) apply_filters( 'dayslot_text_lists', array( 'variants' => array( 'label', 'note' ), 'fields' => array( 'label', 'hint', 'options' ), ) ); } /** * A shop text in the page language: its string translation (Polylang / WPML) when there is one. */ function dayslot_string( $name, $value ) { if ( function_exists( 'pll__' ) ) { return pll__( $value ); } if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { return (string) apply_filters( 'wpml_translate_single_string', $value, 'Dayslot', $name ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's own filter. } return $value; } // Saved settings on a page in another language. add_filter( 'dayslot_saved_settings', function ( $saved ) { if ( ! dayslot_other_language() ) { return $saved; } foreach ( dayslot_text_settings() as $key => $texts ) { if ( ! isset( $saved[ $key ] ) || ! is_string( $saved[ $key ] ) ) { continue; } if ( in_array( $saved[ $key ], $texts, true ) ) { unset( $saved[ $key ] ); // The default, in the page language. } else { $saved[ $key ] = dayslot_string( 'dayslot_' . $key, $saved[ $key ] ); } } foreach ( dayslot_text_lists() as $list => $fields ) { if ( empty( $saved[ $list ] ) || ! is_array( $saved[ $list ] ) ) { continue; } foreach ( $saved[ $list ] as $i => $row ) { foreach ( $fields as $field ) { if ( ! empty( $row[ $field ] ) && ! empty( $row['id'] ) && is_string( $row[ $field ] ) ) { $saved[ $list ][ $i ][ $field ] = dayslot_string( 'dayslot_' . $list . '_' . $row['id'] . '_' . $field, $row[ $field ] ); } } } } return $saved; } ); // Texts tab: on a page in another language, an own text shows only when it has a string translation; // otherwise the plugin's translation of that language. add_filter( 'dayslot_text_override', function ( $text, $original, $translation ) { if ( ! dayslot_other_language() ) { return $text; } $translated = dayslot_string( 'dayslot_text_' . md5( $original ), $text ); return $translated !== $text ? $translated : $translation; }, 10, 3 ); // Register the shop's own texts (the strings list of Polylang / WPML is filled from the admin). add_action( 'admin_init', function () { if ( ! dayslot_multilingual() ) { return; } $saved = get_option( 'dayslot_settings', array() ); $saved = is_array( $saved ) ? $saved : array(); $list = array(); foreach ( dayslot_text_settings() as $key => $texts ) { if ( isset( $saved[ $key ] ) && is_string( $saved[ $key ] ) && '' !== $saved[ $key ] && ! in_array( $saved[ $key ], $texts, true ) ) { $list[ 'dayslot_' . $key ] = $saved[ $key ]; } } foreach ( dayslot_text_lists() as $name => $fields ) { foreach ( isset( $saved[ $name ] ) ? (array) $saved[ $name ] : array() as $row ) { foreach ( $fields as $field ) { if ( ! empty( $row[ $field ] ) && ! empty( $row['id'] ) && is_string( $row[ $field ] ) ) { $list[ 'dayslot_' . $name . '_' . $row['id'] . '_' . $field ] = $row[ $field ]; } } } } $texts = get_option( 'dayslot_texts', array() ); foreach ( dayslot_customer_texts() as $group ) { foreach ( $group as $row ) { $own = is_array( $texts ) && isset( $texts[ dayslot_text_key( $row[0], $row[1] ) ] ) ? $texts[ dayslot_text_key( $row[0], $row[1] ) ] : ''; if ( '' !== $own ) { $list[ 'dayslot_text_' . md5( $row[1] ) ] = $own; } } } foreach ( $list as $name => $value ) { if ( function_exists( 'pll_register_string' ) ) { pll_register_string( $name, $value, 'Dayslot', str_contains( $value, "\n" ) ); } else { do_action( 'wpml_register_single_string', 'Dayslot', $name, $value ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's own action. } } } );