1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Singleton class instantiated by Jetpack_Searc_Debug_Bar::instance() that handles |
5
|
|
|
* rendering the Jetpack Search debug bar menu item and panel. |
6
|
|
|
*/ |
7
|
|
|
class Jetpack_Search_Debug_Bar extends Debug_Bar_Panel { |
8
|
|
|
/** |
9
|
|
|
* Holds singleton instance |
10
|
|
|
* |
11
|
|
|
* @var Jetpack_Search_Debug_Bar |
12
|
|
|
*/ |
13
|
|
|
protected static $instance = null; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Holds an instance of Jetpack_Search |
17
|
|
|
* |
18
|
|
|
* @var Jetpack_Search |
19
|
|
|
*/ |
20
|
|
|
private $jetpack_search; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The title to use in the debug bar navigation |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
public $title; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor |
31
|
|
|
*/ |
32
|
|
|
public function __construct() { |
33
|
|
|
$this->title( esc_html__( 'Jetpack Search', 'jetpack' ) ); |
34
|
|
|
$this->jetpack_search = Jetpack_Search::instance(); |
35
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns the singleton instance of Jetpack_Search_Debug_Bar |
40
|
|
|
* |
41
|
|
|
* @return Jetpack_Search_Debug_Bar |
42
|
|
|
*/ |
43
|
|
|
public static function instance() { |
44
|
|
|
if ( is_null( self::$instance ) ) { |
45
|
|
|
self::$instance = new Jetpack_Search_Debug_Bar(); |
46
|
|
|
} |
47
|
|
|
return self::$instance; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Enqueues styles for our panel in the debug bar |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
public function enqueue_scripts() { |
56
|
|
|
wp_enqueue_style( |
57
|
|
|
'jetpack-search-debug-bar', |
58
|
|
|
plugins_url( '3rd-party/debug-bar/debug-bar.css', JETPACK__PLUGIN_FILE ) |
59
|
|
|
); |
60
|
|
|
wp_enqueue_script( |
61
|
|
|
'jetpack-search-debug-bar', |
62
|
|
|
plugins_url( '3rd-party/debug-bar/debug-bar.js', JETPACK__PLUGIN_FILE ), |
63
|
|
|
array( 'jquery' ) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Should the Jetpack Search Debug Bar show? |
69
|
|
|
* |
70
|
|
|
* Since we've previously done a check for the search module being activated, let's just return true. |
71
|
|
|
* Later on, we can update this to only show when `is_search()` is true. |
72
|
|
|
* |
73
|
|
|
* @return boolean |
74
|
|
|
*/ |
75
|
|
|
public function is_visible() { |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Renders the panel content |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function render() { |
85
|
|
|
$last_query_info = $this->jetpack_search->get_last_query_info(); |
86
|
|
|
|
87
|
|
|
// If not empty, let's reshuffle the order of some things. |
88
|
|
|
if ( ! empty( $last_query_info ) ) { |
89
|
|
|
$args = $last_query_info['args']; |
90
|
|
|
$response = $last_query_info['response']; |
91
|
|
|
$response_code = $last_query_info['response_code']; |
92
|
|
|
|
93
|
|
|
unset( $last_query_info['args'] ); |
94
|
|
|
unset( $last_query_info['response'] ); |
95
|
|
|
unset( $last_query_info['response_code'] ); |
96
|
|
|
|
97
|
|
|
if ( is_null( $last_query_info['es_time'] ) ) { |
98
|
|
|
$last_query_info['es_time'] = esc_html_x( |
99
|
|
|
'cache hit', |
100
|
|
|
'displayed in search results when results are cached', |
101
|
|
|
'jetpack' |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$temp = array_merge( |
106
|
|
|
array( 'response_code' => $response_code ), |
107
|
|
|
array( 'args' => $args ), |
108
|
|
|
$last_query_info, |
109
|
|
|
array( 'response' => $response ) |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$last_query_info = $temp; |
113
|
|
|
} |
114
|
|
|
?> |
115
|
|
|
<div class="jetpack-search-debug-bar"> |
116
|
|
|
<h2><?php esc_html_e( 'Last query information:', 'jetpack' ); ?></h2> |
117
|
|
|
<?php if ( empty( $last_query_info ) ) : ?> |
118
|
|
|
<?php echo esc_html_x( 'None', 'Text displayed when there is no information', 'jetpack' ); ?> |
119
|
|
|
<?php |
120
|
|
|
else : |
121
|
|
|
foreach ( $last_query_info as $key => $info ) : |
|
|
|
|
122
|
|
|
?> |
123
|
|
|
<h3><?php echo esc_html( $key ); ?></h3> |
124
|
|
|
<?php |
125
|
|
|
if ( 'response' !== $key && 'args' !== $key ) : |
126
|
|
|
?> |
127
|
|
|
<pre><?php print_r( esc_html( $info ) ); ?></pre> |
128
|
|
|
<?php |
129
|
|
|
else : |
130
|
|
|
$this->render_json_toggle( $info ); |
131
|
|
|
endif; |
132
|
|
|
?> |
133
|
|
|
<?php |
134
|
|
|
endforeach; |
135
|
|
|
endif; |
136
|
|
|
?> |
137
|
|
|
</div><!-- Closes .jetpack-search-debug-bar --> |
138
|
|
|
<?php |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Responsible for rendering the HTML necessary for the JSON toggle |
143
|
|
|
* |
144
|
|
|
* @param array $value The resonse from the API as an array. |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
|
|
public function render_json_toggle( $value ) { |
148
|
|
|
?> |
149
|
|
|
<div class="json-toggle-wrap"> |
150
|
|
|
<pre class="json"><?php echo wp_json_encode( $value ); ?></pre> |
151
|
|
|
<span class="pretty toggle"><?php echo esc_html_x( 'Pretty', 'label for formatting JSON', 'jetpack' ); ?></span> |
152
|
|
|
<span class="ugly toggle"><?php echo esc_html_x( 'Minify', 'label for formatting JSON', 'jetpack' ); ?></span> |
153
|
|
|
</div> |
154
|
|
|
<?php |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.