Post Type Switcher - Version 0.3

Version Description

Download this release

Release Info

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

Code changes from version 0.2 to 0.3

Files changed (2) hide show
  1. post-type-switcher.php +159 -123
  2. readme.txt +45 -41
post-type-switcher.php CHANGED
@@ -1,123 +1,159 @@
1
- <?php
2
- /*
3
- Plugin Name: Post Type Switcher
4
- Plugin URI: http://wordpress.org
5
- Description: Allow switching of post_type in post publish area
6
- Author: John James Jacoby
7
- Version: 0.2
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;
20
-
21
- // Disallows things like attachments, revisions, etc...
22
- $safe_filter = array( 'public' => true, 'show_ui' => true );
23
-
24
- // Allow to be filtered, just incase you really need to switch between
25
- // those crazy types of posts
26
- $args = apply_filters( 'pts_metabox', $safe_filter );
27
-
28
- // Get the post types based on the above arguments
29
- $post_types = get_post_types( (array)$args );
30
-
31
- // Populate necessary post_type values
32
- $cur_post_type = $post->post_type;
33
- $cur_post_type_object = get_post_type_object( $cur_post_type );
34
-
35
- // Make sure the currently logged in user has the power
36
- $can_publish = current_user_can( $cur_post_type_object->cap->publish_posts );
37
- ?>
38
-
39
- <div class="misc-pub-section misc-pub-section-last post-type-switcher">
40
- <label for="pts_post_type"><?php _e( 'Post Type:' ); ?></label>
41
- <span id="post-type-display"><?php echo $cur_post_type_object->label; ?></span>
42
- <?php if ( $can_publish ) : ?>
43
- <a href="#pts_post_type" class="edit-post-type hide-if-no-js"><?php _e( 'Edit' ); ?></a>
44
- <div id="post-type-select" class="hide-if-js">
45
- <select name="pts_post_type" id="pts_post_type">
46
- <?php
47
- foreach ( $post_types as $post_type ) {
48
- $pt = get_post_type_object( $post_type );
49
- if ( current_user_can( $pt->cap->publish_posts ) ) :
50
- ?>
51
- <option value="<?php echo $pt->name; ?>"<?php if ( $cur_post_type == $post_type ) : ?>selected="selected"<?php endif; ?>><?php echo $pt->label; ?></option>
52
- <?php
53
- endif;
54
- }
55
- ?>
56
- </select>
57
- <input type="hidden" name="hidden_post_type" id="hidden_post_type" value="<?php echo $cur_post_type; ?>" />
58
- <a href="#pts_post_type" class="save-post-type hide-if-no-js button"><?php _e( 'OK' ); ?></a>
59
- <a href="#pts_post_type" class="cancel-post-type hide-if-no-js"><?php _e( 'Cancel' ); ?></a>
60
- </div>
61
- </div>
62
- <?php
63
- endif;
64
- }
65
- add_action( 'post_submitbox_misc_actions', 'pts_metabox' );
66
-
67
- /**
68
- * pts_head()
69
- *
70
- * Adds needed JS and CSS to admin header
71
- */
72
- function pts_head() {
73
- ?>
74
- <script type='text/javascript'>
75
- jQuery(document).ready(function(){
76
- jQuery('#post-type-select').siblings('a.edit-post-type').click(function() {
77
- if (jQuery('#post-type-select').is(":hidden")) {
78
- jQuery('#post-type-select').slideDown("normal");
79
- jQuery(this).hide();
80
- }
81
- return false;
82
- });
83
-
84
- jQuery('.save-post-type', '#post-type-select').click(function() {
85
- jQuery('#post-type-select').slideUp("normal");
86
- jQuery('#post-type-select').siblings('a.edit-post-type').show();
87
- pts_updateText();
88
- return false;
89
- });
90
-
91
- jQuery('.cancel-post-type', '#post-type-select').click(function() {
92
- jQuery('#post-type-select').slideUp("normal");
93
- jQuery('#pts_post_type').val(jQuery('#hidden_post_type').val());
94
- jQuery('#post-type-select').siblings('a.edit-post-type').show();
95
- pts_updateText();
96
- return false;
97
- });
98
-
99
- function pts_updateText() {
100
- jQuery('#post-type-display').html( jQuery('#pts_post_type :selected').text() );
101
- jQuery('#hidden_post_type').val(jQuery('#pts_post_type').val());
102
- jQuery('#post_type').val(jQuery('#pts_post_type').val());
103
- return true;
104
- }
105
- });
106
- </script>
107
- <style type="text/css">
108
- #post-type-select {
109
- line-height: 2.5em;
110
- margin-top: 3px;
111
- }
112
- #post-type-display {
113
- font-weight: bold;
114
- }
115
- div.post-type-switcher {
116
- border-top: 1px solid #eee;
117
- }
118
- </style>
119
- <?php
120
- }
121
- add_action( 'admin_head', 'pts_head' );
122
-
123
- ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ ?>
readme.txt CHANGED
@@ -1,41 +1,45 @@
1
- === Post Type Switcher ===
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
- * Post to Custom
19
- * Post to revision/attachment/navigation menu is possible but turned off by default
20
-
21
- == Changelog ==
22
-
23
- = Version 0.2 =
24
- * Disallow post types that are not public and do not have a visible UI
25
-
26
- = Version 0.1 =
27
- * Release
28
-
29
- == Installation ==
30
-
31
- * Install the plugin into the plugins/post-type-swticher directory, and activate!
32
- * From the post edit screen, above the "Publish" button is the "Post Type" interface.
33
- * Change post types as needed.
34
-
35
- == Frequently Asked Questions ==
36
-
37
- = Why would I need this? =
38
- I needed it to move WordPress posts into a custom post type of my own, so this plugin was borned!
39
-
40
- = Does this ruin my taxonomy associations? =
41
- It shouldn't. The only thing this plugin does for version 0.1 is change the 'post_type' property of the post.
 
 
 
 
1
+ === Post Type Switcher ===
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
26
+
27
+ = Version 0.2 =
28
+ * Disallow post types that are not public and do not have a visible UI
29
+
30
+ = Version 0.1 =
31
+ * Release
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.