Beispiel #1
0
/**
 * Creates form elements.
 * 
 * @param type $post
 * @param type $fields
 * @return type 
 */
function wpcf_admin_post_process_fields($post = false, $fields = array(), $use_cache = true, $add_to_editor = true, $context = 'group')
{
    global $pagenow;
    static $count = array();
    //Need this to count if there are more than one same field on page
    // Get cached
    static $cache = array();
    $cache_key = !empty($post->ID) ? $post->ID . md5(serialize($fields)) : false;
    if ($use_cache && $cache_key && isset($cache[$cache_key])) {
        return $cache[$cache_key];
    }
    $fields_processed = array();
    // Get invalid fields (if submitted)
    $invalid_fields = array();
    if ($post) {
        $invalid_fields = get_post_meta($post->ID, 'wpcf-invalid-fields', true);
        delete_post_meta($post->ID, 'wpcf-invalid-fields');
    }
    $original_cf = array();
    if (function_exists('wpml_get_copied_fields_for_post_edit')) {
        $original_cf = wpml_get_copied_fields_for_post_edit();
    }
    foreach ($fields as $field) {
        // Repetitive fields
        if (wpcf_admin_is_repetitive($field) && $context != 'post_relationship') {
            $temp_flag = false;
            if (!empty($post->ID)) {
                $temp_fields = get_post_meta($post->ID, wpcf_types_get_meta_prefix($field) . $field['slug'], false);
                if (!empty($temp_fields)) {
                    $temp_start = true;
                    $fields_processed[$field['id'] . '_repetitive_wrapper_open'] = array('#type' => 'markup', '#markup' => '<div id="wpcf_' . $field['id'] . '_repetitive_wrapper_' . mt_rand() . '" class="wpcf-repetitive-wrapper">', '#id' => $field['id'] . '_repetitive_wrapper_open');
                    foreach ($temp_fields as $temp_key => $temp_field) {
                        $single_field = $field;
                        $single_field['value'] = $temp_field;
                        $temp_processed = wpcf_admin_post_process_field($post, $single_field, $use_cache, $add_to_editor, $context);
                        if ($temp_processed) {
                            $single_field = $temp_processed['field'];
                            $element = $temp_processed['element'];
                            if ($field['type'] == 'skype') {
                                foreach ($element as $temp_element_key => $temp_element_value) {
                                    if (!$temp_start && !in_array($field['type'], array('checkbox'))) {
                                        unset($temp_element_value['#title']);
                                    }
                                    $temp_start = false;
                                    $fields_processed[$temp_element_value['#id']] = apply_filters('wpcf_post_edit_field', $temp_element_value, $field, $post, $context, $original_cf, $invalid_fields);
                                }
                            } else {
                                if (!$temp_start && !in_array($field['type'], array('checkbox'))) {
                                    unset($element['#title']);
                                }
                                $temp_start = false;
                                $fields_processed[$element['#id']] = apply_filters('wpcf_post_edit_field', $element, $single_field, $post, $context, $original_cf, $invalid_fields);
                            }
                        }
                    }
                    $fields_processed[$field['id'] . '_repetitive_wrapper_close'] = array('#type' => 'markup', '#markup' => '</div>', '#id' => $field['id'] . '_repetitive_wrapper_close');
                } else {
                    $temp_flag = true;
                }
            } else {
                if (!empty($original_cf['fields'])) {
                    foreach ($original_cf['fields'] as $cf_id) {
                        if (wpcf_types_get_meta_prefix($field) . $field['slug'] == $cf_id) {
                            $temp_fields = get_post_meta($original_cf['original_post_id'], wpcf_types_get_meta_prefix($field) . $field['slug'], false);
                            if (!empty($temp_fields)) {
                                foreach ($temp_fields as $temp_key => $temp_field) {
                                    $single_field = $field;
                                    $single_field['wpml_action'] = 'copy';
                                    $single_field['value'] = $temp_field;
                                    $temp_processed = wpcf_admin_post_process_field($post, $single_field, $use_cache, $add_to_editor, $context);
                                    if ($temp_processed) {
                                        $single_field = $temp_processed['field'];
                                        $element = $temp_processed['element'];
                                        if ($field['type'] == 'skype') {
                                            foreach ($element as $temp_element_key => $temp_element_value) {
                                                $fields_processed[$temp_element_value['#id']] = apply_filters('wpcf_post_edit_field', $temp_element_value, $field, $post, $context, $original_cf, $invalid_fields);
                                            }
                                        } else {
                                            $fields_processed[$element['#id']] = apply_filters('wpcf_post_edit_field', $element, $single_field, $post, $context, $original_cf, $invalid_fields);
                                        }
                                    }
                                }
                            } else {
                                $temp_flag = true;
                            }
                            break;
                        }
                    }
                } else {
                    $temp_flag = true;
                }
            }
            if ($temp_flag == true) {
                $temp_processed = wpcf_admin_post_process_field($post, $field, $use_cache, $add_to_editor, $context);
                if ($temp_processed) {
                    $fields_processed[$field['id'] . '_repetitive_wrapper_open'] = array('#type' => 'markup', '#markup' => '<div id="wpcf_' . $field['id'] . '_repetitive_wrapper_' . mt_rand() . '" class="wpcf-repetitive-wrapper">', '#id' => $field['id'] . '_repetitive_wrapper_open');
                    $field = $temp_processed['field'];
                    $element = $temp_processed['element'];
                    if ($field['type'] == 'skype') {
                        foreach ($element as $temp_element_key => $temp_element_value) {
                            $fields_processed[$temp_element_value['#id']] = apply_filters('wpcf_post_edit_field', $temp_element_value, $field, $post, $context, $original_cf, $invalid_fields);
                        }
                    } else {
                        $fields_processed[$element['#id']] = apply_filters('wpcf_post_edit_field', $element, $field, $post, $context, $original_cf, $invalid_fields);
                    }
                    $fields_processed[$field['id'] . '_repetitive_wrapper_close'] = array('#type' => 'markup', '#markup' => '</div>', '#id' => $field['id'] . '_repetitive_wrapper_close');
                }
            }
        } else {
            if ($post) {
                $field['value'] = get_post_meta($post->ID, wpcf_types_get_meta_prefix($field) . $field['slug'], true);
            } else {
                // see if it's in the original custom fields to copy.
                if (!empty($original_cf['fields'])) {
                    foreach ($original_cf['fields'] as $cf_id) {
                        if (wpcf_types_get_meta_prefix($field) . $field['slug'] == $cf_id) {
                            $field['wpml_action'] = 'copy';
                            $field['value'] = get_post_meta($original_cf['original_post_id'], wpcf_types_get_meta_prefix($field) . $field['slug'], true);
                            break;
                        }
                    }
                }
            }
            $temp_processed = wpcf_admin_post_process_field($post, $field, $use_cache, $add_to_editor, $context);
            if ($temp_processed) {
                $field = $temp_processed['field'];
                $element = $temp_processed['element'];
                if ($field['type'] == 'skype') {
                    foreach ($element as $temp_element_key => $temp_element_value) {
                        $fields_processed[$temp_element_value['#id']] = apply_filters('wpcf_post_edit_field', $temp_element_value, $field, $post, $context, $original_cf, $invalid_fields);
                    }
                } else {
                    $fields_processed[$element['#id']] = apply_filters('wpcf_post_edit_field', $element, $field, $post, $context, $original_cf, $invalid_fields);
                }
            }
        }
    }
    if ($cache_key) {
        $cache[$cache_key] = $fields_processed;
    }
    return $fields_processed;
}
Beispiel #2
0
 /**
  * Sets field meta box form.
  *
  * @return type
  */
 function _get_meta_form($meta_value = null, $meta_id = null, $wrap = true)
 {
     /*
      * Note that field may be registered outside of Types.
      * In that case, it's on developer to make sure it's loaded.
      */
     $this->_include_file_by_field_type($this->cf['type']);
     /*
      * Set value
      *
      * IMPORTANT
      * Here we set values for form elements
      */
     $this->cf['value'] = is_null($meta_value) ? $this->meta : $meta_value;
     $form = array();
     $form_meta_box = array();
     $inherited = array();
     $this->__multiple = false;
     // Open main wrapper
     if ($wrap) {
         $form['__meta_form_OPEN'] = array('#type' => 'markup', '#markup' => '' . '<div id="wpcf_wrapper_' . $this->unique_id . '" class="wpcf-wrap wpcf-meta-form">');
     }
     /*
      *
      *
      *
      *
      * Since Types 1.2
      * Avoid using parent (inherited) type
      * $this->config->inherited_field_type
      */
     // See if inherited data and merge
     if (isset($this->config->inherited_field_type)) {
         if (!array_key_exists($this->cf['type'], $this->_deprecated_inherited_allowed())) {
             //                _deprecated_argument( 'inherited_field_type', '1.2',
             //                        'Since Types 1.2 we encourage developers to completely define fields' );
         }
         $file = $this->_include_file_by_field_type($this->config->inherited_field_type);
         if (file_exists($file)) {
             if (function_exists('wpcf_fields_' . $this->config->inherited_field_type . '_meta_box_form')) {
                 $inherited = call_user_func_array('wpcf_fields_' . $this->config->inherited_field_type . '_meta_box_form', array($this->cf, $this));
                 // If single form - convert to array of elements
                 if (isset($inherited['#type'])) {
                     $inherited = array('wpcf_field_' . $this->unique_id => $inherited);
                 }
                 // One value?
                 if (count($inherited) > 1) {
                     $this->__multiple = true;
                 }
                 $form = $form + $inherited;
             }
         }
     }
     $func = 'wpcf_fields_' . $this->cf['type'] . '_meta_box_form';
     if (is_callable($func)) {
         /*
          *
          * From Types 1.2 use complete form setup
          */
         $form_meta_box = call_user_func_array('wpcf_fields_' . $this->cf['type'] . '_meta_box_form', array($this->cf, $this));
         // If single form - convert to array of elements
         if (isset($form_meta_box['#type'])) {
             $form_meta_box = array('wpcf_field_' . $this->unique_id => $form_meta_box);
         }
         // One value?
         if (count($form_meta_box) > 1) {
             $this->__multiple = true;
         }
         // Merge
         $form = array_merge($form, $form_meta_box);
     }
     if (!empty($form)) {
         // Process each field
         foreach ($form as $element_key => $element) {
             /*
              *
              * Start using __ in keys to skip element
              */
             // Skip protected
             if (strpos($element_key, '__') === 0) {
                 $form[$element_key] = $element;
                 continue;
             }
             // Add title and description
             // TODO WPML
             if (empty($started)) {
                 $_title = isset($element['#title']) ? $element['#title'] : $this->cf['name'];
                 $element['#title'] = wpcf_translate('field ' . $this->cf['id'] . ' name', $_title);
                 // Add asterisk for required fields
                 if (isset($this->cf['data']['validate']['required'])) {
                     $element['#title'] .= '&#42;';
                 }
                 $_description = isset($element['#description']) ? $element['#description'] : $this->cf['description'];
                 $element['#description'] = wpautop(wpcf_translate('field ' . $this->cf['id'] . ' description', $_description));
                 $started = true;
             }
             // Set current element
             $this->__current_form_element = $element;
             // Process field
             $temp_processed = wpcf_admin_post_process_field($this);
             if (!empty($temp_processed['element'])) {
                 $element = $temp_processed['element'];
             }
             // Set form element
             $form[$element_key] = apply_filters('wpcf_post_edit_field', $element, $this->cf, $this->post, $this->context);
         }
         // Add to editor
         if ($this->add_to_editor) {
             wpcf_admin_post_add_to_editor($this->cf);
         }
         $this->enqueue_script();
         $this->enqueue_style();
     }
     // Close main wrapper
     if ($wrap) {
         $form['meta_form_CLOSE'] = array('#type' => 'markup', '#markup' => '' . '</div>');
     }
     // Add unique IDs
     foreach ($form as $k => $v) {
         $_form[$k . '_' . $this->unique_id] = $v;
     }
     return apply_filters('wpcf_meta_form', $_form);
 }
/**
 * Creates form elements.
 * 
 * @param type $post
 * @param type $fields
 * @return type 
 */
function wpcf_admin_post_process_fields($post = false, $fields = array(), $use_cache = true, $add_to_editor = true, $context = 'group')
{
    // Get cached
    static $cache = array();
    $cache_key = !empty($post->ID) ? $post->ID . md5(serialize($fields)) : false;
    if ($use_cache && $cache_key && isset($cache[$cache_key])) {
        return $cache[$cache_key];
    }
    $fields_processed = array();
    // Get invalid fields (if submitted)
    $invalid_fields = array();
    if (!empty($post->ID)) {
        $invalid_fields = get_post_meta($post->ID, 'wpcf-invalid-fields', true);
        delete_post_meta($post->ID, 'wpcf-invalid-fields');
    }
    $original_cf = array();
    if (function_exists('wpml_get_copied_fields_for_post_edit')) {
        $original_cf = wpml_get_copied_fields_for_post_edit();
    }
    foreach ($fields as $field) {
        // Repetitive fields
        if (wpcf_admin_is_repetitive($field) && $context != 'post_relationship') {
            $temp_flag = false;
            // First check if repetitive fields are copied using WPML
            if (!empty($original_cf['fields'])) {
                // Check if marked
                if (in_array(wpcf_types_get_meta_prefix($field) . $field['slug'], $original_cf['fields'])) {
                    // Get original post field values
                    $temp_fields = get_post_meta($original_cf['original_post_id'], wpcf_types_get_meta_prefix($field) . $field['slug'], false);
                    // If there are original field values stored
                    if (!empty($temp_fields)) {
                        foreach ($temp_fields as $temp_key => $temp_field) {
                            $single_field = $field;
                            $single_field['wpml_action'] = 'copy';
                            $single_field['value'] = $temp_field;
                            $temp_processed = wpcf_admin_post_process_field($post, $single_field, $use_cache, $add_to_editor, $context);
                            if ($temp_processed) {
                                $single_field = $temp_processed['field'];
                                $element = $temp_processed['element'];
                                if ($field['type'] == 'skype') {
                                    foreach ($element as $temp_element_key => $temp_element_value) {
                                        $fields_processed[$temp_element_value['#id']] = apply_filters('wpcf_post_edit_field', $temp_element_value, $field, $post, $context, $original_cf, $invalid_fields);
                                    }
                                } else {
                                    $fields_processed[$element['#id']] = apply_filters('wpcf_post_edit_field', $element, $single_field, $post, $context, $original_cf, $invalid_fields);
                                }
                            }
                        }
                        // If there are no original fields stored
                        // display empty element
                    } else {
                        $single_field = $field;
                        $single_field['wpml_action'] = 'copy';
                        $single_field['value'] = null;
                        $temp_processed = wpcf_admin_post_process_field($post, $single_field, $use_cache, $add_to_editor, $context);
                        if ($temp_processed) {
                            $single_field = $temp_processed['field'];
                            $element = $temp_processed['element'];
                            if ($field['type'] == 'skype') {
                                foreach ($element as $temp_element_key => $temp_element_value) {
                                    $fields_processed[$temp_element_value['#id']] = apply_filters('wpcf_post_edit_field', $temp_element_value, $field, $post, $context, $original_cf, $invalid_fields);
                                }
                            } else {
                                $fields_processed[$element['#id']] = apply_filters('wpcf_post_edit_field', $element, $single_field, $post, $context, $original_cf, $invalid_fields);
                            }
                        }
                    }
                } else {
                    $temp_flag = true;
                }
            } else {
                $temp_flag = true;
            }
            // Get repetitive fields values
            if ($temp_flag && !empty($post->ID)) {
                $temp_flag = false;
                $temp_fields = get_post_meta($post->ID, wpcf_types_get_meta_prefix($field) . $field['slug'], false);
                if (!empty($temp_fields)) {
                    $temp_start = true;
                    $fields_processed[$field['id'] . '_repetitive_wrapper_open'] = array('#type' => 'markup', '#markup' => '<div id="wpcf_' . $field['id'] . '_repetitive_wrapper_' . mt_rand() . '" class="wpcf-repetitive-wrapper">', '#id' => $field['id'] . '_repetitive_wrapper_open');
                    foreach ($temp_fields as $temp_key => $temp_field) {
                        $single_field = $field;
                        $single_field['value'] = $temp_field;
                        $temp_processed = wpcf_admin_post_process_field($post, $single_field, $use_cache, $add_to_editor, $context);
                        if ($temp_processed) {
                            $single_field = $temp_processed['field'];
                            $element = $temp_processed['element'];
                            if ($field['type'] == 'skype') {
                                foreach ($element as $temp_element_key => $temp_element_value) {
                                    if (!$temp_start && !in_array($field['type'], array('checkbox'))) {
                                        unset($temp_element_value['#title']);
                                    }
                                    $temp_start = false;
                                    $fields_processed[$temp_element_value['#id']] = apply_filters('wpcf_post_edit_field', $temp_element_value, $field, $post, $context, $original_cf, $invalid_fields);
                                }
                            } else {
                                if (!$temp_start && !in_array($field['type'], array('checkbox'))) {
                                    unset($element['#title']);
                                }
                                $temp_start = false;
                                $fields_processed[$element['#id']] = apply_filters('wpcf_post_edit_field', $element, $single_field, $post, $context, $original_cf, $invalid_fields);
                            }
                        }
                    }
                    $fields_processed[$field['id'] . '_repetitive_wrapper_close'] = array('#type' => 'markup', '#markup' => '</div>', '#id' => $field['id'] . '_repetitive_wrapper_close');
                } else {
                    $temp_flag = true;
                }
            }
            // Temp flag for repetitive field is triggered if post is new
            // and field is not marked to be copied
            if ($temp_flag == true) {
                $temp_processed = wpcf_admin_post_process_field($post, $field, $use_cache, $add_to_editor, $context);
                if ($temp_processed) {
                    $fields_processed[$field['id'] . '_repetitive_wrapper_open'] = array('#type' => 'markup', '#markup' => '<div id="wpcf_' . $field['id'] . '_repetitive_wrapper_' . mt_rand() . '" class="wpcf-repetitive-wrapper">', '#id' => $field['id'] . '_repetitive_wrapper_open');
                    $field = $temp_processed['field'];
                    $element = $temp_processed['element'];
                    if ($field['type'] == 'skype') {
                        foreach ($element as $temp_element_key => $temp_element_value) {
                            $fields_processed[$temp_element_value['#id']] = apply_filters('wpcf_post_edit_field', $temp_element_value, $field, $post, $context, $original_cf, $invalid_fields);
                        }
                    } else {
                        $fields_processed[$element['#id']] = apply_filters('wpcf_post_edit_field', $element, $field, $post, $context, $original_cf, $invalid_fields);
                    }
                    $fields_processed[$field['id'] . '_repetitive_wrapper_close'] = array('#type' => 'markup', '#markup' => '</div>', '#id' => $field['id'] . '_repetitive_wrapper_close');
                }
            }
            // Non-repetitive fields
        } else {
            if (!empty($post->ID)) {
                $field['value'] = get_post_meta($post->ID, wpcf_types_get_meta_prefix($field) . $field['slug'], true);
            }
            // Check if repetitive field is copied using WPML
            if (!empty($original_cf['fields'])) {
                if (in_array(wpcf_types_get_meta_prefix($field) . $field['slug'], $original_cf['fields'])) {
                    $field['wpml_action'] = 'copy';
                    $field['value'] = get_post_meta($original_cf['original_post_id'], wpcf_types_get_meta_prefix($field) . $field['slug'], true);
                }
            }
            $temp_processed = wpcf_admin_post_process_field($post, $field, $use_cache, $add_to_editor, $context);
            if ($temp_processed) {
                $field = $temp_processed['field'];
                $element = $temp_processed['element'];
                if ($field['type'] == 'skype') {
                    foreach ($element as $temp_element_key => $temp_element_value) {
                        $fields_processed[$temp_element_value['#id']] = apply_filters('wpcf_post_edit_field', $temp_element_value, $field, $post, $context, $original_cf, $invalid_fields);
                    }
                } else {
                    $fields_processed[$element['#id']] = apply_filters('wpcf_post_edit_field', $element, $field, $post, $context, $original_cf, $invalid_fields);
                }
            }
        }
    }
    // Cache results
    if ($cache_key) {
        $cache[$cache_key] = $fields_processed;
    }
    return $fields_processed;
}