Esempio n. 1
0
 function prepare_order()
 {
     if ($this->is_sortable) {
         $order = $this->datastore->get_value('order_' . $this->id);
         if ($this->is_multilingual && $this->language) {
             $order = UF_ML::split($order, $this->language);
         }
         if ($order) {
             $order_val = $order;
             $order = explode($this->separator, $order);
             $this->options = $this->sort_array_by_array($this->options, $order);
         } else {
             $order_val = implode($this->separator, array_keys($this->options));
         }
         $this->order = $order_val;
     }
 }
Esempio n. 2
0
/**
 * Create field objects by a plain array
 * 
 * @param mixed[] $fields - The plain array, containing raw data
 * @param string $container_type The type of the container those fields will be in. Class name
 * @param UF_Field_Repeater $parent If there is a parent set, all processing callbacks will be sent there.
 * @return UF_Field[] $prepared - The fields, ready to be added to a container
 */
function uf_setup_fields($fields, $container_type, $parent = '')
{
    if (is_a($parent, 'UF_Field_Repeater')) {
        $uf_processors = array();
    } else {
        $uf_processors =& $GLOBALS['uf_datastore_getter']->processors;
    }
    $prepared = array();
    if (!is_array($fields)) {
        return;
    }
    foreach ($fields as $field) {
        if ($field['type'] == 'tab_start' || $field['type'] == 'tab_end') {
            # Add the icon as a path
            if ($field['type'] == 'tab_start') {
                if ($type = $field['icon_type']) {
                    if ($type == 'image' && $field['icon_image']) {
                        $field['icon'] = wp_get_attachment_url($field['icon']);
                    } elseif ($type == 'font' && $field['icon_class']) {
                        $field['icon'] = $field['icon_class'];
                    }
                }
            }
            $prepared[] = $field;
        } else {
            $obj = null;
            switch ($field['type']) {
                case 'separator':
                    $obj = UF_Field::factory('separator', 'separator_' . md5(microtime()));
                    break;
                case 'text':
                    $obj = UF_Field::factory($field['type'], $field['field_id']);
                    if (isset($field['autocomplete_suggestions'])) {
                        $obj->add_suggestions(explode("\n", $field['autocomplete_suggestions']));
                    }
                    break;
                case 'select':
                case 'set':
                case 'radio':
                    $obj = UF_Field::factory($field['type'], $field['field_id']);
                    if ($field['values_source'] == 'textarea') {
                        $values = array();
                        if (isset($field['options'])) {
                            foreach ($field['options'] as $option) {
                                $values[$option['key']] = $option['value'];
                            }
                        }
                        $obj->add_options($values);
                    } else {
                        $obj->add_posts(array('posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'post_title', 'post_type' => $field['post_type']));
                    }
                    if (isset($field['sortable']) && $field['sortable'] && $field['type'] == 'set') {
                        $obj->sortable(true);
                    }
                    if (isset($field['jquery_plugin']) && $field['jquery_plugin']) {
                        $obj->chosen();
                    }
                    break;
                case 'textarea':
                case 'richtext':
                    $obj = UF_Field::factory($field['type'], $field['field_id']);
                    $obj->set_rows($field['rows']);
                    break;
                case 'checkbox':
                    $obj = UF_Field::factory('checkbox', $field['field_id']);
                    if (isset($field['text'])) {
                        $obj->set_text($field['text']);
                    }
                    break;
                case 'select_term':
                    $obj = UF_Field::factory('select_term', $field['field_id']);
                    $obj->set_taxonomy($field['taxonomy']);
                    break;
                case 'repeater':
                    $obj = UF_Field::factory('repeater', $field['field_id']);
                    if (isset($field['repeater_fields'])) {
                        foreach ($field['repeater_fields'] as $group) {
                            $sub_fields_arr = uf_setup_fields($group['group_fields'], 'UF_Field_Repeater', $obj);
                            $obj->add_fields($group['key'], array('title' => UF_ML::split($group['title'])), $sub_fields_arr);
                        }
                    }
                    break;
                default:
                    $obj = UF_Field::factory($field['type'], $field['field_id']);
            }
            if ($obj) {
                foreach ($field as $key => $value) {
                    switch ($key) {
                        case 'title':
                        case 'field_title':
                            $obj->set_title(UF_ML::split($value));
                            break;
                        case 'default_value':
                            $obj->set_default_value($value);
                            break;
                        case 'help_text':
                            $obj->set_help_text(UF_ML::split($value));
                            break;
                        case 'description':
                            $obj->set_description(UF_ML::split($value));
                            break;
                        case 'multilingual':
                            if ($value) {
                                $obj->multilingual();
                            }
                            break;
                    }
                }
                # Add the field as a processor
                if (method_exists($obj, 'process_value')) {
                    if (!isset($uf_processors[$container_type])) {
                        $uf_processors[$container_type] = array();
                    }
                    if (!isset($uf_processors[$container_type][$field['field_id']])) {
                        $uf_processors[$container_type][$field['field_id']] = array(10 => array());
                    }
                    $uf_processors[$container_type][$field['field_id']][10][] = array('callback' => array($obj, 'process_value'), 'data' => $field);
                }
                /**
                 * Modifies the field.
                 * 
                 * When the field is created, additional information might need
                 * to be set up. You can do it here, as the object is passed by reference.
                 * 
                 * @since 2.0
                 * 
                 * @param UF_Field $object The generated field.
                 * @param mixed[] $field_data The all settings of the field as saved in the admin.
                 */
                do_action('uf_setup_field', $obj, $field);
                $prepared[] = $obj;
            }
        }
    }
    if (is_a($parent, 'UF_Field_Repeater')) {
        $parent->processors = $uf_processors['UF_Field_Repeater'];
    }
    return $prepared;
}
Esempio n. 3
0
 /**
  * Output the value of a particular fields. If settings are available, they will be used to tranform the value.
  *
  * @param string $key The key of the field
  * @param string|int Either an ID of the requested post or something like <datastore_type>_<id> if another type is needed
  */
 public function uf($key, $type = null, $echo = true)
 {
     # Prepare the item and fetch the normal value
     $item = $this->prepare_type($type);
     /*if($key == 'ban_image')
     		echo $item->type;*/
     # Now that the type and ID are determined, prepare the datastore
     if (method_exists($this->datastores[$item->type], 'get_current_item_value')) {
         # There is a static method that will provide the value
         $value = call_user_func(array($this->datastores[$item->type], 'get_current_item_value'), $key);
     } else {
         $datastore = new $this->datastores[$item->type]();
         if ($item->id) {
             $datastore->set_id($item->id);
         }
         $value = $datastore->get_value($key);
     }
     # Extract the value if it is multilingual
     $value = UF_ML::split($value);
     # The value will be processed here
     $type_class = $this->datastores[$item->type];
     if (isset($this->processors[$type_class]) && isset($this->processors[$type_class][$key])) {
         foreach ($this->processors[$type_class][$key] as $priority => $processors) {
             foreach ($processors as $processor) {
                 $value = call_user_func($processor['callback'], $value, $processor['data']);
             }
         }
     }
     # Output the final result
     if ($echo) {
         echo $value;
     } else {
         return $value;
     }
 }
Esempio n. 4
0
 private function base_display_input()
 {
     if (!$this->is_multilingual) {
         $this->display_input();
         return;
     }
     $languages = UF_ML::get();
     if (count($languages) > 1) {
         # Backup vars
         $id = $this->input_id;
         $value = $this->value;
         $width = 25;
         echo '<div class="uf-lang-wrap" style="padding-right:' . $width * count($languages) . 'px;">';
         $this->display_language_switcher();
         # Display inputs
         foreach ($languages as $language) {
             $this->language = $language['code'];
             $this->input_id = $id . '[' . $language['code'] . ']';
             $this->value = UF_ML::split($value, $language['code']);
             echo '<div class="lang-input lang-input-' . $language['code'] . '">';
             $this->display_input();
             echo '</div>';
         }
         echo '</div>';
         # Restore vars
         $this->language = null;
         $this->input_id = $id;
         $this->value = $value;
     } else {
         $this->display_input();
     }
 }
Esempio n. 5
0
 /**
  * Made for bulk fields adding
  * 
  * @param mixed[] $items The items that are stored in the dabase or loaded dynamically
  */
 public function add_fields_array(array $items)
 {
     $tabIndex = 0;
     $items = apply_filters('uf_add_fields_array', $items);
     foreach ($items as $item) {
         if (is_a($item, 'UF_Field')) {
             $this->add_field($item);
         } else {
             if ($item['type'] == 'tab_start') {
                 $icon = isset($item['icon']) ? $item['icon'] : '';
                 $this->start_tab('tab-' . $tabIndex++, UF_ML::split($item['title']), $icon);
             } else {
                 $this->end_tab();
             }
         }
     }
 }
Esempio n. 6
0
/**
 * Setup post meta container
 * 
 * @param UF_Field[] $fields
 * @param mixed[] $data
 */
function uf_setup_postmeta_box($fields, $data)
{
    $args = array();
    # Extract what we actually care about
    extract(uf_parse_args_array($data['meta'], array('posttype', 'templates', 'levels'), 'uf_postmeta_'));
    # Don't do anything in some cases
    if (!isset($posttype) || !is_array($posttype) || empty($posttype)) {
        return;
    }
    # Prepare the title
    $title = UF_ML::split($data['meta']['uf_title']);
    $args['title'] = $title;
    # If there's a slug set, it will be used
    if (isset($data['meta']['uf_options_page_slug']) && $data['meta']['uf_options_page_slug']) {
        $args['id'] = $data['meta']['uf_options_page_slug'];
    }
    # Create the page
    $classname = apply_filters('uf_postmeta_classname', 'UF_Postmeta', $data);
    $container = call_user_func($classname . '::box', $title, $posttype, $args);
    # Prepare the description
    if ($description = UF_ML::split($data['post']->post_content)) {
        $container->set_description($description);
    }
    # Choose templates if set
    if (in_array('page', $posttype) && isset($templates) && is_array($templates) && $templates) {
        $container->set_templates($templates);
    }
    # Add level
    if ($levels) {
        $container->set_levels($levels);
    }
    # Set taxonomy info
    $taxonomies = get_taxonomies(array('show_ui' => 1), 'objects');
    foreach ($taxonomies as $id => $taxonomy) {
        # Only hierarchical taxonomies have checkboxes
        if (!$taxonomy->hierarchical) {
            continue;
        }
        if (isset($data['meta']["uf_postmeta_terms_{$id}"]) && is_array($data['meta']["uf_postmeta_terms_{$id}"])) {
            $terms = $data['meta']["uf_postmeta_terms_{$id}"];
            if (!empty($terms)) {
                foreach ($terms as $term) {
                    $container->add_term($id, $term);
                }
            }
        }
    }
    # Add the fields
    $container->add_fields_array($fields);
}