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: 191: 192: 193: 194: 195: 196: 197: 198: 199:
<?php
class Landing_Pages_Row_Actions {
public function __construct() {
self::load_hooks();
}
public static function load_hooks() {
add_action('admin_action_clone_landing_page', array( __CLASS__ , 'clone_landing_page' ) );
add_filter('post_row_actions', array( __CLASS__ ,'add_clone_link' ), 10, 2);
add_filter('post_row_actions', array( __CLASS__ ,'add_clear_stats_link' ), 10, 2);
}
public static function add_clone_link($actions, $post) {
if ($post->post_type != 'landing-page' ) {
return $actions;
}
$actions['clone'] = '<a href="'.self::get_clone_link( $post->ID ).'" title="'
. esc_attr(__("Clone this item", 'landing-pages'))
. '">' . __('Clone', 'landing-pages') . '</a>';
return $actions;
}
public static function get_clone_link( $id = 0 ) {
if ( !$post = get_post( $id ) ) {
return;
}
$link = add_query_arg( array('action'=>'clone_landing_page' , 'post' => $post->ID ) , admin_url("admin.php"));
return $link;
}
public static function clone_landing_page($status = 'pending') {
$id = (isset($_GET['post']) ? intval($_GET['post']) : intval($_POST['post']) );
$post = get_post($id);
if (!isset($post) || !$post) {
$post_type_obj = get_post_type_object( $post->post_type );
wp_die(esc_attr(__('Copy creation failed, could not find original:', 'landing-pages')) . ' ' . $id);
}
if (!is_object($post)&&is_numeric($post)) {
$post = get_post($post);
}
$status = $post->post_status;
if ($post->post_type == 'revision') {
return;
}
$prefix = "Copy of ";
$suffix = "";
$status = 'pending';
$new_post_author = self::get_current_user();
$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);
$new_post_id = wp_insert_post($new_post);
$meta_data = self::get_meta($post->ID);
foreach ($meta_data as $key => $value) {
update_post_meta($new_post_id, $key, $value);
}
wp_redirect(admin_url('edit.php?post_type=' . $post->post_type));
exit;
}
public static function get_current_user() {
if (function_exists('wp_get_current_user')) {
return wp_get_current_user();
} else if (function_exists('wp_get_current_user')) {
global $userdata;
$userdata = wp_get_current_user();
return $userdata;
} else {
$user_login = $_COOKIE[USER_COOKIE];
$current_user = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE user_login='$user_login'");
return $current_user;
}
}
public static function get_meta($landing_page_id) {
global $wpdb;
$data = array();
$wpdb->query("
SELECT `meta_key`, `meta_value`
FROM $wpdb->postmeta
WHERE `post_id` = $landing_page_id
");
foreach ($wpdb->last_result as $k => $v) {
$data[$v->meta_key] = $v->meta_value;
};
return $data;
}
public static function add_clear_stats_link($actions, $post) {
if ($post->post_type != 'landing-page' ) {
return $actions;
}
$actions['clear_the_stats'] = '<a id="'.$post->ID.'" title="'
. esc_attr(__("Clear the stats?", 'landing-pages'))
. '"class="clear_stats"'
. 'style="cursor:pointer;">' . __('Clear Stats', 'landing-pages') . '</a>';
return $actions;
}
}
new Landing_Pages_Row_Actions;