Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 135 lines (113 sloc) 4.638 kb
17e1fbb @kevinlangleyjr First commit
kevinlangleyjr authored
1 <?php
2 /*
3 Plugin Name: Post Type Converter
4 Plugin URI: http://voceconnect.com
5 Description: Gives a qualified user the ability to convert a post from one post_type to another public post_type.
6 Author: Kevin Langley
7 Version: 1.0
8 Author URI: http://voceconnect.com
9 */
10 if(!class_exists('Post_Type_Converter')) {
11
12 class Post_Type_Converter {
13
14 public static function initialize() {
15 if ( current_user_can('manage_options') ){
16 add_action('add_meta_boxes', array(__CLASS__, 'add_convert_meta_box'), 20);;
17 add_action('save_post', array(__CLASS__, 'save_convert'));
18 add_action('admin_enqueue_scripts', array(__CLASS__, 'add_bulk_edit_js'));
19 add_action('admin_init', array(__CLASS__, 'check_bulk_convert'));
20 }
21 }
22
23 public static function get_post_types(){
24 $post_types = get_post_types(array('public' => true));
25 unset($post_types['attachment']);
26 return $post_types;
27 }
28
29 public static function get_script_vars(){
30 $post_types = self::get_post_types();
31 $script_vars = array();
32 foreach($post_types as $post_type){
8538b17 @prettyboymp fixing handling around post types with underscores
prettyboymp authored
33 $script_vars[$post_type] = get_post_type_object($post_type)->labels->singular_name;
17e1fbb @kevinlangleyjr First commit
kevinlangleyjr authored
34 }
35
36 return $script_vars;
37 }
38
39 public static function add_bulk_edit_js(){
40 $screen = get_current_screen();
41 $post_types = self::get_post_types();
42 //screens are id'ed as 'edit-$post_type' so only add the js if on on a edit-$post_type screen.
43 if( ( substr( $screen->id, 0, 4 ) == 'edit' ) && ( in_array( substr( $screen->id, 5, strlen( $screen->id ) ), $post_types ) ) ) {
44
45 $script_vars = self::get_script_vars();
46
47 wp_enqueue_script('post-type-converter', trailingslashit(plugins_url()).trailingslashit(basename(dirname(__FILE__))).'js/post-type-converter.js', array('jquery'));
48 wp_localize_script('post-type-converter', 'script_vars', $script_vars);
49 }
50 }
51
52 public static function add_convert_meta_box() {
53 $screen = get_current_screen();
54
55 if($screen->action == 'add')
56 return;
57
58 $post_types = self::get_post_types();
59
60 foreach($post_types as $post_type) {
61 add_meta_box('convert-post-type', 'Convert Post Type', array(__CLASS__, 'convert_meta_box_content'), $post_type, 'side', 'high');
62 add_post_type_support($post_type, 'post-type-convert');
63 }
64 }
65
66 public static function convert_meta_box_content($post) {
67 $post_types = self::get_post_types();
68
69 echo '<select id="convert_post_type" name="convert_post_type">';
70 foreach($post_types as $single_post_type){
ed85307 @kevinlangleyjr Fixing Variable Name
kevinlangleyjr authored
71 echo '<option value="'.$single_post_type.'" '.selected($post->post_type, $single_post_type).'>'.get_post_type_object($single_post_type)->labels->singular_name.'</option>';
17e1fbb @kevinlangleyjr First commit
kevinlangleyjr authored
72 }
73 echo '</select>';
74 wp_nonce_field('update_post_type_conversion', 'convert_post_type_nonce');
75 }
76
77 public static function check_bulk_convert() {
78 global $pagenow;
30d5587 @kevinlangleyjr Add handling for converting from post to page back to post to keep categ...
kevinlangleyjr authored
79
17e1fbb @kevinlangleyjr First commit
kevinlangleyjr authored
80 if($pagenow == 'edit.php' && isset($_REQUEST['post'])){
81 if(isset($_REQUEST['change_post_type']) && -1 != $_REQUEST['change_post_type'] ){
82 $new_post_type = $_REQUEST['change_post_type'];
83 } elseif(isset($_REQUEST['change_post_type2']) && -1 != $_REQUEST['change_post_type2'] ){
84 $new_post_type = $_REQUEST['change_post_type2'];
85 }
86 if(isset($new_post_type)){
87 foreach($_REQUEST['post'] as $post_id){
88 $post = get_post($post_id);
89 self::convert_post_type($post, $new_post_type);
90 }
8f98547 @kevinlangleyjr Fixing wp_redirect to be within the if statement above it
kevinlangleyjr authored
91
92 $new_url = get_admin_url('', $pagenow);
17e1fbb @kevinlangleyjr First commit
kevinlangleyjr authored
93
8f98547 @kevinlangleyjr Fixing wp_redirect to be within the if statement above it
kevinlangleyjr authored
94 if($_REQUEST['post_type'] != 'post'){
95 $new_url = add_query_arg('post_type', $_REQUEST['post_type'], $new_url);
96 }
97
98 wp_redirect($new_url);
99 exit();
17e1fbb @kevinlangleyjr First commit
kevinlangleyjr authored
100 }
101 }
102 }
103
104 public static function save_convert($post_id) {
105 if(wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)) {
106 return $post_id;
107 }
108 if(isset($_REQUEST['convert_post_type_nonce']) && wp_verify_nonce($_REQUEST['convert_post_type_nonce'], 'update_post_type_conversion')) {
109 $new_post_type = $_REQUEST['convert_post_type'];
110 $post_types = self::get_post_types();
111 if(in_array($new_post_type, $post_types)){
112 self::convert_post_type(get_post($post_id), $new_post_type);
113 }
114 }
115 }
116
117 public static function convert_post_type($post, $new_post_type) {
118 if($post->post_type != $new_post_type){
30d5587 @kevinlangleyjr Add handling for converting from post to page back to post to keep categ...
kevinlangleyjr authored
119 $categories = get_the_terms($post->ID, 'category');
120 $cat_array = array();
985622e @prettyboymp adding category check
prettyboymp authored
121 if($categories) {
122 foreach($categories as $cagtegory){
123 $cat_array[] = $cagtegory->term_id;
124 }
30d5587 @kevinlangleyjr Add handling for converting from post to page back to post to keep categ...
kevinlangleyjr authored
125 }
17e1fbb @kevinlangleyjr First commit
kevinlangleyjr authored
126 $post->post_type = $new_post_type;
30d5587 @kevinlangleyjr Add handling for converting from post to page back to post to keep categ...
kevinlangleyjr authored
127 $post->post_category = $cat_array;
128 wp_insert_post($post);
17e1fbb @kevinlangleyjr First commit
kevinlangleyjr authored
129 }
130 return;
131 }
132 }
133
134 add_action( 'init', array( 'Post_Type_Converter', 'initialize' ) );
135 }
Something went wrong with that request. Please try again.