diff --git a/css/attached-posts-admin.css b/css/attached-posts-admin.css
index 7e2f634..4c9146f 100644
--- a/css/attached-posts-admin.css
+++ b/css/attached-posts-admin.css
@@ -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%;
+}
diff --git a/example-field-setup.php b/example-field-setup.php
index de3d3f1..79d2e24 100644
--- a/example-field-setup.php
+++ b/example-field-setup.php
@@ -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,
diff --git a/init.php b/init.php
index df6dbab..391e53f 100644
--- a/init.php
+++ b/init.php
@@ -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;
@@ -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
@@ -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' ) );
@@ -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 '
';
+ echo '';
// Loop through our posts as list items
$this->display_retrieved( $objects, $attached );
@@ -184,7 +194,7 @@ public function render( $field, $escaped_value, $object_id, $object_type, $field
printf( $filter_boxes, 'attached-search' );
}
- echo '';
+ echo '';
// If we have any ids saved already, display them
$ids = $this->display_attached( $attached );
@@ -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;
}
@@ -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(
- '- %3$s%5$s%6$s
',
+ '- %3$s%5$s%6$s%8$s
',
$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
);
}
@@ -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 '';
+ }
+ }
+
/**
* Get title for the object.
*
@@ -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();
diff --git a/js/attached-posts.js b/js/attached-posts.js
index 02e4b16..77840dd 100755
--- a/js/attached-posts.js
+++ b/js/attached-posts.js
@@ -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;
@@ -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( $( '' ).attr({
+ type: "text",
+ name: fieldname + '_relation['+ itemID + ']',
+ class: fieldname + '_relation"'
+ }) ) : $li.clone();
+ });
app.resetAttachedListItems( $wrap );
};