Preview.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_Display_Preview
  4. */
  5. final class NF_Display_Preview
  6. {
  7. protected $form_id = '';
  8. public function __construct()
  9. {
  10. if ( ! isset( $_GET['nf_preview_form'] ) ) return;
  11. $this->_form_id = $_GET['nf_preview_form'];
  12. add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
  13. add_filter('the_title', array( $this, 'the_title' ) );
  14. remove_filter( 'the_content', 'wpautop' );
  15. remove_filter( 'the_excerpt', 'wpautop' );
  16. add_filter('the_content', array( $this, 'the_content' ), 9001 );
  17. add_filter('get_the_excerpt', array( $this, 'the_content' ) );
  18. add_filter('template_include', array( $this, 'template_include' ) );
  19. add_filter('post_thumbnail_html', array( $this, 'post_thumbnail_html' ) );
  20. }
  21. public function pre_get_posts( $query )
  22. {
  23. $query->set( 'posts_per_page', 1 );
  24. }
  25. /**
  26. * @return string
  27. */
  28. function the_title( $title )
  29. {
  30. if( ! in_the_loop() ) return $title;
  31. $form_title = Ninja_Forms()->form( $this->_form_id )->get()->get_setting( 'title' );
  32. return $form_title . " " . __( 'Preview', 'ninja-forms' );
  33. }
  34. /**
  35. * @return string
  36. */
  37. function the_content()
  38. {
  39. if ( ! is_user_logged_in() ) return __( 'You must be logged in to preview a form.', 'ninja-forms' );
  40. // takes into account if we are trying to preview a non-published form
  41. $tmp_id_test = explode( '-', $this->_form_id );
  42. // if only 1 element, then is it numeric
  43. if( 1 === count( $tmp_id_test) && ! is_numeric( $tmp_id_test[ 0 ] ) ) {
  44. return __( 'You must provide a valid form ID.', 'ninja-forms' );
  45. }
  46. // if 2 array elements, is the first equal to 'tmp' and the second numeric
  47. elseif ( ( 2 === count( $tmp_id_test )
  48. && ('tmp' != $tmp_id_test[ 0 ]
  49. || ! is_numeric( $tmp_id_test[ 1 ] ) ) ) ) {
  50. return __( 'You must provide a valid form ID.', 'ninja-forms' );
  51. }
  52. return do_shortcode( "[nf_preview id='{$this->_form_id}']" );
  53. }
  54. /**
  55. * @return string
  56. */
  57. function template_include()
  58. {
  59. return locate_template( array( 'page.php', 'single.php', 'index.php' ) );
  60. }
  61. function post_thumbnail_html() {
  62. return '';
  63. }
  64. } // END CLASS NF_Display_Preview