<?php
/**
 * Plugin Name: Customize-Preview Posts
 * Plugin Author: Nick Halsey
 * Description: Opens post previews in-page with the Customizer. Experimental.
 */

add_action( 'admin_enqueue_scripts', 'customize_preview_posts_enqueue' );
function customize_preview_posts_enqueue() {
	wp_enqueue_script( 'customize-loader' ); // Opens the Customizer in a seamless full-screen iframe with an animation, rather than a pageload; closing returns to existing admin screen although any changes to options on that screen are not updated.
}

add_filter( 'preview_page_link', 'customize_preview_posts_preview_link' );
add_filter( 'preview_post_link', 'customize_preview_posts_preview_link' );
function customize_preview_posts_preview_link( $url ) {
	return add_query_arg( array( 'url' => urlencode( $url ) ), admin_url( 'customize.php' ) );
}

add_action( 'admin_footer-post-new.php', 'customize_preview_dont_feel_like_making_another_file_for_this' );
add_action( 'admin_footer-post.php', 'customize_preview_dont_feel_like_making_another_file_for_this' );
function customize_preview_dont_feel_like_making_another_file_for_this() {
?>
<script>
	jQuery( document ).ready( function( $ ) {
		$( '#post-preview' ).addClass( 'load-customize' ) // Triggers customize-loader.js.
		                    .attr( 'target', '' ).unbind( 'click.post-preview' ); // Don't try to open in a new tab.
	} ); // @todo: currently seems to require manually saving the draft before it will actually preview new content. In other words, probably need to trigger the auto-save from here, or, the previewing isn't actually working.
</script>
<?php
}