<commit_before><?php
  use League\Csv\Reader;
  use Qaribou\Collection\ImmArray;

  $reader = Reader::createFromPath('_doc/vendors.csv');
  $results = ImmArray::fromArray($reader->fetchAll());
  /*
   prodid
   prodname
   active
   omit
   needsupdate
   needsupdatese
   updated
   MP
   address
   city
   state
   zip
   island
   phone1
   phone2
   fax_pre
   fax
   website
   email
   certified
   country
   estyr
   products
   products_info
   notes1
   notes2
   employ
   annual_volume
   exporter
   export_sales
   title1
   salutation1
   salutation2
   */
  global $wpdb;
  $str =
    $results
      ->map(function($row) use($wpdb) {
        // die(var_dump(acf_get_field_group(5)));
        $fields = ImmArray::fromArray(acf_get_fields(5));
        // die(var_dump($fields->map(function($field){ return $field['name']; })));
        $mapping = [
          'active' => 3,
          'omit' => 4,
          'address' => 7,
          'city' => 8,
          'state' => 9,
          'zip' => 10,
          'island' => 11,
          'phone1' => 12,
          'phone2' => 13,
          'fax_pre' => 14,
          'fax' => 15,
          'website' => 16,
          'certified' => 18,
          'country' => 19,
          'established_year' => 20,
        ];

        $id = intval($row[0]);
        if(empty($row[17])) {
          return;
        }

        $nicename = sanitize_key($row[1]);

        $args = [
          'ID' => $id,
          'user_nicename' => substr($nicename, 0, 50),
          'user_login' => substr($nicename, 0, 60),
          'user_url' => $row[16],
          'user_email' => $row[17],
          'display_name' => $row[1],
          'user_status' => 0,
          'user_activation_key' => '',
          'user_registered' => date("Y-m-d H:i:s"),
          'user_pass' => ''
        ];
        $arg_format = [
            '%d',
            '%s',
            '%s',
            '%s',
            '%s',
            '%s',
            '%d',
            '%s',
            '%s'
          ];
        $user = new WP_User($id);
        if(!$user->ID) {
          $res = $wpdb->insert('wp_users', $args, $arg_format);
        } else {
          // shift off id
          array_shift($args);
          array_shift($arg_format);

          $res = $wpdb->update('wp_users', $args, ['ID' => $id], $arg_format);
        }
        if(!$res) {
          die(var_dump($args));
        }
        // $fields->map(function($field) use($id, $row, $mapping) {
        //   $index = $mapping[$field['name']];
        //   update_user_meta($id, $field['name'], $row[$index]);
        //   update_user_meta($id, '_' . $field['name'], $field['key']);
        // });
        return $row[1];
      });
    echo $str->join('<br>');

?><commit_msg>Assigning user => product relationships<commit_after><?php
use League\Csv\Reader;
use Qaribou\Collection\ImmArray;

$reader = Reader::createFromPath('_doc/vendors.csv');
$results = ImmArray::fromArray($reader->fetchAll());
$beginning_index = 62;

$mapping = [
  "ffruit_bananas",
  "ffruit_citrus",
  "ffruit_papaya",
  "ffruit_avocado",
  "ffruit_exotictrop",
  "ffruit_pineapple",
  "ffruit_other",
  "ffruit_other",
  "fruitpuree_papaya",
  "fruitpuree_guava",
  "fruitpuree_other",
  "fruitpuree_otherdesc",
  "fveg_cabbages",
  "fveg_otherleafy",
  "fveg_ginger",
  "fveg_onions",
  "fveg_taro",
  "fveg_otherroot",
  "fveg_herbs",
  "fveg_other",
  "fveg_otherdesc",
  "macnut_plainroasted",
  "macnut_candiedchoco",
  "macnut_other",
  "macnut_otherdesc",
  "bsfood_cookies",
  "bsfood_crackers",
  "bsfood_candies",
  "bsfood_chips",
  "bsfood_driedfruits",
  "bsfood_crackseed",
  "bsfood_other",
  "bsfood_otherdesc",
  "cond_sauces",
  "cond_syrups",
  "cond_spreads",
  "cond_jamsjellies",
  "cond_dressing",
  "cond_seasonmix",
  "cond_soups",
  "cond_presfruits",
  "cond_pickveg",
  "cond_other",
  "cond_otherdesc",
  "coffee_bigislandexkona",
  "coffee_kona",
  "coffee_molokai",
  "coffee_maui",
  "coffee_kauai",
  "coffee_oahu",
  "bev_tea",
  "bev_drinkmix",
  "bev_concentrates",
  "bev_juices",
  "bev_alcoholic",
  "bev_other",
  "bev_otherdesc",
  "meat_beef",
  "meat_pork",
  "meat_poultry",
  "meat_driedsmoke",
  "meat_other",
  "meat_otherdesc",
  "dairy_icecream",
  "dairy",
  "milk",
  "dairy_other",
  "dairy_otherdesc",
  "oseafood_fresh",
  "oseafood_frozen",
  "oseafood_processed",
  "oseafood_other",
  "oseafood_other",
  "aqua_fresh",
  "aqua_frozen",
  "aqua_processed",
  "aqua_other",
  "aqua_otherdesc",
  "health_prod",
  "health_proddesc",
  "mfg_other",
  "mfg_otherdesc",
  "floral_anthuriums",
  "floral_anthuriumsdesc",
  "floral_other",
  "floral_otherdesc",
  "floral_foliage",
  "floral_foliagedesc",
  "floral_leis",
  "floral_leisdesc",
  "orch_dendrobiums",
  "orch_oncidium",
  "orch_vandas",
  "orch_phalaenopsis",
  "orch_cymbidium",
  "orch_other",
  "orch_otherdesc",
  "trop_heloncias",
  "trop_birdparadise",
  "trop_gingers",
  "trop_other",
  "trop_otherdesc",
  "protea_kings",
  "protea_queens",
  "protea_banksia",
  "protea_minks",
  "protea_pincushions",
  "protea_duchess",
  "protea_other",
  "protea_otherdesc",
  "potplant_dracaena",
  "potplant_palms",
  "potplant_bromeliads",
  "potplant_ferns",
  "potplant_ﬁcus",
  "potplant_succulents",
  "potplant_bonsai",
  "potplant_other",
  "potplant_otherdesc",
  "potorch_denbrobiums",
  "potorch_phalaenopsis",
  "potorch_cattleya",
  "potorch_oncidium",
  "potorch_hybrids",
  "potorch_species",
  "potorch_other",
  "potorch_otherdesc",
  "potflower_chrysanthemums",
  "potflower_poiniettias",
  "potflower_gesneriads",
  "potflower_bulbs",
  "potflower_anthuriums",
  "potflower_hibiscus",
  "potflower_bromeliads",
  "potflower_other",
  "potflower_otherdesc",
  "landplants_palms",
  "landplants_shrubs",
  "landplants_bedding",
  "landplants_groundcover",
  "landplants_specimentree",
  "landplants_vines",
  "landplants_lowerplants",
  "Iandplants_lowertrees",
  "landplants_other",
  "landplants_otherdesc",
  "propag_seeds",
  "propag_cuttings",
  "propag_liners",
  "propag_plugs",
  "propag_micropropagative",
  "propag_other",
  "propag_otherdesc",
];

foreach($results as $row) {
  $id = intval($row[0]);
  $products = [];
  for ($i=0; $i < 152; $i++) {
    $has_product = boolval($row[62 + $i]);
    if($has_product === TRUE) {
      $prod = $mapping[$i];
      $parts = preg_split('/_/', $prod);
      if($parts[1] == 'otherdesc') {
        continue;
      }
      $single_products = get_posts([
        'post_type' => 'product',
        's' => $parts[1],
        'tax_query' => [
          [
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => $parts[0]
          ]
        ]
      ]);
      $products = array_merge($products, $single_products);
    }

  }
  $product_ids = array_map(function($p) { return $p->ID; }, $products);
  echo $id;
  print_r($product_ids);
  echo "<br/><br/>";
  update_user_meta($id, "products", $product_ids);
  update_user_meta($id, "_products", "field_57c687c497a14");
}
?>
{'fraction_non_alphanumeric': 0.16875355719977234, 'fraction_numerical': 0.010671599317017643, 'mean_word_length': 2.89202657807309, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6612512', 'n_tokens_mistral': 2903, 'n_tokens_neox': 2776, 'n_words': 387}