-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathclass-helpers.php
More file actions
263 lines (213 loc) · 6.38 KB
/
Copy pathclass-helpers.php
File metadata and controls
263 lines (213 loc) · 6.38 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
<?php
/**
* Class file for helpers
*
* @package Accessibility_Checker
*/
namespace EDAC\Admin;
/**
* Class that holds helpers
*/
class Helpers {
/**
* Gets a sql prepared/safe list of items from an array.
* Needed b/c wpdb->prepare breaks quotes for IN.
*
* @param array $items Array of items to be made into a sql safe comma delimted list.
* @return string
*/
public static function array_to_sql_safe_list( $items ) {
$items = array_map(
function ( $item ) {
global $wpdb;
return $wpdb->prepare( '%s', $item );
},
$items
);
return implode( ',', $items );
}
/**
* Localizes the format of a number.
*
* @param int|float $number number to format.
* @param integer $precision number of decimals.
* @return string|false
*/
public static function format_number( $number, $precision = 0 ) {
if ( ( ! is_numeric( $number ) ) ) {
return $number;
}
$locale = get_locale();
if ( class_exists( 'NumberFormatter' ) ) {
$formatter = new \NumberFormatter( $locale, \NumberFormatter::DECIMAL );
$formatter->setAttribute( \NumberFormatter::MAX_FRACTION_DIGITS, $precision ); // decimals to include.
$formatter->setAttribute( \NumberFormatter::GROUPING_USED, 1 ); // Include thousands separator.
return $formatter->format( $number );
}
return number_format( $number );
}
/**
* Localizes the format of a percentage.
*
* @param int|float $number number to format.
* @param integer $precision number of decimals.
* @return string|int|float
*/
public static function format_percentage( $number, $precision = 2 ) {
if ( ( ! is_numeric( $number ) ) ) {
return $number;
}
if ( $number > 1 ) {
$number = $number / 100;
}
$locale = get_locale();
if ( class_exists( 'NumberFormatter' ) ) {
$formatter = new \NumberFormatter( $locale, \NumberFormatter::PERCENT );
$formatter->setAttribute( \NumberFormatter::MAX_FRACTION_DIGITS, $precision ); // decimals to include.
return $formatter->format( $number );
}
return sprintf( '%.2f%%', $number * 100 );
}
/**
* Localizes the format of a date.
*
* @param int|string $date date to format, as a Unix timestamp or date string.
* @param boolean $include_time whether to include time in the formatted date.
* @return string
*/
public static function format_date( $date, $include_time = false ) {
$timestamp = $date;
if ( ! is_numeric( $date ) ) { // date as string.
$timestamp = strtotime( $date );
if ( ! $timestamp ) { // The passed string is not a valid date.
return $date;
}
}
$datetime = new \DateTime();
$datetime->setTimestamp( $timestamp );
$datetime->setTimezone( wp_timezone() );
$format = ( ! $include_time )
? get_option( 'date_format' )
: get_option( 'date_format' ) . ' \a\t ' . get_option( 'time_format' );
if ( ! $format ) {
$format = 'j M Y';
if ( $include_time ) {
$format = 'j M Y \a\t g:i a';
}
}
return $datetime->format( $format );
}
/**
* Given an WP option that may contain a string or an array, returns it as an array.
*
* @param string $option_name name of the option to return.
* @return array
*/
public static function get_option_as_array( $option_name ) {
$option = get_option( $option_name );
if ( is_array( $option ) && ! empty( $option ) ) {
return $option;
}
return [];
}
/**
* Determine if a domain is hosted on a local loopback
*
* @param string $domain The domain to check.
* @return boolean
*/
public static function is_domain_loopback( $domain ) {
// Check if this is an ipv4 address in the loopback range.
$record = gethostbyname( $domain );
$loopback_start = ip2long( '127.0.0.0' );
$loopback_end = ip2long( '127.255.255.255' );
$ip_long = ip2long( $record );
if ( $ip_long >= $loopback_start && $ip_long <= $loopback_end ) {
return true;
}
// Check if this is an ipv6 loopback.
try {
$records = dns_get_record( $domain, DNS_AAAA );
} catch ( \Throwable $th ) {
return false;
}
foreach ( $records as $record ) {
// Do ipv6 check.
if ( isset( $record['type'] ) && 'AAAA' === $record['type'] ) {
// Normalize the IPv6 address for comparison.
$normalized_ipv6 = inet_pton( $record['ipv6'] );
// Normalize the loopback address.
$loopback_ipv6 = inet_pton( '::1' );
if ( $normalized_ipv6 === $loopback_ipv6 ) {
return true;
}
}
}
return false;
}
/**
* Filter out inactive rules from the results returned.
*
* @param array $results The results to filter.
* @return array
*/
public static function filter_results_to_only_active_rules( $results ): array {
// determine which rules are active.
$active_rule_slugs = array_map(
function ( $rule ) {
return $rule['slug'];
},
edac_register_rules()
);
// filter out inactive rules from the results returned.
foreach ( $results as $index => $result ) {
if ( ! in_array( $result['rule'], $active_rule_slugs, true ) ) {
unset( $results[ $index ] );
}
}
return $results;
}
/**
* Do a capability check for the current user to ensure they have the required capability
* to see various widgets or notices.
*
* @since 1.9.3
*
* @return bool True if the current user has capabilities required, false otherwise.
*/
public static function current_user_can_see_widgets_and_notices(): bool {
/**
* Filter the capability required to view the dashboard widget.
*
* @since 1.9.3
*
* @param string $capability The capability required to view the dashboard widget.
*/
return current_user_can( apply_filters( 'edac_filter_dashboard_widget_capability', 'edit_posts' ) );
}
/**
* Determine if the current post type is scannable.
*
* @param array $post_types List of scannable post types (optional).
* @return bool
*/
public static function is_current_post_type_scannable( array $post_types = [] ): bool {
if ( empty( $post_types ) ) {
$post_types = Settings::get_scannable_post_types();
}
$current_post_type = get_post_type();
return is_array( $post_types ) && in_array( $current_post_type, $post_types, true );
}
/**
* Check whether the current screen is using the block editor.
*
* @return bool
*/
public static function is_block_editor(): bool {
if ( ! function_exists( 'get_current_screen' ) ) {
return false;
}
$screen = get_current_screen();
return $screen && method_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor();
}
}