Jetpack_Omnisearch_Posts

The Jetpack by WordPress.com Jetpack Omnisearch Posts class.

Defined (1)

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

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