-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathplugin.php
147 lines (122 loc) · 4.02 KB
/
plugin.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
<?php
/**
* Plugin Name: WP ERP Dummy Data Generator
* Description: Generates dummy data for your ERP installtaion
* Plugin URI: http://wperp.com
* Author: Tareq Hasan
* Author URI: http://tareq.co
* Version: 1.0
* License: GPL2
*/
// don't call the file directly
if ( !defined( 'ABSPATH' ) ) exit;
require_once 'vendor/autoload.php';
/**
* The plugin
*/
class WeDevs_ERP_Seeder {
private $faker;
private $employee_count;
private $customer_count;
/**
* Constructor
*/
public function __construct() {
$this->employee_count = 20;
$this->customer_count = 20;
$this->faker = Faker\Factory::create();
$this->faker->addProvider( new Faker\Provider\en_US\Person($this->faker ) );
$this->faker->addProvider( new Faker\Provider\Internet( $this->faker ) );
$this->faker->addProvider( new Faker\Provider\Base( $this->faker ) );
$this->faker->addProvider( new Faker\Provider\DateTime( $this->faker ) );
$this->faker->addProvider( new Faker\Provider\en_US\Address( $this->faker ) );
$this->faker->addProvider( new Faker\Provider\en_US\PhoneNumber( $this->faker ) );
$this->faker->addProvider( new Faker\Provider\Miscellaneous( $this->faker ) );
$this->file_includes();
add_action( 'erp_tools_page', [ $this, 'add_settings_area' ] );
add_action( 'admin_init', [ $this, 'submit_tools_page' ] );
}
/**
* Initializes the WeDevs_ERP_Seeder() class
*
* Checks for an existing WeDevs_ERP_Seeder() instance
* and if it doesn't find one, creates it.
*/
public static function init() {
static $instance = false;
if ( ! $instance ) {
$instance = new self();
}
return $instance;
}
/**
* Seeder files
*
* @return void
*/
public function file_includes() {
include_once __DIR__ . '/includes/class-hr-seeder.php';
include_once __DIR__ . '/includes/class-crm-seeder.php';
if ( defined( 'WP_CLI' ) && WP_CLI ) {
include_once __DIR__ . '/includes/cli/class-commands.php';
}
}
/**
* Tools page
*/
public function add_settings_area() {
include __DIR__ . '/includes/tools-page.php';
}
/**
* Run the seeder
*
* @return void
*/
function submit_tools_page() {
if ( isset( $_POST['erp_generate_dummy_data'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'erp-dummy-data-nonce' ) ) {
$this->employee_count = isset( $_POST['employee_number'] ) ? intval( $_POST['employee_number'] ) : 0;
$this->customer_count = isset( $_POST['customer_number'] ) ? intval( $_POST['customer_number'] ) : 0;
$this->generate_data();
echo "<h1>Done!</h1>";
printf( '<a href="%s">View Employees</a> OR ', admin_url( 'admin.php?page=erp-hr§ion=employee' ) );
printf( '<a href="%s">View Customers</a>', admin_url( 'admin.php?page=erp-crm§ion=contacts' ) );
die();
}
}
/**
* Run the seeder from cli
*
* @param string $type
* @param int $number
*
* @return void
*/
public function run_seeder_from_cli( $type, $number ) {
$this->employee_count = 0;
$this->customer_count = 0;
if ( $type == 'employee' ) {
$this->employee_count = $number;
}
if ( in_array( strtolower( $type ), ['customer', 'contact', 'company'] ) ) {
$this->customer_count = $number;
}
$this->generate_data();
}
/**
* Instantiate the seeder classes
*
* @return void
*/
function generate_data() {
if ( $this->employee_count > 0 ) {
(new WeDevs_ERP_HR_Seeder( $this->faker, $this->employee_count ))->run();
}
if ( $this->customer_count > 0 ) {
(new WeDevs_ERP_CRM_Seeder( $this->faker, $this->customer_count ))->run();
}
}
}
function erp_seeder() {
return WeDevs_ERP_Seeder::init();
}
erp_seeder();