/** * 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; }
/** * Pagination */ function pagination() { global $wpcf; // Pagination $total_items = count($this->children); $per_page = $wpcf->relationship->get_items_per_page($this->parent_post_type, $this->child_post_type); $page = isset($_GET['page']) ? intval($_GET['page']) : 1; $offset = $page == 1 ? 0 : ($page - 1) * $per_page; $next = $total_items > $offset + $per_page; $prev = $page == 1 ? false : true; if ($total_items > $per_page) { $this->children = array_splice($this->children, $offset, $per_page); } $this->pagination_top = wpcf_pr_admin_has_pagination($this->parent, $this->child_post_type, $page, $prev, $next, $per_page, $total_items); /* * * * Add pagination bottom */ $options = array(__('All', 'wpcf') => 'all', 5 => 5, 10 => 10, 15 => 15); // Add sorting $add_data = isset($_GET['sort']) && isset($_GET['field']) ? '&sort=' . sanitize_text_field($_GET['sort']) . '&field=' . sanitize_text_field($_GET['field']) : ''; if (isset($_GET['post_type_sort_parent'])) { $add_data .= '&post_type_sort_parent=' . sanitize_text_field($_GET['post_type_sort_parent']); } $this->pagination_bottom = wpcf_form_simple(array('pagination' => array('#type' => 'select', '#before' => __('Show', 'wpcf'), '#after' => $this->child_post_type_object->labels->name, '#id' => 'wpcf_relationship_num_' . wpcf_unique_id(serialize($this->children)), '#name' => $wpcf->relationship->items_per_page_option_name, '#options' => $options, '#default_value' => $per_page, '#attributes' => array('class' => 'wpcf-relationship-items-per-page', 'data-action' => 'action=wpcf_ajax&wpcf_action=pr_pagination' . '&post_id=' . $this->parent->ID . '&post_type=' . $this->child_post_type . '&_wpnonce=' . wp_create_nonce('pr_pagination') . $add_data)))); }
/** * 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 ''; } global $wpdb; $output = array(); $repetitive_warning = false; // 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&suppress_filters=0&order=' . $_GET['sort']); } else { if ($_GET['field'] == '_wpcf_pr_parent') { $items = get_posts('post_type=' . $post_type . '&numberposts=-1&post_status=null&meta_key=' . '_wpcf_belongs_' . $parent_post_type . '_id&meta_value=' . $post->ID . '&suppress_filters=0'); if (!empty($items)) { $include = array(); $additional = array(); foreach ($items as $key => $item) { $meta = get_post_meta($item->ID, '_wpcf_belongs_' . $_GET['post_type_sort_parent'] . '_id', true); if (empty($meta)) { $additional[] = $item; continue; } $include[] = $item->ID; } if (!empty($include)) { $items = get_posts('post_type=' . $post_type . '&numberposts=-1&post_status=null' . '&meta_key=_wpcf_belongs_' . $_GET['post_type_sort_parent'] . '_id' . '&orderby=meta_value_num&order=' . esc_attr(strtoupper($_GET['sort'])) . '&suppress_filters=0&include=' . implode(',', $include)); $items = array_merge($items, $additional); } } } else { if ($_GET['field'] == '_wp_body') { $query = "\n SELECT p.ID, p.post_title, p.post_content, p.post_type\n FROM {$wpdb->posts} p\n WHERE p.post_type = %s\n AND p.post_status <> 'auto-draft'\n GROUP BY p.ID\n ORDER BY p.post_content " . esc_attr(strtoupper($_GET['sort'])); $items = $wpdb->get_results($wpdb->prepare($query, $post_type)); } else { $field = wpcf_admin_fields_get_field(str_replace('wpcf-', '', $_GET['field'])); $orderby = isset($field['type']) && in_array($field['type'], array('numeric', 'date')) ? 'meta_value_num' : 'meta_value'; $items = get_posts('post_type=' . $post_type . '&numberposts=-1&post_status=null&meta_key=' . '_wpcf_belongs_' . $parent_post_type . '_id&meta_value=' . $post->ID . '&suppress_filters=0'); if (!empty($items)) { $include = array(); $additional = array(); foreach ($items as $key => $item) { $meta = get_post_meta($item->ID, $_GET['field'], true); if (empty($meta)) { $additional[] = $item; continue; } $check = wpcf_cd_post_edit_field_filter(array(), $field, $item, 'post-relationship-sort'); if (isset($check['__wpcf_cd_status']) && $check['__wpcf_cd_status'] == 'failed') { $additional[] = $item; } else { $include[] = $item->ID; } } if (!empty($include)) { $items = get_posts('post_type=' . $post_type . '&numberposts=-1&post_status=null&meta_key=' . $_GET['field'] . '&orderby=' . $orderby . '&order=' . esc_attr(strtoupper($_GET['sort'])) . '&suppress_filters=0&include=' . implode(',', $include)); $items = array_merge($items, $additional); } } } } } } else { $items = get_posts('post_type=' . $post_type . '&numberposts=-1&post_status=null&meta_key=' . '_wpcf_belongs_' . $parent_post_type . '_id&suppress_filters=0&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 = wpcf_pr_admin_post_meta_box_has_form_headers($post, $post_type, $parent_post_type, $data); // 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, $headers); } $return = ''; $return .= $repetitive_warning ? '<div class="wpcf-message wpcf-error"><p>' . __('Repeating fields should not be used in child posts. Types will update all field values.', 'wpcf') . '</p></div>' : ''; $return .= wpcf_pr_admin_has_pagination($post, $post_type, $page, $prev, $next, $per_page, $total_items); $return .= '<div class="wpcf-pr-table-wrapper"><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></div>'; $return .= wpcf_form_render_js_validation('#post', false); return $return; }