/** * Creates form elements. * * @param type $post * @param type $fields * @return type */ function wpcf_admin_post_process_fields($post = false, $fields = array(), $use_cache = true, $add_to_editor = true, $context = 'group') { global $pagenow; static $count = array(); //Need this to count if there are more than one same field on page // Get cached static $cache = array(); $cache_key = $post ? $post->ID : false; if ($use_cache && $cache_key && isset($cache[$cache_key])) { return $cache[$cache_key]; } $fields_processed = array(); // Get invalid fields (if submitted) if ($post) { $invalid_fields = get_post_meta($post->ID, 'wpcf-invalid-fields', true); delete_post_meta($post->ID, 'wpcf-invalid-fields'); } $original_cf = array(); if (function_exists('wpml_get_copied_fields_for_post_edit')) { $original_cf = wpml_get_copied_fields_for_post_edit(); } foreach ($fields as $field) { $field = wpcf_admin_fields_get_field($field['id']); if (!empty($field)) { // TODO Monitor added rand suffix if (isset($count[$field['type'] . '-' . $field['slug']])) { $field_id = 'wpcf-' . $field['type'] . '-' . $field['slug'] . '-' . $count[$field['type'] . '-' . $field['slug']] . '-' . mt_rand(); $count[$field['type'] . '-' . $field['slug']] += 1; } else { $field_id = 'wpcf-' . $field['type'] . '-' . $field['slug'] . '-' . mt_rand(); $count[$field['type'] . '-' . $field['slug']] = 1; } $field_init_data = wpcf_fields_type_action($field['type']); // Get inherited field $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']); } // Set value $field['value'] = ''; if ($post) { $field['value'] = get_post_meta($post->ID, wpcf_types_get_meta_prefix($field) . $field['slug'], true); } else { // see if it's in the original custom fields to copy. if (!empty($original_cf['fields'])) { foreach ($original_cf['fields'] as $cf_id) { if (wpcf_types_get_meta_prefix($field) . $field['slug'] == $cf_id) { $field['wpml_action'] = 'copy'; $field['value'] = get_post_meta($original_cf['original_post_id'], wpcf_types_get_meta_prefix($field) . $field['slug'], true); break; } } } } // Mark any field that is going to be copied. if (!empty($original_cf['fields'])) { foreach ($original_cf['fields'] as $cf_id) { if (wpcf_types_get_meta_prefix($field) . $field['slug'] == $cf_id) { // $field['description_extra'] = $original_cf['copy_message']; $field['readonly'] = true; $field['wpml_action'] = 'copy'; break; } } } // Apply filters $field['value'] = apply_filters('wpcf_fields_value_get', $field['value'], $field, $field_init_data); $field['value'] = apply_filters('wpcf_fields_slug_' . $field['slug'] . '_value_get', $field['value'], $field, $field_init_data); $field['value'] = apply_filters('wpcf_fields_type_' . $field['type'] . '_value_get', $field['value'], $field, $field_init_data); wpcf_admin_post_field_load_js_css($field_init_data); $element = array(); // Set generic values $element = array('#type' => isset($field_init_data['inherited_field_type']) ? $field_init_data['inherited_field_type'] : $field['type'], '#id' => $field_id, '#title' => wpcf_translate('field ' . $field['id'] . ' name', $field['name']), '#description' => wpautop(wpcf_translate('field ' . $field['id'] . ' description', $field['description'])), '#name' => 'wpcf[' . $field['slug'] . ']', '#value' => isset($field['value']) ? $field['value'] : '', 'wpcf-id' => $field['id'], 'wpcf-slug' => $field['slug'], 'wpcf-type' => $field['type']); // Set inherited values $element_inherited = array(); if ($inherited_field_data) { if (function_exists('wpcf_fields_' . $field_init_data['inherited_field_type'] . '_meta_box_form')) { $element_inherited = call_user_func_array('wpcf_fields_' . $field_init_data['inherited_field_type'] . '_meta_box_form', array($field, $element)); } } $element = array_merge($element, $element_inherited); if (isset($field['description_extra'])) { $element['#description'] .= wpautop($field['description_extra']); } // Set atributes #1 if (isset($field['disable'])) { $field['#disable'] = $field['disable']; } if (!empty($field['disable'])) { $field['#attributes']['disabled'] = 'disabled'; } if (!empty($field['readonly'])) { $field['#attributes']['readonly'] = 'readonly'; } // Set specific values if (defined('WPCF_INC_ABSPATH') && file_exists(WPCF_INC_ABSPATH . '/fields/' . $field['type'] . '.php')) { require_once WPCF_INC_ABSPATH . '/fields/' . $field['type'] . '.php'; } // Load field // TODO remove // wpcf_fields_type_action($field['type']); // require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields/' . $field['type'] // . '.php'; if (function_exists('wpcf_fields_' . $field['type'] . '_meta_box_form')) { $element_specific = call_user_func_array('wpcf_fields_' . $field['type'] . '_meta_box_form', array($field, $element)); // Check if it's single if (isset($element_specific['#type'])) { // Format description if (!empty($element_specific['#description'])) { $element_specific['#description'] = wpautop($element_specific['#description']); } $element = array_merge($element, $element_specific); // Set validation element if (isset($field['data']['validate'])) { $element['#validate'] = $field['data']['validate']; } } else { // More fields, loop all foreach ($element_specific as $element_specific_fields_key => $element_specific_fields_value) { // Format description if (!empty($element_specific_fields_value['#description'])) { $element_specific_fields_value['#description'] = wpautop($element_specific_fields_value['#description']); } // If no ID if (!isset($element_specific_fields_value['#id'])) { $element_specific_fields_value['#id'] = 'wpcf-' . $field['slug'] . '-' . mt_rand(); } // Set validation element if (!empty($element_specific_fields_value['#_validate_this']) && isset($field['data']['validate'])) { $element_specific_fields_value['#validate'] = $field['data']['validate']; } // If no name, name = #ignore or id = #ignore - IGNORE if (!isset($element_specific_fields_value['#name']) || $element_specific_fields_value['#name'] == '#ignore' || $element_specific_fields_value['#id'] == '#ignore') { $element_specific_fields_value['#id'] = 'wpcf-' . $field['slug'] . '-' . mt_rand(); $element_specific_fields_value['#name'] = 'wpcf[ignore][' . mt_rand() . ']'; $fields_processed[$element_specific_fields_value['#id']] = $element_specific_fields_value; continue; } // if ($field['type'] == 'skype' && strpos($element_specific_fields_value['#name'], '[skypename]') === false) { // continue; // } // This one is actually value and keep it (#name is required) $element = array_merge($element, $element_specific_fields_value); // Add it here to keep order $fields_processed[$element['#id']] = $element; } } } // Set atributes #2 (override) if (isset($field['disable'])) { $element['#disable'] = $field['disable']; } if (!empty($field['disable'])) { $element['#attributes']['disabled'] = 'disabled'; } if (!empty($field['readonly'])) { $element['#attributes']['readonly'] = 'readonly'; if (!empty($element['#options'])) { foreach ($element['#options'] as $key => $option) { if (!is_array($option)) { $element['#options'][$key] = array('#title' => $key, '#value' => $option); } $element['#options'][$key]['#attributes']['readonly'] = 'readonly'; if ($element['#type'] == 'select') { $element['#options'][$key]['#attributes']['disabled'] = 'disabled'; } } } if ($element['#type'] == 'select') { $element['#attributes']['disabled'] = 'disabled'; } } // Set validation element if ($field['type'] != 'skype' && empty($element['#validate']) && isset($field['data']['validate'])) { $element['#validate'] = $field['data']['validate']; } // Check if it was invalid no submit and add error message if ($post && !empty($invalid_fields)) { if (isset($invalid_fields[$element['#id']]['#error'])) { $element['#error'] = $invalid_fields[$element['#id']]['#error']; } } // Set WPML locked icon if (isset($field['wpml_action']) && $field['wpml_action'] == 'copy') { $element['#title'] .= '<img src="' . WPCF_EMBEDDED_RES_RELPATH . '/images/locked.png" alt="' . __('This field is locked for editing because WPML will copy its value from the original language.', 'wpcf') . '" title="' . __('This field is locked for editing because WPML will copy its value from the original language.', 'wpcf') . '" style="position:relative;left:2px;top:2px;" />'; } // Add to editor if ($add_to_editor) { wpcf_admin_post_add_to_editor($field); } $fields_processed[$element['#id']] = apply_filters('wpcf_post_edit_field', $element, $field, $post, $context); } } if ($cache_key && isset($cache[$cache_key])) { $cache[$cache_key] = $fields_processed; } return $fields_processed; }
/** * Processes single field. * * @staticvar array $repetitive_started * @param type $post * @param type $field * @param type $use_cache * @param type $add_to_editor * @param type $context * @param type $original_cf * @param type $invalid_fields * @return mixed boolean|array */ function wpcf_admin_post_process_field($post = false, $field_unedited = array(), $use_cache = true, $add_to_editor = true, $context = 'group', $original_cf = array(), $invalid_fields = array()) { $field = wpcf_admin_fields_get_field($field_unedited['id']); if (!empty($field)) { // Set values $field['value'] = isset($field_unedited['value']) ? $field_unedited['value'] : ''; $field['wpml_action'] = isset($field_unedited['wpml_action']) ? $field_unedited['wpml_action'] : ''; if (isset($count[$field['type'] . '-' . $field['slug']])) { $field_id = 'wpcf-' . $field['type'] . '-' . $field['slug'] . '-' . $count[$field['type'] . '-' . $field['slug']] . '-' . mt_rand(); $count[$field['type'] . '-' . $field['slug']] += 1; } else { $field_id = 'wpcf-' . $field['type'] . '-' . $field['slug'] . '-' . mt_rand(); $count[$field['type'] . '-' . $field['slug']] = 1; } $field_init_data = wpcf_fields_type_action($field['type']); // Get inherited field $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']); } // Mark any field that is going to be copied. if (!empty($original_cf['fields'])) { foreach ($original_cf['fields'] as $cf_id) { if (wpcf_types_get_meta_prefix($field) . $field['slug'] == $cf_id) { $field['readonly'] = true; $field['wpml_action'] = 'copy'; break; } } } // Apply filters $field['value'] = apply_filters('wpcf_fields_value_get', $field['value'], $field, $field_init_data); $field['value'] = apply_filters('wpcf_fields_slug_' . $field['slug'] . '_value_get', $field['value'], $field, $field_init_data); $field['value'] = apply_filters('wpcf_fields_type_' . $field['type'] . '_value_get', $field['value'], $field, $field_init_data); wpcf_admin_post_field_load_js_css($field_init_data); $element = array(); // Set generic values $element = array('#type' => isset($field_init_data['inherited_field_type']) ? $field_init_data['inherited_field_type'] : $field['type'], '#id' => $field_id, '#title' => wpcf_translate('field ' . $field['id'] . ' name', $field['name']), '#description' => wpautop(wpcf_translate('field ' . $field['id'] . ' description', $field['description'])), '#name' => 'wpcf[' . $field['slug'] . ']', '#value' => isset($field['value']) ? $field['value'] : '', 'wpcf-id' => $field['id'], 'wpcf-slug' => $field['slug'], 'wpcf-type' => $field['type']); // Set inherited values $element_inherited = array(); if ($inherited_field_data) { if (function_exists('wpcf_fields_' . $field_init_data['inherited_field_type'] . '_meta_box_form')) { $element_inherited = call_user_func_array('wpcf_fields_' . $field_init_data['inherited_field_type'] . '_meta_box_form', array($field, $element)); } } $element = array_merge($element, $element_inherited); if (isset($field['description_extra'])) { $element['#description'] .= wpautop($field['description_extra']); } // Set atributes #1 if (isset($field['disable'])) { $field['#disable'] = $field['disable']; } if (!empty($field['disable'])) { $field['#attributes']['disabled'] = 'disabled'; } if (!empty($field['readonly'])) { $field['#attributes']['readonly'] = 'readonly'; } // Set specific values if (defined('WPCF_INC_ABSPATH') && file_exists(WPCF_INC_ABSPATH . '/fields/' . $field['type'] . '.php')) { require_once WPCF_INC_ABSPATH . '/fields/' . $field['type'] . '.php'; } // Load field if (function_exists('wpcf_fields_' . $field['type'] . '_meta_box_form')) { $element_specific = call_user_func_array('wpcf_fields_' . $field['type'] . '_meta_box_form', array($field, $element)); // Check if it's single if (isset($element_specific['#type'])) { // Format description if (!empty($element_specific['#description'])) { $element_specific['#description'] = wpautop($element_specific['#description']); } $element = array_merge($element, $element_specific); // Set validation element if (isset($field['data']['validate'])) { $element['#validate'] = $field['data']['validate']; } // Repetitive fields if (wpcf_admin_is_repetitive($field) && $context != 'post_relationship' && (!isset($field['wpml_action']) || $field['wpml_action'] != 'copy')) { $element = wpcf_admin_post_process_repetitive_field($post, $field, $element); } } else { // More fields, loop all // Only Skype for now have multiple fields, so process only that if ($field['type'] == 'skype') { $skype_element = array(); foreach ($element_specific as $element_specific_fields_key => $element_specific_fields_value) { $element_specific_fields_value['__element_key'] = $element_specific_fields_key; // Format description if (!empty($element_specific_fields_value['#description'])) { $element_specific_fields_value['#description'] = wpautop($element_specific_fields_value['#description']); } // If no ID if (!isset($element_specific_fields_value['#id'])) { $element_specific_fields_value['#id'] = 'wpcf-' . $field['slug'] . '-' . mt_rand(); } // Set validation element if (!empty($element_specific_fields_value['#_validate_this']) && isset($field['data']['validate'])) { $element_specific_fields_value['#validate'] = $field['data']['validate']; } if ($element_specific_fields_key != 'skypename') { if (!isset($element_specific_fields_value['#name'])) { $element_specific_fields_value['#name'] = 'wpcf[ignore][' . mt_rand() . ']'; } $skype_element[$element_specific_fields_value['#id']] = $element_specific_fields_value; continue; } // This one is actually value and keep it (#name is required) $element = array_merge($element, $element_specific_fields_value); // Add it here to keep order $skype_element[$element['#id']] = $element; } // Repetitive fields if (wpcf_admin_is_repetitive($field) && $context != 'post_relationship' && (!isset($field['wpml_action']) || $field['wpml_action'] != 'copy')) { list($element, $skype_element) = wpcf_admin_post_process_repetitive_field_skype($post, $field, $skype_element); } } } } else { // Repetitive fields if (wpcf_admin_is_repetitive($field) && $context != 'post_relationship' && (!isset($field['wpml_action']) || $field['wpml_action'] != 'copy')) { $element = wpcf_admin_post_process_repetitive_field($post, $field, $element); } } // Set atributes #2 (override) if (isset($field['disable'])) { $element['#disable'] = $field['disable']; } if (!empty($field['disable'])) { $element['#attributes']['disabled'] = 'disabled'; } if (!empty($field['readonly'])) { $element['#attributes']['readonly'] = 'readonly'; if (!empty($element['#options'])) { foreach ($element['#options'] as $key => $option) { if (!is_array($option)) { $element['#options'][$key] = array('#title' => $key, '#value' => $option); } $element['#options'][$key]['#attributes']['readonly'] = 'readonly'; if ($element['#type'] == 'select') { $element['#options'][$key]['#attributes']['disabled'] = 'disabled'; } } } if ($element['#type'] == 'select') { $element['#attributes']['disabled'] = 'disabled'; } } // Set validation element if ($field['type'] != 'skype' && empty($element['#validate']) && isset($field['data']['validate'])) { $element['#validate'] = $field['data']['validate']; } // Check if it was invalid no submit and add error message if ($post && !empty($invalid_fields)) { if (isset($invalid_fields[$element['#id']]['#error'])) { $element['#error'] = $invalid_fields[$element['#id']]['#error']; } } // Set WPML locked icon if (isset($field['wpml_action']) && $field['wpml_action'] == 'copy') { $element['#title'] .= '<img src="' . WPCF_EMBEDDED_RES_RELPATH . '/images/locked.png" alt="' . __('This field is locked for editing because WPML will copy its value from the original language.', 'wpcf') . '" title="' . __('This field is locked for editing because WPML will copy its value from the original language.', 'wpcf') . '" style="position:relative;left:2px;top:2px;" />'; } // Add to editor if ($add_to_editor) { wpcf_admin_post_add_to_editor($field); } // Add repetitive class if (wpcf_admin_is_repetitive($field) && $context != 'post_relationship' && (!isset($field['wpml_action']) || $field['wpml_action'] != 'copy')) { if (!empty($element['#options']) && $element['#type'] != 'select') { foreach ($element['#options'] as $temp_key => $temp_value) { $element['#options'][$temp_key]['#attributes']['class'] = isset($element['#attributes']['class']) ? $element['#attributes']['class'] . ' wpcf-repetitive' : 'wpcf-repetitive'; } } else { $element['#attributes']['class'] = isset($element['#attributes']['class']) ? $element['#attributes']['class'] . ' wpcf-repetitive' : 'wpcf-repetitive'; } wpcf_admin_add_js_settings('wpcfFormRepetitiveUniqueValuesCheckText', '\'' . __('Warning: same values set', 'wpcf') . '\''); } // Set read-only if (isset($field['wpml_action']) && $field['wpml_action'] == 'copy') { if (isset($element['#options'])) { foreach ($element['#options'] as $temp_key => $temp_value) { if (isset($temp_value['#attributes'])) { $element['#options'][$temp_key]['#attributes']['readonly'] = 'readonly'; } else { $element['#options'][$temp_key]['#attributes'] = array('readonly' => 'readonly'); } } } if ($field['type'] == 'select') { if (isset($element['#attributes'])) { $element['#attributes']['disabled'] = 'disabled'; } else { $element['#attributes'] = array('disabled' => 'disabled'); } } else { if (isset($element['#attributes'])) { $element['#attributes']['readonly'] = 'readonly'; } else { $element['#attributes'] = array('readonly' => 'readonly'); } } } // Specific for Skype if ($field['type'] == 'skype') { $skype_element[$element['#id']] = $element; $element = $skype_element; } return array('field' => $field, 'element' => $element); } return false; }
/** * 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&wpcf_action=pr_sort&field=' . '_wp_title&sort=' . $title_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_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&wpcf_action=pr_sort&field=' . '_wp_body&sort=' . $body_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_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&wpcf_action=pr_sort&field=' . $header . '&sort=' . $field_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_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&wpcf_action=pr_sort&field=' . $header . '&sort=' . $field_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_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&wpcf_action=pr_sort&field=' . '_wpcf_pr_parent&sort=' . $parent_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&post_type_sort_parent=' . $_parent . '&_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . $temp_parent_type->label . '</a>'; } } return $headers; }
/** * 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&wpcf_action=pr_sort&field=' . '_wp_title&sort=' . $title_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_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&wpcf_action=pr_sort&field=' . '_wp_body&sort=' . $body_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_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&wpcf_action=pr_sort&field=' . $header . '&sort=' . $field_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_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&wpcf_action=pr_sort&field=' . $header . '&sort=' . $field_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_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&wpcf_action=pr_sort&field=' . '_wpcf_pr_parent&sort=' . $parent_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&post_type_sort_parent=' . $temp_parent . '&_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&wpcf_action=pr_sort&field=' . '_wp_title&sort=' . $title_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_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&wpcf_action=pr_sort&field=' . '_wp_body&sort=' . $body_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_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&wpcf_action=pr_sort&field=' . wpcf_types_get_meta_prefix($field) . $field['slug'] . '&sort=' . $field_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_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&wpcf_action=pr_sort&field=' . '_wpcf_pr_parent&sort=' . $parent_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&post_type_sort_parent=' . $temp_parent . '&_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(' </th><th class="wpcf-sortable">', $headers) . ' </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; }
/** * 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&wpcf_action=pr_sort&field=' . '_wp_title&sort=' . $title_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_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&wpcf_action=pr_sort&field=' . '_wp_body&sort=' . $body_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_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&wpcf_action=pr_sort&field=' . $header . '&sort=' . $field_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_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&wpcf_action=pr_sort&field=' . $header . '&sort=' . $field_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_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&wpcf_action=pr_sort&field=' . '_wpcf_pr_parent&sort=' . $parent_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&post_type_sort_parent=' . $temp_parent . '&_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&wpcf_action=pr_sort&field=' . '_wp_title&sort=' . $title_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_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&wpcf_action=pr_sort&field=' . '_wp_body&sort=' . $body_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_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&wpcf_action=pr_sort&field=' . wpcf_types_get_meta_prefix($field) . $field['slug'] . '&sort=' . $field_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_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&wpcf_action=pr_sort&field=' . '_wpcf_pr_parent&sort=' . $parent_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&post_type_sort_parent=' . $temp_parent . '&_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . $temp_parent_type->label . '</a>'; } } } return $headers; }