| @@ -10,8 +10,10 @@ | ||
| 10 | 10 | |
| 11 | 11 | class FormSettings |
| 12 | 12 | { |
| 13 | 13 | /** |
| 14 | + * Request object | |
| 15 | + * | |
| 14 | 16 | * @var \FluentForm\Framework\Request\Request |
| 15 | 17 | */ |
| 16 | 18 | private $request; |
| 17 | 19 | |
| @@ -16,11 +18,8 @@ | ||
| 16 | 18 | private $request; |
| 17 | 19 | |
| 18 | 20 | private $app; |
| 19 | 21 | |
| 20 | - /** | |
| 21 | - * @var int form ID. | |
| 22 | - */ | |
| 23 | 22 | private $formId; |
| 24 | 23 | |
| 25 | 24 | /** |
| 26 | 25 | * The settings (fluentform_form_meta) query builder. |
| @@ -30,8 +29,9 @@ | ||
| 30 | 29 | private $settingsQuery; |
| 31 | 30 | |
| 32 | 31 | /** |
| 33 | 32 | * Construct the object |
| 33 | + * | |
| 34 | 34 | * @throws \Exception |
| 35 | 35 | */ |
| 36 | 36 | public function __construct(Application $application) |
| 37 | 37 | { |
| @@ -44,9 +44,8 @@ | ||
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | 47 | * Get settings for a particular form by id |
| 48 | - * @return void | |
| 49 | 48 | */ |
| 50 | 49 | public function index() |
| 51 | 50 | { |
| 52 | 51 | $metaKey = sanitize_text_field($this->request->get('meta_key')); |
| @@ -61,10 +60,10 @@ | ||
| 61 | 60 | $result = $query->get(); |
| 62 | 61 | |
| 63 | 62 | foreach ($result as $item) { |
| 64 | 63 | $item->value = json_decode($item->value, true); |
| 65 | - if($metaKey == 'notifications') { | |
| 66 | - if(!$item->value) { | |
| 64 | + if ('notifications' == $metaKey) { | |
| 65 | + if (!$item->value) { | |
| 67 | 66 | $item->value = ['name' => '']; |
| 68 | 67 | } |
| 69 | 68 | } |
| 70 | 69 | if (isset($item->value['layout']) && !isset($item->value['layout']['asteriskPlacement'])) { |
| @@ -70,24 +69,47 @@ | ||
| 70 | 69 | if (isset($item->value['layout']) && !isset($item->value['layout']['asteriskPlacement'])) { |
| 71 | 70 | $item->value['layout']['asteriskPlacement'] = 'asterisk-right'; |
| 72 | 71 | } |
| 73 | 72 | } |
| 73 | + | |
| 74 | + $result = apply_filters_deprecated( | |
| 75 | + 'fluentform_get_meta_key_settings_response', | |
| 76 | + [ | |
| 77 | + $result, | |
| 78 | + $this->formId, | |
| 79 | + $metaKey | |
| 80 | + ], | |
| 81 | + FLUENTFORM_FRAMEWORK_UPGRADE, | |
| 82 | + 'fluentform/get_meta_key_settings_response', | |
| 83 | + 'Use fluentform/get_meta_key_settings_response instead of fluentform_get_meta_key_settings_response.' | |
| 84 | + ); | |
| 74 | 85 | |
| 86 | + $result = $this->app->applyFilters('fluentform/get_meta_key_settings_response', $result, $this->formId, $metaKey); | |
| 75 | 87 | wp_send_json_success(['result' => $result], 200); |
| 76 | 88 | } |
| 77 | 89 | |
| 78 | - | |
| 79 | 90 | public function getGeneralSettingsAjax() |
| 80 | 91 | { |
| 81 | 92 | $formId = intval($this->request->get('form_id')); |
| 82 | 93 | $form = new Form($this->app); |
| 83 | 94 | $settings = [ |
| 84 | - 'generalSettings' => $form->getFormsDefaultSettings($formId), | |
| 85 | - 'advancedValidationSettings' => $form->getAdvancedValidationSettings($formId) | |
| 95 | + 'generalSettings' => $form->getFormsDefaultSettings($formId), | |
| 96 | + 'advancedValidationSettings' => $form->getAdvancedValidationSettings($formId), | |
| 86 | 97 | ]; |
| 87 | 98 | |
| 88 | - $settings = apply_filters('fluentform_form_settings_ajax', $settings, $formId); | |
| 99 | + $settings = apply_filters_deprecated( | |
| 100 | + 'fluentform_form_settings_ajax', | |
| 101 | + [ | |
| 102 | + $settings, | |
| 103 | + $formId | |
| 104 | + ], | |
| 105 | + FLUENTFORM_FRAMEWORK_UPGRADE, | |
| 106 | + 'fluentform/form_settings_ajax', | |
| 107 | + 'Use fluentform/form_settings_ajax instead of fluentform_form_settings_ajax.' | |
| 108 | + ); | |
| 89 | 109 | |
| 110 | + $settings = $this->app->applyFilters('fluentform/form_settings_ajax', $settings, $formId); | |
| 111 | + | |
| 90 | 112 | wp_send_json_success($settings, 200); |
| 91 | 113 | } |
| 92 | 114 | |
| 93 | 115 | public function saveGeneralSettingsAjax() |
| @@ -94,9 +116,48 @@ | ||
| 94 | 116 | { |
| 95 | 117 | $formId = intval($this->request->get('form_id')); |
| 96 | 118 | $form = new Form($this->app); |
| 97 | 119 | $formSettings = \json_decode($this->request->get('formSettings'), true); |
| 120 | + | |
| 121 | + $sanitizerMap = [ | |
| 122 | + 'redirectTo' => 'sanitize_text_field', | |
| 123 | + 'redirectMessage' => 'fluentform_sanitize_html', | |
| 124 | + 'messageToShow' => 'fluentform_sanitize_html', | |
| 125 | + 'customPage' => 'sanitize_text_field', | |
| 126 | + 'samePageFormBehavior' => 'sanitize_text_field', | |
| 127 | + 'customUrl' => 'sanitize_url', | |
| 128 | + 'enabled' => 'rest_sanitize_boolean', | |
| 129 | + 'numberOfEntries' => 'intval', | |
| 130 | + 'period' => 'sanitize_text_field', | |
| 131 | + 'limitReachedMsg' => 'sanitize_text_field', | |
| 132 | + 'start' => 'sanitize_text_field', | |
| 133 | + 'end' => 'sanitize_text_field', | |
| 134 | + 'pendingMsg' => 'sanitize_text_field', | |
| 135 | + 'expiredMsg' => 'sanitize_text_field', | |
| 136 | + 'requireLoginMsg' => 'sanitize_text_field', | |
| 137 | + 'message' => 'sanitize_text_field', | |
| 138 | + 'labelPlacement' => 'sanitize_text_field', | |
| 139 | + 'helpMessagePlacement' => 'sanitize_text_field', | |
| 140 | + 'errorMessagePlacement' => 'sanitize_text_field', | |
| 141 | + 'asteriskPlacement' => 'sanitize_text_field', | |
| 142 | + 'delete_entry_on_submission' => 'sanitize_text_field', | |
| 143 | + 'id' => 'intval', | |
| 144 | + 'showLabel' => 'rest_sanitize_boolean', | |
| 145 | + 'showCount' => 'rest_sanitize_boolean', | |
| 146 | + 'status' => 'rest_sanitize_boolean', | |
| 147 | + 'type' => 'sanitize_text_field', | |
| 148 | + 'field' => 'sanitize_text_field', | |
| 149 | + 'operator' => 'sanitize_text_field', | |
| 150 | + 'value' => 'sanitize_text_field', | |
| 151 | + 'error_message' => 'sanitize_text_field', | |
| 152 | + 'validation_type' => 'sanitize_text_field', | |
| 153 | + 'conv_form_per_step_save' => 'rest_sanitize_boolean', | |
| 154 | + 'conv_form_resume_from_last_step' => 'rest_sanitize_boolean', | |
| 155 | + ]; | |
| 156 | + $formSettings = $this->sanitizeData($formSettings, $sanitizerMap); | |
| 157 | + | |
| 98 | 158 | $advancedValidationSettings = \json_decode($this->request->get('advancedValidationSettings'), true); |
| 159 | + $advancedValidationSettings = $this->sanitizeData($advancedValidationSettings, $sanitizerMap); | |
| 99 | 160 | |
| 100 | 161 | Validator::validate( |
| 101 | 162 | 'confirmations', |
| 102 | 163 | ArrayHelper::get($formSettings, 'confirmation', []) |
| @@ -108,9 +169,9 @@ | ||
| 108 | 169 | $deleteAfterXDaysStatus = ArrayHelper::get($formSettings, 'delete_after_x_days'); |
| 109 | 170 | $deleteDaysCount = ArrayHelper::get($formSettings, 'auto_delete_days'); |
| 110 | 171 | $deleteOnSubmission = ArrayHelper::get($formSettings, 'delete_entry_on_submission'); |
| 111 | 172 | |
| 112 | - if($deleteOnSubmission != 'yes' && $deleteDaysCount && $deleteAfterXDaysStatus == 'yes') { | |
| 173 | + if ('yes' != $deleteOnSubmission && $deleteDaysCount && 'yes' == $deleteAfterXDaysStatus) { | |
| 113 | 174 | // We have to set meta values |
| 114 | 175 | $form->updateMeta($formId, 'auto_delete_days', intval($deleteDaysCount)); |
| 115 | 176 | } else { |
| 116 | 177 | // we have to delete meta values |
| @@ -116,14 +177,24 @@ | ||
| 116 | 177 | // we have to delete meta values |
| 117 | 178 | $form->deleteMeta($formId, 'auto_delete_days'); |
| 118 | 179 | } |
| 119 | 180 | |
| 120 | - do_action('fluentform_after_save_form_settings', $formId, $this->request->all()); | |
| 181 | + do_action_deprecated( | |
| 182 | + 'fluentform_after_save_form_settings', | |
| 183 | + [ | |
| 184 | + $formId, | |
| 185 | + $this->request->all() | |
| 186 | + ], | |
| 187 | + FLUENTFORM_FRAMEWORK_UPGRADE, | |
| 188 | + 'fluentform/after_save_form_settings', | |
| 189 | + 'Use fluentform/after_save_form_settings instead of fluentform_after_save_form_settings.' | |
| 190 | + ); | |
| 121 | 191 | |
| 192 | + $this->app->doAction('fluentform/after_save_form_settings', $formId, $this->request->all()); | |
| 193 | + | |
| 122 | 194 | wp_send_json_success([ |
| 123 | - 'message' => __('Settings has been saved.', 'fluentform') | |
| 195 | + 'message' => __('Settings has been saved.', 'fluentform'), | |
| 124 | 196 | ], 200); |
| 125 | - | |
| 126 | 197 | } |
| 127 | 198 | |
| 128 | 199 | /** |
| 129 | 200 | * Save settings/meta for a form in database |
| @@ -135,22 +206,47 @@ | ||
| 135 | 206 | $valueArray = $value ? json_decode($value, true) : []; |
| 136 | 207 | |
| 137 | 208 | $key = sanitize_text_field($this->request->get('meta_key')); |
| 138 | 209 | |
| 139 | - if ($key == 'formSettings') { | |
| 210 | + if ('formSettings' == $key) { | |
| 140 | 211 | Validator::validate( |
| 141 | - 'confirmations', ArrayHelper::get( | |
| 142 | - $valueArray, 'confirmation', [] | |
| 143 | - ) | |
| 212 | + 'confirmations', | |
| 213 | + ArrayHelper::get( | |
| 214 | + $valueArray, | |
| 215 | + 'confirmation', | |
| 216 | + [] | |
| 217 | + ) | |
| 144 | 218 | ); |
| 145 | 219 | } else { |
| 146 | 220 | Validator::validate($key, $valueArray); |
| 147 | 221 | } |
| 222 | + $sanitizerMap = [ | |
| 223 | + 'name' => 'sanitize_text_field', | |
| 224 | + 'field' => 'sanitize_text_field', | |
| 225 | + 'email' => 'sanitize_text_field', | |
| 226 | + 'operator' => 'sanitize_text_field', | |
| 227 | + 'value' => 'sanitize_text_field', | |
| 228 | + 'fromName' => 'sanitize_text_field', | |
| 229 | + 'fromEmail' => 'sanitize_text_field', | |
| 230 | + 'replyTo' => 'sanitize_text_field', | |
| 231 | + 'bcc' => 'sanitize_text_field', | |
| 232 | + 'subject' => 'sanitize_text_field', | |
| 233 | + 'message' => 'wp_kses_post', | |
| 234 | + 'status' => 'rest_sanitize_boolean', | |
| 235 | + 'enabled' => 'rest_sanitize_boolean', | |
| 236 | + 'type' => 'sanitize_text_field', | |
| 237 | + 'url' => 'sanitize_url', | |
| 238 | + 'webhook' => 'sanitize_url', | |
| 239 | + 'textTitle' => 'sanitize_text_field', | |
| 240 | + ]; | |
| 148 | 241 | |
| 242 | + $valueArray = $this->sanitizeData($valueArray, $sanitizerMap); | |
| 243 | + $value = json_encode($valueArray); | |
| 244 | + | |
| 149 | 245 | $data = [ |
| 150 | 246 | 'meta_key' => $key, |
| 151 | - 'value' => $value, | |
| 152 | - 'form_id' => $this->formId | |
| 247 | + 'value' => $value, | |
| 248 | + 'form_id' => $this->formId, | |
| 153 | 249 | ]; |
| 154 | 250 | |
| 155 | 251 | // If the request has an valid id field it's safe to assume |
| 156 | 252 | // that the user wants to update an existing settings. |
| @@ -164,21 +260,20 @@ | ||
| 164 | 260 | if (isset($settings)) { |
| 165 | 261 | $this->settingsQuery->where('id', $settings->id)->update($data); |
| 166 | 262 | $insertId = $settings->id; |
| 167 | 263 | } else { |
| 168 | - $insertId = $this->settingsQuery->insert($data); | |
| 264 | + $insertId = $this->settingsQuery->insertGetId($data); | |
| 169 | 265 | } |
| 170 | 266 | |
| 171 | 267 | wp_send_json_success([ |
| 172 | - 'message' => __('Settings has been saved.', 'fluentform'), | |
| 268 | + 'message' => __('Settings has been saved.', 'fluentform'), | |
| 173 | 269 | 'settings' => json_decode($value, true), |
| 174 | - 'id' => $insertId | |
| 270 | + 'id' => $insertId, | |
| 175 | 271 | ], 200); |
| 176 | 272 | } |
| 177 | 273 | |
| 178 | 274 | /** |
| 179 | 275 | * Delete settings/meta from database for a given form |
| 180 | - * @return void | |
| 181 | 276 | */ |
| 182 | 277 | public function remove() |
| 183 | 278 | { |
| 184 | 279 | $id = intval($this->request->get('id')); |
| @@ -185,6 +280,14 @@ | ||
| 185 | 280 | |
| 186 | 281 | $this->settingsQuery->where('id', $id)->delete(); |
| 187 | 282 | |
| 188 | 283 | wp_send_json([], 200); |
| 284 | + } | |
| 285 | + | |
| 286 | + private function sanitizeData($settings, $sanitizerMap) | |
| 287 | + { | |
| 288 | + if (fluentformCanUnfilteredHTML()) { | |
| 289 | + return $settings; | |
| 290 | + } | |
| 291 | + return fluentform_backend_sanitizer($settings, $sanitizerMap); | |
| 189 | 292 | } |
| 190 | 293 | } |