PluginProbe ʕ •ᴥ•ʔ
Search Regex / 2.1
Search Regex v2.1
trunk 1.4.12 1.4.13 1.4.14 1.4.15 1.4.16 2.0 2.0.1 2.1 2.2 2.2.1 2.3 2.3.1 2.3.2 2.3.3 2.4 2.4.1 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.1 3.1.1 3.1.2 3.2 3.3 3.3.0 3.3.1 3.4 3.4.1 3.4.2
search-regex / source / core / post.php
search-regex / source / core Last commit date
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
186 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 ];
86 }
87
88 public function get_actions( Result $result ) {
89 $edit = get_edit_post_link( $result->get_row_id(), '' );
90 $view = get_permalink( $result->get_row_id() );
91
92 if ( $edit ) {
93 return [
94 'edit' => $edit,
95 'view' => $view,
96 ];
97 }
98
99 return [];
100 }
101
102 public function get_search_conditions() {
103 // If searching a particular post type then just look there
104 if ( $this->source_type !== 'posts' ) {
105 return implode( ' OR ', array_map( function( $cpt ) {
106 global $wpdb;
107
108 return $wpdb->prepare( 'post_type=%s', $cpt );
109 }, $this->cpts ) );
110 }
111
112 return '';
113 }
114
115 public function get_info_columns() {
116 return [ 'post_type' ];
117 }
118
119 public function get_column_label( $column ) {
120 $labels = [
121 'post_content' => __( 'Content', 'search-regex' ),
122 'post_excerpt' => __( 'Excerpt', 'search-regex' ),
123 'post_title' => __( 'Title', 'search-regex' ),
124 'post_name' => __( 'Slug', 'search-regex' ),
125 'guid' => __( 'GUID', 'search-regex' ),
126 ];
127
128 if ( isset( $labels[ $column ] ) ) {
129 return $labels[ $column ];
130 }
131
132 return $column;
133 }
134
135 public function get_columns() {
136 $columns = [
137 'post_content',
138 'post_excerpt',
139 'post_title',
140 'post_name',
141 ];
142
143 if ( $this->source_flags->has_flag( 'post_guid' ) ) {
144 $columns[] = 'guid';
145 }
146
147 return $columns;
148 }
149
150 public function get_title_column() {
151 return 'post_title';
152 }
153
154 public function get_table_id() {
155 return 'ID';
156 }
157
158 public function get_table_name() {
159 global $wpdb;
160
161 return $wpdb->posts;
162 }
163
164 public function save( $row_id, $column_id, $content ) {
165 // This does all the sanitization
166 $result = wp_update_post( [
167 'ID' => $row_id,
168 $column_id => $content,
169 ] );
170
171 if ( $result ) {
172 return true;
173 }
174
175 return new \WP_Error( 'searchregex', 'Failed to update post.' );
176 }
177
178 public function delete_row( $row_id ) {
179 if ( wp_delete_post( $row_id ) ) {
180 return true;
181 }
182
183 return new \WP_Error( 'searchregex_delete', 'Failed to delete post', 401 );
184 }
185 }
186