PluginProbe
Post Grid / trunk
Post Grid vtrunk
2.3.23 2.2.57 2.2.58 2.2.59 2.2.6 2.2.60 2.2.61 2.2.62 2.2.63 2.2.63a 2.2.64 2.2.65 2.2.66 2.2.67 2.2.67-a 2.2.68 2.2.69 2.2.7 2.2.70 2.2.71 2.2.72 2.2.73 2.2.74 2.2.75 2.2.75a All 208 releases
post-grid / includes / duplicate-post.php

duplicate-post.php in Post Grid trunk, at includes/duplicate-post.php

93 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) exit; // if direct access
3 function post_grid_duplicate_post_as_draft()
4 {
5 global $wpdb;
6 if (!(isset($_GET['post']) || isset($_POST['post']) || (isset($_REQUEST['action']) && 'post_grid_duplicate_post_as_draft' == $_REQUEST['action']))) {
7 wp_die('No post to duplicate has been supplied!');
8 }
9 /*
10 * Nonce verification
11 */
12 if (!isset($_GET['duplicate_nonce']) || !wp_verify_nonce(wp_unslash($_GET['duplicate_nonce']), basename(__FILE__)))
13 return;
14 /*
15 * get the original post id
16 */
17 $post_id = (isset($_GET['post']) ? absint($_GET['post']) : absint($_POST['post']));
18 /*
19 * and all the original post data then
20 */
21 $post = get_post($post_id);
22 /*
23 * if you don't want current user to be the new post author,
24 * then change next couple of lines to this: $new_post_author = $post->post_author;
25 */
26 $current_user = wp_get_current_user();
27 $new_post_author = $current_user->ID;
28 /*
29 * if post data exists, create the post duplicate
30 */
31 if (isset($post) && $post != null) {
32 /*
33 * new post data array
34 */
35 $args = array(
36 'comment_status' => $post->comment_status,
37 'ping_status' => $post->ping_status,
38 'post_author' => $new_post_author,
39 'post_content' => $post->post_content,
40 'post_excerpt' => $post->post_excerpt,
41 'post_name' => $post->post_name,
42 'post_parent' => $post->post_parent,
43 'post_password' => $post->post_password,
44 'post_status' => 'draft',
45 'post_title' => $post->post_title . ' - Copy of #' . $post_id,
46 'post_type' => $post->post_type,
47 'to_ping' => $post->to_ping,
48 'menu_order' => $post->menu_order
49 );
50 /*
51 * insert the post by wp_insert_post() function
52 */
53 $new_post_id = wp_insert_post($args);
54 /*
55 * get all current post terms ad set them to the new post draft
56 */
57 $taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
58 foreach ($taxonomies as $taxonomy) {
59 $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
60 wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
61 }
62 /*
63 * duplicate all post meta just in two SQL queries
64 */
65 // Copy post metadata
66 $data = get_post_custom($post_id);
67 foreach ($data as $key => $values) {
68 foreach ($values as $value) {
69 add_post_meta($new_post_id, $key, maybe_unserialize($value)); // it is important to unserialize data to avoid conflicts.
70 }
71 }
72 /*
73 * finally, redirect to the edit post screen for the new draft
74 */
75 wp_safe_redirect(admin_url('post.php?action=edit&post=' . $new_post_id));
76 exit;
77 } else {
78 wp_die('Post creation failed, could not find original post: ' . wp_kses_post($post_id));
79 }
80 }
81 add_action('admin_action_post_grid_duplicate_post_as_draft', 'post_grid_duplicate_post_as_draft');
82 /*
83 * Add the duplicate link to action list for post_row_actions
84 */
85 function post_grid_duplicate_post_link($actions, $post)
86 {
87 if (current_user_can('edit_posts') && ($post->post_type == 'post_grid' || $post->post_type == 'post_grid_template' || $post->post_type == 'post_grid_layout')) {
88 $actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=post_grid_duplicate_post_as_draft&post=' . $post->ID, basename(__FILE__), 'duplicate_nonce') . '" title="Duplicate this item" rel="permalink">Duplicate</a>';
89 }
90 return $actions;
91 }
92 add_filter('post_row_actions', 'post_grid_duplicate_post_link', 10, 2);
93