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

equalizedigital / accessibility-checker / 14783707399

01 May 2025 09:17PM UTC coverage: 29.045% (+3.4%) from 25.605%
14783707399

push

github

web-flow
Merge pull request #930 from equalizedigital/steve/pro-60-remove-legacy-php-scan-code

Remove Legacy PHP Scan Code

1 of 81 new or added lines in 6 files covered. (1.23%)

37 existing lines in 4 files now uncovered.

1542 of 5309 relevant lines covered (29.05%)

1.53 hits per line

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

48.65
/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(
8✔
25
                        function ( $item ) {
8✔
26
                                global $wpdb;
8✔
27

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

33
                return implode( ',', $items );
8✔
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 ) ) ) {
8✔
47
                        return $number;
×
48
                }
49

50
                $locale = get_locale();
8✔
51

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

57
                        return $formatter->format( $number );
8✔
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 ) ) ) {
8✔
72
                        return $number;
×
73
                }
74

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

79
                $locale = get_locale();
8✔
80

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

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

86
                        return $formatter->format( $number );
8✔
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;
8✔
101
                if ( ! is_numeric( $date ) ) { // date as string.
8✔
102
                        $timestamp = strtotime( $date );
×
103
                        if ( ! $timestamp ) { // The passed string is not a valid date.
×
104
                                return $date;
×
105
                        }
106
                }
107

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

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

116
                if ( ! $format ) {
8✔
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 );
8✔
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 );
8✔
135

136
                if ( is_array( $option ) && ! empty( $option ) ) {
8✔
137
                        return $option;
×
138
                }
139
                return [];
8✔
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

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

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

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

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

UNCOV
170
                foreach ( $records as $record ) {
×
171

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

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

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

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

UNCOV
187
                return false;
×
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
}
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