Plugin Directory

source: accordions/tags/2.2.96/includes/duplicate-post.php

Last change on this file was 2831444, checked in by pickplugins, 2 years ago

version update

File size: 3.8 KB
Line 
1<?php
2
3
4
5if (!defined('ABSPATH')) exit;  // if direct access
6
7
8
9function accordions_duplicate_post_as_draft()
10{
11        global $wpdb;
12        if (!(isset($_GET['post']) || isset($_POST['post'])  || (isset($_REQUEST['action']) && 'accordions_duplicate_post_as_draft' == $_REQUEST['action']))) {
13                wp_die('No post to duplicate has been supplied!');
14        }
15
16        /*
17         * Nonce verification
18         */
19        if (!isset($_GET['duplicate_nonce']) || !wp_verify_nonce($_GET['duplicate_nonce'], basename(__FILE__)))
20                return;
21
22        /*
23         * get the original post id
24         */
25        $post_id = (isset($_GET['post']) ? absint($_GET['post']) : absint($_POST['post']));
26        /*
27         * and all the original post data then
28         */
29        $post = get_post($post_id);
30
31        /*
32         * if you don't want current user to be the new post author,
33         * then change next couple of lines to this: $new_post_author = $post->post_author;
34         */
35        $current_user = wp_get_current_user();
36        $new_post_author = $current_user->ID;
37
38        /*
39         * if post data exists, create the post duplicate
40         */
41        if (isset($post) && $post != null) {
42
43                /*
44                 * new post data array
45                 */
46                $args = array(
47                        'comment_status' => $post->comment_status,
48                        'ping_status'    => $post->ping_status,
49                        'post_author'    => $new_post_author,
50                        'post_content'   => $post->post_content,
51                        'post_excerpt'   => $post->post_excerpt,
52                        'post_name'      => $post->post_name,
53                        'post_parent'    => $post->post_parent,
54                        'post_password'  => $post->post_password,
55                        'post_status'    => 'draft',
56                        'post_title'     => $post->post_title . ' - Copy of #' . $post_id,
57                        'post_type'      => $post->post_type,
58                        'to_ping'        => $post->to_ping,
59                        'menu_order'     => $post->menu_order
60                );
61
62                /*
63                 * insert the post by wp_insert_post() function
64                 */
65                $new_post_id = wp_insert_post($args);
66
67                /*
68                 * get all current post terms ad set them to the new post draft
69                 */
70                $taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
71                foreach ($taxonomies as $taxonomy) {
72                        $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
73                        wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
74                }
75
76                /*
77                 * duplicate all post meta just in two SQL queries
78                 */
79                // Copy post metadata
80                $data = get_post_custom($post_id);
81                foreach ($data as $key => $values) {
82                        foreach ($values as $value) {
83                                add_post_meta($new_post_id, $key, maybe_unserialize($value)); // it is important to unserialize data to avoid conflicts.
84                        }
85                }
86
87                $accordions_options = get_post_meta($post_id, 'accordions_options', true);
88
89                if (!empty($accordions_options['content'])) {
90
91                        $content = [];
92                        $now = time();
93                        $i = 0;
94                        foreach ($accordions_options['content'] as $index => $item) {
95
96                                $content[$now . $i] = $item;
97                                $i++;
98                        }
99
100
101                        $accordions_options['content'] = $content;
102
103                        update_post_meta($post_id, 'accordions_options', $accordions_options);
104                }
105
106
107
108
109
110
111                /*
112                 * finally, redirect to the edit post screen for the new draft
113                 */
114                wp_redirect(admin_url('post.php?action=edit&post=' . $new_post_id));
115                exit;
116        } else {
117                wp_die('Post creation failed, could not find original post: ' . $post_id);
118        }
119}
120add_action('admin_action_accordions_duplicate_post_as_draft', 'accordions_duplicate_post_as_draft');
121
122/*
123 * Add the duplicate link to action list for post_row_actions
124 */
125function accordions_duplicate_post_link($actions, $post)
126{
127
128
129        if (current_user_can('edit_posts') && $post->post_type == 'accordions') {
130                $actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=accordions_duplicate_post_as_draft&post=' . $post->ID, basename(__FILE__), 'duplicate_nonce') . '" title="Duplicate this item" rel="permalink">Duplicate</a>';
131        }
132        return $actions;
133}
134
135add_filter('post_row_actions', 'accordions_duplicate_post_link', 10, 2);
Note: See TracBrowser for help on using the repository browser.