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

equalizedigital / accessibility-checker / 23205382981

17 Mar 2026 04:38PM UTC coverage: 60.305%. Remained the same
23205382981

push

github

pattonwebz
Merge remote-tracking branch 'origin/develop' into develop

46 of 47 new or added lines in 2 files covered. (97.87%)

35 existing lines in 1 file now uncovered.

4901 of 8127 relevant lines covered (60.31%)

4.7 hits per line

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

97.83
/includes/classes/Fixes/Fix/EmptySearchFix.php
1
<?php
2
/**
3
 * Empty Search Fix Class
4
 *
5
 * @package accessibility-checker
6
 */
7

8
namespace EqualizeDigital\AccessibilityChecker\Fixes\Fix;
9

10
use EqualizeDigital\AccessibilityChecker\Fixes\FixInterface;
11

12
if ( ! defined( 'ABSPATH' ) ) {
13
        exit;
14
}
15

16
/**
17
 * Forces an error state when a front-end search is submitted with an empty query.
18
 *
19
 * When the `s` query parameter exists but is empty or whitespace-only, this fix
20
 * ensures WordPress still treats the request as a search so themes can display
21
 * a "no results" or "empty search" message via search.php.
22
 *
23
 * @since 1.39.0
24
 */
25
class EmptySearchFix implements FixInterface {
26

27
        /**
28
         * The slug of the fix.
29
         *
30
         * @return string
31
         */
32
        public static function get_slug(): string {
33
                return 'empty-search';
6✔
34
        }
35

36
        /**
37
         * The nicename for the fix.
38
         *
39
         * @return string
40
         */
41
        public static function get_nicename(): string {
42
                return __( 'Force Error on Empty Search', 'accessibility-checker' );
6✔
43
        }
44

45
        /**
46
         * The type of the fix.
47
         *
48
         * @return string
49
         */
50
        public static function get_type(): string {
51
                return 'frontend';
2✔
52
        }
53

54
        /**
55
         * Registers everything needed for the fix.
56
         *
57
         * @return void
58
         */
59
        public function register(): void {
60

61
                add_filter(
6✔
62
                        'edac_filter_fixes_settings_sections',
6✔
63
                        function ( $sections ) {
6✔
64
                                $sections['empty_search'] = [
12✔
65
                                        'title'    => esc_html__( 'Empty Search Handling', 'accessibility-checker' ),
12✔
66
                                        'callback' => [ $this, 'settings_section_callback' ],
12✔
67
                                ];
12✔
68

69
                                return $sections;
12✔
70
                        }
6✔
71
                );
6✔
72

73
                add_filter(
6✔
74
                        'edac_filter_fixes_settings_fields',
6✔
75
                        [ $this, 'get_fields_array' ],
6✔
76
                );
6✔
77
        }
78

79
        /**
80
         * Get the settings fields for the fix.
81
         *
82
         * @param array $fields The array of fields that are already registered, if any.
83
         *
84
         * @return array
85
         */
86
        public function get_fields_array( array $fields = [] ): array {
87
                $fields['edac_fix_empty_search'] = [
4✔
88
                        'label'       => esc_html__( 'Force Error on Empty Search', 'accessibility-checker' ),
4✔
89
                        'type'        => 'checkbox',
4✔
90
                        'labelledby'  => 'force_empty_search_error',
4✔
91
                        'description' => esc_html__( 'When a search is submitted with an empty query, force WordPress to display the search results template so users see a meaningful response. This assumes the active theme has a search.php template.', 'accessibility-checker' ),
4✔
92
                        'section'     => 'empty_search',
4✔
93
                        'fix_slug'    => $this->get_slug(),
4✔
94
                        'group_name'  => $this->get_nicename(),
4✔
95
                ];
4✔
96

97
                return $fields;
4✔
98
        }
99

100
        /**
101
         * Callback for the fix settings section.
102
         *
103
         * @return void
104
         */
105
        public function settings_section_callback() {
106
                echo '<p>' . esc_html__( 'Force WordPress to show a search results page when a search is submitted with an empty query.', 'accessibility-checker' ) . '</p>';
2✔
107
        }
108

109
        /**
110
         * Run the fix.
111
         *
112
         * @return void
113
         */
114
        public function run(): void {
115
                if ( get_option( 'edac_fix_empty_search', false ) ) {
8✔
116
                        add_action( 'pre_get_posts', [ $this, 'handle_empty_search' ] );
2✔
117
                }
118
        }
119

120
        /**
121
         * Handle an empty search submission on the front end.
122
         *
123
         * When the `s` query parameter is present but empty/whitespace, this sets
124
         * the search query var to a space so WordPress treats it as a search and
125
         * forces `is_search` to true.
126
         *
127
         * @param \WP_Query $query The main query object.
128
         *
129
         * @return void
130
         */
131
        public function handle_empty_search( $query ): void {
132
                if ( is_admin() || ! $query->is_main_query() ) {
14✔
133
                        return;
2✔
134
                }
135

136
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only check on a public search parameter.
137
                if ( ! isset( $_GET['s'] ) ) {
12✔
138
                        return;
2✔
139
                }
140

141
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Read-only comparison, not stored.
142
                if ( '' !== trim( wp_unslash( $_GET['s'] ) ) ) {
10✔
143
                        return;
2✔
144
                }
145

146
                $query->query_vars['s'] = '&#32;';
8✔
147

148
                add_filter( 'get_search_query', [ $this, 'clear_fake_search_query' ] );
8✔
149
                add_action( 'template_include', [ $this, 'force_search_template' ] );
8✔
150
        }
151

152
        /**
153
         * Clear the fake search query so it doesn't appear in the search input field.
154
         *
155
         * @param string $query The search query.
156
         *
157
         * @return string Empty string when this fix injected the placeholder value.
158
         */
159
        public function clear_fake_search_query( $query ): string {
160
                if ( '&#32;' === $query ) {
4✔
161
                        return '';
2✔
162
                }
163

164
                return $query;
2✔
165
        }
166

167
        /**
168
         * Force the search template for an empty search.
169
         *
170
         * Uses get_search_template() to respect the full WordPress template
171
         * hierarchy (search_template_hierarchy filter) and the search_template
172
         * filter, falling back to the original template if none is found.
173
         *
174
         * @param string $template The current template path.
175
         *
176
         * @return string The search template path, or the original template.
177
         */
178
        public function force_search_template( $template ): string {
179
                $search_template = get_search_template();
2✔
180
                if ( $search_template ) {
2✔
NEW
181
                        return $search_template;
×
182
                }
183

184
                return $template;
2✔
185
        }
186
}
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