'; exit; } else { wp_die(_e('Post creation failed, could not find original post:', DUPLICATE_POST_I18N_DOMAIN) . ' ' . $id); } } /* * Same as above, for pages */ function duplicate_post_save_as_new_page(){ if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'duplicate_post_save_as_new_post' == $_REQUEST['action'] ) ) ) { wp_die(_e('No page to duplicate has been supplied!', DUPLICATE_POST_I18N_DOMAIN)); } // Get the original page $id = (isset($_GET['post']) ? $_GET['post'] : $_POST['post']); $post = duplicate_post_get_page($id); // Copy the page and insert it if (isset($post) && $post!=null) { $new_id = duplicate_post_create_duplicate_from_page($post); // Show the page edit echo ''; exit; } else { wp_die(_e('Post creation failed, could not find original post:', DUPLICATE_POST_I18N_DOMAIN) . ' ' . $id); } } // Version of the plugin define('DUPLICATE_POST_CURRENT_VERSION', '0.6' ); define('DUPLICATE_POST_COLUMN', 'control_duplicate_post'); define('DUPLICATE_POST_VIEW_USER_LEVEL_OPTION', 'duplicate_post_view_user_level'); define('DUPLICATE_POST_CREATE_USER_LEVEL_OPTION', 'duplicate_post_create_user_level'); define('DUPLICATE_POST_ADMIN_USER_LEVEL_OPTION', 'duplicate_post_admin_user_level'); // i18n plugin domain define('DUPLICATE_POST_I18N_DOMAIN', 'duplicate-post'); /** * Initialise the internationalisation domain */ //if (!function_exists('duplicate_post_init_i18n')) { // function duplicate_post_init_i18n() { load_plugin_textdomain(DUPLICATE_POST_I18N_DOMAIN, 'wp-content/plugins/duplicate-post/languages','duplicate-post/languages'); // } // duplicate_post_init_i18n(); //} /** * Plugin activation */ add_action('activate_duplicate-post/duplicate-post.php','duplicate_post_plugin_activation'); function duplicate_post_plugin_activation() { $installed_version = duplicate_post_get_installed_version(); if ( $installed_version==duplicate_post_get_current_version() ) { // do nothing } else if ( $installed_version=='' ) { // Add all options, nothing already installed add_option( DUPLICATE_POST_VIEW_USER_LEVEL_OPTION, '2', 'Default user level to copy posts' ); add_option( DUPLICATE_POST_CREATE_USER_LEVEL_OPTION, '5', 'Default user level to create the templates' ); add_option( DUPLICATE_POST_ADMIN_USER_LEVEL_OPTION, '8', 'Default user level to change the plugin options' ); } // Update version number update_option( 'duplicate_post_version', duplicate_post_get_current_version() ); } /** * Add a column in the post listing */ add_filter('manage_posts_columns', 'duplicate_post_add_duplicate_post_column'); // Added by WarmStal add_filter('manage_pages_columns', 'duplicate_post_add_duplicate_post_column'); add_action('manage_posts_custom_column', 'duplicate_post_make_duplicate_link', 10, 2); // Added by WarmStal add_action('manage_pages_custom_column', 'duplicate_page_make_duplicate_link', 10, 2); 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'); function duplicate_post_add_duplicate_post_column($columns) { if (duplicate_post_is_current_user_allowed_to_create()) { $columns[DUPLICATE_POST_COLUMN] = ''; } return $columns; } function duplicate_post_make_duplicate_link($column_name, $id) { if (duplicate_post_is_current_user_allowed_to_create()) { if ($column_name == DUPLICATE_POST_COLUMN) { echo "" . __("Duplicate", DUPLICATE_POST_I18N_DOMAIN) . ""; } } } // Added by WarmStal function duplicate_page_make_duplicate_link($column_name, $id) { if (duplicate_post_is_current_user_allowed_to_create()) { if ($column_name == DUPLICATE_POST_COLUMN) { echo "" . __("Duplicate", DUPLICATE_POST_I18N_DOMAIN) . ""; } } } /** * Add a button in the post edit form to create a clone */ add_action( 'edit_form_advanced', 'duplicate_post_add_duplicate_post_button' ); function duplicate_post_add_duplicate_post_button() { if ( isset( $_GET['post'] ) && duplicate_post_is_current_user_allowed_to_create()) { $notifyUrl = "admin.php?action=duplicate_post_save_as_new_post&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 categories of a post to another post */ function duplicate_post_copy_post_categories($id, $new_id) { global $wpdb; if (isset($wpdb->terms)) { // WordPress 2.3 $taxonomies = get_object_taxonomies('post'); //array("category", "post_tag"); foreach ($taxonomies as $taxonomy) { $post_terms = wp_get_object_terms($id, $taxonomy); for ($i=0; $islug, $taxonomy, true); } } } else { $post_categories = $wpdb->get_results("SELECT category_id FROM $wpdb->post2cat WHERE post_id=$id"); if (count($post_categories)!=0) { $sql_query = "INSERT INTO $wpdb->post2cat (post_id, category_id) "; for ($i=0; $icategory_id; if ($iquery($sql_query); } } } /** * 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) "; for ($i=0; $imeta_value); if ($iquery($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 = current_time('mysql'); $new_post_date_gmt = get_gmt_from_date($new_post_date); $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 = str_replace("'", "''", $post->post_title); $post_status = str_replace("'", "''", $post->post_status); $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 categories duplicate_post_copy_post_categories($post->ID, $new_post_id); // 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_date = current_time('mysql'); $new_post_date_gmt = get_gmt_from_date($new_post_date); $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 = 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 ('$post->post_author', '$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 meta information duplicate_post_copy_post_meta_info($post->ID, $new_page_id); return $new_page_id; } ?>