abstracts
4 years ago
admin
4 years ago
elementor
4 years ago
export
4 years ago
fields
4 years ago
interfaces
8 years ago
libraries
7 years ago
log-handlers
4 years ago
shortcodes
4 years ago
templates
5 years ago
class-everest-forms.php
4 years ago
class-evf-ajax.php
4 years ago
class-evf-autoloader.php
7 years ago
class-evf-background-updater.php
7 years ago
class-evf-cache-helper.php
6 years ago
class-evf-deprecated-action-hooks.php
6 years ago
class-evf-deprecated-filter-hooks.php
5 years ago
class-evf-emails.php
5 years ago
class-evf-fields.php
6 years ago
class-evf-form-block.php
4 years ago
class-evf-form-handler.php
4 years ago
class-evf-form-task.php
4 years ago
class-evf-forms-features.php
4 years ago
class-evf-frontend-scripts.php
4 years ago
class-evf-install.php
5 years ago
class-evf-integrations.php
7 years ago
class-evf-log-levels.php
8 years ago
class-evf-logger.php
5 years ago
class-evf-post-types.php
5 years ago
class-evf-privacy.php
6 years ago
class-evf-session-handler.php
7 years ago
class-evf-shortcodes.php
4 years ago
class-evf-smart-tags.php
4 years ago
class-evf-template-loader.php
4 years ago
class-evf-validation.php
6 years ago
evf-conditional-functions.php
6 years ago
evf-core-functions.php
4 years ago
evf-deprecated-functions.php
6 years ago
evf-entry-functions.php
4 years 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
229 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 | } else { |
| 43 | add_filter( 'template_include', array( __CLASS__, 'template_loader' ) ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Hook into pre_get_posts to limit posts. |
| 49 | * |
| 50 | * @param WP_Query $q Query instance. |
| 51 | */ |
| 52 | public static function pre_get_posts( $q ) { |
| 53 | // Limit one post to query. |
| 54 | if ( $q->is_main_query() ) { |
| 55 | $q->set( 'posts_per_page', 1 ); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Change edit link of preview page. |
| 61 | * |
| 62 | * @param string $link Edit post link. |
| 63 | */ |
| 64 | public static function edit_form_link( $link ) { |
| 65 | if ( 0 < self::$form_id ) { |
| 66 | 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>'; |
| 67 | } |
| 68 | |
| 69 | return $link; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * A list of template candidates. |
| 74 | * |
| 75 | * @param array $templates A list of template candidates, in descending order of priority. |
| 76 | * |
| 77 | * @return array |
| 78 | */ |
| 79 | public static function template_include( $templates ) { |
| 80 | return array( 'page.php', 'single.php', 'index.php' ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Load a template. |
| 85 | * |
| 86 | * Handles template usage so that we can use our own templates instead of the themes. |
| 87 | * |
| 88 | * Templates are in the 'templates' folder. everest-forms looks for theme. |
| 89 | * overrides in /theme/everest-forms/ by default. |
| 90 | * |
| 91 | * For beginners, it also looks for a everest-forms.php template first. If the user adds. |
| 92 | * this to the theme (containing a everest-forms() inside) this will be used for all. |
| 93 | * everest-forms templates. |
| 94 | * |
| 95 | * @param string $template Template to load. |
| 96 | * @return string |
| 97 | */ |
| 98 | public static function template_loader( $template ) { |
| 99 | if ( is_embed() ) { |
| 100 | return $template; |
| 101 | } |
| 102 | |
| 103 | $default_file = self::get_template_loader_default_file(); |
| 104 | |
| 105 | if ( $default_file ) { |
| 106 | /** |
| 107 | * Filter hook to choose which files to find before EverestForms does it's own logic. |
| 108 | * |
| 109 | * @since 1.0.0 |
| 110 | * @var array |
| 111 | */ |
| 112 | $search_files = self::get_template_loader_files( $default_file ); |
| 113 | $template = locate_template( $search_files ); |
| 114 | |
| 115 | if ( ! $template || EVF_TEMPLATE_DEBUG_MODE ) { |
| 116 | $template = evf()->plugin_path() . '/templates/' . $default_file; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return $template; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get the default filename for a template. |
| 125 | * |
| 126 | * @since 1.0.0 |
| 127 | * @return string |
| 128 | */ |
| 129 | private static function get_template_loader_default_file() { |
| 130 | return ''; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Get an array of filenames to search for a given template. |
| 135 | * |
| 136 | * @since 1.0.0 |
| 137 | * @param string $default_file The default file name. |
| 138 | * @return string[] |
| 139 | */ |
| 140 | private static function get_template_loader_files( $default_file ) { |
| 141 | $search_files = apply_filters( 'everest_forms_template_loader_files', array(), $default_file ); |
| 142 | $search_files[] = 'everest-forms.php'; |
| 143 | |
| 144 | if ( is_page_template() ) { |
| 145 | $search_files[] = get_page_template_slug(); |
| 146 | } |
| 147 | |
| 148 | $search_files[] = $default_file; |
| 149 | $search_files[] = evf()->template_path() . $default_file; |
| 150 | |
| 151 | return array_unique( $search_files ); |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | |-------------------------------------------------------------------------- |
| 156 | | Form Preview Handling |
| 157 | |-------------------------------------------------------------------------- |
| 158 | */ |
| 159 | |
| 160 | /** |
| 161 | * Hook in methods to enhance the form preview. |
| 162 | */ |
| 163 | public static function form_preview_init() { |
| 164 | if ( ! is_user_logged_in() || is_admin() ) { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | if ( 0 < self::$form_id ) { |
| 169 | add_filter( 'the_title', array( __CLASS__, 'form_preview_title_filter' ) ); |
| 170 | add_filter( 'the_content', array( __CLASS__, 'form_preview_content_filter' ) ); |
| 171 | add_filter( 'get_the_excerpt', array( __CLASS__, 'form_preview_content_filter' ) ); |
| 172 | add_filter( 'post_thumbnail_html', '__return_empty_string' ); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Filter the title and insert form preview title. |
| 178 | * |
| 179 | * @param string $title Existing title. |
| 180 | * @return string |
| 181 | */ |
| 182 | public static function form_preview_title_filter( $title ) { |
| 183 | $form = evf()->form->get( |
| 184 | self::$form_id, |
| 185 | array( |
| 186 | 'content_only' => true, |
| 187 | ) |
| 188 | ); |
| 189 | |
| 190 | if ( ! empty( $form['settings']['form_title'] ) && in_the_loop() ) { |
| 191 | if ( is_customize_preview() ) { |
| 192 | return esc_html( sanitize_text_field( $form['settings']['form_title'] ) ); |
| 193 | } |
| 194 | |
| 195 | /* translators: %s - Form name. */ |
| 196 | return sprintf( esc_html__( '%s – Preview', 'everest-forms' ), sanitize_text_field( $form['settings']['form_title'] ) ); |
| 197 | } |
| 198 | |
| 199 | return $title; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Filter the content and insert form preview content. |
| 204 | * |
| 205 | * @param string $content Existing post content. |
| 206 | * @return string |
| 207 | */ |
| 208 | public static function form_preview_content_filter( $content ) { |
| 209 | if ( ! is_user_logged_in() || ! is_main_query() || ! in_the_loop() ) { |
| 210 | return $content; |
| 211 | } |
| 212 | |
| 213 | self::$in_content_filter = true; |
| 214 | |
| 215 | // Remove the filter we're in to avoid nested calls. |
| 216 | remove_filter( 'the_content', array( __CLASS__, 'form_preview_content_filter' ) ); |
| 217 | |
| 218 | if ( current_user_can( 'everest_forms_view_forms', self::$form_id ) ) { |
| 219 | $content = apply_shortcodes( '[everest_form id="' . absint( self::$form_id ) . '"]' ); |
| 220 | } |
| 221 | |
| 222 | self::$in_content_filter = false; |
| 223 | |
| 224 | return $content; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | add_action( 'init', array( 'EVF_Template_Loader', 'init' ) ); |
| 229 |