• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

equalizedigital / accessibility-checker / 22634711490

03 Mar 2026 05:22PM UTC coverage: 59.97% (-0.7%) from 60.629%
22634711490

push

github

web-flow
Merge pull request #1332 from equalizedigital/william/pro-526-setup-integration-branch-for-ac-sidebar

Integration branch: Sidebar Metabox

385 of 814 new or added lines in 10 files covered. (47.3%)

10 existing lines in 3 files now uncovered.

4773 of 7959 relevant lines covered (59.97%)

4.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.05
/admin/class-helpers.php
1
<?php
2
/**
3
 * Class file for helpers
4
 *
5
 * @package Accessibility_Checker
6
 */
7

8
namespace EDAC\Admin;
9

10
/**
11
 * Class that holds helpers
12
 */
13
class Helpers {
14

15
        /**
16
         * Gets a sql prepared/safe list of items from an array.
17
         * Needed b/c wpdb->prepare breaks quotes for IN.
18
         *
19
         * @param array $items Array of items to be made into a sql safe comma delimted list.
20
         * @return string
21
         */
22
        public static function array_to_sql_safe_list( $items ) {
23

24
                $items = array_map(
28✔
25
                        function ( $item ) {
28✔
26
                                global $wpdb;
26✔
27

28
                                return $wpdb->prepare( '%s', $item );
26✔
29
                        },
28✔
30
                        $items
28✔
31
                );
28✔
32

33
                return implode( ',', $items );
28✔
34
        }
35

36

37
        /**
38
         * Localizes the format of a number.
39
         *
40
         * @param int     $number number to format.
41
         * @param integer $precision number of decimals.
42
         * @return integer
43
         */
44
        public static function format_number( $number, $precision = 0 ) {
45

46
                if ( ( ! is_numeric( $number ) ) ) {
24✔
47
                        return $number;
4✔
48
                }
49

50
                $locale = get_locale();
20✔
51

52
                if ( class_exists( 'NumberFormatter' ) ) {
20✔
53
                        $formatter = new \NumberFormatter( $locale, \NumberFormatter::DECIMAL );
20✔
54
                        $formatter->setAttribute( \NumberFormatter::MAX_FRACTION_DIGITS, $precision ); // decimals to include.
20✔
55
                        $formatter->setAttribute( \NumberFormatter::GROUPING_USED, 1 ); // Include thousands separator.
20✔
56

57
                        return $formatter->format( $number );
20✔
58
                }
59
                return number_format( $number );
×
60
        }
61

62
        /**
63
         * Localizes the format of a percentage.
64
         *
65
         * @param init    $number number to format.
66
         * @param integer $precision number of decimals.
67
         * @return integer
68
         */
69
        public static function format_percentage( $number, $precision = 2 ) {
70

71
                if ( ( ! is_numeric( $number ) ) ) {
24✔
72
                        return $number;
14✔
73
                }
74

75
                if ( $number > 1 ) {
10✔
76
                        $number = $number / 100;
4✔
77
                }
78

79
                $locale = get_locale();
10✔
80

81
                if ( class_exists( 'NumberFormatter' ) ) {
10✔
82

83
                        $formatter = new \NumberFormatter( $locale, \NumberFormatter::PERCENT );
10✔
84
                        $formatter->setAttribute( \NumberFormatter::MAX_FRACTION_DIGITS, $precision ); // decimals to include.
10✔
85

86
                        return $formatter->format( $number );
10✔
87
                }
88
                return sprintf( '%.2f%%', $number * 100 );
×
89
        }
90

91
        /**
92
         * Localizes the format of a date.
93
         *
94
         * @param string  $date date to format.
95
         * @param boolean $include_time whether to include time in the formatted date.
96
         * @return integer
97
         */
98
        public static function format_date( $date, $include_time = false ) {
99

100
                $timestamp = $date;
14✔
101
                if ( ! is_numeric( $date ) ) { // date as string.
14✔
102
                        $timestamp = strtotime( $date );
4✔
103
                        if ( ! $timestamp ) { // The passed string is not a valid date.
4✔
104
                                return $date;
×
105
                        }
106
                }
107

108
                $datetime = new \DateTime();
14✔
109
                $datetime->setTimestamp( $timestamp );
14✔
110
                $datetime->setTimezone( wp_timezone() );
14✔
111

112
                $format = ( ! $include_time )
14✔
113
                        ? get_option( 'date_format' )
4✔
114
                        : get_option( 'date_format' ) . ' \a\t ' . get_option( 'time_format' );
14✔
115

116
                if ( ! $format ) {
14✔
117
                        $format = 'j M Y';
×
118
                        if ( $include_time ) {
×
119
                                $format = 'j M Y \a\t g:i a';
×
120
                        }
121
                }
122

123
                return $datetime->format( $format );
14✔
124
        }
125

126
        /**
127
         * Given an WP option that may contain a string or an array, returns it as an array.
128
         *
129
         * @param string $option_name name of the option to return.
130
         * @return array
131
         */
132
        public static function get_option_as_array( $option_name ) {
133

134
                $option = get_option( $option_name );
74✔
135

136
                if ( is_array( $option ) && ! empty( $option ) ) {
74✔
137
                        return $option;
62✔
138
                }
139
                return [];
12✔
140
        }
141

142

143
        /**
144
         * Determine if a domain is hosted on a local loopback
145
         *
146
         * @param string $domain The domain to check.
147
         * @return boolean
148
         */
149
        public static function is_domain_loopback( $domain ) {
150

151
                // Check if this is an ipv4 address in the loopback range.
152

153
                $record         = gethostbyname( $domain );
38✔
154
                $loopback_start = ip2long( '127.0.0.0' );
38✔
155
                $loopback_end   = ip2long( '127.255.255.255' );
38✔
156
                $ip_long        = ip2long( $record );
38✔
157

158
                if ( $ip_long >= $loopback_start && $ip_long <= $loopback_end ) {
38✔
159
                        return true;
18✔
160
                }
161

162
                // Check if this is an ipv6 loopback.
163

164
                try {
165
                        $records = dns_get_record( $domain, DNS_AAAA );
28✔
166
                } catch ( \Throwable $th ) {
6✔
167
                        return false;
6✔
168
                }
169

170
                foreach ( $records as $record ) {
24✔
171

172
                        // Do ipv6 check.
173
                        if ( isset( $record['type'] ) && 'AAAA' === $record['type'] ) {
6✔
174

175
                                // Normalize the IPv6 address for comparison.
176
                                $normalized_ipv6 = inet_pton( $record['ipv6'] );
6✔
177

178
                                // Normalize the loopback address.
179
                                $loopback_ipv6 = inet_pton( '::1' );
6✔
180

181
                                if ( $normalized_ipv6 === $loopback_ipv6 ) {
6✔
182
                                        return true;
×
183
                                }
184
                        }
185
                }
186

187
                return false;
24✔
188
        }
189

190
        /**
191
         * Filter out inactive rules from the results returned.
192
         *
193
         * @param array $results The results to filter.
194
         */
195
        public static function filter_results_to_only_active_rules( $results ): array {
196
                // determine which rules are active.
197
                $active_rule_slugs = array_map(
×
198
                        function ( $rule ) {
×
199
                                return $rule['slug'];
×
200
                        },
×
201
                        edac_register_rules()
×
202
                );
×
203

204
                // filter out inactive rules from the results returned.
205
                foreach ( $results as $index => $result ) {
×
206
                        if ( ! in_array( $result['rule'], $active_rule_slugs, true ) ) {
×
207
                                unset( $results[ $index ] );
×
208
                        }
209
                }
210
                return $results;
×
211
        }
212

213
        /**
214
         * Do a capability check for the current user to ensure they have the required capability
215
         * to see various widgets or notices.
216
         *
217
         * @since 1.9.3
218
         *
219
         * @return bool True if the current user has capabilities required, false otherwise.
220
         */
221
        public static function current_user_can_see_widgets_and_notices(): bool {
222
                /**
223
                 * Filter the capability required to view the dashboard widget.
224
                 *
225
                 * @since 1.9.3
226
                 *
227
                 * @param string $capability The capability required to view the dashboard widget.
228
                 */
229
                return current_user_can( apply_filters( 'edac_filter_dashboard_widget_capability', 'edit_posts' ) );
20✔
230
        }
231

232
        /**
233
         * Determine if the current post type is scannable.
234
         *
235
         * @param array $post_types List of scannable post types (optional).
236
         * @return bool
237
         */
238
        public static function is_current_post_type_scannable( array $post_types = [] ): bool {
239
                if ( empty( $post_types ) ) {
26✔
240
                        $post_types = Settings::get_scannable_post_types();
4✔
241
                }
242

243
                $current_post_type = get_post_type();
26✔
244

245
                return is_array( $post_types ) && in_array( $current_post_type, $post_types, true );
26✔
246
        }
247

248
        /**
249
         * Check whether the current screen is using the block editor.
250
         *
251
         * @return bool
252
         */
253
        public static function is_block_editor(): bool {
254
                if ( ! function_exists( 'get_current_screen' ) ) {
8✔
NEW
255
                        return false;
×
256
                }
257

258
                $screen = get_current_screen();
8✔
259

260
                return $screen && method_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor();
8✔
261
        }
262
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc