| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The preview class |
| 5 |
* |
| 6 |
* This is a clever technique to preview a form without having to set any placeholder page by |
| 7 |
* setting the page templates to singular pages (page.php, single.php and index.php). Setting |
| 8 |
* the posts per page to 1 and changing the page title and contents dynamiclly allows us to |
| 9 |
* preview the form without any placeholder page. |
| 10 |
* |
| 11 |
* This technique requires the theme to have at least the above mentioned templates in the theme |
| 12 |
* and requires to have the WordPress Loop, otherwise we wouldn't be able to set the title and |
| 13 |
* the page content dynamically. |
| 14 |
* |
| 15 |
* The technique is borrowed from Ninja Forms (thanks guys!) |
| 16 |
*/ |
| 17 |
class WeForms_Form_Preview { |
| 18 |
|
| 19 |
/** |
| 20 |
* Form id |
| 21 |
* |
| 22 |
* @var integer |
| 23 |
*/ |
| 24 |
private $form_id; |
| 25 |
|
| 26 |
function __construct() { |
| 27 |
|
| 28 |
if ( ! isset( $_GET['weforms_preview'] ) ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
$this->form_id = isset( $_GET['form_id'] ) ? intval( $_GET['form_id'] ) : 0; |
| 33 |
|
| 34 |
add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) ); |
| 35 |
add_filter( 'template_include', array( $this, 'template_include' ) ); |
| 36 |
|
| 37 |
add_filter( 'the_title', array( $this, 'the_title' ) ); |
| 38 |
add_filter( 'the_content', array( $this, 'the_content' ) ); |
| 39 |
add_filter( 'get_the_excerpt', array( $this, 'the_content' ) ); |
| 40 |
add_filter( 'post_thumbnail_html', '__return_empty_string' ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Set the page title |
| 45 |
* |
| 46 |
* @param string $title |
| 47 |
* |
| 48 |
* @return string |
| 49 |
*/ |
| 50 |
public function the_title( $title ) { |
| 51 |
if ( ! in_the_loop() ) { |
| 52 |
return $title; |
| 53 |
} |
| 54 |
|
| 55 |
$form = weform_get_form( $this->form_id ); |
| 56 |
|
| 57 |
if ( ! $form ) { |
| 58 |
return $title; |
| 59 |
} |
| 60 |
|
| 61 |
return $form->post_title . ' ' . __( 'Preview', 'weforms' ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Set the content of the page |
| 66 |
* |
| 67 |
* @param string $content |
| 68 |
* |
| 69 |
* @return string |
| 70 |
*/ |
| 71 |
public function the_content( $content ) { |
| 72 |
|
| 73 |
if ( ! is_user_logged_in() ) { |
| 74 |
return __( 'You must be logged in to preview this form.', 'weforms' ); |
| 75 |
} |
| 76 |
|
| 77 |
$viewing_capability = apply_filters( 'weforms_preview_form_cap', 'edit_posts' ); // at least has to be contributor |
| 78 |
|
| 79 |
if ( ! current_user_can( $viewing_capability ) ) { |
| 80 |
return __( 'Sorry, you are not eligible to preview this form.', 'weforms' ); |
| 81 |
} |
| 82 |
|
| 83 |
return do_shortcode( sprintf( '[weforms id="%d"]', $this->form_id ) ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Set the posts to one |
| 88 |
* |
| 89 |
* @param WP_Query $query |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public function pre_get_posts( $query ) { |
| 94 |
if ( $query->is_main_query() ) { |
| 95 |
$query->set( 'posts_per_page', 1 ); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Limit the page templates to singular pages only |
| 101 |
* |
| 102 |
* @return string |
| 103 |
*/ |
| 104 |
function template_include() { |
| 105 |
return locate_template( array( 'page.php', 'single.php', 'index.php' ) ); |
| 106 |
} |
| 107 |
} |
| 108 |
|