| @@ -1,18 +1,20 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace ImportWP\Common\Importer; |
| 4 | 4 | |
| 5 | +use ImportWP\Common\Importer\Exception\RecordUpdatedSkippedException; | |
| 6 | + | |
| 5 | 7 | class ParsedData |
| 6 | 8 | { |
| 7 | - private $id; | |
| 8 | - private $method; | |
| 9 | - private $data; | |
| 9 | + protected $id; | |
| 10 | + protected $method; | |
| 11 | + protected $data; | |
| 10 | 12 | |
| 11 | 13 | /** |
| 12 | 14 | * @var MapperInterface $mapper |
| 13 | 15 | */ |
| 14 | - private $mapper; | |
| 16 | + protected $mapper; | |
| 15 | 17 | |
| 16 | 18 | public function __construct(MapperInterface $mapper) |
| 17 | 19 | { |
| 18 | 20 | $this->mapper = $mapper; |
| @@ -123,13 +125,35 @@ | ||
| 123 | 125 | |
| 124 | 126 | do_action('iwp/importer/mapper/before', $this); |
| 125 | 127 | |
| 126 | 128 | if (false === $this->id) { |
| 129 | + | |
| 127 | 130 | do_action('iwp/importer/mapper/before_insert', $this); |
| 131 | + | |
| 132 | + // NOTE: has should be fetched before insert | |
| 133 | + $data_hash = $this->generate_hash(); | |
| 134 | + | |
| 128 | 135 | $this->id = $this->mapper->insert($this); |
| 136 | + $this->update_hash($data_hash); | |
| 129 | 137 | } else { |
| 138 | + | |
| 130 | 139 | do_action('iwp/importer/mapper/before_update', $this); |
| 131 | - $this->id = $this->mapper->update($this); | |
| 140 | + | |
| 141 | + // NOTE: has should be fetched before update | |
| 142 | + $hash_check = $this->generate_hash(); | |
| 143 | + | |
| 144 | + if (!$this->hash_compare($hash_check)) { | |
| 145 | + | |
| 146 | + $this->id = $this->mapper->update($this); | |
| 147 | + $this->update_hash($hash_check); | |
| 148 | + } else { | |
| 149 | + | |
| 150 | + // we need to log this result | |
| 151 | + $this->mapper->add_version_tag(); | |
| 152 | + $this->update_hash($hash_check); | |
| 153 | + throw new RecordUpdatedSkippedException("Data matches previous import"); | |
| 154 | + return; | |
| 155 | + } | |
| 132 | 156 | } |
| 133 | 157 | |
| 134 | 158 | do_action('iwp/importer/mapper/after', $this); |
| 135 | 159 | } |
| @@ -161,6 +185,39 @@ | ||
| 161 | 185 | |
| 162 | 186 | public function isUpdate() |
| 163 | 187 | { |
| 164 | 188 | return $this->method === 'UPDATE'; |
| 189 | + } | |
| 190 | + | |
| 191 | + public function generate_hash() | |
| 192 | + { | |
| 193 | + return $this->array_md5($this->data); | |
| 194 | + } | |
| 195 | + | |
| 196 | + function array_md5($array) | |
| 197 | + { | |
| 198 | + array_multisort($array); | |
| 199 | + return md5(json_encode(apply_filters('iwp/hash_compare', $array))); | |
| 200 | + } | |
| 201 | + | |
| 202 | + public function hash_compare($hash_b) | |
| 203 | + { | |
| 204 | + $hash_a = $this->get_last_hash(); | |
| 205 | + $hash_check_enabled = apply_filters('iwp/importer/mapper/hash_check_enabled', false); | |
| 206 | + | |
| 207 | + if ($hash_check_enabled && !empty($hash_a) && $hash_a === $hash_b) { | |
| 208 | + return true; | |
| 209 | + } | |
| 210 | + | |
| 211 | + return false; | |
| 212 | + } | |
| 213 | + | |
| 214 | + public function get_last_hash() | |
| 215 | + { | |
| 216 | + return $this->mapper->get_custom_field($this->id, '_iwp_hash', true); | |
| 217 | + } | |
| 218 | + | |
| 219 | + public function update_hash($hash) | |
| 220 | + { | |
| 221 | + $this->mapper->update_custom_field($this->id, '_iwp_hash', $hash); | |
| 165 | 222 | } |
| 166 | 223 | } |