prefix . 'aieo_dmm_order_cancellations'; $cr = $wpdb->prefix . 'aieo_dmm_cancellation_reasons'; $out = array( 'scanned' => 0, 'updated' => 0, 'skipped_unclassified' => 0, 'skipped_already_set' => 0, 'by_instigator' => array(), 'by_frame' => array(), 'kind_fixed' => 0, ); if ((string) $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $oc)) !== $oc) { return $out; } $vocab = $wpdb->get_results( "SELECT slug, default_instigator, default_frame, default_event_kind FROM {$cr}", ARRAY_A ); $map = array(); foreach ((array) $vocab as $v) { $map[(string) $v['slug']] = array( 'instigator' => (string) $v['default_instigator'], 'frame' => (string) $v['default_frame'], 'kind' => ($v['default_event_kind'] === null || $v['default_event_kind'] === '') ? null : (int) $v['default_event_kind'], ); } $rows = $wpdb->get_results( "SELECT id, reason_slug, instigator, frame, event_kind, status_after FROM {$oc} ORDER BY id ASC", ARRAY_A ); foreach ((array) $rows as $r) { $out['scanned']++; $slug = (string) $r['reason_slug']; if ($slug === '' || !isset($map[$slug])) { $out['skipped_unclassified']++; continue; } $want_inst = $map[$slug]['instigator']; $want_frame = $map[$slug]['frame']; if ($want_inst === '' && $want_frame === '') { $out['skipped_unclassified']++; continue; } $set = array(); if ($want_inst !== '' && (string) $r['instigator'] === '') { $set['instigator'] = $want_inst; } if ($want_frame !== '' && (string) $r['frame'] === '') { $set['frame'] = $want_frame; } $vocab_kind = $map[$slug]['kind']; if ($vocab_kind === self::lost_kind() && (int) $r['event_kind'] !== self::lost_kind() && in_array((int) $r['event_kind'], array(0, 1), true)) { $set['event_kind'] = self::lost_kind(); if (!$dry_run) { $out['kind_fixed']++; } else { $out['kind_fixed']++; } } if (!$set) { $out['skipped_already_set']++; continue; } if (isset($set['instigator'])) { $out['by_instigator'][$set['instigator']] = (isset($out['by_instigator'][$set['instigator']]) ? $out['by_instigator'][$set['instigator']] : 0) + 1; } if (isset($set['frame'])) { $out['by_frame'][$set['frame']] = (isset($out['by_frame'][$set['frame']]) ? $out['by_frame'][$set['frame']] : 0) + 1; } if (!$dry_run) { $wpdb->update($oc, $set, array('id' => (int) $r['id'])); } $out['updated']++; } if (!$dry_run) { if (class_exists('AIEO_DMM_Cancellation_Reasons_Data') && method_exists('AIEO_DMM_Cancellation_Reasons_Data', 'flush_cache')) { AIEO_DMM_Cancellation_Reasons_Data::flush_cache(); } wp_cache_delete('aieo_cr_all', 'aieo_dmm'); } return $out; } private static function lost_kind() { return class_exists('AIEO_DMM_Order_Cancellations') ? AIEO_DMM_Order_Cancellations::KIND_LOST : 4; } }