PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.6.2
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.6.2
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.6.2, at includes/class-form-preview.php

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