/**
  * Summary.
  *
  * Description.
  *
  * @since x.x.x
  * @access (for functions: only use if private)
  *
  * @see Function/method/class relied on
  * @link URL
  * @global type $varname Description.
  * @global type $varname Description.
  *
  * @param type $var Description.
  * @param type $var Optional. Description.
  * @return type Description.
  */
 private function get_available_types($type = false)
 {
     $allowed = array();
     if ($type) {
         include_once WPCF_INC_ABSPATH . '/fields.php';
         $allowed = wpcf_admin_custom_fields_change_type_allowed_matrix();
         if (isset($allowed[$type])) {
             $allowed = $allowed[$type];
         } else {
             $allowed = array($type);
         }
     }
     $options = array();
     $options_disabled = array();
     $fields_registered = wpcf_admin_fields_get_available_types();
     foreach ($fields_registered as $field_slug => $data) {
         $one = array('#name' => $data['title'], '#value' => $field_slug, '#title' => $data['title']);
         if (!empty($allowed) && !in_array($field_slug, $allowed)) {
             $one['#attributes'] = array('disabled' => 'disabled');
             $one['#title'] .= sprintf(' - %s', __('not allowed', 'wpcf'));
             $options_disabled[] = $one;
         } else {
             $options[] = $one;
         }
     }
     return array_merge($options, $options_disabled);
 }
Example #2
0
/**
 * Changes field type.
 * Modified by Gen, 13.02.2013
 *
 * @param $fields
 * @param $type
 */
function wpcf_admin_custom_fields_change_type($fields, $type, $post_type = TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, $meta_name = 'wpcf-fields')
{
    if (!is_array($fields)) {
        $fields = array(strval($fields));
    }
    $fields = wpcf_types_cf_under_control('add', array('fields' => $fields, 'type' => $type), $post_type, $meta_name);
    $allowed = wpcf_admin_custom_fields_change_type_allowed_matrix();
    $all_fields = wpcf_admin_fields_get_fields(false, false, false, $meta_name);
    foreach ($fields as $field_id) {
        if (!isset($all_fields[$field_id])) {
            continue;
        }
        $field = $all_fields[$field_id];
        if (isset($allowed[$field['type']]) && !in_array($type, $allowed[$field['type']])) {
            wpcf_admin_message_store(sprintf(__('Field "%s" type was converted from %s to %s. You need to set some further settings in the group editor.', 'wpcf'), $field['name'], $field['type'], $type), 'notice notice-warning');
            $all_fields[$field_id]['data']['disabled_by_type'] = 1;
        } else {
            $all_fields[$field_id]['data']['disabled'] = 0;
            $all_fields[$field_id]['data']['disabled_by_type'] = 0;
            wpcf_admin_message_store(sprintf(__('Field "%s" type was converted successful from %s to %s', 'wpcf'), $field['name'], $field['type'], $type));
        }
        if ($field['type'] == 'numeric' && isset($all_fields[$field_id]['data']['validate']['number'])) {
            unset($all_fields[$field_id]['data']['validate']['number']);
        } else {
            if ($type == 'numeric') {
                $all_fields[$field_id]['data']['validate'] = array('number' => array('active' => true, 'message' => __('Please enter numeric data', 'wpcf')));
            }
        }
        $all_fields[$field_id]['type'] = $type;
    }
    update_option($meta_name, $all_fields);
}