Skip to content

Commit

Permalink
Fix all of the bugs.
Browse files Browse the repository at this point in the history
Bump to 3.0.0.
  • Loading branch information
JJJ committed Sep 7, 2016
1 parent f04c88b commit ec7638a
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 56 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).


## 3.0.0
* Improved Quick-Edit and Bulk-Edit support
* Remove `attachment` type support for now, as there is no way to switch back
* Fix bug causing some post-types to switch unexpectedly

## 2.0.1
* Ensure quick-edit works with new procedure
* Quick-edit "Type" column works again!
Expand Down
28 changes: 20 additions & 8 deletions assets/js/quickedit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@ function pts_quick_edit() {
var $ = jQuery;
var _edit = inlineEditPost.edit;

$( '#bulk-edit' )
.find( '.inline-edit-col-right .inline-edit-col' )
.append(
$('#bulk-edit #pts_bulk_edit' )
);

$( '.inline-edit-row' ).not( '#bulk-edit' )
.find( '.inline-edit-col-right .inline-edit-col' )
.append(
$( '.inline-edit-row #pts_quick_edit' )
);

inlineEditPost.edit = function( id ) {

var args = [].slice.call(arguments );
var args = [].slice.call( arguments );

_edit.apply( this, args );

Expand All @@ -16,17 +28,17 @@ function pts_quick_edit() {

var

// editRow is the quick-edit row, containing the inputs that need to be updated
editRow = $( '#edit-' + id ),
// edit_row is the quick-edit row, containing the inputs that need to be updated
edit_row = $( '#edit-' + id ),

// postRow is the row shown when a book isn't being edited, which also holds the existing values.
postRow = $( '#post-' + id ),
// post_row is the row shown when a book isn't being edited, which also holds the existing values.
post_row = $( '#post-' + id ),

// get the existing values
post_type = $( '.post_type', postRow ).data( 'post-type' );
post_type = $( 'td.post_type span', post_row ).data( 'post-type' );

// set the values in the quick-editor
$( 'select[name="pts_post_type"] option[value="' + post_type + '"]', editRow ).attr( 'selected', 'selected' );
$( 'select[name="pts_post_type"] option[value="' + post_type + '"]', edit_row ).attr( 'selected', 'selected' );
};
}

Expand Down
115 changes: 92 additions & 23 deletions post-type-switcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Description: Allow switching of a post type while editing a post (in post publish section)
* Version: 2.0.1
* Version: 3.0.0
* Text Domain: post-type-switcher
* Domain Path: /assets/lang/
*/
Expand All @@ -31,6 +31,15 @@
*/
final class Post_Type_Switcher {

/**
* Asset version, for cache busting
*
* @since 3.0.1
*
* @var int
*/
private $asset_version = 201609170001;

/**
* Hook in the basic early actions
*
Expand Down Expand Up @@ -71,11 +80,11 @@ public function admin_init() {
add_action( 'manage_pages_custom_column', array( $this, 'manage_column' ), 10, 2 );

// Add UI to "Publish" metabox
add_action( 'admin_head', array( $this, 'admin_head' ) );
add_action( 'post_submitbox_misc_actions', array( $this, 'metabox' ) );
add_action( 'quick_edit_custom_box', array( $this, 'quickedit' ), 10, 2 );
add_action( 'bulk_edit_custom_box', array( $this, 'quickedit' ), 10, 2 );
add_action( 'admin_enqueue_scripts', array( $this, 'quickedit_script' ), 10, 1 );
add_action( 'admin_head', array( $this, 'admin_head' ) );
add_action( 'post_submitbox_misc_actions', array( $this, 'metabox' ) );
add_action( 'quick_edit_custom_box', array( $this, 'quick_edit' ) );
add_action( 'bulk_edit_custom_box', array( $this, 'quick_edit_bulk' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'quick_edit_script' ) );

// Override
add_filter( 'wp_insert_attachment_data', array( $this, 'override_type' ), 10, 2 );
Expand Down Expand Up @@ -108,9 +117,14 @@ public function metabox() {
// https://wordpress.org/support/topic/dont-show-for-non-public-post-types?replies=4#post-5849287
if ( ! in_array( $cpt_object, $post_types, true ) ) {
$post_types[ $post_type ] = $cpt_object;
} ?>
}

<div class="misc-pub-section misc-pub-section-last post-type-switcher">
// Unset attachment types, since support seems to be broken
if ( isset( $post_types['attachment'] ) ) {
unset( $post_types['attachment'] );
}

?><div class="misc-pub-section misc-pub-section-last post-type-switcher">
<label for="pts_post_type"><?php esc_html_e( 'Post Type:', 'post-type-switcher' ); ?></label>
<span id="post-type-display"><?php echo esc_html( $cpt_object->labels->singular_name ); ?></span>

Expand Down Expand Up @@ -175,25 +189,47 @@ public function manage_column( $column, $post_id ) {
*
* @since 1.2.0
*/
public function quickedit( $column_name, $post_type ) {
public function quick_edit( $column_name = '' ) {

// Bail to prevent multiple dropdowns in each column
if ( 'post_type' !== $column_name ) {
return;
} ?>

<div id="pts_quick_edit" class="inline-edit-group wp-clearfix">
<label class="alignleft">
<span class="title"><?php esc_html_e( 'Post Type', 'post-type-switcher' ); ?></span><?php

wp_nonce_field( 'post-type-selector', 'pts-nonce-select' );

$this->select_box();

?></label>
</div>

<?php
}

/**
* Adds quickedit button for bulk-editing post types
*
* @since 1.2.0
*/
public function quick_edit_bulk( $column_name = '' ) {

// Bail to prevent multiple dropdowns in each column
if ( 'post_type' !== $column_name ) {
return;
} ?>

<fieldset class="inline-edit-col-right">
<div class="inline-edit-col">
<label class="alignleft">
<span class="title"><?php esc_html_e( 'Post Type', 'post-type-switcher' ); ?></span><?php
<label id="pts_bulk_edit" class="alignleft">
<span class="title"><?php esc_html_e( 'Post Type', 'post-type-switcher' ); ?></span><?php

wp_nonce_field( 'post-type-selector', 'pts-nonce-select' );
wp_nonce_field( 'post-type-selector', 'pts-nonce-select' );

$this->select_box();
$this->select_box( true );

?></label>
</div>
</fieldset>
?></label>

<?php
}
Expand All @@ -203,38 +239,68 @@ public function quickedit( $column_name, $post_type ) {
*
* @since 1.2
*/
public function quickedit_script( $hook = '' ) {
public function quick_edit_script( $hook = '' ) {

// Bail if not edit.php admin page
if ( 'edit.php' !== $hook ) {
return;
}

wp_enqueue_script( 'pts_quickedit', plugin_dir_url( __FILE__ ) . 'assets/js/quickedit.js', array( 'jquery' ), '', true );
// Enqueue quick edit JS
wp_enqueue_script( 'pts_quickedit', plugin_dir_url( __FILE__ ) . 'assets/js/quickedit.js', array( 'jquery' ), $this->asset_version, true );
}

/**
* Output a post-type dropdown
*
* @since 1.2
*/
public function select_box() {
$post_types = get_post_types( $this->get_post_type_args(), 'objects' );
public function select_box( $bulk = false ) {

// Get post type specific data
$args = $this->get_post_type_args();
$post_types = get_post_types( $args, 'objects' );
$post_type = get_post_type();
$selected = '';

// Unset attachment types, since support seems to be broken
if ( isset( $post_types['attachment'] ) ) {
unset( $post_types['attachment'] );
}

// Start an output buffer
ob_start();

// Output
?><select name="pts_post_type" id="pts_post_type"><?php

// Maybe include "No Change" option for bulk
if ( true === $bulk ) :
?><option value="-1"><?php esc_html_e( '&mdash; No Change &mdash;', 'post-type-switcher' ); ?></option><?php
endif;

// Loop through post types
foreach ( $post_types as $_post_type => $pt ) :

// Skip if user cannot publish this type of post
if ( ! current_user_can( $pt->cap->publish_posts ) ) :
continue;
endif;

?><option value="<?php echo esc_attr( $pt->name ); ?>" <?php selected( $post_type, $_post_type ); ?>><?php echo esc_html( $pt->labels->singular_name ); ?></option><?php
// Only select if not bulk
if ( false === $bulk ) :
$selected = selected( $post_type, $_post_type );
endif;

// Output option
?><option value="<?php echo esc_attr( $pt->name ); ?>" <?php echo $selected; // Do not escape ?>><?php echo esc_html( $pt->labels->singular_name ); ?></option><?php

endforeach;

?></select><?php

// Output the current buffer
echo ob_get_clean();
}

/**
Expand Down Expand Up @@ -343,6 +409,9 @@ public function admin_head() {
});
</script>
<style type="text/css">
#wpbody-content .inline-edit-row .inline-edit-col-right .alignleft + .alignleft {
float: right;
}
#post-type-select {
line-height: 2.5em;
margin-top: 3px;
Expand Down
Loading

0 comments on commit ec7638a

Please sign in to comment.