-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_dummy_data.php
155 lines (116 loc) · 3.71 KB
/
generate_dummy_data.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/*
Plugin Name: Dummy Data
Description: Generates dummy data
Version: 0.1.2
Author: Daniel Dvorkin
Author URI: http://danieldvork.in
License: GPLv2 or later
*/
class bbpFauxData {
private $faker;
public function __construct() {
require_once 'lib/Faker/src/autoload.php';
$this->faker = Faker\Factory::create();
}
public function generate_user() {
if ( !function_exists( 'wp_hash_password' ) ) {
_doing_it_wrong( 'bbpFauxData->generate_user()', "This function should be called after plugins_loaded", '0.1' );
return false;
}
$first = $this->faker->firstName;
$last = $this->faker->lastName;
$user_data = array(
'user_pass' => 'password',
'user_login' => sanitize_user( strtolower( $first . '.' . $last ) ),
'display_name' => $first . ' ' . $last,
'first_name' => $first,
'last_name' => $last,
'user_email' => $this->faker->safeEmail,
);
$ret = wp_insert_user( $user_data );
unset( $user_data );
return $ret;
}
public function generate_forum() {
$date = $this->faker->dateTimeBetween( $startDate = '-4 years', $endDate = '-3 years' );
$forum = array(
'post_author' => $this->get_random_existing_user_id(),
'post_title' => ucwords( $this->faker->word ),
'post_date' => $date->format( 'Y-m-d H:i:s' ),
'post_content' => ucwords( $this->faker->sentence( $nbWords = 6, $variableNbWords = true ) ),
);
$meta = array(
'forum_type' => 'forum',
'status' => 'open',
);
$ret = bbp_insert_forum( $forum, $meta );
unset( $forum );
unset( $meta );
return $ret;
}
public function generate_topic() {
$forum = $this->get_random_existing_forum_id();
$date = $this->faker->dateTimeBetween( $startDate = '-3 years', $endDate = 'now' );
$text_size = $this->faker->numerify( $string = '###' );
if ( $text_size < 20 )
$text_size = 500;
$topic = array(
'post_parent' => $forum,
'post_author' => $this->get_random_existing_user_id(),
'post_content' => $this->faker->text( $maxNbChars = $text_size ),
'post_title' => $this->faker->sentence,
'post_date' => $date->format( 'Y-m-d H:i:s' ),
);
$meta = array(
'author_ip' => $this->faker->ipv4,
'forum_id' => $forum,
);
$ret = bbp_insert_topic( $topic, $meta );
unset( $topic );
unset( $meta );
return $ret;
}
public function generate_reply() {
$topic = $this->get_random_existing_topic_id();
$min_date = get_post_field( 'post_date', $topic );
$forum = bbp_get_topic_forum_id( $topic );
$date = $this->faker->dateTimeBetween( $startDate = $min_date, $endDate = 'now' );
$text_size = $this->faker->numerify( $string = '###' );
if ( $text_size < 20 )
$text_size = 500;
$reply = array(
'post_parent' => $topic,
'post_author' => $this->get_random_existing_user_id(),
'post_content' => $this->faker->text( $maxNbChars = $text_size ),
'post_date' => $date->format( 'Y-m-d H:i:s' ),
);
$meta = array(
'author_ip' => $this->faker->ipv4,
'forum_id' => $forum,
'topic_id' => $topic,
);
$ret = bbp_insert_reply( $reply, $meta );
unset( $reply );
unset( $meta );
return $ret;
}
public function get_random_existing_user_id() {
global $wpdb;
$sql = "SELECT id FROM $wpdb->users ORDER BY rand() LIMIT 1";
$id = $wpdb->get_var( $sql );
return $id;
}
public function get_random_existing_forum_id() {
global $wpdb;
$sql = "SELECT id FROM $wpdb->posts WHERE post_type='forum' ORDER BY rand() LIMIT 1";
$id = $wpdb->get_var( $sql );
return $id;
}
public function get_random_existing_topic_id() {
global $wpdb;
$sql = "SELECT id FROM $wpdb->posts WHERE post_type='topic' ORDER BY rand() LIMIT 1";
$id = $wpdb->get_var( $sql );
return $id;
}
}