Passed
Push — develop ( 3892e2...b1663d )
by Aristeides
03:47
created

core/class-kirki-l10n.php (1 issue)

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     http://opensource.org/licenses/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
	/**
55
	 * Load the plugin textdomain
56
	 *
57
	 * @access public
58
	 */
59
	public function load_textdomain() {
60
61
		if ( null !== $this->get_path() ) {
0 ignored issues
show
The condition null !== $this->get_path() is always true.
Loading history...
62
			load_textdomain( $this->textdomain, $this->get_path() );
63
		}
64
		load_plugin_textdomain( $this->textdomain, false, Kirki::$path . '/languages' );
65
66
	}
67
68
	/**
69
	 * Gets the path to a translation file.
70
	 *
71
	 * @access protected
72
	 * @return string Absolute path to the translation file.
73
	 */
74
	protected function get_path() {
75
		$path_found = false;
76
		$found_path = null;
77
		foreach ( $this->get_paths() as $path ) {
78
			if ( $path_found ) {
79
				continue;
80
			}
81
			$path = wp_normalize_path( $path );
82
			if ( file_exists( $path ) ) {
83
				$path_found = true;
84
				$found_path = $path;
85
			}
86
		}
87
88
		return $found_path;
89
90
	}
91
92
	/**
93
	 * Returns an array of paths where translation files may be located.
94
	 *
95
	 * @access protected
96
	 * @return array
97
	 */
98
	protected function get_paths() {
99
100
		return array(
101
			WP_LANG_DIR . '/' . $this->textdomain . '-' . get_locale() . '.mo',
102
			Kirki::$path . '/languages/' . $this->textdomain . '-' . get_locale() . '.mo',
103
		);
104
105
	}
106
107
	/**
108
	 * Allows overriding the "kirki" textdomain from a theme.
109
	 *
110
	 * @since 3.0.12
111
	 * @access public
112
	 * @param bool   $override Whether to override the .mo file loading. Default false.
113
	 * @param string $domain   Text domain. Unique identifier for retrieving translated strings.
114
	 * @param string $mofile   Path to the MO file.
115
	 * @return bool
116
	 */
117
	public function override_load_textdomain( $override, $domain, $mofile ) {
118
119
		global $l10n;
120
		if ( isset( $l10n[ $this->get_theme_textdomain() ] ) ) {
121
			// @codingStandardsIgnoreLine WordPress.Variables.GlobalVariables.OverrideProhibited
122
			$l10n['kirki'] = $l10n[ $this->get_theme_textdomain() ];
123
		}
124
125
		// Check if the domain is "kirki".
126
		if ( 'kirki' === $domain ) {
127
			return true;
128
		}
129
		return $override;
130
131
	}
132
133
	/**
134
	 * Get the theme's textdomain.
135
	 *
136
	 * @since 3.0.12
137
	 * @access private
138
	 * @return string
139
	 */
140
	private function get_theme_textdomain() {
141
142
		if ( '' === $this->theme_textdomain ) {
143
144
			// Get the textdomain.
145
			$theme                  = wp_get_theme();
146
			$this->theme_textdomain = $theme->get( 'TextDomain' );
147
148
			// If no texdomain was found, use the template folder name.
149
			if ( ! $this->theme_textdomain ) {
150
				$this->theme_textdomain = get_template();
151
			}
152
		}
153
		return $this->theme_textdomain;
154
155
	}
156
}
157