| 1 |
<?php |
| 2 |
|
| 3 |
/* --------------------------------------------------------- */ |
| 4 |
/* !Duplicate the post - 2.26 */ |
| 5 |
/* --------------------------------------------------------- */ |
| 6 |
|
| 7 |
function mtphr_duplicate_post( $original_id, $args=array(), $do_action=true ) { |
| 8 |
|
| 9 |
// Get access to the database |
| 10 |
global $wpdb; |
| 11 |
|
| 12 |
// Get the post as an array |
| 13 |
$duplicate = get_post( $original_id, 'ARRAY_A' ); |
| 14 |
|
| 15 |
$global_settings = get_mtphr_post_duplicator_settings(); |
| 16 |
$settings = wp_parse_args( $args, $global_settings ); |
| 17 |
|
| 18 |
// Modify some of the elements |
| 19 |
$appended = isset( $settings['title'] ) ? $settings['title'] : esc_html__( 'Copy', 'post-duplicator' ); |
| 20 |
$duplicate['post_title'] = $duplicate['post_title'] . ' ' . $appended; |
| 21 |
$duplicate['post_name'] = sanitize_title( $duplicate['post_name'] . '-' . $settings['slug'] ); |
| 22 |
|
| 23 |
// Set the status |
| 24 |
if( $settings['status'] != 'same' ) { |
| 25 |
$duplicate['post_status'] = $settings['status']; |
| 26 |
} |
| 27 |
|
| 28 |
// Set the type |
| 29 |
if( $settings['type'] != 'same' ) { |
| 30 |
$duplicate['post_type'] = $settings['type']; |
| 31 |
} |
| 32 |
|
| 33 |
// Set the post date |
| 34 |
$timestamp = ( $settings['timestamp'] == 'duplicate' ) ? strtotime($duplicate['post_date']) : current_time('timestamp',0); |
| 35 |
$timestamp_gmt = ( $settings['timestamp'] == 'duplicate' ) ? strtotime($duplicate['post_date_gmt']) : current_time('timestamp',1); |
| 36 |
|
| 37 |
if( $settings['time_offset'] ) { |
| 38 |
$offset = intval($settings['time_offset_seconds']+$settings['time_offset_minutes']*60+$settings['time_offset_hours']*3600+$settings['time_offset_days']*86400); |
| 39 |
if( $settings['time_offset_direction'] == 'newer' ) { |
| 40 |
$timestamp = intval($timestamp+$offset); |
| 41 |
$timestamp_gmt = intval($timestamp_gmt+$offset); |
| 42 |
} else { |
| 43 |
$timestamp = intval($timestamp-$offset); |
| 44 |
$timestamp_gmt = intval($timestamp_gmt-$offset); |
| 45 |
} |
| 46 |
} |
| 47 |
$duplicate['post_date'] = date('Y-m-d H:i:s', $timestamp); |
| 48 |
$duplicate['post_date_gmt'] = date('Y-m-d H:i:s', $timestamp_gmt); |
| 49 |
$duplicate['post_modified'] = date('Y-m-d H:i:s', current_time('timestamp',0)); |
| 50 |
$duplicate['post_modified_gmt'] = date('Y-m-d H:i:s', current_time('timestamp',1)); |
| 51 |
if ( 'current_user' == $settings['post_author'] ) { |
| 52 |
$duplicate['post_author'] = get_current_user_id(); |
| 53 |
} |
| 54 |
|
| 55 |
// Remove some of the keys |
| 56 |
unset( $duplicate['ID'] ); |
| 57 |
unset( $duplicate['guid'] ); |
| 58 |
unset( $duplicate['comment_count'] ); |
| 59 |
|
| 60 |
$duplicate['post_content'] = str_replace( array( '\r\n', '\r', '\n' ), '<br />', $duplicate['post_content'] ); //Handles guttenburg escaping in returns for blocks |
| 61 |
|
| 62 |
// Insert the post into the database |
| 63 |
$duplicate_id = wp_insert_post( $duplicate ); |
| 64 |
|
| 65 |
// Duplicate all the taxonomies/terms |
| 66 |
$taxonomies = get_object_taxonomies( $duplicate['post_type'] ); |
| 67 |
foreach( $taxonomies as $taxonomy ) { |
| 68 |
$terms = wp_get_post_terms( $original_id, $taxonomy, array('fields' => 'names') ); |
| 69 |
wp_set_object_terms( $duplicate_id, $terms, $taxonomy ); |
| 70 |
} |
| 71 |
|
| 72 |
// Duplicate all the custom fields |
| 73 |
$custom_fields = get_post_custom( $original_id ); |
| 74 |
foreach ( $custom_fields as $key => $value ) { |
| 75 |
if( is_array($value) && count($value) > 0 ) { |
| 76 |
foreach( $value as $i=>$v ) { |
| 77 |
$data = array( |
| 78 |
'post_id' => $duplicate_id, |
| 79 |
'meta_key' => $key, |
| 80 |
'meta_value' => $v, |
| 81 |
); |
| 82 |
$formats = array( |
| 83 |
'%d', |
| 84 |
'%s', |
| 85 |
'%s', |
| 86 |
); |
| 87 |
$result = $wpdb->insert( $wpdb->prefix.'postmeta', $data, $formats ); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
// Add an action for others to do custom stuff |
| 93 |
if( $do_action ) { |
| 94 |
do_action( 'mtphr_post_duplicator_created', $original_id, $duplicate_id, $settings ); |
| 95 |
} |
| 96 |
|
| 97 |
return $duplicate_id; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
/* --------------------------------------------------------- */ |
| 102 |
/* !Ajax duplicate post - 2.25 */ |
| 103 |
/* --------------------------------------------------------- */ |
| 104 |
|
| 105 |
function m4c_duplicate_post() { |
| 106 |
|
| 107 |
// Check the nonce |
| 108 |
check_ajax_referer( 'm4c_ajax_file_nonce', 'security' ); |
| 109 |
|
| 110 |
// Get variables |
| 111 |
$original_id = intval( $_POST['original_id'] ); |
| 112 |
|
| 113 |
// Duplicate the post |
| 114 |
$duplicate_id = mtphr_duplicate_post( $original_id ); |
| 115 |
|
| 116 |
$data = array( |
| 117 |
'duplicate_id' => esc_attr( $duplicate_id ), |
| 118 |
); |
| 119 |
wp_send_json( $data ); |
| 120 |
} |
| 121 |
add_action( 'wp_ajax_m4c_duplicate_post', 'm4c_duplicate_post' ); |