admin
9 years ago
emails
9 years ago
fields
9 years ago
templates
9 years ago
class-fields.php
9 years ago
class-form.php
9 years ago
class-frontend.php
9 years ago
class-install.php
9 years ago
class-logging.php
9 years ago
class-preview.php
9 years ago
class-process.php
9 years ago
class-smart-tags.php
9 years ago
class-templates.php
9 years ago
class-widget.php
10 years ago
functions.php
9 years ago
integrations.php
9 years ago
class-preview.php
289 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Preview class. |
| 4 | * |
| 5 | * @package WPForms |
| 6 | * @author WPForms |
| 7 | * @since 1.1.5 |
| 8 | * @license GPL-2.0+ |
| 9 | * @copyright Copyright (c) 2016, WPForms LLC |
| 10 | */ |
| 11 | class WPForms_Preview { |
| 12 | |
| 13 | /** |
| 14 | * Primary class constructor. |
| 15 | * |
| 16 | * @since 1.1.5 |
| 17 | */ |
| 18 | public function __construct() { |
| 19 | |
| 20 | // Maybe load a preview page |
| 21 | add_action( 'init', array( $this, 'init' ) ); |
| 22 | |
| 23 | // Hide preview page from admin |
| 24 | add_action( 'pre_get_posts', array( $this, 'form_preview_hide' ) ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Determing if the user should see a preview page, if so, party on. |
| 29 | * |
| 30 | * @since 1.1.5 |
| 31 | */ |
| 32 | public function init() { |
| 33 | |
| 34 | // Check for preview param with allowed values |
| 35 | if ( empty( $_GET['wpforms_preview'] ) || !in_array( $_GET['wpforms_preview'], array( 'print', 'form' ) ) ) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | // Check for authenticated user with correct capabilities |
| 40 | if ( !is_user_logged_in() || !current_user_can( apply_filters( 'wpforms_manage_cap', 'manage_options' ) ) ) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | // Print preview |
| 45 | if ( 'print' == $_GET['wpforms_preview'] && !empty( $_GET['entry_id'] ) ) { |
| 46 | $this->print_preview(); |
| 47 | } |
| 48 | |
| 49 | // Form preview |
| 50 | if ( 'form' == $_GET['wpforms_preview'] && !empty( $_GET['form_id'] ) ) { |
| 51 | $this->form_preview(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Print Preview. |
| 57 | * |
| 58 | * @since 1.1.5 |
| 59 | */ |
| 60 | public function print_preview() { |
| 61 | |
| 62 | // Load entry details |
| 63 | $entry = wpforms()->entry->get( absint( $_GET['entry_id'] ) ); |
| 64 | |
| 65 | // Double check that we found a real entry |
| 66 | if ( ! $entry || empty( $entry ) ) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | // Get form details |
| 71 | $form_data = wpforms()->form->get( $entry->form_id, array( 'content_only' => true ) ); |
| 72 | |
| 73 | // Double check that we found a valid entry |
| 74 | if ( ! $form_data || empty( $form_data ) ) { |
| 75 | return; |
| 76 | } |
| 77 | ?> |
| 78 | <!doctype html> |
| 79 | <html> |
| 80 | <head> |
| 81 | <meta charset="utf-8"> |
| 82 | <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> |
| 83 | <title>WPForms Print Preview - <?php echo ucfirst( sanitize_text_field( $form_data['settings']['form_title'] ) ); ?> </title> |
| 84 | <meta name="description" content=""> |
| 85 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 86 | <meta name="robots" content="noindex,nofollow,noarchive"> |
| 87 | <link rel="stylesheet" href="<?php echo includes_url('css/buttons.min.css'); ?>" type="text/css"> |
| 88 | <link rel="stylesheet" href="<?php echo WPFORMS_PLUGIN_URL; ?>assets/css/wpforms-preview.css" type="text/css"> |
| 89 | <script type="text/javascript" src="<?php echo includes_url('js/jquery/jquery.js'); ?>"></script> |
| 90 | <script type="text/javascript" src="<?php echo WPFORMS_PLUGIN_URL; ?>assets/js/wpforms-preview.js"></script> |
| 91 | </head> |
| 92 | <body class="wp-core-ui"> |
| 93 | <div class="wpforms-preview" id="print"> |
| 94 | <h1> |
| 95 | <?php echo sanitize_text_field( $form_data['settings']['form_title'] ); ?> <span> - <?php printf( __( 'Entry #%d', 'wpforms' ), absint( $entry->entry_id ) ); ?></span> |
| 96 | <div class="buttons"> |
| 97 | <a href="" class="button button-secondary close-window">Close</a> |
| 98 | <a href="" class="button button-primary print">Print</a> |
| 99 | </div> |
| 100 | </h1> |
| 101 | <?php |
| 102 | $fields = apply_filters( 'wpforms_entry_single_data', wpforms_decode( $entry->fields ), $entry, $form_data ); |
| 103 | |
| 104 | if ( empty( $fields ) ) { |
| 105 | |
| 106 | // Whoops, no fields! This shouldn't happen under normal use cases. |
| 107 | echo '<p class="no-fields">' . __( 'This entry does not have any fields', 'wpforms' ) . '</p>'; |
| 108 | |
| 109 | } else { |
| 110 | |
| 111 | echo '<div class="fields">'; |
| 112 | |
| 113 | // Display the fields and their values |
| 114 | foreach ( $fields as $key => $field ) { |
| 115 | |
| 116 | $field_value = apply_filters( 'wpforms_html_field_value', wp_strip_all_tags( $field['value'] ), $field, $form_data, 'entry-single' ); |
| 117 | $field_class = sanitize_html_class( 'wpforms-field-' . $field['type'] ); |
| 118 | $field_class .= empty( $field_value ) ? ' empty' : ''; |
| 119 | |
| 120 | echo '<div class="wpforms-entry-field ' . $field_class . '">'; |
| 121 | |
| 122 | // Field name |
| 123 | echo '<p class="wpforms-entry-field-name">'; |
| 124 | echo !empty( $field['name'] ) ? wp_strip_all_tags( $field['name'] ) : sprintf( __( 'Field ID #%d', 'wpforms' ), absint( $field['id'] ) ); |
| 125 | echo '</p>'; |
| 126 | |
| 127 | // Field value |
| 128 | echo '<p class="wpforms-entry-field-value">'; |
| 129 | echo !empty( $field_value ) ? nl2br( make_clickable( $field_value ) ) : __( 'Empty', 'wpforms' ); |
| 130 | echo '</p>'; |
| 131 | |
| 132 | echo '</div>'; |
| 133 | } |
| 134 | |
| 135 | echo '</div>'; |
| 136 | } |
| 137 | ?> |
| 138 | </div><!-- .wrap --> |
| 139 | <p class="site"><a href="<?php echo home_url(); ?>"><?php echo get_bloginfo( 'name'); ?></a></p> |
| 140 | </body> |
| 141 | <?php |
| 142 | exit(); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Check if preview page exists, if not create it. |
| 147 | * |
| 148 | * @since 1.1.9 |
| 149 | */ |
| 150 | public function form_preview_check() { |
| 151 | |
| 152 | if ( !is_admin() ) |
| 153 | return; |
| 154 | |
| 155 | // Verify page exits |
| 156 | $preview = get_option( 'wpforms_preview_page' ); |
| 157 | |
| 158 | if ( $preview ) { |
| 159 | |
| 160 | $preview_page = get_post( $preview ); |
| 161 | |
| 162 | // Check to see if the visibility has been changed, if so correct it |
| 163 | if ( !empty( $preview_page ) && 'private' != $preview_page->post_status ) { |
| 164 | $preview_page->post_status = 'private'; |
| 165 | wp_update_post( $preview_page ); |
| 166 | return; |
| 167 | } elseif ( !empty( $preview_page ) ) { |
| 168 | return; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // Create the custom preview page |
| 173 | $content = '<p>' . __( 'This is the WPForms preview page. All your form previews will be handled on this page.', 'wpforms' ) . '</p>'; |
| 174 | $content .= '<p>' . __( 'The page is set to private, so it is not publically accessible. Please do not delete this page :) .', 'wpforms' ) . '</p>'; |
| 175 | $args = array( |
| 176 | 'post_type' => 'page', |
| 177 | 'post_name' => 'wpforms-preview', |
| 178 | 'post_author' => 1, |
| 179 | 'post_title' => __( 'WPForms Preview', 'wpforms' ), |
| 180 | 'post_status' => 'private', |
| 181 | 'post_content' => $content, |
| 182 | 'comment_status' => 'closed' |
| 183 | ); |
| 184 | $id = wp_insert_post( $args ); |
| 185 | if ( $id ) { |
| 186 | update_option( 'wpforms_preview_page', $id ); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Preview page URL. |
| 192 | * |
| 193 | * @since 1.1.9 |
| 194 | * @param int $form_id |
| 195 | * @return string |
| 196 | */ |
| 197 | public function form_preview_url( $form_id ) { |
| 198 | |
| 199 | $id = get_option( 'wpforms_preview_page' ); |
| 200 | |
| 201 | if ( ! $id ) { |
| 202 | return home_url(); |
| 203 | } |
| 204 | |
| 205 | $url = get_permalink( $id ); |
| 206 | |
| 207 | if ( ! $url ) { |
| 208 | return home_url(); |
| 209 | } |
| 210 | |
| 211 | return add_query_arg( array( 'wpforms_preview' => 'form', 'form_id' => absint( $form_id ) ), $url ); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Fires when form preview might be detected. |
| 216 | * |
| 217 | * @since 1.1.9 |
| 218 | */ |
| 219 | public function form_preview() { |
| 220 | |
| 221 | add_filter( 'the_posts', array( $this, 'form_preview_query' ), 10, 2 ); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Tweak the page content for form preview page requests. |
| 226 | * |
| 227 | * @since 1.1.9 |
| 228 | * @param array $posts |
| 229 | * @param object $query |
| 230 | * @return array |
| 231 | */ |
| 232 | public function form_preview_query( $posts, $query ) { |
| 233 | |
| 234 | // One last cap check, just for fun. |
| 235 | if ( !is_user_logged_in() || !current_user_can( apply_filters( 'wpforms_manage_cap', 'manage_options' ) ) ) { |
| 236 | return $posts; |
| 237 | } |
| 238 | |
| 239 | // Only target main query |
| 240 | if ( ! $query->is_main_query() ) { |
| 241 | return $posts; |
| 242 | } |
| 243 | |
| 244 | // If our queried object ID does not match the preview page ID, return early. |
| 245 | $preview_id = absint( get_option( 'wpforms_preview_page' ) ); |
| 246 | $queried = $query->get_queried_object_id(); |
| 247 | if ( $queried && $queried != $preview_id && isset( $query->query_vars['page_id'] ) && $preview_id != $query->query_vars['page_id'] ) { |
| 248 | return $posts; |
| 249 | } |
| 250 | |
| 251 | // Get the form details |
| 252 | $form = wpforms()->form->get( absint( $_GET['form_id'] ), array( 'content_only' => true ) ); |
| 253 | |
| 254 | if ( ! $form || empty( $form ) ) { |
| 255 | return $posts; |
| 256 | } |
| 257 | |
| 258 | // Customize the page content |
| 259 | $title = sanitize_text_field( $form['settings']['form_title'] ); |
| 260 | $shortcode = '[wpforms id="' . absint( $form['id'] ) . '"]'; |
| 261 | $content = __( 'This is a preview of your form. This page not publically accessible.', 'wpforms' ); |
| 262 | if ( !empty( $_GET['new_window'] ) ) { |
| 263 | $content .= ' <a href="javascript:window.close();">' . __( 'Close this window', 'wpforms' ) . '.</a>'; |
| 264 | } |
| 265 | $posts[0]->post_title = $title . __( ' Preview', 'wpforms' ); |
| 266 | $posts[0]->post_content = $content . $shortcode; |
| 267 | $posts[0]->post_status = 'public'; |
| 268 | |
| 269 | return $posts; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Hide the preview page from admin |
| 274 | * |
| 275 | * @since 1.2.3 |
| 276 | * @param object $query |
| 277 | */ |
| 278 | function form_preview_hide( $query ) { |
| 279 | |
| 280 | if( $query->is_main_query() && is_admin() && isset( $query->query_vars['post_type'] ) && 'page' == $query->query_vars['post_type'] ) { |
| 281 | $wpforms_preview = intval( get_option( 'wpforms_preview_page' ) ); |
| 282 | if( $wpforms_preview ) { |
| 283 | $exclude = $query->query_vars['post__not_in']; |
| 284 | $exclude[] = $wpforms_preview; |
| 285 | $query->set( 'post__not_in', $exclude ); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |