Skip to content

Commit d8f73d8

Browse files
author
metaphorcreations
committed
Added ability to duplicate posts to other post types
git-svn-id: http://plugins.svn.wordpress.org/post-duplicator/trunk@1288874 b8457f37-d9ea-0310-8a92-e5e31aec5664
1 parent 98da2cd commit d8f73d8

File tree

9 files changed

+98
-10
lines changed

9 files changed

+98
-10
lines changed

assets/js/pd-admin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jQuery( document ).ready( function() {
66
* Creates an ajax request that creates a new post,
77
* duplicating all the data and custom meta.
88
*
9-
* @since 1.0.0
9+
* @since 2.11
1010
*/
1111
jQuery( '.m4c-duplicate-post' ).click( function( e ) {
1212

@@ -23,7 +23,7 @@ jQuery( document ).ready( function() {
2323
jQuery.post( ajaxurl, data, function( response ) {
2424

2525
// Reload the page
26-
location.reload();
26+
window.location.href = window.location.pathname+"?"+jQuery.param({'post-duplicated':response});
2727
});
2828
});
2929
});

includes/ajax.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Thehe jQuery ajax call to create a new post.
55
* Duplicates all the data including custom meta.
66
*
7-
* @since 2.9
7+
* @since 2.11
88
*/
99
function m4c_duplicate_post() {
1010

@@ -30,6 +30,11 @@ function m4c_duplicate_post() {
3030
$duplicate['post_status'] = $settings['status'];
3131
}
3232

33+
// Set the type
34+
if( $settings['type'] != 'same' ) {
35+
$duplicate['post_type'] = $settings['type'];
36+
}
37+
3338
// Set the post date
3439
$timestamp = ( $settings['timestamp'] == 'duplicate' ) ? strtotime($duplicate['post_date']) : current_time('timestamp',0);
3540
$timestamp_gmt = ( $settings['timestamp'] == 'duplicate' ) ? strtotime($duplicate['post_date_gmt']) : current_time('timestamp',1);
@@ -74,7 +79,7 @@ function m4c_duplicate_post() {
7479
}
7580
}
7681

77-
echo 'Duplicate Post Created!';
82+
echo $duplicate_id;
7883

7984
die(); // this is required to return a proper result
8085
}

includes/edit.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,26 @@
66
* @since 2.10
77
*/
88
function mtphr_post_duplicator_action_row( $actions, $post ){
9+
10+
$settings = get_mtphr_post_duplicator_settings();
911

1012
// Get the post type object
1113
$post_type = get_post_type_object( $post->post_type );
1214

15+
// Set the button label
16+
$label = sprintf( __( 'Duplicate %s', 'post-duplicator' ), $post_type->labels->singular_name );
17+
18+
// Modify the label if duplicating to new post type
19+
if( $settings['type'] != 'same' ) {
20+
$new_post_type = get_post_type_object( $settings['type'] );
21+
if( $post_type->name != $new_post_type->name ) {
22+
$label = sprintf( __( 'Duplicate %1$s to %2$s', 'post-duplicator' ), $post_type->labels->singular_name, $new_post_type->labels->singular_name );
23+
}
24+
}
25+
1326
// Create a nonce & add an action
1427
$nonce = wp_create_nonce( 'm4c_ajax_file_nonce' );
15-
$actions['duplicate_post'] = '<a class="m4c-duplicate-post" rel="'.$nonce.'" href="'.$post->ID.'">'. sprintf( __( 'Duplicate %s', 'post-duplicator' ), $post_type->labels->singular_name ).'</a>';
28+
$actions['duplicate_post'] = '<a class="m4c-duplicate-post" rel="'.$nonce.'" href="'.$post->ID.'">'.$label.'</a>';
1629

1730
return $actions;
1831
}

includes/functions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Return a value from the options table if it exists,
55
* or return a default value
66
*
7-
* @since 2.5
7+
* @since 2.11
88
*/
99
function get_mtphr_post_duplicator_settings() {
1010

@@ -13,6 +13,7 @@ function get_mtphr_post_duplicator_settings() {
1313

1414
$defaults = array(
1515
'status' => 'same',
16+
'type' => 'same',
1617
'timestamp' => 'current',
1718
'time_offset' => false,
1819
'time_offset_days' => 0,

includes/helpers.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/* --------------------------------------------------------- */
4+
/* !Return an array of post types - 2.11 */
5+
/* --------------------------------------------------------- */
6+
7+
if( !function_exists('mtphr_post_duplicator_post_types') ) {
8+
function mtphr_post_duplicator_post_types() {
9+
10+
$post_types = array('same' => __('Same as original', 'post-duplicator'));
11+
$pts = get_post_types(array(), 'objects');
12+
13+
// Remove framework post types
14+
unset( $pts['attachment'] );
15+
unset( $pts['revision'] );
16+
unset( $pts['nav_menu_item'] );
17+
unset( $pts['wooframework'] );
18+
19+
if( is_array($pts) && count($pts) > 0 ) {
20+
foreach( $pts as $i=>$pt ) {
21+
$post_types[$i] = $pt->labels->name;
22+
}
23+
}
24+
25+
return $post_types;
26+
}
27+
}

includes/notices.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/* --------------------------------------------------------- */
4+
/* !Create an admin notice that a post has been duplicated - 2.11 */
5+
/* --------------------------------------------------------- */
6+
7+
function mtphr_post_duplicator_notice() {
8+
9+
$duplicated_id = isset($_GET['post-duplicated']) ? $_GET['post-duplicated'] : '';
10+
if( $duplicated_id != '' ) {
11+
12+
$settings = get_mtphr_post_duplicator_settings();
13+
14+
// Get the post type object
15+
$duplicated_post = get_post($duplicated_id);
16+
$post_type = get_post_type_object( $duplicated_post->post_type );
17+
18+
// Set the button label
19+
$pt = $post_type->labels->singular_name;
20+
$link = '<a href="'.get_edit_post_link($duplicated_id).'">'.__('here', 'post-duplicator').'</a>';
21+
$label = sprintf( __( 'Successfully Duplicated! You can edit your new %1$s %2$s.', 'post-duplicator' ), $pt, $link );
22+
23+
?>
24+
<div class="updated">
25+
<p><?php echo $label; ?></p>
26+
</div>
27+
<?php
28+
}
29+
}
30+
add_action('admin_notices', 'mtphr_post_duplicator_notice');

includes/settings.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function mtphr_post_duplicator_settings_page() {
2424
/**
2525
* Initializes the options page.
2626
*
27-
* @since 2.6
27+
* @since 2.11
2828
*/
2929
function mtphr_post_duplicator_initialize_settings() {
3030

@@ -40,6 +40,13 @@ function mtphr_post_duplicator_initialize_settings() {
4040
'default' => 'draft'
4141
);
4242

43+
$settings['type'] = array(
44+
'title' => __( 'Post Type', 'post-duplicator' ),
45+
'type' => 'select',
46+
'options' => mtphr_post_duplicator_post_types(),
47+
'default' => 'same'
48+
);
49+
4350
$settings['timestamp'] = array(
4451
'title' => __( 'Post Date', 'post-duplicator' ),
4552
'type' => 'radio',

m4c-postduplicator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
Plugin Name: Post Duplicator
44
Description: Creates functionality to duplicate any and all post types, including taxonomies & custom fields
5-
Version: 2.10
5+
Version: 2.11
66
Author: Metaphor Creations
77
Author URI: http://www.metaphorcreations.com
88
License: GPL2
@@ -29,7 +29,7 @@
2929

3030

3131
/**Define Widget Constants */
32-
define ( 'MTPHR_POST_DUPLICATOR_VERSION', '2.10' );
32+
define ( 'MTPHR_POST_DUPLICATOR_VERSION', '2.11' );
3333
define ( 'MTPHR_POST_DUPLICATOR_DIR', plugin_dir_path(__FILE__) );
3434
define ( 'MTPHR_POST_DUPLICATOR_URL', plugins_url().'/post-duplicator' );
3535

@@ -57,12 +57,14 @@ function mtphr_post_duplicator_localization() {
5757
if ( is_admin() ) {
5858

5959
// Load Metaboxer
60+
require_once( MTPHR_POST_DUPLICATOR_DIR.'includes/helpers.php' );
6061
require_once( MTPHR_POST_DUPLICATOR_DIR.'metaboxer/metaboxer.php' );
6162
require_once( MTPHR_POST_DUPLICATOR_DIR.'metaboxer/metaboxer-class.php' );
6263
require_once( MTPHR_POST_DUPLICATOR_DIR.'includes/scripts.php' );
6364
require_once( MTPHR_POST_DUPLICATOR_DIR.'includes/ajax.php' );
6465
require_once( MTPHR_POST_DUPLICATOR_DIR.'includes/edit.php' );
6566
require_once( MTPHR_POST_DUPLICATOR_DIR.'includes/functions.php' );
67+
require_once( MTPHR_POST_DUPLICATOR_DIR.'includes/notices.php' );
6668
require_once( MTPHR_POST_DUPLICATOR_DIR.'includes/settings.php' );
6769
}
6870

readme.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ Check out the 'Installation' tab.
4141

4242
== Changelog ==
4343

44+
= 2.11 =
45+
* Added ability to duplicate posts to other post types
46+
4447
= 2.10 =
4548
* Added page duplication support for the WP Customer Area plugin
4649

@@ -87,4 +90,4 @@ Must upgrade in order for the plugin to work. The file paths where initially wro
8790

8891
== Upgrade Notice ==
8992

90-
Added page duplication support for the WP Customer Area plugin.
93+
Added ability to duplicate posts to other post types.

0 commit comments

Comments
 (0)