| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Translates a string ( previously registered with wpml_register_string ) |
| 5 |
* |
| 6 |
* @param string $string the string to translate |
| 7 |
* @return string the string translation in the current language |
| 8 |
* @since 1.1 |
| 9 |
* @since 1.3.28 manage string with encoded caractere |
| 10 |
* @update 1.3.52 remove the 1.3.28 fix done directly in translate_string_recursive with wp_kses_normalize_entities |
| 11 |
* |
| 12 |
*/ |
| 13 |
function falang__($string) |
| 14 |
{ |
| 15 |
return is_scalar($string) ? __(trim($string), 'falang_string') : $string; // PHPCS:ignore WordPress.WP.I18n |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Translates a string ( previously registered with falang_register_string ) |
| 20 |
* |
| 21 |
* @param string $string the string to translate |
| 22 |
* @param string $lang language code |
| 23 |
* @return string the string translation in the requested language |
| 24 |
* @since 1.1 |
| 25 |
* |
| 26 |
*/ |
| 27 |
function falang_translate_string( $string, $lang ) { |
| 28 |
|
| 29 |
if (Falang() instanceof Falang_Public && falang_current_language() == $lang ) { |
| 30 |
return falang__( $string ); |
| 31 |
} |
| 32 |
|
| 33 |
if ( ! is_scalar( $string ) ) { |
| 34 |
return $string; |
| 35 |
} |
| 36 |
|
| 37 |
static $cache; // Cache object to avoid loading the same translations object several times |
| 38 |
|
| 39 |
if ( empty( $cache ) ) { |
| 40 |
$cache = new Falang\Core\Cache(); |
| 41 |
} |
| 42 |
|
| 43 |
if ( false === $mo = $cache->get( $lang ) ) { |
| 44 |
$mo = new Falang\Core\FALANG_MO(); |
| 45 |
$mo->import_from_db( FALANG()->get_model()->get_language_by_slug( $lang ) ); |
| 46 |
$cache->set( $lang, $mo ); |
| 47 |
} |
| 48 |
|
| 49 |
return $mo->translate( $string ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Returns the current language on frontend |
| 54 |
* Returns the language set in admin language filter on backend ( false if set to all languages ) |
| 55 |
* @function similar from Polylang pll_current_language |
| 56 |
* |
| 57 |
* @since 1.1 |
| 58 |
* |
| 59 |
* @param string $field Optional, the language field to return ( see Falang Language ), defaults to 'slug', pass OBJECT constant to get the language object. |
| 60 |
* @return string|Falang_Language|bool The requested field for the current language |
| 61 |
*/ |
| 62 |
function falang_current_language( $field = 'slug' ) { |
| 63 |
if ( OBJECT === $field ) { |
| 64 |
return Falang()->get_current_language(); |
| 65 |
} |
| 66 |
return isset( Falang()->get_current_language()->$field ) ? Falang()->get_current_language()->$field : false; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Returns the default language. |
| 71 |
* @function similar from Polylang falang_current_language |
| 72 |
* |
| 73 |
* @api |
| 74 |
* @since 1.3.35 |
| 75 |
* |
| 76 |
* @param string $field Optional, the language field to return, defaults to 'slug'. Pass OBJECT constant to get the language object. |
| 77 |
* @return string|Falang_Language|false The requested field for the default language. |
| 78 |
*/ |
| 79 |
function falang_default_language( $field = 'slug' ) { |
| 80 |
$lang = Falang()->model->get_default_language(); |
| 81 |
if ( $lang ) { |
| 82 |
if ( OBJECT === $field ) { |
| 83 |
return $lang; |
| 84 |
} |
| 85 |
return isset( $lang->$field ) ? $lang->$field : false; |
| 86 |
} |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Registers a string for translation in the "strings translation" panel |
| 92 |
* |
| 93 |
* @since 1.3.2 |
| 94 |
* |
| 95 |
* @param string $name a unique name for the string |
| 96 |
* @param string $string the string to register |
| 97 |
* @param string $context optional the group in which the string is registered, defaults to 'falang' |
| 98 |
* @param bool $multiline optional whether the string table should display a multiline textarea or a single line input, defaults to single line |
| 99 |
*/ |
| 100 |
function falang_register_string( $name, $string, $context = 'falang', $multiline = false ) { |
| 101 |
if ( Falang() instanceof Falang_Admin ) { |
| 102 |
//PLL_Admin_Strings::register_string( $name, $string, $context, $multiline );//polylang method |
| 103 |
FALANG_WPML_Compat::instance()->register_string( $context, $name, $string ); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Our own version of wc_clean to prevent errors in case WC gets deactivated |
| 109 |
* @param array|string $var |
| 110 |
* @return array|string |
| 111 |
*/ |
| 112 |
function falang_clean( $var ) { |
| 113 |
if ( is_callable( 'wc_clean' ) ) { |
| 114 |
return wc_clean( $var ); |
| 115 |
} else { |
| 116 |
if ( is_array( $var ) ) { |
| 117 |
return array_map( 'wpla_clean', $var ); |
| 118 |
} else { |
| 119 |
return is_scalar( $var ) ? sanitize_text_field( $var ) : $var; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Returns the list of available languages. |
| 126 |
* based on the pll_languages_list to follow the same idee |
| 127 |
* |
| 128 |
* @api |
| 129 |
* @since 1.3.61 |
| 130 |
* |
| 131 |
* @param array $args { |
| 132 |
* Optional array of arguments. |
| 133 |
* |
| 134 |
*/ |
| 135 |
function falang_languages_list( $args = array() ) { |
| 136 |
return Falang()->model->get_languages_list( $args ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Display message when paid version activated |
| 141 |
* Since 1.3.29 |
| 142 |
*/ |
| 143 |
function display_free_message(){ |
| 144 |
$file_path = 'falang-pro/falang-pro.php'; |
| 145 |
if (!is_plugin_active($file_path)) { |
| 146 |
echo ' |
| 147 |
<div class="falang_free_msg"> |
| 148 |
<div class="alert alert-warning"> |
| 149 |
<p>'; |
| 150 |
echo __('You are using the FREE version of Falang.<br />Support the development by upgrading to the paid version: you will get full functionality as well as on-site support', 'falang'); |
| 151 |
echo ' |
| 152 |
</p> |
| 153 |
<a class="btn btn-free-msg" target="_blank" |
| 154 |
href="https://www.faboba.com/en/wordpress/falang-for-wordpress/telechargement-achat.html?utm_source=WordPress&utm_medium=upgradebutton&utm_campaign=freeversion"> |
| 155 |
<i class="fas fa-heart"></i>'; |
| 156 |
echo __("Upgrade to Paid", "falang"); |
| 157 |
echo ' |
| 158 |
</a> |
| 159 |
</div> |
| 160 |
</div> |
| 161 |
'; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
/** |
| 169 |
* Allows to access the Falang instance |
| 170 |
* It is always preferable to use API functions |
| 171 |
* Internal methods may be changed without prior notice |
| 172 |
* |
| 173 |
* @since 1.1 |
| 174 |
*/ |
| 175 |
function FALANG() { // PHPCS:ignore WordPress.NamingConventions.ValidFunctionName |
| 176 |
return $GLOBALS['falang_core']; |
| 177 |
} |
| 178 |
|