| @@ -1,140 +1,145 @@ | ||
| 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 | -} | |
| 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 | +} | |