PluginProbe
The Innovative Form Builder – IvyForms / 1.0
The Innovative Form Builder – IvyForms v1.0
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.0, at backend/src/Services/Preview/PreviewService.php

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