Skip to content

Commit cf61742

Browse files
committed
if no user id is passed then first admin role used id is used
1 parent 97b68f8 commit cf61742

File tree

3 files changed

+163
-104
lines changed

3 files changed

+163
-104
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ This plugin provides command line interface for creating dummy job and resume.
1818
### All possible default params:
1919

2020
```
21-
wp cbxjob-generate --total=100 --status=published --is-remote=0 --is-featured=1 --is-filled=0 --user-id=1 --salary-unit=monthly --currency=USD
21+
wp cbxjob-generate --total=100 --status=published --is-remote=0 --is-featured=1 --is-filled=0 --salary-unit=monthly --currency=USD
2222
```
2323

2424
total = number of jobs to be created.
@@ -42,7 +42,7 @@ user-id = user ID
4242
### All possible default params:
4343

4444
```
45-
wp cbxresume-generate --total=100 --user-id=1 --status=published --privacy=public --is-primary=1
45+
wp cbxresume-generate --total=100 --status=published --privacy=public --is-primary=0
4646
```
4747

4848
total = number of resume to be created.

includes/Factories/Job/DummyJobGenerate.php

Lines changed: 79 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Cocur\Slugify\Slugify;
66
use Cbx\Careertoolkit\Factories\Factory;
77
use Faker\Factory as FakerFactory;
8+
use WP_User_Query;
89

910
/**
1011
* Dummy job data generate command
@@ -33,67 +34,96 @@ public function run( $args, $assoc_args ) {
3334
$start = microtime( true );
3435

3536
$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';
3738
$is_remote = isset( $assoc_args['is-remote'] ) && intval( $assoc_args['is-remote'] ) ? intval( $assoc_args['is-remote'] ) : 0;
3839
$is_featured = isset( $assoc_args['is-featured'] ) && intval( $assoc_args['is-featured'] ) ? intval( $assoc_args['is-featured'] ) : 1;
3940
$is_filled = isset( $assoc_args['is-filled'] ) && intval( $assoc_args['is-filled'] ) ? intval( $assoc_args['is-filled'] ) : 0;
4041

4142
$salary_currency = isset( $assoc_args['currency'] ) ? sanitize_text_field( $assoc_args['currency'] ) : 'USD';
4243
$salary_unit = isset( $assoc_args['salary-unit'] ) ? sanitize_text_field( $assoc_args['salary-unit'] ) : 'monthly';
4344

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+
}
9166
}
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+
92122
$end = microtime( true );
93123

94124
$elapsed = $end - $start;
95125

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
97127

98128
} //end method run
99129
} //end class DummyJobGenerate

0 commit comments

Comments
 (0)