| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\DonationForms\Migrations; |
| 4 |
|
| 5 |
use Give\DonationForms\Repositories\DonationFormRepository; |
| 6 |
use Give\Framework\Migrations\Contracts\Migration; |
| 7 |
|
| 8 |
/** |
| 9 |
* Update the donation levels schema to support descriptions |
| 10 |
* |
| 11 |
* @since 3.12.0 |
| 12 |
*/ |
| 13 |
class UpdateDonationLevelsSchema extends Migration |
| 14 |
{ |
| 15 |
/** |
| 16 |
* @inheritdoc |
| 17 |
*/ |
| 18 |
public static function id() |
| 19 |
{ |
| 20 |
return 'donation-forms-donation-levels-schema'; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* @inheritdoc |
| 25 |
*/ |
| 26 |
public static function title() |
| 27 |
{ |
| 28 |
return 'Update Donation Levels schema to support descriptions'; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @inheritdoc |
| 33 |
*/ |
| 34 |
public static function timestamp() |
| 35 |
{ |
| 36 |
return strtotime('2024-04-22'); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @since 3.12.0 |
| 41 |
*/ |
| 42 |
public function run() |
| 43 |
{ |
| 44 |
$forms = give(DonationFormRepository::class) |
| 45 |
->prepareQuery() |
| 46 |
->whereIsNotNull("give_formmeta_attach_meta_fields.meta_value") |
| 47 |
->getAll(); |
| 48 |
|
| 49 |
if ( ! $forms) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
foreach ($forms as $form) { |
| 54 |
$amountBlock = $form->blocks->findByName('givewp/donation-amount'); |
| 55 |
|
| 56 |
if ( ! $amountBlock) { |
| 57 |
continue; |
| 58 |
} |
| 59 |
|
| 60 |
$blockAttributes = $amountBlock->getAttributes(); |
| 61 |
$levels = $blockAttributes["levels"]; |
| 62 |
$defaultLevel = $blockAttributes["defaultLevel"] ?? 0; |
| 63 |
|
| 64 |
if ( ! is_array($levels) || empty($levels) || isset($levels[0]['value'])) { |
| 65 |
continue; |
| 66 |
} |
| 67 |
|
| 68 |
$levels = array_map(function($level) use ($defaultLevel) { |
| 69 |
return [ |
| 70 |
'value' => $level, |
| 71 |
'label' => '', |
| 72 |
'checked' => $level === $defaultLevel, |
| 73 |
]; |
| 74 |
}, $levels); |
| 75 |
|
| 76 |
$amountBlock->setAttribute('levels', $levels); |
| 77 |
$form->save(); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|