-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.php
136 lines (108 loc) · 3.43 KB
/
functions.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
<?php
declare( strict_types=1 );
namespace knutsp;
/**
* @see https://github.com/symfony/polyfill/blob/main/src/Php80/Php80.php
*/
function get_debug_type( $value ): string {
switch ( true ) {
case \is_null( $value ): return 'null';
case \is_bool( $value ): return 'bool';
case \is_string( $value ): return 'string';
case \is_array( $value ): return 'array';
case \is_int( $value ): return 'int';
case \is_float( $value ): return 'float';
case \is_object( $value ): break;
case $value instanceof \__PHP_Incomplete_Class: return '__PHP_Incomplete_Class';
default:
if ( null === $type = @\get_resource_type( $value ) ) {
return 'unknown';
}
if ( $type === 'Unknown' ) {
$type = 'closed';
}
return "resource ($type)";
}
$class = \get_class( $value );
if ( \strpos( $class, '@' ) === false ) {
return $class;
}
return ( \get_parent_class($class) ?: \key( \class_implements( $class ) ) ?: 'class' ) . '@anonymous';
}
function get_valid_type( ?string $type ): string {
if ( \array_key_exists( $type, VALID_TYPES ) ) {
return VALID_TYPES[ $type ];
}
return '';
}
function update_option( string $option, $value, $autoload = null, ?string $type = null ): bool {
$type_prefix = TYPE_PREFIX[ 'options' ];
if ( \is_null( $type ) ) {
$type = get_debug_type( $value );
$type = get_valid_type( $type );
}
if ( $type === 'null' ) {
$value = '';
}
$upd = \update_option( $option, $value, $autoload );
if ( $upd && ! \str_starts_with( $type, 'str' ) ) {
\update_option( $type_prefix . $option, $type, $autoload );
}
return (bool) $upd;
}
function get_option( string $option, $default_value = false ) {
$type_prefix = TYPE_PREFIX[ 'options' ];
$type = (string) \get_option( $type_prefix . $option );
$type = get_valid_type( $type );
$value = \get_option( $option, $default_value );
if ( $type ) {
\settype( $value, $type );
}
return $value;
}
function add_term_meta( int $term_id, string $meta_key, $meta_value, bool $unique = false, ?string $type = null ) {
$type_prefix = TYPE_PREFIX[ 'meta' ];
if ( \is_null( $type ) ) {
$type = get_term_meta( $term_id, $meta_key, true );
$no_pre = ! $type;
if ( $no_pre ) {
$type = get_debug_type( $meta_value );
}
} else {
$no_pre = true;
$type = get_valid_type( $type );
}
if ( $type === 'null' ) {
$meta_value = '';
}
$res = \add_term_meta( $term_id, $meta_key, $meta_value );
if ( $no_pre && ! \is_wp_error( $res ) && ! \str_starts_with( $type, 'str' ) ) {
\update_term_meta( $term_id, $type_prefix . $meta_key, $type );
}
return $res;
}
function update_term_meta( int $term_id, string $meta_key, $meta_value, ?string $prev_value = '', ?string $type = null ) {
$type_prefix = TYPE_PREFIX[ 'meta' ];
if ( \is_null( $type ) ) {
$type = get_debug_type( $meta_value );
$type = get_valid_type( $type );
}
if ( $type === 'null' ) {
$meta_value = '';
}
$res = \update_term_meta( $term_id, $meta_key, $meta_value, $prev_value );
if ( ! \is_wp_error( $res ) && ! \str_starts_with( $type, 'str' ) ) {
\update_term_meta( $term_id, $type_prefix . $meta_key, $type );
}
return $res;
}
function get_term_meta( int $term_id, string $key = '', bool $single = false ) {
$type_prefix = TYPE_PREFIX[ 'meta' ];
$type = (string) \get_term_meta( $term_id, $type_prefix . $key, true );
$type = get_valid_type( $type );
$value = \get_term_meta( $term_id, $key, $single );
if ( $type ) {
\settype( $value, $type );
}
return $value;
}