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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Singleton class instantiated by Jetpack_Searc_Debug_Bar::instance() that handles
  4. * rendering the Jetpack Search debug bar menu item and panel.
  5. */
  6. class Jetpack_Search_Debug_Bar extends Debug_Bar_Panel {
  7. /**
  8. * Holds singleton instance
  9. *
  10. * @var Jetpack_Search_Debug_Bar
  11. */
  12. protected static $instance = null;
  13. /**
  14. * The title to use in the debug bar navigation
  15. *
  16. * @var string
  17. */
  18. public $title;
  19. /**
  20. * Constructor
  21. */
  22. public function __construct() {
  23. $this->title( esc_html__( 'Jetpack Search', 'jetpack' ) );
  24. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
  25. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
  26. add_action( 'login_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
  27. add_action( 'enqueue_embed_scripts', array( $this, 'enqueue_scripts' ) );
  28. }
  29. /**
  30. * Returns the singleton instance of Jetpack_Search_Debug_Bar
  31. *
  32. * @return Jetpack_Search_Debug_Bar
  33. */
  34. public static function instance() {
  35. if ( is_null( self::$instance ) ) {
  36. self::$instance = new Jetpack_Search_Debug_Bar();
  37. }
  38. return self::$instance;
  39. }
  40. /**
  41. * Enqueues styles for our panel in the debug bar
  42. *
  43. * @return void
  44. */
  45. public function enqueue_scripts() {
  46. // Do not enqueue scripts if we haven't already enqueued Debug Bar or Query Monitor styles.
  47. if ( ! wp_style_is( 'debug-bar' ) && ! wp_style_is( 'query-monitor' ) ) {
  48. return;
  49. }
  50. wp_enqueue_style(
  51. 'jetpack-search-debug-bar',
  52. plugins_url( '3rd-party/debug-bar/debug-bar.css', JETPACK__PLUGIN_FILE )
  53. );
  54. wp_enqueue_script(
  55. 'jetpack-search-debug-bar',
  56. plugins_url( '3rd-party/debug-bar/debug-bar.js', JETPACK__PLUGIN_FILE ),
  57. array( 'jquery' )
  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. * Renders the panel content
  73. *
  74. * @return void
  75. */
  76. public function render() {
  77. if ( ! class_exists( 'Jetpack_Search' ) ) {
  78. return;
  79. }
  80. $jetpack_search = Jetpack_Search::instance();
  81. $last_query_info = $jetpack_search->get_last_query_info();
  82. // If not empty, let's reshuffle the order of some things.
  83. if ( ! empty( $last_query_info ) ) {
  84. $args = $last_query_info['args'];
  85. $response = $last_query_info['response'];
  86. $response_code = $last_query_info['response_code'];
  87. unset( $last_query_info['args'] );
  88. unset( $last_query_info['response'] );
  89. unset( $last_query_info['response_code'] );
  90. if ( is_null( $last_query_info['es_time'] ) ) {
  91. $last_query_info['es_time'] = esc_html_x(
  92. 'cache hit',
  93. 'displayed in search results when results are cached',
  94. 'jetpack'
  95. );
  96. }
  97. $temp = array_merge(
  98. array( 'response_code' => $response_code ),
  99. array( 'args' => $args ),
  100. $last_query_info,
  101. array( 'response' => $response )
  102. );
  103. $last_query_info = $temp;
  104. }
  105. ?>
  106. <div class="jetpack-search-debug-bar">
  107. <h2><?php esc_html_e( 'Last query information:', 'jetpack' ); ?></h2>
  108. <?php if ( empty( $last_query_info ) ) : ?>
  109. <?php echo esc_html_x( 'None', 'Text displayed when there is no information', 'jetpack' ); ?>
  110. <?php
  111. else :
  112. foreach ( $last_query_info as $key => $info ) :
  113. ?>
  114. <h3><?php echo esc_html( $key ); ?></h3>
  115. <?php
  116. if ( 'response' !== $key && 'args' !== $key ) :
  117. ?>
  118. <pre><?php print_r( esc_html( $info ) ); ?></pre>
  119. <?php
  120. else :
  121. $this->render_json_toggle( $info );
  122. endif;
  123. ?>
  124. <?php
  125. endforeach;
  126. endif;
  127. ?>
  128. </div><!-- Closes .jetpack-search-debug-bar -->
  129. <?php
  130. }
  131. /**
  132. * Responsible for rendering the HTML necessary for the JSON toggle
  133. *
  134. * @param array $value The resonse from the API as an array.
  135. * @return void
  136. */
  137. public function render_json_toggle( $value ) {
  138. ?>
  139. <div class="json-toggle-wrap">
  140. <pre class="json"><?php echo wp_json_encode( $value ); ?></pre>
  141. <span class="pretty toggle"><?php echo esc_html_x( 'Pretty', 'label for formatting JSON', 'jetpack' ); ?></span>
  142. <span class="ugly toggle"><?php echo esc_html_x( 'Minify', 'label for formatting JSON', 'jetpack' ); ?></span>
  143. </div>
  144. <?php
  145. }
  146. }