spivurno
10/1/2013 - 5:02 PM

GW Preview On Page // Automatically create a page to preview your Gravity Form on.

GW Preview On Page // Automatically create a page to preview your Gravity Form on.

<?php
/**
* GW Preview On Page
* Automatically create a page to preview your Gravity Form on.
*/
class GWPreviewOnPage {
    
    function __construct() {
      
        if( !class_exists( 'GFForms' ) )
            return;
            
        add_action( 'admin_footer', array( $this, 'display_preview_link' ) );
        add_action( 'wp_ajax_gwpop_add_page', array( $this, 'ajax_add_page' ) );
        
    }
    
    function display_preview_link() {
        
        if( ! $this->is_applicable_page() )
            return;
        
        $form_id = rgget( 'id' );
        $page_id = $this->get_form_page_id( $form_id );
        $url = $page_id ? get_permalink( $page_id ) : '';
        
        ?>
        
        <script type="text/javascript">
        (function($){
            $('<li class="gf_form_toolbar_preview"><a href="<?php echo $url; ?>" id="gw-page-preview"><?php _e( 'Preview on Page' ); ?></a></li>').insertAfter( 'li.gf_form_toolbar_preview' );
            $('#gw-page-preview').click(function(event){
                
                if( $(this).attr('href') )
                    return;
                
                event.preventDefault();
                
                var pageTitle = prompt( '<?php _e( 'Please enter a page title:' ); ?>' );
                var isAjax = prompt( '<?php _e( 'Enable AJAX for this form?' ); ?>' );
                
                $.post( ajaxurl, {
                    post_title: pageTitle,
                    form_id: '<?php echo $form_id; ?>',
                    is_ajax: isAjax,
                    action: 'gwpop_add_page',
                    nonce: '<?php echo wp_create_nonce( 'gwpop_add_page' ); ?>'  
                }, function( response ) {
                    location.href = $.parseJSON( response );
                });
               
            });
        })(jQuery);
        </script>
        
        <?php
    }
    
    function ajax_add_page() {
        
        if( ! wp_verify_nonce( rgpost( 'nonce' ), 'gwpop_add_page' ) )
            return;
        
        $post_title = rgpost( 'post_title' );
        $form_id = rgpost( 'form_id' );
        $is_ajax = rgpost( 'is_ajax' );
        
        $ajax = $is_ajax ? 'ajax="true"' : '';
        
        $page_id = wp_insert_post( array( 
            'post_type' => 'page',
            'post_title' => $post_title,
            'post_content' => '[gravityforms id="' . $form_id . '" ' . $ajax . ']'
            ) );
        
        $this->update_form_page_id( $form_id, $page_id );
        
        die( json_encode( get_permalink( $page_id ) ) );
    }
    
    function is_applicable_page() {
        return in_array( rgget( 'page' ), array( 'gf_edit_forms', 'gf_entries' ) ) && rgget( 'id' );
    }
    
    function get_form_page_id( $form_id ) {
        
        $pages = get_option( 'gw_form_pages' );
        if( ! is_array( $pages ) )
            return false;
            
        return rgar( $pages, $form_id );
    }
    
    function update_form_page_id( $form_id, $page_id ) {
        
        $pages = get_option( 'gw_form_pages' );
        if( ! is_array( $pages ) )
            $pages = array();
            
        $pages[$form_id] = $page_id;
        update_option( 'gw_form_pages', $pages );
        
    }
    
}

new GWPreviewOnPage();