-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathwp-function-stubs.php
More file actions
407 lines (390 loc) · 13.4 KB
/
Copy pathwp-function-stubs.php
File metadata and controls
407 lines (390 loc) · 13.4 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?php
/**
* WordPress Function Stubs (CLI + Tests)
*
* Minimal implementations of WordPress globals used by Translation Bridge
* outside a WP runtime (CLI, PHPUnit). All definitions are guarded with
* function_exists / class_exists so this file is safe to load alongside a
* real WordPress environment.
*
* Used by:
* - /devtb-php (CLI entrypoint)
* - /tests/bootstrap.php (PHPUnit)
*
* @package DevelopmentTranslation_Bridge
* @subpackage Compat
* @version 5.1.0
*/
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
if ( ! defined( 'MINUTE_IN_SECONDS' ) ) { define( 'MINUTE_IN_SECONDS', 60 ); }
if ( ! defined( 'HOUR_IN_SECONDS' ) ) { define( 'HOUR_IN_SECONDS', 60 * MINUTE_IN_SECONDS ); }
if ( ! defined( 'DAY_IN_SECONDS' ) ) { define( 'DAY_IN_SECONDS', 24 * HOUR_IN_SECONDS ); }
if ( ! defined( 'WEEK_IN_SECONDS' ) ) { define( 'WEEK_IN_SECONDS', 7 * DAY_IN_SECONDS ); }
if ( ! defined( 'MONTH_IN_SECONDS' ) ) { define( 'MONTH_IN_SECONDS', 30 * DAY_IN_SECONDS ); }
if ( ! defined( 'YEAR_IN_SECONDS' ) ) { define( 'YEAR_IN_SECONDS', 365 * DAY_IN_SECONDS ); }
// ---------------------------------------------------------------------------
// Hooks
// ---------------------------------------------------------------------------
if ( ! function_exists( 'add_action' ) ) {
function add_action( $hook, $callback, $priority = 10, $accepted_args = 1 ) { return true; }
}
if ( ! function_exists( 'add_filter' ) ) {
function add_filter( $hook, $callback, $priority = 10, $accepted_args = 1 ) { return true; }
}
if ( ! function_exists( 'do_action' ) ) {
function do_action( $hook, ...$args ) { return true; }
}
if ( ! function_exists( 'apply_filters' ) ) {
function apply_filters( $hook, $value, ...$args ) { return $value; }
}
// ---------------------------------------------------------------------------
// Options (in-memory)
// ---------------------------------------------------------------------------
if ( ! isset( $GLOBALS['__devtb_stub_options'] ) ) {
$GLOBALS['__devtb_stub_options'] = array();
}
if ( ! function_exists( 'get_option' ) ) {
function get_option( $option, $default = false ) {
return $GLOBALS['__devtb_stub_options'][ $option ] ?? $default;
}
}
if ( ! function_exists( 'update_option' ) ) {
function update_option( $option, $value ) {
$GLOBALS['__devtb_stub_options'][ $option ] = $value;
return true;
}
}
if ( ! function_exists( 'delete_option' ) ) {
function delete_option( $option ) {
unset( $GLOBALS['__devtb_stub_options'][ $option ] );
return true;
}
}
// ---------------------------------------------------------------------------
// Transients (in-memory; TTL ignored for test purposes)
// ---------------------------------------------------------------------------
if ( ! isset( $GLOBALS['__devtb_stub_transients'] ) ) {
$GLOBALS['__devtb_stub_transients'] = array();
}
if ( ! function_exists( 'get_transient' ) ) {
function get_transient( $key ) {
return $GLOBALS['__devtb_stub_transients'][ $key ] ?? false;
}
}
if ( ! function_exists( 'set_transient' ) ) {
function set_transient( $key, $value, $expiration = 0 ) {
$GLOBALS['__devtb_stub_transients'][ $key ] = $value;
return true;
}
}
if ( ! function_exists( 'delete_transient' ) ) {
function delete_transient( $key ) {
unset( $GLOBALS['__devtb_stub_transients'][ $key ] );
return true;
}
}
// ---------------------------------------------------------------------------
// Cron (no-op in CLI / tests; jobs scheduled here are not actually executed)
// ---------------------------------------------------------------------------
if ( ! function_exists( 'wp_schedule_single_event' ) ) {
function wp_schedule_single_event( $timestamp, $hook, $args = array(), $wp_error = false ) { return true; }
}
if ( ! function_exists( 'wp_schedule_event' ) ) {
function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array(), $wp_error = false ) { return true; }
}
if ( ! function_exists( 'wp_next_scheduled' ) ) {
function wp_next_scheduled( $hook, $args = array() ) { return false; }
}
if ( ! function_exists( 'wp_clear_scheduled_hook' ) ) {
function wp_clear_scheduled_hook( $hook, $args = array(), $wp_error = false ) { return 0; }
}
// ---------------------------------------------------------------------------
// Time
// ---------------------------------------------------------------------------
if ( ! function_exists( 'current_time' ) ) {
function current_time( $type = 'mysql', $gmt = 0 ) {
if ( 'timestamp' === $type || 'U' === $type ) {
return time();
}
return $gmt ? gmdate( 'Y-m-d H:i:s' ) : date( 'Y-m-d H:i:s' );
}
}
// ---------------------------------------------------------------------------
// I18n
// ---------------------------------------------------------------------------
if ( ! function_exists( '__' ) ) {
function __( $text, $domain = 'default' ) { return $text; }
}
if ( ! function_exists( '_e' ) ) {
function _e( $text, $domain = 'default' ) { echo $text; }
}
if ( ! function_exists( '_n' ) ) {
function _n( $single, $plural, $number, $domain = 'default' ) {
return 1 === (int) $number ? $single : $plural;
}
}
if ( ! function_exists( '_x' ) ) {
function _x( $text, $context, $domain = 'default' ) { return $text; }
}
if ( ! function_exists( 'esc_html__' ) ) {
function esc_html__( $text, $domain = 'default' ) {
return htmlspecialchars( $text, ENT_QUOTES, 'UTF-8' );
}
}
if ( ! function_exists( 'esc_attr__' ) ) {
function esc_attr__( $text, $domain = 'default' ) {
return htmlspecialchars( $text, ENT_QUOTES, 'UTF-8' );
}
}
// ---------------------------------------------------------------------------
// Escaping / sanitization
// ---------------------------------------------------------------------------
if ( ! function_exists( 'esc_html' ) ) {
function esc_html( $text ) {
return htmlspecialchars( (string) $text, ENT_QUOTES, 'UTF-8' );
}
}
if ( ! function_exists( 'esc_attr' ) ) {
function esc_attr( $text ) {
return htmlspecialchars( (string) $text, ENT_QUOTES, 'UTF-8' );
}
}
if ( ! function_exists( 'esc_url' ) ) {
function esc_url( $url ) {
return filter_var( (string) $url, FILTER_SANITIZE_URL );
}
}
if ( ! function_exists( 'esc_url_raw' ) ) {
function esc_url_raw( $url ) {
return filter_var( (string) $url, FILTER_SANITIZE_URL );
}
}
if ( ! function_exists( 'esc_textarea' ) ) {
function esc_textarea( $text ) {
return htmlspecialchars( (string) $text, ENT_QUOTES, 'UTF-8' );
}
}
if ( ! function_exists( 'wp_kses_post' ) ) {
function wp_kses_post( $data ) { return $data; }
}
if ( ! function_exists( 'sanitize_text_field' ) ) {
function sanitize_text_field( $str ) { return strip_tags( (string) $str ); }
}
if ( ! function_exists( 'sanitize_key' ) ) {
function sanitize_key( $key ) {
return strtolower( preg_replace( '/[^a-z0-9_\-]/', '', (string) $key ) );
}
}
if ( ! function_exists( 'sanitize_title' ) ) {
function sanitize_title( $title, $fallback_title = '', $context = 'save' ) {
return strtolower( preg_replace( '/[^a-z0-9\-]+/i', '-', (string) $title ) );
}
}
if ( ! function_exists( 'absint' ) ) {
function absint( $value ) { return abs( (int) $value ); }
}
if ( ! function_exists( 'wp_unslash' ) ) {
function wp_unslash( $value ) {
return is_string( $value ) ? stripslashes( $value ) : $value;
}
}
if ( ! function_exists( 'wp_strip_all_tags' ) ) {
function wp_strip_all_tags( $text, $remove_breaks = false ) {
if ( ! is_scalar( $text ) ) { return ''; }
$text = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', (string) $text );
$text = strip_tags( $text );
if ( $remove_breaks ) {
$text = preg_replace( '/[\r\n\t ]+/', ' ', $text );
}
return trim( $text );
}
}
// ---------------------------------------------------------------------------
// JSON / serialization
// ---------------------------------------------------------------------------
if ( ! function_exists( 'wp_json_encode' ) ) {
function wp_json_encode( $data, $options = 0, $depth = 512 ) {
return json_encode( $data, $options, $depth );
}
}
if ( ! function_exists( 'is_serialized' ) ) {
function is_serialized( $data, $strict = true ) {
if ( ! is_string( $data ) ) { return false; }
$data = trim( $data );
if ( 'N;' === $data ) { return true; }
if ( strlen( $data ) < 4 ) { return false; }
if ( ':' !== $data[1] ) { return false; }
if ( $strict ) {
$lastc = substr( $data, -1 );
if ( ';' !== $lastc && '}' !== $lastc ) { return false; }
} else {
$semicolon = strpos( $data, ';' );
$brace = strpos( $data, '}' );
if ( false === $semicolon && false === $brace ) { return false; }
if ( false !== $semicolon && $semicolon < 3 ) { return false; }
if ( false !== $brace && $brace < 4 ) { return false; }
}
$token = $data[0];
switch ( $token ) {
case 's':
if ( $strict ) {
if ( '"' !== substr( $data, -2, 1 ) ) { return false; }
} elseif ( ! str_contains( $data, '"' ) ) {
return false;
}
// fall through
case 'a':
case 'O':
case 'E':
return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data );
case 'b':
case 'i':
case 'd':
$end = $strict ? '$' : '';
return (bool) preg_match( "/^{$token}:[0-9.E+-]+;{$end}/", $data );
}
return false;
}
}
if ( ! function_exists( 'maybe_unserialize' ) ) {
function maybe_unserialize( $data ) {
if ( is_serialized( $data ) ) {
return @unserialize( trim( $data ) );
}
return $data;
}
}
// ---------------------------------------------------------------------------
// Shortcodes
// ---------------------------------------------------------------------------
if ( ! function_exists( 'do_shortcode' ) ) {
function do_shortcode( $content ) { return $content; }
}
if ( ! function_exists( 'shortcode_atts' ) ) {
function shortcode_atts( $pairs, $atts, $shortcode = '' ) {
$atts = (array) $atts;
$out = array();
foreach ( $pairs as $name => $default ) {
$out[ $name ] = array_key_exists( $name, $atts ) ? $atts[ $name ] : $default;
}
return $out;
}
}
if ( ! function_exists( 'get_shortcode_regex' ) ) {
function get_shortcode_regex( $tagnames = null ) {
$tagregexp = '[a-zA-Z0-9_-]+';
if ( null !== $tagnames && is_array( $tagnames ) ) {
$tagregexp = implode( '|', array_map( 'preg_quote', $tagnames ) );
}
return '\\['
. '(\\[?)'
. "($tagregexp)"
. '(?![\\w-])'
. '('
. '[^\\]\\/]*'
. '(?:'
. '\\/(?!\\])'
. '[^\\]\\/]*'
. ')*?'
. ')'
. '(?:'
. '(\\/)'
. '\\]'
. '|'
. '\\]'
. '(?:'
. '('
. '[^\\[]*+'
. '(?:'
. '\\[(?!\\/\\2\\])'
. '[^\\[]*+'
. ')*+'
. ')'
. '\\[\\/\\2\\]'
. ')?'
. ')'
. '(\\]?)';
}
}
if ( ! function_exists( 'shortcode_parse_atts' ) ) {
function shortcode_parse_atts( $text ) {
$atts = array();
$pattern = '/(\w+)\s*=\s*"([^"]*)"|(\w+)\s*=\s*\'([^\']*)\'|(\w+)\s*=\s*([^\s\]]+)/';
if ( preg_match_all( $pattern, (string) $text, $matches, PREG_SET_ORDER ) ) {
foreach ( $matches as $match ) {
if ( ! empty( $match[1] ) ) {
$atts[ $match[1] ] = $match[2];
} elseif ( ! empty( $match[3] ) ) {
$atts[ $match[3] ] = $match[4];
} elseif ( ! empty( $match[5] ) ) {
$atts[ $match[5] ] = $match[6];
}
}
}
return $atts;
}
}
if ( ! function_exists( 'strip_shortcodes' ) ) {
function strip_shortcodes( $content ) {
if ( empty( $content ) ) { return $content; }
return preg_replace( '/\[[^\]]+\]/', '', $content );
}
}
// ---------------------------------------------------------------------------
// REST + WP_Error classes
// ---------------------------------------------------------------------------
if ( ! function_exists( 'is_wp_error' ) ) {
function is_wp_error( $thing ) {
return ( $thing instanceof \WP_Error );
}
}
if ( ! class_exists( 'WP_Error' ) ) {
class WP_Error {
private $errors = array();
private $error_data = array();
public function __construct( $code = '', $message = '', $data = '' ) {
if ( empty( $code ) ) { return; }
$this->errors[ $code ][] = $message;
if ( ! empty( $data ) ) {
$this->error_data[ $code ] = $data;
}
}
public function get_error_code() { return key( $this->errors ); }
public function get_error_message( $code = '' ) {
if ( empty( $code ) ) { $code = $this->get_error_code(); }
return $this->errors[ $code ][0] ?? '';
}
public function get_error_data( $code = '' ) {
if ( empty( $code ) ) { $code = $this->get_error_code(); }
return $this->error_data[ $code ] ?? null;
}
}
}
if ( ! class_exists( 'WP_REST_Request' ) ) {
class WP_REST_Request {
private $params = array();
private $headers = array();
public function __construct( $method = 'GET', $route = '', $params = array() ) {
$this->params = $params;
}
public function get_param( $key ) { return $this->params[ $key ] ?? null; }
public function get_params() { return $this->params; }
public function set_param( $key, $value ) { $this->params[ $key ] = $value; }
public function get_header( $key ) { return $this->headers[ $key ] ?? null; }
public function set_header( $key, $value ) { $this->headers[ $key ] = $value; }
}
}
if ( ! class_exists( 'WP_REST_Response' ) ) {
class WP_REST_Response {
private $data;
private $status;
public function __construct( $data = null, $status = 200 ) {
$this->data = $data;
$this->status = $status;
}
public function get_data() { return $this->data; }
public function get_status() { return $this->status; }
}
}