/modules/omnisearch/omnisearch-posts.php

  1. <?php 
  2.  
  3. if( ! class_exists( 'WP_List_Table' ) ) 
  4. require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); 
  5.  
  6. class Jetpack_Omnisearch_Posts extends WP_List_Table { 
  7. public $post_type = 'post',  
  8. $post_type_object; 
  9.  
  10. function __construct( $post_type = 'post' ) { 
  11. $this->post_type = $post_type; 
  12. add_filter( 'omnisearch_results', array( $this, 'search'), 10, 2 ); 
  13.  
  14. // Push 'post_type_obj' to accepted fields for WP_List_Table (since WP 4.2) 
  15. global $wp_version; 
  16. if ( version_compare( $wp_version, '4.2-z', '>=' ) && $this->compat_fields && is_array( $this->compat_fields ) ) { 
  17. array_push( $this->compat_fields, 'post_type_obj', 'posts' ); 
  18.  
  19. function search( $results, $search_term ) { 
  20. if( ! post_type_exists( $this->post_type ) ) 
  21. return $results; 
  22.  
  23. parent::__construct(); 
  24.  
  25. $this->post_type_obj = get_post_type_object( $this->post_type ); 
  26.  
  27. $search_url = esc_url( admin_url( sprintf( 'edit.php?post_type=%s&s=%s', urlencode( $this->post_type_obj->name ), urlencode( $search_term ) ) ) ); 
  28. $search_link = sprintf( ' <a href="%s" class="add-new-h2">%s</a>', $search_url, esc_html( $this->post_type_obj->labels->search_items ) ); 
  29. $html = '<h2>' . esc_html( $this->post_type_obj->labels->name ) . $search_link .'</h2>'; 
  30.  
  31. /** This action is documented in modules/omnisearch/omnisearch-core.php */ 
  32. $num_results = apply_filters( 'omnisearch_num_results', 5 ); 
  33.  
  34. $this->posts = get_posts( array( 
  35. 's' => $search_term,  
  36. 'post_type' => $this->post_type,  
  37. 'posts_per_page' => $num_results,  
  38. 'post_status' => 'any',  
  39. 'suppress_filters' => false,  
  40. ) ); 
  41.  
  42. $this->prepare_items(); 
  43.  
  44. ob_start(); 
  45. $this->display(); 
  46. $html .= ob_get_clean(); 
  47.  
  48. $results[ $this->post_type_obj->labels->name ] = $html; 
  49. return $results; 
  50.  
  51. function get_columns() { 
  52. $columns = array( 
  53. # 'id' => __('ID', 'jetpack'),  
  54. 'post_title' => __('Title', 'jetpack'),  
  55. 'snippet' => __('Snippet', 'jetpack'),  
  56. 'date' => __('Date', 'jetpack'),  
  57. ); 
  58. return $columns; 
  59.  
  60. function prepare_items() { 
  61. $columns = $this->get_columns(); 
  62. $hidden = array(); 
  63. $sortable = array(); 
  64. $this->_column_headers = array( $columns, $hidden, $sortable ); 
  65. $this->items = $this->posts; 
  66.  
  67. function column_post_title( $post ) { 
  68. $actions = array(); 
  69. if ( current_user_can( $this->post_type_obj->cap->edit_post, $post ) ) { 
  70. $post_title = sprintf( '<a href="%s">%s</a>', esc_url( get_edit_post_link( $post->ID ) ), wptexturize( $post->post_title ) ); 
  71. $actions['edit'] = sprintf( '<a href="%s">%s</a>', esc_url( get_edit_post_link( $post->ID ) ), esc_html( $this->post_type_obj->labels->edit_item ) ); 
  72. } else { 
  73. $post_title = wptexturize( $post->post_title ); 
  74. if ( current_user_can( $this->post_type_obj->cap->delete_post, $post ) ) { 
  75. $actions['delete'] = sprintf( '<a href="%s">%s</a>', esc_url( get_delete_post_link( $post->ID ) ), esc_html__('Trash', 'jetpack') ); 
  76. $actions['view'] = sprintf( '<a href="%s">%s</a>', esc_url( get_permalink( $post->ID ) ), esc_html( $this->post_type_obj->labels->view_item ) ); 
  77. return $post_title . $this->row_actions( $actions ); 
  78.  
  79. function column_date( $post ) { 
  80. $html = ''; 
  81.  
  82. if ( '0000-00-00 00:00:00' == $post->post_date ) { 
  83. $t_time = $h_time = __('Unpublished', 'jetpack'); 
  84. $time_diff = 0; 
  85. } else { 
  86. $t_time = date( __('Y/m/d g:i:s A', 'jetpack'), mysql2date( 'G', $post->post_date ) ); 
  87. $m_time = $post->post_date; 
  88. $time = get_post_time( 'G', true, $post ); 
  89.  
  90. $time_diff = time() - $time; 
  91.  
  92. if ( $time_diff > 0 && $time_diff < DAY_IN_SECONDS ) 
  93. $h_time = sprintf( __('%s ago', 'jetpack'), human_time_diff( $time ) ); 
  94. else 
  95. $h_time = mysql2date( __('Y/m/d', 'jetpack'), $m_time ); 
  96.  
  97. $html .= '<abbr title="' . esc_attr( $t_time ) . '">' . esc_html( $h_time ) . '</abbr>'; 
  98. $html .= '<br />'; 
  99. if ( 'publish' == $post->post_status ) { 
  100. $html .= esc_html__('Published', 'jetpack'); 
  101. } elseif ( 'future' == $post->post_status ) { 
  102. if ( $time_diff > 0 ) 
  103. $html .= '<strong class="attention">' . esc_html__('Missed schedule', 'jetpack') . '</strong>'; 
  104. else 
  105. $html .= esc_html__('Scheduled', 'jetpack'); 
  106. } else { 
  107. $html .= esc_html__('Last Modified', 'jetpack'); 
  108. return $html; 
  109.  
  110. function column_default( $post, $column_name ) { 
  111. switch ( $column_name ) { 
  112. case 'id': 
  113. return $post->ID; 
  114. case 'post_title': // Will never happen, class method overrides. 
  115. return $post->post_title; 
  116. case 'snippet': 
  117. return wp_trim_words( $post->post_content, 55 ); 
  118. case 'date': // Will never happen, class method overrides. 
  119. $d = get_option('date_format'); 
  120. $t = get_option('time_format'); 
  121. return get_post_modified_time( $d, 0, $post, 1 ) . ' @ ' . get_post_modified_time( $t, 0, $post, 1 ); 
  122. default: 
  123. return print_r( $post, true ); 
.