comment-meta.php
6 years ago
comment.php
6 years ago
meta.php
6 years ago
options.php
6 years ago
post-meta.php
6 years ago
post.php
6 years ago
user-meta.php
6 years ago
user.php
6 years ago
post.php
151 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex; |
| 4 | |
| 5 | use SearchRegex\Search_Source; |
| 6 | use SearchRegex\Result; |
| 7 | |
| 8 | class Source_Post extends Search_Source { |
| 9 | /** |
| 10 | * Get the custom post type from the source |
| 11 | * |
| 12 | * @param String $post_type Source post type. |
| 13 | * @return null|Array |
| 14 | */ |
| 15 | private function get_post_type( $post_type ) { |
| 16 | $post_types = array_values( array_filter( Source_Manager::get_all_grouped(), function( $source ) { |
| 17 | return $source['name'] === 'posttype'; |
| 18 | } ) ); |
| 19 | |
| 20 | if ( count( $post_types ) === 1 ) { |
| 21 | foreach ( $post_types[0]['sources'] as $source ) { |
| 22 | if ( $source['name'] === $post_type ) { |
| 23 | return $source; |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | return null; |
| 29 | } |
| 30 | |
| 31 | public function get_type( array $row ) { |
| 32 | $post_type = isset( $row['post_type'] ) ? $this->get_post_type( $row['post_type'] ) : false; |
| 33 | if ( $post_type ) { |
| 34 | return $post_type['name']; |
| 35 | } |
| 36 | |
| 37 | return $this->source_type; |
| 38 | } |
| 39 | |
| 40 | public function get_name( array $row ) { |
| 41 | $post_type = isset( $row['post_type'] ) ? $this->get_post_type( $row['post_type'] ) : false; |
| 42 | if ( $post_type ) { |
| 43 | return $post_type['label']; |
| 44 | } |
| 45 | |
| 46 | return $this->source_name; |
| 47 | } |
| 48 | |
| 49 | public function get_supported_flags() { |
| 50 | return [ |
| 51 | 'post_guid' => __( 'Search GUID', 'search-regex' ), |
| 52 | ]; |
| 53 | } |
| 54 | |
| 55 | public function get_actions( Result $result ) { |
| 56 | $edit = get_edit_post_link( $result->get_row_id(), '' ); |
| 57 | $view = get_permalink( $result->get_row_id() ); |
| 58 | |
| 59 | if ( $edit ) { |
| 60 | return [ |
| 61 | 'edit' => $edit, |
| 62 | 'view' => $view, |
| 63 | ]; |
| 64 | } |
| 65 | |
| 66 | return []; |
| 67 | } |
| 68 | |
| 69 | public function get_search_conditions( $search ) { |
| 70 | global $wpdb; |
| 71 | |
| 72 | // If searching a particular post type then just look there |
| 73 | if ( $this->source_type !== 'posts' ) { |
| 74 | return [ $wpdb->prepare( 'post_type=%s', $this->source_type ) ]; |
| 75 | } |
| 76 | |
| 77 | return []; |
| 78 | } |
| 79 | |
| 80 | public function get_info_columns() { |
| 81 | return [ 'post_type' ]; |
| 82 | } |
| 83 | |
| 84 | public function get_column_label( $column ) { |
| 85 | $labels = [ |
| 86 | 'post_content' => __( 'Content', 'search-regex' ), |
| 87 | 'post_excerpt' => __( 'Excerpt', 'search-regex' ), |
| 88 | 'post_title' => __( 'Title', 'search-regex' ), |
| 89 | 'post_name' => __( 'Slug', 'search-regex' ), |
| 90 | 'guid' => __( 'GUID', 'search-regex' ), |
| 91 | ]; |
| 92 | |
| 93 | if ( isset( $labels[ $column ] ) ) { |
| 94 | return $labels[ $column ]; |
| 95 | } |
| 96 | |
| 97 | return $column; |
| 98 | } |
| 99 | |
| 100 | public function get_columns() { |
| 101 | $columns = [ |
| 102 | 'post_content', |
| 103 | 'post_excerpt', |
| 104 | 'post_title', |
| 105 | 'post_name', |
| 106 | ]; |
| 107 | |
| 108 | if ( $this->source_flags->has_flag( 'post_guid' ) ) { |
| 109 | $columns[] = 'guid'; |
| 110 | } |
| 111 | |
| 112 | return $columns; |
| 113 | } |
| 114 | |
| 115 | public function get_title_column() { |
| 116 | return 'post_title'; |
| 117 | } |
| 118 | |
| 119 | public function get_table_id() { |
| 120 | return 'ID'; |
| 121 | } |
| 122 | |
| 123 | public function get_table_name() { |
| 124 | global $wpdb; |
| 125 | |
| 126 | return $wpdb->posts; |
| 127 | } |
| 128 | |
| 129 | public function save( $row_id, $column_id, $content ) { |
| 130 | // This does all the sanitization |
| 131 | $result = wp_update_post( [ |
| 132 | 'ID' => $row_id, |
| 133 | $column_id => $content, |
| 134 | ] ); |
| 135 | |
| 136 | if ( $result ) { |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | return new \WP_Error( 'searchregex', 'Failed to update post' ); |
| 141 | } |
| 142 | |
| 143 | public function delete_row( $row_id ) { |
| 144 | if ( wp_delete_post( $row_id ) ) { |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | return new \WP_Error( 'searchregex_delete', 'Failed to delete post', 401 ); |
| 149 | } |
| 150 | } |
| 151 |