Post Type Switcher - Version 1.1.1

Version Description

Download this release

Release Info

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

Code changes from version 1.1 to 1.1.1

Files changed (2) hide show
  1. post-type-switcher.php +31 -19
  2. readme.txt +8 -2
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
17
  * Author: johnjamesjacoby
18
  * Author URI: http://johnjamesjacoby.com
19
  */
@@ -38,9 +38,9 @@ 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' ), 10, 2 );
43
- add_action( 'admin_head', array( $this, 'admin_head' ) );
44
  }
45
 
46
  /**
@@ -58,16 +58,16 @@ final class Post_Type_Switcher {
58
  'public' => true,
59
  'show_ui' => true
60
  ) );
61
- $post_types = get_post_types( $args, 'objects' );
62
- $cpt_object = get_post_type_object( get_post_type() );
63
 
64
- // Bail if object is dirty
65
  if ( empty( $cpt_object ) || is_wp_error( $cpt_object ) )
66
  return; ?>
67
 
68
  <div class="misc-pub-section misc-pub-section-last post-type-switcher">
69
  <label for="pts_post_type"><?php _e( 'Post Type:' ); ?></label>
70
- <span id="post-type-display"><?php echo $cpt_object->labels->singular_name; ?></span>
71
 
72
  <?php if ( current_user_can( $cpt_object->cap->publish_posts ) ) : ?>
73
 
@@ -78,12 +78,13 @@ final class Post_Type_Switcher {
78
  <div id="post-type-select">
79
  <select name="pts_post_type" id="pts_post_type">
80
 
81
- <?php foreach ( $post_types as $post_type => $pt ) :
82
- if ( ! current_user_can( $pt->cap->publish_posts ) )
83
- continue;
84
 
85
- echo '<option value="' . esc_attr( $pt->name ) . '"' . selected( get_post_type(), $post_type, false ) . '>' . $pt->labels->singular_name . "</option>\n";
86
- endforeach; ?>
 
 
 
87
 
88
  </select>
89
  <a href="#" id="save-post-type-switcher" class="hide-if-no-js button"><?php _e( 'OK' ); ?></a>
@@ -100,6 +101,17 @@ final class Post_Type_Switcher {
100
  /**
101
  * Set the post type on save_post but only when editing
102
  *
 
 
 
 
 
 
 
 
 
 
 
103
  * @since PostTypeSwitcher (0.3)
104
  * @param int $post_id
105
  * @param object $post
@@ -122,7 +134,7 @@ final class Post_Type_Switcher {
122
  if ( empty( $_POST['pts_post_type'] ) )
123
  return;
124
 
125
- if ( $_POST['pts_post_type'] == $post->post_type )
126
  return;
127
 
128
  if ( ! $new_post_type_object = get_post_type_object( $_POST['pts_post_type'] ) )
@@ -131,9 +143,6 @@ final class Post_Type_Switcher {
131
  if ( ! current_user_can( $new_post_type_object->cap->publish_posts ) )
132
  return;
133
 
134
- if ( 'revision' == $post->post_type )
135
- return;
136
-
137
  set_post_type( $post_id, $new_post_type_object->name );
138
  }
139
 
@@ -192,6 +201,11 @@ final class Post_Type_Switcher {
192
  private static function is_allowed_page() {
193
  global $pagenow;
194
 
 
 
 
 
 
195
  $pages = apply_filters( 'pts_allowed_pages', array(
196
  'post.php'
197
  ) );
@@ -201,5 +215,3 @@ final class Post_Type_Switcher {
201
  }
202
  }
203
  new Post_Type_Switcher();
204
-
205
- ?>
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
  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
  /**
58
  'public' => true,
59
  'show_ui' => true
60
  ) );
61
+ $post_types = get_post_types( $args, 'objects' );
62
+ $cpt_object = get_post_type_object( get_post_type() );
63
 
64
+ // Bail if object does not exist or produces an error
65
  if ( empty( $cpt_object ) || is_wp_error( $cpt_object ) )
66
  return; ?>
67
 
68
  <div class="misc-pub-section misc-pub-section-last post-type-switcher">
69
  <label for="pts_post_type"><?php _e( 'Post Type:' ); ?></label>
70
+ <span id="post-type-display"><?php echo esc_html( $cpt_object->labels->singular_name ); ?></span>
71
 
72
  <?php if ( current_user_can( $cpt_object->cap->publish_posts ) ) : ?>
73
 
78
  <div id="post-type-select">
79
  <select name="pts_post_type" id="pts_post_type">
80
 
81
+ <?php foreach ( $post_types as $post_type => $pt ) : ?>
 
 
82
 
83
+ <?php if ( ! current_user_can( $pt->cap->publish_posts ) ) continue; ?>
84
+
85
+ <option value="<?php echo esc_attr( $pt->name ); ?>" <?php selected( get_post_type(), $post_type ); ?>><?php echo esc_html( $pt->labels->singular_name ); ?></option>
86
+
87
+ <?php endforeach; ?>
88
 
89
  </select>
90
  <a href="#" id="save-post-type-switcher" class="hide-if-no-js button"><?php _e( 'OK' ); ?></a>
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
110
+ * - Check $_POST input name
111
+ * - Check if revision or current post-type
112
+ * - Check new post-type exists
113
+ * - Check that user can publish posts of new type
114
+ *
115
  * @since PostTypeSwitcher (0.3)
116
  * @param int $post_id
117
  * @param object $post
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'] ) )
143
  if ( ! current_user_can( $new_post_type_object->cap->publish_posts ) )
144
  return;
145
 
 
 
 
146
  set_post_type( $post_id, $new_post_type_object->name );
147
  }
148
 
201
  private static function is_allowed_page() {
202
  global $pagenow;
203
 
204
+ // Only for admin area
205
+ if ( ! is_admin() )
206
+ return false;
207
+
208
+ // Allowed admin pages
209
  $pages = apply_filters( 'pts_allowed_pages', array(
210
  'post.php'
211
  ) );
215
  }
216
  }
217
  new Post_Type_Switcher();
 
 
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: johnjamesjacoby
3
  Tags: post type
4
  Requires at least: 3.0
5
- Tested up to: 3.4
6
- Stable tag: 1.1
7
 
8
  A simple way to change a post type in WordPress.
9
 
@@ -20,6 +20,12 @@ Note: Invisible post types (revisions, menus, etc...) are purposely excluded. Fi
20
 
21
  == Changelog ==
22
 
 
 
 
 
 
 
23
  = Version 1.1 =
24
  * Fix revisions being nooped
25
  * Fix malformed HTML for some user roles
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
 
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
26
+ * Remove ending closing php tag
27
+ * HTML and PHPDoc improvements
28
+
29
  = Version 1.1 =
30
  * Fix revisions being nooped
31
  * Fix malformed HTML for some user roles