/var/www/html_it/wp-content/plugins/loco-translate/src/mvc/ViewParams.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
<?php
/**
 * 
 */
class Loco_mvc_ViewParams extends ArrayObject implements JsonSerializable {
    
    
/**
     * Default escape function for view type is HTML
     * @param string $text
     * @return string
     */
    
public function escape$text ){
        return 
htmlspecialchars( (string) $textENT_COMPAT'UTF-8' );
    }


    
/**
     * format integer as string date, including time according to user settings
     * @param int $u unix timestamp
     * @param string|null $f date format
     */
     
public static function date_i18nint $u, ?string $f null ):string {
        static 
$tf$df$tz;
        if( 
is_null($f) ){
            if( 
is_null($tf) ){
                
$tf get_option('time_format') or $tf 'g:i A';
                
$df get_option('date_format') or $df'M jS Y'
            }
            
$f $df.' '.$tf;
        }
        
// date_i18n was replaced with wp_date in WP 5.3
        
if( function_exists('wp_date') ){
             return 
wp_date($f,$u);
        }
        
// date_i18n expects timestamp to include offset
        
if( is_null($tz) ){
            try {
                
$wp get_option('timezone_string') or $wp date_default_timezone_get();
                
$tz = new DateTimeZone($wp);
            }
            catch( 
Exception $e ){
                
$tz = new DateTimeZone('UTC');
            }
        }
        
$d = new DateTime(null,$tz);
        
$d->setTimestamp($u);
        return 
date_i18n$f$u $d->getOffset() );
    }


    
/**
     * Wrapper for sprintf so we can handle PHP 8 exceptions
     */
    
public static function formatstring $format, array $args ):string {
        try {
            return 
vsprintf($format,$args);
        }
        
// Note that PHP8 will throw Error (not Exception), PHP 7 will trigger E_WARNING
        
catch( Error $e ){
            
Loco_error_AdminNotices::warn$e->getMessage().' in vsprintf('.var_export($format,true).')' );
            return 
'';
        }
    }


    
/**
     * @internal
     * @param string $p property name
     * @return mixed
     */
    
public function __get$p ){
        return 
$this->offsetExists($p) ? $this->offsetGet($p) : null;
    }


    
/**
     * Test if a property exists, even if null
     * @param string $p property name
     * @return bool
     */
    
public function has$p ){
        return 
$this->offsetExists($p);
    }


    
/**
     * Print escaped property value
     * @param string $p property key
     * @return string empty string
     */
    
public function e$p ){
        
$text $this->__get($p);
        echo 
$this->escape$text );
        return 
'';
    }


    
/**
     * Print property as string date, including time
     * @param string $p property name
     * @param string|null $f date format
     * @return string empty string
     */ 
    
public function datestring $p, ?string $f null ):string {
        
$u = (int) $this->__get($p);
        if( 
$u ){
            echo 
$this->escapeself::date_i18n($u,$f) );
        }
        return 
'';
    }


    
/**
     * Print property as a string-formatted number
     * @param string $p property name
     * @param int $dp optional decimal places
     * @return string empty string
     */
    
public function nstring $pint $dp ):string {
        
// number_format_i18n is pre-escaped for HTML
        
echo number_format_i18n$this->__get($p), $dp );
        return 
'';
    }


    
/**
     * Print property with passed formatting string
     * e.g. $params->f('name', 'My name is %s' );
     * @param string $p property name
     * @param string $f formatting string
     * @return string empty string
     */
    
public function fstring $pstring $f '%s' ):string {
        echo 
$this->escapeself::format$f, [$this->__get($p)] ) );
        return 
'';
    }


    
/**
     * Print property value for JavaScript
     * @param string $p property name
     * @return string empty string
     */
    
public function jstring $p ):string {
        echo 
json_encode($this->__get($p) );
        return 
'';
    }


    
/**
     * @return array
     */
    
#[ReturnTypeWillChange]
    public function 
jsonSerialize(){
        return 
$this->getArrayCopy();
    }
    
    
    
/**
     * Fetch whole object as JSON
     */
    
public function exportJson():string {
        return 
json_encode$this->jsonSerialize() );
    }
    
    
    
/**
     * Merge parameters into ours
     */
    
public function concatArrayObject $more ):self {
        foreach( 
$more as $name => $value ){
            
$this[$name] = $value;
        }
        return 
$this;
    }


    
/**
     * Debugging function
     * @codeCoverageIgnore
     */
    
public function dump():void {
        echo 
'<pre>',$this->escapejson_encode$this->getArrayCopy(),JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE ) ),'</pre>';
    }


    
/**
     * @param callable $callback
     * @return Loco_mvc_ViewParams
     */
    
public function sort$callback ):self {
        
$raw $this->getArrayCopy();
        
uasort$raw$callback );
        
$this->exchangeArray$raw );
        return 
$this;
    }

}