PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.2.0
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.2.0
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / class-form-preview.php

class-form-preview.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.2.0, at includes/class-form-preview.php

146 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /**
27 * is_preview
28 *
29 * @var string
30 */
31 private $is_preview = true;
32
33 function __construct() {
34
35 if ( ! isset( $_GET['weforms_preview'] ) && empty( $_GET['weforms'] ) ) {
36 return;
37 }
38
39 if ( ! empty( $_GET['weforms'] ) ) {
40
41 $hash = explode("_", base64_decode( $_GET['weforms'] ));
42 $_hash = $hash[0];
43 $this->form_id = intval( end($hash) );
44 $form = weforms()->form->get( $this->form_id );
45
46 if ( ! $form ) {
47 return;
48 }
49
50 $form_settings = $form->get_settings();
51
52 if ( ! isset($form_settings['sharing_on']) || $form_settings['sharing_on'] !== 'on' ) {
53 return;
54 }
55
56 if ( ! isset($form_settings['sharing_hash']) || $form_settings['sharing_hash'] !== $_hash ) {
57 return;
58 }
59
60 $this->is_preview = false;
61
62 } else {
63
64 $this->form_id = isset( $_GET['form_id'] ) ? intval( $_GET['form_id'] ) : 0;
65 }
66
67 add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
68 add_filter( 'template_include', array( $this, 'template_include' ) );
69
70 add_filter( 'the_title', array( $this, 'the_title' ) );
71 add_filter( 'the_content', array( $this, 'the_content' ) );
72 add_filter( 'get_the_excerpt', array( $this, 'the_content' ) );
73 add_filter( 'post_thumbnail_html', '__return_empty_string' );
74 }
75
76 /**
77 * Set the page title
78 *
79 * @param string $title
80 *
81 * @return string
82 */
83 public function the_title( $title ) {
84 if ( ! in_the_loop() ) {
85 return $title;
86 }
87
88 $form = weform_get_form( $this->form_id );
89
90 if ( ! $form ) {
91 return $title;
92 }
93
94 $preview = $this->is_preview ? 'Preview' : '';
95
96 return $form->get_name() . ' ' . $preview;
97 }
98
99 /**
100 * Set the content of the page
101 *
102 * @param string $content
103 *
104 * @return string
105 */
106 public function the_content( $content ) {
107
108 if ( $this->is_preview ) {
109
110 if ( ! is_user_logged_in() ) {
111 return __( 'You must be logged in to preview this form.', 'weforms' );
112 }
113
114 $viewing_capability = apply_filters( 'weforms_preview_form_cap', 'edit_posts' ); // at least has to be contributor
115
116 if ( ! current_user_can( $viewing_capability ) ) {
117 return __( 'Sorry, you are not eligible to preview this form.', 'weforms' );
118 }
119 }
120
121 return do_shortcode( sprintf( '[weforms id="%d"]', $this->form_id ) );
122 }
123
124 /**
125 * Set the posts to one
126 *
127 * @param WP_Query $query
128 *
129 * @return void
130 */
131 public function pre_get_posts( $query ) {
132 if ( $query->is_main_query() ) {
133 $query->set( 'posts_per_page', 1 );
134 }
135 }
136
137 /**
138 * Limit the page templates to singular pages only
139 *
140 * @return string
141 */
142 function template_include() {
143 return locate_template( array( 'page.php', 'single.php', 'index.php' ) );
144 }
145 }
146