Uname: Linux webm012.cluster130.gra.hosting.ovh.net 5.15.167-ovh-vps-grsec-zfs-classid #1 SMP Tue Sep 17 08:14:20 UTC 2024 x86_64
Software: Apache
PHP version: 8.0.30 [ PHP INFO ] PHP os: Linux
Server Ip: 145.239.37.162
Your Ip: 216.73.216.190
User: dreampi (1009562) | Group: users (100)
Safe Mode: OFF
Disable Function:
_dyuweyrj4,_dyuweyrj4r,dl

name : Migration_20240322_110443_App.php
<?php declare(strict_types = 1);

namespace MailPoet\Migrations\App;

if (!defined('ABSPATH')) exit;


use MailPoet\Entities\StatisticsWooCommercePurchaseEntity;
use MailPoet\Migrator\AppMigration;
use MailPoet\WooCommerce\Helper;
use MailPoetVendor\Doctrine\DBAL\ArrayParameterType;

class Migration_20240322_110443_App extends AppMigration {
  public function run(): void {
    $wooCommerceHelper = $this->container->get(Helper::class);

    // If Woo is not active and the table doesn't exist, we can skip this migration
    if (!$wooCommerceHelper->isWooCommerceActive()) {
      return;
    }

    $purchaseStatisticsTable = $this->getTableName(StatisticsWooCommercePurchaseEntity::class);
    $purchaseStatistics = $this->entityManager->getConnection()->fetchAllAssociative("
      SELECT order_id
      FROM {$purchaseStatisticsTable}
    ");

    global $wpdb;
    if ($wooCommerceHelper->isWooCommerceCustomOrdersTableEnabled()) {
      $ordersTable = $wooCommerceHelper->getOrdersTableName();
      $query = "
        SELECT id AS order_id, status AS status
        FROM `{$ordersTable}`
        WHERE type = 'shop_order' AND id in (:orderIds)
        ";
    } else {
      $query = "
        SELECT wpp.id AS order_id, wpp.post_status AS status
        FROM `{$wpdb->posts}` wpp
        WHERE wpp.post_type = 'shop_order'
        AND wpp.ID in (:orderIds)
      ";
    }

    foreach (array_chunk($purchaseStatistics, 2) as $chunk) {
      $orderIds = array_column($chunk, 'order_id');

      /** @var array{order_id: int, status: string}[] $orders */
      $orders = $this->entityManager->getConnection()->executeQuery(
        $query,
        ['orderIds' => $orderIds],
        ['orderIds' => ArrayParameterType::INTEGER],
      )->fetchAllAssociative();

      foreach ($orders as $order) {
        $this->entityManager->getConnection()->executeStatement("
          UPDATE {$purchaseStatisticsTable}
          SET status = :status
          WHERE order_id = :orderId
        ", [
          'orderId' => $order['order_id'],
          'status' => str_replace('wc-', '', $order['status']), // WC order status in DB is prefixed with 'wc-'
        ]);
      }
    }
  }

  /**
   * @param class-string $entityClassName
   */
  private function getTableName(string $entityClassName): string {
    return $this->entityManager->getClassMetadata($entityClassName)->getTableName();
  }
}
© 2026 GrazzMean-Shell