Post Type Switcher - Version 1.1

Version Description

Download this release

Release Info

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

Code changes from version 0.3 to 1.1

Files changed (2) hide show
  1. post-type-switcher.php +205 -159
  2. readme.txt +21 -11
post-type-switcher.php CHANGED
@@ -1,159 +1,205 @@
1
- <?php
2
- /*
3
- Plugin Name: Post Type Switcher
4
- Plugin URI: http://wordpress.org/extend/post-type-switcher/
5
- Description: Allow switching of a post type in post publish area.
6
- Author: John James Jacoby
7
- Version: 0.3
8
- Author URI: http://johnjamesjacoby.com
9
- */
10
-
11
- /**
12
- * pts_metabox()
13
- *
14
- * Adds post_publish metabox to allow changing post_type
15
- *
16
- * @global object $post Current post
17
- */
18
- function pts_metabox() {
19
- global $post, $pagenow;
20
-
21
- // Only show switcher when editing
22
- if ( $pagenow == 'post-new.php' )
23
- return;
24
-
25
- // Disallows things like attachments, revisions, etc...
26
- $safe_filter = array( 'public' => true, 'show_ui' => true );
27
-
28
- // Allow to be filtered, just incase you really need to switch between
29
- // those crazy types of posts
30
- $args = apply_filters( 'pts_metabox', $safe_filter );
31
-
32
- // Get the post types based on the above arguments
33
- $post_types = get_post_types( (array) $args, 'objects' );
34
-
35
- // Populate necessary post_type values
36
- $cur_post_type = $post->post_type;
37
- $cur_post_type_object = get_post_type_object( $cur_post_type );
38
-
39
- // Make sure the currently logged in user has the power
40
- $can_publish = current_user_can( $cur_post_type_object->cap->publish_posts );
41
- ?>
42
-
43
- <div class="misc-pub-section misc-pub-section-last post-type-switcher">
44
- <label for="pts_post_type"><?php _e( 'Post Type:' ); ?></label>
45
- <span id="post-type-display"><?php echo $cur_post_type_object->labels->singular_name; ?></span>
46
-
47
- <?php if ( !empty( $can_publish ) ) : ?>
48
-
49
- <a href="#" id="edit-post-type-switcher" class="hide-if-no-js"><?php _e( 'Edit' ); ?></a>
50
-
51
- <?php wp_nonce_field( 'post-type-selector', 'pts-nonce-select' ); ?>
52
-
53
- <div id="post-type-select">
54
- <select name="pts_post_type" id="pts_post_type">
55
-
56
- <?php
57
- foreach ( $post_types as $post_type => $pt ) {
58
- if ( ! current_user_can( $pt->cap->publish_posts ) )
59
- continue;
60
-
61
- echo '<option value="' . esc_attr( $pt->name ) . '"' . selected( $cur_post_type, $post_type, false ) . '>' . $pt->labels->singular_name . "</option>\n";
62
- }
63
- ?>
64
-
65
- </select>
66
- <a href="#" id="save-post-type-switcher" class="hide-if-no-js button"><?php _e( 'OK' ); ?></a>
67
- <a href="#" id="cancel-post-type-switcher" class="hide-if-no-js"><?php _e( 'Cancel' ); ?></a>
68
- </div>
69
- </div>
70
-
71
- <?php
72
- endif;
73
- }
74
- add_action( 'post_submitbox_misc_actions', 'pts_metabox' );
75
-
76
- function pts_save_post( $post_id, $post ) {
77
- global $pagenow;
78
-
79
- // Only show switcher when editing
80
- if ( $pagenow == 'post-new.php' )
81
- return;
82
-
83
- if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
84
- return;
85
-
86
- if ( ! isset( $_POST['pts-nonce-select'] ) )
87
- return;
88
-
89
- if ( ! wp_verify_nonce( $_POST['pts-nonce-select'], 'post-type-selector' ) )
90
- return;
91
-
92
- if ( ! current_user_can( 'edit_post', $post_id ) )
93
- return;
94
-
95
- if ( $_POST['pts_post_type'] == $post->post_type )
96
- return;
97
-
98
- if ( ! $new_post_type_object = get_post_type_object( $_POST['pts_post_type'] ) )
99
- return;
100
-
101
- if ( ! current_user_can( $new_post_type_object->cap->publish_posts ) )
102
- return;
103
-
104
- set_post_type( $post_id, $new_post_type_object->name );
105
- }
106
- add_action( 'save_post', 'pts_save_post', 10, 2 );
107
-
108
- /**
109
- * pts_head()
110
- *
111
- * Adds needed JS and CSS to admin header
112
- */
113
- function pts_head() {
114
- global $pagenow;
115
-
116
- // Only show switcher when editing
117
- if ( $pagenow == 'post-new.php' )
118
- return; ?>
119
-
120
- <script type='text/javascript'>
121
- jQuery(document).ready(function($){
122
- $('#edit-post-type-switcher').click( function() {
123
- $(this).hide();
124
- $('#post-type-select').slideDown();
125
- e.preventDefault();
126
- });
127
-
128
- $('#save-post-type-switcher').click( function() {
129
- $('#post-type-select').slideUp();
130
- $('#edit-post-type-switcher').show();
131
- $('#post-type-display').text( $('#pts_post_type :selected').text() );
132
- e.preventDefault();
133
- });
134
-
135
- $('#cancel-post-type-switcher').click( function() {
136
- $('#post-type-select').slideUp();
137
- $('#edit-post-type-switcher').show();
138
- e.preventDefault();
139
- });
140
- });
141
- </script>
142
- <style type="text/css">
143
- #post-type-select {
144
- line-height: 2.5em;
145
- margin-top: 3px;
146
- display: none;
147
- }
148
- #post-type-display {
149
- font-weight: bold;
150
- }
151
- div.post-type-switcher {
152
- border-top: 1px solid #eee;
153
- }
154
- </style>
155
- <?php
156
- }
157
- add_action( 'admin_head', 'pts_head' );
158
-
159
- ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * Post Type Switcher
5
+ *
6
+ * Allow switching of a post type while editing a post (in post publish section)
7
+ *
8
+ * @package PostTypeSwitcher
9
+ * @subpackage Main
10
+ */
11
+
12
+ /**
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
+ */
20
+
21
+ // Exit if accessed directly
22
+ if ( !defined( 'ABSPATH' ) ) exit;
23
+
24
+ /**
25
+ * The main post type switcher class
26
+ *
27
+ * @package PostTypeSwitcher
28
+ */
29
+ final class Post_Type_Switcher {
30
+
31
+ /**
32
+ * Setup the actions needed to execute class methods where needed
33
+ *
34
+ * @since PostTypeSwitcher (1.1)
35
+ */
36
+ public function __construct() {
37
+
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
+ /**
47
+ * pts_metabox()
48
+ *
49
+ * Adds post_publish metabox to allow changing post_type
50
+ *
51
+ * @since PostTypeSwitcher (0.3)
52
+ */
53
+ public function metabox() {
54
+
55
+ // Allow types to be filtered, just incase you really need to switch
56
+ // between crazy types of posts.
57
+ $args = (array) apply_filters( 'pts_post_type_filter', array(
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
+
74
+ <a href="#" id="edit-post-type-switcher" class="hide-if-no-js"><?php _e( 'Edit' ); ?></a>
75
+
76
+ <?php wp_nonce_field( 'post-type-selector', 'pts-nonce-select' ); ?>
77
+
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>
90
+ <a href="#" id="cancel-post-type-switcher" class="hide-if-no-js"><?php _e( 'Cancel' ); ?></a>
91
+ </div>
92
+
93
+ <?php endif; ?>
94
+
95
+ </div>
96
+
97
+ <?php
98
+ }
99
+
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
106
+ * @return If any number of condtions are met
107
+ */
108
+ function save_post( $post_id, $post ) {
109
+
110
+ if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
111
+ return;
112
+
113
+ if ( ! isset( $_POST['pts-nonce-select'] ) )
114
+ return;
115
+
116
+ if ( ! wp_verify_nonce( $_POST['pts-nonce-select'], 'post-type-selector' ) )
117
+ return;
118
+
119
+ if ( ! current_user_can( 'edit_post', $post_id ) )
120
+ return;
121
+
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'] ) )
129
+ return;
130
+
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
+
140
+ /**
141
+ * Adds needed JS and CSS to admin header
142
+ *
143
+ * @since PostTypeSwitcher (0.3)
144
+ * @return If on post-new.php
145
+ */
146
+ function admin_head() {
147
+ ?>
148
+
149
+ <script type="text/javascript">
150
+ jQuery( document ).ready( function( $ ) {
151
+ jQuery( '.misc-pub-section.curtime.misc-pub-section-last' ).removeClass( 'misc-pub-section-last' );
152
+ jQuery( '#edit-post-type-switcher' ).click( function(e) {
153
+ jQuery( this ).hide();
154
+ jQuery( '#post-type-select' ).slideDown();
155
+ e.preventDefault();
156
+ });
157
+
158
+ jQuery( '#save-post-type-switcher' ).click( function(e) {
159
+ jQuery( '#post-type-select' ).slideUp();
160
+ jQuery( '#edit-post-type-switcher' ).show();
161
+ jQuery( '#post-type-display' ).text( jQuery( '#pts_post_type :selected' ).text() );
162
+ e.preventDefault();
163
+ });
164
+
165
+ jQuery( '#cancel-post-type-switcher' ).click( function(e) {
166
+ jQuery( '#post-type-select' ).slideUp();
167
+ jQuery( '#edit-post-type-switcher' ).show();
168
+ e.preventDefault();
169
+ });
170
+ });
171
+ </script>
172
+ <style type="text/css">
173
+ #post-type-select {
174
+ line-height: 2.5em;
175
+ margin-top: 3px;
176
+ display: none;
177
+ }
178
+ #post-type-display {
179
+ font-weight: bold;
180
+ }
181
+ </style>
182
+
183
+ <?php
184
+ }
185
+
186
+ /**
187
+ * Whether or not the current file requires the post type switcher
188
+ *
189
+ * @since PostTypeSwitcher (1.1)
190
+ * @return bool True if it should load, false if not
191
+ */
192
+ private static function is_allowed_page() {
193
+ global $pagenow;
194
+
195
+ $pages = apply_filters( 'pts_allowed_pages', array(
196
+ 'post.php'
197
+ ) );
198
+
199
+ // Only show switcher when editing
200
+ return (bool) in_array( $pagenow, $pages );
201
+ }
202
+ }
203
+ new Post_Type_Switcher();
204
+
205
+ ?>
readme.txt CHANGED
@@ -2,24 +2,34 @@
2
  Contributors: johnjamesjacoby
3
  Tags: post type
4
  Requires at least: 3.0
5
- Tested up to: 3.0
6
- Stable tag: 0.2
7
 
8
  A simple way to change a post type in WordPress.
9
 
10
  == Description ==
11
 
12
- A simple way to change a post type in WordPress.
13
 
14
- Any combination is possible, even custom post types.
 
 
 
15
 
16
- Page to Post
17
- Post to Page
18
- Page to Attachment
19
- Post to Custom
20
 
21
  == Changelog ==
22
 
 
 
 
 
 
 
 
 
 
 
23
  = Version 0.3 =
24
  * Use the API to change the post type, fixing a conflict with persistent object caches
25
  * No longer requires JavaScript
@@ -32,14 +42,14 @@ Post to Custom
32
 
33
  == Installation ==
34
 
35
- * Install the plugin into the plugins/post-type-swticher directory, and activate!
36
  * From the post edit screen, above the "Publish" button is the "Post Type" interface.
37
  * Change post types as needed.
38
 
39
  == Frequently Asked Questions ==
40
 
41
  = Why would I need this? =
42
- I needed it to move WordPress posts into a custom post type of my own, so this plugin was borned!
43
 
44
  = Does this ruin my taxonomy associations? =
45
- It shouldn't. The only thing this plugin does for version 0.1 is change the 'post_type' property of the post.
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
 
10
  == Description ==
11
 
12
+ Any combination is possible, even custom post types:
13
 
14
+ * Page to Post
15
+ * Post to Page
16
+ * Page to Attachment
17
+ * Post to Custom
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 =
24
+ * Fix revisions being nooped
25
+ * Fix malformed HTML for some user roles
26
+ * Classificationate
27
+
28
+ = Version 1.0 =
29
+ * Fix JS bugs
30
+ * Audit post save bail conditions
31
+ * Tweak UI for WordPress 3.3
32
+
33
  = Version 0.3 =
34
  * Use the API to change the post type, fixing a conflict with persistent object caches
35
  * No longer requires JavaScript
42
 
43
  == Installation ==
44
 
45
+ * Install the plugin into the plugins/post-type-swticher directory, and activate.
46
  * From the post edit screen, above the "Publish" button is the "Post Type" interface.
47
  * Change post types as needed.
48
 
49
  == Frequently Asked Questions ==
50
 
51
  = Why would I need this? =
52
+ You need to selectively change a posts type from one to another.
53
 
54
  = Does this ruin my taxonomy associations? =
55
+ It should not. This plugin only changes the 'post_type' property of a post.