" . __("Duplicate", DUPLICATE_POST_I18N_DOMAIN) . "";
}
}
}
/**
* WP version < 2.8: add link to custom column for pages
*/
// Added by WarmStal
function duplicate_page_make_duplicate_link($column_name, $id) {
if (duplicate_post_is_current_user_allowed_to_copy()) {
if ($column_name == DUPLICATE_POST_COLUMN) {
echo "" . __("Duplicate", DUPLICATE_POST_I18N_DOMAIN) . "";
}
}
}
/**
* Connect actions to functions
*/
add_action('admin_action_duplicate_post_save_as_new_post', 'duplicate_post_save_as_new_post');
add_action('admin_action_duplicate_post_save_as_new_page', 'duplicate_post_save_as_new_page');
/**
* Add the link to action list for post_row_actions
*/
function duplicate_post_make_duplicate_link_row($actions, $post) {
if (duplicate_post_is_current_user_allowed_to_copy()) {
$actions['duplicate'] = '' . __("Duplicate", DUPLICATE_POST_I18N_DOMAIN) . '';
}
return $actions;
}
/**
* Add the link to action list for page_row_actions
*/
function duplicate_page_make_duplicate_link_row($actions, $page) {
if (duplicate_post_is_current_user_allowed_to_copy()) {
$actions['duplicate'] = '' . __("Duplicate", DUPLICATE_POST_I18N_DOMAIN) . '';
}
return $actions;
}
/**
* Add a button in the post/page edit screen to create a clone
*/
add_action( 'post_submitbox_start', 'duplicate_post_add_duplicate_post_button' );
function duplicate_post_add_duplicate_post_button() {
if ( isset( $_GET['post'] ) && duplicate_post_is_current_user_allowed_to_copy()) {
$act = "admin.php?action=duplicate_post_save_as_new_post";
global $post;
if ($post->post_type == "page") $act = "admin.php?action=duplicate_post_save_as_new_page";
$notifyUrl = $act."&post=" . $_GET['post'];
?>
=2 && $level<=4) {
// authors 2..4
return 2;
} else if ($level>=5 && $level<=7) {
// editors 5..7
return 3;
} else if ($level>=8) {
// admins 8..10
return 4;
}
return 0;
}
/**
* Get the currently registered user
*/
function duplicate_post_get_current_user() {
if (function_exists('wp_get_current_user')) {
return wp_get_current_user();
} else if (function_exists('get_currentuserinfo')) {
global $userdata;
get_currentuserinfo();
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;
}
}
/**
* Escape single quotes, specialchar double quotes, and fix line endings.
*/
function duplicate_post_js_escape($text) {
if (function_exists('js_escape')) {
return js_escape($text);
} else {
$safe_text = str_replace('&&', '&&', $text);
$safe_text = str_replace('&&', '&&', $safe_text);
$safe_text = preg_replace('/&(?:$|([^#])(?![a-z1-4]{1,8};))/', '&$1', $safe_text);
$safe_text = str_replace('<', '<', $safe_text);
$safe_text = str_replace('>', '>', $safe_text);
$safe_text = str_replace('"', '"', $safe_text);
$safe_text = str_replace(''', "'", $safe_text);
$safe_text = preg_replace("/\r?\n/", "\\n", addslashes($safe_text));
return safe_text;
}
}
/**
* Get a page from the database
*/
function duplicate_post_get_page($id) {
global $wpdb;
$post = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID=$id");
if ($post->post_type == "revision"){
$id = $post->post_parent;
$post = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID=$id");
}
return $post[0];
}
/**
* Get a post from the database
*/
function duplicate_post_get_post($id) {
global $wpdb;
$post = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID=$id");
if ($post->post_type == "revision"){
$id = $post->post_parent;
$post = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID=$id");
}
return $post[0];
}
/**
* Copy the taxonomies of a post to another post
*/
function duplicate_post_copy_post_taxonomies($id, $new_id, $post_type) {
global $wpdb;
if (isset($wpdb->terms)) {
// WordPress 2.3
$taxonomies = get_object_taxonomies($post_type); //array("category", "post_tag");
foreach ($taxonomies as $taxonomy) {
$post_terms = wp_get_object_terms($id, $taxonomy);
for ($i=0; $islug, $taxonomy, true);
}
}
}
}
/**
* Copy the meta information of a post to another post
*/
function duplicate_post_copy_post_meta_info($id, $new_id) {
global $wpdb;
$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$id");
if (count($post_meta_infos)!=0) {
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
$meta_no_copy = explode(",",get_option('duplicate_post_blacklist'));
foreach ($post_meta_infos as $meta_info) {
$meta_key = $meta_info->meta_key;
$meta_value = addslashes($meta_info->meta_value);
if (!in_array($meta_key,$meta_no_copy)) {
$sql_query_sel[]= "SELECT $new_id, '$meta_key', '$meta_value'";
}
}
$sql_query.= implode(" UNION ALL ", $sql_query_sel);
$wpdb->query($sql_query);
}
}
/**
* Create a duplicate from a post
*/
function duplicate_post_create_duplicate_from_post($post) {
global $wpdb;
//$new_post_type = 'post';
$new_post_author = duplicate_post_get_current_user();
$new_post_date = (get_option('duplicate_post_copydate') == 1)? $post->post_date : current_time('mysql');
$new_post_date_gmt = get_gmt_from_date($new_post_date);
$prefix = get_option('duplicate_post_title_prefix');
if (!empty($prefix)) $prefix.= " ";
$new_post_type = $post->post_type;
$post_content = str_replace("'", "''", $post->post_content);
$post_content_filtered = str_replace("'", "''", $post->post_content_filtered);
$post_excerpt = str_replace("'", "''", $post->post_excerpt);
$post_title = $prefix.str_replace("'", "''", $post->post_title);
$post_status = str_replace("'", "''", $post->post_status);
$post_name = str_replace("'", "''", $post->post_name);
$comment_status = str_replace("'", "''", $post->comment_status);
$ping_status = str_replace("'", "''", $post->ping_status);
// Insert the new template in the post table
$wpdb->query(
"INSERT INTO $wpdb->posts
(post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt, post_status, post_type, comment_status, ping_status, post_password, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type)
VALUES
('$new_post_author->ID', '$new_post_date', '$new_post_date_gmt', '$post_content', '$post_content_filtered', '$post_title', '$post_excerpt', 'draft', '$new_post_type', '$comment_status', '$ping_status', '$post->post_password', '$post->to_ping', '$post->pinged', '$new_post_date', '$new_post_date_gmt', '$post->post_parent', '$post->menu_order', '$post->post_mime_type')");
$new_post_id = $wpdb->insert_id;
// Copy the taxonomies
duplicate_post_copy_post_taxonomies($post->ID, $new_post_id, $post->post_type);
// Copy the meta information
duplicate_post_copy_post_meta_info($post->ID, $new_post_id);
return $new_post_id;
}
/**
* Create a duplicate from a page
*/
function duplicate_post_create_duplicate_from_page($post) {
global $wpdb;
//$new_post_type = 'page';
$new_post_author = duplicate_post_get_current_user();
$new_post_date = (get_option('duplicate_post_copydate') == 1)? $post->post_date : current_time('mysql');
$new_post_date_gmt = get_gmt_from_date($new_post_date);
$prefix = get_option('duplicate_post_title_prefix');
if (!empty($prefix)) $prefix.= " ";
$new_post_type = $post->post_type;
$post_content = str_replace("'", "''", $post->post_content);
$post_content_filtered = str_replace("'", "''", $post->post_content_filtered);
$post_excerpt = str_replace("'", "''", $post->post_excerpt);
$post_title = $prefix.str_replace("'", "''", $post->post_title);
$post_status = str_replace("'", "''", $post->post_status);
$post_name = str_replace("'", "''", $post->post_name);
$comment_status = str_replace("'", "''", $post->comment_status);
$ping_status = str_replace("'", "''", $post->ping_status);
// Insert the new template in the post table
$wpdb->query(
"INSERT INTO $wpdb->posts
(post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt, post_status, post_type, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type)
VALUES
('$new_post_author->ID', '$new_post_date', '$new_post_date_gmt', '$post_content', '$post_content_filtered', '$post_title', '$post_excerpt', 'draft', '$new_post_type', '$comment_status', '$ping_status', '$post->post_password', '$post_name', '$post->to_ping', '$post->pinged', '$new_post_date', '$new_post_date_gmt', '$post->post_parent', '$post->menu_order', '$post->post_mime_type')");
$new_page_id = $wpdb->insert_id;
// Copy the taxonomies
duplicate_post_copy_post_taxonomies($post->ID, $new_page_id, $post->post_type);
// Copy the meta information
duplicate_post_copy_post_meta_info($post->ID, $new_page_id);
return $new_page_id;
}
/**
* Add an option page where you can specify which meta fields you don't want to copy
*/
if ( is_admin() ){ // admin actions
add_action('admin_menu', 'duplicate_post_menu');
add_action( 'admin_init', 'duplicate_post_register_settings');
}
function duplicate_post_register_settings() { // whitelist options
register_setting( 'duplicate_post_group', 'duplicate_post_copydate');
register_setting( 'duplicate_post_group', 'duplicate_post_blacklist');
register_setting( 'duplicate_post_group', 'duplicate_post_title_prefix');
register_setting( 'duplicate_post_group', 'duplicate_post_copy_user_level');
}
function duplicate_post_menu() {
add_options_page(__("Duplicate Post Options", DUPLICATE_POST_I18N_DOMAIN), __("Duplicate Post", DUPLICATE_POST_I18N_DOMAIN), 'administrator', 'duplicatepost', 'duplicate_post_options');
}
function duplicate_post_options() {
?>
' . __('Donate', DUPLICATE_POST_I18N_DOMAIN) . '';
$links[] = '' . __('Translate', DUPLICATE_POST_I18N_DOMAIN) . '';
}
return $links;
}
?>