示例#1
0
/**
 * Returns meta_key type for specific field type.
 * 
 * @param type $type
 * @return type 
 */
function types_get_field_type($type)
{
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
    $data = wpcf_fields_type_action($type);
    if (!empty($data['meta_key_type'])) {
        return $data['meta_key_type'];
    }
    return 'CHAR';
}
示例#2
0
/**
 * Processes field form data.
 * 
 * @param type $type
 * @param type $form_data
 * @return type 
 */
function wpcf_fields_get_field_form_data($type, $form_data = array())
{
    // Get field type data
    $field_data = wpcf_fields_type_action($type);
    if (!empty($field_data)) {
        //        require_once $filename;
        $form = array();
        // Set right ID if existing field
        if (isset($form_data['submitted_key'])) {
            $id = $form_data['submitted_key'];
        } else {
            $id = $type . '-' . rand();
        }
        // Set remove link
        $remove_link = isset($form_data['group_id']) ? admin_url('admin-ajax.php?' . 'wpcf_ajax_callback=wpcfFieldsFormDeleteElement&wpcf_warning=' . __('Are you sure?', 'wpcf') . '&action=wpcf_ajax&wpcf_action=remove_field_from_group' . '&group_id=' . intval($form_data['group_id']) . '&field_id=' . $form_data['id']) . '&_wpnonce=' . wp_create_nonce('remove_field_from_group') : admin_url('admin-ajax.php?' . 'wpcf_ajax_callback=wpcfFieldsFormDeleteElement&wpcf_warning=' . __('Are you sure?', 'wpcf') . '&action=wpcf_ajax&wpcf_action=remove_field_from_group') . '&_wpnonce=' . wp_create_nonce('remove_field_from_group');
        // Set move button
        $form['wpcf-' . $id . '-control'] = array('#type' => 'markup', '#markup' => '<img src="' . WPCF_RES_RELPATH . '/images/move.png" class="wpcf-fields-form-move-field" alt="' . __('Move this field', 'wpcf') . '" /><a href="' . $remove_link . '" ' . 'class="wpcf-form-fields-delete wpcf-ajax-link">' . '<img src="' . WPCF_RES_RELPATH . '/images/delete-2.png" alt="' . __('Delete this field', 'wpcf') . '" /></a>');
        // Set fieldset
        $collapsed = wpcf_admin_fields_form_fieldset_is_collapsed('fieldset-' . $id);
        // Set collapsed on AJAX call (insert)
        $collapsed = defined('DOING_AJAX') ? false : $collapsed;
        // Set title
        $title = !empty($form_data['name']) ? $form_data['name'] : __('Untitled', 'wpcf');
        $title = '<span class="wpcf-legend-update">' . $title . '</span> - ' . sprintf(__('%s field', 'wpcf'), $field_data['title']);
        $form['wpcf-' . $id] = array('#type' => 'fieldset', '#title' => $title, '#id' => 'fieldset-' . $id, '#collapsible' => true, '#collapsed' => $collapsed);
        // Get init data
        $field_init_data = wpcf_fields_type_action($type);
        // See if field inherits some other
        $inherited_field_data = false;
        if (isset($field_init_data['inherited_field_type'])) {
            $inherited_field_data = wpcf_fields_type_action($field_init_data['inherited_field_type']);
        }
        $form_field = array();
        // Force name and description
        $form_field['name'] = array('#type' => 'textfield', '#name' => 'name', '#attributes' => array('class' => 'wpcf-forms-set-legend', 'style' => 'width:100%;margin:10px 0 10px 0;'), '#validate' => array('required' => array('value' => true)), '#inline' => true, '#value' => __('Enter field name', 'wpcf'));
        if (empty($form_data['name'])) {
            $form_field['name']['#attributes']['onclick'] = 'if (jQuery(this).val() == \'' . __('Enter field name', 'wpcf') . '\') { jQuery(this).val(\'\'); }';
            $form_field['name']['#attributes']['onblur'] = 'if (jQuery(this).val() == \'\') { jQuery(this).val(\'' . __('Enter field name', 'wpcf') . '\') }';
        }
        $form_field['slug'] = array('#type' => 'textfield', '#name' => 'slug', '#attributes' => array('style' => 'width:100%;margin:0 0 10px 0;'), '#validate' => array('nospecialchars' => array('value' => true)), '#inline' => true, '#value' => __('Enter field slug', 'wpcf'));
        if (empty($form_data['slug'])) {
            $form_field['slug']['#attributes']['onclick'] = 'if (jQuery(this).val() == \'' . __('Enter field slug', 'wpcf') . '\') { jQuery(this).val(\'\'); }';
            $form_field['slug']['#attributes']['onblur'] = 'if (jQuery(this).val() == \'\') { jQuery(this).val(\'' . __('Enter field slug', 'wpcf') . '\') }';
        }
        // If insert form callback is not provided, use generic form data
        if (function_exists('wpcf_fields_' . $type . '_insert_form')) {
            $form_field_temp = call_user_func('wpcf_fields_' . $type . '_insert_form', $form_data, 'wpcf[fields][' . $id . ']');
            if (is_array($form_field_temp)) {
                unset($form_field_temp['name'], $form_field_temp['slug']);
                $form_field = $form_field + $form_field_temp;
            }
        }
        $form_field['description'] = array('#type' => 'textarea', '#name' => 'description', '#attributes' => array('rows' => 5, 'cols' => 1, 'style' => 'margin:0 0 10px 0;'), '#inline' => true, '#value' => __('Describe this field', 'wpcf'));
        if (empty($form_data['description'])) {
            $form_field['description']['#attributes']['onfocus'] = 'if (jQuery(this).val() == \'' . __('Describe this field', 'wpcf') . '\') { jQuery(this).val(\'\'); }';
            $form_field['description']['#attributes']['onblur'] = 'if (jQuery(this).val() == \'\') { jQuery(this).val(\'' . __('Describe this field', 'wpcf') . '\') }';
        }
        // Process all form fields
        foreach ($form_field as $k => $field) {
            $form['wpcf-' . $id][$k] = $field;
            // Check if nested
            if (isset($field['#name']) && strpos($field['#name'], '[') === false) {
                $form['wpcf-' . $id][$k]['#name'] = 'wpcf[fields][' . $id . '][' . $field['#name'] . ']';
            } else {
                if (isset($field['#name'])) {
                    $form['wpcf-' . $id][$k]['#name'] = 'wpcf[fields][' . $id . ']' . $field['#name'];
                }
            }
            if (!isset($field['#id'])) {
                $form['wpcf-' . $id][$k]['#id'] = $type . '-' . $field['#type'] . '-' . rand();
            }
            if (isset($field['#name']) && isset($form_data[$field['#name']])) {
                $form['wpcf-' . $id][$k]['#value'] = $form_data[$field['#name']];
                $form['wpcf-' . $id][$k]['#default_value'] = $form_data[$field['#name']];
                // Check if it's in 'data'
            } else {
                if (isset($field['#name']) && isset($form_data['data'][$field['#name']])) {
                    $form['wpcf-' . $id][$k]['#value'] = $form_data['data'][$field['#name']];
                    $form['wpcf-' . $id][$k]['#default_value'] = $form_data['data'][$field['#name']];
                }
            }
        }
        // Set type
        $form['wpcf-' . $id]['type'] = array('#type' => 'hidden', '#name' => 'wpcf[fields][' . $id . '][type]', '#value' => $type, '#id' => $id . '-type');
        // Add validation box
        $form_validate = wpcf_admin_fields_form_validation('wpcf[fields][' . $id . '][validate]', call_user_func('wpcf_fields_' . $type), $form_data);
        foreach ($form_validate as $k => $v) {
            $form['wpcf-' . $id][$k] = $v;
        }
        // WPML Translation Preferences
        if (function_exists('wpml_cf_translation_preferences')) {
            $custom_field = !empty($form_data['slug']) ? wpcf_types_get_meta_prefix($form_data) . $form_data['slug'] : false;
            $translatable = array('textfield', 'textarea', 'wysiwyg');
            $action = in_array($type, $translatable) ? 'translate' : 'copy';
            $form['wpcf-' . $id]['wpml-preferences'] = array('#type' => 'fieldset', '#title' => __('Translation preferences', 'wpcf'), '#collapsed' => true);
            $form['wpcf-' . $id]['wpml-preferences']['form'] = array('#type' => 'markup', '#markup' => wpml_cf_translation_preferences($id, $custom_field, 'wpcf', false, $action));
        }
        if (empty($form_data) || isset($form_data['is_new'])) {
            $form['wpcf-' . $id]['is_new'] = array('#type' => 'hidden', '#name' => 'wpcf[fields][' . $id . '][is_new]', '#value' => '1');
        }
        return $form;
    }
    return false;
}
示例#3
0
/**
 * Add User Fields to editor
 *
 * @author Gen gen.i@icanlocalize.com
 * @since Types 1.3
 */
function wpcf_admin_post_add_usermeta_to_editor_js($items, $views_callback = false)
{
    global $wpcf;
    $post = wpcf_admin_get_edited_post();
    if (empty($post)) {
        $post = (object) array('ID' => -1);
    }
    $groups = wpcf_admin_fields_get_groups('wp-types-user-group');
    $user_id = wpcf_usermeta_get_user();
    $add = array();
    if (!empty($groups)) {
        $item_styles = array();
        foreach ($groups as $group_id => $group) {
            if (empty($group['is_active'])) {
                continue;
            }
            $group['name'] .= ' (User meta fields)';
            $fields = wpcf_admin_fields_get_fields_by_group($group['id'], 'slug', true, false, true, 'wp-types-user-group', 'wpcf-usermeta');
            if (!empty($fields)) {
                foreach ($fields as $field_id => $field) {
                    // Use field class
                    $wpcf->usermeta_field->set($user_id, $field);
                    // Get field data
                    $data = (array) $wpcf->usermeta_field->config;
                    // Get inherited field
                    if (isset($data['inherited_field_type'])) {
                        $inherited_field_data = wpcf_fields_type_action($data['inherited_field_type']);
                    }
                    $callback = 'wpcfFieldsEditorCallback(\'' . $field['id'] . '\', \'usermeta\', ' . $post->ID . ')';
                    // Added for Views:users filter Vicon popup
                    if ($views_callback) {
                        $callback = 'wpcfFieldsEditorCallback(\'' . $field['id'] . '\', \'views-usermeta\', ' . $post->ID . ')';
                    }
                    $add[$group['name']][stripslashes($field['name'])] = array(stripslashes($field['name']), trim(wpcf_usermeta_get_shortcode($field), '[]'), $group['name'], $callback);
                }
                /*
                 * Since Types 1.2
                 * We use field class to enqueue JS and CSS
                 */
                $wpcf->usermeta_field->enqueue_script();
                $wpcf->usermeta_field->enqueue_style();
            }
        }
    }
    $items = $items + $add;
    return $items;
}
 /**
  * @param $type
  * @param array $form_data
  *
  * @return array
  */
 protected function get_field_form_data($type, $form_data = array())
 {
     /**
      * this function replace: wpcf_fields_get_field_form_data()
      */
     require_once WPCF_ABSPATH . '/includes/conditional-display.php';
     $form = array();
     /**
      * row fir field data
      */
     $table_row_typeproof = '<tr class="js-wpcf-fields-typeproof"><td><LABEL></td><td><ERROR><BEFORE><ELEMENT><AFTER></td></tr>';
     $table_row = '<tr><td><LABEL></td><td><ERROR><BEFORE><ELEMENT><AFTER></td></tr>';
     // Get field type data
     if (empty($field_data)) {
         $field_data = wpcf_fields_type_action($type);
         if (empty($field_data)) {
             return $form;
         }
     }
     // Set right ID if existing field
     if (isset($form_data['submitted_key'])) {
         $id = $form_data['submitted_key'];
     } else {
         $id = $type . '-' . rand();
     }
     // Sanitize
     $form_data = wpcf_sanitize_field($form_data);
     $required = isset($form_data['data']['validate']['required']['active']) && $form_data['data']['validate']['required']['active'] === "1" ? __('- required', 'wpcf') : '';
     $form_data['id'] = $id;
     /**
      *  Set title
      */
     $title = !empty($form_data['name']) ? $form_data['name'] : __('Untitled', 'wpcf');
     $title = sprintf('<span class="wpcf-legend-update">%s</span> <span class="description">(%s)</span> <span class="wpcf_required_data">%s</span>', $title, $field_data['title'], $required);
     // Get init data
     $field_init_data = wpcf_fields_type_action($type);
     // See if field inherits some other
     $inherited_field_data = false;
     if (isset($field_init_data['inherited_field_type'])) {
         $inherited_field_data = wpcf_fields_type_action($field_init_data['inherited_field_type']);
     }
     $form_field = array();
     /**
      * Font Awesome Icon
      */
     $icon = '';
     $icon = $this->render_field_icon($field_init_data);
     /**
      * box id & class
      */
     $closed_postboxes = $this->get_closed_postboxes();
     $clasess = array('postbox');
     // close all elements except new added fields
     if (!isset($_REQUEST['type'])) {
         $clasess[] = 'closed';
     }
     $box_id = sprintf('types-custom-field-%s', $id);
     /* Only close boxes which user closed manually
           if ( !empty($closed_postboxes) ) {
        if ( in_array($box_id, $closed_postboxes) ) {
     	   $clasess[] = 'closed';
        }
        }
        */
     /**
      * box title
      */
     $form_field['box-open'] = array('#type' => 'markup', '#markup' => sprintf('<div id="%s" class="%s"><div class="handlediv" title="%s"><br></div><h3 class="hndle ui-sortable-handle">%s%s</h3>', esc_attr($box_id), esc_attr(implode(' ', $clasess)), esc_attr__('Click to toggle', 'wpcf'), $icon, $title));
     $form_field['table-open'] = array('#type' => 'markup', '#markup' => '<table class="widefat inside js-wpcf-slugize-container">');
     // Force name and description
     $form_field['name'] = array('#type' => 'textfield', '#name' => 'name', '#attributes' => array('class' => 'widefat wpcf-forms-set-legend wpcf-forms-field-name js-wpcf-slugize-source', 'placeholder' => __('Enter field name', 'wpcf'), 'tooltip' => __('This will be the label for the field in the post editor.', 'wpcf')), '#validate' => array('required' => array('value' => true)), '#inline' => true, '#pattern' => $table_row_typeproof, '#title' => __('Field name', 'wpcf'));
     $form_field['slug'] = array('#type' => 'textfield', '#name' => 'slug', '#attributes' => array('class' => 'widefat wpcf-forms-field-slug js-wpcf-slugize', 'maxlength' => 255, 'placeholder' => __('Enter field slug', 'wpcf'), 'tooltip' => __('This is the machine name of the field.', 'wpcf')), '#validate' => array('nospecialchars' => array('value' => true)), '#inline' => true, '#pattern' => $table_row_typeproof, '#title' => __('Field slug', 'wpcf'));
     // existing field
     if (isset($form_data['submitted_key'])) {
         $form_field['slug-pre-save'] = array('#type' => 'hidden', '#name' => 'slug-pre-save');
     }
     $options = $this->get_available_types($type);
     if (empty($options)) {
         $form_field['type'] = array('#type' => 'markup', '#markup' => wpautop($type));
     } else {
         $form_field['type'] = array('#type' => 'select', '#name' => 'type', '#options' => $options, '#default_value' => $type, '#description' => __('Options for this filed will be available after group save.', 'wpcf'), '#attributes' => array('class' => 'js-wpcf-fields-type', 'data-message-after-change' => esc_attr__('Field options will be available after you save this group.', 'wpcf')));
     }
     $form_field['type']['#title'] = __('Field type', 'wpcf');
     $form_field['type']['#inline'] = true;
     $form_field['type']['#pattern'] = $table_row_typeproof;
     // If insert form callback is not provided, use generic form data
     if (function_exists('wpcf_fields_' . $type . '_insert_form')) {
         $form_field_temp = call_user_func('wpcf_fields_' . $type . '_insert_form', $form_data, 'wpcf[fields][' . $id . ']');
         if (is_array($form_field_temp)) {
             unset($form_field_temp['name'], $form_field_temp['slug']);
             /**
              * add default patter
              */
             foreach ($form_field_temp as $key => $data) {
                 if (isset($data['#pattern'])) {
                     continue;
                 }
                 $form_field_temp[$key]['#pattern'] = $table_row;
             }
             $form_field = $form_field + $form_field_temp;
         }
     }
     $form_field['description'] = array('#type' => 'textarea', '#name' => 'description', '#attributes' => array('rows' => 5, 'cols' => 1, 'placeholder' => __('Enter field description', 'wpcf'), 'class' => 'widefat', 'tooltip' => __('This optional text appears next to the field and helps users understand what this field is for.', 'wpcf')), '#inline' => true, '#pattern' => $table_row, '#title' => __('Description', 'wpcf'));
     /**
      * add placeholder field
      */
     switch ($type) {
         case 'audio':
         case 'colorpicker':
         case 'date':
         case 'email':
         case 'embed':
         case 'file':
         case 'image':
         case 'numeric':
         case 'phone':
         case 'skype':
         case 'textarea':
         case 'textfield':
         case 'url':
         case 'video':
             $form_field['placeholder'] = array('#type' => 'textfield', '#name' => 'placeholder', '#inline' => true, '#title' => __('Placeholder', 'wpcf'), '#attributes' => array('placeholder' => __('Enter placeholder', 'wpcf'), 'class' => 'widefat', 'tooltip' => __('This value is being displayed when the field is empty in the post editor.', 'wpcf')), '#pattern' => preg_replace('/<tr>/', '<tr class="wpcf-border-top">', $table_row));
             break;
     }
     /**
      * add default value
      */
     switch ($type) {
         case 'audio':
         case 'email':
         case 'embed':
         case 'file':
         case 'image':
         case 'numeric':
         case 'phone':
         case 'textfield':
         case 'url':
         case 'video':
             $form_field['user_default_value'] = array('#type' => 'textfield', '#name' => 'user_default_value', '#inline' => true, '#title' => __('Default Value', 'wpcf'), '#attributes' => array('placeholder' => __('Enter default value', 'wpcf'), 'class' => 'widefat', 'tooltip' => __('This is the initial value of the field.', 'wpcf')), '#pattern' => $table_row);
             break;
         case 'textarea':
         case 'wysiwyg':
             $form_field['user_default_value'] = array('#type' => 'textarea', '#name' => 'user_default_value', '#inline' => true, '#title' => __('Default Value', 'wpcf'), '#attributes' => array('style' => 'width:100%;margin:0 0 10px 0;', 'placeholder' => __('Enter default value', 'wpcf')), '#pattern' => $table_row);
             break;
     }
     switch ($type) {
         case 'audio':
         case 'file':
         case 'image':
         case 'embed':
         case 'url':
         case 'video':
             $form_field['user_default_value']['#validate'] = array('url' => array());
             break;
         case 'email':
             $form_field['user_default_value']['#validate'] = array('email' => array());
             break;
         case 'numeric':
             $form_field['user_default_value']['#validate'] = array('number' => array());
             break;
     }
     if (wpcf_admin_can_be_repetitive($type)) {
         $temp_warning_message = '';
         // We need to set the "repetitive" setting to a string '0' or '1', not numbers, because it will be used
         // again later in this method (which I'm not going to refactor now) and because the form renderer
         // is oversensitive.
         $is_repetitive_as_string = 1 == wpcf_getnest($form_data, array('data', 'repetitive'), '0') ? '1' : '0';
         if (!array_key_exists('data', $form_data) || !is_array($form_data['data'])) {
             $form_data['data'] = array();
         }
         $form_data['data']['repetitive'] = $is_repetitive_as_string;
         $form_field['repetitive'] = array('#type' => 'radios', '#name' => 'repetitive', '#title' => __('Single or repeating field?', 'wpcf'), '#options' => array('repeat' => array('#title' => __('Allow multiple-instances of this field', 'wpcf'), '#value' => '1', '#attributes' => array('onclick' => 'jQuery(this).parent().parent().find(\'.wpcf-cd-warning\').hide(); jQuery(this).parent().find(\'.wpcf-cd-repetitive-warning\').show();'), '#before' => '<li>', '#after' => '</li>', '#inline' => true), 'norepeat' => array('#title' => __('This field can have only one value', 'wpcf'), '#value' => '0', '#attributes' => array('onclick' => 'jQuery(this).parent().parent().find(\'.wpcf-cd-warning\').show(); jQuery(this).parent().find(\'.wpcf-cd-repetitive-warning\').hide();'), '#before' => '<li>', '#after' => '</li>', '#inline' => true)), '#default_value' => $is_repetitive_as_string, '#after' => wpcf_admin_is_repetitive($form_data) ? '<div class="wpcf-message wpcf-cd-warning wpcf-error" style="display:none;"><p>' . __("There may be multiple instances of this field already. When you switch back to single-field mode, all values of this field will be updated when it's edited.", 'wpcf') . '</p></div>' . $temp_warning_message : $temp_warning_message, '#pattern' => preg_replace('/<tr>/', '<tr class="wpcf-border-top">', $table_row), '#inline' => true, '#before' => '<ul>', '#after' => '</ul>');
     }
     /**
             /* Add validation box
     */
     $validate_function = sprintf('wpcf_fields_%s', $type);
     if (is_callable($validate_function)) {
         $form_validate = $this->form_validation('wpcf[fields][' . $id . '][validate]', call_user_func($validate_function), $form_data);
         foreach ($form_validate as $k => $v) {
             if ('hidden' != $v['#type'] && !isset($v['#pattern'])) {
                 $v['#pattern'] = $table_row;
             }
             $form_field['wpcf-' . $id . $k] = $v;
         }
     }
     /**
      * WPML Translation Preferences
      *
      * only for post meta
      *
      */
     $form_field += $this->wpml($form_data);
     // Conditional display, Relevanssi integration and other modifications can be added here.
     // Note that form_data may contain only meta_key when the field is newly
     $form_field = apply_filters('wpcf_form_field', $form_field, $form_data, $type);
     /**
      * add Remove button
      */
     $form_field['remove-field'] = array('#type' => 'markup', '#markup' => sprintf('<a href="#" class="js-wpcf-field-remove wpcf-field-remove" data-message-confirm="%s"><i class="fa fa-trash"></i> %s</a>', esc_attr__('Are you sure?', 'wpcf'), __('Remove field', 'wpcf')), '#pattern' => '<tfoot><tr><td colspan="2"><ELEMENT></td></tr></tfoot>');
     /**
      * close table
      */
     $form_field[$id . 'table-close'] = array('#type' => 'markup', '#markup' => '</table>');
     /**
      * close foldable field div
      */
     $form_field['box-close'] = array('#type' => 'markup', '#markup' => '</div>');
     // Process all form fields
     foreach ($form_field as $k => $field) {
         $name = sprintf('wpcf-%s[%s]', $id, $k);
         $form[$name] = $field;
         // Check if nested
         if (isset($field['#name']) && strpos($field['#name'], '[') === false) {
             $form[$name]['#name'] = 'wpcf[fields][' . $id . '][' . $field['#name'] . ']';
         } else {
             if (isset($field['#name']) && strpos($field['#name'], 'wpcf[fields]') === false) {
                 $form[$name]['#name'] = 'wpcf[fields][' . $id . ']' . $field['#name'];
             } else {
                 if (isset($field['#name'])) {
                     $form[$name]['#name'] = $field['#name'];
                 }
             }
         }
         if (!isset($field['#id'])) {
             $form[$name]['#id'] = $type . '-' . $field['#type'] . '-' . rand();
         }
         if (isset($field['#name']) && isset($form_data[$field['#name']])) {
             $form[$name]['#value'] = $form_data[$field['#name']];
             $form[$name]['#default_value'] = $form_data[$field['#name']];
             if (!isset($form[$name]['#pattern'])) {
                 $form[$name]['#pattern'] = $table_row;
             }
             // Check if it's in 'data'
         } else {
             if (isset($field['#name']) && isset($form_data['data'][$field['#name']])) {
                 $form[$name]['#value'] = $form_data['data'][$field['#name']];
                 $form[$name]['#default_value'] = $form_data['data'][$field['#name']];
                 if (!isset($form[$name]['#pattern'])) {
                     $form[$name]['#pattern'] = $table_row;
                 }
             }
         }
         if ($k == 'slug-pre-save') {
             $form[$name]['#value'] = $form_data['slug'];
         }
     }
     /**
      * last setup of form
      */
     if (empty($form_data) || isset($form_data['is_new'])) {
         $form['wpcf-' . $id]['is_new'] = array('#type' => 'hidden', '#name' => 'wpcf[fields][' . $id . '][is_new]', '#value' => '1', '#attributes' => array('class' => 'wpcf-is-new'));
     }
     // Set type
     $form['wpcf-' . $id]['type'] = array('#type' => 'hidden', '#name' => 'wpcf[fields][' . $id . '][type]', '#value' => $type, '#id' => $id . '-type');
     /**
      * just return this form
      */
     return $form;
 }
示例#5
0
/**
 * Gets all fields.
 *
 * @todo Move to WPCF_Fields
 * @param bool $only_active
 * @param bool $disabled_by_type
 * @param bool $strictly_active
 * @param string $option_name
 * @param bool $use_cache
 * @param bool $clear_cache
 * @return array
 *
 * added param $use_cache by Gen (used when adding new fields to group)
 * added param $use_cache by Gen (used when adding new fields to group)
 */
function wpcf_admin_fields_get_fields($only_active = false, $disabled_by_type = false, $strictly_active = false, $option_name = 'wpcf-fields', $use_cache = true, $clear_cache = false)
{
    static $cache = array();
    if ($clear_cache) {
        $cache = array();
    }
    /**
     * Sanitize option name
     */
    switch ($option_name) {
        case 'wpcf-usermeta':
        case 'wpcf-fields':
        case WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION:
            break;
        default:
            $option_name = 'wpcf-fields';
            break;
    }
    $cache_key = md5($only_active . $disabled_by_type . $strictly_active . $option_name . $use_cache);
    if (isset($cache[$cache_key]) && $use_cache == true) {
        return $cache[$cache_key];
    }
    $required_data = array('id', 'name', 'type', 'slug');
    $fields = (array) get_option($option_name, array());
    foreach ($fields as $k => $v) {
        $failed = false;
        foreach ($required_data as $required) {
            if (!isset($v[$required])) {
                $failed = true;
                continue;
            }
            if (is_numeric($v[$required]) === true) {
                $failed = true;
                continue;
            }
        }
        if (is_numeric($k) === true || $failed) {
            unset($fields[$k]);
            continue;
        }
        // This call loads config file
        $data = wpcf_fields_type_action($v['type']);
        if (empty($data)) {
            unset($fields[$k]);
            continue;
        }
        if (isset($data['wp_version']) && wpcf_compare_wp_version($data['wp_version'], '<')) {
            unset($fields[$k]);
            continue;
        }
        if ($strictly_active) {
            if (!empty($v['data']['disabled']) || !empty($v['data']['disabled_by_type'])) {
                unset($fields[$k]);
                continue;
            }
        } else {
            if ($only_active && !empty($v['data']['disabled'])) {
                unset($fields[$k]);
                continue;
            }
            if (!$disabled_by_type && !empty($v['data']['disabled_by_type'])) {
                unset($fields[$k]);
                continue;
            }
        }
        $v['id'] = $k;
        $v['meta_key'] = wpcf_types_get_meta_prefix($v) . $k;
        $option_name_to_meta_type = array('wpcf-fields' => 'postmeta', 'wpcf-usermeta' => 'usermeta', WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION => 'termmeta');
        $v['meta_type'] = $option_name_to_meta_type[$option_name];
        $fields[$k] = wpcf_sanitize_field($v);
    }
    $cache[$cache_key] = apply_filters('types_fields', $fields);
    return $cache[$cache_key];
}
/**
 * Processes field form data.
 *
 * @param type $type
 * @param type $form_data
 * @return type
 */
function wpcf_fields_get_field_form_data($type, $form_data = array())
{
    // Get field type data
    $field_data = wpcf_fields_type_action($type);
    if (!empty($field_data)) {
        $form = array();
        // Set right ID if existing field
        if (isset($form_data['submitted_key'])) {
            $id = $form_data['submitted_key'];
        } else {
            $id = $type . '-' . rand();
        }
        // Sanitize
        $form_data = wpcf_sanitize_field($form_data);
        // Set remove link
        $remove_link = isset($form_data['group_id']) ? admin_url('admin-ajax.php?' . 'wpcf_ajax_callback=wpcfFieldsFormDeleteElement&amp;wpcf_warning=' . __('Are you sure?', 'wpcf') . '&amp;action=wpcf_ajax&amp;wpcf_action=remove_field_from_group' . '&amp;group_id=' . intval($form_data['group_id']) . '&amp;field_id=' . $form_data['id']) . '&amp;_wpnonce=' . wp_create_nonce('remove_field_from_group') : admin_url('admin-ajax.php?' . 'wpcf_ajax_callback=wpcfFieldsFormDeleteElement&amp;wpcf_warning=' . __('Are you sure?', 'wpcf') . '&amp;action=wpcf_ajax&amp;wpcf_action=remove_field_from_group') . '&amp;_wpnonce=' . wp_create_nonce('remove_field_from_group');
        // Set move button
        $form['wpcf-' . $id . '-control'] = array('#type' => 'markup', '#markup' => '<img src="' . WPCF_RES_RELPATH . '/images/move.png" class="wpcf-fields-form-move-field" alt="' . __('Move this field', 'wpcf') . '" /><a href="' . $remove_link . '" ' . 'class="wpcf-form-fields-delete wpcf-ajax-link">' . '<img src="' . WPCF_RES_RELPATH . '/images/delete-2.png" alt="' . __('Delete this field', 'wpcf') . '" /></a>');
        // Set fieldset
        $collapsed = wpcf_admin_fields_form_fieldset_is_collapsed('fieldset-' . $id);
        // Set collapsed on AJAX call (insert)
        $collapsed = defined('DOING_AJAX') ? false : $collapsed;
        // Set title
        $title = !empty($form_data['name']) ? $form_data['name'] : __('Untitled', 'wpcf');
        $title = '<span class="wpcf-legend-update">' . $title . '</span> - ' . sprintf(__('%s field', 'wpcf'), $field_data['title']);
        // Do not display on Usermeta Group edit screen
        if (!isset($_GET['page']) || $_GET['page'] != 'wpcf-edit-usermeta') {
            if (!empty($form_data['data']['conditional_display']['conditions'])) {
                $title .= ' ' . __('(conditional)', 'wpcf');
            }
        }
        $form['wpcf-' . $id] = array('#type' => 'fieldset', '#title' => $title, '#id' => 'fieldset-' . $id, '#collapsible' => true, '#collapsed' => $collapsed);
        // Get init data
        $field_init_data = wpcf_fields_type_action($type);
        // See if field inherits some other
        $inherited_field_data = false;
        if (isset($field_init_data['inherited_field_type'])) {
            $inherited_field_data = wpcf_fields_type_action($field_init_data['inherited_field_type']);
        }
        $form_field = array();
        // Force name and description
        $form_field['name'] = array('#type' => 'textfield', '#name' => 'name', '#attributes' => array('class' => 'wpcf-forms-set-legend wpcf-forms-field-name', 'style' => 'width:100%;margin:10px 0 10px 0;', 'placeholder' => __('Enter field name', 'wpcf')), '#validate' => array('required' => array('value' => true)), '#inline' => true);
        $form_field['slug'] = array('#type' => 'textfield', '#name' => 'slug', '#attributes' => array('class' => 'wpcf-forms-field-slug', 'style' => 'width:100%;margin:0 0 10px 0;', 'maxlength' => 255, 'placeholder' => __('Enter field slug', 'wpcf')), '#validate' => array('nospecialchars' => array('value' => true)), '#inline' => true);
        // If insert form callback is not provided, use generic form data
        if (function_exists('wpcf_fields_' . $type . '_insert_form')) {
            $form_field_temp = call_user_func('wpcf_fields_' . $type . '_insert_form', $form_data, 'wpcf[fields][' . $id . ']');
            if (is_array($form_field_temp)) {
                unset($form_field_temp['name'], $form_field_temp['slug']);
                $form_field = $form_field + $form_field_temp;
            }
        }
        $form_field['description'] = array('#type' => 'textarea', '#name' => 'description', '#attributes' => array('rows' => 5, 'cols' => 1, 'style' => 'margin:0 0 10px 0;', 'placeholder' => __('Describe this field', 'wpcf')), '#inline' => true);
        /**
         * add placeholder field
         */
        switch ($type) {
            case 'audio':
            case 'colorpicker':
            case 'date':
            case 'email':
            case 'embed':
            case 'file':
            case 'image':
            case 'numeric':
            case 'phone':
            case 'skype':
            case 'textarea':
            case 'textfield':
            case 'url':
            case 'video':
                $form_field['placeholder'] = array('#type' => 'textfield', '#name' => 'placeholder', '#inline' => true, '#title' => __('Placeholder', 'wpcf'), '#attributes' => array('style' => 'width:100%;margin:0 0 10px 0;', 'placeholder' => __('Enter placeholder', 'wpcf')));
                break;
        }
        if (wpcf_admin_can_be_repetitive($type)) {
            $temp_warning_message = '';
            $form_field['repetitive'] = array('#type' => 'radios', '#name' => 'repetitive', '#title' => __('Single or repeating field?', 'wpcf'), '#options' => array('repeat' => array('#title' => __('Allow multiple-instances of this field', 'wpcf'), '#value' => '1', '#attributes' => array('onclick' => 'jQuery(this).parent().parent().find(\'.wpcf-cd-warning\').hide(); jQuery(this).parent().find(\'.wpcf-cd-repetitive-warning\').show();')), 'norepeat' => array('#title' => __('This field can have only one value', 'wpcf'), '#value' => '0', '#attributes' => array('onclick' => 'jQuery(this).parent().parent().find(\'.wpcf-cd-warning\').show(); jQuery(this).parent().find(\'.wpcf-cd-repetitive-warning\').hide();'))), '#default_value' => isset($form_data['data']['repetitive']) ? $form_data['data']['repetitive'] : '0', '#after' => wpcf_admin_is_repetitive($form_data) ? '<div class="wpcf-message wpcf-cd-warning wpcf-error" style="display:none;"><p>' . __("There may be multiple instances of this field already. When you switch back to single-field mode, all values of this field will be updated when it's edited.", 'wpcf') . '</p></div>' . $temp_warning_message : $temp_warning_message);
        }
        // Process all form fields
        foreach ($form_field as $k => $field) {
            $form['wpcf-' . $id][$k] = $field;
            // Check if nested
            if (isset($field['#name']) && strpos($field['#name'], '[') === false) {
                $form['wpcf-' . $id][$k]['#name'] = 'wpcf[fields][' . $id . '][' . $field['#name'] . ']';
            } else {
                if (isset($field['#name'])) {
                    $form['wpcf-' . $id][$k]['#name'] = 'wpcf[fields][' . $id . ']' . $field['#name'];
                }
            }
            if (!isset($field['#id'])) {
                $form['wpcf-' . $id][$k]['#id'] = $type . '-' . $field['#type'] . '-' . rand();
            }
            if (isset($field['#name']) && isset($form_data[$field['#name']])) {
                $form['wpcf-' . $id][$k]['#value'] = $form_data[$field['#name']];
                $form['wpcf-' . $id][$k]['#default_value'] = $form_data[$field['#name']];
                // Check if it's in 'data'
            } else {
                if (isset($field['#name']) && isset($form_data['data'][$field['#name']])) {
                    $form['wpcf-' . $id][$k]['#value'] = $form_data['data'][$field['#name']];
                    $form['wpcf-' . $id][$k]['#default_value'] = $form_data['data'][$field['#name']];
                }
            }
        }
        // Set type
        $form['wpcf-' . $id]['type'] = array('#type' => 'hidden', '#name' => 'wpcf[fields][' . $id . '][type]', '#value' => $type, '#id' => $id . '-type');
        // Add validation box
        $form_validate = wpcf_admin_fields_form_validation('wpcf[fields][' . $id . '][validate]', call_user_func('wpcf_fields_' . $type), $form_data);
        foreach ($form_validate as $k => $v) {
            $form['wpcf-' . $id][$k] = $v;
        }
        // WPML Translation Preferences
        if (function_exists('wpml_cf_translation_preferences')) {
            $custom_field = !empty($form_data['slug']) ? wpcf_types_get_meta_prefix($form_data) . $form_data['slug'] : false;
            $suppress_errors = $custom_field == false ? true : false;
            $translatable = array('textfield', 'textarea', 'wysiwyg');
            $action = in_array($type, $translatable) ? 'translate' : 'copy';
            $form['wpcf-' . $id]['wpml-preferences'] = array('#type' => 'fieldset', '#title' => __('Translation preferences', 'wpcf'), '#collapsed' => true);
            $wpml_prefs = wpml_cf_translation_preferences($id, $custom_field, 'wpcf', false, $action, false, $suppress_errors);
            $wpml_prefs = str_replace('<span style="color:#FF0000;">', '<span class="wpcf-form-error">', $wpml_prefs);
            $form['wpcf-' . $id]['wpml-preferences']['form'] = array('#type' => 'markup', '#markup' => $wpml_prefs);
        }
        if (empty($form_data) || isset($form_data['is_new'])) {
            $form['wpcf-' . $id]['is_new'] = array('#type' => 'hidden', '#name' => 'wpcf[fields][' . $id . '][is_new]', '#value' => '1', '#attributes' => array('class' => 'wpcf-is-new'));
        }
        $form_data['id'] = $id;
        $form['wpcf-' . $id] = apply_filters('wpcf_form_field', $form['wpcf-' . $id], $form_data);
        return $form;
    }
    return false;
}
示例#7
0
/**
 * Calls view function for specific field type.
 * 
 * @param type $field
 * @param type $atts
 * @return type 
 */
function types_render_field($field, $params, $content = null, $code = '')
{
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
    // Count fields (if there are duplicates)
    static $count = array();
    // Get field
    $field = wpcf_fields_get_field_by_slug($field);
    if (empty($field)) {
        return '';
    }
    // Count it
    if (!isset($count[$field['slug']])) {
        $count[$field['slug']] = 1;
    } else {
        $count[$field['slug']] += 1;
    }
    // Get post field value
    global $post;
    $value = get_post_meta($post->ID, wpcf_types_get_meta_prefix($field) . $field['slug'], true);
    if ($value == '' && $field['type'] != 'checkbox') {
        return '';
    }
    // Load type
    $type = wpcf_fields_type_action($field['type']);
    // Apply filters to field value
    $value = apply_filters('wpcf_fields_value_display', $value);
    $value = apply_filters('wpcf_fields_slug_' . $field['slug'] . '_value_display', $value);
    $value = apply_filters('wpcf_fields_type_' . $field['type'] . '_value_display', $value);
    // To make sure
    if (is_string($value)) {
        $value = addslashes(stripslashes($value));
    }
    // Set values
    $field['name'] = wpcf_translate('field ' . $field['id'] . ' name', $field['name']);
    $params['field'] = $field;
    $params['post'] = $post;
    $params['field_value'] = $value;
    // Get output
    $params['#content'] = htmlspecialchars($content);
    $params['#code'] = $code;
    $output = wpcf_fields_type_action($field['type'], 'view', $params);
    // Convert to string
    if (!empty($output)) {
        $output = strval($output);
    }
    // @todo Reconsider if ever changing this (works fine now)
    // If no output or 'raw' return default
    if (($params['raw'] == 'true' || empty($output)) && !empty($value)) {
        $field_name = '';
        if ($params['show_name'] == 'true') {
            $field_name = wpcf_frontend_wrap_field_name($field, $field['name'], $params);
        }
        $field_value = wpcf_frontend_wrap_field_value($field, $value, $params);
        $output = wpcf_frontend_wrap_field($field, $field_name . $field_value);
    }
    // Apply filters
    $output = strval(apply_filters('types_view', $output, $value, $field['type'], $field['slug'], $field['name'], $params));
    // Add count
    if (isset($count[$field['slug']]) && intval($count[$field['slug']]) > 1) {
        $add = '-' . intval($count[$field['slug']]);
        $output = str_replace('id="wpcf-field-' . $field['slug'] . '"', 'id="wpcf-field-' . $field['slug'] . $add . '"', $output);
    }
    return htmlspecialchars_decode(stripslashes($output));
}
示例#8
0
/**
 * Load JS and CSS for field type.
 * 
 * Core function. Works and stable. Do not move or change.
 * If required, add hooks only.
 * 
 * @staticvar array $cache
 * @param string $type
 * @return string 
 */
function wpcf_field_enqueue_scripts($type)
{
    global $wpcf;
    static $cache = array();
    $config = wpcf_fields_type_action($type);
    if (!empty($config)) {
        // Check if cached
        if (isset($cache[$config['id']])) {
            return $cache[$config['id']];
        }
        // Use field object
        $wpcf->field->enqueue_script($config);
        $wpcf->field->enqueue_style($config);
        $cache[$config['id']] = $config;
        return $config;
    } else {
        $wpcf->debug->errors['missing_type_config'][] = $type;
        return array();
    }
}
示例#9
0
/**
 * Calls view function for specific field type by single field.
 * 
 * @param type $field
 * @param type $atts
 * @return type 
 */
function types_render_field_single($field, $params, $content = null, $code = '')
{
    global $post;
    // Count fields (if there are duplicates)
    static $count = array();
    // Count it
    if (!isset($count[$field['slug']])) {
        $count[$field['slug']] = 1;
    } else {
        $count[$field['slug']] += 1;
    }
    // Load type
    $type = wpcf_fields_type_action($field['type']);
    // If 'class' or 'style' parameters are set - force HTML output
    if ((!empty($params['class']) || !empty($params['style'])) && $field['type'] != 'date') {
        $params['output'] = 'html';
    }
    // Apply filters to field value
    $params['field_value'] = apply_filters('wpcf_fields_value_display', $params['field_value'], $params);
    $params['field_value'] = apply_filters('wpcf_fields_slug_' . $field['slug'] . '_value_display', $params['field_value'], $params);
    $params['field_value'] = apply_filters('wpcf_fields_type_' . $field['type'] . '_value_display', $params['field_value'], $params);
    // To make sure
    if (is_string($params['field_value'])) {
        $params['field_value'] = addslashes(stripslashes($params['field_value']));
    }
    // Set values
    $field['name'] = wpcf_translate('field ' . $field['id'] . ' name', $field['name']);
    $params['field'] = $field;
    $params['#content'] = htmlspecialchars($content);
    $params['#code'] = $code;
    $output = '';
    if (isset($params['raw']) && $params['raw'] == 'true') {
        // Skype is array
        if ($field['type'] == 'skype' && isset($params['field_value']['skypename'])) {
            $output = $params['field_value']['skypename'];
        } else {
            $output = $params['field_value'];
        }
    } else {
        $output = wpcf_fields_type_action($field['type'], 'view', $params);
        // Convert to string
        if (!empty($output)) {
            $output = strval($output);
        }
        // If no output
        if (empty($output) && !empty($params['field_value'])) {
            $output = wpcf_frontend_wrap_field_value($field, $params['field_value'], $params);
            $output = wpcf_frontend_wrap_field($field, $output, $params);
        } else {
            if ($output != '__wpcf_skip_empty') {
                $output = wpcf_frontend_wrap_field_value($field, $output, $params);
                $output = wpcf_frontend_wrap_field($field, $output, $params);
            } else {
                $output = '';
            }
        }
        // Add count
        if (isset($count[$field['slug']]) && intval($count[$field['slug']]) > 1) {
            $add = '-' . intval($count[$field['slug']]);
            $output = str_replace('id="wpcf-field-' . $field['slug'] . '"', 'id="wpcf-field-' . $field['slug'] . $add . '"', $output);
        }
    }
    // Apply filters
    $output = strval(apply_filters('types_view', $output, $params['field_value'], $field['type'], $field['slug'], $field['name'], $params));
    return htmlspecialchars_decode(stripslashes($output));
}
示例#10
0
/**
 * Adds items to view dropdown.
 * 
 * @param type $items
 * @return type 
 */
function wpcf_admin_post_editor_addon_menus_filter($items)
{
    $groups = wpcf_admin_fields_get_groups();
    $add = array();
    if (!empty($groups)) {
        // $group_id is blank therefore not equal to $group['id']
        // use array for item key and CSS class
        $item_styles = array();
        $all_post_types = implode(' ', get_post_types(array('public' => true)));
        foreach ($groups as $group_id => $group) {
            $fields = wpcf_admin_fields_get_fields_by_group($group['id'], 'slug', true, false, true);
            if (!empty($fields)) {
                // code from Types used here without breaking the flow
                // get post types list for every group or apply all
                $post_types = get_post_meta($group['id'], '_wp_types_group_post_types', true);
                if ($post_types == 'all') {
                    $post_types = $all_post_types;
                }
                $post_types = trim(str_replace(',', ' ', $post_types));
                $item_styles[$group['name']] = $post_types;
                foreach ($fields as $field_id => $field) {
                    // Get field data
                    $data = wpcf_fields_type_action($field['type']);
                    // Get inherited field
                    if (isset($data['inherited_field_type'])) {
                        $inherited_field_data = wpcf_fields_type_action($data['inherited_field_type']);
                    }
                    $callback = '';
                    if (isset($data['editor_callback'])) {
                        $callback = sprintf($data['editor_callback'], $field['id']);
                    } else {
                        // Set callback if function exists
                        $function = 'wpcf_fields_' . $field['type'] . '_editor_callback';
                        $callback = function_exists($function) ? 'wpcfFieldsEditorCallback(\'' . $field['id'] . '\')' : '';
                    }
                    $add[$group['name']][stripslashes($field['name'])] = array(stripslashes($field['name']), trim(wpcf_fields_get_shortcode($field), '[]'), $group['name'], $callback);
                    // Process JS
                    if (!empty($data['meta_box_js'])) {
                        foreach ($data['meta_box_js'] as $handle => $data_script) {
                            if (isset($data_script['inline'])) {
                                add_action('admin_footer', $data_script['inline']);
                                continue;
                            }
                            $deps = !empty($data_script['deps']) ? $data_script['deps'] : array();
                            wp_enqueue_script($handle, $data_script['src'], $deps, WPCF_VERSION);
                        }
                    }
                    // Process CSS
                    if (!empty($data['meta_box_css'])) {
                        foreach ($data['meta_box_css'] as $handle => $data_script) {
                            $deps = !empty($data_script['deps']) ? $data_script['deps'] : array();
                            if (isset($data_script['inline'])) {
                                add_action('admin_header', $data_script['inline']);
                                continue;
                            }
                            wp_enqueue_style($handle, $data_script['src'], $deps, WPCF_VERSION);
                        }
                    }
                }
            }
        }
    }
    $search_key = '';
    // Iterate all items to be displayed in the "V" menu
    foreach ($items as $key => $item) {
        if ($key == __('Basic', 'wpv-views')) {
            $search_key = 'found';
            continue;
        }
        if ($search_key == 'found') {
            $search_key = $key;
        }
        if ($key == __('Field', 'wpv-views') && isset($item[trim(wpcf_types_get_meta_prefix(), '-')])) {
            unset($items[$key][trim(wpcf_types_get_meta_prefix(), '-')]);
        }
    }
    if (empty($search_key) || $search_key == 'found') {
        $search_key = count($items);
    }
    $insert_position = array_search($search_key, array_keys($items));
    $part_one = array_slice($items, 0, $insert_position);
    $part_two = array_slice($items, $insert_position);
    $items = $part_one + $add + $part_two;
    // apply CSS styles to each item based on post types
    foreach ($items as $key => $value) {
        if (isset($item_styles[$key])) {
            $items[$key]['css'] = $item_styles[$key];
        } else {
            $items[$key]['css'] = $all_post_types;
        }
    }
    return $items;
}
/**
 * Filters groups on post edit page.
 * 
 * @param type $groups
 * @param type $post
 * @return type 
 */
function wpcf_cd_post_groups_filter($groups, $post, $context)
{
    if ($context != 'group') {
        return $groups;
    }
    foreach ($groups as $key => &$group) {
        $meta_conditional = !isset($group['conditional_display']) ? get_post_meta($group['id'], '_wpcf_conditional_display', true) : $group['conditional_display'];
        if (!empty($meta_conditional['conditions'])) {
            $group['conditional_display'] = $meta_conditional;
            add_action('admin_head', 'wpcf_cd_add_group_js');
            if (empty($post->ID)) {
                $group['_conditional_display'] = 'failed';
                continue;
            }
            $passed = true;
            if (isset($group['conditional_display']['custom_use'])) {
                if (empty($group['conditional_display']['custom'])) {
                    $group['_conditional_display'] = 'failed';
                    continue;
                }
                $evaluate = trim(stripslashes($group['conditional_display']['custom']));
                // Check dates
                $evaluate = wpv_filter_parse_date($evaluate);
                // Add quotes = > < >= <= === <> !==
                $strings_count = preg_match_all('/[=|==|===|<=|<==|<===|>=|>==|>===|\\!===|\\!==|\\!=|<>]\\s(?!\\$)(\\w*)[\\)|\\$|\\W]/', $evaluate, $matches);
                if (!empty($matches[1])) {
                    foreach ($matches[1] as $temp_match) {
                        $temp_replace = is_numeric($temp_match) ? $temp_match : '\'' . $temp_match . '\'';
                        $evaluate = str_replace(' ' . $temp_match . ')', ' ' . $temp_replace . ')', $evaluate);
                    }
                }
                preg_match_all('/\\$([^\\s]*)/', $group['conditional_display']['custom'], $matches);
                if (empty($matches)) {
                    $group['_conditional_display'] = 'failed';
                    continue;
                }
                $fields = array();
                foreach ($matches[1] as $key => $field_name) {
                    $fields[$field_name] = wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_name)) . $field_name;
                    wpcf_cd_add_group_js('add', $field_name, '', '', $group['id']);
                }
                $fields['evaluate'] = $evaluate;
                $check = wpv_condition($fields);
                $passed = $check;
                if (!is_bool($check)) {
                    $passed = false;
                    $group['_conditional_display'] = 'failed';
                } else {
                    if ($check) {
                        $group['_conditional_display'] = 'passed';
                    } else {
                        $group['_conditional_display'] = 'failed';
                    }
                }
            } else {
                $passed_all = true;
                $passed_one = false;
                foreach ($group['conditional_display']['conditions'] as $condition) {
                    // Load field
                    $field = wpcf_admin_fields_get_field($condition['field']);
                    wpcf_fields_type_action($field['type']);
                    wpcf_cd_add_group_js('add', $condition['field'], $condition['value'], $condition['operation'], $group['id']);
                    $value = get_post_meta($post->ID, wpcf_types_get_meta_prefix($condition['field']) . $condition['field'], true);
                    $value = apply_filters('wpcf_conditional_display_compare_meta_value', $value, $condition['field'], $condition['operation'], $key, $post);
                    $condition['value'] = apply_filters('wpcf_conditional_display_compare_condition_value', $condition['value'], $condition['field'], $condition['operation'], $key, $post);
                    $check = wpcf_cd_admin_compare($condition['operation'], $value, $condition['value']);
                    if (!$check) {
                        $passed_all = false;
                    } else {
                        $passed_one = true;
                    }
                }
                if (!$passed_all && $group['conditional_display']['relation'] == 'AND') {
                    $passed = false;
                }
                if (!$passed_one && $group['conditional_display']['relation'] == 'OR') {
                    $passed = false;
                }
            }
            if (!$passed) {
                $group['_conditional_display'] = 'failed';
            } else {
                $group['_conditional_display'] = 'passed';
            }
        }
    }
    return $groups;
}
示例#12
0
/**
 * Gets all fields.
 *
 * @todo Move to WPCF_Fields
 * @param bool $only_active
 * @param bool $disabled_by_type
 * @param bool $strictly_active
 * @param string $option_name
 * @param bool $use_cache
 * @param bool $clear_cache
 * @return type
 *
 * added param $use_cache by Gen (used when adding new fields to group)
 * added param $use_cache by Gen (used when adding new fields to group)
 */
function wpcf_admin_fields_get_fields($only_active = false, $disabled_by_type = false, $strictly_active = false, $option_name = 'wpcf-fields', $use_cache = true, $clear_cache = false)
{
    static $cache = array();
    if ($clear_cache) {
        $cache = array();
    }
    $cache_key = md5($only_active . $disabled_by_type . $strictly_active . $option_name . $use_cache);
    if (isset($cache[$cache_key]) && $use_cache == true) {
        return $cache[$cache_key];
    }
    $required_data = array('id', 'name', 'type', 'slug');
    $fields = (array) get_option($option_name, array());
    foreach ($fields as $k => $v) {
        $failed = false;
        foreach ($required_data as $required) {
            if (!isset($v[$required])) {
                $failed = true;
                continue;
            }
            if (is_numeric($v[$required]) === true) {
                $failed = true;
                continue;
            }
        }
        if (is_numeric($k) === true || $failed) {
            unset($fields[$k]);
            continue;
        }
        // This call loads config file
        $data = wpcf_fields_type_action($v['type']);
        if (empty($data)) {
            unset($fields[$k]);
            continue;
        }
        if (isset($data['wp_version']) && wpcf_compare_wp_version($data['wp_version'], '<')) {
            unset($fields[$k]);
            continue;
        }
        if ($strictly_active) {
            if (!empty($v['data']['disabled']) || !empty($v['data']['disabled_by_type'])) {
                unset($fields[$k]);
                continue;
            }
        } else {
            if ($only_active && !empty($v['data']['disabled'])) {
                unset($fields[$k]);
                continue;
            }
            if (!$disabled_by_type && !empty($v['data']['disabled_by_type'])) {
                unset($fields[$k]);
                continue;
            }
        }
        $v['id'] = $k;
        $v['meta_key'] = wpcf_types_get_meta_prefix($v) . $k;
        $v['meta_type'] = $option_name == 'wpcf-fields' ? 'postmeta' : 'usermeta';
        $fields[$k] = wpcf_sanitize_field($v);
    }
    $cache[$cache_key] = apply_filters('types_fields', $fields);
    return $cache[$cache_key];
}
示例#13
0
/**
 * Has form.
 * 
 * @param type $post
 * @param type $post_type
 * @param type $data
 * @param type $parent_post_type
 * @return string 
 */
function wpcf_pr_admin_post_meta_box_has_form($post, $post_type, $data, $parent_post_type)
{
    if (empty($post)) {
        return '';
    }
    $output = array();
    // Sorting
    $dir = isset($_GET['sort']) && $_GET['sort'] == 'ASC' ? 'DESC' : 'ASC';
    $dir_default = 'ASC';
    $sort_field = isset($_GET['field']) ? $_GET['field'] : '';
    // Cleanup data
    if (empty($data['fields_setting'])) {
        $data['fields_setting'] = 'all_cf';
    }
    // List items
    if (isset($_GET['sort']) && isset($_GET['field'])) {
        if ($_GET['field'] == '_wp_title') {
            $items = get_posts('post_type=' . $post_type . '&numberposts=-1&post_status=null&meta_key=' . '_wpcf_belongs_' . $parent_post_type . '_id&meta_value=' . $post->ID . '&orderby=title&order=' . $_GET['sort']);
        } else {
            if ($_GET['field'] == '_wpcf_pr_parent') {
                global $wpdb;
                $query = "\n        SELECT p.ID, p.post_title, p.post_content, p.post_type, mr.meta_key, mr.meta_value\n        FROM {$wpdb->posts} p\n        INNER JOIN {$wpdb->postmeta} ml ON (p.ID = ml.post_id )\n        INNER JOIN {$wpdb->postmeta} mr ON (p.ID = mr.post_id )\n        INNER JOIN {$wpdb->posts} pp ON (pp.ID = mr.meta_value )\n        WHERE p.post_type = %s\n        AND p.post_status <> 'auto-draft'\n        AND ml.meta_key = %s\n        AND ml.meta_value = %s\n        AND mr.meta_key = %s\n        GROUP BY p.ID\n        ORDER BY pp.post_title " . esc_attr(strtoupper($_GET['sort'])) . "\n";
                $items = $wpdb->get_results($wpdb->prepare($query, $post_type, '_wpcf_belongs_' . $parent_post_type . '_id', $post->ID, '_wpcf_belongs_' . $_GET['post_type_sort_parent'] . '_id'));
                $exclude = array();
                foreach ($items as $key => $item) {
                    $exclude[] = $item->ID;
                }
                $additional = get_posts('post_type=' . $post_type . '&numberposts=-1&post_status=null&meta_key=' . '_wpcf_belongs_' . $parent_post_type . '_id&meta_value=' . $post->ID . '&exclude=' . implode(',', $exclude));
                $items = array_merge(array_values($items), array_values($additional));
            } else {
                if ($_GET['field'] == '_wp_title' || $_GET['field'] == '_wp_body') {
                    if ($_GET['field'] == '_wp_body') {
                        $query['orderby'] = 'post_content';
                    } else {
                        $query['orderby'] = 'post_title';
                    }
                    $query['numberposts'] = -1;
                    $query['post_type'] = $post_type;
                    $query['order'] = strtoupper($_GET['sort']);
                    $items = query_posts($query);
                } else {
                    global $wpdb;
                    $field = wpcf_admin_fields_get_field($_GET['field']);
                    if (isset($field['type'])) {
                        $field_db_type = types_get_field_type($field['type']);
                    } else {
                        $field_db_type = 'CHAR';
                    }
                    $query = "\n        SELECT p.ID, p.post_title, p.post_content, p.post_type, CAST(mr.meta_key AS " . $field_db_type . "), mr.meta_value\n        FROM {$wpdb->posts} p\n        INNER JOIN {$wpdb->postmeta} ml ON (p.ID = ml.post_id )\n        INNER JOIN {$wpdb->postmeta} mr ON (p.ID = mr.post_id )\n        WHERE p.post_type = %s\n        AND p.post_status <> 'auto-draft'\n        AND ml.meta_key = %s\n        AND ml.meta_value = %s\n        AND mr.meta_key = %s\n        GROUP BY p.ID\n        ORDER BY mr.meta_value " . esc_attr(strtoupper($_GET['sort'])) . "\n";
                    $items = $wpdb->get_results($wpdb->prepare($query, $post_type, '_wpcf_belongs_' . $parent_post_type . '_id', $post->ID, $_GET['field']));
                    $exclude = array();
                    foreach ($items as $key => $item) {
                        $exclude[] = $item->ID;
                    }
                    $additional = get_posts('post_type=' . $post_type . '&numberposts=-1&post_status=null&meta_key=' . '_wpcf_belongs_' . $parent_post_type . '_id&meta_value=' . $post->ID . '&exclude=' . implode(',', $exclude));
                    $items = array_merge(array_values($items), array_values($additional));
                }
            }
        }
    } else {
        $items = get_posts('post_type=' . $post_type . '&numberposts=-1&post_status=null&meta_key=' . '_wpcf_belongs_' . $parent_post_type . '_id&meta_value=' . $post->ID);
    }
    // Pagination
    $total_items = count($items);
    $per_page = 5;
    $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
    $numberposts = $page == 1 ? 1 : ($page - 1) * $per_page;
    $slice = $page == 1 ? 0 : ($page - 1) * $per_page;
    $next = count($items) >= $numberposts + $per_page;
    $prev = $page == 1 ? false : true;
    if ($total_items > $per_page) {
        $items = array_splice($items, $slice, $per_page);
    }
    $headers = array();
    $wpcf_fields = wpcf_admin_fields_get_fields(true);
    if ($data['fields_setting'] == 'specific') {
        $title_dir = $sort_field == '_wp_title' ? $dir : 'ASC';
        $_wp_title = '';
        $_wp_title .= $sort_field == '_wp_title' ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
        $_wp_title .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wp_title&amp;sort=' . $title_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . __('Post Title') . '</a>';
        $keys = array_keys($data['fields']);
        foreach ($keys as $k => $header) {
            if ($header == '_wp_title' || $header == '_wpcf_pr_parents') {
                continue;
            }
            if ($header == '_wp_body') {
                $body_dir = $sort_field == '_wp_body' ? $dir : $dir_default;
                $headers[$k] = '';
                $headers[$k] .= $sort_field == '_wp_body' ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
                $headers[$k] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wp_body&amp;sort=' . $body_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . __('Post Body') . '</a>';
            } else {
                if (strpos($header, WPCF_META_PREFIX) === 0 && isset($wpcf_fields[str_replace(WPCF_META_PREFIX, '', $header)])) {
                    wpcf_admin_post_field_load_js_css(wpcf_fields_type_action($wpcf_fields[str_replace(WPCF_META_PREFIX, '', $header)]['type']));
                    $field_dir = $sort_field == $header ? $dir : $dir_default;
                    $headers[$k] = '';
                    $headers[$k] .= $sort_field == $header ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
                    $headers[$k] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . $header . '&amp;sort=' . $field_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . $wpcf_fields[str_replace(WPCF_META_PREFIX, '', $header)]['name'] . '</a>';
                } else {
                    $field_dir = $sort_field == $header ? $dir : $dir_default;
                    $headers[$k] = '';
                    $headers[$k] .= $sort_field == $header ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
                    $headers[$k] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . $header . '&amp;sort=' . $field_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . $header . '</a>';
                }
            }
        }
        if (!empty($data['fields']['_wpcf_pr_parents'])) {
            foreach ($data['fields']['_wpcf_pr_parents'] as $temp_parent => $temp_data) {
                if ($temp_parent == $parent_post_type) {
                    continue;
                }
                $temp_parent_type = get_post_type_object($temp_parent);
                if (empty($temp_parent_type)) {
                    continue;
                }
                $parent_dir = $sort_field == '_wpcf_pr_parent' ? $dir : $dir_default;
                $headers['_wpcf_pr_parent_' . $temp_parent] = '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wpcf_pr_parent&amp;sort=' . $parent_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;post_type_sort_parent=' . $temp_parent . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . $temp_parent_type->label . '</a>';
            }
        }
        array_unshift($headers, $_wp_title);
    } else {
        $item = new stdClass();
        $item->ID = 'new_' . mt_rand();
        $item->post_title = '';
        $item->post_content = '';
        $item->post_type = $post_type;
        $groups = wpcf_admin_post_get_post_groups_fields($item, 'post_relationships');
        $title_dir = $sort_field == '_wp_title' ? $dir : $dir_default;
        $headers['_wp_title'] = '';
        $headers['_wp_title'] .= $sort_field == '_wp_title' ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
        $headers['_wp_title'] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wp_title&amp;sort=' . $title_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . __('Post Title') . '</a>';
        if ($data['fields_setting'] == 'all_cf_standard') {
            $body_dir = $sort_field == '_wp_body' ? $dir : $dir_default;
            $headers['_wp_body'] = '';
            $headers['_wp_body'] .= $sort_field == '_wp_body' ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
            $headers['_wp_body'] = '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wp_body&amp;sort=' . $body_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . __('Post Body') . '</a>';
        }
        foreach ($groups as $group) {
            foreach ($group['fields'] as $field) {
                wpcf_admin_post_field_load_js_css(wpcf_fields_type_action($field['type']));
                $field_dir = $sort_field == wpcf_types_get_meta_prefix($field) . $field['slug'] ? $dir : $dir_default;
                $headers[$field['id']] = '';
                $headers[$field['id']] .= $sort_field == wpcf_types_get_meta_prefix($field) . $field['slug'] ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
                $headers[$field['id']] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . wpcf_types_get_meta_prefix($field) . $field['slug'] . '&amp;sort=' . $field_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . $field['name'] . '</a>';
            }
        }
        // Get all parents
        $item_parents = wpcf_pr_admin_get_belongs($post_type);
        if ($item_parents) {
            foreach ($item_parents as $temp_parent => $temp_data) {
                if ($temp_parent == $parent_post_type) {
                    continue;
                }
                $temp_parent_type = get_post_type_object($temp_parent);
                $parent_dir = $sort_field == '_wpcf_pr_parent' ? $dir : $dir_default;
                $headers['_wpcf_pr_parent_' . $temp_parent] = '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wpcf_pr_parent&amp;sort=' . $parent_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;post_type_sort_parent=' . $temp_parent . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . $temp_parent_type->label . '</a>';
            }
        }
    }
    // If headers are empty, that means there is nothing to render
    if (empty($headers)) {
        return '';
    }
    $header = '<thead><tr><th class="wpcf-sortable">' . implode('&nbsp;&nbsp;&nbsp;</th><th class="wpcf-sortable">', $headers) . '&nbsp;&nbsp;&nbsp;</th><th>' . __('Action', 'wpcf') . '</th></tr></thead>';
    foreach ($items as $key => $item) {
        $output[] = wpcf_pr_admin_post_meta_box_has_row($post, $post_type, $data, $parent_post_type, $item);
    }
    $return = '';
    $return .= wpcf_pr_admin_has_pagination($post, $post_type, $page, $prev, $next, $per_page, $total_items);
    $return .= '<table id="wpcf_pr_table_sortable_' . md5($post_type) . '" class="tablesorter wpcf_pr_table_sortable" cellpadding="0" cellspacing="0" style="width:100%;">' . $header . '<tbody>' . implode($output) . '</tbody></table>';
    $return .= wpcf_form_render_js_validation('#post', false);
    return $return;
}
示例#14
0
 /**
  * Header HTML formatted output.
  * 
  * Each header <th> is array element. Sortable.
  * 
  * @return array 'header_id' => html
  */
 function headers()
 {
     // Sorting
     $dir = isset($_GET['sort']) && $_GET['sort'] == 'ASC' ? 'DESC' : 'ASC';
     $dir_default = 'ASC';
     $sort_field = isset($_GET['field']) ? $_GET['field'] : '';
     // Set values
     $post = $this->parent;
     $post_type = $this->child_post_type;
     $parent_post_type = $this->parent_post_type;
     $data = $this->data;
     $wpcf_fields = wpcf_admin_fields_get_fields(true);
     $headers = array();
     foreach ($this->headers as $k => $header) {
         if ($k === '__parents') {
             continue;
         }
         if ($header == '_wp_title') {
             $title_dir = $sort_field == '_wp_title' ? $dir : 'ASC';
             $headers[$header] = '';
             $headers[$header] .= $sort_field == '_wp_title' ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
             $headers[$header] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wp_title&amp;sort=' . $title_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . __('Post Title') . '</a>';
         } else {
             if ($header == '_wp_body') {
                 $body_dir = $sort_field == '_wp_body' ? $dir : $dir_default;
                 $headers[$header] = '';
                 $headers[$header] .= $sort_field == '_wp_body' ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
                 $headers[$header] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wp_body&amp;sort=' . $body_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . __('Post Body') . '</a>';
             } else {
                 if (strpos($header, WPCF_META_PREFIX) === 0 && isset($wpcf_fields[str_replace(WPCF_META_PREFIX, '', $header)])) {
                     wpcf_admin_post_field_load_js_css($post, wpcf_fields_type_action($wpcf_fields[str_replace(WPCF_META_PREFIX, '', $header)]['type']));
                     $field_dir = $sort_field == $header ? $dir : $dir_default;
                     $headers[$header] = '';
                     $headers[$header] .= $sort_field == $header ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
                     $headers[$header] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . $header . '&amp;sort=' . $field_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . stripslashes($wpcf_fields[str_replace(WPCF_META_PREFIX, '', $header)]['name']) . '</a>';
                     if (wpcf_admin_is_repetitive($wpcf_fields[str_replace(WPCF_META_PREFIX, '', $header)])) {
                         $repetitive_warning = true;
                     }
                 } else {
                     $field_dir = $sort_field == $header ? $dir : $dir_default;
                     $headers[$header] = '';
                     $headers[$header] .= $sort_field == $header ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
                     $headers[$header] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . $header . '&amp;sort=' . $field_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . stripslashes($header) . '</a>';
                 }
             }
         }
     }
     if (!empty($this->headers['__parents'])) {
         foreach ($this->headers['__parents'] as $_parent => $data) {
             if ($_parent == $parent_post_type) {
                 continue;
             }
             $temp_parent_type = get_post_type_object($_parent);
             if (empty($temp_parent_type)) {
                 continue;
             }
             $parent_dir = $sort_field == '_wpcf_pr_parent' ? $dir : $dir_default;
             $headers['_wpcf_pr_parent_' . $_parent] = $sort_field == '_wpcf_pr_parent' ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
             $headers['_wpcf_pr_parent_' . $_parent] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wpcf_pr_parent&amp;sort=' . $parent_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;post_type_sort_parent=' . $_parent . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . $temp_parent_type->label . '</a>';
         }
     }
     return $headers;
 }
/**
 * Add User Fields to editor
 *
 * @author Gen gen.i@icanlocalize.com
 * @since Types 1.3
 */
function wpcf_admin_post_add_usermeta_to_editor_js($menu, $views_callback = false)
{
    global $wpcf;
    $post = apply_filters('wpcf_filter_wpcf_admin_get_current_edited_post', null);
    if (!$post) {
        $post = (object) array('ID' => -1);
    }
    $groups = wpcf_admin_fields_get_groups(TYPES_USER_META_FIELD_GROUP_CPT_NAME);
    $user_id = wpcf_usermeta_get_user();
    if (!empty($groups)) {
        $item_styles = array();
        foreach ($groups as $group_id => $group) {
            if (empty($group['is_active'])) {
                continue;
            }
            $group_name = sprintf(__('%s (Usermeta fields)', 'wpcf'), $group['name']);
            $fields = wpcf_admin_fields_get_fields_by_group($group['id'], 'slug', true, false, true, TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'wpcf-usermeta');
            if (!empty($fields)) {
                foreach ($fields as $field_id => $field) {
                    // Use field class
                    $wpcf->usermeta_field->set($user_id, $field);
                    // Get field data
                    $data = (array) $wpcf->usermeta_field->config;
                    // Get inherited field
                    if (isset($data['inherited_field_type'])) {
                        $inherited_field_data = wpcf_fields_type_action($data['inherited_field_type']);
                    }
                    $callback = 'wpcfFieldsEditorCallback(\'' . $field['id'] . '\', \'usermeta\', ' . $post->ID . ')';
                    // Added for Views:users filter Vicon popup
                    if ($views_callback) {
                        $callback = 'wpcfFieldsEditorCallback(\'' . $field['id'] . '\', \'views-usermeta\', ' . $post->ID . ')';
                    }
                    $menu[$group_name][stripslashes($field['name'])] = array(stripslashes(wp_kses_post($field['name'])), trim(wpcf_usermeta_get_shortcode($field), '[]'), $group_name, $callback);
                }
                /*
                 * Since Types 1.2
                 * We use field class to enqueue JS and CSS
                 */
                $wpcf->usermeta_field->enqueue_script();
                $wpcf->usermeta_field->enqueue_style();
            }
        }
    }
    return $menu;
}
/**
 * Adds items to view dropdown.
 * 
 * @param type $items
 * @return type 
 */
function wpcf_admin_post_editor_addon_menus_filter($items)
{
    $groups = wpcf_admin_fields_get_groups();
    $add = array();
    if (!empty($groups)) {
        foreach ($groups as $group_id => $group) {
            $fields = wpcf_admin_fields_get_fields_by_group($group['id'], 'slug', true, false, true);
            if (!empty($fields)) {
                foreach ($fields as $field_id => $field) {
                    // Get field data
                    $data = wpcf_fields_type_action($field['type']);
                    // Get inherited field
                    if (isset($data['inherited_field_type'])) {
                        $inherited_field_data = wpcf_fields_type_action($data['inherited_field_type']);
                    }
                    $callback = '';
                    if (isset($data['editor_callback'])) {
                        $callback = sprintf($data['editor_callback'], $field['id']);
                    } else {
                        // Set callback if function exists
                        $function = 'wpcf_fields_' . $field['type'] . '_editor_callback';
                        $callback = function_exists($function) ? 'wpcfFieldsEditorCallback(\'' . $field['id'] . '\')' : '';
                    }
                    $add[$group['name']][stripslashes($field['name'])] = array(stripslashes($field['name']), trim(wpcf_fields_get_shortcode($field), '[]'), $group['name'], $callback);
                    // Process JS
                    if (!empty($data['meta_box_js'])) {
                        foreach ($data['meta_box_js'] as $handle => $data_script) {
                            if (isset($data_script['inline'])) {
                                add_action('admin_footer', $data_script['inline']);
                                continue;
                            }
                            $deps = !empty($data_script['deps']) ? $data_script['deps'] : array();
                            wp_enqueue_script($handle, $data_script['src'], $deps, WPCF_VERSION);
                        }
                    }
                    // Process CSS
                    if (!empty($data['meta_box_css'])) {
                        foreach ($data['meta_box_css'] as $handle => $data_script) {
                            $deps = !empty($data_script['deps']) ? $data_script['deps'] : array();
                            if (isset($data_script['inline'])) {
                                add_action('admin_header', $data_script['inline']);
                                continue;
                            }
                            wp_enqueue_style($handle, $data_script['src'], $deps, WPCF_VERSION);
                        }
                    }
                }
            }
        }
    }
    $search_key = '';
    foreach ($items as $key => $item) {
        if ($key == __('Basic', 'wpv-views')) {
            $search_key = 'found';
            continue;
        }
        if ($search_key == 'found') {
            $search_key = $key;
        }
        if ($key == __('Field', 'wpv-views') && isset($item[trim(wpcf_types_get_meta_prefix(), '-')])) {
            unset($items[$key][trim(wpcf_types_get_meta_prefix(), '-')]);
        }
    }
    if (empty($search_key) || $search_key == 'found') {
        $search_key = count($items);
    }
    $insert_position = array_search($search_key, array_keys($items));
    $part_one = array_slice($items, 0, $insert_position);
    $part_two = array_slice($items, $insert_position);
    $items = $part_one + $add + $part_two;
    return $items;
}
示例#17
0
/**
 * Adds items to view dropdown.
 *
 * Core function. Works and stable. Do not move or change.
 * If required, add hooks only.
 *
 * @todo Remove (to WPCF_WPViews)
 *
 * @param type $menu
 * @return type
 */
function wpcf_admin_post_editor_addon_menus_filter($menu)
{
    global $wpcf;
    $post = wpcf_admin_get_edited_post();
    if (empty($post)) {
        $post = (object) array('ID' => -1);
    }
    $groups = wpcf_admin_fields_get_groups('wp-types-group', 'group_active');
    $all_post_types = implode(' ', get_post_types(array('public' => true)));
    $add = array();
    if (!empty($groups)) {
        // $group_id is blank therefore not equal to $group['id']
        // use array for item key and CSS class
        $item_styles = array();
        foreach ($groups as $group_id => $group) {
            $fields = wpcf_admin_fields_get_fields_by_group($group['id'], 'slug', true, false, true);
            if (!empty($fields)) {
                // code from Types used here without breaking the flow
                // get post types list for every group or apply all
                $post_types = get_post_meta($group['id'], '_wp_types_group_post_types', true);
                if ($post_types == 'all') {
                    $post_types = $all_post_types;
                }
                $post_types = trim(str_replace(',', ' ', $post_types));
                $item_styles[$group['name']] = $post_types;
                foreach ($fields as $field_id => $field) {
                    // Use field class
                    $wpcf->field->set($wpcf->post, $field);
                    // Get field data
                    $data = (array) $wpcf->field->config;
                    // Get inherited field
                    if (isset($data['inherited_field_type'])) {
                        $inherited_field_data = wpcf_fields_type_action($data['inherited_field_type']);
                    }
                    $callback = 'wpcfFieldsEditorCallback(\'' . $field['id'] . '\', \'postmeta\', ' . $post->ID . ')';
                    $menu[$group['name']][stripslashes($field['name'])] = array(stripslashes($field['name']), trim(wpcf_fields_get_shortcode($field), '[]'), $group['name'], $callback);
                    /*
                     * Since Types 1.2
                     * We use field class to enqueue JS and CSS
                     */
                    $wpcf->field->enqueue_script();
                    $wpcf->field->enqueue_style();
                }
            }
        }
    }
    /*
        $search_key = '';
    
        // Iterate all items to be displayed in the "V" menu
        foreach ( $items as $key => $item ) {
            if ( $key == __( 'Basic', 'wpv-views' ) ) {
                $search_key = 'found';
                continue;
            }
            if ( $search_key == 'found' ) {
                $search_key = $key;
            }
    
            if ( $key == __( 'Field', 'wpv-views' ) && isset( $item[trim( wpcf_types_get_meta_prefix(),
                                    '-' )] ) ) {
                unset( $items[$key][trim( wpcf_types_get_meta_prefix(), '-' )] );
            }
        }
        if ( empty( $search_key ) || $search_key == 'found' ) {
            $search_key = count( $items );
        }
    
        $insert_position = array_search( $search_key, array_keys( $items ) );
        $part_one = array_slice( $items, 0, $insert_position );
        $part_two = array_slice( $items, $insert_position );
        $items = $part_one + $add + $part_two;
    
        // apply CSS styles to each item based on post types
        foreach ( $items as $key => $value ) {
            if ( isset( $item_styles[$key] ) ) {
                $items[$key]['css'] = $item_styles[$key];
            } else {
                $items[$key]['css'] = $all_post_types;
            }
        }
    */
    return $menu;
}
/**
 * Adds items to view dropdown.
 *
 * Core function. Works and stable. Do not move or change.
 * If required, add hooks only.
 *
 * @todo Remove (to WPCF_WPViews)
 *
 * @param type $menu
 * @return type
 */
function wpcf_admin_post_editor_addon_menus_filter($menu)
{
    global $wpcf;
    $post = wpcf_admin_get_edited_post();
    if (empty($post)) {
        $post = (object) array('ID' => -1);
    }
    $groups = wpcf_admin_fields_get_groups(TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, 'group_active');
    $all_post_types = implode(' ', get_post_types(array('public' => true)));
    $add = array();
    if (!empty($groups)) {
        // $group_id is blank therefore not equal to $group['id']
        // use array for item key and CSS class
        $item_styles = array();
        foreach ($groups as $group_id => $group) {
            $fields = wpcf_admin_fields_get_fields_by_group($group['id'], 'slug', true, false, true);
            if (!empty($fields)) {
                // code from Types used here without breaking the flow
                // get post types list for every group or apply all
                $post_types = get_post_meta($group['id'], '_wp_types_group_post_types', true);
                if ($post_types == 'all') {
                    $post_types = $all_post_types;
                }
                $post_types = trim(str_replace(',', ' ', $post_types));
                $item_styles[$group['name']] = $post_types;
                foreach ($fields as $field_id => $field) {
                    // Use field class
                    $wpcf->field->set($wpcf->post, $field);
                    // Get field data
                    $data = (array) $wpcf->field->config;
                    // Get inherited field
                    if (isset($data['inherited_field_type'])) {
                        $inherited_field_data = wpcf_fields_type_action($data['inherited_field_type']);
                    }
                    $callback = 'wpcfFieldsEditorCallback(\'' . $field['id'] . '\', \'postmeta\', ' . $post->ID . ')';
                    $menu[$group['name']][stripslashes($field['name'])] = array(stripslashes($field['name']), trim(wpcf_fields_get_shortcode($field), '[]'), $group['name'], $callback);
                    /*
                     * Since Types 1.2
                     * We use field class to enqueue JS and CSS
                     */
                    $wpcf->field->enqueue_script();
                    $wpcf->field->enqueue_style();
                }
            }
        }
    }
    return $menu;
}
示例#19
0
/**
 * Gets field by ID.
 * 
 * @global type $wpdb
 * @param type $field_id
 * @param type $only_active
 * @return type 
 */
function wpcf_admin_fields_get_field($field_id, $only_active = false, $disabled_by_type = false, $strictly_active = false)
{
    $fields = wpcf_admin_fields_get_fields($only_active, $disabled_by_type, $strictly_active);
    if (!empty($fields[$field_id])) {
        $data = wpcf_fields_type_action($fields[$field_id]['type']);
        if (isset($data['wp_version']) && wpcf_compare_wp_version($data['wp_version'], '<')) {
            return array();
        }
        $fields[$field_id]['id'] = $field_id;
        return $fields[$field_id];
    }
    return array();
}
/**
 * Post relationship has form headers.
 * 
 * @global type $wpcf_post_relationship_headers
 * @param type $post
 * @param type $post_type
 * @param type $parent_post_type
 * @param type $data
 * @return string 
 */
function wpcf_pr_admin_post_meta_box_has_form_headers($post, $post_type, $parent_post_type, $data)
{
    // Sorting
    $dir = isset($_GET['sort']) && $_GET['sort'] == 'ASC' ? 'DESC' : 'ASC';
    $dir_default = 'ASC';
    $sort_field = isset($_GET['field']) ? $_GET['field'] : '';
    $headers = array();
    $wpcf_fields = wpcf_admin_fields_get_fields(true);
    if (empty($data['fields_setting'])) {
        $data['fields_setting'] = 'all_cf';
    }
    if ($data['fields_setting'] == 'specific') {
        $keys = array_keys($data['fields']);
        foreach ($keys as $k => $header) {
            if ($header == '_wpcf_pr_parents') {
                continue;
            }
            if ($header == '_wp_title') {
                $title_dir = $sort_field == '_wp_title' ? $dir : 'ASC';
                $headers[$header] = '';
                $headers[$header] .= $sort_field == '_wp_title' ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
                $headers[$header] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wp_title&amp;sort=' . $title_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . __('Post Title') . '</a>';
            } else {
                if ($header == '_wp_body') {
                    $body_dir = $sort_field == '_wp_body' ? $dir : $dir_default;
                    $headers[$header] = '';
                    $headers[$header] .= $sort_field == '_wp_body' ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
                    $headers[$header] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wp_body&amp;sort=' . $body_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . __('Post Body') . '</a>';
                } else {
                    if (strpos($header, WPCF_META_PREFIX) === 0 && isset($wpcf_fields[str_replace(WPCF_META_PREFIX, '', $header)])) {
                        wpcf_admin_post_field_load_js_css(wpcf_fields_type_action($wpcf_fields[str_replace(WPCF_META_PREFIX, '', $header)]['type']));
                        $field_dir = $sort_field == $header ? $dir : $dir_default;
                        $headers[$header] = '';
                        $headers[$header] .= $sort_field == $header ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
                        $headers[$header] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . $header . '&amp;sort=' . $field_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . stripslashes($wpcf_fields[str_replace(WPCF_META_PREFIX, '', $header)]['name']) . '</a>';
                        if (wpcf_admin_is_repetitive($wpcf_fields[str_replace(WPCF_META_PREFIX, '', $header)])) {
                            $repetitive_warning = true;
                        }
                    } else {
                        $field_dir = $sort_field == $header ? $dir : $dir_default;
                        $headers[$header] = '';
                        $headers[$header] .= $sort_field == $header ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
                        $headers[$header] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . $header . '&amp;sort=' . $field_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . stripslashes($header) . '</a>';
                    }
                }
            }
        }
        if (!empty($data['fields']['_wpcf_pr_parents'])) {
            foreach ($data['fields']['_wpcf_pr_parents'] as $temp_parent => $temp_data) {
                if ($temp_parent == $parent_post_type) {
                    continue;
                }
                $temp_parent_type = get_post_type_object($temp_parent);
                if (empty($temp_parent_type)) {
                    continue;
                }
                $parent_dir = $sort_field == '_wpcf_pr_parent' ? $dir : $dir_default;
                $headers['_wpcf_pr_parent_' . $temp_parent] = $sort_field == '_wpcf_pr_parent' ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
                $headers['_wpcf_pr_parent_' . $temp_parent] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wpcf_pr_parent&amp;sort=' . $parent_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;post_type_sort_parent=' . $temp_parent . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . $temp_parent_type->label . '</a>';
            }
        }
    } else {
        $item = new stdClass();
        $item->ID = 'new_' . wpcf_unique_id(serialize($post));
        $item->post_title = '';
        $item->post_content = '';
        $item->post_type = $post_type;
        $groups = wpcf_admin_post_get_post_groups_fields($item, 'post_relationships_header');
        $title_dir = $sort_field == '_wp_title' ? $dir : $dir_default;
        $headers['_wp_title'] = '';
        $headers['_wp_title'] .= $sort_field == '_wp_title' ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
        $headers['_wp_title'] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wp_title&amp;sort=' . $title_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . __('Post Title') . '</a>';
        if ($data['fields_setting'] == 'all_cf_standard') {
            $body_dir = $sort_field == '_wp_body' ? $dir : $dir_default;
            $headers['_wp_body'] = '';
            $headers['_wp_body'] .= $sort_field == '_wp_body' ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
            $headers['_wp_body'] = '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wp_body&amp;sort=' . $body_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . __('Post Body') . '</a>';
        }
        foreach ($groups as $group) {
            foreach ($group['fields'] as $field) {
                if (wpcf_admin_is_repetitive($field)) {
                    $repetitive_warning = true;
                }
                $header_key = wpcf_types_get_meta_prefix($field) . $field['slug'];
                wpcf_admin_post_field_load_js_css(wpcf_fields_type_action($field['type']));
                $field_dir = $sort_field == wpcf_types_get_meta_prefix($field) . $field['slug'] ? $dir : $dir_default;
                $headers[$header_key] = '';
                $headers[$header_key] .= $sort_field == wpcf_types_get_meta_prefix($field) . $field['slug'] ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : '';
                $headers[$header_key] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . wpcf_types_get_meta_prefix($field) . $field['slug'] . '&amp;sort=' . $field_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . stripslashes($field['name']) . '</a>';
            }
        }
        // Get all parents
        $item_parents = wpcf_pr_admin_get_belongs($post_type);
        if ($item_parents) {
            foreach ($item_parents as $temp_parent => $temp_data) {
                if ($temp_parent == $parent_post_type) {
                    continue;
                }
                $temp_parent_type = get_post_type_object($temp_parent);
                $parent_dir = $sort_field == '_wpcf_pr_parent' ? $dir : $dir_default;
                $headers['_wpcf_pr_parent_' . $temp_parent] = '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=pr_sort&amp;field=' . '_wpcf_pr_parent&amp;sort=' . $parent_dir . '&amp;post_id=' . $post->ID . '&amp;post_type=' . $post_type . '&amp;post_type_sort_parent=' . $temp_parent . '&amp;_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . $temp_parent_type->label . '</a>';
            }
        }
    }
    return $headers;
}