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