Jetpack_Omnisearch

The Jetpack by WordPress.com Jetpack Omnisearch class.

Defined (1)

The class is defined in the following location(s).

/modules/omnisearch/omnisearch-core.php  
  1. class Jetpack_Omnisearch { 
  2. static $instance; 
  3. static $num_results = 5; 
  4.  
  5. function __construct() { 
  6. self::$instance = $this; 
  7. add_action( 'wp_loaded', array( $this, 'wp_loaded' ) ); 
  8. add_action( 'admin_init', array( $this, 'add_providers' ) ); 
  9. add_action( 'jetpack_admin_menu', array( $this, 'jetpack_admin_menu' ) ); 
  10. add_action( 'admin_menu', array( $this, 'admin_menu' ), 20 ); 
  11. if( ! jetpack_is_mobile() ) { 
  12. add_action( 'admin_bar_menu', array( $this, 'admin_bar_search' ), 4 ); 
  13. add_filter( 'omnisearch_num_results', array( $this, 'omnisearch_num_results' ) ); 
  14.  
  15. static function add_providers() { 
  16. // omnisearch-posts.php is included above, so that other plugins can more easily extend it. 
  17. new Jetpack_Omnisearch_Posts; 
  18. new Jetpack_Omnisearch_Posts( 'page' ); 
  19.  
  20. require_once( dirname(__FILE__) . '/omnisearch-comments.php' ); 
  21. new Jetpack_Omnisearch_Comments; 
  22.  
  23. if ( current_user_can( 'upload_files' ) ) { 
  24. require_once( dirname(__FILE__) . '/omnisearch-media.php' ); 
  25. new Jetpack_Omnisearch_Media; 
  26.  
  27. if ( current_user_can( 'install_plugins' ) ) { 
  28. require_once( dirname(__FILE__) . '/omnisearch-plugins.php' ); 
  29. new Jetpack_Omnisearch_Plugins; 
  30.  
  31. /** 
  32. * Fires after each default omnisearch provider has been required. 
  33. * Can be used to add your own Omnisearch provider. 
  34. * @module minileven 
  35. * @since 2.3.2 
  36. */ 
  37. do_action( 'omnisearch_add_providers' ); 
  38.  
  39. static function omnisearch_num_results( $num ) { 
  40. return self::$num_results; 
  41.  
  42. function wp_loaded() { 
  43. $deps = null; 
  44. if ( wp_style_is( 'genericons', 'registered' ) ) { 
  45. $deps = array( 'genericons' ); 
  46. if( is_rtl() ) { 
  47. wp_register_style( 'omnisearch-admin', plugins_url( 'rtl/omnisearch-rtl.css', __FILE__ ), $deps ); 
  48. } else { 
  49. wp_register_style( 'omnisearch-admin', plugins_url( 'omnisearch.css', __FILE__ ), $deps ); 
  50.  
  51.  
  52. function jetpack_admin_menu() { 
  53. remove_submenu_page( 'index.php', 'omnisearch' ); 
  54. $this->slug = add_submenu_page( 'jetpack', __( 'Omnisearch', 'jetpack' ), __( 'Omnisearch', 'jetpack' ), 'edit_posts', 'omnisearch', array( $this, 'omnisearch_page' ) ); 
  55. add_action( "admin_print_styles-{$this->slug}", array( $this, 'admin_print_styles_jetpack' ) ); 
  56.  
  57. function admin_menu() { 
  58. $this->slug = add_dashboard_page( __( 'Omnisearch', 'jetpack' ), __( 'Omnisearch', 'jetpack' ), 'edit_posts', 'omnisearch', array( $this, 'omnisearch_page' ) ); 
  59. add_action( "admin_print_styles-{$this->slug}", array( $this, 'admin_print_styles' ) ); 
  60.  
  61. function admin_print_styles() { 
  62. wp_enqueue_style( 'omnisearch-admin' ); 
  63.  
  64. function admin_print_styles_jetpack() { 
  65. wp_enqueue_style( 'omnisearch-admin' ); 
  66. wp_enqueue_style( 'omnisearch-jetpack' ); 
  67.  
  68. function omnisearch_page() { 
  69. $results = array(); 
  70. $s = isset( $_GET['s'] ) ? $_GET['s'] : ''; 
  71. if ( $s ) { 
  72. /** 
  73. * Filter the results returned for a given Omnisearch search query. 
  74. * @module minileven 
  75. * @since 2.3.0 
  76. * @param array $results Array of Omnisearch results. 
  77. * @param string $s Search parameter. 
  78. */ 
  79. $results = apply_filters( 'omnisearch_results', $results, $s ); 
  80. /** 
  81. * Filter the number of results displayed for each Omnisearch searched section. 
  82. * @module minileven 
  83. * @since 2.3.0 
  84. * @param int 5 Number of results displayed for each Omnisearch searched section. 
  85. */ 
  86. $num_results = intval( apply_filters( 'omnisearch_num_results', 5 ) ); 
  87. ?> 
  88. <div class="wrap"> 
  89. <h2 class="page-title"><?php esc_html_e( 'Omnisearch', 'jetpack' ); ?> <small><?php esc_html_e( 'search everything', 'jetpack' ); ?></small></h2> 
  90. <br class="clear" /> 
  91. <?php echo self::get_omnisearch_form( array( 
  92. 'form_class' => 'omnisearch-form',  
  93. 'search_class' => 'omnisearch',  
  94. 'search_placeholder' => '',  
  95. 'submit_class' => 'omnisearch-submit',  
  96. 'alternate_submit' => true,  
  97. ) ); ?> 
  98. <?php if( ! empty( $results ) ): ?> 
  99. <h3 id="results-title"><?php esc_html_e( 'Results:', 'jetpack' ); ?></h3> 
  100. <div class="jump-to"><strong><?php esc_html_e( 'Jump to:', 'jetpack' ); ?></strong> 
  101. <?php foreach( $results as $label => $result ) : ?> 
  102. <a href="#result-<?php echo sanitize_title( $label ); ?>"><?php echo esc_html( $label ); ?></a> 
  103. <?php endforeach; ?> 
  104. </div> 
  105. <br class="clear" /> 
  106. <script>var search_term = '<?php echo esc_js( $s ); ?>', num_results = <?php echo $num_results; ?>;</script> 
  107. <ul class="omnisearch-results"> 
  108. <?php foreach( $results as $label => $result ) : ?> 
  109. <li id="result-<?php echo sanitize_title( $label ); ?>" data-label="<?php echo esc_attr( $label ); ?>"> 
  110. <?php echo $result; ?> 
  111. <a class="back-to-top" href="#results-title"><?php esc_html_e( 'Back to Top ↑', 'jetpack' ); ?></a> 
  112. </li> 
  113. <?php endforeach; ?> 
  114. </ul> 
  115. <?php endif; ?> 
  116. </div><!-- /wrap --> 
  117. <?php 
  118.  
  119. function admin_bar_search( $wp_admin_bar ) { 
  120. if( ! is_admin() || ! current_user_can( 'edit_posts' ) ) 
  121. return; 
  122.  
  123. $form = self::get_omnisearch_form( array( 
  124. 'form_id' => 'adminbarsearch',  
  125. 'search_id' => 'adminbar-search',  
  126. 'search_class' => 'adminbar-input',  
  127. 'submit_class' => 'adminbar-button',  
  128. ) ); 
  129.  
  130. $form .= "<style> 
  131. #adminbar-search::-webkit-input-placeholder,  
  132. #adminbar-search:-moz-placeholder,  
  133. #adminbar-search::-moz-placeholder,  
  134. #adminbar-search:-ms-input-placeholder { 
  135. text-shadow: none; 
  136. </style>"; 
  137.  
  138. $wp_admin_bar->add_menu( array( 
  139. 'parent' => 'top-secondary',  
  140. 'id' => 'search',  
  141. 'title' => $form,  
  142. 'meta' => array( 
  143. 'class' => 'admin-bar-search',  
  144. 'tabindex' => -1,  
  145. ) ); 
  146.  
  147. static function get_omnisearch_form( $args = array() ) { 
  148. $defaults = array( 
  149. 'form_id' => null,  
  150. 'form_class' => null,  
  151. 'search_class' => null,  
  152. 'search_id' => null,  
  153. 'search_value' => isset( $_REQUEST['s'] ) ? $_REQUEST['s'] : null,  
  154. 'search_placeholder' => __( 'Search Everything', 'jetpack' ),  
  155. 'submit_class' => 'button',  
  156. 'submit_value' => __( 'Search', 'jetpack' ),  
  157. 'alternate_submit' => false,  
  158. ); 
  159. extract( array_map( 'esc_attr', wp_parse_args( $args, $defaults ) ) ); 
  160.  
  161. $rand = rand(); 
  162. if( empty( $form_id ) ) 
  163. $form_id = "omnisearch_form_$rand"; 
  164. if( empty( $search_id ) ) 
  165. $search_id = "omnisearch_search_$rand"; 
  166.  
  167. ob_start(); 
  168. ?> 
  169.  
  170. <form action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" method="get" class="<?php echo $form_class; ?>" id="<?php echo $form_id; ?>"> 
  171. <input type="hidden" name="page" value="omnisearch" /> 
  172. <input name="s" type="search" class="<?php echo $search_class; ?>" id="<?php echo $search_id; ?>" value="<?php echo $search_value; ?>" placeholder="<?php echo $search_placeholder; ?>" /> 
  173. <?php if ( $alternate_submit ) : ?> 
  174. <button type="submit" class="<?php echo $submit_class; ?>"><span><?php echo $submit_value; ?></span></button> 
  175. <?php else : ?> 
  176. <input type="submit" class="<?php echo $submit_class; ?>" value="<?php echo $submit_value; ?>" /> 
  177. <?php endif; ?> 
  178. </form> 
  179.  
  180. <?php 
  181. /** 
  182. * Filters the Omnisearch search form output. 
  183. * @module minileven 
  184. * @since 2.3.0 
  185. * @param string ob_get_clean() Omnisearch search form output. 
  186. * @param array $args Array of arguments to pass to the form to overwrite the default form parameters. 
  187. * @param array $defaults Array of default form parameters. 
  188. */ 
  189. return apply_filters( 'get_omnisearch_form', ob_get_clean(), $args, $defaults ); 
  190.