Reply To: Insert Message from another application


Home Forums Front End PM PRO Insert Message from another application Reply To: Insert Message from another application

#6189
Shamim Hasan
Keymaster

Please use following (Untested).
let me know


function action_putmessage( $args ) {
	global $out, $admin_user_login;
	
	if( ! function_exists( 'fep_send_message' ) )
	return false;
	
	$sender = fep_get_userdata( $admin_user_login, 'ID', 'login' );
	
	if (!$sender) {
		$out['errmsg'] = "No such sender '$admin_user_login'";
		return false;
	}
	$recipient = fep_get_userdata( $args['user'], 'ID', 'login' );
	if (!$recipient) {
		$out['errmsg'] = "No such recipient '{$args['user']}'";
		return false;
	}
	$message = array(
		'message_title' => $args['title'],
		'message_content' => $args['message'],
		'message_to_id' => $recipient,
		'open_emr_args' => $args, //pass to get in attachment function
	);
	$override = array(
		'post_author' => $sender,
	);
	$message_id = fep_send_message( $message, $override );
}

add_action ('fep_action_message_after_send', 'fep_open_emr_upload_attachments', 10, 3 );

function fep_open_emr_upload_attachments( $message_id, $message, $inserted_message ){
	if ( ! fep_get_option( 'allow_attachment', 1 ) || ! $message_id || empty( $message['open_emr_args']) )
	return false;
	
	$args = $message['open_emr_args'];
	
	$name = isset( $args['filename'] ) ? $args['filename'] : '';
	
	if( ! $name )
	return false;
	
	$size_limit = (int) wp_convert_hr_to_bytes(fep_get_option('attachment_size','4MB'));
	$fields = (int) fep_get_option('attachment_no', 4);
	
	if( class_exists( 'Fep_Attachment' ) ){
		add_filter('upload_dir', array(Fep_Attachment::init(), 'upload_dir'));
	}
	
	$mime = isset( $args['mimetype'] ) ? $args['mimetype'] : '';
	$content = isset( $args['contents'] ) ? base64_decode($args['contents']) : '';
	
	if( !$mime || !in_array( $mime, get_allowed_mime_types() ) )
	return false;
	
	$size = strlen( $content );
	if( $size > $size_limit )
	return false;
	
	$att = wp_upload_bits( $name, null, $content );
	
	if( ! isset( $att['file'] ) || ! isset( $att['url'] ) || ! isset( $att['type'] ) )
	return false;
	
	$attachment = array(
		'guid'           => $att['url'], 
		'post_mime_type' => $att['type'],
		'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $att['url'] ) ),
		'post_content'   => '',
		'post_author'	=> $inserted_message->post_author,
		'post_status'    => 'inherit'
	);
	
	// Insert the attachment.
	wp_insert_attachment( $attachment, $att['file'], $message_id );
	
	if( class_exists( 'Fep_Attachment' ) ){
		remove_filter('upload_dir', array(Fep_Attachment::init(), 'upload_dir'));
	}
}