Hi,
I need help to run my custom function just before a custom post is gonna be edited using the Cred Post edit form. We have a post type called "client" and we are fetching the posts data from an API. So currently below custom function triggers and insert or update all posts when a user logs in to website. as soon as the user login, API Function automatically updates/insert all posts data of the api into existing client posts or insert new posts to database if new posts found in API.
So we are actually looking to run this function, not only during the login, but in case user is already logged in and he/she closes the site but after some times when he opens the website homepage then that api function should trigger again. And Also when user wants to edit any post using the edit post link, then as soon as he clicks in Edit Post link, before displaying the post edit form, this function should run again to update the post data and then display in the edit form.
So I am wondering if toolset team has any action hook that can execute the below function. its an urgent task. So if any of you can respond, it would be great.
add_action( 'update_client_list', 'get_clients_from_api' );
add_action( 'wp_ajax_nopriv_get_clients_from_api', 'get_clients_from_api' );
add_action( 'wp_ajax_get_clients_from_api', 'get_clients_from_api' );
function get_clients_from_api() {
$current_page = ( ! empty( $_POST['current_page'] ) ) ? $_POST['current_page'] : 1;
$clients = [];
// Should return an array of objects
$resultsJson = wp_remote_retrieve_body( wp_remote_get('<em><u>hidden link</u></em>') );
// turn it into a PHP array from JSON string
$results = json_decode( $resultsJson );
// Either the API is down or something else spooky happened. Just be done.
if( ! is_array( $results ) || empty( $results ) ){
return false;
}
global $wpdb;
foreach( $results as $client ){
// $client_slug = slugify( $client["post_title"] );
$rdata = $wpdb->get_results( "select post_id, meta_key from $wpdb->postmeta where meta_value = '".$client->{"wpcf-patient-id"}."'", ARRAY_A );
//$existing_client = get_page_by_path( $client_slug, 'OBJECT', 'client' );
$existing_client=null;
if(is_array($rdata) && count($rdata)>0){
$existing_client=$rdata[0]['post_id'];
}
if( $existing_client == null ){
$inserted_client = wp_insert_post( [
'post_name' => $client->post_title,
'post_title' => $client->post_title,
'post_type' => 'client',
'post_status' => 'pending'
] );
if( is_wp_error( $inserted_client ) || $inserted_client === 0 ) {
// die('Could not insert client: ' . $client_slug);
// error_log( 'Could not insert client: ' . $client_slug );
continue;
}
foreach( $client as $key => $value ) {
// update_field( $key, $client->$name, $inserted_client );
add_post_meta($inserted_client,$key,$value);
}
} else {
$existing_client_id = $existing_client;
$exisiting_client_timestamp = get_post_meta($existing_client_id,'updated_at',true);
$d1 = new DateTime($client->updated_at);
$d2 = new DateTime($exisiting_client_timestamp);
if( $d1 >= $d2){
foreach( $client as $key => $value ){
// update_field( $name, $client->$name, $existing_client_id);
update_post_meta($existing_client_id,$key,$value);
}
}
}
}
$current_page = $current_page + 1;
wp_remote_post( admin_url('admin-ajax.php?action=get_clients_from_api'), [
'blocking' => false,
'sslverify' => false, // we are sending this to ourselves, so trust it.
'body' => [
'current_page' => $current_page
]
] );
}
WP_CLI::add_command('jst-update-data', 'get_clients_from_api');