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
meta.php
86 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, $data ) { |
| 18 | if ( $column === 'meta_key' ) { |
| 19 | return __( 'Name', 'search-regex' ); |
| 20 | } |
| 21 | |
| 22 | if ( $column === 'meta_value' ) { |
| 23 | if ( is_serialized( $data ) ) { |
| 24 | return __( 'Serialized Value', 'search-regex' ); |
| 25 | } |
| 26 | |
| 27 | return __( 'Value', 'search-regex' ); |
| 28 | } |
| 29 | |
| 30 | return $column; |
| 31 | } |
| 32 | |
| 33 | public function get_table_id() { |
| 34 | return 'meta_id'; |
| 35 | } |
| 36 | |
| 37 | public function get_title_column() { |
| 38 | return 'meta_key'; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Return the meta object ID name |
| 43 | * |
| 44 | * @return String |
| 45 | */ |
| 46 | abstract public function get_meta_object_id(); |
| 47 | |
| 48 | /** |
| 49 | * Return the meta table name |
| 50 | * |
| 51 | * @return String |
| 52 | */ |
| 53 | abstract public function get_meta_table(); |
| 54 | |
| 55 | public function save( $row_id, $column_id, $content ) { |
| 56 | global $wpdb; |
| 57 | |
| 58 | if ( $column_id === 'meta_key' ) { |
| 59 | $content = sanitize_key( $content ); |
| 60 | |
| 61 | return parent::save( $row_id, $column_id, strlen( $content ) === 0 ? $column_id : $content ); |
| 62 | } |
| 63 | |
| 64 | // Known values |
| 65 | // phpcs:ignore |
| 66 | $existing = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$this->get_table_name()} WHERE meta_id=%d", $row_id ), ARRAY_A ); |
| 67 | |
| 68 | if ( $existing ) { |
| 69 | // Serialized data. Until we properly support this then just raw-update the value and leave it to the user to ensure it's correct |
| 70 | $meta_table = $wpdb->prefix . $this->get_meta_table() . 'meta'; |
| 71 | |
| 72 | if ( is_serialized( $existing['meta_value'] ) && $wpdb->update( $meta_table, [ 'meta_value' => $content ], [ 'meta_id' => $row_id ] ) ) { |
| 73 | wp_cache_delete( $existing[ $this->get_meta_object_id() ], $this->get_meta_table() . '_meta' ); |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | // Unserialized data. Update as usual |
| 78 | if ( update_metadata( $this->get_meta_table(), $existing[ $this->get_meta_object_id() ], $existing['meta_key'], $content, $existing['meta_value'] ) ) { |
| 79 | return true; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return new \WP_Error( 'searchregex', 'Failed to update meta data: ' . $this->get_meta_table() ); |
| 84 | } |
| 85 | } |
| 86 |