function duplicate_post_options() { if (current_user_can('promote_users') && (isset($_GET['settings-updated']) && $_GET['settings-updated'] == true)) { global $wp_roles; $roles = $wp_roles->get_names(); $dp_roles = get_option('duplicate_post_roles'); if ($dp_roles == "") { $dp_roles = array(); } foreach ($roles as $name => $display_name) { $role = get_role($name); // role should have at least edit_posts capability if (!$role->has_cap('edit_posts')) { continue; } /* If the role doesn't have the capability and it was selected, add it. */ if (!$role->has_cap('copy_posts') && in_array($name, $dp_roles)) { $role->add_cap('copy_posts'); } elseif ($role->has_cap('copy_posts') && !in_array($name, $dp_roles)) { $role->remove_cap('copy_posts'); } } } ?> <div class="wrap"> <div id="icon-options-general" class="icon32"> <br> </div> <h1> <?php _e("Duplicate Post Options", 'duplicate-post'); ?> </h1> <div style="margin: 9px 15px 4px 0; padding: 5px; text-align: center; font-weight: bold; float: left;"> <a href="http://lopo.it/duplicate-post-plugin"><?php _e('Visit plugin site'); ?> </a> - <a href="https://translate.wordpress.org/projects/wp-plugins/duplicate-post"><?php _e('Translate', 'duplicate-post'); ?> </a> - <a href="https://wordpress.org/plugins/duplicate-post/faq/"><?php _e('FAQ', 'duplicate-post'); ?> </a> - <a href="http://lopo.it/duplicate-post-plugin"><?php _e('Donate', 'duplicate-post'); ?> (10¢)</a> <a href="http://lopo.it/duplicate-post-plugin"><img id="donate-button" src="<?php echo plugins_url('donate.png', __FILE__); ?> " alt="Donate"/></a> </div> <script> jQuery(document).on( 'click', '.nav-tab-wrapper a', function() { jQuery('.nav-tab').removeClass('nav-tab-active'); jQuery(this).addClass('nav-tab-active'); jQuery('section').hide(); jQuery('section').eq(jQuery(this).index()).show(); return false; }); function toggle_private_taxonomies(){ jQuery('.taxonomy_private').toggle(); } jQuery(function(){ jQuery('.taxonomy_private').hide(); }); </script> <style> h2.nav-tab-wrapper { margin: 22px 0 0 0; } h2 .nav-tab:focus { color: #555; box-shadow: none; } #sections { padding: 22px; background: #fff; border: 1px solid #ccc; border-top: 0px; } section { display: none; } section:first-of-type { display: block; } .no-js h2.nav-tab-wrapper { display: none; } .no-js #sections { border-top: 1px solid #ccc; margin-top: 22px; } .no-js section { border-top: 1px dashed #aaa; margin-top: 22px; padding-top: 22px; } .no-js section:first-child { margin: 0px; padding: 0px; border: 0px; } label { display: block; } label.taxonomy_private { font-style: italic; } a.toggle_link { font-size: small; } img#donate-button{ vertical-align: middle; } </style> <form method="post" action="options.php" style="clear: both"> <?php settings_fields('duplicate_post_group'); ?> <h2 class="nav-tab-wrapper"> <a class="nav-tab nav-tab-active" href="<?php echo admin_url(); ?> /index.php?page=duplicate-post-what"><?php _e('What to copy', 'duplicate-post'); ?> </a> <a class="nav-tab" href="<?php echo admin_url(); ?> /index.php?page=duplicate-post-who"><?php _e('Permissions', 'duplicate-post'); ?> </a> <a class="nav-tab" href="<?php echo admin_url(); ?> /index.php?page=duplicate-post-where"><?php _e('Display', 'duplicate-post'); ?> </a> </h2> <section> <table class="form-table"> <tr valign="top"> <th scope="row"><?php _e('Post/page elements to copy', 'duplicate-post'); ?> </th> <td colspan="2"><label> <input type="checkbox" name="duplicate_post_copytitle" value="1" <?php if (get_option('duplicate_post_copytitle') == 1) { echo 'checked="checked"'; } ?> "/> <?php _e("Title", 'default'); ?> </label> <label> <input type="checkbox" name="duplicate_post_copydate" value="1" <?php if (get_option('duplicate_post_copydate') == 1) { echo 'checked="checked"'; } ?> "/> <?php _e("Date", 'default'); ?> </label> <label> <input type="checkbox" name="duplicate_post_copystatus" value="1" <?php if (get_option('duplicate_post_copystatus') == 1) { echo 'checked="checked"'; } ?> "/> <?php _e("Status", 'default'); ?> </label> <label> <input type="checkbox" name="duplicate_post_copyslug" value="1" <?php if (get_option('duplicate_post_copyslug') == 1) { echo 'checked="checked"'; } ?> "/> <?php _e("Slug", 'default'); ?> </label> <label> <input type="checkbox" name="duplicate_post_copyexcerpt" value="1" <?php if (get_option('duplicate_post_copyexcerpt') == 1) { echo 'checked="checked"'; } ?> "/> <?php _e("Excerpt", 'default'); ?> </label> <label> <input type="checkbox" name="duplicate_post_copycontent" value="1" <?php if (get_option('duplicate_post_copycontent') == 1) { echo 'checked="checked"'; } ?> "/> <?php _e("Content", 'default'); ?> </label> <label> <input type="checkbox" name="duplicate_post_copypassword" value="1" <?php if (get_option('duplicate_post_copypassword') == 1) { echo 'checked="checked"'; } ?> "/> <?php _e("Password", 'default'); ?> </label> <label> <input type="checkbox" name="duplicate_post_copyattachments" value="1" <?php if (get_option('duplicate_post_copyattachments') == 1) { echo 'checked="checked"'; } ?> "/> <?php _e("Attachments", 'duplicate-post'); ?> </label> <label> <input type="checkbox" name="duplicate_post_copychildren" value="1" <?php if (get_option('duplicate_post_copychildren') == 1) { echo 'checked="checked"'; } ?> "/> <?php _e("Children", 'duplicate-post'); ?> </label> <label> <input type="checkbox" name="duplicate_post_copycomments" value="1" <?php if (get_option('duplicate_post_copycomments') == 1) { echo 'checked="checked"'; } ?> "/> <?php _e("Comments", 'default'); ?> (<?php _e("except pingbacks and trackbacks", 'duplicate-post'); ?> ) </label> </td> </tr> <tr valign="top"> <th scope="row"><?php _e("Title prefix", 'duplicate-post'); ?> </th> <td><input type="text" name="duplicate_post_title_prefix" value="<?php echo get_option('duplicate_post_title_prefix'); ?> " /> </td> <td><span class="description"><?php _e("Prefix to be added before the title, e.g. \"Copy of\" (blank for no prefix)", 'duplicate-post'); ?> </span> </td> </tr> <tr valign="top"> <th scope="row"><?php _e("Title suffix", 'duplicate-post'); ?> </th> <td><input type="text" name="duplicate_post_title_suffix" value="<?php echo get_option('duplicate_post_title_suffix'); ?> " /> </td> <td><span class="description"><?php _e("Suffix to be added after the title, e.g. \"(dup)\" (blank for no suffix)", 'duplicate-post'); ?> </span> </td> </tr> <tr valign="top"> <th scope="row"><?php _e("Increase menu order by", 'duplicate-post'); ?> </th> <td><input type="text" name="duplicate_post_increase_menu_order_by" value="<?php echo get_option('duplicate_post_increase_menu_order_by'); ?> " /> </td> <td><span class="description"><?php _e("Add this number to the original menu order (blank or zero to retain the value)", 'duplicate-post'); ?> </span> </td> </tr> <tr valign="top"> <th scope="row"><?php _e("Do not copy these fields", 'duplicate-post'); ?> </th> <td id="textfield"><input type="text" name="duplicate_post_blacklist" value="<?php echo get_option('duplicate_post_blacklist'); ?> " /></td> <td><span class="description"><?php _e("Comma-separated list of meta fields that must not be copied", 'duplicate-post'); ?> <br /> <small><?php _e("Add <code>_thumbnail_id</code> to prevent featured images to be copied", 'duplicate-post'); ?> </small> </span> </td> </tr> <tr valign="top"> <th scope="row"><?php _e("Do not copy these taxonomies", 'duplicate-post'); ?> <br /> <a class="toggle_link" href="#" onclick="toggle_private_taxonomies();return false;"><?php _e('Show/hide private taxonomies', 'duplicate-post'); ?> </a> </th> <td colspan="2"><?php $taxonomies = get_taxonomies(array(), 'objects'); usort($taxonomies, 'duplicate_post_tax_obj_cmp'); $taxonomies_blacklist = get_option('duplicate_post_taxonomies_blacklist'); if ($taxonomies_blacklist == "") { $taxonomies_blacklist = array(); } foreach ($taxonomies as $taxonomy) { ?> <label class="taxonomy_<?php echo $taxonomy->public ? 'public' : 'private'; ?> "> <input type="checkbox" name="duplicate_post_taxonomies_blacklist[]" value="<?php echo $taxonomy->name; ?> " <?php if (in_array($taxonomy->name, $taxonomies_blacklist)) { echo 'checked="checked"'; } ?> /> <?php echo $taxonomy->labels->name . ' [' . $taxonomy->name . ']'; ?> </label> <?php } ?> <span class="description"><?php _e("Select the taxonomies you don't want to be copied", 'duplicate-post'); ?> </span> </td> </tr> </table> </section> <section> <table class="form-table"> <?php if (current_user_can('promote_users')) { ?> <tr valign="top"> <th scope="row"><?php _e("Roles allowed to copy", 'duplicate-post'); ?> </th> <td><?php global $wp_roles; $roles = $wp_roles->get_names(); foreach ($roles as $name => $display_name) { $role = get_role($name); if (!$role->has_cap('edit_posts')) { continue; } ?> <label> <input type="checkbox" name="duplicate_post_roles[]" value="<?php echo $name; ?> " <?php if ($role->has_cap('copy_posts')) { echo 'checked="checked"'; } ?> /> <?php echo translate_user_role($display_name); ?> </label> <?php } ?> <span class="description"><?php _e("Warning: users will be able to copy all posts, even those of other users", 'duplicate-post'); ?> <br /> <?php _e("Passwords and contents of password-protected posts may become visible to undesired users and visitors", 'duplicate-post'); ?> </span> </td> </tr> <?php } ?> <tr valign="top"> <th scope="row"><?php _e("Enable for these post types", 'duplicate-post'); ?> </th> <td><?php $post_types = get_post_types(array('show_ui' => true), 'objects'); foreach ($post_types as $post_type_object) { if ($post_type_object->name == 'attachment') { continue; } ?> <label> <input type="checkbox" name="duplicate_post_types_enabled[]" value="<?php echo $post_type_object->name; ?> " <?php if (duplicate_post_is_post_type_enabled($post_type_object->name)) { echo 'checked="checked"'; } ?> /> <?php echo $post_type_object->labels->name; ?> </label> <?php } ?> <span class="description"><?php _e("Select the post types you want the plugin to be enabled", 'duplicate-post'); ?> <br /> <?php _e("Whether the links are displayed for custom post types registered by themes or plugins depends on their use of standard WordPress UI elements", 'duplicate-post'); ?> </span> </td> </tr> </table> </section> <section> <table class="form-table"> <tr valign="top"> <th scope="row"><?php _e("Show links in", 'duplicate-post'); ?> </th> <td><label><input type="checkbox" name="duplicate_post_show_row" value="1" <?php if (get_option('duplicate_post_show_row') == 1) { echo 'checked="checked"'; } ?> "/> <?php _e("Post list", 'duplicate-post'); ?> </label> <label><input type="checkbox" name="duplicate_post_show_submitbox" value="1" <?php if (get_option('duplicate_post_show_submitbox') == 1) { echo 'checked="checked"'; } ?> "/> <?php _e("Edit screen", 'duplicate-post'); ?> </label> <label><input type="checkbox" name="duplicate_post_show_adminbar" value="1" <?php if (get_option('duplicate_post_show_adminbar') == 1) { echo 'checked="checked"'; } ?> "/> <?php _e("Admin bar", 'duplicate-post'); ?> </label> </td> </tr> <tr valign="top"> <td colspan="2"><span class="description"><?php _e("Whether the links are displayed for custom post types registered by themes or plugins depends on their use of standard WordPress UI elements", 'duplicate-post'); ?> <br /> <?php printf(__('You can also use the template tag duplicate_post_clone_post_link( $link, $before, $after, $id ). More info <a href="%s">here</a>', 'duplicate-post'), 'https://wordpress.org/plugins/duplicate-post/other_notes/'); ?> </span> </td> </tr> </table> </section> <p class="submit"> <input type="submit" class="button-primary" value="<?php _e('Save Changes', 'duplicate-post'); ?> " /> </p> </form> </div> <?php }
/** * Create a duplicate from a post */ function duplicate_post_create_duplicate($post, $status = '', $parent_id = '') { if (!duplicate_post_is_post_type_enabled($post->post_type) && $post->post_type != 'attachment') { wp_die(__('Copy features for this post type are not enabled in options page', 'duplicate-post')); } if ($post->post_type != 'attachment') { $prefix = sanitize_text_field(get_option('duplicate_post_title_prefix')); $suffix = sanitize_text_field(get_option('duplicate_post_title_suffix')); $title = ' '; if (get_option('duplicate_post_copytitle') == 1) { $title = $post->post_title; if (!empty($prefix)) { $prefix .= " "; } if (!empty($suffix)) { $suffix = " " . $suffix; } } else { $title = ' '; } $title = trim($prefix . $title . $suffix); if ($title == '') { // empty title $title = __('Untitled'); } if (get_option('duplicate_post_copystatus') == 0) { $status = 'draft'; } } $new_post_author = wp_get_current_user(); $menu_order = $post->menu_order; $increase_menu_order_by = get_option('duplicate_post_increase_menu_order_by'); if (!empty($increase_menu_order_by) && is_numeric($increase_menu_order_by)) { $menu_order += intval($increase_menu_order_by); } $new_post = array('menu_order' => $menu_order, 'comment_status' => $post->comment_status, 'ping_status' => $post->ping_status, 'post_author' => $new_post_author->ID, 'post_content' => get_option('duplicate_post_copycontent') == '1' ? addslashes($post->post_content) : "", 'post_content_filtered' => get_option('duplicate_post_copycontent') == '1' ? addslashes($post->post_content_filtered) : "", 'post_excerpt' => get_option('duplicate_post_copyexcerpt') == '1' ? addslashes($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' => get_option('duplicate_post_copypassword') == '1' ? $post->post_password : "", 'post_status' => $new_post_status = empty($status) ? $post->post_status : $status, 'post_title' => addslashes($title), 'post_type' => $post->post_type); if (get_option('duplicate_post_copydate') == 1) { $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); // If the copy is published or scheduled, we have to set a proper slug. if ($new_post_status == 'publish' || $new_post_status == 'future') { $post_name = $post->post_name; if (get_option('duplicate_post_copyslug') != 1) { $post_name = ''; } $post_name = wp_unique_post_slug($post_name, $new_post_id, $new_post_status, $post->post_type, $new_post_parent); $new_post = array(); $new_post['ID'] = $new_post_id; $new_post['post_name'] = $post_name; // Update the post into the database wp_update_post($new_post); } // If you have written a plugin which uses non-WP database tables to save // information about a post you can hook this action to dupe that data. if ($post->post_type == 'page' || function_exists('is_post_type_hierarchical') && is_post_type_hierarchical($post->post_type)) { do_action('dp_duplicate_page', $new_post_id, $post); } else { do_action('dp_duplicate_post', $new_post_id, $post); } delete_post_meta($new_post_id, '_dp_original'); add_post_meta($new_post_id, '_dp_original', $post->ID); return $new_post_id; }
function duplicate_post_add_css() { $current_object = get_queried_object(); if (empty($current_object)) { return; } if (!empty($current_object->post_type) && ($post_type_object = get_post_type_object($current_object->post_type)) && duplicate_post_is_current_user_allowed_to_copy() && ($post_type_object->show_ui || 'attachment' == $current_object->post_type) && duplicate_post_is_post_type_enabled($current_object->post_type)) { wp_enqueue_style('duplicate-post', plugins_url('/duplicate-post.css', __FILE__)); } }