PluginProbe
The Innovative Form Builder – IvyForms / 1.4.1
The Innovative Form Builder – IvyForms v1.4.1
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Services / Preview / PreviewService.php

PreviewService.php in The Innovative Form Builder – IvyForms 1.4.1, at backend/src/Services/Preview/PreviewService.php

220 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Services\Preview;
4
5 use IvyForms\Common\Exceptions\ForbiddenException;
6 use IvyForms\Common\Sanitizer\Sanitizer;
7 use IvyForms\Services\API\IvyFormsAPI;
8 use IvyForms\Services\Permissions\IvyFormsAccess;
9 use stdClass;
10 use WP_Post;
11
12 /**
13 * Class PreviewService
14 *
15 * @package IvyForms\Services\Preview
16 *
17 * This class handles the preview functionality of forms in the IvyForms plugin.
18 * It sets up a fake post to display the form preview and manages the template redirection.
19 */
20 class PreviewService
21 {
22 protected int $formId = 0;
23
24 /**
25 * Get the form ID
26 *
27 * @return int Form ID
28 */
29 public function getFormId(): int
30 {
31 return $this->formId;
32 }
33
34 /**
35 * Set the form ID
36 *
37 * @param int $formId Form ID
38 */
39 public function setFormId(int $formId): void
40 {
41 $this->formId = $formId;
42 }
43
44 /**
45 * @throws ForbiddenException
46 */
47 public function __construct()
48 {
49 if ($this->isPreview()) {
50 $this->init();
51 }
52 }
53
54 /**
55 * Initialize the preview service
56 *
57 * This function sets up the necessary hooks and filters for the preview functionality.
58 * @throws ForbiddenException
59 */
60 public function init(): void
61 {
62 $this->checkFormId();
63 remove_all_filters('the_content');
64 remove_all_filters('get_the_excerpt');
65
66 add_action('template_redirect', array($this, 'templateRedirect'));
67 add_filter('template_include', array($this, 'templateInclude'), 1000);
68
69 add_filter('post_thumbnail_html', function () {
70 return '';
71 });
72 }
73
74 /**
75 * Check if the current request is for a form preview
76 *
77 * @SuppressWarnings(PHPMD)
78 *
79 * @return bool
80 */
81 public function isPreview(): bool
82 {
83 return isset($_GET['ivyforms_preview']);
84 }
85
86 /**
87 * Check if the form ID is valid and the user has permission to view it
88 *
89 * @SuppressWarnings(PHPMD)
90 * @throws ForbiddenException
91 */
92 public function checkFormId(): bool
93 {
94 // Verify the nonce
95 Sanitizer::verifyNonce($_GET['_wpNonce'] ?? '');
96
97 // Use WordPress functions to safely get and sanitize input
98 $formId = absint(sanitize_text_field(wp_unslash($_GET['ivyforms_preview'])));
99 if ($formId === 0) {
100 wp_die(
101 esc_html__('Sorry, the form ID for this preview does not exist.', 'ivyforms'),
102 esc_html__('You need to check that the right form ID exists.', 'ivyforms'),
103 403
104 );
105 }
106
107 if (!IvyFormsAccess::canAccessFormPreview()) {
108 wp_die(
109 esc_html__('Sorry, you are not allowed to visit this preview form as this user.', 'ivyforms'),
110 esc_html__('You need a higher level of permission.', 'ivyforms'),
111 403
112 );
113 }
114
115 $this->setFormId($formId);
116
117 if (IvyFormsAPI::formExists($this->getFormId()) === false) {
118 wp_die(
119 esc_html__('Sorry, the form ID for this preview does not exist.', 'ivyforms'),
120 esc_html__('You need to check that the right form ID exists.', 'ivyforms'),
121 403
122 );
123 }
124 return true;
125 }
126
127 /**
128 * Redirect to the template for form preview
129 *
130 * This function sets up a fake post and updates the
131 * main query to display the form preview.
132 *
133 * @SuppressWarnings(PHPMD)
134 */
135 public function templateRedirect(): void
136 {
137 global $wp, $wp_query;
138
139 // Set post ID
140 $postId = 0;
141
142 // Post constructor
143 $post = new stdClass();
144 $post->ID = $postId;
145 $post->post_author = 1;
146 $post->post_date = current_time('mysql');
147 $post->post_date_gmt = current_time('mysql', 1);
148 $post->post_title = __('Preview form', 'ivyforms');
149 $post->post_content = sprintf(
150 '<a href="%s" class="ivyforms-edit-form-link">Edit form</a>%s',
151 esc_url(admin_url('admin.php?page=ivyforms-builder#/manage/' . $this->getFormId())),
152 do_shortcode(
153 sprintf(
154 '[%s id="%d"]',
155 'ivyforms',
156 $this->getFormId()
157 )
158 )
159 );
160 $post->post_status = 'publish';
161 $post->comment_status = 'closed';
162 $post->ping_status = 'closed';
163 $post->post_name = 'ivyforms-post-preview';
164 $post->post_type = 'page';
165 $post->filter = 'raw';
166
167 // Create fake post
168 $wpPost = new WP_Post($post);
169
170 // Add post to cache
171 wp_cache_add($postId, $wpPost, 'posts');
172
173 // Update the main query
174 $wp_query->post = $wpPost;
175 $wp_query->posts = array($wpPost);
176 $wp_query->posts_per_page = 1;
177 $wp_query->queried_object = $wpPost;
178 $wp_query->queried_object_id = $postId;
179 $wp_query->found_posts = 1;
180 $wp_query->post_count = 1;
181 $wp_query->max_num_pages = 1;
182 $wp_query->is_page = true;
183 $wp_query->is_singular = true;
184 $wp_query->is_single = false;
185 $wp_query->is_attachment = false;
186 $wp_query->is_archive = false;
187 $wp_query->is_category = false;
188 $wp_query->is_tag = false;
189 $wp_query->is_tax = false;
190 $wp_query->is_author = false;
191 $wp_query->is_date = false;
192 $wp_query->is_year = false;
193 $wp_query->is_month = false;
194 $wp_query->is_day = false;
195 $wp_query->is_time = false;
196 $wp_query->is_search = false;
197 $wp_query->is_feed = false;
198 $wp_query->is_comment_feed = false;
199 $wp_query->is_trackback = false;
200 $wp_query->is_home = false;
201 $wp_query->is_embed = false;
202 $wp_query->is_404 = false;
203 $wp_query->is_paged = false;
204 $wp_query->is_admin = false;
205 $wp_query->is_preview = false;
206 $wp_query->is_robots = false;
207 $wp_query->is_posts_page = false;
208 $wp_query->is_post_type_archive = false;
209
210 // Update globals using proper WordPress functions
211 $GLOBALS['wp_query'] = $wp_query;
212 $wp->register_globals();
213 }
214
215 public function templateInclude(): string
216 {
217 return IVYFORMS_PATH . '/view/backend/preview.php';
218 }
219 }
220