| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @package FirePlugins Framework |
| 4 | - * @version 1.0.16 | |
| 4 | + * @version 1.2.6 | |
| 5 | 5 | * |
| 6 | 6 | * @author FirePlugins <[email protected]> |
| 7 | 7 | * @link https://www.fireplugins.com |
| 8 | - * @copyright Copyright © 2021 FirePlugins All Rights Reserved | |
| 8 | + * @copyright Copyright © 2026 FirePlugins All Rights Reserved | |
| 9 | 9 | * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | namespace FPFramework\Helpers; |
| @@ -15,9 +15,10 @@ | ||
| 15 | 15 | { |
| 16 | 16 | exit; // Exit if accessed directly. |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | -class FormHelper { | |
| 19 | +class FormHelper | |
| 20 | +{ | |
| 20 | 21 | /** |
| 21 | 22 | * Extra validation on the fields that we are preparing to save |
| 22 | 23 | * |
| 23 | 24 | * @param array $fields |
| @@ -23,27 +24,24 @@ | ||
| 23 | 24 | * @param array $fields |
| 24 | 25 | * |
| 25 | 26 | * @return array |
| 26 | 27 | */ |
| 27 | - public static function validateFields(&$fields) | |
| 28 | + public static function validateFields($fields) | |
| 28 | 29 | { |
| 29 | 30 | // remove repeater ITEM_ID template items |
| 30 | - foreach ($fields as $key => &$value) | |
| 31 | + foreach ($fields as $key => $value) | |
| 31 | 32 | { |
| 32 | - if (!is_array($value)) | |
| 33 | + if ($key === 'ITEM_ID') | |
| 33 | 34 | { |
| 34 | - continue; | |
| 35 | - } | |
| 36 | - | |
| 37 | - if ($key == 'ITEM_ID') | |
| 38 | - { | |
| 39 | 35 | unset($fields[$key]); |
| 40 | 36 | } |
| 41 | - else | |
| 37 | + else if (is_array($value)) | |
| 42 | 38 | { |
| 43 | - self::validateFields($value); | |
| 39 | + $fields[$key] = self::validateFields($value); | |
| 44 | 40 | } |
| 45 | 41 | } |
| 42 | + | |
| 43 | + return $fields; | |
| 46 | 44 | } |
| 47 | 45 | |
| 48 | 46 | /** |
| 49 | 47 | * Filters every field based on the form settings |
| @@ -49,41 +47,16 @@ | ||
| 49 | 47 | * Filters every field based on the form settings |
| 50 | 48 | * |
| 51 | 49 | * @param array $fields |
| 52 | 50 | * @param array $form_settings |
| 53 | - * @param array $saved_fields The fields that have been already saved in the database for this post. Used to find the updated field values. | |
| 54 | 51 | * |
| 55 | 52 | * @return array |
| 56 | 53 | */ |
| 57 | - public static function filterFields(&$fields, $form_settings, $saved_fields = []) | |
| 54 | + public static function filterFields(&$fields, $form_settings) | |
| 58 | 55 | { |
| 59 | 56 | // Validates the fields |
| 60 | - self::validateFields($fields); | |
| 57 | + $fields = self::validateFields($fields); | |
| 61 | 58 | |
| 62 | - $previous_fields = []; | |
| 63 | - | |
| 64 | - /** | |
| 65 | - * If we were given the previously saved fields, | |
| 66 | - * we filter the $fields by only filtering the values | |
| 67 | - * of the changed fields | |
| 68 | - */ | |
| 69 | - if ($saved_fields) | |
| 70 | - { | |
| 71 | - // find all $saved_fields keys that no longer exist in $fields and remove them | |
| 72 | - $removed_keys = array_diff_key($saved_fields, $fields); | |
| 73 | - // remove these keys from $saved_fields | |
| 74 | - foreach ($removed_keys as $key => $value) | |
| 75 | - { | |
| 76 | - unset($saved_fields[$key]); | |
| 77 | - } | |
| 78 | - | |
| 79 | - // find updated fields | |
| 80 | - $data = self::findUpdatedFieldsToBeFiltered($fields, $saved_fields); | |
| 81 | - | |
| 82 | - $fields = $data['updated_fields']; | |
| 83 | - $previous_fields = $data['previous_fields']; | |
| 84 | - } | |
| 85 | - | |
| 86 | 59 | // Filter the fields only if we have entered new values |
| 87 | 60 | if ($fields) |
| 88 | 61 | { |
| 89 | 62 | // filter all fields |
| @@ -88,49 +61,11 @@ | ||
| 88 | 61 | { |
| 89 | 62 | // filter all fields |
| 90 | 63 | self::loopFieldsRecursive($fields, $form_settings); |
| 91 | 64 | } |
| 92 | - | |
| 93 | - $fields = array_merge($fields, $previous_fields); | |
| 94 | 65 | } |
| 95 | 66 | |
| 96 | 67 | /** |
| 97 | - * Finds the updated fields and only filters these fields in order to speed up the saving process | |
| 98 | - * | |
| 99 | - * @param array $updated_fields | |
| 100 | - * @param array $saved_fields | |
| 101 | - * | |
| 102 | - * @return array | |
| 103 | - */ | |
| 104 | - private static function findUpdatedFieldsToBeFiltered($updated_fields, $saved_fields) | |
| 105 | - { | |
| 106 | - if (!$updated_fields || !$saved_fields || !is_array($updated_fields) || !is_array($saved_fields)) | |
| 107 | - { | |
| 108 | - return [ | |
| 109 | - 'updated_fields' => [], | |
| 110 | - 'previous_fields' => [] | |
| 111 | - ]; | |
| 112 | - } | |
| 113 | - | |
| 114 | - // Compare all values by a json_encode | |
| 115 | - $updated_fields = array_diff_assoc(array_map('json_encode', $updated_fields), array_map('json_encode', $saved_fields)); | |
| 116 | - | |
| 117 | - // Json decode the result | |
| 118 | - $updated_fields = array_map('json_decode', $updated_fields); | |
| 119 | - | |
| 120 | - // If we have modified the fields, then get the unmodified form fields | |
| 121 | - if ($updated_fields) | |
| 122 | - { | |
| 123 | - $saved_fields = array_diff_key($saved_fields, $updated_fields); | |
| 124 | - } | |
| 125 | - | |
| 126 | - return [ | |
| 127 | - 'updated_fields' => $updated_fields, | |
| 128 | - 'previous_fields' => $saved_fields | |
| 129 | - ]; | |
| 130 | - } | |
| 131 | - | |
| 132 | - /** | |
| 133 | 68 | * Loop all submitted fields and filter their values |
| 134 | 69 | * |
| 135 | 70 | * @param array $fields |
| 136 | 71 | * @param array $form_settings |
| @@ -165,8 +100,11 @@ | ||
| 165 | 100 | * @return void |
| 166 | 101 | */ |
| 167 | 102 | public static function sanitizeFieldRecursive($form_settings, &$fieldValue, $fieldName = '') |
| 168 | 103 | { |
| 104 | + // set filter | |
| 105 | + $filter = \FPFramework\Base\Filter::getInstance(); | |
| 106 | + | |
| 169 | 107 | foreach ($form_settings as $key => $value) |
| 170 | 108 | { |
| 171 | 109 | // Skip if value is not an array |
| 172 | 110 | if (!is_array($value)) |
| @@ -178,11 +116,8 @@ | ||
| 178 | 116 | if (isset($value['name']) && $value['name'] == $fieldName) |
| 179 | 117 | { |
| 180 | 118 | $filterType = isset($value['filter']) ? $value['filter'] : \FPFramework\Base\Filter::defaultFilter; |
| 181 | 119 | |
| 182 | - // set filter | |
| 183 | - $filter = \FPFramework\Base\Filter::getInstance(); | |
| 184 | - | |
| 185 | 120 | $fieldValue = $filter->clean($fieldValue, $filterType); |
| 186 | 121 | return; |
| 187 | 122 | } |
| 188 | 123 | else |
| @@ -258,9 +193,9 @@ | ||
| 258 | 193 | } |
| 259 | 194 | } |
| 260 | 195 | else |
| 261 | 196 | { |
| 262 | - $field = $formPath ? $formPath . '[' . $showOnPartBlocks[0] . ']' : $showOnPartBlocks[0]; | |
| 197 | + $field = $formPath ? $formPath . $showOnPartBlocks[0] : $showOnPartBlocks[0]; | |
| 263 | 198 | } |
| 264 | 199 | |
| 265 | 200 | $showOnData[] = [ |
| 266 | 201 | 'field' => $field, |
| @@ -274,7 +209,7 @@ | ||
| 274 | 209 | $op = ''; |
| 275 | 210 | } |
| 276 | 211 | } |
| 277 | 212 | |
| 278 | - return json_encode($showOnData); | |
| 213 | + return wp_json_encode($showOnData); | |
| 279 | 214 | } |
| 280 | 215 | } |