'; 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; $i