CleanMultipleSlashesOnDB.php
2 years ago
RemoveDuplicateMeta.php
2 years ago
UpdateDonationLevelsSchema.php
2 years ago
CleanMultipleSlashesOnDB.php
79 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\Migrations; |
| 4 | |
| 5 | use Give\DonationForms\ValueObjects\DonationFormMetaKeys; |
| 6 | use Give\Framework\Database\DB; |
| 7 | use Give\Framework\Migrations\Contracts\Migration; |
| 8 | |
| 9 | /** |
| 10 | * @since 3.2.0 |
| 11 | */ |
| 12 | class CleanMultipleSlashesOnDB extends Migration |
| 13 | { |
| 14 | /** |
| 15 | * @since 3.2.0 |
| 16 | */ |
| 17 | public function run() |
| 18 | { |
| 19 | $formMetaTable = DB::prefix('give_formmeta'); |
| 20 | |
| 21 | /** |
| 22 | * For some reason, these 24 backslashes used in the SQL instruction became 12 when we passed it to the $sql var, |
| 23 | * and became 6 when it runs on the database, which represents a sequence of only 3 backslashes as we need 2 of |
| 24 | * them to escape just 1 as you can check here: https://dev.mysql.com/doc/refman/8.0/en/string-literals.html |
| 25 | */ |
| 26 | $sql = DB::prepare("SELECT * FROM $formMetaTable as fm |
| 27 | WHERE fm.meta_key = 'formBuilderSettings' AND fm.meta_value LIKE '%\\\\\\\\\\\\\\\\\\\\\\\\%'"); |
| 28 | |
| 29 | $items = DB::get_results($sql); |
| 30 | |
| 31 | foreach ($items as $item) { |
| 32 | $settings = $this->cleanMultipleSlashes(json_decode($item->meta_value, true, JSON_UNESCAPED_SLASHES)); |
| 33 | |
| 34 | DB::table('give_formmeta') |
| 35 | ->where('form_id', $item->form_id) |
| 36 | ->where('meta_key', DonationFormMetaKeys::SETTINGS()->getValue()) |
| 37 | ->update([ |
| 38 | 'meta_value' => json_encode($settings), |
| 39 | ]); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @inheritdoc |
| 45 | */ |
| 46 | public static function id() |
| 47 | { |
| 48 | return 'donation-forms-clean-multiple-slashes-on-db'; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @inheritdoc |
| 53 | */ |
| 54 | public static function title() |
| 55 | { |
| 56 | return 'Clean multiple slashes in the formBuilderSettings meta value'; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @inheritdoc |
| 61 | */ |
| 62 | public static function timestamp() |
| 63 | { |
| 64 | return strtotime('2023-20-11'); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @since 3.2.0 |
| 69 | */ |
| 70 | public function cleanMultipleSlashes($var) |
| 71 | { |
| 72 | if (is_array($var)) { |
| 73 | return array_map([$this, 'cleanMultipleSlashes'], $var); |
| 74 | } else { |
| 75 | return is_string($var) ? deslash($var) : $var; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 |