class-comment-meta.php
5 months ago
class-comment.php
5 months ago
class-meta.php
5 months ago
class-options.php
5 months ago
class-post-meta.php
5 months ago
class-post.php
5 months ago
class-terms.php
4 months ago
class-user-meta.php
5 months ago
class-user.php
5 months ago
class-meta.php
126 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex\Source\Core; |
| 4 | |
| 5 | use SearchRegex\Source; |
| 6 | use SearchRegex\Plugin; |
| 7 | |
| 8 | abstract class Meta extends Source\Source { |
| 9 | public function get_table_id() { |
| 10 | return 'meta_id'; |
| 11 | } |
| 12 | |
| 13 | public function get_title_column() { |
| 14 | return 'meta_key'; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Label for the meta-data |
| 19 | * |
| 20 | * @return string |
| 21 | */ |
| 22 | abstract public function get_meta_name(); |
| 23 | |
| 24 | /** |
| 25 | * Return the meta object ID name |
| 26 | * |
| 27 | * @return string |
| 28 | */ |
| 29 | abstract public function get_meta_object_id(); |
| 30 | |
| 31 | /** |
| 32 | * Return the meta table name |
| 33 | * |
| 34 | * @return string |
| 35 | */ |
| 36 | abstract public function get_meta_table(); |
| 37 | |
| 38 | public function save( $row_id, array $changes ) { |
| 39 | global $wpdb; |
| 40 | |
| 41 | $meta = $this->get_columns_to_change( $changes ); |
| 42 | |
| 43 | if ( count( $meta ) > 0 ) { |
| 44 | $this->log_save( 'meta', $meta ); |
| 45 | |
| 46 | // This does all the sanitization |
| 47 | $result = true; |
| 48 | |
| 49 | if ( Plugin\Settings::init()->can_save() ) { |
| 50 | $result = $wpdb->update( _get_meta_table( $this->get_meta_table() ), $meta, [ $this->get_table_id() => $row_id ] ); |
| 51 | if ( $result === false ) { |
| 52 | return new \WP_Error( 'searchregex', 'Failed to update meta data: ' . $this->get_meta_table() ); |
| 53 | } |
| 54 | |
| 55 | // Clear any cache |
| 56 | wp_cache_delete( $this->get_meta_object_id(), $this->get_meta_table() . '_meta' ); |
| 57 | } |
| 58 | |
| 59 | return $result; |
| 60 | } |
| 61 | |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | public function delete_row( $row_id ) { |
| 66 | global $wpdb; |
| 67 | |
| 68 | $this->log_save( 'delete meta', $row_id ); |
| 69 | |
| 70 | if ( Plugin\Settings::init()->can_save() ) { |
| 71 | $result = $wpdb->delete( $this->get_table_name(), [ $this->get_table_id() => $row_id ] ); |
| 72 | |
| 73 | if ( $result ) { |
| 74 | wp_cache_delete( $this->get_meta_object_id(), $this->get_meta_table() . '_meta' ); |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | return new \WP_Error( 'searchregex_delete', 'Failed to delete meta', 401 ); |
| 79 | } |
| 80 | |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | public function autocomplete( array $column, $value ) { |
| 85 | global $wpdb; |
| 86 | |
| 87 | if ( in_array( $column['column'], [ 'meta_key', 'meta_value' ], true ) ) { |
| 88 | // phpcs:ignore |
| 89 | return $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT " . $column['column'] . " as id," . $column['column'] . " as value FROM {$this->get_table_name()} WHERE " . $column['column'] . " LIKE %s LIMIT %d", '%' . $wpdb->esc_like( $value ) . '%', self::AUTOCOMPLETE_LIMIT ) ); |
| 90 | } |
| 91 | |
| 92 | return []; |
| 93 | } |
| 94 | |
| 95 | public function get_schema() { |
| 96 | return [ |
| 97 | 'name' => $this->get_meta_name(), |
| 98 | 'table' => $this->get_table_name(), |
| 99 | 'columns' => [ |
| 100 | [ |
| 101 | 'column' => $this->get_meta_object_id(), |
| 102 | 'type' => 'integer', |
| 103 | 'title' => __( 'Owner ID', 'search-regex' ), |
| 104 | 'options' => 'api', |
| 105 | 'joined_by' => $this->get_meta_table(), |
| 106 | ], |
| 107 | [ |
| 108 | 'column' => 'meta_key', |
| 109 | 'type' => 'string', |
| 110 | 'title' => __( 'Meta Key', 'search-regex' ), |
| 111 | 'options' => 'api', |
| 112 | 'global' => true, |
| 113 | ], |
| 114 | [ |
| 115 | 'column' => 'meta_value', |
| 116 | 'type' => 'string', |
| 117 | 'title' => __( 'Meta Value', 'search-regex' ), |
| 118 | 'options' => 'api', |
| 119 | 'multiline' => true, |
| 120 | 'global' => true, |
| 121 | ], |
| 122 | ], |
| 123 | ]; |
| 124 | } |
| 125 | } |
| 126 |