/var/www/html_it/wp-content/plugins/loco-translate/src/mvc/FileParams.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
 * Abstracts information about a file into a view parameter object.
 * 
 * @property-read string $name
 * @property-read string $path
 * @property-read string $relpath
 * @property-read string $size
 * @property-read int $imode
 * @property-read string $smode
 * @property-read string $group
 * @property-read string $owner
 */
class Loco_mvc_FileParams extends Loco_mvc_ViewParams {
    
    
/**
     * File reference from which to take live properties.
     * @var Loco_fs_File
     */
    
private $file;
    
    
    
/**
     * Print property as a number of bytes in larger denominations
     * @param int $n
     * @return string 
     */
    
public static function renderBytes$n ){
        
$i 0;
        
$dp 0;
        while( 
$n >= 1024 ){
            
$i++;
            
$dp++;
            
$n /= 1024;
        }
        
$s number_format$n$dp'.'',' );
        
// trim trailing zeros from decimal places
        
$a explode('.',$s);
        if( isset(
$a[1]) ){
            
$s $a[0];
            
$d trim($a[1],'0') and $s .= '.'.$d;
        }
        
$units = [ ' bytes'' KB'' MB'' GB'' TB' ];
        
$s .= $units[$i];
        
        return 
$s;
    }


    
/**
     * @return Loco_mvc_FileParams 
     */
    
public static function createLoco_fs_File $file ) {
        return new 
Loco_mvc_FileParams( [], $file );
    }


    
/**
     * Override does lazy property initialization
     * @param array $props initial extra properties
     */
    
public function __construct( array $propsLoco_fs_File $file ){
        
parent::__construct(  [
            
'name' => '',
            
'path' => '',
            
'relpath' => '',
            
'reltime' => '',
            
'bytes' => 0,
            
'size' => '',
            
'imode' => '',
            
'smode' => '',
            
'owner' => '',
            
'group' => '',
        ] + 
$props );
        
$this->file $file;
    }


    
/**
     * {@inheritdoc}
     * Override to get live information from file object
     */
    
#[ReturnTypeWillChange]
    public function 
offsetGet$prop ){
        
$getter = [ $this'_get_'.$prop ];
        if( 
is_callable($getter) ){
            return 
call_user_func$getter );
        }
        return 
parent::offsetGet($prop);
    }


    
/**
     * {@inheritdoc}
     * Override to ensure all properties populated
     */
    
#[ReturnTypeWillChange]
    public function 
getArrayCopy(){
        
$a = [];
        foreach( 
$this as $prop => $dflt ){
            
$a[$prop] = $this[$prop];
        }
        return 
$a;
    }


    
/**
     * @internal
     * @return string
     */    
    
private function _get_name(){
        return  
$this->file->basename();
    }


    
/**
     * @internal
     * @return string
     */    
    
private function _get_path(){
        return  
$this->file->getPath();
    }


    
/**
     * @internal
     * @return string
     */
    
private function _get_relpath(){
        return 
$this->file->getRelativePathloco_constant('WP_CONTENT_DIR') );
    }


    
/**
     * Using slightly modified version of WordPress's Human time differencing
     * + Added "Just now" when in the last 30 seconds
     * @internal
     * @return string
     */
    
private function _get_reltime(){
        
$time $this->has('mtime') ? $this['mtime'] : $this->file->modified();
        
$time_diff time() - $time;
        
// use same time format as posts listing when in future or more than a day ago
        
if( $time_diff || $time_diff >= 86400 ){
            return 
Loco_mvc_ViewParams::date_i18n$time__('Y/m/d','default') );
        }
        if( 
$time_diff 30 ){
            
// translators: relative time when something happened in the last 30 seconds
            
return __('Just now','loco-translate');
        }
        
// translators: %s: Human-readable time difference.
        
return sprintf__('%s ago','default'), human_time_diff($time) );
    }


    
/**
     * @internal
     * @return int
     */
    
private function _get_bytes(){
        return 
$this->file->size();
    }


    
/**
     * @internal
     * @return string
     */
    
private function _get_size(){
        return 
self::renderBytes$this->_get_bytes() );
    }

 
    
/**
     * Get octal file mode
     * @internal
     * @return string
     */
    
private function _get_imode(){
        
$mode = new Loco_fs_FileMode$this->file->mode() );
        return (string) 
$mode;
    }

 
    
/**
     * Get rwx file mode
     * @internal
     * @return string
     */
    
private function _get_smode(){
        
$mode = new Loco_fs_FileMode$this->file->mode() );
        return 
$mode->format();
    }


    
/**
     * Get file owner name
     * @internal
     * @return string
     */
    
private function _get_owner(){
        if( ( 
$uid $this->file->uid() ) && function_exists('posix_getpwuid') && ( $a posix_getpwuid($uid) ) ){
            return 
$a['name'];
        }
        return 
sprintf('%u',$uid);
    }


    
/**
     * Get group owner name
     * @internal
     * @return string
     */
    
private function _get_group(){
        if( ( 
$gid $this->file->gid() ) && function_exists('posix_getpwuid') && ( $a posix_getgrgid($gid) ) ){
            return 
$a['name'];
        }
        return 
sprintf('%u',$gid);
    }
            

    
/**
     * Print pseudo console line
     * @return string;
     */
    
public function ls(){
        
$this->e('smode');
        echo 
'  ';
        
$this->e('owner');
        echo 
':';
        
$this->e('group');
        echo 
'  ';
        
$this->e('relpath');
        return 
'';
    }
    
}