|
5 | 5 | use Cocur\Slugify\Slugify; |
6 | 6 | use Cbx\Careertoolkit\Factories\Factory; |
7 | 7 | use Faker\Factory as FakerFactory; |
| 8 | +use WP_User_Query; |
8 | 9 |
|
9 | 10 | /** |
10 | 11 | * Dummy job data generate command |
@@ -33,67 +34,96 @@ public function run( $args, $assoc_args ) { |
33 | 34 | $start = microtime( true ); |
34 | 35 |
|
35 | 36 | $total = isset( $assoc_args['total'] ) && intval( $assoc_args['total'] ) ? intval( $assoc_args['total'] ) : 20; |
36 | | - $status = isset( $assoc_args['status'] ) ? sanitize_text_field( $assoc_args['status'] ) : 'publish'; |
| 37 | + $status = isset( $assoc_args['status'] ) ? sanitize_text_field( $assoc_args['status'] ) : 'published'; |
37 | 38 | $is_remote = isset( $assoc_args['is-remote'] ) && intval( $assoc_args['is-remote'] ) ? intval( $assoc_args['is-remote'] ) : 0; |
38 | 39 | $is_featured = isset( $assoc_args['is-featured'] ) && intval( $assoc_args['is-featured'] ) ? intval( $assoc_args['is-featured'] ) : 1; |
39 | 40 | $is_filled = isset( $assoc_args['is-filled'] ) && intval( $assoc_args['is-filled'] ) ? intval( $assoc_args['is-filled'] ) : 0; |
40 | 41 |
|
41 | 42 | $salary_currency = isset( $assoc_args['currency'] ) ? sanitize_text_field( $assoc_args['currency'] ) : 'USD'; |
42 | 43 | $salary_unit = isset( $assoc_args['salary-unit'] ) ? sanitize_text_field( $assoc_args['salary-unit'] ) : 'monthly'; |
43 | 44 |
|
44 | | - $user_id = isset( $assoc_args['user-id'] ) && intval( $assoc_args['user-id'] ) ? intval( $assoc_args['user-id'] ) : 1; |
45 | | - |
46 | | - for ( $i = 0; $i < $total; $i ++ ) { |
47 | | - |
48 | | - $user_data = get_userdata( $user_id ); |
49 | | - |
50 | | - $cbxjob['add_by'] = $user_id; //user id = X |
51 | | - $cbxjob['add_date'] = date( 'Y-m-d H:i:s' ); |
52 | | - //$cbxjob['email'] = FakerFactory::create()->email(); |
53 | | - $cbxjob['email'] = $user_data->user_email; |
54 | | - $cbxjob['title'] = FakerFactory::create()->jobTitle(); |
55 | | - $cbxjob['status'] = $status; |
56 | | - $cbxjob['job_location'] = FakerFactory::create()->address(); |
57 | | - $cbxjob['is_featured'] = $is_featured; |
58 | | - $cbxjob['is_filled'] = $is_filled; |
59 | | - $cbxjob['salary_amount'] = FakerFactory::create()->randomNumber( 3 ); |
60 | | - $cbxjob['is_remote'] = $is_remote; |
61 | | - $cbxjob['application_url'] = FakerFactory::create()->url(); |
62 | | - $cbxjob['description'] = FakerFactory::create()->text(); |
63 | | - $cbxjob['open_positions'] = FakerFactory::create()->randomNumber( 1 ); |
64 | | - |
65 | | - $cbxjob['misc'] = [ |
66 | | - 'salary_currency' => $salary_currency, |
67 | | - 'job_location' => FakerFactory::create()->address(), |
68 | | - 'company_name' => FakerFactory::create()->company(), |
69 | | - 'salary_unit' => $salary_unit, |
70 | | - 'company_website' => FakerFactory::create()->url(), |
71 | | - 'company_logo' => '', |
72 | | - 'company_logo_source' => 'job', |
73 | | - 'company_logo_url' => FakerFactory::create()->imageUrl( 360, 360, 'company', true ), |
74 | | - ]; |
75 | | - |
76 | | - |
77 | | - //$cbxjob['mod_by'] = $job->post_author; |
78 | | - $cbxjob['mod_date'] = date( 'Y-m-d H:i:s' ); |
79 | | - $cbxjob['closing_date'] = date( 'Y-m-d H:i:s', strtotime( '+7 days' ) ); |
80 | | - $cbxjob['expiry_date'] = date( 'Y-m-d H:i:s', strtotime( '+7 days' ) ); |
81 | | - |
82 | | - $slugify = new Slugify(); |
83 | | - |
84 | | - $existing_slugs = \Cbx\Job\Models\CBXJob::query()->pluck( 'slug' )->toArray(); |
85 | | - $temp_slug = $slugify->slugify( $cbxjob['title'] ); |
86 | | - $slug = \Cbx\Job\Helpers\CBXJobHelpers::generate_unique_slug( $temp_slug, $existing_slugs ); |
87 | | - |
88 | | - $cbxjob['slug'] = $slug; |
89 | | - |
90 | | - \Cbx\Job\Models\CBXJob::query()->create( $cbxjob ); |
| 45 | + $user_id = isset( $assoc_args['user-id'] ) && intval( $assoc_args['user-id'] ) ? intval( $assoc_args['user-id'] ) : 0; |
| 46 | + |
| 47 | + if ( $user_id == 0 ) { |
| 48 | + // Query for the first user with the Administrator role |
| 49 | + $user_query = new WP_User_Query( [ |
| 50 | + 'role' => 'Administrator', |
| 51 | + 'orderby' => 'ID', |
| 52 | + 'order' => 'ASC', |
| 53 | + 'number' => 1, |
| 54 | + ] ); |
| 55 | + |
| 56 | + // Get the results |
| 57 | + $users = $user_query->get_results(); |
| 58 | + |
| 59 | + //write_log($users); |
| 60 | + |
| 61 | + // Check if any users are found and output the first user's ID |
| 62 | + if ( ! empty( $users ) ) { |
| 63 | + $user_id = $users[0]->ID; |
| 64 | + |
| 65 | + } |
91 | 66 | } |
| 67 | + |
| 68 | + |
| 69 | + if($user_id > 0){ |
| 70 | + for ( $i = 0; $i < $total; $i ++ ) { |
| 71 | + $user_data = get_userdata( $user_id ); |
| 72 | + |
| 73 | + $job = []; |
| 74 | + $job['add_by'] = $user_id; |
| 75 | + $job['owner'] = $user_id; |
| 76 | + $job['add_date'] = date( 'Y-m-d H:i:s' ); |
| 77 | + //$job['email'] = FakerFactory::create()->email(); |
| 78 | + $job['email'] = $user_data->user_email; |
| 79 | + $job['title'] = FakerFactory::create()->jobTitle(); |
| 80 | + $job['status'] = $status; |
| 81 | + $job['job_location'] = FakerFactory::create()->address(); |
| 82 | + $job['is_featured'] = $is_featured; |
| 83 | + $job['is_filled'] = $is_filled; |
| 84 | + $job['salary_amount'] = FakerFactory::create()->randomNumber( 3 ); |
| 85 | + $job['is_remote'] = $is_remote; |
| 86 | + $job['application_url'] = FakerFactory::create()->url(); |
| 87 | + $job['description'] = FakerFactory::create()->text(); |
| 88 | + $job['open_positions'] = FakerFactory::create()->randomNumber( 1 ); |
| 89 | + |
| 90 | + $job['misc'] = [ |
| 91 | + 'salary_currency' => $salary_currency, |
| 92 | + 'job_location' => FakerFactory::create()->address(), |
| 93 | + 'company_name' => FakerFactory::create()->company(), |
| 94 | + 'salary_unit' => $salary_unit, |
| 95 | + 'company_website' => FakerFactory::create()->url(), |
| 96 | + 'company_logo' => '', |
| 97 | + 'company_logo_source' => 'job', |
| 98 | + 'company_logo_url' => FakerFactory::create()->imageUrl( 360, 360, 'company', true ), |
| 99 | + ]; |
| 100 | + |
| 101 | + |
| 102 | + //$job['mod_by'] = $job->post_author; |
| 103 | + $job['mod_date'] = date( 'Y-m-d H:i:s' ); |
| 104 | + $job['closing_date'] = date( 'Y-m-d H:i:s', strtotime( '+7 days' ) ); |
| 105 | + $job['expiry_date'] = date( 'Y-m-d H:i:s', strtotime( '+7 days' ) ); |
| 106 | + |
| 107 | + $slugify = new Slugify(); |
| 108 | + |
| 109 | + $existing_slugs = \Cbx\Job\Models\CBXJob::query()->pluck( 'slug' )->toArray(); |
| 110 | + $temp_slug = $slugify->slugify( $job['title'] ); |
| 111 | + $slug = \Cbx\Job\Helpers\CBXJobHelpers::generate_unique_slug( $temp_slug, $existing_slugs ); |
| 112 | + |
| 113 | + $job['slug'] = $slug; |
| 114 | + |
| 115 | + \Cbx\Job\Models\CBXJob::query()->create( $job ); |
| 116 | + } |
| 117 | + } |
| 118 | + else{ |
| 119 | + $i = 0; |
| 120 | + } |
| 121 | + |
92 | 122 | $end = microtime( true ); |
93 | 123 |
|
94 | 124 | $elapsed = $end - $start; |
95 | 125 |
|
96 | | - \WP_CLI::success( "Successfully $total dummy job added. Execution time $elapsed seconds" );//todo: translation missing |
| 126 | + \WP_CLI::success( "Successfully $i dummy job added. Execution time: $elapsed seconds" );//todo: translation missing |
97 | 127 |
|
98 | 128 | } //end method run |
99 | 129 | } //end class DummyJobGenerate |
0 commit comments