Skip to content

Commit

Permalink
Merge branch 'mckernanin-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsternberg committed Aug 31, 2016
2 parents ceaa2a3 + 73891af commit e8bc5aa
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 46 deletions.
14 changes: 13 additions & 1 deletion example-field-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,19 @@ function cmb2_attached_posts_field_metaboxes_example() {
'show_thumbnails' => true, // Show thumbnails on the left
'filter_boxes' => true, // Show a text box for filtering the results
'query_args' => array( 'posts_per_page' => 10 ), // override the get_posts args
)
),
) );

$example_meta->add_field( array(
'name' => __( 'Attached Users', 'cmb2' ),
'desc' => __( 'Drag users from the left column to the right column to attach them to this page.<br />You may rearrange the order of the users in the right column by dragging and dropping.', 'cmb2' ),
'id' => 'attached_cmb2_attached_users',
'type' => 'custom_attached_posts',
'options' => array(
'show_thumbnails' => true, // Show thumbnails on the left
'filter_boxes' => true, // Show a text box for filtering the results
'query_users' => true,
),
) );

}
Expand Down
141 changes: 96 additions & 45 deletions init.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,40 +43,56 @@ public function render( $field, $escaped_value, $object_id, $object_type, $field

$this->setup_admin_scripts();

// Setup our args
$args = wp_parse_args( (array) $field->options( 'query_args' ), array(
'post_type' => 'post',
'posts_per_page' => 100,
'orderby' => 'name',
'order' => 'ASC',
) );

// loop through post types to get labels for all
$post_type_labels = array();
foreach ( (array) $args['post_type'] as $post_type ) {
// Get post type object for attached post type
$attached_post_type = get_post_type_object( $post_type );

// continue if we don't have a label for the post type
if ( ! $attached_post_type || ! isset( $attached_post_type->labels->name ) ) {
continue;
$query_users = $field->options( 'query_users' );

if ( ! $query_users ) {

// Setup our args
$args = wp_parse_args( (array) $field->options( 'query_args' ), array(
'post_type' => 'post',
'posts_per_page' => 100,
'orderby' => 'name',
'order' => 'ASC',
) );

// loop through post types to get labels for all
$post_type_labels = array();
foreach ( (array) $args['post_type'] as $post_type ) {
// Get post type object for attached post type
$attached_post_type = get_post_type_object( $post_type );

// continue if we don't have a label for the post type
if ( ! $attached_post_type || ! isset( $attached_post_type->labels->name ) ) {
continue;
}

$post_type_labels[] = $attached_post_type->labels->name;
$post_type_labels = implode( '/', $post_type_labels );
}

$post_type_labels[] = $attached_post_type->labels->name;
} else {
// Setup our args
$args = wp_parse_args( (array) $field->options( 'query_args' ), array(
'number' => 100,
) );
$post_type_labels = $field_type->_text( 'users_text', esc_html__( 'Users' ) );
}

$post_type_labels = implode( '/', $post_type_labels );

// Check 'filter' setting
$filter_boxes = $field->options( 'filter_boxes' )
? '<div class="search-wrap"><input type="text" placeholder="' . sprintf( __( 'Filter %s', 'cmb' ), $post_type_labels ) . '" class="regular-text search" name="%s" /></div>'
: '';

// Get our posts
$posts = get_posts( $args );
if ( ! $query_users ) {
// Get our posts
$objects = get_posts( $args );
} else {
// Get our users
$objects = new WP_User_Query( $args );
$objects = ! $objects || empty( $objects->results ) ? false : $objects->results;
}

// If there are no posts found, just stop
if ( ! $posts ) {
if ( empty( $objects ) ) {
return;
}

Expand Down Expand Up @@ -104,23 +120,38 @@ public function render( $field, $escaped_value, $object_id, $object_type, $field
echo '<ul class="retrieved connected' . $has_thumbnail . $hide_selected . '">';

// Loop through our posts as list items
foreach ( $posts as $post ) {
foreach ( $objects as $object ) {

// Increase our count
$count++;

// Set our zebra stripes
$zebra = $count % 2 == 0 ? 'even' : 'odd';
$class = $count % 2 == 0 ? 'even' : 'odd';

// Set a class if our post is in our attached post meta
$added = ! empty ( $attached ) && in_array( $post->ID, $attached ) ? ' added' : '';
// Set a class if our post is in our attached meta
$class .= ! empty ( $attached ) && in_array( $object->ID, $attached ) ? ' added' : '';

// Set thumbnail if the options is true
$thumbnail = $has_thumbnail ? get_the_post_thumbnail( $post->ID, array( 50, 50 ) ) : '';
$thumbnail = '';

// Build our list item
echo '<li data-id="', $post->ID ,'" class="' . $zebra . $added . '">', $thumbnail ,'<a title="'. __( 'Edit' ) .'" href="', get_edit_post_link( $post ) ,'">', get_the_title( $post ) ,'</a><span class="dashicons dashicons-plus add-remove"></span></li>';
if ( $has_thumbnail ) {
// Set thumbnail if the options is true
$thumbnail = $query_users
? get_avatar( $object->ID, 25 )
: get_the_post_thumbnail( $object->ID, array( 50, 50 ) );
}

$edit_link = $query_users ? get_edit_user_link( $object->ID ) : get_edit_post_link( $object );
$title = $query_users ? $object->data->display_name : get_the_title( $object );

// Build our list item
printf(
'<li data-id="%d" class="%s">%s<a title="' . __( 'Edit' ) . '" href="%s">%s</a><span class="dashicons dashicons-plus add-remove"></span></li>',
$object->ID,
$class,
$thumbnail,
$edit_link,
$title
);
}

// Close our retrieved, or found, posts
Expand All @@ -137,10 +168,10 @@ public function render( $field, $escaped_value, $object_id, $object_type, $field

echo '<ul class="attached connected', $has_thumbnail ,'">';

// If we have any posts saved already, display them
$post_ids = $this->display_attached( $field, $attached );
// If we have any ids saved already, display them
$ids = $this->display_attached( $field, $attached );

$value = ! empty( $post_ids ) ? implode( ',', $post_ids ) : '';
$value = ! empty( $ids ) ? implode( ',', $ids ) : '';

// Close up shop
echo '</ul><!-- #attached -->';
Expand Down Expand Up @@ -173,37 +204,57 @@ protected function display_attached( $field, $attached ) {
return;
}

$query_users = $field->options( 'query_users' );

// Set our count to zero
$count = 0;

$show_thumbnails = $field->options( 'show_thumbnails' );
// Remove any empty values
$attached = array_filter( $attached );
$post_ids = array();

$ids = array();

// Loop through and build our existing display items
foreach ( $attached as $post_id ) {
if ( ! get_post( $post_id ) ) {
foreach ( $attached as $id ) {
$object = $query_users ? get_user_by( 'id', $id ) : get_post( $id );

if ( empty( $object ) ) {
continue;
}

// Increase our count
$count++;

// Set our zebra stripes
$zebra = $count % 2 == 0 ? 'even' : 'odd';
$class = $count % 2 == 0 ? 'even' : 'odd';

// Set thumbnail if the options is true
$thumbnail = $show_thumbnails ? get_the_post_thumbnail( $post_id, array( 50, 50 ) ) : '';
$thumbnail = '';

// Build our list item
echo '<li data-id="' . $post_id . '" class="' . $zebra . '">', $thumbnail ,'<a title="'. __( 'Edit' ) .'" href="', get_edit_post_link( $post_id ) ,'">'. get_the_title( $post_id ) .'</a><span class="dashicons dashicons-minus add-remove"></span></li>';
if ( $show_thumbnails ) {
// Set thumbnail if the options is true
$thumbnail = $query_users
? get_avatar( $object->ID, 25 )
: get_the_post_thumbnail( $object->ID, array( 50, 50 ) );
}

$edit_link = $query_users ? get_edit_user_link( $object->ID ) : get_edit_post_link( $object );
$title = $query_users ? $object->data->display_name : get_the_title( $object );

$post_ids[] = $post_id;
// Build our list item
printf(
'<li data-id="%d" class="%s">%s<a title="' . __( 'Edit' ) . '" href="%s">%s</a><span class="dashicons dashicons-minus add-remove"></span></li>',
$id,
$class,
$thumbnail,
$edit_link,
$title
);

$ids[] = $id;
}

return $post_ids;
return $ids;
}

public function sanitize( $sanitized_val, $val ) {
Expand Down

0 comments on commit e8bc5aa

Please sign in to comment.