PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.5.3
JetFormBuilder — Dynamic Blocks Form Builder v3.6.5.3
3.6.5.4 3.6.5.3 3.6.5.2 3.6.5.1 3.6.5 3.6.4.2 3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 All 132 releases
jetformbuilder / modules / cli / commands / seed-payments.php

seed-payments.php in JetFormBuilder — Dynamic Blocks Form Builder 3.6.5.3, at modules/cli/commands/seed-payments.php

140 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 namespace JFB_Modules\Cli\Commands;
5
6 // If this file is called directly, abort.
7 if ( ! defined( 'WPINC' ) ) {
8 die;
9 }
10
11 use JFB_Modules\Form_Record\Models\Record_Model;
12 use JFB_Modules\Gateways\Base_Gateway;
13 use JFB_Modules\Gateways\Db_Models\Payer_Model;
14 use JFB_Modules\Gateways\Db_Models\Payer_Shipping_Model;
15 use JFB_Modules\Gateways\Db_Models\Payment_Model;
16 use JFB_Modules\Gateways\Db_Models\Payment_To_Payer_Shipping_Model;
17 use JFB_Modules\Gateways\Db_Models\Payment_To_Record;
18 use JFB_Modules\Cli\Cli_Tools;
19
20 class Seed_Payments implements Base_Command_It {
21
22 public function rep_item_id() {
23 return 'seed-payments';
24 }
25
26 public function condition(): bool {
27 return true;
28 }
29
30 /**
31 * @param $args
32 * @param $assoc_args
33 *
34 * @throws \WP_CLI\ExitException
35 */
36 public function do_command( $args, $assoc_args ) {
37 $form = Cli_Tools::resolve_form( $assoc_args );
38 $limit = absint( $assoc_args['limit'] ?? 1000000 );
39
40 if ( 0 >= $limit ) {
41 $limit = 1;
42 }
43
44 /** @var \WP_User $user */
45 list( $user ) = get_users(
46 array(
47 'number' => 1,
48 )
49 );
50
51 $record_id = 0;
52 try {
53 $record_id = $this->get_record_id();
54 } catch ( \Exception $exception ) {
55 \WP_CLI::error( $exception->getMessage() );
56 }
57
58 $current = 0;
59
60 foreach ( range( 0, $limit ) as $current ) {
61 $payment_id = ( new Payment_Model() )->insert_soft(
62 array(
63 'transaction_id' => wp_unique_id( 'cli-' ),
64 'form_id' => $form->ID,
65 'user_id' => $user->ID,
66 'gateway_id' => 'paypal',
67 'scenario' => 'PAY_NOW',
68 'amount_value' => random_int( 1, 500 ),
69 'amount_code' => 'USD',
70 'type' => Base_Gateway::PAYMENT_TYPE_INITIAL,
71 'status' => 'COMPLETED',
72 )
73 );
74
75 ( new Payment_To_Record() )->insert_soft(
76 array(
77 'payment_id' => $payment_id,
78 'record_id' => $record_id,
79 )
80 );
81
82 $payer_id = ( new Payer_Model() )->insert_soft(
83 array(
84 'user_id' => $user->ID,
85 'payer_id' => wp_unique_id( 'payer-' ),
86 'first_name' => 'John',
87 'last_name' => 'Doe',
88 'email' => '[email protected]',
89 )
90 );
91
92 $shipping_id = ( new Payer_Shipping_Model() )->insert_soft(
93 array(
94 'payer_id' => $payer_id,
95 'full_name' => 'John Doe',
96 'address_line_1' => '1 Main St',
97 'admin_area_2' => 'San Jose',
98 'admin_area_1' => 'CA',
99 'postal_code' => 95131,
100 'country_code' => 'US',
101 )
102 );
103
104 ( new Payment_To_Payer_Shipping_Model() )->insert_soft(
105 array(
106 'payment_id' => $payment_id,
107 'payer_shipping_id' => $shipping_id,
108 )
109 );
110
111 if ( 0 === $current % 5000 ) {
112 \WP_CLI::log( 'Reached: ' . $current );
113 }
114 }
115
116 \WP_CLI::success( "Executed successfully ($current)" );
117 }
118
119 /**
120 * @return int
121 * @throws \Exception
122 */
123 protected function get_record_id(): int {
124 global $wpdb;
125
126 // phpcs:disable WordPress.DB
127 $record = $wpdb->get_row(
128 $wpdb->prepare( 'SELECT MAX(id) as max, MIN(id) as min FROM %i;', Record_Model::table() )
129 );
130 // phpcs:enable WordPress.DB
131
132 if ( empty( $record ) ) {
133 throw new \Exception( 'empty_records_table' );
134 }
135
136 return random_int( $record->min, $record->max );
137 }
138
139 }
140