function wpcf_admin_metabox_custom_fields($ct) { $form = array(); $options = array(); $option_to_key = array(); $fields = wpcf_admin_fields_get_active_fields_by_post_type($ct['slug']); foreach ($fields as $field => $data) { if (isset($data['data']['repetitive']) && $data['data']['repetitive']) { continue; } switch ($data['type']) { case 'embed': case 'checkboxes': case 'audio': case 'file': case 'textarea': case 'video': case 'wysiwyg': continue; default: $option_to_key[$data['meta_key']] = $field; $options[$field] = array('#name' => sprintf('ct[custom_fields][%s]', esc_attr($data['meta_key'])), '#title' => sprintf('%s <small>(%s)</small>', $data['name'], $data['type']), '#value' => 1, '#inline' => true, '#before' => '<li class="menu-item-handle">', '#after' => '</li>', '#default_value' => intval(isset($ct['custom_fields']) && isset($ct['custom_fields'][$data['meta_key']]))); } } /** * get custom fields order */ $options_to_add = array(); if (isset($ct['custom_fields']) && is_array($ct['custom_fields']) && !empty($ct['custom_fields'])) { foreach ($ct['custom_fields'] as $key) { if (isset($option_to_key[$key]) && isset($options[$option_to_key[$key]])) { $options_to_add[$option_to_key[$key]] = $options[$option_to_key[$key]]; unset($options[$option_to_key[$key]]); } } } $options_to_add += $options; $form['table-custom_fields-open'] = wpcf_admin_metabox_begin(__('Custom Fields', 'wpcf'), 'custom_fields', 'wpcf-types-form-visiblity-custom-fields-table', false, true, '_builtin'); $form['table-custom_fields-description'] = array('#type' => 'checkboxes', '#options' => $options_to_add + $options, '#name' => 'wpcf[group][supports]', '#inline' => true, '_builtin' => true); if (empty($options_to_add)) { $form['table-custom_fields-description'] += array('#before' => wpautop(__('There is no custom fields assign to this post type.', 'wpcf')) . '<ul>'); } else { $form['table-custom_fields-description'] += array('#before' => wpautop(__('Check which fields should be shown on custom post list as a column.', 'wpcf')) . '<ul>'); if (count($options_to_add) > 1) { $form['table-custom_fields-description'] += array('#after' => '</ul>' . wpautop(__('Drag and drop ticked custom fields to reorder.', 'wpcf'))); } } $form['table-custom_fields-close'] = wpcf_admin_metabox_end(true, '_builtin'); return $form; }
/** * Summary. * * Description. * * @since x.x.x * @access (for functions: only use if private) * * @see Function/method/class relied on * @link URL * @global type $varname Description. * @global type $varname Description. * * @param type $var Description. * @param type $var Optional. Description. * @return type Description. */ public function ajax_metabox_custom_fields_get_content() { // check nonce if (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], $this->get_nonce('custom-fields-box', $_REQUEST['type'], $_REQUEST['id']))) { $this->verification_failed_and_die(); } // we need an array of all possible fields to check agains what is stored in the database, // otherwise stored db fields will always be shown until next cpt save $possible_fields = array(); // current selected groups $current_selected_groups = isset($_REQUEST['current_groups']) && !empty($_REQUEST['current_groups']) ? $_REQUEST['current_groups'] : array(); // current selected fields $current_selected_fields = isset($_REQUEST['current_fields']) && !empty($_REQUEST['current_fields']) ? $_REQUEST['current_fields'] : array(); // get post type slug $post_type_slug = $this->get_type_from_request('id'); // not saved yet if (empty($post_type_slug)) { $this->print_notice_and_die(__('Please save first, before you can edit the custom fields.', 'wpcf')); } // current post type require_once WPCF_INC_ABSPATH . '/classes/class.types.admin.post-type.php'; $wpcf_post_type = new Types_Admin_Post_Type(); $post_type = $wpcf_post_type->get_post_type($post_type_slug); // no groups selected if (empty($current_selected_groups)) { $this->print_notice_and_die(sprintf(__('You can include Post Fields in the <b>listing page for %s</b>. Add Post Fields to this Post Type and you will be able to select which ones will display in the listing page.', 'wpcf'), $post_type['labels']['name'])); } // all custom field groups $custom_field_groups = wpcf_admin_fields_get_groups(TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, true, true); if (empty($custom_field_groups)) { echo wpautop(__('To use custom fields, please create a group to hold them.', 'wpcf')); die; } // form $form = $field_checkboxes = array(); // add description to form $form['description'] = array('#type' => 'markup', '#markup' => sprintf('<p class="description">%s</p>', __('Check which fields should be shown on this Post Type list as a columns.', 'wpcf'))); $form_add = array(); // for all custom field groups foreach ($custom_field_groups as $custom_field_group) { // skip group if not selected if (!in_array($custom_field_group['id'], $current_selected_groups)) { continue; } $id = 'group-' . $custom_field_group['id'] . '-'; // add fields to possible fields if (isset($custom_field_group['fields'])) { $possible_fields = array_merge($possible_fields, $custom_field_group['fields']); } else { $custom_field_group['fields'] = array(); } // get field checkboxes $field_checkboxes = $this->build_options($post_type, $custom_field_group['fields'], 'custom-field-%s'); // no fields if (empty($field_checkboxes)) { $form_add[$id] = array('#type' => 'markup', '#title' => $custom_field_group['name'], '#markup' => __('There are no available fields in this group.', 'wpcf'), '#inline' => true, '_builtin' => true, '#pattern' => '<div class="js-wpcf-custom-field-group wpcf-custom-field-group"><h4><TITLE></h4><ul><ELEMENT></ul></div>'); continue; } // if not first time loading fields use the selected fields and not the database stored if ($_REQUEST['init'] == 0) { foreach ($field_checkboxes as $field_key => $field_values) { // check if field is selected $field_values['#default_value'] = in_array('wpcf-' . $field_key, $current_selected_fields) || in_array($field_key, $current_selected_fields) ? 1 : 0; $field_checkboxes[$field_key] = $field_values; } } // add checkboxes to form $form_add[$id] = array('#type' => 'checkboxes', '#title' => $custom_field_group['name'], '#options' => $field_checkboxes, '#name' => 'wpcf[group][supports]', '#inline' => true, '_builtin' => true, '#pattern' => '<div class="js-wpcf-custom-field-group wpcf-custom-field-group"><h4><TITLE></h4><ul><ELEMENT></ul></div>'); } // if form is still empty here the user didn't create any group if (empty($form_add)) { $form['no-fields'] = array('#type' => 'markup', '#markup' => sprintf('<p class="description">%s</p>', __('No custom field groups found.', 'wpcf'))); $form = wpcf_form(__FUNCTION__, $form); echo $form->renderForm(); die; } $form['groups-open'] = array('#type' => 'markup', '#markup' => '<div class="clearfix wpcf-custom-field-group-container js-wpcf-custom-field-group-container">'); $form += $form_add; $form['groups-close'] = array('#type' => 'markup', '#markup' => '</div>'); /** * Sortable Area */ // on first load use db stored fields if ($_REQUEST['init'] == 1) { $fields_selected = wpcf_admin_fields_get_active_fields_by_post_type($post_type['slug']); // otherwise use unsaved selected fields } else { $fields_selected = array(); foreach ($custom_field_groups as $custom_field_group) { if (isset($custom_field_group['fields'])) { $fields_selected += $custom_field_group['fields']; } } } if (!empty($fields_selected)) { $field_checkboxes = $this->build_options($post_type, $fields_selected, 'ct[custom_fields][%s]'); foreach ($field_checkboxes as $field_key => $field_values) { if ($_REQUEST['init'] == 0) { $field_values['#default_value'] = in_array('wpcf-' . $field_key, $current_selected_fields) || in_array($field_key, $current_selected_fields) ? 1 : 0; } // remove if unchecked if (empty($field_values['#default_value'])) { unset($field_checkboxes[$field_key]); continue; } $field_checkboxes[$field_key]['#type'] = 'hidden'; $field_checkboxes[$field_key]['#pattern'] = '<div class="wpcf-custom-field js-wpcf-custom-field"><ELEMENT><TITLE></div>'; } } $form['wpcf-custom-field-order-open'] = array('#type' => 'markup', '#markup' => '<div class="wpcf-custom-field-order">'); $fields_in_sortable = array(); // on first load use db data if not empty if ($_REQUEST['init'] == 1 && isset($post_type['custom_fields']) && !empty($post_type['custom_fields'])) { $fields_in_sortable = $post_type['custom_fields']; // otherwise currently selected fields } elseif (!empty($current_selected_fields)) { foreach ($current_selected_fields as $field) { $fields_in_sortable[$field] = 1; } } if (!empty($fields_in_sortable)) { $fields_to_add_to_sortable = array(); foreach (array_keys($fields_in_sortable) as $key) { // remove "wpcf-" of id if field in sortable area is not part of checkboxes if (!isset($field_checkboxes[$key])) { $key = preg_replace('/^wpcf-/', '', $key); } // abort if sortable field is still not part of checkboxes // OR if field isn't in possible fields if (!isset($field_checkboxes[$key]) || !array_key_exists($key, $possible_fields)) { continue; } $fields_to_add_to_sortable['wpcf-custom-field-order-' . $key] = array('#type' => 'markup', '#markup' => sprintf('<li class="menu-item-handle" id="wpcf-custom-field-%s"><input type="hidden" name="%s" value="1" />%s</li>', esc_attr($field_checkboxes[$key]['#attributes']['data-wpcf-key']), esc_attr($field_checkboxes[$key]['#name']), $field_checkboxes[$key]['#title'])); } // add fields to sortable area if (!empty($fields_to_add_to_sortable)) { $form['wpcf-custom-field-order-ul-open'] = $this->get_empty_ul(); $form += $fields_to_add_to_sortable; $form['wpcf-custom-field-order-ul-close'] = array('#type' => 'markup', '#markup' => '</ul>'); } } // if no fields selected add empty ul if (!isset($fields_to_add_to_sortable) || empty($fields_to_add_to_sortable)) { $form['wpcf-custom-field-order-ul'] = $this->get_empty_ul(true); } // close sortable container $form['wpcf-custom-field-order-close'] = array('#type' => 'markup', '#markup' => '</div>'); $form = wpcf_form(__FUNCTION__, $form); echo $form->renderForm(); die; }