| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\FormBuilder\Components; |
| 4 |
|
| 5 |
use FluentForm\App\Helpers\Helper; |
| 6 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 7 |
|
| 8 |
class SubmitButton extends BaseComponent |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Compile and echo the html element |
| 12 |
* |
| 13 |
* @param array $data [element data] |
| 14 |
* @param \stdClass $form [Form Object] |
| 15 |
* |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
public function compile($data, $form) |
| 19 |
{ |
| 20 |
if (apply_filters('fluentform_is_hide_submit_btn_' . $form->id, false)) { |
| 21 |
return ''; |
| 22 |
} |
| 23 |
|
| 24 |
$elementName = $data['element']; |
| 25 |
|
| 26 |
$data = apply_filters('fluentform_rendering_field_data_' . $elementName, $data, $form); |
| 27 |
|
| 28 |
$btnStyle = ArrayHelper::get($data['settings'], 'button_style'); |
| 29 |
$btnSize = 'ff-btn-'; |
| 30 |
$btnSize .= isset($data['settings']['button_size']) ? $data['settings']['button_size'] : 'md'; |
| 31 |
$oldBtnType = isset($data['settings']['button_style']) ? '' : ' ff-btn-primary '; |
| 32 |
|
| 33 |
$btnClasses = [ |
| 34 |
'ff-btn ff-btn-submit', |
| 35 |
$oldBtnType, |
| 36 |
$btnSize, |
| 37 |
$data['attributes']['class'], |
| 38 |
]; |
| 39 |
|
| 40 |
if ('no_style' == $btnStyle) { |
| 41 |
$btnClasses[] = 'ff_btn_no_style'; |
| 42 |
} else { |
| 43 |
$btnClasses[] = 'ff_btn_style'; |
| 44 |
} |
| 45 |
|
| 46 |
$align = 'ff-el-group ff-text-' . @$data['settings']['align']; |
| 47 |
$data['attributes']['class'] = trim(implode(' ', array_filter($btnClasses))); |
| 48 |
|
| 49 |
if ($tabIndex = Helper::getNextTabIndex()) { |
| 50 |
$data['attributes']['tabindex'] = $tabIndex; |
| 51 |
} |
| 52 |
|
| 53 |
$styles = ''; |
| 54 |
if ('' == ArrayHelper::get($data, 'settings.button_style')) { |
| 55 |
$data['attributes']['class'] .= ' wpf_has_custom_css'; |
| 56 |
// it's a custom button |
| 57 |
$buttonActiveStyles = ArrayHelper::get($data, 'settings.normal_styles', []); |
| 58 |
$buttonHoverStyles = ArrayHelper::get($data, 'settings.hover_styles', []); |
| 59 |
|
| 60 |
$activeStates = ''; |
| 61 |
foreach ($buttonActiveStyles as $styleAtr => $styleValue) { |
| 62 |
if (! $styleValue) { |
| 63 |
continue; |
| 64 |
} |
| 65 |
if ('borderRadius' == $styleAtr) { |
| 66 |
$styleValue .= 'px'; |
| 67 |
} |
| 68 |
$activeStates .= ltrim(strtolower(preg_replace('/[A-Z]([A-Z](?![a-z]))*/', '-$0', $styleAtr)), '_') . ':' . $styleValue . ';'; |
| 69 |
} |
| 70 |
if ($activeStates) { |
| 71 |
$styles .= 'form.fluent_form_' . $form->id . ' .wpf_has_custom_css.ff-btn-submit { ' . $activeStates . ' }'; |
| 72 |
} |
| 73 |
$hoverStates = ''; |
| 74 |
foreach ($buttonHoverStyles as $styleAtr => $styleValue) { |
| 75 |
if (! $styleValue) { |
| 76 |
continue; |
| 77 |
} |
| 78 |
if ('borderRadius' == $styleAtr) { |
| 79 |
$styleValue .= 'px'; |
| 80 |
} |
| 81 |
$hoverStates .= ltrim(strtolower(preg_replace('/[A-Z]([A-Z](?![a-z]))*/', '-$0', $styleAtr)), '-') . ':' . $styleValue . ';'; |
| 82 |
} |
| 83 |
if ($hoverStates) { |
| 84 |
$styles .= 'form.fluent_form_' . $form->id . ' .wpf_has_custom_css.ff-btn-submit:hover { ' . $hoverStates . ' } '; |
| 85 |
} |
| 86 |
} elseif ('no_style' != $btnStyle) { |
| 87 |
$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')) . '; }'; |
| 88 |
} |
| 89 |
|
| 90 |
$atts = $this->buildAttributes($data['attributes']); |
| 91 |
$cls = trim($align . ' ' . $data['settings']['container_class']); |
| 92 |
|
| 93 |
$html = "<div class='" . esc_attr($cls) . " ff_submit_btn_wrapper'>"; |
| 94 |
|
| 95 |
// ADDED IN v1.2.6 - updated in 1.4.4 |
| 96 |
if (isset($data['settings']['button_ui'])) { |
| 97 |
if ('default' == $data['settings']['button_ui']['type']) { |
| 98 |
$html .= '<button ' . $atts . '>' . fluentform_sanitize_html($data['settings']['button_ui']['text'], false) . '</button>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $atts is escaped before being passed in. |
| 99 |
} else { |
| 100 |
$html .= "<button class='ff-btn-submit' type='submit'><img style='max-width: 200px;' src='" . esc_url($data['settings']['button_ui']['img_url']) . "' alt='Submit Form'></button>"; |
| 101 |
} |
| 102 |
} else { |
| 103 |
$html .= '<button ' . $atts . '>' . fluentform_sanitize_html($data['settings']['btn_text'], false) . '</button>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $atts is escaped before being passed in. |
| 104 |
} |
| 105 |
|
| 106 |
if ($styles) { |
| 107 |
if (did_action('wp_footer')) { |
| 108 |
$html .= '<style>' . $styles . '</style>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $styles is escaped before being passed in. |
| 109 |
} else { |
| 110 |
add_action('wp_footer', function () use ($styles) { |
| 111 |
echo '<style>' . $styles . '</style>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $styles is escaped before being passed in. |
| 112 |
}); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
$html .= '</div>'; |
| 117 |
|
| 118 |
$this->printContent('fluentform_rendering_field_html_' . $elementName, $html, $data, $form); |
| 119 |
} |
| 120 |
} |
| 121 |
|