Post Type Switcher - Version 1.2

Version Description

Download this release

Release Info

Developer johnjamesjacoby
Plugin Icon 128x128 Post Type Switcher
Version 1.2
Comparing to
See all releases

Code changes from version 1.1.1 to 1.2

Files changed (3) hide show
  1. js/quickedit.js +37 -0
  2. post-type-switcher.php +109 -14
  3. readme.txt +10 -4
js/quickedit.js ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function pts_quick_edit() {
2
+
3
+ var $ = jQuery;
4
+ var _edit = inlineEditPost.edit;
5
+
6
+ inlineEditPost.edit = function( id ) {
7
+
8
+ var args = [].slice.call(arguments );
9
+
10
+ _edit.apply( this, args );
11
+
12
+ if ( typeof( id ) == 'object' ) {
13
+ id = this.getId( id );
14
+ }
15
+
16
+ var
17
+
18
+ // editRow is the quick-edit row, containing the inputs that need to be updated
19
+ editRow = $( '#edit-' + id ),
20
+
21
+ // postRow is the row shown when a book isn't being edited, which also holds the existing values.
22
+ postRow = $( '#post-' + id ),
23
+
24
+ // get the existing values
25
+ post_type = $( '.post_type', postRow ).data( 'post-type' );
26
+
27
+ // set the values in the quick-editor
28
+ $( 'select[name="pts_post_type"] option[value="' + post_type + '"]', editRow ).attr( 'selected', 'selected' );
29
+ };
30
+ }
31
+
32
+ // Another way of ensuring inlineEditPost.edit isn't patched until it's defined
33
+ if ( inlineEditPost ) {
34
+ pts_quick_edit();
35
+ } else {
36
+ jQuery( pts_quick_edit );
37
+ }
post-type-switcher.php CHANGED
@@ -13,7 +13,7 @@
13
  * Plugin Name: Post Type Switcher
14
  * Plugin URI: http://wordpress.org/extend/post-type-switcher/
15
  * Description: Allow switching of a post type while editing a post (in post publish section)
16
- * Version: 1.1.1
17
  * Author: johnjamesjacoby
18
  * Author URI: http://johnjamesjacoby.com
19
  */
@@ -38,9 +38,18 @@ final class Post_Type_Switcher {
38
  if ( ! $this->is_allowed_page() )
39
  return;
40
 
41
- add_action( 'post_submitbox_misc_actions', array( $this, 'metabox' ) );
42
- add_action( 'save_post', array( $this, 'save_post' ), 999, 2 ); // Late priority for plugin friendliness
43
- add_action( 'admin_head', array( $this, 'admin_head' ) );
 
 
 
 
 
 
 
 
 
44
  }
45
 
46
  /**
@@ -97,13 +106,97 @@ final class Post_Type_Switcher {
97
 
98
  <?php
99
  }
100
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  /**
102
  * Set the post type on save_post but only when editing
103
  *
104
  * We do a bunch of sanity checks here, to make sure we're only changing the
105
  * post type when the user explicitly intends to.
106
- *
107
  * - Not during autosave
108
  * - Check nonce
109
  * - Check user capabilities
@@ -119,26 +212,28 @@ final class Post_Type_Switcher {
119
  */
120
  function save_post( $post_id, $post ) {
121
 
122
- if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
 
123
  return;
 
124
 
125
- if ( ! isset( $_POST['pts-nonce-select'] ) )
126
  return;
127
 
128
- if ( ! wp_verify_nonce( $_POST['pts-nonce-select'], 'post-type-selector' ) )
129
  return;
130
 
131
  if ( ! current_user_can( 'edit_post', $post_id ) )
132
  return;
133
 
134
- if ( empty( $_POST['pts_post_type'] ) )
135
  return;
136
 
137
- if ( in_array( $post->post_type, array( $_POST['pts_post_type'], 'revision' ) ) )
138
  return;
139
 
140
- if ( ! $new_post_type_object = get_post_type_object( $_POST['pts_post_type'] ) )
141
- return;
142
 
143
  if ( ! current_user_can( $new_post_type_object->cap->publish_posts ) )
144
  return;
@@ -207,7 +302,7 @@ final class Post_Type_Switcher {
207
 
208
  // Allowed admin pages
209
  $pages = apply_filters( 'pts_allowed_pages', array(
210
- 'post.php'
211
  ) );
212
 
213
  // Only show switcher when editing
13
  * Plugin Name: Post Type Switcher
14
  * Plugin URI: http://wordpress.org/extend/post-type-switcher/
15
  * Description: Allow switching of a post type while editing a post (in post publish section)
16
+ * Version: 1.2
17
  * Author: johnjamesjacoby
18
  * Author URI: http://johnjamesjacoby.com
19
  */
38
  if ( ! $this->is_allowed_page() )
39
  return;
40
 
41
+ // @todo Remove this; since it's janky to need to do this.
42
+ add_action( 'manage_posts_columns', array( $this, 'add_column' ) );
43
+ add_action( 'manage_pages_columns', array( $this, 'add_column' ) );
44
+ add_action( 'manage_posts_custom_column', array( $this, 'manage_column' ), 10, 2 );
45
+ add_action( 'manage_pages_custom_column', array( $this, 'manage_column' ), 10, 2 );
46
+
47
+ add_action( 'post_submitbox_misc_actions', array( $this, 'metabox' ) );
48
+ add_action( 'quick_edit_custom_box', array( $this, 'quickedit' ), 10, 2 );
49
+ add_action( 'bulk_edit_custom_box', array( $this, 'quickedit' ), 10, 2 );
50
+ add_action( 'admin_enqueue_scripts', array( $this, 'quickedit_script' ), 10, 1 );
51
+ add_action( 'save_post', array( $this, 'save_post' ), 999, 2 ); // Late priority for plugin friendliness
52
+ add_action( 'admin_head', array( $this, 'admin_head' ) );
53
  }
54
 
55
  /**
106
 
107
  <?php
108
  }
109
+
110
+ /**
111
+ * Adds the post type column
112
+ *
113
+ * @since PostTypeSwitcher (1.2)
114
+ */
115
+ public function add_column( $columns ) {
116
+ return array_merge( $columns, array( 'post_type' => __( 'Type' ) ) );
117
+ }
118
+
119
+ /**
120
+ * Manages the post type column
121
+ *
122
+ * @since PostTypeSwitcher (1.1.1)
123
+ */
124
+ public function manage_column( $column, $post_id ) {
125
+ switch( $column ) {
126
+ case 'post_type' :
127
+ $post_type = get_post_type_object( get_post_type( $post_id ) ); ?>
128
+
129
+ <span data-post-type="<?php echo esc_attr( $post_type->name ); ?>"><?php echo esc_html( $post_type->labels->singular_name ); ?></span>
130
+
131
+ <?php
132
+ break;
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Adds quickedit button for bulk-editing post types
138
+ *
139
+ * @since PostTypeSwitcher (1.2)
140
+ */
141
+ public function quickedit( $column_name, $post_type ) {
142
+ ?>
143
+ <fieldset class="inline-edit-col-right">
144
+ <div class="inline-edit-col">
145
+ <label class="alignleft">
146
+ <span class="title"><?php _e( 'Post Type' ); ?></span>
147
+ <?php wp_nonce_field( 'post-type-selector', 'pts-nonce-select' ); ?>
148
+ <?php $this->select_box(); ?>
149
+ </label>
150
+ </div>
151
+ </fieldset>
152
+ <?php
153
+ }
154
+
155
+ /**
156
+ * Adds quickedit script for getting values into quickedit box
157
+ *
158
+ * @since PostTypeSwitcher (1.2)
159
+ */
160
+ public function quickedit_script( $hook = '' ) {
161
+ if ( 'edit.php' != $hook )
162
+ return;
163
+
164
+ wp_enqueue_script( 'pts_quickedit', plugins_url( 'js/quickedit.js', __FILE__ ), array( 'jquery' ), '', true );
165
+ }
166
+
167
+ /**
168
+ * Output a post-type dropdown
169
+ *
170
+ * @since PostTypeSwitcher (1.2)
171
+ */
172
+ public function select_box() {
173
+ $args = (array) apply_filters( 'pts_post_type_filter', array(
174
+ 'public' => true,
175
+ 'show_ui' => true
176
+ ) );
177
+ $post_types = get_post_types( $args, 'objects' ); ?>
178
+
179
+ <select name="pts_post_type" id="pts_post_type">
180
+
181
+ <?php foreach ( $post_types as $post_type => $pt ) : ?>
182
+
183
+ <?php if ( ! current_user_can( $pt->cap->publish_posts ) ) continue; ?>
184
+
185
+ <option value="<?php echo esc_attr( $pt->name ); ?>" <?php selected( get_post_type(), $post_type ); ?>><?php echo esc_html( $pt->labels->singular_name ); ?></option>
186
+
187
+ <?php endforeach; ?>
188
+
189
+ </select>
190
+
191
+ <?php
192
+ }
193
+
194
  /**
195
  * Set the post type on save_post but only when editing
196
  *
197
  * We do a bunch of sanity checks here, to make sure we're only changing the
198
  * post type when the user explicitly intends to.
199
+ *
200
  * - Not during autosave
201
  * - Check nonce
202
  * - Check user capabilities
212
  */
213
  function save_post( $post_id, $post ) {
214
 
215
+ if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
216
+ die( 'autosave' );
217
  return;
218
+ }
219
 
220
+ if ( ! isset( $_REQUEST['pts-nonce-select'] ) )
221
  return;
222
 
223
+ if ( ! wp_verify_nonce( $_REQUEST['pts-nonce-select'], 'post-type-selector' ) )
224
  return;
225
 
226
  if ( ! current_user_can( 'edit_post', $post_id ) )
227
  return;
228
 
229
+ if ( empty( $_REQUEST['pts_post_type'] ) )
230
  return;
231
 
232
+ if ( in_array( $post->post_type, array( $_REQUEST['pts_post_type'], 'revision' ) ) )
233
  return;
234
 
235
+ if ( ! $new_post_type_object = get_post_type_object( $_REQUEST['pts_post_type'] ) )
236
+ return;
237
 
238
  if ( ! current_user_can( $new_post_type_object->cap->publish_posts ) )
239
  return;
302
 
303
  // Allowed admin pages
304
  $pages = apply_filters( 'pts_allowed_pages', array(
305
+ 'post.php', 'edit.php', 'admin-ajax.php'
306
  ) );
307
 
308
  // Only show switcher when editing
readme.txt CHANGED
@@ -1,11 +1,11 @@
1
  === Post Type Switcher ===
2
- Contributors: johnjamesjacoby
3
  Tags: post type
4
  Requires at least: 3.0
5
- Tested up to: 3.5
6
- Stable tag: 1.1.1
7
 
8
- A simple way to change a post type in WordPress.
9
 
10
  == Description ==
11
 
@@ -18,8 +18,14 @@ Any combination is possible, even custom post types:
18
 
19
  Note: Invisible post types (revisions, menus, etc...) are purposely excluded. Filter 'pts_post_type_filter' to adjust the boundaries.
20
 
 
 
21
  == Changelog ==
22
 
 
 
 
 
23
  = Version 1.1.1 =
24
  * Add is_admin() check to prevent theme-side interference
25
  * Change save_post priority to 999 to avoid plugin compatibility issues
1
  === Post Type Switcher ===
2
+ Contributors: johnjamesjacoby, beatpanda
3
  Tags: post type
4
  Requires at least: 3.0
5
+ Tested up to: 3.9
6
+ Stable tag: 1.2
7
 
8
+ A simple way to change a post's type in WordPress
9
 
10
  == Description ==
11
 
18
 
19
  Note: Invisible post types (revisions, menus, etc...) are purposely excluded. Filter 'pts_post_type_filter' to adjust the boundaries.
20
 
21
+ Now with bulk editing, thanks to Matthew Gerring!
22
+
23
  == Changelog ==
24
 
25
+ = Version 1.2 =
26
+ * Add bulk editing to supported post types
27
+ * Props Matthew Gerring for bulk edit contribution
28
+
29
  = Version 1.1.1 =
30
  * Add is_admin() check to prevent theme-side interference
31
  * Change save_post priority to 999 to avoid plugin compatibility issues