Skip to content

Commit

Permalink
Add post type label next to post title when multiple post-types are s…
Browse files Browse the repository at this point in the history
…pecified in the query args. Closes #34
  • Loading branch information
jtsternberg committed Jan 19, 2017
1 parent 30a77ae commit 0ba4c2e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -40,6 +40,9 @@ Once you have the post data for the post ID, you can proceed with the desired fu

## Changelog

### 1.2.6
* Add post type label next to post title when multiple post-types are specified in the query args.

### 1.2.5
* 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)).

Expand Down
16 changes: 8 additions & 8 deletions cmb2-attached-posts-field.php
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: CMB2 Field Type: Attached Posts
* Plugin URI: https://github.com/WebDevStudios/cmb2-attached-posts
* Description: Attached posts field type for CMB2.
* Version: 1.2.5
* Version: 1.2.6
* Author: WebDevStudios
* Author URI: http://webdevstudios.com
* License: GPLv2+
Expand All @@ -19,7 +19,7 @@
* @author WebDevStudios <contact@webdevstudios.com>
* @copyright 2016 WebDevStudios <contact@webdevstudios.com>
* @license GPL-2.0+
* @version 1.2.5
* @version 1.2.6
* @link https://github.com/WebDevStudios/cmb2-attached-posts
* @since 1.2.3
*/
Expand All @@ -46,7 +46,7 @@
* Loader versioning: http://jtsternberg.github.io/wp-lib-loader/
*/

if ( ! class_exists( 'WDS_CMB2_Attached_Posts_Field_125', false ) ) {
if ( ! class_exists( 'WDS_CMB2_Attached_Posts_Field_126', false ) ) {

/**
* Versioned loader class-name
Expand All @@ -57,18 +57,18 @@
* @package WDS_CMB2_Attached_Posts_Field
* @author WebDevStudios <contact@webdevstudios.com>
* @license GPL-2.0+
* @version 1.2.5
* @version 1.2.6
* @link https://github.com/WebDevStudios/cmb2-attached-posts
* @since 1.2.3
*/
class WDS_CMB2_Attached_Posts_Field_125 {
class WDS_CMB2_Attached_Posts_Field_126 {

/**
* WDS_CMB2_Attached_Posts_Field version number
* @var string
* @since 1.2.3
*/
const VERSION = '1.2.5';
const VERSION = '1.2.6';

/**
* Current version hook priority.
Expand All @@ -77,7 +77,7 @@ class WDS_CMB2_Attached_Posts_Field_125 {
* @var int
* @since 1.2.3
*/
const PRIORITY = 9997;
const PRIORITY = 9996;

/**
* Starts the version checking process.
Expand Down Expand Up @@ -155,5 +155,5 @@ public function include_lib() {
}

// Kick it off.
new WDS_CMB2_Attached_Posts_Field_125;
new WDS_CMB2_Attached_Posts_Field_126;
}
35 changes: 34 additions & 1 deletion init.php
Expand Up @@ -21,6 +21,14 @@ class WDS_CMB2_Attached_Posts_Field {
*/
protected $field;

/**
* Whether to output the type label.
* Determined when multiple post types exist in the query_args field arg.
*
* @var bool
*/
protected $do_type_label = false;

/**
* Creates or returns an instance of this class.
* @since 0.1.0
Expand Down Expand Up @@ -51,6 +59,7 @@ protected function __construct() {
public function render( $field, $escaped_value, $object_id, $object_type, $field_type ) {
self::setup_scripts();
$this->field = $field;
$this->do_type_label = false;

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

$post_type_labels[] = $post_type_obj->labels->name;
}

$this->do_type_label = count( $post_type_labels ) > 1;

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

} else {
Expand Down Expand Up @@ -274,12 +286,13 @@ protected function display_attached( $attached ) {
public function list_item( $object, $li_class, $icon_class = 'dashicons-plus' ) {
// Build our list item
printf(
'<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>',
'<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>',
$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
);
}
Expand Down Expand Up @@ -334,6 +347,26 @@ public function get_title( $object ) {
: get_the_title( $object );
}

/**
* Get object label.
*
* @since 1.2.6
*
* @param mixed $object Post or User
*
* @return string The object label.
*/
public function get_object_label( $object ) {
if ( ! $this->do_type_label ) {
return '';
}

$post_type_obj = get_post_type_object( $object->post_type );
$label = isset( $post_type_obj->labels->singular_name ) ? $post_type_obj->labels->singular_name : $post_type_obj->label;

return ' &mdash; <span class="object-label">'. $label .'</span>';
}

/**
* Get edit link for the object.
*
Expand Down
11 changes: 6 additions & 5 deletions js/attached-posts.js
Expand Up @@ -11,6 +11,7 @@ window.CMBAP = window.CMBAP || {};
var $wrap = $( '.attached-posts-wrap' );
app.$.retrievedPosts = $wrap.find( '.retrieved' );
app.$.attachedPosts = $wrap.find( '.attached' );
app.doType = $wrap.find( '.object-label' ).length;
};

app.init = function() {
Expand Down Expand Up @@ -189,7 +190,8 @@ window.CMBAP = window.CMBAP || {};
};

app.rowTmpl = function( row ) {
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>';
row.type = app.doType ? ' &mdash; <span class="object-label">'+ row.type +'</span>' : '';
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>';
};

app.$retrievedPosts = function() {
Expand Down Expand Up @@ -297,12 +299,10 @@ window.CMBAP = window.CMBAP || {};
},

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

ajaxSuccess: function( response ) {
console.warn('ajaxSuccess');
if ( ! response.success ) {
this.$response.text( this.errortxt );
}
Expand All @@ -313,7 +313,6 @@ window.CMBAP = window.CMBAP || {};
},

ajaxFail: function( response ) {
console.warn('ajaxFail');
this.$response.text( this.errortxt );
},

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

var $row = $( this ).parents( '.found-posts' );
html += app.rowTmpl( {
title : $( this ).parents( '.found-posts' ).find( 'label' ).html(),
title : $row.find( 'label' ).html(),
type : $row.find( '> td' ).eq( 2 ).text(),
id : this.value,
class : nextClass
} );
Expand Down

0 comments on commit 0ba4c2e

Please sign in to comment.