GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
Translations/Framework.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @license GPL-2.0-or-later
4  *
5  * Modified by gravityview on 13-January-2023 using Strauss.
6  * @see https://github.com/BrianHenryIE/strauss
7  */
8 
10 
14 
15 class Framework {
16  const ID = 'gk-translations';
17 
18  const WP_LANG_DIR = WP_LANG_DIR . '/plugins';
19 
20  /**
21  * Class instance.
22  *
23  * @since 1.0.0
24  *
25  * @var Framework
26  */
27  private static $_instance;
28 
29  /**
30  * Logger class instance.
31  *
32  * @since 1.0.0
33  *
34  * @var LoggerFramework
35  */
36  private $_logger;
37 
38  /**
39  * @since 1.0.0
40  *
41  * @var array Text domain for which translations are fetched.
42  */
43  private $_text_domains = [];
44 
45  /**
46  * Returns class instance.
47  *
48  * @since 1.0.0
49  *
50  * @return Framework
51  */
52  public static function get_instance() {
53  if ( ! self::$_instance ) {
54  self::$_instance = new self();
55  }
56 
57  return self::$_instance;
58  }
59 
60  /**
61  * Returns TranslationsPress updater instance.
62  *
63  * @since 1.0.0
64  *
65  * @param string $text_domain Text domain.
66  *
67  * @return TranslationsPress_Updater
68  */
69  public function get_T15s_updater( $text_domain ) {
70  return TranslationsPress_Updater::get_instance( $text_domain );
71  }
72 
73  /**
74  * Initializes Translations framework.
75  *
76  * @since 1.0.0
77  *
78  * @return void
79  */
80  public function init() {
81  if ( did_action( 'gk/foundation/translations/initialized' ) ) {
82  return;
83  }
84 
85  foreach ( FoundationCore::get_instance()->get_registered_plugins() as $plugin_file ) {
86  $plugin_data = CoreHelpers::get_plugin_data( $plugin_file );
87 
88  if ( isset( $plugin_data['TextDomain'] ) ) {
89  $this->_text_domains[] = $plugin_data['TextDomain'];
90  }
91  }
92 
93  if ( empty( $this->_text_domains ) ) {
94  return;
95  }
96 
97  add_action( 'update_option_WPLANG', [ $this, 'on_site_language_change' ], 10, 2 );
98  add_action( 'gk/foundation/plugin_activated', [ $this, 'on_plugin_activation' ] );
99  add_action( 'gk/foundation/plugin_deactivated', [ $this, 'on_plugin_deactivation' ] );
100 
101  $this->_logger = LoggerFramework::get_instance();
102 
103  /**
104  * @action `gk/foundation/translations/initialized` Fires when the class has finished initializing.
105  *
106  * @since 1.0.0
107  *
108  * @param $this
109  */
110  do_action( 'gk/foundation/translations/initialized', $this );
111  }
112 
113  /**
114  * Checks of user has permissions to install languages.
115  *
116  * @since 1.0.0
117  *
118  * @return bool
119  */
120  public function can_install_languages() {
121  /**
122  * @filter `gk/foundation/translations/permissions/can_install_languages` Sets permission to install languages.
123  *
124  * @since 1.0.0
125  *
126  * @param bool $can_install_languages Default: 'install_languages' capability.
127  */
128  return apply_filters( 'gk/foundation/translations/permissions/can-install-languages', current_user_can( 'install_languages' ) );
129  }
130 
131  /**
132  * Downloads and installs translations from TranslationsPress.
133  *
134  * @since 1.0.0
135  *
136  * @param string $text_domain Text domain.
137  * @param string $new_language The new site language, only set if user is updating their language settings.
138  *
139  * @return void
140  */
141  public function install( $text_domain, $new_language ) {
142  $current_user = wp_get_current_user();
143 
144  if ( ! $this->can_install_languages() ) {
145  $this->_logger->addError(
146  sprintf(
147  'User "%s" does not have permissions to install languages.',
148  $current_user->user_login
149  )
150  );
151  }
152 
153  try {
154  $T15s_updater = $this->get_T15s_updater( $text_domain );
155 
156  $T15s_updater->install( $new_language );
157 
158  $translations = $T15s_updater->get_installed_translations( true );
159 
160  if ( isset( $translations[ $new_language ] ) ) {
161  $this->load_backend_translations( $text_domain, $new_language );
162  }
163  } catch ( \Exception $e ) {
164  $this->_logger->addError( $e->getMessage() );
165  }
166  }
167 
168  /**
169  * Loads and sets frontend and backend translations.
170  *
171  * @since 1.0.0
172  *
173  * @param string $text_domain Text domain.
174  * @param string $language (optional) Language to load. Default is site locale.
175  *
176  * @return void
177  */
178  public function load_all_translations( $text_domain, $language = '' ) {
179  $this->load_backend_translations( $text_domain, $language );
180  $this->load_frontend_translations( $text_domain, $language );
181  }
182 
183  /**
184  * Loads and sets backend translations.
185  *
186  * @since 1.0.0
187  *
188  * @param string $text_domain Text domain.
189  * @param string $language (optional) Language to load. Default is site locale.
190  *
191  * @return void
192  */
193  function load_backend_translations( $text_domain, $language = '' ) {
194  if ( ! $language ) {
195  $language = get_locale();
196  }
197 
198  $mo_translations = $this->get_translation_filename( $text_domain, $language );
199 
200  if ( ! $mo_translations ) {
201  $this->_logger->notice(
202  sprintf(
203  'No "%s" .mo translations found for "%s".',
204  $text_domain,
205  $language
206  )
207  );
208 
209  return;
210  }
211 
212  load_textdomain( $text_domain, $mo_translations );
213  }
214 
215  /**
216  * Loads and sets frontend translations.
217  *
218  * @since 1.0.0
219  *
220  * @param string $text_domain Text domain.
221  * @param string $language (optional) Language to load. Default is site locale.
222  * @param string $frontend_text_domain (optional) Frontend text domain if different from the backend text domain (e.g., plugin uses 'foo', but JS uses 'bar' for the same translations).
223  *
224  * @return void
225  */
226  function load_frontend_translations( $text_domain, $language = '', $frontend_text_domain = '' ) {
227  if ( ! $language ) {
228  $language = get_locale();
229  }
230 
231  if ( $this->is_en_locale( $language ) ) {
232  return;
233  }
234 
235  $json_translations = $this->get_translation_filename( $text_domain, $language, 'json' );
236 
237  if ( ! $json_translations ) {
238  $this->_logger->notice(
239  sprintf(
240  'No %s.json translations file found for "%s" text domain.',
241  $text_domain ?: $frontend_text_domain,
242  $language
243  )
244  );
245 
246  return;
247  }
248 
249  $json_translations = file_get_contents( $json_translations );
250 
251  // Optionally override text domain if UI expects a different one.
252  $text_domain = $frontend_text_domain ?: $text_domain;
253 
254  add_filter( 'gk/foundation/inline-scripts', function ( $scripts ) use ( $text_domain, $json_translations ) {
255  $js = <<<JS
256 ( function( domain, translations ) {
257  var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
258  localeData[""].domain = domain;
259  wp.i18n.setLocaleData( localeData, domain );
260 } )( '${text_domain}', ${json_translations});
261 JS;
262 
263  $scripts[] = [
264  'script' => $js,
265  'dependencies' => [ 'wp-i18n' ],
266  ];
267 
268  return $scripts;
269  } );
270  }
271 
272  /**
273  * Returns the translation filename for a given language.
274  *
275  * @param string $text_domain Text domain.
276  * @param string $language Translation language (e.g. 'en_EN').
277  * @param string $extension (optional) File extension. Default is 'mo'.
278  *
279  * @return string|null
280  */
281  public static function get_translation_filename( $text_domain, $language, $extension = 'mo' ) {
282  $filename = sprintf( '%s/%s-%s.%s', self::WP_LANG_DIR, $text_domain, $language, $extension );
283 
284  return ( file_exists( $filename ) ) ? $filename : null;
285  }
286 
287  /**
288  * Installs or updates translations for all plugins when the site's language setting is changed.
289  *
290  * @since 1.0.0
291  *
292  * @param string $from_language The language before the user changed their language setting.
293  * @param string $to_language The new language after the user changed their language setting.
294  *
295  * @return void
296  */
297  public function on_site_language_change( $from_language, $to_language ) {
298  if ( empty( $to_language ) || ! $this->can_install_languages() ) {
299  return;
300  }
301 
302  foreach ( $this->_text_domains as $text_domain ) {
303  $this->install( $text_domain, $to_language );
304  }
305  }
306 
307  /**
308  * Installs or updates translations for all plugins when a plugin is activated.
309  *
310  * @since 1.0.0
311  *
312  * @param string $plugin_file Plugin file.
313  *
314  * @return void
315  */
316  public function on_plugin_activation( $plugin_file ) {
317  if ( ! function_exists( 'get_plugin_data' ) ) {
318  require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
319  }
320 
321  $plugin_data = get_plugin_data( $plugin_file );
322 
323  if ( ! isset( $plugin_data['TextDomain'] ) || $this->is_en_locale() || ! $this->can_install_languages() ) {
324  return;
325  }
326 
327  $this->install( $plugin_data['TextDomain'], get_locale() );
328  }
329 
330  /**
331  * Deletes translations when the plugin is deactivated.
332  *
333  * @since 1.0.0
334  *
335  * @param string $plugin_file Plugin file.
336  *
337  * @return void
338  */
339  public function on_plugin_deactivation( $plugin_file ) {
340  if ( ! function_exists( 'get_plugin_data' ) ) {
341  require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
342  }
343 
344  $plugin_data = get_plugin_data( $plugin_file );
345 
346  if ( ! isset( $plugin_data['TextDomain'] ) || $this->is_en_locale() || ! $this->can_install_languages() ) {
347  return;
348  }
349 
350  $files = glob( sprintf(
351  '%s/%s-*',
352  self::WP_LANG_DIR,
353  $plugin_data['TextDomain']
354  ) );
355 
356  if ( empty( $files ) ) {
357  return;
358  }
359 
360  array_walk( $files, 'wp_delete_file' );
361  }
362 
363  /**
364  * Checks whether the current locale is set to English language.
365  *
366  * @since 1.0.0
367  *
368  * @param string $locale (optional) Locale to check. Default is site locale.
369  *
370  * @return bool
371  */
372  public function is_en_locale( $locale = '' ) {
373  if ( ! $locale ) {
374  $locale = get_locale();
375  }
376 
377  // en_EN = en_US; en_GB and en_CA can have their own "translations" due to differences in spelling.
378  return in_array( $locale, [ 'en_EN', 'en_US' ], true );
379  }
380 }
install( $text_domain, $new_language)
Downloads and installs translations from TranslationsPress.
on_site_language_change( $from_language, $to_language)
Installs or updates translations for all plugins when the site&#39;s language setting is changed...
on_plugin_activation( $plugin_file)
Installs or updates translations for all plugins when a plugin is activated.
static get_translation_filename( $text_domain, $language, $extension='mo')
Returns the translation filename for a given language.
static get_instance( $slug, $translations_path='')
Returns an instance of this class for the given slug.
load_backend_translations( $text_domain, $language='')
Loads and sets backend translations.
load_frontend_translations( $text_domain, $language='', $frontend_text_domain='')
Loads and sets frontend translations.
is_en_locale( $locale='')
Checks whether the current locale is set to English language.
on_plugin_deactivation( $plugin_file)
Deletes translations when the plugin is deactivated.
can_install_languages()
Checks of user has permissions to install languages.
get_T15s_updater( $text_domain)
Returns TranslationsPress updater instance.
load_all_translations( $text_domain, $language='')
Loads and sets frontend and backend translations.