create an integration with crm through its api to save the property information in custom post types in wordpress and be able to display these in the frontend, this custom post type has custom fields created with advanced custom fields, one of the fields that I created it is from the gallery in which I want to save the photos of the property, the problem is that the photos that come to me in url format through the crm api, are not saved in the acf pro gallery field, everything else is saved fine but not the images, the api gives me an array with the urls of the images.
Here is my code:
//Permitimos que la funcion se pueda activar mediante ajax
add_action( 'wp_ajax_nopriv_get_properties_from_api', 'get_properties_from_api' );
add_action( 'wp_ajax_get_properties_from_api', 'get_properties_from_api' );
function get_properties_from_api(){
if ( current_user_can( 'manage_options' ) ) {
$current_page = ( ! empty( $_POST['current_page'] ) ) ? $_POST['current_page'] : 1;
$properties = [];
//Clave de autorizacion de la api
$args = array(
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer 56c24c3c83f44b4ca3bc7e7a1c807177'
));
//Recogiendo datos de la api
$results = wp_remote_retrieve_body(wp_remote_get('https://witei.com/api/v1/houses/?page=' . $current_page . '&per_page=1', $args));
// Convertimos los resultados en una matriz de PHP
$results = json_decode( $results );
$properties[] = $results->results;
// Recorremos el array de propiedades
foreach($properties[0] as $property){
// Creamos variables para Titulo, slug y direccion
$property_title = $property->kind . ' - ' . $property->street . ' - ' . $property->town;
$property_title = ucwords($property_title);
$property_slug = sanitize_title($property->kind . ' en ' . $property->street . ', ' . $property->town);
$property_direction = $property->street . ' ' . $property->street_number . ', ' . $property->zip_code . ', ' . $property->town . ', ' . $property->province;
// Comprobaremos si la propiedad se va a vender o rentar
$precio;
$property_renting = $property->renting;
$property_selling = $property->selling;
if(!$property_renting && $property_selling){
$property_operation = 'Venta';
$precio = number_format($property->selling_cost, 0, ".", ".");
}else{
$property_operation = 'Alquiler';
$precio = number_format($property->renting_cost, 0, ".", ".");
}
// Comprobaremos si la propiedad ya existe
$existing_property = get_page_by_path( $property_slug, 'OBJECT', 'propiedades' );
if($existing_property === null){
// Insertaremos el post en la base de datos
$inserted_property = wp_insert_post([
'post_name' => $property_slug,
'post_title' => $property_title,
'post_type' => 'propiedades',
'meta_input' => array(
'identificacion' => $property->identifier,
'descripcion' => $property->description,
'direccion' => $property_direction,
'provincia' => $property->province,
'codigo-postal' => $property->zip_code,
'ciudad' => $property->town,
'calle' => $property->street,
'numero-de-calle' => $property->street_number,
'tipo-de-propiedad' => $property->kind,
'plantas' => $property->floor,
'dormitorios' => $property->bedrooms,
'banos' => $property->bathrooms,
'superficie' => $property->area,
'superficie-parcela' => $property->area_plot,
'superficie-util' => $property->area_util,
'ano-de-construccion' => $property->year_built,
'tipo-de-operacion' => $property_operation,
'precio' => $precio,
'periodicidad-de-cobro' => $property->renting_period,
'fotos' => $fotos,
'email_de_propietario' => $property->owner->email,
'fecha-de-actualizacion' => $property->updated
),
'post_status' => 'publish'
]);
if( is_wp_error( $inserted_property ) || $inserted_property === 0 ) {
error_log("Error: ". $inserted_property->get_error_message());
return false;
}
}else{
$existing_property_id = $existing_property->ID;
$exisiting_property_timestamp = get_post_meta($existing_property_id, 'fecha-de-actualizacion', true);
if( $property->updated > $exisiting_property_timestamp ){
$inserted_property = wp_insert_post([
'ID' => $existing_property_id,
'post_name' => $property_slug,
'post_title' => $property_title,
'post_type' => 'propiedades',
'meta_input' => array(
'identificacion' => $property->identifier,
'descripcion' => $property->description,
'direccion' => $property_direction,
'provincia' => $property->province,
'codigo-postal' => $property->zip_code,
'ciudad' => $property->town,
'calle' => $property->street,
'numero-de-calle' => $property->street_number,
'tipo-de-propiedad' => $property->kind,
'plantas' => $property->floor,
'dormitorios' => $property->bedrooms,
'banos' => $property->bathrooms,
'superficie' => $property->area,
'superficie-parcela' => $property->area_plot,
'superficie-util' => $property->area_util,
'ano-de-construccion' => $property->year_built,
'tipo-de-operacion' => $property_operation,
'precio-venta' => $property->selling_cost,
'precio-alquiler' => $property->renting_cost,
'periodicidad-de-cobro' => $property->renting_period,
'fotos' => $property->pictures,
'fecha-de-actualizacion' => $property->updated
),
'post_status' => 'publish'
]);
}
}
}
// Esto nos permite llamar a la funcion mediante ajax
$current_page = $current_page + 1;
wp_remote_post( admin_url('admin-ajax.php?action=get_properties_from_api'), [
'blocking' => false, //Esto nos permite activar la funcion y dejarnos ir a otra pagina sin bloquearnos
'sslverify' => false, // Desactiva la verificacion del SSl.
'body' => [
'current_page' => $current_page
]
] );
}else{
echo "No eres administrador";
}
}