| 1 |
<?php |
| 2 |
|
| 3 |
namespace King_Addons; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Shared Form Builder checks for public AJAX requests. |
| 11 |
*/ |
| 12 |
class Form_Builder_Security |
| 13 |
{ |
| 14 |
/** |
| 15 |
* Checks whether a published document contains the Form Builder widget. |
| 16 |
* |
| 17 |
* @param mixed $page_id Page or post ID from the request. |
| 18 |
* @return bool True when the document is published and includes Form Builder. |
| 19 |
*/ |
| 20 |
public static function page_has_form_builder($page_id): bool |
| 21 |
{ |
| 22 |
$page_id = absint($page_id); |
| 23 |
if ($page_id <= 0) { |
| 24 |
return false; |
| 25 |
} |
| 26 |
|
| 27 |
$post = get_post($page_id); |
| 28 |
if (!$post || 'publish' !== $post->post_status) { |
| 29 |
return false; |
| 30 |
} |
| 31 |
|
| 32 |
$elementor_data = get_post_meta($page_id, '_elementor_data', true); |
| 33 |
if (empty($elementor_data)) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
if (!is_string($elementor_data)) { |
| 38 |
$elementor_data = wp_json_encode($elementor_data); |
| 39 |
} |
| 40 |
|
| 41 |
return false !== strpos($elementor_data, 'king-addons-form-builder'); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Validates a page ID for public form submissions. |
| 46 |
* |
| 47 |
* Accepts a published page that contains Form Builder. Also accepts a |
| 48 |
* published source page or template when the widget is injected by Theme |
| 49 |
* Builder, Header/Footer, or a popup and is therefore not stored on the |
| 50 |
* queried page. |
| 51 |
* |
| 52 |
* @param mixed $page_id Page or post ID from the request. |
| 53 |
* @return bool True when the page ID is safe to store with a submission. |
| 54 |
*/ |
| 55 |
public static function is_valid_submission_page($page_id): bool |
| 56 |
{ |
| 57 |
if (self::page_has_form_builder($page_id)) { |
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
$page_id = absint($page_id); |
| 62 |
if ($page_id <= 0) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
$post = get_post($page_id); |
| 67 |
if (!$post || 'publish' !== $post->post_status) { |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
if (is_post_publicly_viewable($post)) { |
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
return in_array( |
| 76 |
$post->post_type, |
| 77 |
['elementor_library', 'king-addons-el-hf', 'king_addons_popup'], |
| 78 |
true |
| 79 |
); |
| 80 |
} |
| 81 |
} |
| 82 |
|