summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/3rd-party/debug-bar')
-rw-r--r--plugins/jetpack/3rd-party/debug-bar/class.jetpack-search-debug-bar.php173
-rw-r--r--plugins/jetpack/3rd-party/debug-bar/debug-bar.css56
-rw-r--r--plugins/jetpack/3rd-party/debug-bar/debug-bar.js22
3 files changed, 251 insertions, 0 deletions
diff --git a/plugins/jetpack/3rd-party/debug-bar/class.jetpack-search-debug-bar.php b/plugins/jetpack/3rd-party/debug-bar/class.jetpack-search-debug-bar.php
new file mode 100644
index 00000000..025f7c21
--- /dev/null
+++ b/plugins/jetpack/3rd-party/debug-bar/class.jetpack-search-debug-bar.php
@@ -0,0 +1,173 @@
+<?php
+
+/**
+ * Singleton class instantiated by Jetpack_Searc_Debug_Bar::instance() that handles
+ * rendering the Jetpack Search debug bar menu item and panel.
+ */
+class Jetpack_Search_Debug_Bar extends Debug_Bar_Panel {
+ /**
+ * Holds singleton instance
+ *
+ * @var Jetpack_Search_Debug_Bar
+ */
+ protected static $instance = null;
+
+ /**
+ * The title to use in the debug bar navigation
+ *
+ * @var string
+ */
+ public $title;
+
+ /**
+ * Constructor
+ */
+ public function __construct() {
+ $this->title( esc_html__( 'Jetpack Search', 'jetpack' ) );
+ add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
+ add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
+ add_action( 'login_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
+ add_action( 'enqueue_embed_scripts', array( $this, 'enqueue_scripts' ) );
+ }
+
+ /**
+ * Returns the singleton instance of Jetpack_Search_Debug_Bar
+ *
+ * @return Jetpack_Search_Debug_Bar
+ */
+ public static function instance() {
+ if ( is_null( self::$instance ) ) {
+ self::$instance = new Jetpack_Search_Debug_Bar();
+ }
+ return self::$instance;
+ }
+
+ /**
+ * Enqueues styles for our panel in the debug bar
+ *
+ * @return void
+ */
+ public function enqueue_scripts() {
+ // Do not enqueue scripts if we haven't already enqueued Debug Bar or Query Monitor styles.
+ if ( ! wp_style_is( 'debug-bar' ) && ! wp_style_is( 'query-monitor' ) ) {
+ return;
+ }
+
+ wp_enqueue_style(
+ 'jetpack-search-debug-bar',
+ plugins_url( '3rd-party/debug-bar/debug-bar.css', JETPACK__PLUGIN_FILE )
+ );
+ wp_enqueue_script(
+ 'jetpack-search-debug-bar',
+ plugins_url( '3rd-party/debug-bar/debug-bar.js', JETPACK__PLUGIN_FILE ),
+ array( 'jquery' )
+ );
+ }
+
+ /**
+ * Should the Jetpack Search Debug Bar show?
+ *
+ * Since we've previously done a check for the search module being activated, let's just return true.
+ * Later on, we can update this to only show when `is_search()` is true.
+ *
+ * @return boolean
+ */
+ public function is_visible() {
+ return true;
+ }
+
+ /**
+ * Renders the panel content
+ *
+ * @return void
+ */
+ public function render() {
+ if ( ! class_exists( 'Jetpack_Search' ) ) {
+ return;
+ }
+
+ $jetpack_search = Jetpack_Search::instance();
+ $last_query_info = $jetpack_search->get_last_query_info();
+
+ // If not empty, let's reshuffle the order of some things.
+ if ( ! empty( $last_query_info ) ) {
+ $args = $last_query_info['args'];
+ $response = $last_query_info['response'];
+ $response_code = $last_query_info['response_code'];
+
+ unset( $last_query_info['args'] );
+ unset( $last_query_info['response'] );
+ unset( $last_query_info['response_code'] );
+
+ if ( is_null( $last_query_info['es_time'] ) ) {
+ $last_query_info['es_time'] = esc_html_x(
+ 'cache hit',
+ 'displayed in search results when results are cached',
+ 'jetpack'
+ );
+ }
+
+ $temp = array_merge(
+ array( 'response_code' => $response_code ),
+ array( 'args' => $args ),
+ $last_query_info,
+ array( 'response' => $response )
+ );
+
+ $last_query_info = $temp;
+ }
+ ?>
+ <div class="jetpack-search-debug-bar">
+ <h2><?php esc_html_e( 'Last query information:', 'jetpack' ); ?></h2>
+ <?php if ( empty( $last_query_info ) ) : ?>
+ <?php echo esc_html_x( 'None', 'Text displayed when there is no information', 'jetpack' ); ?>
+ <?php
+ else :
+ foreach ( $last_query_info as $key => $info ) :
+ ?>
+ <h3><?php echo esc_html( $key ); ?></h3>
+ <?php
+ if ( 'response' !== $key && 'args' !== $key ) :
+ ?>
+ <pre><?php print_r( esc_html( $info ) ); ?></pre>
+ <?php
+ else :
+ $this->render_json_toggle( $info );
+ endif;
+ ?>
+ <?php
+ endforeach;
+ endif;
+ ?>
+ </div><!-- Closes .jetpack-search-debug-bar -->
+ <?php
+ }
+
+ /**
+ * Responsible for rendering the HTML necessary for the JSON toggle
+ *
+ * @param array $value The resonse from the API as an array.
+ * @return void
+ */
+ public function render_json_toggle( $value ) {
+ ?>
+ <div class="json-toggle-wrap">
+ <pre class="json"><?php
+ // esc_html() will not double-encode entities (&amp; -> &amp;amp;).
+ // If any entities are part of the JSON blob, we want to re-encoode them
+ // (double-encode them) so that they are displayed correctly in the debug
+ // bar.
+ // Use _wp_specialchars() "manually" to ensure entities are encoded correctly.
+ echo _wp_specialchars(
+ wp_json_encode( $value ),
+ ENT_NOQUOTES, // Don't need to encode quotes (output is for a text node).
+ 'UTF-8', // wp_json_encode() outputs UTF-8 (really just ASCII), not the blog's charset.
+ true // Do "double-encode" existing HTML entities
+ );
+ ?></pre>
+ <span class="pretty toggle"><?php echo esc_html_x( 'Pretty', 'label for formatting JSON', 'jetpack' ); ?></span>
+ <span class="ugly toggle"><?php echo esc_html_x( 'Minify', 'label for formatting JSON', 'jetpack' ); ?></span>
+ </div>
+ <?php
+ }
+}
diff --git a/plugins/jetpack/3rd-party/debug-bar/debug-bar.css b/plugins/jetpack/3rd-party/debug-bar/debug-bar.css
new file mode 100644
index 00000000..2cc922c4
--- /dev/null
+++ b/plugins/jetpack/3rd-party/debug-bar/debug-bar.css
@@ -0,0 +1,56 @@
+.jetpack-search-debug-bar h2,
+.qm-debug-bar-output .jetpack-search-debug-bar h2 {
+ float: none !important;
+ padding: 0 !important;
+ text-align: left !important;
+}
+
+.qm-debug-bar-output .jetpack-search-debug-bar h2 {
+ margin-top: 1em !important;
+}
+
+.qm-debug-bar-output .jetpack-search-debug-bar h2:first-child {
+ margin-top: .5em !important;
+}
+
+.debug-menu-target h3 {
+ padding-top: 0
+}
+
+.jetpack-search-debug-output-toggle .print-r {
+ display: none;
+}
+
+.json-toggle-wrap {
+ position: relative;
+}
+
+.json-toggle-wrap .toggle {
+ position: absolute;
+ bottom: 10px;
+ right: 10px;
+ background: #fff;
+ border: 1px solid #000;
+ cursor: pointer;
+ padding: 2px 4px;
+}
+
+.json-toggle-wrap .ugly {
+ display: none;
+}
+
+.json-toggle-wrap.pretty .pretty {
+ display: none;
+}
+
+.json-toggle-wrap.pretty .ugly {
+ display: inline;
+}
+
+.jetpack-search-debug-bar pre {
+ white-space: pre-wrap;
+ white-space: -moz-pre-wrap;
+ white-space: -pre-wrap;
+ white-space: -o-pre-wrap;
+ word-wrap: break-word;
+}
diff --git a/plugins/jetpack/3rd-party/debug-bar/debug-bar.js b/plugins/jetpack/3rd-party/debug-bar/debug-bar.js
new file mode 100644
index 00000000..5043cf10
--- /dev/null
+++ b/plugins/jetpack/3rd-party/debug-bar/debug-bar.js
@@ -0,0 +1,22 @@
+/* global jQuery, JSON */
+
+( function( $ ) {
+ $( document ).ready( function() {
+ $( '.jetpack-search-debug-bar .json-toggle-wrap .toggle' ).click( function() {
+ var t = $( this ),
+ wrap = t.closest( '.json-toggle-wrap' ),
+ pre = wrap.find( 'pre' ),
+ content = pre.text(),
+ isPretty = wrap.hasClass( 'pretty' );
+
+ if ( ! isPretty ) {
+ pre.text( JSON.stringify( JSON.parse( content ), null, 2 ) );
+ } else {
+ content.replace( '\t', '' ).replace( '\n', '' ).replace( ' ', '' );
+ pre.text( JSON.stringify( JSON.parse( content ) ) );
+ }
+
+ wrap.toggleClass( 'pretty' );
+ } );
+ } );
+} )( jQuery );