Completed
Branch BUG/load-translations (0ece5b)
by
unknown
08:42 queued 48s
created

EE_Load_Textdomain::loadTranslationsForLocale()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 47
rs 9.1563
c 0
b 0
f 0
1
<?php
2
3
use EventEspresso\core\exceptions\InvalidDataTypeException;
4
use EventEspresso\core\exceptions\InvalidInterfaceException;
5
6
/**
7
 * EE_Load_Textdomain
8
 *
9
 * @package        Event Espresso
10
 * @subpackage     /includes/core/EE_Load_Textdomain.core.php
11
 * @author         Darren Ethier
12
 */
13
class EE_Load_Textdomain extends EE_Base
14
{
15
16
    /**
17
     * holds the current lang in WP
18
     *
19
     * @var string
20
     */
21
    private static $locale;
22
23
24
    /**
25
     * this takes care of retrieving a matching textdomain for event espresso for the current WPLANG from EE GitHub
26
     * repo (if necessary) and then loading it for translations. should only be called in wp plugins_loaded callback
27
     *
28
     * @return void
29
     * @throws EE_Error
30
     * @throws InvalidArgumentException
31
     * @throws ReflectionException
32
     * @throws InvalidDataTypeException
33
     * @throws InvalidInterfaceException
34
     */
35
    public static function load_textdomain()
36
    {
37
        EE_Load_Textdomain::loadTranslationsForLocale();
38
        // now load the textdomain
39
        if (!empty(EE_Load_Textdomain::$locale)) {
40
            $github_mo_path = EE_LANGUAGES_SAFE_DIR . 'event_espresso-' . EE_Load_Textdomain::$locale . '.mo';
41
            if (is_readable($github_mo_path)) {
42
                load_plugin_textdomain('event_espresso', false, EE_LANGUAGES_SAFE_LOC);
43
                return;
44
            }
45
            $glotpress_mo_path = EE_LANGUAGES_SAFE_DIR . 'event_espresso-4-' . EE_Load_Textdomain::$locale . '.mo';
46
            if (is_readable($glotpress_mo_path)) {
47
                load_textdomain('event_espresso', $glotpress_mo_path);
48
                return;
49
            }
50
        }
51
        load_plugin_textdomain('event_espresso', false, dirname(EE_PLUGIN_BASENAME) . '/languages/');
52
    }
53
54
55
    /**
56
     * The purpose of this method is to sideload all of the lang files for EE, this includes the POT file and also the PO/MO files for the given WPLANG locale (if necessary).
57
     *
58
     * @access private
59
     * @static
60
     * @return void
61
     * @throws EE_Error
62
     * @throws InvalidArgumentException
63
     * @throws ReflectionException
64
     * @throws InvalidDataTypeException
65
     * @throws InvalidInterfaceException
66
     */
67
    private static function loadTranslationsForLocale()
68
    {
69
        EE_Load_Textdomain::$locale = get_locale();
70
        // can't download a language file if a language isn't set <taps temple>
71
        if (empty(EE_Load_Textdomain::$locale)) {
72
            return;
73
        }
74
        $language_check_option_name = 'ee_lang_check_' . EE_Load_Textdomain::$locale . '_' . EVENT_ESPRESSO_VERSION;
75
        // check if language files has already been sideloaded
76
        if (get_option($language_check_option_name)) {
77
            return;
78
        }
79
80
        $repo_base_URL = 'https://github.com/eventespresso/languages-ee4/blob/master/event_espresso';
81
82
        // load sideloader and sideload the .POT file as this should always be included.
83
        $sideloader_args = array(
84
            '_upload_to'     => EE_PLUGIN_DIR_PATH . 'languages/',
85
            '_download_from'   => $repo_base_URL .'.pot?raw=true',
86
            '_new_file_name' => 'event_espresso.pot',
87
        );
88
        /** @var EEH_Sideloader $sideloader */
89
        $sideloader = EE_Registry::instance()->load_helper('Sideloader', $sideloader_args, false);
90
        $sideloader->sideload();
91
92
        // if locale is "en_US" then lets just get out, since Event Espresso core is already "en_US"
93
        if (EE_Load_Textdomain::$locale === 'en_US') {
94
            // but set option first else we'll forever be downloading the pot file
95
            update_option($language_check_option_name, 1);
96
            return;
97
        }
98
        $repo_locale_URL = $repo_base_URL . '-' . EE_Load_Textdomain::$locale;
99
        $file_name_base = 'event_espresso-' . EE_Load_Textdomain::$locale;
100
101
        // made it here so let's get the language files from the github repo, first the .mo file
102
        $sideloader->set_download_from("{$repo_locale_URL}.mo?raw=true");
103
        $sideloader->set_new_file_name("{$file_name_base}.mo");
104
        $sideloader->sideload();
105
106
        // now the .po file:
107
        $sideloader->set_download_from("{$repo_locale_URL}.po?raw=true");
108
        $sideloader->set_new_file_name("{$file_name_base}.po");
109
        $sideloader->sideload();
110
111
        // set option so the above only runs when EE updates.
112
        update_option($language_check_option_name, 1);
113
    }
114
}
115