Skip to content
Browse files

First commit

  • Loading branch information...
0 parents commit 17e1fbb5a363be80eaff4e7adb1c1a7a2d3de489 Kevin Langley committed Nov 2, 2011
Showing with 138 additions and 0 deletions.
  1. +12 −0 js/post-type-converter.js
  2. +126 −0 post-type-converter.php
12 js/post-type-converter.js
@@ -0,0 +1,12 @@
+jQuery(function(){
+
+ var options = '<option value="-1">Convert Post Type</option>';
+ for(var index in script_vars){
+ options += '<option value="'+index.replace('_', '-')+'">'+script_vars[index]+'</option>';
+ }
+ var top_html = '<div class="alignleft actions"><select name="change_post_type">'+options+'</select></div><input type="submit" name="" id="apply_convert_post_type" class="button-secondary action" value="Convert">';
+ var bottom_html = '<div class="alignleft actions"><select name="change_post_type2">'+options+'</select></div><input type="submit" name="" id="apply_convert_post_type" class="button-secondary action" value="Convert">';
+
+ jQuery(top_html).insertBefore('.tablenav.top .tablenav-pages');
+ jQuery(bottom_html).insertBefore('.tablenav.bottom .tablenav-pages');
+});
126 post-type-converter.php
@@ -0,0 +1,126 @@
+<?php
+/*
+Plugin Name: Post Type Converter
+Plugin URI: http://voceconnect.com
+Description: Gives a qualified user the ability to convert a post from one post_type to another public post_type.
+Author: Kevin Langley
+Version: 1.0
+Author URI: http://voceconnect.com
+*/
+if(!class_exists('Post_Type_Converter')) {
+
+ class Post_Type_Converter {
+
+ public static function initialize() {
+ if ( current_user_can('manage_options') ){
+ add_action('add_meta_boxes', array(__CLASS__, 'add_convert_meta_box'), 20);;
+ add_action('save_post', array(__CLASS__, 'save_convert'));
+ add_action('admin_enqueue_scripts', array(__CLASS__, 'add_bulk_edit_js'));
+ add_action('admin_init', array(__CLASS__, 'check_bulk_convert'));
+ }
+ }
+
+ public static function get_post_types(){
+ $post_types = get_post_types(array('public' => true));
+ unset($post_types['attachment']);
+ return $post_types;
+ }
+
+ public static function get_script_vars(){
+ $post_types = self::get_post_types();
+ $script_vars = array();
+ foreach($post_types as $post_type){
+ $script_vars[str_replace('-', '_', $post_type)] .= ucwords(str_replace('-', ' ', $post_type));
+ }
+
+ return $script_vars;
+ }
+
+ public static function add_bulk_edit_js(){
+ $screen = get_current_screen();
+ $post_types = self::get_post_types();
+ //screens are id'ed as 'edit-$post_type' so only add the js if on on a edit-$post_type screen.
+ if( ( substr( $screen->id, 0, 4 ) == 'edit' ) && ( in_array( substr( $screen->id, 5, strlen( $screen->id ) ), $post_types ) ) ) {
+
+ $script_vars = self::get_script_vars();
+
+ wp_enqueue_script('post-type-converter', trailingslashit(plugins_url()).trailingslashit(basename(dirname(__FILE__))).'js/post-type-converter.js', array('jquery'));
+ wp_localize_script('post-type-converter', 'script_vars', $script_vars);
+ }
+ }
+
+ public static function add_convert_meta_box() {
+ $screen = get_current_screen();
+
+ if($screen->action == 'add')
+ return;
+
+ $post_types = self::get_post_types();
+
+ foreach($post_types as $post_type) {
+ add_meta_box('convert-post-type', 'Convert Post Type', array(__CLASS__, 'convert_meta_box_content'), $post_type, 'side', 'high');
+ add_post_type_support($post_type, 'post-type-convert');
+ }
+ }
+
+ public static function convert_meta_box_content($post) {
+ $post_types = self::get_post_types();
+
+ echo '<select id="convert_post_type" name="convert_post_type">';
+ foreach($post_types as $single_post_type){
+ echo '<option value="'.$single_post_type.'" '.selected($post->post_type, $single_post_type).'>'.ucwords(str_replace('-', ' ', $single_post_type)).'</option>';
+ }
+ echo '</select>';
+ wp_nonce_field('update_post_type_conversion', 'convert_post_type_nonce');
+ }
+
+ public static function check_bulk_convert() {
+ global $pagenow;
+ if($pagenow == 'edit.php' && isset($_REQUEST['post'])){
+ if(isset($_REQUEST['change_post_type']) && -1 != $_REQUEST['change_post_type'] ){
+ $new_post_type = $_REQUEST['change_post_type'];
+ } elseif(isset($_REQUEST['change_post_type2']) && -1 != $_REQUEST['change_post_type2'] ){
+ $new_post_type = $_REQUEST['change_post_type2'];
+ }
+ if(isset($new_post_type)){
+ foreach($_REQUEST['post'] as $post_id){
+ $post = get_post($post_id);
+ self::convert_post_type($post, $new_post_type);
+ }
+ }
+
+ $new_url = get_admin_url('', $pagenow);
+
+ if($_REQUEST['post_type'] != 'post'){
+ $new_url = add_query_arg('post_type', $_REQUEST['post_type'], $new_url);
+ }
+
+ wp_redirect($new_url);
+ exit();
+ }
+ }
+
+ public static function save_convert($post_id) {
+ if(wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)) {
+ return $post_id;
+ }
+ if(isset($_REQUEST['convert_post_type_nonce']) && wp_verify_nonce($_REQUEST['convert_post_type_nonce'], 'update_post_type_conversion')) {
@banderon
banderon added a note Jan 17, 2013

This should also check if the post is the one currently being edited, in case other posts are saved programmatically along with the post being edited.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $new_post_type = $_REQUEST['convert_post_type'];
+ $post_types = self::get_post_types();
+ if(in_array($new_post_type, $post_types)){
+ self::convert_post_type(get_post($post_id), $new_post_type);
+ }
+ }
+ }
+
+ public static function convert_post_type($post, $new_post_type) {
+ if($post->post_type != $new_post_type){
+ $post->post_type = $new_post_type;
+ return wp_update_post($post);
+ }
+ return;
+ }
+ }
+
+ add_action( 'init', array( 'Post_Type_Converter', 'initialize' ) );
+}

0 comments on commit 17e1fbb

Please sign in to comment.
Something went wrong with that request. Please try again.