%PDF- %PDF-
| Direktori : /dom645759/wp-content/plugins/fusion-builder/shortcodes/form/ |
| Current File : //dom645759/wp-content/plugins/fusion-builder/shortcodes/form/rich-text.php |
<?php
/**
* Add an element to fusion-builder.
*
* @package fusion-builder
* @since 3.17
*/
if ( fusion_is_element_enabled( 'fusion_form_rich_text' ) ) {
if ( ! class_exists( 'FusionForm_Rich_Text' ) ) {
/**
* A text field with a formatting toolbar, submitting HTML (see AWB_Form_Rich_Text).
*
* @since 3.17
*/
class FusionForm_Rich_Text extends Fusion_Form_Component {
/**
* Constructor.
*
* @access public
* @since 3.17
*/
public function __construct() {
parent::__construct( 'fusion_form_rich_text' );
}
/**
* Gets the default values.
*
* @static
* @access public
* @since 3.17
* @return array
*/
public static function get_element_defaults() {
return [
'label' => '',
'name' => '',
'required' => '',
'empty_notice' => '',
'placeholder' => '',
'toolbar' => implode( ',', AWB_Form_Rich_Text::get_default_toolbar() ),
'upload_size' => '2',
'max_images' => '5',
'rows' => '6',
'minlength' => '0',
'maxlength' => '0',
'tab_index' => '',
'class' => '',
'id' => '',
'logics' => '',
'tooltip' => '',
];
}
/**
* The toolbar's buttons, in the tools' order: some tools have two.
*
* @static
* @access public
* @since 3.17
* @param array $tools The tools chosen.
* @return array Button command => [ label, icon ].
*/
public static function get_buttons( $tools ) {
$buttons = [
'bold' => [ 'bold', esc_attr__( 'Bold', 'fusion-builder' ), 'bold' ],
'italic' => [ 'italic', esc_attr__( 'Italic', 'fusion-builder' ), 'italic' ],
'color' => [ 'color', esc_attr__( 'Text Color', 'fusion-builder' ), 'text-color' ],
'h2' => [ 'heading', esc_attr__( 'Heading 2', 'fusion-builder' ), 'heading-2' ],
'h3' => [ 'heading', esc_attr__( 'Heading 3', 'fusion-builder' ), 'heading-3' ],
'h4' => [ 'heading', esc_attr__( 'Heading 4', 'fusion-builder' ), 'heading-4' ],
'ul' => [ 'lists', esc_attr__( 'Bulleted List', 'fusion-builder' ), 'list-ul' ],
'ol' => [ 'lists', esc_attr__( 'Numbered List', 'fusion-builder' ), 'list-ol' ],
'quote' => [ 'quote', esc_attr__( 'Quote', 'fusion-builder' ), 'quote' ],
'left' => [ 'align', esc_attr__( 'Align Left', 'fusion-builder' ), 'align-left' ],
'center' => [ 'align', esc_attr__( 'Align Center', 'fusion-builder' ), 'align-center' ],
'right' => [ 'align', esc_attr__( 'Align Right', 'fusion-builder' ), 'align-right' ],
'link' => [ 'link', esc_attr__( 'Link', 'fusion-builder' ), 'link' ],
'image' => [ 'image', esc_attr__( 'Add Image', 'fusion-builder' ), 'image' ],
'button' => [ 'button', esc_attr__( 'Add Button', 'fusion-builder' ), 'button' ],
'checklist' => [ 'checklist', esc_attr__( 'Checklist', 'fusion-builder' ), 'check-square-o' ],
];
// In the order the tools were chosen in.
$out = [];
foreach ( $tools as $tool ) {
foreach ( $buttons as $command => $button ) {
if ( $tool === $button[0] ) {
$out[ $command ] = [ $button[1], $button[2] ];
}
}
}
return $out;
}
/**
* Render form field html.
*
* @access public
* @since 3.17
* @param string $content The content.
* @return string
*/
public function render_input_field( $content ) {
$element_data = $this->create_element_data( $this->args );
$name = $this->args['name'];
$tools = array_filter( array_map( 'trim', explode( ',', $this->args['toolbar'] ) ) );
$images = in_array( 'image', $tools, true );
if ( '' !== $this->args['tooltip'] ) {
$element_data['label'] .= $this->get_field_tooltip( $this->args );
}
$toolbar = '';
foreach ( self::get_buttons( $tools ) as $command => $button ) {
$toolbar .= '<button type="button" class="awb-form-rich-text__tool" data-command="' . esc_attr( $command ) . '" title="' . esc_attr( $button[0] ) . '" aria-label="' . esc_attr( $button[0] ) . '"' . ( in_array( $command, [ 'image', 'button', 'link', 'color', 'more' ], true ) ? '' : ' aria-pressed="false"' ) . '>';
$toolbar .= '<i class="awb-icon-' . esc_attr( $button[1] ) . '" aria-hidden="true"></i>';
$toolbar .= '</button>';
}
$data = ' data-max-images="' . esc_attr( max( 1, (int) $this->args['max_images'] ) ) . '"';
$data .= ' data-upload-size="' . esc_attr( max( 0.1, (float) $this->args['upload_size'] ) ) . '"';
$data .= ' data-minlength="' . esc_attr( absint( $this->args['minlength'] ) ) . '"';
$data .= ' data-maxlength="' . esc_attr( absint( $this->args['maxlength'] ) ) . '"';
// The palette colours the text can have.
if ( in_array( 'color', $tools, true ) ) {
$colors = [];
foreach ( AWB_Form_Rich_Text::get_colors() as $key => $color ) {
$colors[] = [ $key, $color['label'] ];
}
$data .= ' data-colors="' . esc_attr( wp_json_encode( $colors ) ) . '"';
}
$placeholder = '' !== $this->args['placeholder'] ? $this->args['placeholder'] . ( '' !== $element_data['required_placeholder'] ? ' *' : '' ) : '';
$element_html = '<div class="awb-form-rich-text"' . $data . ' style="--awb-rich-text-rows:' . esc_attr( max( 2, (int) $this->args['rows'] ) ) . ';">';
$element_html .= $toolbar ? '<div class="awb-form-rich-text__toolbar" role="toolbar" aria-label="' . esc_attr__( 'Formatting', 'fusion-builder' ) . '" aria-controls="' . esc_attr( $name ) . '-editor">' . $toolbar . '</div>' : '';
$element_html .= '<div class="awb-form-rich-text__body">';
$element_html .= '<div id="' . esc_attr( $name ) . '-editor" class="awb-form-rich-text__editor" contenteditable="true" role="textbox" aria-multiline="true" aria-label="' . esc_attr( $this->args['label'] ? $this->args['label'] : $name ) . '"' . ( $placeholder ? ' data-placeholder="' . esc_attr( $placeholder ) . '"' : '' ) . ( '' !== $this->args['tab_index'] ? ' tabindex="' . esc_attr( $this->args['tab_index'] ) . '"' : '' ) . '></div>';
// The value, which the form submits and validates; the editor keeps it current.
$element_html .= '<textarea id="' . esc_attr( $name ) . '" name="' . esc_attr( $name ) . '"' . str_replace( 'class="fusion-form-input', 'class="fusion-form-input awb-form-rich-text__value', $element_data['class'] ) . ' tabindex="-1" aria-hidden="true"';
$element_html .= '' !== $element_data['empty_notice'] ? ' data-empty-notice="' . esc_attr( $element_data['empty_notice'] ) . '"' : '';
$element_html .= $element_data['required'] . $element_data['holds_private_data'] . '></textarea>';
$element_html .= '</div>';
// The images added, sent with the submission like an upload field's files.
if ( $images ) {
$element_html .= '<input type="file" class="awb-form-rich-text__files" name="' . esc_attr( AWB_Form_Rich_Text::images_name( $name ) ) . '[]" multiple hidden tabindex="-1" aria-hidden="true" />';
}
$element_html .= '</div>';
$html = '';
if ( 'above' === $this->params['form_meta']['label_position'] ) {
$html .= $element_data['label'] . $element_html;
} else {
$html .= $element_html . $element_data['label'];
}
return $html;
}
/**
* Sets the necessary scripts.
*
* @access public
* @since 3.17
* @return void
*/
public function on_first_render() {
Fusion_Dynamic_JS::enqueue_script(
'awb-form-rich-text',
FusionBuilder::$js_folder_url . '/general/awb-form-rich-text.js',
FusionBuilder::$js_folder_path . '/general/awb-form-rich-text.js',
[ 'jquery' ],
FUSION_BUILDER_VERSION,
true
);
Fusion_Dynamic_JS::localize_script(
'awb-form-rich-text',
'awbFormRichText',
[
'text_color' => esc_html__( 'Text Color', 'fusion-builder' ),
'no_color' => esc_html__( 'Default', 'fusion-builder' ),
'select_text' => esc_html__( 'Select the text to color first.', 'fusion-builder' ),
'link_text' => esc_html__( 'Link Text', 'fusion-builder' ),
'link_url' => esc_html__( 'Link URL', 'fusion-builder' ),
'button_text' => esc_html__( 'Button Text', 'fusion-builder' ),
'button_url' => esc_html__( 'Button Link', 'fusion-builder' ),
'image_alt' => esc_html__( 'Image Description', 'fusion-builder' ),
'apply' => esc_html__( 'Apply', 'fusion-builder' ),
'remove' => esc_html__( 'Remove', 'fusion-builder' ),
'cancel' => esc_html__( 'Cancel', 'fusion-builder' ),
'edit_button' => esc_html__( 'Edit Button', 'fusion-builder' ),
'edit_image' => esc_html__( 'Edit Image', 'fusion-builder' ),
'invalid_url' => esc_html__( 'Please enter a web address starting with http:// or https://.', 'fusion-builder' ),
/* translators: The image types. */
'image_type' => sprintf( esc_html__( 'Only these image types can be added: %s.', 'fusion-builder' ), 'JPG, PNG, GIF, WebP' ),
/* translators: The size in MB. */
'image_size' => esc_html__( 'Images can be at most %s MB.', 'fusion-builder' ),
/* translators: The number of images. */
'image_count' => esc_html__( 'You can add up to %s images.', 'fusion-builder' ),
/* translators: The number of characters. */
'min_length' => esc_html__( 'Please enter at least %s characters.', 'fusion-builder' ),
/* translators: The number of characters. */
'max_length' => esc_html__( 'Please enter at most %s characters.', 'fusion-builder' ),
]
);
}
/**
* Load base CSS.
*
* @access public
* @since 3.17
* @return void
*/
public function add_css_files() {
FusionBuilder()->add_element_css( FUSION_BUILDER_PLUGIN_DIR . 'assets/css/form/rich-text.min.css' );
}
}
}
new FusionForm_Rich_Text();
}
/**
* Map shortcode to Fusion Builder
*
* @since 3.17
*/
function fusion_form_rich_text() {
$image_dependency = [
[
'element' => 'toolbar',
'value' => 'image',
'operator' => 'contains',
],
];
fusion_builder_map(
fusion_builder_frontend_data(
'FusionForm_Rich_Text',
[
'name' => esc_attr__( 'Rich Text Field', 'fusion-builder' ),
'shortcode' => 'fusion_form_rich_text',
'icon' => 'fusiona-af-text-area',
'form_component' => true,
'preview' => FUSION_BUILDER_PLUGIN_DIR . 'inc/templates/previews/fusion-form-element-preview.php',
'preview_id' => 'fusion-builder-block-module-form-element-preview-template',
'params' => [
[
'type' => 'textfield',
'heading' => esc_attr__( 'Field Label', 'fusion-builder' ),
'description' => esc_attr__( 'Enter the label for the input field. This is how users will identify individual fields.', 'fusion-builder' ),
'param_name' => 'label',
'value' => '',
'placeholder' => true,
],
[
'type' => 'textfield',
'heading' => esc_attr__( 'Field Name', 'fusion-builder' ),
'description' => esc_attr__( 'Enter the field name. Please use only lowercase alphanumeric characters, dashes, and underscores.', 'fusion-builder' ),
'param_name' => 'name',
'value' => '',
'placeholder' => true,
],
[
'type' => 'radio_button_set',
'heading' => esc_attr__( 'Required Field', 'fusion-builder' ),
'description' => esc_attr__( 'Make a selection to ensure that this field is completed before allowing the user to submit the form.', 'fusion-builder' ),
'param_name' => 'required',
'default' => 'no',
'value' => [
'yes' => esc_attr__( 'Yes', 'fusion-builder' ),
'no' => esc_attr__( 'No', 'fusion-builder' ),
],
],
[
'type' => 'textfield',
'heading' => esc_attr__( 'Empty Input Notice', 'fusion-builder' ),
'description' => esc_attr__( 'Enter text validation notice that should display if data input is empty.', 'fusion-builder' ),
'param_name' => 'empty_notice',
'value' => '',
'dependency' => [
[
'element' => 'required',
'value' => 'yes',
'operator' => '==',
],
],
],
[
'type' => 'textfield',
'heading' => esc_attr__( 'Placeholder Text', 'fusion-builder' ),
'param_name' => 'placeholder',
'value' => '',
'description' => esc_attr__( 'The placeholder text to display as hint for the input type.', 'fusion-builder' ),
],
[
'type' => 'textfield',
'heading' => esc_attr__( 'Tooltip Text', 'fusion-builder' ),
'param_name' => 'tooltip',
'value' => '',
'description' => esc_attr__( 'The text to display as tooltip hint for the input.', 'fusion-builder' ),
],
[
'type' => 'connected_sortable',
'heading' => esc_attr__( 'Toolbar', 'fusion-builder' ),
'description' => esc_attr__( 'Choose the tools the field offers, and their order. Images are uploaded with the form, buttons link to a web address. Formatting that isn\'t offered can\'t be submitted either.', 'fusion-builder' ),
'param_name' => 'toolbar',
'choices' => AWB_Form_Rich_Text::get_tools(),
'default' => implode( ',', AWB_Form_Rich_Text::get_default_toolbar() ),
'empty' => esc_html__( 'Drag tools here to remove them from the toolbar.', 'fusion-builder' ),
],
[
'type' => 'range',
'heading' => esc_attr__( 'Maximum Number Of Images', 'fusion-builder' ),
'description' => esc_attr__( 'Controls how many images can be added to the field.', 'fusion-builder' ),
'param_name' => 'max_images',
'value' => '5',
'min' => '1',
'max' => '20',
'step' => '1',
'dependency' => $image_dependency,
],
[
'type' => 'range',
'heading' => esc_attr__( 'Maximum Image Size', 'fusion-builder' ),
'description' => esc_attr__( 'Controls the largest image that can be added, in MB.', 'fusion-builder' ),
'param_name' => 'upload_size',
'value' => '2',
'min' => '1',
'max' => '20',
'step' => '1',
'dependency' => $image_dependency,
],
[
'type' => 'range',
'heading' => esc_html__( 'Field Height', 'fusion-builder' ),
'description' => esc_html__( 'Controls the height of the field, in rows of text. The field grows as text is added.', 'fusion-builder' ),
'param_name' => 'rows',
'value' => '6',
'min' => '2',
'max' => '30',
'step' => '1',
],
[
'type' => 'range',
'heading' => esc_attr__( 'Minimum Required Characters', 'fusion-builder' ),
'description' => esc_attr__( 'Controls the minimum number of characters of text that will be required for this field. Leave at 0 to have no minimum.', 'fusion-builder' ),
'param_name' => 'minlength',
'value' => '0',
'min' => '0',
'max' => '1000',
'step' => '5',
],
[
'type' => 'range',
'heading' => esc_attr__( 'Maximum Allowed Characters', 'fusion-builder' ),
'description' => esc_attr__( 'Controls the maximum number of characters of text that will be allowed for this field. Leave at 0 to have no maximum.', 'fusion-builder' ),
'param_name' => 'maxlength',
'value' => '0',
'min' => '0',
'max' => '20000',
'step' => '50',
],
[
'type' => 'textfield',
'heading' => esc_attr__( 'Tab Index', 'fusion-builder' ),
'param_name' => 'tab_index',
'value' => '',
'description' => esc_attr__( 'Tab index for the form field.', 'fusion-builder' ),
],
[
'type' => 'textfield',
'heading' => esc_attr__( 'CSS Class', 'fusion-builder' ),
'param_name' => 'class',
'value' => '',
'description' => esc_attr__( 'Add a class for the form field.', 'fusion-builder' ),
],
[
'type' => 'textfield',
'heading' => esc_attr__( 'CSS ID', 'fusion-builder' ),
'param_name' => 'id',
'value' => '',
'description' => esc_attr__( 'Add an ID for the form field.', 'fusion-builder' ),
],
'fusion_form_logics_placeholder' => [],
],
]
)
);
}
add_action( 'fusion_builder_before_init', 'fusion_form_rich_text' );