Skip to content

Instantly share code, notes, and snippets.

@davevsdave
Created July 18, 2022 15:07
Show Gist options
  • Save davevsdave/8f3fa10ae0af6dea40b0ec37fdc28bf3 to your computer and use it in GitHub Desktop.
Save davevsdave/8f3fa10ae0af6dea40b0ec37fdc28bf3 to your computer and use it in GitHub Desktop.
function wp_update_job_post($data, $post_id) {
//setup your application username and application password here
$username = '[your_user_name]'; // site username
$application_password = '[your_application_password]';
$i = 0;
//update html content for compliance items
if(!empty($data['compliance'])):
foreach($data['compliance'] as $item):
$description_raw = $item['description'];
$description_update = htmlspecialchars_decode($description_raw);
$data['compliance'][$i]['description'] = $description_update;
$i++;
endforeach;
endif;
$i = 0;
//update html content for questions items
if(!empty($data['questions'])):
foreach($data['questions'] as $item):
$description_raw = $item['description'];
$description_update = htmlspecialchars_decode($description_raw);
$data['questions'][$i]['description'] = $description_update;
$i++;
endforeach;
endif;
$i = 0;
//update html content for demographic_questions items
if(!empty($data['demographic_questions'])):
foreach($data['demographic_questions'] as $item):
$description_raw = $item['description'];
$description_update = htmlspecialchars_decode($description_raw);
$data['demographic_questions'][$i]['description'] = $description_update;
$i++;
endforeach;
endif;
//setup taxonomy
$departments = $data['departments'];
$department_names = [];
if(!empty($departments)):
foreach($departments as $item):
$department_id = $item['id'];
$department_name = $item['name'];
array_push($department_names, $department_name);
endforeach;
endif;
//set up data
$title = strval($data['id']);
$updated_at = $data['updated_at'];
$acf = [];
$acf['absolute_url'] = urldecode($data['absolute_url']);
$acf['internal_job_id'] = $data['internal_job_id'];
$acf['location_name'] = $data['location']['name'];
$acf['data_compliance_type'] = $data['data_compliance'][0]['type'];
$acf['data_compliance_requires_consent'] = $data['data_compliance'][0]['requires_consent'];
$acf['data_compliance_retention_period'] = $data['data_compliance'][0]['retention_period'];
$acf['id'] = strval($data['id']);
$acf['updated_at'] = $data['updated_at'];
$acf['requisition_id'] = $data['requisition_id'];
$acf['title'] = $data['title'];
$acf['content'] = htmlspecialchars_decode($data['content']);
$acf['departments'] = $departments;
$acf['offices'] = $data['offices'];
if(!empty($data['compliance'])):
$acf['compliance'] = $data['compliance'];
endif;
if(!empty($data['demographic_questions'])):
$acf['demographic_questions'] = $data['demographic_questions'];
endif;
if(!empty($data['questions'])):
$acf['questions'] = $data['questions'];
endif;
try {
$url = 'https://example.com/wp-json/wp/v2/jobs/'.$post_id;
$the_title = $data['title'];
$slug = sanitize_title_with_dashes($the_title, null, 'save');
$json = json_encode([
'title' => $title,
'acf' => $acf,
'slug' => $slug,
'date' => $updated_at,
'status' => 'publish',
'meta' => $meta,
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$application_password);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$result = json_decode($result, true);
$post_id = intval($result['id']);
//set the Department for this post
$set_terms = wp_set_object_terms( $post_id, $department_names, 'careers_departments', true );
//set up the meta title and description for this post
$meta_description = strip_tags($data['content']);
$meta_title = $data['title'] . ' | Careers | Company Name';
global $wpdb;
$row_exists = $wpdb->get_var(
$wpdb->prepare("SELECT COUNT(*) AS total FROM wp_aioseo_posts WHERE post_id = %d",
array($post_id)
)
);
if($row_exists):
$update_aioseo_meta = $wpdb->query(
$wpdb->prepare("UPDATE wp_aioseo_posts SET title = %s, description = %s WHERE post_id = {$post_id}", array($meta_title, $meta_description))
);
else:
$wpdb->query(
$wpdb->prepare("insert into wp_aioseo_posts (title, description, post_id) values (%s, %s, %d)", $meta_title, $meta_description, $post_id)
);
endif;
} catch(Exception $e) {
error_log($e->getMessage(), true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment