Helpers
11 months ago
Integrations
1 week ago
RestApi
2 weeks ago
abilities
2 weeks ago
abstracts
2 weeks ago
admin
2 weeks ago
blocks
1 year ago
elementor
2 years ago
export
2 months ago
fields
2 weeks ago
interfaces
8 years ago
libraries
2 years ago
log-handlers
1 year ago
shortcodes
2 weeks ago
stats
5 months ago
templates
3 months ago
traits
2 weeks ago
class-everest-forms.php
1 week ago
class-evf-addon-upsell.php
2 weeks ago
class-evf-ajax.php
2 weeks ago
class-evf-autoloader.php
7 years ago
class-evf-background-process-import-entries.php
1 year ago
class-evf-background-updater.php
7 years ago
class-evf-cache-helper.php
2 months ago
class-evf-cron.php
2 years ago
class-evf-deprecated-action-hooks.php
6 years ago
class-evf-deprecated-filter-hooks.php
5 years ago
class-evf-email-entries-report.php
3 months ago
class-evf-emails.php
2 weeks ago
class-evf-fields.php
2 weeks ago
class-evf-form-handler.php
2 weeks ago
class-evf-form-task.php
2 weeks ago
class-evf-forms-features.php
2 weeks ago
class-evf-frontend-scripts.php
2 weeks ago
class-evf-install.php
2 months ago
class-evf-integrations.php
3 months ago
class-evf-log-levels.php
8 years ago
class-evf-logger.php
5 years ago
class-evf-post-types.php
1 year ago
class-evf-privacy.php
6 years ago
class-evf-report-cron.php
2 months ago
class-evf-reporting.php
2 months ago
class-evf-session-handler.php
7 years ago
class-evf-shortcodes.php
1 year ago
class-evf-smart-tags.php
9 months ago
class-evf-template-loader.php
1 year ago
class-evf-validation.php
6 years ago
evf-conditional-functions.php
6 years ago
evf-core-functions.php
2 weeks ago
evf-deprecated-functions.php
6 years ago
evf-entry-functions.php
4 months ago
evf-formatting-functions.php
4 years ago
evf-notice-functions.php
4 years ago
evf-template-functions.php
4 years ago
evf-template-hooks.php
7 years ago
evf-update-functions.php
5 years ago
class-evf-template-loader.php
417 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Template Loader |
| 4 | * |
| 5 | * @package EverestForms\Classes |
| 6 | * @version 1.3.1 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * Template loader class. |
| 13 | */ |
| 14 | class EVF_Template_Loader { |
| 15 | |
| 16 | /** |
| 17 | * Store the form ID. |
| 18 | * |
| 19 | * @var integer |
| 20 | */ |
| 21 | private static $form_id = 0; |
| 22 | |
| 23 | /** |
| 24 | * Store whether we're processing a form preview inside the_content filter. |
| 25 | * |
| 26 | * @var boolean |
| 27 | */ |
| 28 | private static $in_content_filter = false; |
| 29 | |
| 30 | /** |
| 31 | * Hook in methods. |
| 32 | */ |
| 33 | public static function init() { |
| 34 | self::$form_id = isset( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification |
| 35 | |
| 36 | if ( ! is_admin() && isset( $_GET['evf_preview'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 37 | add_action( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ) ); |
| 38 | add_filter( 'edit_post_link', array( __CLASS__, 'edit_form_link' ) ); |
| 39 | add_filter( 'home_template_hierarchy', array( __CLASS__, 'template_include' ) ); |
| 40 | add_filter( 'frontpage_template_hierarchy', array( __CLASS__, 'template_include' ) ); |
| 41 | add_action( 'template_redirect', array( __CLASS__, 'form_preview_init' ) ); |
| 42 | add_filter( 'astra_remove_entry_header_content', '__return_true' ); // Need to remove in next version, If astra release the patches. |
| 43 | } elseif ( isset( $_GET['evf_email_preview'] ) ? sanitize_text_field( wp_unslash( $_GET['evf_email_preview'] ) ) : '' ) { |
| 44 | add_filter( 'template_include', array( __CLASS__, 'email_preview_init' ) ); |
| 45 | } else { |
| 46 | add_filter( 'template_include', array( __CLASS__, 'template_loader' ) ); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Hook into pre_get_posts to limit posts. |
| 52 | * |
| 53 | * @param WP_Query $q Query instance. |
| 54 | */ |
| 55 | public static function pre_get_posts( $q ) { |
| 56 | // Limit one post to query. |
| 57 | if ( $q->is_main_query() ) { |
| 58 | $q->set( 'posts_per_page', 1 ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Change edit link of preview page. |
| 64 | * |
| 65 | * @param string $link Edit post link. |
| 66 | */ |
| 67 | public static function edit_form_link( $link ) { |
| 68 | if ( 0 < self::$form_id ) { |
| 69 | return '<a href="' . esc_url( admin_url( 'admin.php?page=evf-builder&tab=fields&form_id=' . self::$form_id ) ) . '" class="post-edit-link">' . esc_html__( 'Edit Form', 'everest-forms' ) . '</a>'; |
| 70 | } |
| 71 | |
| 72 | return $link; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * A list of template candidates. |
| 77 | * |
| 78 | * @param array $templates A list of template candidates, in descending order of priority. |
| 79 | * |
| 80 | * @return array |
| 81 | */ |
| 82 | public static function template_include( $templates ) { |
| 83 | $templates = apply_filters( 'everest_forms_templates_includes', array( 'page.php', 'single.php', 'index.php' ), $templates ); |
| 84 | return $templates; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Load a template. |
| 89 | * |
| 90 | * Handles template usage so that we can use our own templates instead of the themes. |
| 91 | * |
| 92 | * Templates are in the 'templates' folder. everest-forms looks for theme. |
| 93 | * overrides in /theme/everest-forms/ by default. |
| 94 | * |
| 95 | * For beginners, it also looks for a everest-forms.php template first. If the user adds. |
| 96 | * this to the theme (containing a everest-forms() inside) this will be used for all. |
| 97 | * everest-forms templates. |
| 98 | * |
| 99 | * @param string $template Template to load. |
| 100 | * @return string |
| 101 | */ |
| 102 | public static function template_loader( $template ) { |
| 103 | if ( is_embed() ) { |
| 104 | return $template; |
| 105 | } |
| 106 | |
| 107 | $default_file = self::get_template_loader_default_file(); |
| 108 | |
| 109 | if ( $default_file ) { |
| 110 | /** |
| 111 | * Filter hook to choose which files to find before EverestForms does it's own logic. |
| 112 | * |
| 113 | * @since 1.0.0 |
| 114 | * @var array |
| 115 | */ |
| 116 | $search_files = self::get_template_loader_files( $default_file ); |
| 117 | $template = locate_template( $search_files ); |
| 118 | |
| 119 | if ( ! $template || EVF_TEMPLATE_DEBUG_MODE ) { |
| 120 | $template = evf()->plugin_path() . '/templates/' . $default_file; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return $template; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get the default filename for a template. |
| 129 | * |
| 130 | * @since 1.0.0 |
| 131 | * @return string |
| 132 | */ |
| 133 | private static function get_template_loader_default_file() { |
| 134 | return ''; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get an array of filenames to search for a given template. |
| 139 | * |
| 140 | * @since 1.0.0 |
| 141 | * @param string $default_file The default file name. |
| 142 | * @return string[] |
| 143 | */ |
| 144 | private static function get_template_loader_files( $default_file ) { |
| 145 | $search_files = apply_filters( 'everest_forms_template_loader_files', array(), $default_file ); |
| 146 | $search_files[] = 'everest-forms.php'; |
| 147 | |
| 148 | if ( is_page_template() ) { |
| 149 | $search_files[] = get_page_template_slug(); |
| 150 | } |
| 151 | |
| 152 | $search_files[] = $default_file; |
| 153 | $search_files[] = evf()->template_path() . $default_file; |
| 154 | |
| 155 | return array_unique( $search_files ); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | |-------------------------------------------------------------------------- |
| 160 | | Form Preview Handling |
| 161 | |-------------------------------------------------------------------------- |
| 162 | */ |
| 163 | |
| 164 | /** |
| 165 | * Hook in methods to enhance the form preview. |
| 166 | */ |
| 167 | public static function form_preview_init() { |
| 168 | if ( ! is_user_logged_in() || is_admin() ) { |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | add_filter( 'template_include', array( __CLASS__, 'evf_form_preview_template' ), PHP_INT_MAX ); |
| 173 | // if ( 0 < self::$form_id ) { |
| 174 | // add_filter( 'the_title', array( __CLASS__, 'form_preview_title_filter' ), 100, 1 ); |
| 175 | // add_filter( 'the_content', array( __CLASS__, 'form_preview_content_filter' ), 999 ); |
| 176 | // add_filter( 'get_the_excerpt', array( __CLASS__, 'form_preview_content_filter' ), 999 ); |
| 177 | // add_filter( 'post_thumbnail_html', '__return_empty_string' ); |
| 178 | // } |
| 179 | } |
| 180 | |
| 181 | public static function evf_form_preview_template( $evf_form_preview_template ) { |
| 182 | if ( is_embed() ) { |
| 183 | return $evf_form_preview_template; |
| 184 | } |
| 185 | |
| 186 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 187 | wp_register_style( 'evf-form-preview-style', evf()->plugin_url() . '/assets/css/evf-form-preview.css', array(), EVF_VERSION ); |
| 188 | wp_enqueue_style( 'evf-form-preview-style' ); |
| 189 | |
| 190 | wp_register_script( 'evf-form-preview-script', evf()->plugin_url() . '/assets/js/frontend/evf-form-preview' . $suffix . '.js', array( 'jquery', 'tooltipster' ), EVF_VERSION ); |
| 191 | wp_enqueue_script( 'evf-form-preview-script' ); |
| 192 | wp_register_script( 'tooltipster', evf()->plugin_url() . '/assets/js/tooltipster/tooltipster.bundle' . $suffix . '.js', array( 'jquery' ), '4.6.2', true ); |
| 193 | |
| 194 | wp_localize_script( |
| 195 | 'evf-form-preview-script', |
| 196 | 'everest_forms_form_preview ', |
| 197 | array( |
| 198 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 199 | 'form_preview_nonce' => wp_create_nonce( 'evf_form_preview_nonce' ), |
| 200 | 'pro_upgrade_link' => esc_url( 'https://everestforms.net/upgrade/?utm_medium=evf-form-preview&utm_source=evf-free&utm_campaign=preview-sidebar-btn&utm_content=Upgrade%20to%20Pro' ), |
| 201 | ) |
| 202 | ); |
| 203 | |
| 204 | ob_start(); |
| 205 | if ( is_user_logged_in() && isset( $_GET['form_id'] ) ) { |
| 206 | self::generate_form_preview(); |
| 207 | } |
| 208 | |
| 209 | $form_content = ob_get_clean(); |
| 210 | ob_start(); |
| 211 | self::side_panel_content(); |
| 212 | |
| 213 | $side_panel_content = ob_get_clean(); |
| 214 | $template = evf_get_template( |
| 215 | 'form-preview/evf-form-preview-template.php', |
| 216 | array( |
| 217 | 'form_content' => $form_content, |
| 218 | 'side_panel_content' => $side_panel_content, |
| 219 | ) |
| 220 | ); |
| 221 | |
| 222 | return $template; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | |-------------------------------------------------------------------------- |
| 227 | | Email Preview Handling |
| 228 | |-------------------------------------------------------------------------- |
| 229 | */ |
| 230 | |
| 231 | /** |
| 232 | * Hook in methods to enhance the form preview. |
| 233 | */ |
| 234 | public static function email_preview_init() { |
| 235 | if ( ! is_user_logged_in() || is_admin() ) { |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | $email_preview = evf()->plugin_path() . '/templates/emails/email-preview.php'; |
| 240 | return $email_preview; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Filter the title and insert form preview title. |
| 245 | * |
| 246 | * @param string $title Existing title. |
| 247 | * @return string |
| 248 | */ |
| 249 | public static function form_preview_title_filter( $title ) { |
| 250 | $form = evf()->form->get( |
| 251 | self::$form_id, |
| 252 | array( |
| 253 | 'content_only' => true, |
| 254 | ) |
| 255 | ); |
| 256 | |
| 257 | if ( ! empty( $form['settings']['form_title'] ) && in_the_loop() ) { |
| 258 | if ( is_customize_preview() ) { |
| 259 | return esc_html( sanitize_text_field( $form['settings']['form_title'] ) ); |
| 260 | } |
| 261 | |
| 262 | /* translators: %s - Form name. */ |
| 263 | return sprintf( esc_html__( '%s – Preview', 'everest-forms' ), sanitize_text_field( $form['settings']['form_title'] ) ); |
| 264 | } |
| 265 | |
| 266 | return $title; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Filter the content and insert form preview content. |
| 271 | * |
| 272 | * @param string $content Existing post content. |
| 273 | * @return string |
| 274 | */ |
| 275 | public static function form_preview_content_filter( $content ) { |
| 276 | if ( ! is_user_logged_in() || ! is_main_query() ) { |
| 277 | return $content; |
| 278 | } |
| 279 | |
| 280 | self::$in_content_filter = true; |
| 281 | |
| 282 | // Remove the filter we're in to avoid nested calls. |
| 283 | remove_filter( 'the_content', array( __CLASS__, 'form_preview_content_filter' ) ); |
| 284 | |
| 285 | if ( current_user_can( 'everest_forms_view_forms', self::$form_id ) ) { |
| 286 | if ( function_exists( 'apply_shortcodes' ) ) { |
| 287 | $content = apply_shortcodes( '[everest_form id="' . absint( self::$form_id ) . '"]' ); |
| 288 | } else { |
| 289 | $content = do_shortcode( '[everest_form id="' . absint( self::$form_id ) . '"]' ); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | self::$in_content_filter = false; |
| 294 | |
| 295 | return $content; |
| 296 | } |
| 297 | |
| 298 | |
| 299 | /** |
| 300 | * Handles the preview of form. |
| 301 | * |
| 302 | * @since 3.2.2 |
| 303 | */ |
| 304 | public static function generate_form_preview() { |
| 305 | if ( ! is_user_logged_in() ) { |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | if ( isset( $_GET['form_id'] ) ) { |
| 310 | $form_id = absint( $_GET['form_id'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 311 | |
| 312 | $html = ''; |
| 313 | $html .= '<div class="evf-preview-content">'; |
| 314 | $html .= '<span class="evf-form-preview-title">'; |
| 315 | $html .= esc_html( get_the_title( $form_id ) ); |
| 316 | $html .= '</span>'; |
| 317 | |
| 318 | $shortcode = sprintf( '[everest_form id="%d"]', $form_id ); |
| 319 | |
| 320 | if ( function_exists( 'apply_shortcodes' ) ) { |
| 321 | $content = apply_shortcodes( $shortcode ); |
| 322 | } else { |
| 323 | $content = do_shortcode( $shortcode ); |
| 324 | } |
| 325 | $html .= $content; |
| 326 | $html .= '</div>'; |
| 327 | |
| 328 | echo $html; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Side panel content. |
| 334 | * |
| 335 | * @since 4.0 |
| 336 | */ |
| 337 | public static function side_panel_content() { |
| 338 | |
| 339 | $is_pro_active = is_plugin_active( 'everest-forms-pro/everest-forms-pro.php' ); |
| 340 | if ( ! $is_pro_active ) { |
| 341 | $heading = esc_html__( 'Our Pro Features.', 'everest-forms' ); |
| 342 | $pro_features = array( |
| 343 | esc_html__( 'Stripe & PayPal Integration', 'everest-forms' ), |
| 344 | esc_html__( 'Style Export & Import', 'everest-forms' ), |
| 345 | esc_html__( 'Conditional Email Routing', 'everest-forms' ), |
| 346 | esc_html__( 'Advanced Form Fields', 'everest-forms' ), |
| 347 | esc_html__( 'Quiz & Survey Forms', 'everest-forms' ), |
| 348 | esc_html__( '40+ Integrations', 'everest-forms' ), |
| 349 | esc_html__( 'Multi-Step Forms', 'everest-forms' ), |
| 350 | esc_html__( 'SMS Notifications', 'everest-forms' ), |
| 351 | esc_html__( 'Calculated Fields', 'everest-forms' ), |
| 352 | |
| 353 | ); |
| 354 | } else { |
| 355 | $heading = esc_html__( 'Our Top Addons', 'everest-forms' ); |
| 356 | $pro_features = array( |
| 357 | esc_html__( 'Style Customizer', 'everest-forms' ), |
| 358 | esc_html__( 'Custom Captcha', 'everest-forms' ), |
| 359 | esc_html__( 'Multi Part Forms', 'everest-forms' ), |
| 360 | esc_html__( 'PDF Form Submission', 'everest-forms' ), |
| 361 | esc_html__( 'Email Templates', 'everest-forms' ), |
| 362 | esc_html__( 'Survey, Polls and Quiz', 'everest-forms' ), |
| 363 | esc_html__( 'Paypal Standard', 'everest-forms' ), |
| 364 | esc_html__( 'Repeater Fields', 'everest-forms' ), |
| 365 | ); |
| 366 | } |
| 367 | $is_theme_style = get_post_meta( $_GET['form_id'], 'everest_forms_enable_theme_style', true ); |
| 368 | if ( 'default' === $is_theme_style ) { |
| 369 | $checked = ''; |
| 370 | $data_theme = 'default'; |
| 371 | } else { |
| 372 | $checked = 'checked'; |
| 373 | $data_theme = 'theme'; |
| 374 | } |
| 375 | $html = ''; |
| 376 | $html .= '<div class="evf-from-preview-theme-toggle">'; |
| 377 | $html .= '<label class="evf-form-preview-toggle-title">' . esc_html__( 'Apply Theme Style', 'everest-forms' ) . '</label>'; |
| 378 | $html .= '<span class="evf-form-preview-toggle-theme-preview">'; |
| 379 | $html .= '<input type="checkbox" class="evf-form-preview-theme-toggle-checkbox input-checkbox " id="evf_toggle_form_preview_theme" ' . $checked . '>'; |
| 380 | $html .= '<span class="slider round"></span>'; |
| 381 | $html .= '</span>'; |
| 382 | $html .= '</div>'; |
| 383 | $html .= '<div class="evf-form-preview-save hidden" id="evf-form-save" data-theme="' . $data_theme . '" data-id="' . absint( $_GET['form_id'] ) . '">'; |
| 384 | $html .= '<img src="' . esc_url( evf()->plugin_url() . '/assets/images/save-frame.svg' ) . '" alt="Save">'; |
| 385 | $html .= '<div class="evf-form-preview-save-title">' . esc_html__( 'Save', 'everest-forms' ) . '</div>'; |
| 386 | $html .= '</div>'; |
| 387 | $html .= '<div class="evf-form-preview-pro-features">'; |
| 388 | $html .= '<p class="evf-form-preview-pro-features-title">' . esc_html__( $heading, 'everest-forms' ) . '</p>'; |
| 389 | foreach ( $pro_features as $list ) { |
| 390 | $html .= '<div class="evf-form-preview-sidebar__body--list-item">'; |
| 391 | |
| 392 | $html .= '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 18 18" fill="none"> |
| 393 | <path d="M15 5.25L6.75 13.5L3 9.75" stroke="#4CC741" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/> |
| 394 | </svg>'; |
| 395 | |
| 396 | $html .= '<span>'; |
| 397 | $html .= wp_kses_post( $list ); |
| 398 | $html .= '</span>'; |
| 399 | $html .= '</div>'; |
| 400 | |
| 401 | } |
| 402 | if ( ! $is_pro_active ) { |
| 403 | $html .= '<div class="evf-form-preview-upgrade id="evf-form-save" data-theme="default" ">'; |
| 404 | $html .= '<img src="' . esc_url( evf()->plugin_url() . '/assets/images/upgrade-boost-icon.svg' ) . '" alt="Upgrade">'; |
| 405 | $html .= '<div class="evf-form-preview-upgrade-title">Upgrade to Pro</div>'; |
| 406 | $html .= '</div>'; |
| 407 | } |
| 408 | |
| 409 | echo $html; // phpcs:ignore |
| 410 | |
| 411 | ?> |
| 412 | <?php |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | add_action( 'init', array( 'EVF_Template_Loader', 'init' ) ); |
| 417 |