Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First commit of attached post relationship label option - adds input … #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions css/attached-posts-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,8 @@ ul.hide-selected li.added{
color: #999;
margin: .2em -.2em 0 1px;
}

.attached-wrap .attached.connected li input{
display: block;
width: 60%;
}
1 change: 1 addition & 0 deletions example-field-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function cmb2_attached_posts_field_metaboxes_example() {
'type' => 'custom_attached_posts',
'options' => array(
'show_thumbnails' => true, // Show thumbnails on the left
//'show_relation' => true, // Show a text box for attached post relationship
'filter_boxes' => true, // Show a text box for filtering the results
'query_args' => array(
'posts_per_page' => 10,
Expand Down
64 changes: 56 additions & 8 deletions init.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class WDS_CMB2_Attached_Posts_Field {
const VERSION = CMB2_ATTACHED_POSTS_FIELD_VERSION;

/**
* @var WDS_CMB2_Attached_Posts_Field
* @var WDS_CMB2_Attached_Posts_Field
*/
protected static $single_instance = null;

Expand All @@ -29,6 +29,14 @@ class WDS_CMB2_Attached_Posts_Field {
*/
protected $do_type_label = false;

/**
* If show_relation option active relationship meta key and meta key suffic
*
* @var string
*/
protected $relation_key;
protected $meta_key_suffix = '_relation';

/**
* Creates or returns an instance of this class.
* @since 0.1.0
Expand All @@ -47,6 +55,7 @@ public static function get_instance() {
*/
protected function __construct() {
add_action( 'cmb2_render_custom_attached_posts', array( $this, 'render' ), 10, 5 );
add_action( 'cmb2_save_field', array( $this, 'attached_posts_relation_meta' ), 10, 4 );
add_action( 'cmb2_sanitize_custom_attached_posts', array( $this, 'sanitize' ), 10, 2 );
add_action( 'cmb2_attached_posts_field_add_find_posts_div', array( $this, 'add_find_posts_div' ) );
add_action( 'cmb2_after_init', array( $this, 'ajax_find_posts' ) );
Expand Down Expand Up @@ -143,12 +152,13 @@ public function render( $field, $escaped_value, $object_id, $object_type, $field
// Set .has_thumbnail
$has_thumbnail = $this->field->options( 'show_thumbnails' ) ? ' has-thumbnails' : '';
$hide_selected = $this->field->options( 'hide_selected' ) ? ' hide-selected' : '';
$has_relation = $this->field->options( 'show_relation' ) ? ' has-relation' : '';

if ( $filter_boxes ) {
printf( $filter_boxes, 'available-search' );
}

echo '<ul class="retrieved connected' . $has_thumbnail . $hide_selected . '">';
echo '<ul class="retrieved connected' . $has_thumbnail . $hide_selected . $has_relation . '">';

// Loop through our posts as list items
$this->display_retrieved( $objects, $attached );
Expand Down Expand Up @@ -184,7 +194,7 @@ public function render( $field, $escaped_value, $object_id, $object_type, $field
printf( $filter_boxes, 'attached-search' );
}

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

// If we have any ids saved already, display them
$ids = $this->display_attached( $attached );
Expand Down Expand Up @@ -261,11 +271,11 @@ protected function display_attached( $attached ) {
if ( empty( $object ) ) {
continue;
}

$input = $this->get_object_input( $object, $id );
// Set our zebra stripes
$class = ++$count % 2 == 0 ? 'even' : 'odd';

$this->list_item( $object, $class, 'dashicons-minus' );
$this->list_item( $object, $class, 'dashicons-minus', $input );
$ids[ $id ] = $id;
}

Expand All @@ -283,17 +293,18 @@ protected function display_attached( $attached ) {
*
* @return void
*/
public function list_item( $object, $li_class, $icon_class = 'dashicons-plus' ) {
public function list_item( $object, $li_class, $icon_class = 'dashicons-plus', $input = '' ) {
// Build our list item
printf(
'<li data-id="%1$d" class="%2$s" target="_blank">%3$s<a title="' . __( 'Edit' ) . '" href="%4$s">%5$s</a>%6$s<span class="dashicons %7$s add-remove"></span></li>',
'<li data-id="%1$d" class="%2$s" target="_blank">%3$s<a title="' . __( 'Edit' ) . '" href="%4$s">%5$s</a>%6$s<span class="dashicons %7$s add-remove"></span>%8$s</li>',
$this->get_id( $object ),
$li_class,
$this->get_thumb( $object ),
$this->get_edit_link( $object ),
$this->get_title( $object ),
$this->get_object_label( $object ),
$icon_class
$icon_class,
$input
);
}

Expand Down Expand Up @@ -332,6 +343,27 @@ public function get_id( $object ) {
return $object->ID;
}

/**
* Get input for the object.
*
* @since 1.2.4
*
* @param mixed $object Post or User
*
* @return string The object edit link.
*/
public function get_object_input( $object, $id ) {
if ( $this->field->options( 'show_relation' ) ) {
$this->relation_key = $this->field->args['id'] . $this->meta_key_suffix;
$value = get_post_meta( get_the_ID(), $this->relation_key, true);
return '<input
type="text"
name="' . $this->relation_key . '['. $id .']"
class="' . $this->relation_key . '"
value="'. ( isset( $value[ $id ]) ? $value[ $id ] : '' ) .'" />';
}
}

/**
* Get title for the object.
*
Expand Down Expand Up @@ -610,5 +642,21 @@ public function maybe_callback( $query, $post_args ) {
}
}

public function attached_posts_relation_meta( $field_id, $updated, $action, $field ) {
if ( $field->args['type'] != 'custom_attached_posts' && !isset( $field->args['options']['show_relation'] ) ) {
return;
}
if ( !$field->args['options']['show_relation'] ) {
return;
}
$this->relation_key = $field->args['id'] . $this->meta_key_suffix;
$labels = $field->data_to_save[ $this->relation_key ];
if ( !empty($labels) ) {
update_post_meta( $field->object_id, $this->relation_key, $labels );
}else{
delete_post_meta( $field->object_id, $this->relation_key );
}
}

}
WDS_CMB2_Attached_Posts_Field::get_instance();
10 changes: 8 additions & 2 deletions js/attached-posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ window.CMBAP = window.CMBAP || {};
app.moveRowToAttached = function( $li ) {
var itemID = $li.data( 'id' );
var $wrap = $li.parents( '.attached-posts-wrap' );
var fieldname = $wrap.data( 'fieldname' );

if ( $li.hasClass( 'added' ) ) {
return;
Expand All @@ -100,8 +101,13 @@ window.CMBAP = window.CMBAP || {};
$li.addClass( 'added' );

// Add the item to the right list
$wrap.find( '.attached' ).append( $li.clone() );

$wrap.find( '.attached' ).append( function(){
return $( this ).closest( 'ul' ).hasClass( 'has-relation' ) ? $li.clone().append( $( '<input>' ).attr({
type: "text",
name: fieldname + '_relation['+ itemID + ']',
class: fieldname + '_relation"'
}) ) : $li.clone();
});
app.resetAttachedListItems( $wrap );
};

Expand Down