| 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 Jet_Form_Builder\Blocks\Block_Helper; |
| 12 |
use JFB_Modules\Form_Record\Models\Record_Action_Result_Model; |
| 13 |
use JFB_Modules\Form_Record\Models\Record_Field_Model; |
| 14 |
use JFB_Modules\Form_Record\Models\Record_Model; |
| 15 |
use JFB_Modules\Security\Csrf\Csrf_Tools; |
| 16 |
use JFB_Modules\Cli\Cli_Tools; |
| 17 |
|
| 18 |
class Seed_Records implements Base_Command_It { |
| 19 |
|
| 20 |
public function rep_item_id() { |
| 21 |
return 'seed-records'; |
| 22 |
} |
| 23 |
|
| 24 |
public function condition(): bool { |
| 25 |
return true; |
| 26 |
} |
| 27 |
|
| 28 |
public function do_command( $args, $assoc_args ) { |
| 29 |
$form = Cli_Tools::resolve_form( $assoc_args ); |
| 30 |
$limit = absint( $assoc_args['limit'] ?? 1000000 ); |
| 31 |
|
| 32 |
if ( 0 >= $limit ) { |
| 33 |
$limit = 1; |
| 34 |
} |
| 35 |
|
| 36 |
$field_names = array_keys( |
| 37 |
Block_Helper::get_form_field_names( |
| 38 |
Block_Helper::get_blocks_by_post( $form ) |
| 39 |
) |
| 40 |
); |
| 41 |
|
| 42 |
/** @var \WP_User $user */ |
| 43 |
list( $user ) = get_users( |
| 44 |
array( |
| 45 |
'number' => 1, |
| 46 |
) |
| 47 |
); |
| 48 |
|
| 49 |
/** @var \WP_Post $post */ |
| 50 |
list( $post ) = get_posts( |
| 51 |
array( |
| 52 |
'numberposts' => 1, |
| 53 |
) |
| 54 |
); |
| 55 |
|
| 56 |
$current = 0; |
| 57 |
|
| 58 |
foreach ( range( 0, $limit ) as $current ) { |
| 59 |
$record_id = ( new Record_Model() )->insert_soft( |
| 60 |
array( |
| 61 |
'user_id' => $user->ID, |
| 62 |
'form_id' => $form->ID, |
| 63 |
'from_content_id' => $post->ID, |
| 64 |
'from_content_type' => 'cli', |
| 65 |
'status' => 'failed', |
| 66 |
) |
| 67 |
); |
| 68 |
|
| 69 |
foreach ( range( 0, 2 ) as $next ) { |
| 70 |
( new Record_Action_Result_Model() )->insert_soft( |
| 71 |
array( |
| 72 |
'record_id' => $record_id, |
| 73 |
'action_slug' => 'insert_post', |
| 74 |
'action_id' => 9999, |
| 75 |
'status' => 'failed', |
| 76 |
'on_event' => 'GENERATED.CLI', |
| 77 |
) |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
foreach ( $field_names as $field_name ) { |
| 82 |
( new Record_Field_Model() )->insert_soft( |
| 83 |
array( |
| 84 |
'record_id' => $record_id, |
| 85 |
'field_name' => $field_name, |
| 86 |
'field_value' => Csrf_Tools::generate(), |
| 87 |
'field_type' => 'text-field', |
| 88 |
'field_attrs' => '{}', |
| 89 |
) |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
if ( 0 === $current % 5000 ) { |
| 94 |
\WP_CLI::line( 'Reached: ' . $current ); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
\WP_CLI::success( "Executed successfully ($current)" ); |
| 99 |
} |
| 100 |
|
| 101 |
} |
| 102 |
|