Skip to content

Commit 0ba4c2e

Browse files
committed
Add post type label next to post title when multiple post-types are specified in the query args. Closes #34
1 parent 30a77ae commit 0ba4c2e

File tree

4 files changed

+51
-14
lines changed

4 files changed

+51
-14
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Once you have the post data for the post ID, you can proceed with the desired fu
4040

4141
## Changelog
4242

43+
### 1.2.6
44+
* Add post type label next to post title when multiple post-types are specified in the query args.
45+
4346
### 1.2.5
4447
* Combined the best bits from the [CMB2 Post Search field](https://github.com/WebDevStudios/CMB2-Post-Search-field) type and this field type, so now you can search for additional posts/pages/etc to be attached. User search is not currently supported. ([#7](https://github.com/WebDevStudios/cmb2-attached-posts/pull/7)).
4548

cmb2-attached-posts-field.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: CMB2 Field Type: Attached Posts
44
* Plugin URI: https://github.com/WebDevStudios/cmb2-attached-posts
55
* Description: Attached posts field type for CMB2.
6-
* Version: 1.2.5
6+
* Version: 1.2.6
77
* Author: WebDevStudios
88
* Author URI: http://webdevstudios.com
99
* License: GPLv2+
@@ -19,7 +19,7 @@
1919
* @author WebDevStudios <[email protected]>
2020
* @copyright 2016 WebDevStudios <[email protected]>
2121
* @license GPL-2.0+
22-
* @version 1.2.5
22+
* @version 1.2.6
2323
* @link https://github.com/WebDevStudios/cmb2-attached-posts
2424
* @since 1.2.3
2525
*/
@@ -46,7 +46,7 @@
4646
* Loader versioning: http://jtsternberg.github.io/wp-lib-loader/
4747
*/
4848

49-
if ( ! class_exists( 'WDS_CMB2_Attached_Posts_Field_125', false ) ) {
49+
if ( ! class_exists( 'WDS_CMB2_Attached_Posts_Field_126', false ) ) {
5050

5151
/**
5252
* Versioned loader class-name
@@ -57,18 +57,18 @@
5757
* @package WDS_CMB2_Attached_Posts_Field
5858
* @author WebDevStudios <[email protected]>
5959
* @license GPL-2.0+
60-
* @version 1.2.5
60+
* @version 1.2.6
6161
* @link https://github.com/WebDevStudios/cmb2-attached-posts
6262
* @since 1.2.3
6363
*/
64-
class WDS_CMB2_Attached_Posts_Field_125 {
64+
class WDS_CMB2_Attached_Posts_Field_126 {
6565

6666
/**
6767
* WDS_CMB2_Attached_Posts_Field version number
6868
* @var string
6969
* @since 1.2.3
7070
*/
71-
const VERSION = '1.2.5';
71+
const VERSION = '1.2.6';
7272

7373
/**
7474
* Current version hook priority.
@@ -77,7 +77,7 @@ class WDS_CMB2_Attached_Posts_Field_125 {
7777
* @var int
7878
* @since 1.2.3
7979
*/
80-
const PRIORITY = 9997;
80+
const PRIORITY = 9996;
8181

8282
/**
8383
* Starts the version checking process.
@@ -155,5 +155,5 @@ public function include_lib() {
155155
}
156156

157157
// Kick it off.
158-
new WDS_CMB2_Attached_Posts_Field_125;
158+
new WDS_CMB2_Attached_Posts_Field_126;
159159
}

init.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ class WDS_CMB2_Attached_Posts_Field {
2121
*/
2222
protected $field;
2323

24+
/**
25+
* Whether to output the type label.
26+
* Determined when multiple post types exist in the query_args field arg.
27+
*
28+
* @var bool
29+
*/
30+
protected $do_type_label = false;
31+
2432
/**
2533
* Creates or returns an instance of this class.
2634
* @since 0.1.0
@@ -51,6 +59,7 @@ protected function __construct() {
5159
public function render( $field, $escaped_value, $object_id, $object_type, $field_type ) {
5260
self::setup_scripts();
5361
$this->field = $field;
62+
$this->do_type_label = false;
5463

5564
if ( ! is_admin() ) {
5665
// Will need custom styling!
@@ -95,6 +104,9 @@ public function render( $field, $escaped_value, $object_id, $object_type, $field
95104

96105
$post_type_labels[] = $post_type_obj->labels->name;
97106
}
107+
108+
$this->do_type_label = count( $post_type_labels ) > 1;
109+
98110
$post_type_labels = implode( '/', $post_type_labels );
99111

100112
} else {
@@ -274,12 +286,13 @@ protected function display_attached( $attached ) {
274286
public function list_item( $object, $li_class, $icon_class = 'dashicons-plus' ) {
275287
// Build our list item
276288
printf(
277-
'<li data-id="%1$d" class="%2$s">%3$s<a title="' . __( 'Edit' ) . '" href="%4$s">%5$s</a><span class="dashicons %6$s add-remove"></span></li>',
289+
'<li data-id="%1$d" class="%2$s">%3$s<a title="' . __( 'Edit' ) . '" href="%4$s">%5$s</a>%6$s<span class="dashicons %7$s add-remove"></span></li>',
278290
$this->get_id( $object ),
279291
$li_class,
280292
$this->get_thumb( $object ),
281293
$this->get_edit_link( $object ),
282294
$this->get_title( $object ),
295+
$this->get_object_label( $object ),
283296
$icon_class
284297
);
285298
}
@@ -334,6 +347,26 @@ public function get_title( $object ) {
334347
: get_the_title( $object );
335348
}
336349

350+
/**
351+
* Get object label.
352+
*
353+
* @since 1.2.6
354+
*
355+
* @param mixed $object Post or User
356+
*
357+
* @return string The object label.
358+
*/
359+
public function get_object_label( $object ) {
360+
if ( ! $this->do_type_label ) {
361+
return '';
362+
}
363+
364+
$post_type_obj = get_post_type_object( $object->post_type );
365+
$label = isset( $post_type_obj->labels->singular_name ) ? $post_type_obj->labels->singular_name : $post_type_obj->label;
366+
367+
return ' &mdash; <span class="object-label">'. $label .'</span>';
368+
}
369+
337370
/**
338371
* Get edit link for the object.
339372
*

js/attached-posts.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ window.CMBAP = window.CMBAP || {};
1111
var $wrap = $( '.attached-posts-wrap' );
1212
app.$.retrievedPosts = $wrap.find( '.retrieved' );
1313
app.$.attachedPosts = $wrap.find( '.attached' );
14+
app.doType = $wrap.find( '.object-label' ).length;
1415
};
1516

1617
app.init = function() {
@@ -189,7 +190,8 @@ window.CMBAP = window.CMBAP || {};
189190
};
190191

191192
app.rowTmpl = function( row ) {
192-
return '<li data-id="'+ row.id +'" class="'+ row.class +' ui-draggable ui-draggable-handle"><a title="'+ app.editTitle +'" href="'+ app.edit_link_template.replace( 'REPLACEME', row.id ) +'">'+ row.title +'</a><span class="dashicons dashicons-plus add-remove"></span></li>';
193+
row.type = app.doType ? ' &mdash; <span class="object-label">'+ row.type +'</span>' : '';
194+
return '<li data-id="'+ row.id +'" class="'+ row.class +' ui-draggable ui-draggable-handle"><a title="'+ app.editTitle +'" href="'+ app.edit_link_template.replace( 'REPLACEME', row.id ) +'">'+ row.title +'</a>'+ row.type +'<span class="dashicons dashicons-plus add-remove"></span></li>';
193195
};
194196

195197
app.$retrievedPosts = function() {
@@ -297,12 +299,10 @@ window.CMBAP = window.CMBAP || {};
297299
},
298300

299301
hideSpinner: function() {
300-
console.warn('hideSpinner');
301302
this.$spinner.removeClass( 'is-active' );
302303
},
303304

304305
ajaxSuccess: function( response ) {
305-
console.warn('ajaxSuccess');
306306
if ( ! response.success ) {
307307
this.$response.text( this.errortxt );
308308
}
@@ -313,7 +313,6 @@ window.CMBAP = window.CMBAP || {};
313313
},
314314

315315
ajaxFail: function( response ) {
316-
console.warn('ajaxFail');
317316
this.$response.text( this.errortxt );
318317
},
319318

@@ -336,8 +335,10 @@ window.CMBAP = window.CMBAP || {};
336335
$checked.each( function() {
337336
ids.push( this.value );
338337

338+
var $row = $( this ).parents( '.found-posts' );
339339
html += app.rowTmpl( {
340-
title : $( this ).parents( '.found-posts' ).find( 'label' ).html(),
340+
title : $row.find( 'label' ).html(),
341+
type : $row.find( '> td' ).eq( 2 ).text(),
341342
id : this.value,
342343
class : nextClass
343344
} );

0 commit comments

Comments
 (0)