/var/www/html_sp/wp-content/plugins/loco-translate/src/package/Theme.php


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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
 * Represents a bundle of type "theme"
 */
class Loco_package_Theme extends Loco_package_Bundle {

    
/**
     * @var Loco_package_Theme|null
     */
    
private $parent;


    
/**
     * {@inheritdoc}
     */
    
public function getSystemTargets():array {
        return  [ 
            
trailingslashitloco_constant('LOCO_LANG_DIR') ).'themes',
            
trailingslashitloco_constant('WP_LANG_DIR') ).'themes',
        ];
    }


    
/**
     * {@inheritdoc}
     */
    
public function isTheme():bool {
        return 
true;
    }


    
/**
     * {@inheritdoc}
     */
    
public function getType():string {
        return 
'Theme';
    }


    
/**
     * {@inheritDoc}
     */
    
public function getDirectoryUrl(): string {
        
$slug $this->getHandle();
        return 
trailingslashit(get_theme_root_uri($slug)).$slug.'/';
    }


    
/**
     * {@inheritdoc}
     */
    
public function getHeaderInfo(): Loco_package_Header {
        
$root dirname$this->getDirectoryPath() );
        
$theme = new WP_Theme$this->getSlug(), $root );
        return new 
Loco_package_Header$theme );
    }


    
/**
     * {@inheritdoc}
     */
    
public function getMetaTranslatable(): array {
        return  [
            
'Name'        => 'Name of the theme',
            
'Description' => 'Description of the theme',
            
'ThemeURI'    => 'URI of the theme',
            
'Author'      => 'Author of the theme',
            
'AuthorURI'   => 'Author URI of the theme',
            
// 'Tags'        => 'Tags of the theme',
        
];
    }


    
/**
     * @inheritDoc
     */
    
public function getParent(): ?Loco_package_Theme {
        return 
$this->parent;
    }


    
/**
     * @return static[]
     */
    
public static function getAll(): array {
        
$themes = [];
        foreach( 
wp_get_themes(['errors'=>null]) as $theme ){
            try {
                
$themes[] = self::createFromTheme($theme);
            }
            catch( 
Exception $e ){
                
// @codeCoverageIgnore
            
}
        }
        return 
$themes;
    }


    
/**
     * Create theme bundle definition from WordPress theme handle 
     * 
     * @param string $slug Short name of theme, e.g. "twentyfifteen"
     * @param string $root Theme root if known
     * @return self
     */
    
public static function createstring $slugstring $root '' ):self {
        return 
self::createFromThemewp_get_theme$slug$root ) );
    }


    
/**
     * Create theme bundle definition from WordPress theme data 
     */
    
public static function createFromThemeWP_Theme $theme ):self {
        
$slug $theme->get_stylesheet();
        
$base $theme->get_stylesheet_directory();
        
$name $theme->get('Name') or $name $slug;
        if( ! 
$theme->exists() ){
            throw new 
Loco_error_Exception('Theme not found: '.$name );
        }

        
$bundle = new Loco_package_Theme$slug$name );
        
        
// ideally theme has declared its TextDomain
        // if not, we can see if the Domain listener has picked it up 
        
$domain $theme->get('TextDomain') ?: Loco_package_Listener::singleton()->getDomain($slug);
        
// otherwise we won't try to guess as it results in silent problems when guess is wrong
        
        // ideally theme has declared its DomainPath. if not, we can see if the listener has picked it up 
        // otherwise project will use theme root by default
        
$target $theme->get('DomainPath') ?: Loco_package_Listener::singleton()->getDomainPath($domain);

        
$bundle->configure$base,  [
            
'Name' => $name,
            
'TextDomain' => $domain,
            
'DomainPath' => $target,
        ] );
        
        
// parent theme inheritance:
        
if( $parent $theme->parent() ){
            try {
                
$bundle->parent self::createFromTheme($parent);
                
$bundle->inherit$bundle->parent );
            }
            catch( 
Loco_error_Exception $e ){
                
Loco_error_AdminNotices::add($e);
            }
        }
        return 
$bundle;
    }


    
/**
     * {@inheritDoc}
     */
    
public static function fromFileLoco_fs_File $file ):?Loco_package_Bundle {
        
$find $file->getPath();
        foreach( 
wp_get_themes( ['errors'=>null] ) as $theme ){
            
$base $theme->get_stylesheet_directory();
            
$path $base.substr$findstrlen($base) );
            if( 
$find === $path ){
                return 
self::createFromTheme($theme);
            }
        }
        return 
null;
    }

}