wpml-st-translations-file-components-find-theme.php
1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
class WPML_ST_Translations_File_Components_Find_Theme implements WPML_ST_Translations_File_Components_Find {
/** @var WPML_Debug_BackTrace */
private $debug_backtrace;
/** @var WPML_File $file */
private $file;
/** @var string */
private $theme_dir;
/**
* @param WPML_Debug_BackTrace $debug_backtrace
* @param WPML_File $file
*/
public function __construct( WPML_Debug_BackTrace $debug_backtrace, WPML_File $file ) {
$this->debug_backtrace = $debug_backtrace;
$this->file = $file;
$this->theme_dir = $this->file->fix_dir_separator( get_theme_root() );
}
public function find_id( $file ) {
return $this->find_theme_directory( $file );
}
private function find_theme_directory( $file ) {
if ( false !== strpos( $file, $this->theme_dir ) ) {
return $this->extract_theme_directory( $file );
}
return $this->find_theme_directory_in_backtrace();
}
private function find_theme_directory_in_backtrace() {
$file = $this->find_file_in_backtrace();
if ( ! $file ) {
return null;
}
return $this->extract_theme_directory( $file );
}
private function find_file_in_backtrace() {
$stack = $this->debug_backtrace->get_backtrace();
foreach ( $stack as $call ) {
if ( isset( $call['function'] ) && 'load_theme_textdomain' === $call['function'] ) {
return $call['file'];
}
}
return null;
}
/**
* @param string $file_path
*
* @return string
*/
private function extract_theme_directory( $file_path ) {
$file_path = $this->file->fix_dir_separator( $file_path );
$dir = ltrim( str_replace( $this->theme_dir, '', $file_path ), DIRECTORY_SEPARATOR );
$dir = explode( DIRECTORY_SEPARATOR, $dir );
return trim( $dir[0], DIRECTORY_SEPARATOR );
}
}