PageRenderTime 31ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/omnisearch/omnisearch-posts.php

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