Bild eines Immobilienmaklers importieren

Das Anhangsbild eines Immobilienmaklers soll aus der OpenImmoXML-Datei ebenfalls importiert werden.

Für WPCasa:

/**
 * Ansprechpartnerbild importieren.
 */
function mysite_get_current_import_dir( $dir ) {
  global $mysite_current_import_dir;
  $mysite_current_import_dir = $dir;
}
add_action( 'immonex_oi2wp_start_import_process', 'mysite_get_current_import_dir' );
function mysite_add_agent_image( $post_id, $immobilie ) {
  global $immonex_openimmo2wp, $mysite_current_import_dir;
  if ( ! isset( $immobilie->kontaktperson->foto->daten->pfad ) ) {
    return;
  }
  $source_file = trailingslashit( $mysite_current_import_dir ) . (string) $immobilie->kontaktperson->foto->daten->pfad;
  $temp_file   = uniqid() . '_' . basename( $source_file );
  $tmp         = $immonex_openimmo2wp->wp_filesystem->copy( $source_file, $temp_file ) ? $temp_file : false;
  if ( ! $tmp ) return;
  $file_array = array(
    'name'     => basename( $source_file ),
    'tmp_name' => $tmp
  );
  $image_id = media_handle_sideload( $file_array, $post_id, '' );
  if ( ! is_wp_error( $image_id ) ) {
    $logo_url = wp_get_attachment_url( $image_id );
    if ( $logo_url ) {
      add_post_meta( $post_id, '_agent_logo_id', $logo_id, true );
      add_post_meta( $post_id, '_agent_logo', $logo_url, true );
    }
  }
}
add_action( 'immonex_oi2wp_property_imported', 'mysite_add_agent_image', 10, 2 );

Für Theme WPCasa.

Für WP Residence (und andere Themes):

/**
 * Ansprechpartner-Foto importieren/zuweisen.
 */
function mysite_process_agent_image( $post_id, $immobilie ) {
  global $immonex_openimmo2wp;
 
  if ( ! isset( $immonex_openimmo2wp ) ) {
    return;
  }
  $agent_photo = (string) $immobilie->kontaktperson->foto->daten->pfad;
  if ( ! $agent_photo ) {
    return;
  }
  $property_author_id = get_post_meta( $post_id, 'property_user', true );
  $author_photo_url   = $property_author_id ? get_user_meta( $property_author_id, 'custom_picture', true ) : false;
  $agent_id       = get_post_meta( $post_id, 'property_agent', true );
  $agent_image_id = $agent ? get_post_thumbnail_id( $agent_id ) : false;
  if ( ! $author_photo_url || ! $agent_image_id ) {
    $dir         = dirname( $immonex_openimmo2wp->current_import_xml_file );
    $source_file = trailingslashit( $dir ) . $agent_photo;
    $temp_file   = trailingslashit( $dir ) . uniqid() . '_' . $agent_photo;
    $tmp         = $immonex_openimmo2wp->wp_filesystem->copy( $source_file, $temp_file ) ? $temp_file : false;
    if ( ! $tmp ) return;
    $file_array = array(
      'name'     => basename( $source_file ),
      'tmp_name' => $tmp
    );
    $image_id = media_handle_sideload( $file_array, $post_id, '' );
    if ( ! is_wp_error( $image_id ) ) {
      if ( $property_author_id && ! $author_photo_url ) {
        $image_url = wp_get_attachment_url( $image_id );
        update_user_meta( $property_author_id, 'custom_picture', $image_url );
      }
      if ( $agent_id && ! $agent_image_id ) {
        update_post_meta( $agent_id, '_thumbnail_id', $image_id );
      }
    }
  }
}
add_action( 'immonex_oi2wp_property_imported', 'mysite_process_agent_image', 100, 2 );

Für Theme WP Residence.