Esempio n. 1
0
/**
 * Generate a number on a bell curbe.
 *
 * @link http://www.eboodevelopment.com/php-random-number-generator-with-normal-distribution-bell-curve/
 *
 * @param     $min
 * @param     $max
 * @param     $std_deviation
 * @param int $step
 *
 * @return float
 */
function itelic_purebell($min, $max, $std_deviation, $step = 1)
{
    $rand1 = (double) mt_rand() / (double) mt_getrandmax();
    $rand2 = (double) mt_rand() / (double) mt_getrandmax();
    $gaussian_number = sqrt(-2 * log($rand1)) * cos(2 * M_PI * $rand2);
    $mean = ($max + $min) / 2;
    $random_number = $gaussian_number * $std_deviation + $mean;
    $random_number = round($random_number / $step) * $step;
    if ($random_number < $min || $random_number > $max) {
        $random_number = itelic_purebell($min, $max, $std_deviation);
    }
    return $random_number;
}
 /**
  * Generate products.
  *
  * ## Options
  *
  * [--file=<file>]
  * : Specify which files to used. By default pulls random zips from DB.
  *
  * [--count=<count>]
  * : How many products to generate. Default: 15
  *
  * @param $args
  * @param $assoc_args
  */
 public function generate($args, $assoc_args)
 {
     $count = \WP_CLI\Utils\get_flag_value($assoc_args, 'count', 15);
     if (!\WP_CLI\Utils\get_flag_value($assoc_args, 'file')) {
         /**
          * @var \wpdb $wpdb
          */
         global $wpdb;
         $results = $wpdb->get_results($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type = 'attachment'\n\t\t\t\t AND post_mime_type = 'application/zip' LIMIT %d", $count));
         $files = array();
         foreach ($results as $result) {
             $files[] = $result->ID;
         }
     } else {
         if (get_post_type($assoc_args['file']) != 'attachment') {
             WP_CLI::error("Invalid file post type.");
         }
         $files = array($assoc_args['file']);
     }
     if (empty($files)) {
         WP_CLI::error('No files exist.');
     }
     $notify = \WP_CLI\Utils\make_progress_bar('Generating products.', $count);
     $limits = array('-', '2', '2', '5', '5', '10');
     for ($i = 0; $i < $count; $i++) {
         $title = $this->faker->catchPhrase . ' software';
         $price = itelic_purebell(44, 199, 45);
         $price = floatval(intval($price));
         $index = array_rand($files);
         $file = get_post($files[$index]);
         try {
             $file = itelic_rename_file($file, $title . '-1.0.zip');
         } catch (InvalidArgumentException $e) {
             WP_CLI::error($e->getMessage());
         }
         $params = array('description' => $this->faker->realText(), 'limit' => $limits[array_rand($limits)], 'file' => $file->ID);
         $recurring = array(array('interval' => 'month', 'count' => 1, 'frequency' => 10), array('interval' => 'month', 'count' => 6, 'frequency' => 20), array('interval' => 'none', 'frequency' => 30), array('interval' => 'year', 'count' => 1, 'frequency' => 100));
         $rand = rand(0, 100);
         foreach ($recurring as $option) {
             if ($rand <= $option['frequency']) {
                 if ($option['interval'] != 'none') {
                     $params['interval'] = $option['interval'];
                     $params['interval-count'] = $option['count'];
                 }
                 break;
             }
         }
         $result = $this->create_product($title, $price, $params);
         if (is_wp_error($result)) {
             WP_CLI::error($result);
         }
         $notify->tick();
     }
     $notify->finish();
 }