Пример #1
0
 /**
  * Check if there is a multilingual plugin that's active.
  * If support is enabled, but there are no plugins, a notice will be generated
  */
 static function check()
 {
     # If there is an adapter set, it's all okay
     if (isset(self::$adapter)) {
         return true;
     }
     # Check if the multilingual UF functionality is available
     $enabled = get_option('uf_multilingual');
     # The option might not be set but the theme might allow/deny it
     if (defined('UF_ENABLE_MULTILINGUAL')) {
         $enabled = UF_ENABLE_MULTILINGUAL;
     }
     # Stop if not enabled
     if (!$enabled) {
         return false;
     }
     # Now that it's allowed, check adapters
     $available_adapters = array('UF_Qtranslate');
     $available_adapters = apply_filters('uf_ml_adapters', $available_adapters);
     foreach ($available_adapters as $adapter) {
         if (call_user_func(array($adapter, 'check'))) {
             self::$adapter = new $adapter();
             break;
         }
     }
     # After checking adapters
     if (isset(self::$adapter)) {
         return true;
     } else {
         $message = __('There is no multilingual plugin that UF supports being installed or activated! Such a plugin is required for multilingual functionality the active theme or a plugin. Please don\'t edit anything before installing a plugin!', 'uf');
         $message = apply_filters('uf_no_ml_plugin', $message);
         UF_Notices::add($message, true);
         return false;
     }
 }
Пример #2
0
 public function save($data)
 {
     if ($this->is_multilingual) {
         $languages = UF_ML::get();
         $this->value = array();
         foreach ($languages as $l) {
             $this->value[$l['code']] = isset($data[$this->id][$l['code']]);
         }
         $this->value = UF_ML::join($this->value);
     } else {
         $this->value = isset($data[$this->id]);
     }
     $this->datastore->save_value($this->id, $this->value);
 }
Пример #3
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;
}
Пример #4
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;
     }
 }
Пример #5
0
 /**
  * Get setting fields for the settings page.
  * Calls static additional_settings() for child classes.
  * 
  * @param string $field_type The type of the field.
  * @return UF_Field[] The fields for the group in the Fields repeater
  */
 public static function settings_fields($field_type)
 {
     $default_value_description = __('This value will be automatically populated when the field is initially shown.', 'uf');
     $class_name = self::get_class($field_type);
     # Optionally, set a specific type for the default value
     $default_field_type = $field_type;
     if (method_exists($class_name, 'get_default_value_type')) {
         $default_field_type = call_user_func(array($class_name, 'get_default_value_type'));
     }
     # Generic fields, which are available for each field
     $fields = array('field_title' => UF_Field::factory('text', 'field_title', __('Field Title', 'uf'))->set_description(__('This name will appear as a label next to the field.', 'uf'))->make_required()->multilingual(), 'field_id' => UF_Field::factory('text', 'field_id', __('Field ID (key)', 'uf'))->set_description(__('You will be able to get the field\'s value using this ID', 'uf'))->make_required('/[a-z0-9_]+/'), 'description' => UF_Field::factory('text', 'description', __('Description', 'uf'))->multilingual()->set_description(__('The description would appear after the field, just like this one.', 'uf')));
     # A checkbox for enabling multilingual functionality
     if (UF_ML::check() && $field_type != 'repeater') {
         $fields['multilingual'] = UF_Field::factory('checkbox', 'multilingual', __('Multilingual', 'uf'))->set_text(__('Enable multilingual functionality', 'uf'))->set_description(__('If enabled, Ultimate Fields will automatically display fields for each language.', 'uf'));
     }
     if ($field_type != 'repeater') {
         # The default value, which is a field of the same type. This one is not multilingual
         $fields['default_value'] = UF_Field::factory($default_field_type, 'default_value', __('Default Value', 'uf'))->set_description($default_value_description);
     }
     # The site is multilingual
     if (UF_ML::check() && $field_type != 'repeater') {
         $fields['default_value']->set_dependency('multilingual', true, '!=');
         $fields['default_value_multilingual'] = UF_Field::factory($default_field_type, 'default_value_ml', __('Default Value', 'uf'))->multilingual()->set_description($default_value_description)->set_dependency('multilingual');
     }
     # Provide a way to change those fields
     $fields = apply_filters('uf_default_field_settings_fields', $fields, $field_type);
     # Get additional field settings
     if (method_exists($class_name, 'additional_settings')) {
         $fields = array_merge($fields, call_user_func(array($class_name, 'additional_settings')));
     }
     return apply_filters('uf_field_settings_fields', $fields, $field_type);
 }
Пример #6
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();
             }
         }
     }
 }
Пример #7
0
 function save($source)
 {
     if (isset($source[$this->id]) && $source[$this->id] != NULL) {
         $value = $source[$this->id];
         if ($this->is_multilingual) {
             $result_values = array();
             $result_order = array();
             foreach ($value as $code => $language) {
                 $result_values[$code] = $language;
                 unset($result_values[$code]["__" . md5($this->id) . "__"]);
                 if ($this->is_sortable) {
                     $result_order[$code] = $source[$this->id][$code]['order'];
                 }
             }
             if (isset($result_values['order'])) {
                 unset($result_values['order']);
             }
             $result_values = UF_ML::join($result_values);
             $result_order = UF_ML::join($result_order);
             $this->datastore->save_value($this->id, $result_values);
             $this->datastore->save_value('order_' . $this->id, $result_order);
         } else {
             if ($value == NULL) {
                 $value = array();
             }
             unset($value["__" . md5($this->id) . "__"]);
             $this->value = $value;
             if (is_array($this->value) && isset($this->value['order'])) {
                 unset($this->value['order']);
             }
             $this->datastore->save_value($this->id, $this->value);
             if ($this->is_sortable) {
                 $this->order = $source[$this->id]['order'];
                 $this->datastore->save_value('order_' . $this->id, $this->order);
             }
         }
     }
 }
Пример #8
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);
}