← All changes
|
app/Services/FormBuilder/Components/DateTime.php
+112
-54
3.6.64
→
6.2.14
View file →
| @@ -1,8 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentForm\App\Services\FormBuilder\Components; |
| 4 | 4 | |
| 5 | +defined('ABSPATH') || die; | |
| 6 | + | |
| 5 | 7 | use FluentForm\App\Helpers\Helper; |
| 6 | 8 | use FluentForm\Framework\Helpers\ArrayHelper; |
| 7 | 9 | |
| 8 | 10 | class DateTime extends BaseComponent |
| @@ -8,16 +10,28 @@ | ||
| 8 | 10 | class DateTime extends BaseComponent |
| 9 | 11 | { |
| 10 | 12 | /** |
| 11 | 13 | * Compile and echo the html element |
| 12 | - * @param array $data [element data] | |
| 13 | - * @param stdClass $form [Form Object] | |
| 14 | - * @return viod | |
| 14 | + * | |
| 15 | + * @param array $data [element data] | |
| 16 | + * @param \stdClass $form [Form Object] | |
| 17 | + * | |
| 18 | + * @return void | |
| 15 | 19 | */ |
| 16 | 20 | public function compile($data, $form) |
| 17 | 21 | { |
| 18 | 22 | $elementName = $data['element']; |
| 19 | - $data = apply_filters('fluenform_rendering_field_data_' . $elementName, $data, $form); | |
| 23 | + $data = apply_filters_deprecated( | |
| 24 | + 'fluentform_rendering_field_data_' . $elementName, | |
| 25 | + [ | |
| 26 | + $data, | |
| 27 | + $form, | |
| 28 | + ], | |
| 29 | + FLUENTFORM_FRAMEWORK_UPGRADE, | |
| 30 | + 'fluentform/rendering_field_data_' . $elementName, | |
| 31 | + 'Use fluentform/rendering_field_data_' . $elementName . ' instead of fluentform_rendering_field_data_' . $elementName | |
| 32 | + ); | |
| 33 | + $data = apply_filters('fluentform/rendering_field_data_' . $elementName, $data, $form); | |
| 20 | 34 | |
| 21 | 35 | wp_enqueue_script('flatpickr'); |
| 22 | 36 | wp_enqueue_style('flatpickr'); |
| 23 | 37 | |
| @@ -27,24 +41,49 @@ | ||
| 27 | 41 | $dateFormat = $data['settings']['date_format']; |
| 28 | 42 | |
| 29 | 43 | $data['attributes']['id'] = $this->makeElementId($data, $form); |
| 30 | 44 | |
| 31 | - if($tabIndex = \FluentForm\App\Helpers\Helper::getNextTabIndex()) { | |
| 45 | + if ($tabIndex = Helper::getNextTabIndex()) { | |
| 32 | 46 | $data['attributes']['tabindex'] = $tabIndex; |
| 33 | 47 | } |
| 34 | 48 | |
| 35 | - $elMarkup = "<input data-type-datepicker data-format='" . $dateFormat . "' " . $this->buildAttributes($data['attributes']) . ">"; | |
| 49 | + $atts = $this->buildAttributes($data['attributes']); | |
| 36 | 50 | |
| 37 | - $this->getDateFormatConfigJSON($data['settings'], $form, $data['attributes']['id']); | |
| 51 | + $ariaRequired = 'false'; | |
| 52 | + if (ArrayHelper::get($data, 'settings.validation_rules.required.value')) { | |
| 53 | + $ariaRequired = 'true'; | |
| 54 | + } | |
| 55 | + $id = $data['attributes']['id']; | |
| 38 | 56 | |
| 57 | + $ariaLabel = esc_html__(' Use arrow keys to navigate dates. Press enter to select a date.', 'fluentform'); | |
| 58 | + $label = ArrayHelper::get($data, 'settings.label'); | |
| 59 | + // SECURITY (FINDING-12): esc_attr the settings.label before interpolating it into the | |
| 60 | + // single-quoted aria-label; the save-time wp_kses does not encode quotes, so an unescaped | |
| 61 | + // label allows an attribute breakout (stored XSS). | |
| 62 | + $elMarkup = "<input aria-label='" . esc_attr($label) . $ariaLabel . "' aria-haspopup='dialog' data-type-datepicker data-format='" . esc_attr($dateFormat) . "' " . $atts . " aria-invalid='false' aria-required={$ariaRequired}>"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $atts is escaped before being passed in. | |
| 63 | + $config = $this->getDateFormatConfigJSON($data['settings'], $form); | |
| 64 | + $customConfig = $this->getCustomConfig($data['settings'], $form); | |
| 65 | + $this->loadToFooter($config, $customConfig, $form, $id); | |
| 39 | 66 | $html = $this->buildElementMarkup($elMarkup, $data, $form); |
| 40 | - echo apply_filters('fluenform_rendering_field_html_' . $elementName, $html, $data, $form); | |
| 67 | + | |
| 68 | + $html = apply_filters_deprecated( | |
| 69 | + 'fluentform_rendering_field_html_' . $elementName, | |
| 70 | + [ | |
| 71 | + $html, | |
| 72 | + $data, | |
| 73 | + $form, | |
| 74 | + ], | |
| 75 | + FLUENTFORM_FRAMEWORK_UPGRADE, | |
| 76 | + 'fluentform/rendering_field_html_' . $elementName, | |
| 77 | + 'Use fluentform/rendering_field_html_' . $elementName . ' instead of fluentform_rendering_field_html_' . $elementName | |
| 78 | + ); | |
| 79 | + | |
| 80 | + $this->printContent('fluentform/rendering_field_html_' . $elementName, $html, $data, $form); | |
| 41 | 81 | } |
| 42 | 82 | |
| 43 | - | |
| 44 | 83 | public function getAvailableDateFormats() |
| 45 | 84 | { |
| 46 | - $dateFormats = apply_filters('fluentform/available_date_formats', array( | |
| 85 | + $dateFormats = apply_filters('fluentform/available_date_formats', [ | |
| 47 | 86 | 'm/d/Y' => 'm/d/Y - (Ex: 04/28/2018)', // USA |
| 48 | 87 | 'd/m/Y' => 'd/m/Y - (Ex: 28/04/2018)', // Canada, UK |
| 49 | 88 | 'd.m.Y' => 'd.m.Y - (Ex: 28.04.2019)', // Germany |
| 50 | 89 | 'n/j/y' => 'n/j/y - (Ex: 4/28/18)', |
| @@ -60,79 +99,98 @@ | ||
| 60 | 99 | 'd.m.Y h:i K' => 'd.m.Y h:i K - (Ex: 28.04.2019 08:55 PM)', // Germany |
| 61 | 100 | 'd.m.Y H:i' => 'd.m.Y H:i - (Ex: 28.04.2019 20:55)', // Germany |
| 62 | 101 | 'h:i K' => 'h:i K (Only Time Ex: 08:55 PM)', |
| 63 | 102 | 'H:i' => 'H:i (Only Time Ex: 20:55)', |
| 64 | - )); | |
| 103 | + ]); | |
| 65 | 104 | |
| 66 | 105 | $formatted = []; |
| 67 | 106 | foreach ($dateFormats as $format => $label) { |
| 68 | 107 | $formatted[] = [ |
| 69 | 108 | 'label' => $label, |
| 70 | - 'value' => $format | |
| 109 | + 'value' => $format, | |
| 71 | 110 | ]; |
| 72 | 111 | } |
| 73 | 112 | return $formatted; |
| 74 | 113 | } |
| 75 | 114 | |
| 76 | - private function getDateFormatConfigJSON($settings, $form, $id) | |
| 115 | + public function getDateFormatConfigJSON($settings, $form) | |
| 77 | 116 | { |
| 78 | 117 | $dateFormat = ArrayHelper::get($settings, 'date_format'); |
| 79 | - if (!$dateFormat) { | |
| 118 | + | |
| 119 | + if (! $dateFormat) { | |
| 80 | 120 | $dateFormat = 'm/d/Y'; |
| 81 | 121 | } |
| 82 | 122 | |
| 83 | - $customConfigObject = trim(ArrayHelper::get($settings, 'date_config')); | |
| 84 | - | |
| 85 | - if(!$customConfigObject || substr($customConfigObject, 0, 1) != '{' || substr($customConfigObject, -1) != '}') { | |
| 86 | - $customConfigObject = '{}'; | |
| 87 | - } | |
| 88 | - | |
| 89 | 123 | $hasTime = $this->hasTime($dateFormat); |
| 90 | 124 | $time24 = false; |
| 91 | 125 | |
| 92 | - if($hasTime && strpos($dateFormat, 'H') !== false) { | |
| 126 | + if ($hasTime && false !== strpos($dateFormat, 'H')) { | |
| 93 | 127 | $time24 = true; |
| 94 | 128 | } |
| 95 | 129 | |
| 96 | - $config = apply_filters('fluentform/frontend_date_format', array( | |
| 97 | - 'dateFormat' => $dateFormat, | |
| 98 | - 'enableTime' => $hasTime, | |
| 99 | - 'noCalendar' => !$this->hasDate($dateFormat), | |
| 130 | + $config = apply_filters('fluentform/frontend_date_format', [ | |
| 131 | + 'dateFormat' => $dateFormat, | |
| 132 | + 'ariaDateFormat' =>'F j, Y', | |
| 133 | + 'enableTime' => $hasTime, | |
| 134 | + 'noCalendar' => ! $this->hasDate($dateFormat), | |
| 100 | 135 | 'disableMobile' => true, |
| 101 | - 'time_24hr' => $time24 | |
| 102 | - ), $settings, $form); | |
| 136 | + 'time_24hr' => $time24, | |
| 137 | + ], $settings, $form); | |
| 103 | 138 | |
| 104 | - $config = json_encode($config, JSON_FORCE_OBJECT); | |
| 139 | + return json_encode($config, JSON_FORCE_OBJECT); | |
| 140 | + } | |
| 105 | 141 | |
| 142 | + public function getCustomConfig($settings, $form = null) | |
| 143 | + { | |
| 144 | + $customConfigObject = trim((string) ArrayHelper::get($settings, 'date_config')); | |
| 145 | + | |
| 146 | + if ( | |
| 147 | + !$customConfigObject || | |
| 148 | + '{' !== substr($customConfigObject, 0, 1) || | |
| 149 | + '}' !== substr($customConfigObject, -1) | |
| 150 | + ) { | |
| 151 | + $customConfigObject = '{}'; | |
| 152 | + } else { | |
| 153 | + $customConfigObject = str_ireplace('</script', '<\\/script', $customConfigObject); | |
| 154 | + } | |
| 155 | + | |
| 156 | + return apply_filters('fluentform/date_time_custom_config', $customConfigObject, $settings, $form); | |
| 157 | + } | |
| 158 | + | |
| 159 | + private function loadToFooter($config, $customConfigObject, $form, $id) | |
| 160 | + { | |
| 106 | 161 | add_action('wp_footer', function () use ($config, $customConfigObject, $id, $form) { |
| 107 | 162 | ?> |
| 108 | 163 | <script type="text/javascript"> |
| 109 | - jQuery(document).ready(function ($) { | |
| 110 | - function initPicker() { | |
| 111 | - if(typeof flatpickr == 'undefined') { | |
| 112 | - return; | |
| 113 | - } | |
| 114 | - flatpickr.localize(window.fluentFormVars.date_i18n); | |
| 115 | - var config = <?php echo $config; ?>; | |
| 116 | - try { | |
| 117 | - var customConfig = <?php echo $customConfigObject; ?>; | |
| 118 | - } catch (e) { | |
| 119 | - var customConfig = {}; | |
| 120 | - } | |
| 164 | + jQuery(document).ready(function($) { | |
| 165 | + function initPicker() { | |
| 166 | + if (typeof flatpickr == 'undefined') { | |
| 167 | + return; | |
| 168 | + } | |
| 169 | + flatpickr.localize(window.fluentFormVars.date_i18n); | |
| 170 | + var config = <?php echo fluentform_kses_js($config); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $config is escaped using fluentform_kses_js ?> ; | |
| 171 | + try { | |
| 172 | + var customConfig = <?php echo fluentform_kses_js($customConfigObject); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $config is escaped using fluentform_kses_js ?>; | |
| 173 | + } catch (e) { | |
| 174 | + var customConfig = {}; | |
| 175 | + } | |
| 121 | 176 | |
| 122 | - var config = $.extend({}, config, customConfig); | |
| 123 | - if (!config.locale) { | |
| 124 | - config.locale = 'default'; | |
| 125 | - } | |
| 126 | - | |
| 127 | - if(jQuery('#<?php echo $id; ?>').length) { | |
| 128 | - flatpickr('#<?php echo $id; ?>', config); | |
| 129 | - } | |
| 130 | - } | |
| 177 | + var config = $.extend({}, config, customConfig); | |
| 178 | + if (!config.locale) { | |
| 179 | + config.locale = 'default'; | |
| 180 | + } | |
| 181 | + if (jQuery('#<?php echo esc_attr($id); ?>').length) { | |
| 182 | + flatpickr('#<?php echo esc_attr($id); ?>', config); | |
| 183 | + } | |
| 184 | + } | |
| 131 | 185 | initPicker(); |
| 132 | - $(document).on('reInitExtras', '.<?php echo $form->instance_css_class; ?>', function () { | |
| 133 | - initPicker(); | |
| 134 | - }); | |
| 186 | + $(document).on( | |
| 187 | + 'reInitExtras', | |
| 188 | + '.<?php echo esc_attr($form->instance_css_class); ?>', | |
| 189 | + function() { | |
| 190 | + initPicker(); | |
| 191 | + } | |
| 192 | + ); | |
| 135 | 193 | }); |
| 136 | 194 | </script> |
| 137 | 195 | <?php |
| 138 | 196 | }, 99999); |
| @@ -141,9 +199,9 @@ | ||
| 141 | 199 | private function hasTime($string) |
| 142 | 200 | { |
| 143 | 201 | $timeStrings = ['H', 'h', 'G', 'i', 'S', 's', 'K']; |
| 144 | 202 | foreach ($timeStrings as $timeString) { |
| 145 | - if (strpos($string, $timeString) != false) { | |
| 203 | + if (false != strpos($string, $timeString)) { | |
| 146 | 204 | return true; |
| 147 | 205 | } |
| 148 | 206 | } |
| 149 | 207 | return false; |
| @@ -152,9 +210,9 @@ | ||
| 152 | 210 | private function hasDate($string) |
| 153 | 211 | { |
| 154 | 212 | $dateStrings = ['d', 'D', 'l', 'j', 'J', 'w', 'W', 'F', 'm', 'n', 'M', 'U', 'Y', 'y', 'Z']; |
| 155 | 213 | foreach ($dateStrings as $dateString) { |
| 156 | - if (strpos($string, $dateString) != false) { | |
| 214 | + if (false != strpos($string, $dateString)) { | |
| 157 | 215 | return 'true'; |
| 158 | 216 | } |
| 159 | 217 | } |
| 160 | 218 | return false; |