PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.8.1
GiveWP – Donation Plugin and Fundraising Platform v4.16.8.1
4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 All 254 releases
give / src / FormMigration / Concerns / Blocks / BlockDifference.php

BlockDifference.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.8.1, at src/FormMigration/Concerns/Blocks/BlockDifference.php

70 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\FormMigration\Concerns\Blocks;
4
5 use Give\Framework\Blocks\BlockCollection;
6 use Give\Framework\Blocks\BlockModel;
7
8 class BlockDifference
9 {
10 protected $blockLookup;
11
12 protected $skip = [];
13
14 protected $blockAddedCallback;
15
16 protected $blockDifferenceCallback;
17
18 public function __construct(BlockCollection $collection) {
19 $collection->walk(function(BlockModel $block) {
20 $this->blockLookup[$block->clientId] = $block;
21 });
22 }
23
24 public function skip(...$blockNames): self
25 {
26 $this->skip = array_merge($this->skip, $blockNames);
27 return $this;
28 }
29
30 public function onBlockAdded(callable $callback): self
31 {
32 $this->blockAddedCallback = $callback;
33 return $this;
34 }
35
36 public function onBlockDifference(callable $callback): self
37 {
38 $this->blockDifferenceCallback = $callback;
39 return $this;
40 }
41
42 public function diff(BlockCollection $collection)
43 {
44 $collection->walk(function(BlockModel $block) {
45 if(in_array($block->name, $this->skip)) return;
46
47
48 if(!isset($this->blockLookup[$block->clientId])) {
49 call_user_func($this->blockAddedCallback, $block);
50 return;
51 }
52
53 $differences = [];
54 foreach($block->getAttributes() as $key => $value) {
55 $previousValue = $this->blockLookup[$block->clientId]->getAttributes()[$key] ?? null;
56 if($previousValue !== $value) {
57 $differences[$key] = [
58 'previous' => $previousValue,
59 'current' => $value,
60 ];
61 }
62 }
63
64 if($differences) {
65 call_user_func($this->blockDifferenceCallback, $block, $differences);
66 }
67 });
68 }
69 }
70