class-delete.php
6 months ago
class-export.php
1 week ago
class-global-replace.php
6 months ago
class-modify.php
6 months ago
class-nothing.php
6 months ago
class-run.php
1 week ago
class-modify.php
147 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex\Action\Type; |
| 4 | |
| 5 | use SearchRegex\Action; |
| 6 | use SearchRegex\Source; |
| 7 | use SearchRegex\Schema; |
| 8 | use SearchRegex\Modifier; |
| 9 | |
| 10 | /** |
| 11 | * Perform modification of columns |
| 12 | */ |
| 13 | class Modify extends Action\Action { |
| 14 | /** |
| 15 | * Columns that are to be modified |
| 16 | * |
| 17 | * @var Modifier\Modifier[] |
| 18 | */ |
| 19 | private array $columns = []; |
| 20 | |
| 21 | /** |
| 22 | * Dynamic shortcode handler |
| 23 | * |
| 24 | * @phpstan-ignore property.onlyWritten |
| 25 | */ |
| 26 | private ?Action\Dynamic_Column $dynamic_column = null; |
| 27 | |
| 28 | /** |
| 29 | * Constructor |
| 30 | * |
| 31 | * @param array<int, array<string, mixed>>|array<string, mixed>|string $options Options. |
| 32 | * @param Schema\Schema $schema Schema. |
| 33 | */ |
| 34 | public function __construct( $options, Schema\Schema $schema ) { |
| 35 | if ( is_array( $options ) ) { |
| 36 | foreach ( $options as $option ) { |
| 37 | $source_schema = $schema->get_for_source( $option['source'] ?? '' ); |
| 38 | |
| 39 | if ( $source_schema ) { |
| 40 | $column = Modifier\Modifier::create( $option, $source_schema ); |
| 41 | |
| 42 | if ( $column && $column->is_valid() ) { |
| 43 | $this->columns[] = $column; |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | if ( count( $this->columns ) > 0 ) { |
| 50 | $this->dynamic_column = new Action\Dynamic_Column(); |
| 51 | } |
| 52 | |
| 53 | parent::__construct( $options, $schema ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @return list<string> |
| 58 | */ |
| 59 | public function get_view_columns() { |
| 60 | $remember = function ( $return, $tag, $attr ) { |
| 61 | if ( $tag === 'column' ) { |
| 62 | if ( isset( $attr['name'] ) ) { |
| 63 | return 'column::' . $attr['name'] . ' '; |
| 64 | } elseif ( isset( $attr[0] ) ) { |
| 65 | return 'column::' . $attr[0] . ' '; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return ''; |
| 70 | }; |
| 71 | |
| 72 | add_filter( 'pre_do_shortcode_tag', $remember, 10, 3 ); |
| 73 | |
| 74 | $views = array_map( |
| 75 | function ( $column ) { |
| 76 | if ( ! $column instanceof Modifier\Value\String_Value ) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | $replace = $column->get_replace_value(); |
| 81 | if ( $replace === null ) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | if ( has_shortcode( $replace, 'column' ) ) { |
| 86 | $result = do_shortcode( $replace ); |
| 87 | |
| 88 | if ( preg_match_all( '/column::(.*?)\s/', $result, $matches ) > 0 ) { |
| 89 | foreach ( $matches[1] as $match ) { |
| 90 | return $column->get_schema()->get_source() . '__' . $match; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return false; |
| 96 | }, $this->columns |
| 97 | ); |
| 98 | |
| 99 | remove_filter( 'pre_do_shortcode_tag', $remember, 10 ); |
| 100 | |
| 101 | $modify = array_map( |
| 102 | fn( $column ) => $column->get_source_name() . '__' . $column->get_column_name(), |
| 103 | $this->columns |
| 104 | ); |
| 105 | |
| 106 | return array_values( array_filter( [ ...$views, ...$modify ] ) ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @return array<string, mixed> |
| 111 | */ |
| 112 | public function to_json() { |
| 113 | return [ |
| 114 | 'action' => 'modify', |
| 115 | 'actionOption' => array_map( |
| 116 | fn( $column ) => $column->to_json(), |
| 117 | $this->columns |
| 118 | ), |
| 119 | ]; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @param int $row_id |
| 124 | * @param array<string, mixed> $row |
| 125 | * @param Source\Source $source |
| 126 | * @param array<\SearchRegex\Search\Column> $columns |
| 127 | * @return array<\SearchRegex\Search\Column> |
| 128 | */ |
| 129 | public function perform( $row_id, array $row, Source\Source $source, array $columns ) { |
| 130 | foreach ( $columns as $pos => $column ) { |
| 131 | foreach ( $this->columns as $action_column ) { |
| 132 | if ( $source->is_type( $action_column->get_source_name() ) && $action_column->is_for_column( $column->get_column_id() ) ) { |
| 133 | $value = $action_column->get_row_data( $row ); |
| 134 | |
| 135 | if ( $value ) { |
| 136 | $columns[ $pos ] = $action_column->perform( $row_id, $value, $source, $column, $row, $this->should_save() ); |
| 137 | } |
| 138 | |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return $columns; |
| 145 | } |
| 146 | } |
| 147 |