1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190:
<?php
class CTA_Clone_Post {
public function __construct() {
self::load_hooks();
}
public static function load_hooks() {
add_filter('post_row_actions', array(__CLASS__, 'add_row_actions'),8,2);
add_action('admin_action_cta_clone_post', array(__CLASS__, 'clone_post'));
}
public static function add_row_actions($actions, $post) {
if ( $post->post_type != 'wp-call-to-action' ) {
return $actions;
}
$actions['clone'] = '<a href="'. self::build_clone_link( $post->ID, 'display', true ).'" title="'
. esc_attr(__( 'Clone this item', 'inbound-pro' ))
. '">' . __( 'Clone', 'inbound-pro' ) . '</a>';
return $actions;
}
public static function build_clone_link( $id = 0, $context = 'display', $draft = true){
if ( !$post = get_post( $id ) ) {
return;
}
$action_name = "cta_clone_post";
if ( 'display' == $context )
$action = '?action='.$action_name.'&post='.$post->ID;
else
$action = '?action='.$action_name.'&post='.$post->ID;
$post_type_object = get_post_type_object( $post->post_type );
if ( !$post_type_object )
return;
return apply_filters( 'wp_cta_build_clone_link', admin_url( "admin.php". $action ), $post->ID, $context );
}
public static function clone_post($status = '') {
$id = (isset($_GET['post']) ? intval($_GET['post']) : intval($_POST['post']));
$post = get_post(intval($id));
if (isset($post) && $post!=null) {
$new_id = self::clone_post_callback($post, $status);
if ($status == ''){
wp_redirect( admin_url( 'edit.php?post_type='.$post->post_type));
} else {
wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_id ));
}
exit;
} else {
$post_type_obj = get_post_type_object( $post->post_type );
wp_die(esc_attr(__( 'Copy creation failed, could not find original:', 'inbound-pro' )) . ' ' . $id);
}
}
public static function clone_post_callback($post, $status = '', $parent_id = '', $blank = false) {
$prefix = "";
$suffix = "";
if (!is_object($post)&&is_numeric($post)) {
$post = get_post($post);
}
$status = $post->post_status;
if ($post->post_type == 'revision') {
return;
}
if ($post->post_type != 'attachment'){
$prefix = "Copy of ";
$suffix = "";
$status = 'pending';
}
$new_post_author = wp_get_current_user();
if ($blank==false) {
$new_post = array(
'menu_order' => $post->menu_order,
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author->ID,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt ,
'post_mime_type' => $post->post_mime_type,
'post_parent' => $new_post_parent = empty($parent_id)? $post->post_parent : $parent_id,
'post_password' => $post->post_password,
'post_status' => $status,
'post_title' => $prefix.$post->post_title.$suffix,
'post_type' => $post->post_type,
);
$new_post['post_date'] = $new_post_date = $post->post_date ;
$new_post['post_date_gmt'] = get_gmt_from_date($new_post_date);
} else {
$new_post = array(
'menu_order' => $post->menu_order,
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author->ID,
'post_content' => "",
'post_excerpt' => "" ,
'post_mime_type' => $post->post_mime_type,
'post_status' => $status,
'post_title' => "New Blank Landing Page",
'post_type' => $post->post_type,
'post_date' => date('Y-m-d H:i:s')
);
}
$new_post_id = wp_insert_post($new_post);
$meta_data = self::get_post_meta($post->ID);
foreach ($meta_data as $key=>$value) {
update_post_meta($new_post_id,$key,$value);
}
return $new_post_id;
}
public static function get_post_meta($post_id) {
global $wpdb;
$data = array();
$wpdb->query("
SELECT `meta_key`, `meta_value`
FROM $wpdb->postmeta
WHERE `post_id` = $post_id
");
foreach($wpdb->last_result as $k => $v) {
$data[$v->meta_key] = $v->meta_value;
}
return $data;
}
}
$CTA_Clone_Post = new CTA_Clone_Post();