/** * Determine value to be displayed for this option. * * @param bool $is_checked For which value should the output be rendered. * @return string Display value depending on option definition and field display mode * @since 1.9.1 */ public function get_display_value($is_checked = true) { $field_definition_array = $this->field_definition->get_definition_array(); $display_mode = wpcf_getnest($field_definition_array, array('data', 'display'), 'db'); $display_mode = 'value' == $display_mode ? 'value' : 'db'; if ('db' == $display_mode) { return $is_checked ? $this->get_value_to_store() : ''; } else { if ($is_checked) { return wpcf_getarr($this->config, 'display_value'); } else { return ''; } } }
/** * Theoretically this should be placed in self::post_to_intermediate(), but toolset-forms & legacy code * might expect the POST data instead what I think should be the intermediate format. * * First of all, $value is expected to be an array. If checkbox is checked, an element with its id as key and value * will be present. In that case it will be stored in the same way. If it's not checked, we either store nothing * or a zero, depending on the field definition setting "Save empty value". * * @param array|mixed $value * * @return array */ public function intermediate_to_database($value) { $options = $this->field_definition->get_field_options(); $result = array(); foreach ($options as $option_id => $ignored) { $option_is_checked = isset($value[$option_id]); if ($option_is_checked) { $result[$option_id] = $value[$option_id]; } else { if ($this->field_definition->get_should_save_empty_value()) { $result[$option_id] = 0; } } } return $result; }
/** * @param WPCF_Field_Definition $field_definition * @param int $term_id * * @return WP_Error|WP_Error[]|true */ private function update_single_field($field_definition, $term_id) { $field = new WPCF_Field_Instance_Term($field_definition, $term_id); $saver = new WPCF_Field_Data_Saver($field, self::EDIT_FORM_ID); $validation_results = $saver->validate_field_data(); $errors = array(); foreach ($validation_results as $index => $validation_result) { if ($validation_result instanceof WP_Error) { $error_message = sprintf('%s %s', sprintf(__('Field "%s" not updated:', 'wpcf'), $field_definition->get_name()), implode(', ', $validation_result->get_error_data())); $errors[] = new WP_Error('wpcf_field_not_updated', $error_message); } } if (!empty($errors)) { return $errors; } $saving_result = $saver->save_field_data(); return $saving_result; }
/** * Permanently delete field definition. * * That means: * - remove it from all field groups, * - delete field data from the database (sic!) and * - delete the definition itself. * * After calling this method, the field definition object passed as parameter should never be used again. * * @param WPCF_Field_Definition $field_definiton * @return bool */ public function delete_definition($field_definiton) { // We accept only fields that are under Types control if (!$field_definiton instanceof WPCF_Field_Definition) { return false; } if (!$field_definiton->is_under_types_control()) { return false; } // Remove field from all groups $associated_groups = $field_definiton->get_associated_groups(); foreach ($associated_groups as $group) { $group->remove_field_definition($field_definiton); } // Delete field data $is_success = $field_definiton->delete_all_fields(); // Delete the definition $slug_to_delete = $field_definiton->get_slug(); $this->erase_field_definition_from_options($slug_to_delete); return $is_success; }
/** * Update existing field definition. * * @param WPCF_Field_Definition $field_definition * @throws InvalidArgumentException * @return bool True when the update was successful, false otherwise. * @since 2.0 */ public function update_definition($field_definition) { if (!$field_definition instanceof WPCF_Field_Definition) { throw new InvalidArgumentException('Not a Types field definition.'); } $fields_from_options = $this->get_fields_from_options(); if (!array_key_exists($field_definition->get_slug(), $fields_from_options)) { return false; } $this->set_field_definition($field_definition->get_slug(), $field_definition->get_definition_array()); return true; }
/** * Associate a field definition with this group. * * @param WPCF_Field_Definition $field_definition * @return bool True on success, false otherwise. * @since 2.0 */ public function add_field_definition($field_definition) { $field_slugs = $this->get_field_slugs(); $slug_to_add = $field_definition->get_slug(); if (!in_array($slug_to_add, $field_slugs)) { $field_slugs[] = $slug_to_add; $is_success = $this->set_field_slugs($field_slugs); } else { $is_success = true; } return $is_success; }
/** * Remove field definition from this group. * * @param WPCF_Field_Definition $field_definition * @return bool */ public function remove_field_definition($field_definition) { $field_slugs = $this->get_field_slugs(); $slug_to_remove = $field_definition->get_slug(); $key = array_search($slug_to_remove, $field_slugs); if ($key !== false) { unset($field_slugs[$key]); } $is_success = $this->set_field_slugs($field_slugs); return $is_success; }
/** * @inheritdoc * * Adds properties: domain * * @return array * @since 2.0 */ public function to_json() { $object_data = parent::to_json(); $additions = array('domain' => 'users'); return array_merge($object_data, $additions); }