No Description

class-jetpack-search-debug-bar.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * Adds a Jetpack Search debug panel to Debug Bar.
  4. *
  5. * @package automattic/jetpack
  6. */
  7. /**
  8. * Singleton class instantiated by Jetpack_Searc_Debug_Bar::instance() that handles
  9. * rendering the Jetpack Search debug bar menu item and panel.
  10. */
  11. class Jetpack_Search_Debug_Bar extends Debug_Bar_Panel {
  12. /**
  13. * Holds singleton instance
  14. *
  15. * @var Jetpack_Search_Debug_Bar
  16. */
  17. protected static $instance = null;
  18. /**
  19. * The title to use in the debug bar navigation
  20. *
  21. * @var string
  22. */
  23. public $title;
  24. /**
  25. * Constructor
  26. */
  27. public function __construct() {
  28. $this->title( esc_html__( 'Jetpack Search', 'jetpack' ) );
  29. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
  30. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
  31. add_action( 'login_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
  32. add_action( 'enqueue_embed_scripts', array( $this, 'enqueue_scripts' ) );
  33. }
  34. /**
  35. * Returns the singleton instance of Jetpack_Search_Debug_Bar
  36. *
  37. * @return Jetpack_Search_Debug_Bar
  38. */
  39. public static function instance() {
  40. if ( is_null( self::$instance ) ) {
  41. self::$instance = new Jetpack_Search_Debug_Bar();
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * Enqueues styles for our panel in the debug bar
  47. *
  48. * @return void
  49. */
  50. public function enqueue_scripts() {
  51. // Do not enqueue scripts if we haven't already enqueued Debug Bar or Query Monitor styles.
  52. if ( ! wp_style_is( 'debug-bar' ) && ! wp_style_is( 'query-monitor' ) ) {
  53. return;
  54. }
  55. wp_enqueue_style(
  56. 'jetpack-search-debug-bar',
  57. plugins_url( '3rd-party/debug-bar/debug-bar.css', JETPACK__PLUGIN_FILE ),
  58. array(),
  59. JETPACK__VERSION
  60. );
  61. wp_enqueue_script(
  62. 'jetpack-search-debug-bar',
  63. plugins_url( '3rd-party/debug-bar/debug-bar.js', JETPACK__PLUGIN_FILE ),
  64. array( 'jquery' ),
  65. JETPACK__VERSION,
  66. true
  67. );
  68. }
  69. /**
  70. * Should the Jetpack Search Debug Bar show?
  71. *
  72. * Since we've previously done a check for the search module being activated, let's just return true.
  73. * Later on, we can update this to only show when `is_search()` is true.
  74. *
  75. * @return boolean
  76. */
  77. public function is_visible() {
  78. return true;
  79. }
  80. /**
  81. * Renders the panel content
  82. *
  83. * @return void
  84. */
  85. public function render() {
  86. if ( ! class_exists( 'Jetpack_Search' ) ) {
  87. return;
  88. }
  89. $jetpack_search = Jetpack_Search::instance();
  90. $last_query_info = $jetpack_search->get_last_query_info();
  91. // If not empty, let's reshuffle the order of some things.
  92. if ( ! empty( $last_query_info ) ) {
  93. $args = $last_query_info['args'];
  94. $response = $last_query_info['response'];
  95. $response_code = $last_query_info['response_code'];
  96. unset( $last_query_info['args'] );
  97. unset( $last_query_info['response'] );
  98. unset( $last_query_info['response_code'] );
  99. if ( is_null( $last_query_info['es_time'] ) ) {
  100. $last_query_info['es_time'] = esc_html_x(
  101. 'cache hit',
  102. 'displayed in search results when results are cached',
  103. 'jetpack'
  104. );
  105. }
  106. $temp = array_merge(
  107. array( 'response_code' => $response_code ),
  108. array( 'args' => $args ),
  109. $last_query_info,
  110. array( 'response' => $response )
  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 ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions ?></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. * Responsible for rendering the HTML necessary for the JSON toggle
  142. *
  143. * @param array $value The resonse from the API as an array.
  144. * @return void
  145. */
  146. public function render_json_toggle( $value ) {
  147. ?>
  148. <div class="json-toggle-wrap">
  149. <pre class="json">
  150. <?php
  151. // esc_html() will not double-encode entities (&amp; -> &amp;amp;).
  152. // If any entities are part of the JSON blob, we want to re-encoode them
  153. // (double-encode them) so that they are displayed correctly in the debug
  154. // bar.
  155. // Use _wp_specialchars() "manually" to ensure entities are encoded correctly.
  156. echo _wp_specialchars( // phpcs:ignore WordPress.Security.EscapeOutput
  157. wp_json_encode( $value ),
  158. ENT_NOQUOTES, // Don't need to encode quotes (output is for a text node).
  159. 'UTF-8', // wp_json_encode() outputs UTF-8 (really just ASCII), not the blog's charset.
  160. true // Do "double-encode" existing HTML entities.
  161. );
  162. ?>
  163. </pre>
  164. <span class="pretty toggle"><?php echo esc_html_x( 'Pretty', 'label for formatting JSON', 'jetpack' ); ?></span>
  165. <span class="ugly toggle"><?php echo esc_html_x( 'Minify', 'label for formatting JSON', 'jetpack' ); ?></span>
  166. </div>
  167. <?php
  168. }
  169. }