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
meta.php
72 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex; |
| 4 | |
| 5 | use SearchRegex\Search_Source; |
| 6 | |
| 7 | abstract class Source_Meta extends Search_Source { |
| 8 | public function get_columns() { |
| 9 | $columns = [ |
| 10 | 'meta_key', |
| 11 | 'meta_value', |
| 12 | ]; |
| 13 | |
| 14 | return $columns; |
| 15 | } |
| 16 | |
| 17 | public function get_column_label( $column ) { |
| 18 | $labels = [ |
| 19 | 'meta_key' => __( 'Name', 'search-regex' ), |
| 20 | 'meta_value' => __( 'Value', 'search-regex' ), |
| 21 | ]; |
| 22 | |
| 23 | if ( isset( $labels[ $column ] ) ) { |
| 24 | return $labels[ $column ]; |
| 25 | } |
| 26 | |
| 27 | return $column; |
| 28 | } |
| 29 | |
| 30 | public function get_table_id() { |
| 31 | return 'meta_id'; |
| 32 | } |
| 33 | |
| 34 | public function get_title_column() { |
| 35 | return 'meta_key'; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Return the meta object ID name |
| 40 | * |
| 41 | * @return String |
| 42 | */ |
| 43 | abstract public function get_meta_object_id(); |
| 44 | |
| 45 | /** |
| 46 | * Return the meta table name |
| 47 | * |
| 48 | * @return String |
| 49 | */ |
| 50 | abstract public function get_meta_table(); |
| 51 | |
| 52 | public function save( $row_id, $column_id, $content ) { |
| 53 | global $wpdb; |
| 54 | |
| 55 | if ( $column_id === 'meta_key' ) { |
| 56 | $content = sanitize_key( $content ); |
| 57 | |
| 58 | return parent::save( $row_id, $column_id, strlen( $content ) === 0 ? $column_id : $content ); |
| 59 | } |
| 60 | |
| 61 | // Known values |
| 62 | // phpcs:ignore |
| 63 | $existing = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$this->get_table_name()} WHERE meta_id=%d", $row_id ), ARRAY_A ); |
| 64 | |
| 65 | if ( $existing && update_metadata( $this->get_meta_table(), $existing[ $this->get_meta_object_id() ], $existing['meta_key'], $content, $existing['meta_value'] ) ) { |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | return new \WP_Error( 'searchregex', 'Failed to update meta data: ' . $this->get_meta_table() ); |
| 70 | } |
| 71 | } |
| 72 |