| 1 |
<?php |
| 2 |
defined('ABSPATH') || die(); |
| 3 |
|
| 4 |
class HashFormFieldHTML extends HashFormFieldType { |
| 5 |
|
| 6 |
protected $type = 'html'; |
| 7 |
|
| 8 |
public function field_settings_for_type() { |
| 9 |
return array( |
| 10 |
'default' => false, |
| 11 |
'required' => false, |
| 12 |
'label' => false, |
| 13 |
'description' => false, |
| 14 |
'field_alignment' => true, |
| 15 |
); |
| 16 |
} |
| 17 |
|
| 18 |
protected function extra_field_default_opts() { |
| 19 |
return array( |
| 20 |
'field_alignment' => 'left', |
| 21 |
); |
| 22 |
} |
| 23 |
|
| 24 |
public function show_primary_options() { |
| 25 |
$field = $this->get_field(); |
| 26 |
|
| 27 |
do_action('hashform_before_html_primary_option'); |
| 28 |
?> |
| 29 |
<div class="hf-form-row"> |
| 30 |
<label><?php esc_html_e('Content', 'hash-form'); ?></label> |
| 31 |
<div class="hf-form-text-editor"> |
| 32 |
<?php |
| 33 |
$args = array( |
| 34 |
'textarea_name' => 'field_options[description_' . absint($field['id']) . ']', |
| 35 |
'textarea_rows' => 8, |
| 36 |
); |
| 37 |
$html_id = 'hf-field-desc_' . absint($field['id']); |
| 38 |
|
| 39 |
/* |
| 40 |
* wp_editor() gives no way to put an attribute on the textarea |
| 41 |
* it prints, and the canvas has to know where to mirror what is |
| 42 |
* typed. handleTinyMceChange() already writes the editor back |
| 43 |
* to this textarea and fires change on it, so naming a target |
| 44 |
* here is all the live preview needs. |
| 45 |
*/ |
| 46 |
$preview_id = self::preview_id($field['id']); |
| 47 |
$add_target = function ($editor_html) use ($html_id, $preview_id) { |
| 48 |
return str_replace( |
| 49 |
'<textarea', |
| 50 |
'<textarea data-changeme="' . esc_attr($preview_id) . '"', |
| 51 |
$editor_html |
| 52 |
); |
| 53 |
}; |
| 54 |
|
| 55 |
add_filter('the_editor', $add_target); |
| 56 |
wp_editor($field['description'], $html_id, $args); |
| 57 |
remove_filter('the_editor', $add_target); |
| 58 |
?> |
| 59 |
</div> |
| 60 |
<p class="description"> |
| 61 |
<?php esc_html_e('Scripts and styles are removed when this is saved. Use the theme or a plugin for anything that has to run.', 'hash-form'); ?> |
| 62 |
</p> |
| 63 |
</div> |
| 64 |
<?php |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* The id of the block this field draws on the canvas. |
| 69 |
* |
| 70 |
* @param int $field_id |
| 71 |
* @return string |
| 72 |
*/ |
| 73 |
public static function preview_id($field_id) { |
| 74 |
return 'hf-html-preview-' . absint($field_id); |
| 75 |
} |
| 76 |
|
| 77 |
public function input_html() { |
| 78 |
$field = $this->get_field(); |
| 79 |
$content = isset($field['description']) ? $field['description'] : ''; |
| 80 |
$content = apply_filters('hashform_translate_string', $content, 'Hash Form', HashFormBuilder::get_form_title($field['form_id']) . ' - ' . $field['id'] . ' - ' . 'Field Description'); |
| 81 |
|
| 82 |
/* |
| 83 |
* Sanitized again on the way out, not only on the way in: rows saved |
| 84 |
* before that was done still hold whatever was pasted into them. |
| 85 |
*/ |
| 86 |
$content = HashFormHelper::sanitize_html_field_content($content); |
| 87 |
?> |
| 88 |
<div class="hf-custom-html-field"<?php echo is_admin() ? ' id="' . esc_attr(self::preview_id($field['id'])) . '" data-empty-text="' . esc_attr__('Custom HTML - nothing added yet', 'hash-form') . '"' : ''; ?>> |
| 89 |
<?php |
| 90 |
if ('' === trim(wp_strip_all_tags($content)) && is_admin()) { |
| 91 |
/* |
| 92 |
* The canvas used to show this whatever the field held, so |
| 93 |
* there was no way to see what you had written without saving |
| 94 |
* and looking at the page. What is drawn now is what the page |
| 95 |
* will draw, having been through the same sanitizer. |
| 96 |
*/ |
| 97 |
?> |
| 98 |
<div class="hf-custom-html-preview"> |
| 99 |
<?php esc_html_e('Custom HTML - nothing added yet', 'hash-form'); ?> |
| 100 |
</div> |
| 101 |
<?php |
| 102 |
} else { |
| 103 |
echo wp_kses_post($content); |
| 104 |
} |
| 105 |
?> |
| 106 |
</div> |
| 107 |
<?php |
| 108 |
} |
| 109 |
|
| 110 |
} |
| 111 |
|