core
4 months ago
class-autocomplete.php
6 months ago
class-convert-values.php
5 months ago
class-has-meta.php
5 months ago
class-has-terms.php
5 months ago
class-manager.php
5 months ago
class-source.php
5 months ago
class-has-meta.php
100 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex\Source; |
| 4 | |
| 5 | use SearchRegex\Context\Type; |
| 6 | use SearchRegex\Plugin; |
| 7 | |
| 8 | /** |
| 9 | * Provides meta table support to sources |
| 10 | * |
| 11 | * @phpstan-type MetaItem array{key: string, value: string, value_type: string} |
| 12 | * @phpstan-type MetaData array{column: string, items: list<MetaItem>} |
| 13 | */ |
| 14 | trait Has_Meta { |
| 15 | /** |
| 16 | * Process meta data in any updates |
| 17 | * |
| 18 | * @param int $row_id Row ID. |
| 19 | * @param string $type Type of meta data. |
| 20 | * @param array<string, mixed> $updates Array of updates. |
| 21 | * @return void |
| 22 | */ |
| 23 | protected function process_meta( $row_id, $type, array $updates ) { |
| 24 | foreach ( $updates as $column => $update ) { |
| 25 | if ( $column === 'meta' ) { |
| 26 | $this->set_meta( $row_id, $type, $update['change'] ); |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Get meta data |
| 33 | * |
| 34 | * @param array<string, array<mixed>> $meta Meta data. |
| 35 | * @return MetaData |
| 36 | */ |
| 37 | private function get_meta( array $meta ) { |
| 38 | $items = array_map( |
| 39 | function ( $item ) use ( $meta ) { |
| 40 | $value = new Type\Value( $meta[ $item ][0] ); |
| 41 | |
| 42 | return [ |
| 43 | 'key' => $item, |
| 44 | 'value' => $meta[ $item ][0], |
| 45 | 'value_type' => $value->get_value_type(), |
| 46 | ]; |
| 47 | }, array_keys( $meta ) |
| 48 | ); |
| 49 | |
| 50 | return [ |
| 51 | 'column' => 'meta', |
| 52 | 'items' => $items, |
| 53 | ]; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Set meta data |
| 58 | * |
| 59 | * @param int $row_id Row ID. |
| 60 | * @param string $meta_type Type of meta data. |
| 61 | * @param array<mixed> $update Array of updates. |
| 62 | * @return void |
| 63 | */ |
| 64 | private function set_meta( $row_id, $meta_type, $update ) { |
| 65 | foreach ( $update as $meta ) { |
| 66 | $key = $meta->get_key(); |
| 67 | $value = $meta->get_value(); |
| 68 | |
| 69 | // Delete a meta if we are changing the key, or deleting it |
| 70 | if ( $key->get_type() === Type\Replace::TYPE_REPLACE || $key->get_type() === Type\Delete::TYPE_DELETE ) { |
| 71 | $this->log_save( $meta_type . ' meta delete', (string) $row_id . ' = ' . $key->get_value() . ' = ' . $value->get_value() ); |
| 72 | |
| 73 | if ( Plugin\Settings::init()->can_save() ) { |
| 74 | delete_metadata( $meta_type, $row_id, $key->get_value(), $value->get_value() ); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Update the meta if we are changing the key, or replacing the value |
| 79 | if ( $value->get_type() === Type\Replace::TYPE_REPLACE || $key->get_type() === Type\Replace::TYPE_REPLACE ) { |
| 80 | $key_value = $key->get_type() === Type\Replace::TYPE_REPLACE ? $key->get_replacement() : $key->get_value(); |
| 81 | $value_value = $value->get_type() === Type\Replace::TYPE_REPLACE ? $value->get_replacement() : $value->get_value(); |
| 82 | |
| 83 | $this->log_save( $meta_type . ' meta update', (string) $row_id . ' = ' . $key_value . ' => ' . $value_value ); |
| 84 | |
| 85 | if ( Plugin\Settings::init()->can_save() ) { |
| 86 | update_metadata( $meta_type, $row_id, $key_value, $value_value ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if ( $key->get_type() === Type\Add::TYPE_ADD ) { |
| 91 | $this->log_save( $meta_type . ' meta add', (string) $row_id . ' = ' . $key->get_value() . ' => ' . $value->get_value() ); |
| 92 | |
| 93 | if ( Plugin\Settings::init()->can_save() ) { |
| 94 | update_metadata( $meta_type, $row_id, $key->get_value(), $value->get_value() ); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 |