Completed
Push — try/woocommerce-analytics ( 2b8c13...0a25c9 )
by
unknown
32:37 queued 22:26
created

Jetpack_Search_Debug_Bar   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 148
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A instance() 0 6 2
A enqueue_scripts() 0 11 1
A is_visible() 0 3 1
B render() 0 61 8
A render_json_toggle() 0 9 1
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
	 * The title to use in the debug bar navigation
17
	 *
18
	 * @var string
19
	 */
20
	public $title;
21
22
	/**
23
	 * Constructor
24
	 */
25
	public function __construct() {
26
		$this->title( esc_html__( 'Jetpack Search', 'jetpack' ) );
27
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
28
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
29
	}
30
31
	/**
32
	 * Returns the singleton instance of Jetpack_Search_Debug_Bar
33
	 *
34
	 * @return Jetpack_Search_Debug_Bar
35
	 */
36
	public static function instance() {
37
		if ( is_null( self::$instance ) ) {
38
			self::$instance = new Jetpack_Search_Debug_Bar();
39
		}
40
		return self::$instance;
41
	}
42
43
	/**
44
	 * Enqueues styles for our panel in the debug bar
45
	 *
46
	 * @return void
47
	 */
48
	public function enqueue_scripts() {
49
		wp_enqueue_style(
50
			'jetpack-search-debug-bar',
51
			plugins_url( '3rd-party/debug-bar/debug-bar.css', JETPACK__PLUGIN_FILE )
52
		);
53
		wp_enqueue_script(
54
			'jetpack-search-debug-bar',
55
			plugins_url( '3rd-party/debug-bar/debug-bar.js', JETPACK__PLUGIN_FILE ),
56
			array( 'jquery' )
57
		);
58
	}
59
60
	/**
61
	 * Should the Jetpack Search Debug Bar show?
62
	 *
63
	 * Since we've previously done a check for the search module being activated, let's just return true.
64
	 * Later on, we can update this to only show when `is_search()` is true.
65
	 *
66
	 * @return boolean
67
	 */
68
	public function is_visible() {
69
		return true;
70
	}
71
72
	/**
73
	 * Renders the panel content
74
	 *
75
	 * @return void
76
	 */
77
	public function render() {
78
		if ( ! class_exists( 'Jetpack_Search' ) ) {
79
			return;
80
		}
81
82
		$jetpack_search = Jetpack_Search::instance();
83
		$last_query_info = $jetpack_search->get_last_query_info();
84
85
		// If not empty, let's reshuffle the order of some things.
86
		if ( ! empty( $last_query_info ) ) {
87
			$args     = $last_query_info['args'];
88
			$response = $last_query_info['response'];
89
			$response_code = $last_query_info['response_code'];
90
91
			unset( $last_query_info['args'] );
92
			unset( $last_query_info['response'] );
93
			unset( $last_query_info['response_code'] );
94
95
			if ( is_null( $last_query_info['es_time'] ) ) {
96
				$last_query_info['es_time'] = esc_html_x(
97
					'cache hit',
98
					'displayed in search results when results are cached',
99
					'jetpack'
100
				);
101
			}
102
103
			$temp = array_merge(
104
				array( 'response_code' => $response_code ),
105
				array( 'args' => $args ),
106
				$last_query_info,
107
				array( 'response' => $response )
108
			);
109
110
			$last_query_info = $temp;
111
		}
112
		?>
113
		<div class="jetpack-search-debug-bar">
114
			<h2><?php esc_html_e( 'Last query information:', 'jetpack' ); ?></h2>
115
			<?php if ( empty( $last_query_info ) ) : ?>
116
					<?php echo esc_html_x( 'None', 'Text displayed when there is no information', 'jetpack' ); ?>
117
			<?php
118
				else :
119
					foreach ( $last_query_info as $key => $info ) :
0 ignored issues
show
Bug introduced by
The expression $last_query_info of type boolean|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. 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:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
120
					?>
121
						<h3><?php echo esc_html( $key ); ?></h3>
122
					<?php
123
					if ( 'response' !== $key && 'args' !== $key ) :
124
					?>
125
						<pre><?php print_r( esc_html( $info ) ); ?></pre>
126
					<?php
127
					else :
128
						$this->render_json_toggle( $info );
129
					endif;
130
					?>
131
					<?php
132
					endforeach;
133
			endif;
134
			?>
135
		</div><!-- Closes .jetpack-search-debug-bar -->
136
		<?php
137
	}
138
139
	/**
140
	 * Responsible for rendering the HTML necessary for the JSON toggle
141
	 *
142
	 * @param array $value The resonse from the API as an array.
143
	 * @return void
144
	 */
145
	public function render_json_toggle( $value ) {
146
	?>
147
		<div class="json-toggle-wrap">
148
			<pre class="json"><?php echo wp_json_encode( $value ); ?></pre>
149
			<span class="pretty toggle"><?php echo esc_html_x( 'Pretty', 'label for formatting JSON', 'jetpack' ); ?></span>
150
			<span class="ugly toggle"><?php echo esc_html_x( 'Minify', 'label for formatting JSON', 'jetpack' ); ?></span>
151
		</div>
152
	<?php
153
	}
154
}
155