public function test_update_value() { $result = $this->text_field->update_value('test', $this->ticket_id); global $wpdb; $meta_value = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->postmeta} WHERE meta_key = '%s' AND post_id = '%d'", '_wpas_my_test_field', $this->ticket_id)); $this->assertEquals(1, $result); $this->assertEquals('test', $meta_value->meta_value); }
/** * Save all custom fields given in $data to the database. * * @since 3.2.0 * * @param int $post_id ID of the post being saved * @param array $data Array of data that might contain custom fields values. * @param bool $allow_log Whether or not to allow logging actions. If this is set to false and the custom field is * set to true, $log has higher priority. I tis used to prevent logging on ticket creation. * * @return array Array of custom field / value saved to the database */ public function save_custom_fields($post_id, $data = array(), $allow_log = true) { /* We store all the data to log in here */ $log = array(); /* Store all fields saved to DB and the value saved */ $saved = array(); $fields = $this->get_custom_fields(); /** * wpas_save_custom_fields_before hook * * @since 3.0.0 */ do_action('wpas_save_custom_fields_before', $post_id); foreach ($fields as $field_id => $field) { /** * All name attributes are prefixed with wpas_ * so we need to add it to get the real field ID. */ $field_form_id = "wpas_{$field_id}"; /* Process core fields differently. */ if (true === $field['args']['core']) { if (isset($data[$field_form_id])) { $this->save_core_field($post_id, $field, $data[$field_form_id]); } continue; } /** * Ignore fields in "no edit" mode. * * If we're on the admin and the custom field is set as * "no edit" (by restricting the capability), then the field * won't be passed in the $_POST, which as a result would have * the field deleted. * * If the no edit mode is enabled for the current field, we simply ignore it. */ if (is_admin() && !current_user_can($field['args']['capability'])) { continue; } /** * Get the custom field object. */ $custom_field = new WPAS_Custom_Field($field_id, $field); if (isset($data[$field_form_id])) { $value = $custom_field->get_sanitized_value($data[$field_form_id]); $result = $custom_field->update_value($value, $post_id); } else { /** * This is actually important as passing an empty value * for custom fields that aren't set in the form allows * for deleting values that aren't used from the database. * An unchecked checkbox for instance will not be set * in the form even though the value has to be deleted. */ $value = ''; $result = $custom_field->update_value($value, $post_id); } if (1 === $result || 2 === $result) { $saved[$field['name']] = $value; } if (true === $field['args']['log'] && true === $allow_log) { /** * If the custom field is a taxonomy we need to convert the term ID into its name. * * By checking if $result is different from 0 we make sure that the term actually exists. * If the term didn't exist the save function would have seen it and returned 0. */ if ('taxonomy' === $field['args']['field_type'] && 0 !== $result) { $term = get_term((int) $value, $field['name']); $value = $term->name; } /** * If the "options" parameter is set for this field, we assume it is because * the field type has multiple options. In order to make is more readable, * we try to replace the field value by the value label. * * This process is based on the fact that field types options always follow * the schema option_id => option_label. */ if (isset($field['args']['options']) && is_array($field['args']['options'])) { /* Make sure arrays are still readable */ if (is_array($value)) { $new_values = array(); foreach ($value as $val) { if (array_key_exists($val, $field['args']['options'])) { array_push($new_values, $field['args']['options'][$val]); } } /* Only if all original values were replaced we update the $value var. */ if (count($new_values) === count($value)) { $value = $new_values; } $value = implode(', ', $value); } else { if (array_key_exists($value, $field['args']['options'])) { $value = $field['args']['options'][$value]; } } } $tmp = array('action' => '', 'label' => wpas_get_field_title($field), 'value' => $value, 'field_id' => $field['name']); switch ((int) $result) { case 1: $tmp['action'] = 'added'; break; case 2: $tmp['action'] = 'updated'; break; case 3: $tmp['action'] = 'deleted'; break; } /* Only add this to the log if something was done to the field value */ if (!empty($tmp['action'])) { $log[] = $tmp; } } } /** * Log the changes if any. */ if (!empty($log)) { wpas_log($post_id, $log); } /** * wpas_save_custom_fields_before hook * * @since 3.0.0 */ do_action('wpas_save_custom_fields_after', $post_id); return $saved; }