| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\FormBuilder\Components; |
| 4 |
|
| 5 |
if (! defined('ABSPATH')) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
use FluentForm\App\Helpers\Helper; |
| 10 |
use FluentForm\App\Services\FormBuilder\BaseFieldManager; |
| 11 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 12 |
|
| 13 |
class CustomSubmitButton extends BaseFieldManager |
| 14 |
{ |
| 15 |
public function __construct() |
| 16 |
{ |
| 17 |
parent::__construct( |
| 18 |
'custom_submit_button', |
| 19 |
'Custom Submit Button', |
| 20 |
['submit', 'button', 'custom'], |
| 21 |
'advanced' |
| 22 |
); |
| 23 |
} |
| 24 |
|
| 25 |
public function pushFormInputType($types) |
| 26 |
{ |
| 27 |
return $types; |
| 28 |
} |
| 29 |
|
| 30 |
public function getComponent() |
| 31 |
{ |
| 32 |
return [ |
| 33 |
'index' => 12, |
| 34 |
'element' => $this->key, |
| 35 |
'attributes' => [ |
| 36 |
'class' => '', |
| 37 |
'type' => 'submit', |
| 38 |
], |
| 39 |
'settings' => [ |
| 40 |
'button_style' => '', |
| 41 |
'button_size' => 'md', |
| 42 |
'align' => 'left', |
| 43 |
'container_class' => '', |
| 44 |
'current_state' => 'normal_styles', |
| 45 |
'background_color' => 'rgb(64, 158, 255)', |
| 46 |
'color' => 'rgb(255, 255, 255)', |
| 47 |
'hover_styles' => (object) [ |
| 48 |
'backgroundColor' => '#ffffff', |
| 49 |
'borderColor' => '#1a7efb', |
| 50 |
'color' => '#1a7efb', |
| 51 |
'borderRadius' => '', |
| 52 |
'minWidth' => '100%', |
| 53 |
], |
| 54 |
'normal_styles' => (object) [ |
| 55 |
'backgroundColor' => '#1a7efb', |
| 56 |
'borderColor' => '#1a7efb', |
| 57 |
'color' => '#ffffff', |
| 58 |
'borderRadius' => '', |
| 59 |
'minWidth' => '100%', |
| 60 |
], |
| 61 |
'button_ui' => (object) [ |
| 62 |
'text' => 'Submit', |
| 63 |
'type' => 'default', |
| 64 |
'img_url' => '', |
| 65 |
], |
| 66 |
'conditional_logics' => [], |
| 67 |
], |
| 68 |
'editor_options' => [ |
| 69 |
'title' => $this->title, |
| 70 |
'icon_class' => 'dashicons dashicons-arrow-right-alt', |
| 71 |
'template' => 'customButton', |
| 72 |
], |
| 73 |
]; |
| 74 |
} |
| 75 |
|
| 76 |
public function pushConditionalSupport($conditonalItems) |
| 77 |
{ |
| 78 |
return $conditonalItems; |
| 79 |
} |
| 80 |
|
| 81 |
public function getGeneralEditorElements() |
| 82 |
{ |
| 83 |
return [ |
| 84 |
'btn_text', |
| 85 |
'button_ui', |
| 86 |
'button_style', |
| 87 |
'button_size', |
| 88 |
'align', |
| 89 |
]; |
| 90 |
} |
| 91 |
|
| 92 |
public function getAdvancedEditorElements() |
| 93 |
{ |
| 94 |
return [ |
| 95 |
'container_class', |
| 96 |
'class', |
| 97 |
'conditional_logics', |
| 98 |
]; |
| 99 |
} |
| 100 |
|
| 101 |
public function render($data, $form) |
| 102 |
{ |
| 103 |
// @todo: We will remove this in our next version [added: 4.0.0] |
| 104 |
if (class_exists('\FluentFormPro\Components\CustomSubmitField')) { |
| 105 |
return ''; |
| 106 |
} |
| 107 |
|
| 108 |
$hideSubmit = apply_filters_deprecated( |
| 109 |
'fluentform_is_hide_submit_btn_' . $form->id, |
| 110 |
[ |
| 111 |
'__return_true' |
| 112 |
], |
| 113 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 114 |
'fluentform/is_hide_submit_btn_' . $form->id, |
| 115 |
'Use fluentform/is_hide_submit_btn_' . $form->id . ' instead of fluentform_is_hide_submit_btn_' . $form->id |
| 116 |
); |
| 117 |
|
| 118 |
add_filter('fluentform/is_hide_submit_btn_' . $form->id, $hideSubmit); |
| 119 |
|
| 120 |
$elementName = $data['element']; |
| 121 |
|
| 122 |
$data = apply_filters_deprecated( |
| 123 |
'fluentform_rendering_field_data_' . $elementName, |
| 124 |
[ |
| 125 |
$data, |
| 126 |
$form |
| 127 |
], |
| 128 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 129 |
'fluentform/rendering_field_data_' . $elementName, |
| 130 |
'Use fluentform/rendering_field_data_' . $elementName . ' instead of fluentform_rendering_field_data_' . $elementName |
| 131 |
); |
| 132 |
|
| 133 |
$data = apply_filters('fluentform/rendering_field_data_' . $elementName, $data, $form); |
| 134 |
|
| 135 |
$btnStyle = ArrayHelper::get($data['settings'], 'button_style'); |
| 136 |
|
| 137 |
$btnSize = 'ff-btn-'; |
| 138 |
$btnSize .= isset($data['settings']['button_size']) ? $data['settings']['button_size'] : 'md'; |
| 139 |
$oldBtnType = isset($data['settings']['button_style']) ? '' : ' ff-btn-primary '; |
| 140 |
|
| 141 |
$align = 'ff-el-group ff-text-' . @$data['settings']['align']; |
| 142 |
|
| 143 |
$btnClasses = [ |
| 144 |
'ff-btn ff-btn-submit', |
| 145 |
$oldBtnType, |
| 146 |
$btnSize, |
| 147 |
$data['attributes']['class'], |
| 148 |
]; |
| 149 |
|
| 150 |
if ('no_style' == $btnStyle) { |
| 151 |
$btnClasses[] = 'ff_btn_no_style'; |
| 152 |
} else { |
| 153 |
$btnClasses[] = 'ff_btn_style'; |
| 154 |
} |
| 155 |
|
| 156 |
$data['attributes']['class'] = trim(implode(' ', array_filter($btnClasses))); |
| 157 |
|
| 158 |
if ($tabIndex = Helper::getNextTabIndex()) { |
| 159 |
$data['attributes']['tabindex'] = $tabIndex; |
| 160 |
} |
| 161 |
|
| 162 |
$styles = ''; |
| 163 |
if ('' == ArrayHelper::get($data, 'settings.button_style')) { |
| 164 |
$data['attributes']['class'] .= ' wpf_has_custom_css'; |
| 165 |
// it's a custom button |
| 166 |
$buttonActiveStyles = ArrayHelper::get($data, 'settings.normal_styles', []); |
| 167 |
$buttonHoverStyles = ArrayHelper::get($data, 'settings.hover_styles', []); |
| 168 |
|
| 169 |
$activeStates = ''; |
| 170 |
foreach ($buttonActiveStyles as $styleAtr => $styleValue) { |
| 171 |
if ('0' != $styleValue && !$styleValue) { |
| 172 |
continue; |
| 173 |
} |
| 174 |
if ('borderRadius' == $styleAtr) { |
| 175 |
$styleValue .= 'px'; |
| 176 |
} |
| 177 |
$activeStates .= ltrim(strtolower(preg_replace('/[A-Z]([A-Z](?![a-z]))*/', '-$0', $styleAtr)), '_') . ':' . $styleValue . ';'; |
| 178 |
} |
| 179 |
if ($activeStates) { |
| 180 |
$styles .= 'form.fluent_form_' . $form->id . ' .wpf_has_custom_css.ff-btn-submit { ' . $activeStates . ' }'; |
| 181 |
} |
| 182 |
$hoverStates = ''; |
| 183 |
foreach ($buttonHoverStyles as $styleAtr => $styleValue) { |
| 184 |
if ('0' != $styleValue && !$styleValue) { |
| 185 |
continue; |
| 186 |
} |
| 187 |
if ('borderRadius' == $styleAtr) { |
| 188 |
$styleValue .= 'px'; |
| 189 |
} |
| 190 |
$hoverStates .= ltrim(strtolower(preg_replace('/[A-Z]([A-Z](?![a-z]))*/', '-$0', $styleAtr)), '-') . ':' . $styleValue . ';'; |
| 191 |
} |
| 192 |
if ($hoverStates) { |
| 193 |
$styles .= 'form.fluent_form_' . $form->id . ' .wpf_has_custom_css.ff-btn-submit:hover { ' . $hoverStates . ' } '; |
| 194 |
} |
| 195 |
} elseif ('no_style' != $btnStyle) { |
| 196 |
$styles .= 'form.fluent_form_' . $form->id . ' .ff-btn-submit { background-color: ' . esc_attr(ArrayHelper::get($data, 'settings.background_color')) . '; color: ' . esc_attr(ArrayHelper::get($data, 'settings.color')) . '; }'; |
| 197 |
} |
| 198 |
|
| 199 |
$atts = $this->buildAttributes($data['attributes']); |
| 200 |
$hasConditions = $this->hasConditions($data) ? 'has-conditions ' : ''; |
| 201 |
$cls = trim($align . ' ' . $data['settings']['container_class'] . ' ' . $hasConditions); |
| 202 |
|
| 203 |
$html = "<div class='" . esc_attr($cls) . " ff_submit_btn_wrapper ff_submit_btn_wrapper_custom'>"; |
| 204 |
|
| 205 |
// ADDED IN v1.2.6 - updated in 1.4.4 |
| 206 |
if (isset($data['settings']['button_ui'])) { |
| 207 |
if ('default' == $data['settings']['button_ui']['type']) { |
| 208 |
$buttonText = $data['settings']['button_ui']['text']; |
| 209 |
$html .= '<button ' . $atts . ' aria-label="' . esc_attr($this->removeShortcode($buttonText)) . '">' . fluentform_sanitize_html($buttonText) . '</button>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $atts is escaped before being passed in. |
| 210 |
} else { |
| 211 |
$html .= "<button class='ff-btn-submit' type='submit' aria-label='Submit The Form'><img style='max-width: 200px;' src='" . esc_url($data['settings']['button_ui']['img_url']) . "' alt='Submit Form'></button>"; |
| 212 |
} |
| 213 |
} else { |
| 214 |
$buttonText = $data['settings']['btn_text']; |
| 215 |
$html .= '<button ' . $atts . ' aria-label="' . esc_attr($this->removeShortcode($buttonText)) . '">' . fluentform_sanitize_html($buttonText) . '</button>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $atts is escaped before being passed in. |
| 216 |
} |
| 217 |
|
| 218 |
// SECURITY (FINDING-12): neutralize any </style> breakout in the user-built custom-button CSS. |
| 219 |
$styles = fluentformSanitizeCSS($styles); |
| 220 |
if ($styles) { |
| 221 |
$html .= '<style>' . $styles . '</style>'; |
| 222 |
} |
| 223 |
|
| 224 |
$html .= '</div>'; |
| 225 |
|
| 226 |
$html = apply_filters_deprecated( |
| 227 |
'fluentform_rendering_field_html_' . $elementName, |
| 228 |
[ |
| 229 |
$html, |
| 230 |
$data, |
| 231 |
$form |
| 232 |
], |
| 233 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 234 |
'fluentform/rendering_field_html_' . $elementName, |
| 235 |
'Use fluentform/rendering_field_html_' . $elementName . ' instead of fluentform_rendering_field_html_' . $elementName |
| 236 |
); |
| 237 |
|
| 238 |
$this->printContent('fluentform/rendering_field_html_' . $elementName, $html, $data, $form); |
| 239 |
} |
| 240 |
} |
| 241 |
|