Completed
Push — develop ( a41371...a3f86d )
by Aristeides
02:14
created

Kirki_L10n::get_path()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 0
dl 0
loc 17
rs 9.2
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     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() ) {
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
	 * @param bool   $override Whether to override the text domain. Default false.
112
	 * @param string $domain   Text domain. Unique identifier for retrieving translated strings.
113
	 * @return bool
114
	 */
115
	function override_load_textdomain( $override, $domain, $mofile ) {
0 ignored issues
show
Unused Code introduced by
The parameter $mofile is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
116
117
		global $l10n;
118
		if ( isset( $l10n[ $this->get_theme_textdomain() ] ) ) {
119
			$l10n['kirki'] = $l10n[ $this->get_theme_textdomain() ];
120
		}
121
122
		// Check if the domain is "kirki".
123
		if ( 'kirki' === $domain ) {
124
			return true;
125
		}
126
		return $override;
127
128
	}
129
130
	/**
131
	 * Get the theme's textdomain.
132
	 *
133
	 * @since 3.0.12
134
	 * @access private
135
	 * @return string
136
	 */
137
	private function get_theme_textdomain() {
138
139
		if ( '' === $this->theme_textdomain ) {
140
141
			// Get the textdomain.
142
			$theme = wp_get_theme();
143
			$this->theme_textdomain = $theme->get( 'TextDomain' );
144
145
			// If no texdomain was found, use the template folder name.
146
			if ( ! $this->theme_textdomain ) {
147
				$this->theme_textdomain = get_template();
148
			}
149
		}
150
		return $this->theme_textdomain;
151
152
	}
153
}
154