Kirki_L10n::load_textdomain()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Internationalization helper.
4
 *
5
 * @package     Kirki
6
 * @category    Core
7
 * @author      Aristeides Stathopoulos
8
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
9
 * @license    https://opensource.org/licenses/MIT
10
 * @since       1.0
11
 */
12
13
/**
14
 * Handles translations
15
 */
16
class Kirki_L10n {
17
18
	/**
19
	 * The plugin textdomain
20
	 *
21
	 * @access private
22
	 * @var string
23
	 */
24
	private $textdomain = 'kirki';
25
26
	/**
27
	 * The theme textdomain
28
	 *
29
	 * @access private
30
	 * @var string
31
	 */
32
	private $theme_textdomain = '';
33
34
	/**
35
	 * The class constructor.
36
	 * Adds actions & filters to handle the rest of the methods.
37
	 *
38
	 * @access public
39
	 */
40
	public function __construct() {
41
42
		// If Kirki is installed as a plugin, load the texdomain.
43
		if ( Kirki_Util::is_plugin() ) {
44
			add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
45
			return;
46
		}
47
48
		// If we got this far, then Kirki is embedded in a plugin.
49
		// We want the theme's textdomain to handle translations.
50
		add_filter( 'override_load_textdomain', array( $this, 'override_load_textdomain' ), 5, 3 );
51
	}
52
53
	/**
54
	 * Load the plugin textdomain
55
	 *
56
	 * @access public
57
	 */
58
	public function load_textdomain() {
59
		if ( null !== $this->get_path() ) {
0 ignored issues
show
introduced by Aristeides Stathopoulos
The condition null !== $this->get_path() is always true.
Loading history...
60
			load_textdomain( $this->textdomain, $this->get_path() );
61
		}
62
		load_plugin_textdomain( $this->textdomain, false, Kirki::$path . '/languages' );
63
	}
64
65
	/**
66
	 * Gets the path to a translation file.
67
	 *
68
	 * @access protected
69
	 * @return string Absolute path to the translation file.
70
	 */
71
	protected function get_path() {
72
		$path_found = false;
73
		$found_path = null;
74
		foreach ( $this->get_paths() as $path ) {
75
			if ( $path_found ) {
76
				continue;
77
			}
78
			$path = wp_normalize_path( $path );
79
			if ( file_exists( $path ) ) {
80
				$path_found = true;
81
				$found_path = $path;
82
			}
83
		}
84
		return $found_path;
85
	}
86
87
	/**
88
	 * Returns an array of paths where translation files may be located.
89
	 *
90
	 * @access protected
91
	 * @return array
92
	 */
93
	protected function get_paths() {
94
		return array(
95
			WP_LANG_DIR . '/' . $this->textdomain . '-' . get_locale() . '.mo',
96
			Kirki::$path . '/languages/' . $this->textdomain . '-' . get_locale() . '.mo',
97
		);
98
	}
99
100
	/**
101
	 * Allows overriding the "kirki" textdomain from a theme.
102
	 *
103
	 * @since 3.0.12
104
	 * @access public
105
	 * @param bool   $override Whether to override the .mo file loading. Default false.
106
	 * @param string $domain   Text domain. Unique identifier for retrieving translated strings.
107
	 * @param string $mofile   Path to the MO file.
108
	 * @return bool
109
	 */
110
	public function override_load_textdomain( $override, $domain, $mofile ) {
111
		global $l10n;
112
		if ( isset( $l10n[ $this->get_theme_textdomain() ] ) ) {
113
			$l10n['kirki'] = $l10n[ $this->get_theme_textdomain() ]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
0 ignored issues
show
introduced by aristath
Overriding WordPress globals is prohibited. Found assignment to $l10n
Loading history...
Coding Style introduced by aristath
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
114
		}
115
116
		// Check if the domain is "kirki".
117
		if ( 'kirki' === $domain ) {
118
			return true;
119
		}
120
		return $override;
121
	}
122
123
	/**
124
	 * Get the theme's textdomain.
125
	 *
126
	 * @since 3.0.12
127
	 * @access private
128
	 * @return string
129
	 */
130
	private function get_theme_textdomain() {
131
		if ( '' === $this->theme_textdomain ) {
132
133
			// Get the textdomain.
134
			$theme                  = wp_get_theme();
135
			$this->theme_textdomain = $theme->get( 'TextDomain' );
136
137
			// If no texdomain was found, use the template folder name.
138
			if ( ! $this->theme_textdomain ) {
139
				$this->theme_textdomain = get_template();
140
			}
141
		}
142
		return $this->theme_textdomain;
143
	}
144
}
145