PluginProbe
Packeta / 2.1
Packeta v2.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Upgrade / Version_1_4_2.php

Version_1_4_2.php in Packeta 2.1, at src/Packetery/Module/Upgrade/Version_1_4_2.php

95 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Version_1_4_2
4 *
5 * @package Packetery\Upgrade
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Upgrade;
11
12 use Packetery\Module\Carrier\OptionPrefixer;
13 use Packetery\Module\Product;
14 use Packetery\Module\WpdbAdapter;
15
16 /**
17 * Class Version_1_4_2
18 *
19 * @package Packetery\Upgrade
20 */
21 class Version_1_4_2 {
22
23 /**
24 * WPDB.
25 *
26 * @var WpdbAdapter
27 */
28 private $wpdbAdapter;
29
30 /**
31 * Version_1_4_2 constructor.
32 *
33 * @param WpdbAdapter $wpdbAdapter WPDB.
34 */
35 public function __construct( WpdbAdapter $wpdbAdapter ) {
36 $this->wpdbAdapter = $wpdbAdapter;
37 }
38
39 /**
40 * Run migration.
41 *
42 * @return void
43 */
44 public function run() {
45 $this->deduplicateCarrierPrefix();
46 }
47
48 /**
49 * In version 1.4.1 has been bug, which stored names of disabled carriers witch duplicated prefix.
50 * This migration will replace carrier name prefix with correct format.
51 *
52 * @return void
53 */
54 private function deduplicateCarrierPrefix(): void {
55 $duplicatedCarrierPrefix = OptionPrefixer::CARRIER_OPTION_PREFIX . OptionPrefixer::CARRIER_OPTION_PREFIX;
56 $productIds = $this->getDisallowedCarriersProductsIds( $duplicatedCarrierPrefix );
57 foreach ( $productIds as $productId ) {
58 $oldMeta = get_post_meta( $productId, Product\Entity::META_DISALLOWED_SHIPPING_RATES, true );
59 if ( ! is_array( $oldMeta ) || count( $oldMeta ) === 0 ) {
60 continue;
61 }
62
63 $newMeta = [];
64 foreach ( $oldMeta as $optionId => $isDisallowed ) {
65 $optionId = str_replace( $duplicatedCarrierPrefix, OptionPrefixer::CARRIER_OPTION_PREFIX, $optionId );
66 $newMeta[ $optionId ] = $isDisallowed;
67 }
68
69 update_post_meta( $productId, Product\Entity::META_DISALLOWED_SHIPPING_RATES, $newMeta );
70 }
71 }
72
73 /**
74 * Get IDs of products, which have duplicate carrier prefix caused by bug in version 1.4.1.
75 *
76 * @param string $prefix Duplicated prefix.
77 *
78 * @return int[] Array of post IDs.
79 */
80 private function getDisallowedCarriersProductsIds( string $prefix ): array {
81 $productIds = $this->wpdbAdapter->get_col(
82 $this->wpdbAdapter->prepare(
83 'SELECT `post_id`
84 FROM `' . $this->wpdbAdapter->postmeta . '`
85 WHERE `meta_key` = %s
86 AND `meta_value` LIKE %s',
87 Product\Entity::META_DISALLOWED_SHIPPING_RATES,
88 '%' . $prefix . '%'
89 )
90 );
91
92 return array_map( 'intval', $productIds );
93 }
94 }
95