| @@ -6,9 +6,8 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace BitCode\BitForm\Admin\Form; |
| 9 | 9 | |
| 10 | - | |
| 11 | 10 | class CustomFieldHandler |
| 12 | 11 | { |
| 13 | 12 | public function updatedEntries($formEntries, $fieldDetails) |
| 14 | 13 | { |
| @@ -16,24 +15,36 @@ | ||
| 16 | 15 | foreach ($fieldDetails as $field) { |
| 17 | 16 | $fieldKey = $field['key']; |
| 18 | 17 | if (isset($field['customType']) && !empty($entry->$fieldKey) && !empty($field['customType']->hiddenValue)) { |
| 19 | 18 | $hiddenvalue = $field['customType']->hiddenValue; |
| 20 | - if ($field['customType']->fieldType == 'taxanomy_field') { | |
| 19 | + if ('taxanomy_field' === $field['customType']->fieldType) { | |
| 21 | 20 | $hiddenvalue = $field['customType']->hiddenValue; |
| 22 | - if (isset($field['mul'])) $mul = $field['mul']; | |
| 23 | - else $mul = ''; | |
| 21 | + if (isset($field['mul'])) { | |
| 22 | + $mul = $field['mul']; | |
| 23 | + } else { | |
| 24 | + $mul = ''; | |
| 25 | + } | |
| 26 | + | |
| 24 | 27 | $value = $this->getTermFieldValue($field['type'], $entry->$fieldKey, $mul, $hiddenvalue); |
| 25 | 28 | $formEntries['entries'][$index]->$fieldKey = $value; |
| 26 | - } else if ($field['customType']->fieldType == "user_field") { | |
| 29 | + } elseif ('user_field' === $field['customType']->fieldType) { | |
| 27 | 30 | $hiddenvalue = $field['customType']->hiddenValue; |
| 28 | - if (isset($field['mul'])) $mul = $field['mul']; | |
| 29 | - else $mul = ''; | |
| 31 | + if (isset($field['mul'])) { | |
| 32 | + $mul = $field['mul']; | |
| 33 | + } else { | |
| 34 | + $mul = ''; | |
| 35 | + } | |
| 36 | + | |
| 30 | 37 | $value = $this->getUserFieldValue($field['type'], $entry->$fieldKey, $mul, $hiddenvalue); |
| 31 | 38 | $formEntries['entries'][$index]->$fieldKey = $value; |
| 32 | - } else if ($field['customType']->fieldType == "post_field") { | |
| 39 | + } elseif ('post_field' === $field['customType']->fieldType) { | |
| 33 | 40 | $hiddenvalue = $field['customType']->hiddenValue; |
| 34 | - if (isset($field['mul'])) $mul = $field['mul']; | |
| 35 | - else $mul = ''; | |
| 41 | + if (isset($field['mul'])) { | |
| 42 | + $mul = $field['mul']; | |
| 43 | + } else { | |
| 44 | + $mul = ''; | |
| 45 | + } | |
| 46 | + | |
| 36 | 47 | $value = $this->getPostFieldValue($field['type'], $entry->$fieldKey, $mul, $hiddenvalue); |
| 37 | 48 | $formEntries['entries'][$index]->$fieldKey = $value; |
| 38 | 49 | } |
| 39 | 50 | } |
| @@ -43,25 +54,37 @@ | ||
| 43 | 54 | } |
| 44 | 55 | |
| 45 | 56 | public function updatedData($form_fields, $toUpdateValues) |
| 46 | 57 | { |
| 47 | - foreach ($form_fields as $field) { | |
| 58 | + foreach ($form_fields as $field) { | |
| 48 | 59 | if (isset($field['customType']) && !empty($toUpdateValues[$field['key']])) { |
| 49 | 60 | if (isset($field['customType']->hiddenValue)) { |
| 50 | 61 | $hiddenvalue = $field['customType']->hiddenValue; |
| 51 | - if ($field['customType']->fieldType == 'taxanomy_field') { | |
| 52 | - if (isset($field['mul'])) $mul = $field['mul']; | |
| 53 | - else $mul = ''; | |
| 62 | + if ('taxanomy_field' === $field['customType']->fieldType) { | |
| 63 | + if (isset($field['mul'])) { | |
| 64 | + $mul = $field['mul']; | |
| 65 | + } else { | |
| 66 | + $mul = ''; | |
| 67 | + } | |
| 68 | + | |
| 54 | 69 | $value = $this->getTermFieldValue($field['type'], $toUpdateValues[$field['key']], $mul, $hiddenvalue); |
| 55 | 70 | $toUpdateValues[$field['key']] = $value; |
| 56 | - } else if ($field['customType']->fieldType == "user_field") { | |
| 57 | - if (isset($field['mul'])) $mul = $field['mul']; | |
| 58 | - else $mul = ''; | |
| 71 | + } elseif ('user_field' === $field['customType']->fieldType) { | |
| 72 | + if (isset($field['mul'])) { | |
| 73 | + $mul = $field['mul']; | |
| 74 | + } else { | |
| 75 | + $mul = ''; | |
| 76 | + } | |
| 77 | + | |
| 59 | 78 | $value = $this->getUserFieldValue($field['type'], $toUpdateValues[$field['key']], $mul, $hiddenvalue); |
| 60 | 79 | $toUpdateValues[$field['key']] = $value; |
| 61 | - } else if ($field['customType']->fieldType == "post_field") { | |
| 62 | - if (isset($field['mul'])) $mul = $field['mul']; | |
| 63 | - else $mul = ''; | |
| 80 | + } elseif ('post_field' === $field['customType']->fieldType) { | |
| 81 | + if (isset($field['mul'])) { | |
| 82 | + $mul = $field['mul']; | |
| 83 | + } else { | |
| 84 | + $mul = ''; | |
| 85 | + } | |
| 86 | + | |
| 64 | 87 | $value = $this->getPostFieldValue($field['type'], $toUpdateValues[$field['key']], $mul, $hiddenvalue); |
| 65 | 88 | $toUpdateValues[$field['key']] = $value; |
| 66 | 89 | } |
| 67 | 90 | } |
| @@ -72,37 +95,38 @@ | ||
| 72 | 95 | |
| 73 | 96 | public function getPostFieldValue($type, $fieldValue, $mul, $hiddenvalue) |
| 74 | 97 | { |
| 75 | 98 | $value = ''; |
| 76 | - if ($type == "select") { | |
| 77 | - if ($mul == true) { | |
| 99 | + | |
| 100 | + if ('select' === $type) { | |
| 101 | + if (true === $mul) { | |
| 78 | 102 | $multipleValue = []; |
| 79 | 103 | foreach (explode(',', $fieldValue) as $val) { |
| 80 | 104 | $exists = get_post($val); |
| 81 | - if ($exists != false) { | |
| 105 | + if (false !== $exists) { | |
| 82 | 106 | $multipleValue[] = $exists->$hiddenvalue; |
| 83 | 107 | } |
| 84 | 108 | } |
| 85 | - $value = implode(",", $multipleValue); | |
| 109 | + $value = implode(',', $multipleValue); | |
| 86 | 110 | } else { |
| 87 | 111 | $exists = get_post($fieldValue); |
| 88 | - if ($exists != false) { | |
| 112 | + if (false !== $exists) { | |
| 89 | 113 | $value = is_array($exists->$hiddenvalue) ? $exists->$hiddenvalue : (string) $exists->$hiddenvalue; |
| 90 | 114 | } |
| 91 | 115 | } |
| 92 | - } else if ($type == "radio" || $type == "check") { | |
| 116 | + } elseif ('radio' === $type || 'check' === $type) { | |
| 93 | 117 | if (is_array(json_decode($fieldValue))) { |
| 94 | 118 | $multipleValues = []; |
| 95 | 119 | foreach (json_decode($fieldValue) as $value) { |
| 96 | 120 | $exists = get_post($value); |
| 97 | - if ($exists != false) { | |
| 121 | + if (false !== $exists) { | |
| 98 | 122 | $multipleValues[] = $exists->$hiddenvalue; |
| 99 | 123 | } |
| 100 | 124 | } |
| 101 | - $value = json_encode($multipleValues); | |
| 125 | + $value = wp_json_encode($multipleValues); | |
| 102 | 126 | } else { |
| 103 | 127 | $exists = get_post($fieldValue); |
| 104 | - if ($exists != false) { | |
| 128 | + if (false !== $exists) { | |
| 105 | 129 | $value = $exists->$hiddenvalue; |
| 106 | 130 | } |
| 107 | 131 | } |
| 108 | 132 | } |
| @@ -111,37 +135,37 @@ | ||
| 111 | 135 | |
| 112 | 136 | public function getUserFieldValue($type, $fieldValue, $mul, $hiddenvalue) |
| 113 | 137 | { |
| 114 | 138 | $value = ''; |
| 115 | - if ($type == "select") { | |
| 116 | - if ($mul == true) { | |
| 139 | + if ('select' === $type) { | |
| 140 | + if (true === $mul) { | |
| 117 | 141 | $multipleValue = []; |
| 118 | 142 | foreach (explode(',', $fieldValue) as $val) { |
| 119 | 143 | $exists = get_user_by('ID', $val); |
| 120 | - if ($exists != false) { | |
| 144 | + if (false !== $exists) { | |
| 121 | 145 | $multipleValue[] = $exists->data->$hiddenvalue; |
| 122 | 146 | } |
| 123 | 147 | } |
| 124 | - $value = implode(",", $multipleValue); | |
| 148 | + $value = implode(',', $multipleValue); | |
| 125 | 149 | } else { |
| 126 | - $exists = get_user_by('ID', $fieldValue); | |
| 127 | - if ($exists != false) { | |
| 150 | + $exists = get_user_by('ID', $fieldValue); | |
| 151 | + if (false !== $exists) { | |
| 128 | 152 | $value = is_array($exists->data->$hiddenvalue) ? $exists->data->$hiddenvalue : (string) $exists->data->$hiddenvalue; |
| 129 | 153 | } |
| 130 | 154 | } |
| 131 | - } else if ($type == "radio" || $type == "check") { | |
| 155 | + } elseif ('radio' === $type || 'check' === $type) { | |
| 132 | 156 | if (is_array(json_decode($fieldValue))) { |
| 133 | 157 | $multipleValues = []; |
| 134 | 158 | foreach (json_decode($fieldValue) as $value) { |
| 135 | - $exists = get_user_by('ID', $value); | |
| 136 | - if ($exists != false) { | |
| 159 | + $exists = get_user_by('ID', $value); | |
| 160 | + if (false !== $exists) { | |
| 137 | 161 | $multipleValues[] = $exists->data->$hiddenvalue; |
| 138 | 162 | } |
| 139 | 163 | } |
| 140 | - $value = json_encode($multipleValues); | |
| 164 | + $value = wp_json_encode($multipleValues); | |
| 141 | 165 | } else { |
| 142 | - $exists = get_user_by('ID', $fieldValue); | |
| 143 | - if ($exists != false) { | |
| 166 | + $exists = get_user_by('ID', $fieldValue); | |
| 167 | + if (false !== $exists) { | |
| 144 | 168 | $value = $exists->data->$hiddenvalue; |
| 145 | 169 | } |
| 146 | 170 | } |
| 147 | 171 | } |
| @@ -150,37 +174,37 @@ | ||
| 150 | 174 | |
| 151 | 175 | public function getTermFieldValue($type, $fieldValue, $mul, $hiddenvalue) |
| 152 | 176 | { |
| 153 | 177 | $value = ''; |
| 154 | - if ($type == "select") { | |
| 155 | - if ($mul == true) { | |
| 178 | + if ('select' === $type) { | |
| 179 | + if (true === $mul) { | |
| 156 | 180 | $multipleValue = []; |
| 157 | 181 | foreach (explode(',', $fieldValue) as $val) { |
| 158 | 182 | $exists = get_term_by('term_taxonomy_id', $val); |
| 159 | - if ($exists != false) { | |
| 183 | + if (false !== $exists) { | |
| 160 | 184 | $multipleValue[] = $exists->$hiddenvalue; |
| 161 | 185 | } |
| 162 | 186 | } |
| 163 | - $value = implode(",", $multipleValue); | |
| 187 | + $value = implode(',', $multipleValue); | |
| 164 | 188 | } else { |
| 165 | - $exists = get_term_by('term_taxonomy_id', $fieldValue); | |
| 166 | - if ($exists != false) { | |
| 189 | + $exists = get_term_by('term_taxonomy_id', $fieldValue); | |
| 190 | + if (false !== $exists) { | |
| 167 | 191 | $value = is_array($exists->$hiddenvalue) ? $exists->$hiddenvalue : (string) $exists->$hiddenvalue; |
| 168 | 192 | } |
| 169 | 193 | } |
| 170 | - } else if ($type == "radio" || $type == "check") { | |
| 194 | + } elseif ('radio' === $type || 'check' === $type) { | |
| 171 | 195 | if (is_array(json_decode($fieldValue))) { |
| 172 | 196 | $multipleValues = []; |
| 173 | 197 | foreach (json_decode($fieldValue) as $value) { |
| 174 | 198 | $exists = get_term_by('term_taxonomy_id', $value); |
| 175 | - if ($exists != false) { | |
| 199 | + if (false !== $exists) { | |
| 176 | 200 | $multipleValues[] = $exists->$hiddenvalue; |
| 177 | 201 | } |
| 178 | 202 | } |
| 179 | - $value = json_encode($multipleValues); | |
| 203 | + $value = wp_json_encode($multipleValues); | |
| 180 | 204 | } else { |
| 181 | - $exists = get_term_by('term_taxonomy_id', $fieldValue); | |
| 182 | - if ($exists != false) { | |
| 205 | + $exists = get_term_by('term_taxonomy_id', $fieldValue); | |
| 206 | + if (false !== $exists) { | |
| 183 | 207 | $value = $exists->$hiddenvalue; |
| 184 | 208 | } |
| 185 | 209 | } |
| 186 | 210 | } |