<?php
/*
Plugin Name: Add Link to Large Image in Add Media Window
Version: 1.0
Plugin URI: http://core.trac.wordpress.org/ticket/21114
Description: Allows to insert a link to the large version of the image into post content.
Author: Robert Staddon, Sergey Biryukov
*/

function add_link_to_large_image( $form_fields, $post ) {
	$file = wp_get_attachment_url( $post->ID );
	$link = get_attachment_link( $post->ID );

	$url_type = get_option( 'image_default_link_type' );
	if ( empty( $url_type ) )
		$url_type = get_user_setting( 'urlbutton', 'post' );

	$url = '';
	if ( $url_type == 'file' )
		$url = $file;
	elseif ( $url_type == 'post' )
		$url = $link;

	if ( substr( $post->post_mime_type, 0, 5 ) == 'image' ) {
		$large_image_array = wp_get_attachment_image_src( $post->ID, 'large' );
		$file_large = $large_image_array[0];
		$url = $file_large;
	}

	$form_fields['url']['html'] = "
		<input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr( $url ) . "' /><br />
		<button type='button' class='button urlnone' data-link-url=''>" . __( 'None' ) . "</button>
	";
	if ( ! empty( $file_large ) )
		$form_fields['url']['html'] .= "
		<button type='button' class='button urlfile' data-link-url='" . esc_attr( $file_large ) . "'>" . __( 'Large Image' ) . "</button>
	";
	$form_fields['url']['html'] .= "
		<button type='button' class='button urlfile' data-link-url='" . esc_attr( $file ) . "'>" . __( 'File URL' ) . "</button>
		<button type='button' class='button urlpost' data-link-url='" . esc_attr( $link ) . "'>" . __( 'Attachment Post URL' ) . "</button>
	";

	return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'add_link_to_large_image', 10, 2 );
?>