comment-meta.php
6 years ago
comment.php
5 years ago
meta.php
5 years ago
options.php
5 years ago
post-meta.php
6 years ago
post.php
5 years ago
user-meta.php
6 years ago
user.php
5 years ago
post.php
193 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 | * Array of supported custom post types |
| 11 | * |
| 12 | * @var Array |
| 13 | */ |
| 14 | private $cpts = []; |
| 15 | |
| 16 | /** |
| 17 | * Set all the custom post types that this source supports |
| 18 | * |
| 19 | * @param Array $cpts Array of custom post type names. |
| 20 | * @return void |
| 21 | */ |
| 22 | public function set_custom_post_types( array $cpts ) { |
| 23 | $this->cpts = $cpts; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Get the custom post type from the source |
| 28 | * |
| 29 | * @param String $post_type Source post type. |
| 30 | * @return null|Array |
| 31 | */ |
| 32 | private function get_post_type( $post_type ) { |
| 33 | $post_types = array_values( array_filter( Source_Manager::get_all_grouped(), function( $source ) { |
| 34 | return $source['name'] === 'posttype'; |
| 35 | } ) ); |
| 36 | |
| 37 | if ( count( $post_types ) === 1 ) { |
| 38 | foreach ( $post_types[0]['sources'] as $source ) { |
| 39 | if ( $source['name'] === $post_type ) { |
| 40 | return $source; |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return null; |
| 46 | } |
| 47 | |
| 48 | public function get_type( array $row = [] ) { |
| 49 | $post_type = isset( $row['post_type'] ) ? $this->get_post_type( $row['post_type'] ) : false; |
| 50 | if ( $post_type ) { |
| 51 | return $post_type['name']; |
| 52 | } |
| 53 | |
| 54 | // Handle post types when it's not registered |
| 55 | if ( isset( $row['post_type'] ) ) { |
| 56 | return $row['post_type']; |
| 57 | } |
| 58 | |
| 59 | return $this->source_type; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Return true if the source matches the type, false otherwise |
| 64 | * |
| 65 | * @param String $type Source type. |
| 66 | * @return boolean |
| 67 | */ |
| 68 | public function is_type( $type ) { |
| 69 | return in_array( $type, $this->cpts, true ); |
| 70 | } |
| 71 | |
| 72 | public function get_name( array $row = [] ) { |
| 73 | $post_type = isset( $row['post_type'] ) ? $this->get_post_type( $row['post_type'] ) : false; |
| 74 | if ( $post_type ) { |
| 75 | return $post_type['label']; |
| 76 | } |
| 77 | |
| 78 | // Handle post types when it's not registered |
| 79 | return isset( $row['post_type'] ) ? ucwords( $row['post_type'] ) : $this->source_name; |
| 80 | } |
| 81 | |
| 82 | public function get_supported_flags() { |
| 83 | return [ |
| 84 | 'post_guid' => __( 'Search GUID', 'search-regex' ), |
| 85 | 'post_draft' => __( 'Exclude drafts', 'search-regex' ), |
| 86 | ]; |
| 87 | } |
| 88 | |
| 89 | public function get_actions( Result $result ) { |
| 90 | $edit = get_edit_post_link( $result->get_row_id(), '' ); |
| 91 | $view = get_permalink( $result->get_row_id() ); |
| 92 | |
| 93 | if ( $edit ) { |
| 94 | return [ |
| 95 | 'edit' => $edit, |
| 96 | 'view' => $view, |
| 97 | ]; |
| 98 | } |
| 99 | |
| 100 | return []; |
| 101 | } |
| 102 | |
| 103 | public function get_search_conditions() { |
| 104 | $parts = []; |
| 105 | |
| 106 | // If searching a particular post type then just look there |
| 107 | if ( $this->source_type !== 'posts' ) { |
| 108 | $parts[] = implode( ' OR ', array_map( function( $cpt ) { |
| 109 | global $wpdb; |
| 110 | |
| 111 | return $wpdb->prepare( 'post_type=%s', $cpt ); |
| 112 | }, $this->cpts ) ); |
| 113 | } |
| 114 | |
| 115 | if ( $this->source_flags->has_flag( 'post_draft' ) ) { |
| 116 | $parts[] = "post_status != 'draft'"; |
| 117 | } |
| 118 | |
| 119 | return implode( ' AND ', $parts ); |
| 120 | } |
| 121 | |
| 122 | public function get_info_columns() { |
| 123 | return [ 'post_type' ]; |
| 124 | } |
| 125 | |
| 126 | public function get_column_label( $column, $data ) { |
| 127 | $labels = [ |
| 128 | 'post_content' => __( 'Content', 'search-regex' ), |
| 129 | 'post_excerpt' => __( 'Excerpt', 'search-regex' ), |
| 130 | 'post_title' => __( 'Title', 'search-regex' ), |
| 131 | 'post_name' => __( 'Slug', 'search-regex' ), |
| 132 | 'guid' => __( 'GUID', 'search-regex' ), |
| 133 | ]; |
| 134 | |
| 135 | if ( isset( $labels[ $column ] ) ) { |
| 136 | return $labels[ $column ]; |
| 137 | } |
| 138 | |
| 139 | return $column; |
| 140 | } |
| 141 | |
| 142 | public function get_columns() { |
| 143 | $columns = [ |
| 144 | 'post_content', |
| 145 | 'post_excerpt', |
| 146 | 'post_title', |
| 147 | 'post_name', |
| 148 | ]; |
| 149 | |
| 150 | if ( $this->source_flags->has_flag( 'post_guid' ) ) { |
| 151 | $columns[] = 'guid'; |
| 152 | } |
| 153 | |
| 154 | return $columns; |
| 155 | } |
| 156 | |
| 157 | public function get_title_column() { |
| 158 | return 'post_title'; |
| 159 | } |
| 160 | |
| 161 | public function get_table_id() { |
| 162 | return 'ID'; |
| 163 | } |
| 164 | |
| 165 | public function get_table_name() { |
| 166 | global $wpdb; |
| 167 | |
| 168 | return $wpdb->posts; |
| 169 | } |
| 170 | |
| 171 | public function save( $row_id, $column_id, $content ) { |
| 172 | // This does all the sanitization |
| 173 | $result = wp_update_post( [ |
| 174 | 'ID' => $row_id, |
| 175 | $column_id => $content, |
| 176 | ] ); |
| 177 | |
| 178 | if ( $result ) { |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | return new \WP_Error( 'searchregex', 'Failed to update post.' ); |
| 183 | } |
| 184 | |
| 185 | public function delete_row( $row_id ) { |
| 186 | if ( wp_delete_post( $row_id, true ) ) { |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | return new \WP_Error( 'searchregex_delete', 'Failed to delete post', 401 ); |
| 191 | } |
| 192 | } |
| 193 |