PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 2.0.3
Adminify – White Label, Admin Menu Editor, Login Customizer v2.0.3
4.3.1 4.3.0 4.2.26 4.2.25 4.2.24 4.2.23 4.2.22 4.2.21 4.2.20 4.2.19 4.2.18 4.2.17 4.2.16 4.2.15 4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 4.1.17 All 164 releases
adminify / Inc / Modules / PostDuplicator / PostDuplicator.php

PostDuplicator.php in Adminify – White Label, Admin Menu Editor, Login Customizer 2.0.3, at Inc/Modules/PostDuplicator/PostDuplicator.php

149 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPAdminify\Inc\Modules\PostDuplicator;
4
5 use WPAdminify\Inc\Utils;
6 // no direct access allowed
7 if (!defined('ABSPATH')) exit;
8
9 /**
10 * WP Adminify
11 * @package Duplicate Post/Page/Custom Posts
12 *
13 * @author Jewel Theme <support@jeweltheme.com>
14 */
15
16
17 if (!defined('ABSPATH')) exit; // If this file is called directly, abort.
18
19 class PostDuplicator
20 {
21
22 public function __construct()
23 {
24 add_action('admin_action_adminify_duplicate', [$this, 'adminify_duplicate_post_as_draft']);
25 add_filter('post_row_actions', [$this, 'jltma_duplicator_row_actions'], 10, 2);
26 add_filter('page_row_actions', [$this, 'jltma_duplicator_row_actions'], 10, 2);
27 }
28
29
30 /*
31 * Add the duplicate link to action list for post_row_actions
32 */
33 public function jltma_duplicator_row_actions($actions, $post)
34 {
35 if (current_user_can('edit_posts')) {
36
37 $adminify_duplicate_link = admin_url('admin.php?action=adminify_duplicate&post=' . $post->ID);
38 $adminify_duplicate_link = wp_nonce_url($adminify_duplicate_link, 'jltma_post_duplicator_nonce');
39
40 $actions['adminify_duplicate'] = sprintf('<a href="%s" title="%s" rel="permalink">%s</a>', $adminify_duplicate_link, __('Duplicate this item ' . $post->post_title, WP_ADMINIFY_TD), esc_html__('Adminify Clone', WP_ADMINIFY_TD));
41 }
42 return $actions;
43 }
44
45
46 /*
47 * Function creates post duplicate as a draft and redirects then to the edit post screen
48 */
49 public function adminify_duplicate_post_as_draft()
50 {
51
52 global $wpdb;
53
54 if (!(isset($_GET['post']) || isset($_POST['post']) || (isset($_REQUEST['action']) && 'adminify_duplicate' == $_REQUEST['action']))) {
55 wp_die('No post to duplicate has been supplied!');
56 }
57
58 /*
59 * Nonce verification
60 * Return if nonce is not valid
61 */
62 if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'jltma_post_duplicator_nonce')) {
63 return;
64 }
65
66 /*
67 * get the original post id
68 */
69 $post_id = (isset($_GET['post']) ? absint($_GET['post']) : absint($_POST['post']));
70 /*
71 * and all the original post data then
72 */
73 $post = get_post($post_id);
74
75 /*
76 * if you don't want current user to be the new post author,
77 * then change next couple of lines to this: $new_post_author = $post->post_author;
78 */
79 $current_user = wp_get_current_user();
80 $new_post_author = $current_user->ID;
81
82 /*
83 * if post data exists, create the post duplicate
84 */
85 if (isset($post) && $post != null) {
86
87 /*
88 * new post data array
89 */
90 $args = array(
91 'post_title' => $post->post_title,
92 'post_type' => $post->post_type,
93 'post_content' => $post->post_content,
94 'post_excerpt' => $post->post_excerpt,
95 'post_author' => $new_post_author,
96 'comment_status' => $post->comment_status,
97 'ping_status' => $post->ping_status,
98 'post_name' => $post->post_name,
99 'post_parent' => $post->post_parent,
100 'post_password' => $post->post_password,
101 'post_status' => 'draft',
102 'to_ping' => $post->to_ping,
103 'menu_order' => $post->menu_order
104 );
105
106 /*
107 * insert the post by wp_insert_post() function
108 */
109 $new_post_id = wp_insert_post($args);
110
111 /*
112 * get all current post terms ad set them to the new post draft
113 */
114 $taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
115 foreach ($taxonomies as $taxonomy) {
116 $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
117 wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
118 }
119
120 /*
121 * duplicate all post meta just in two SQL queries
122 */
123 $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
124 if (count($post_meta_infos) != 0) {
125 $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
126 foreach ($post_meta_infos as $meta_info) {
127 $meta_key = $meta_info->meta_key;
128 if ($meta_key == '_wp_old_slug') continue;
129 $meta_value = addslashes($meta_info->meta_value);
130 $sql_query_sel[] = "SELECT $new_post_id, '$meta_key', '$meta_value'";
131 }
132 $sql_query .= implode(" UNION ALL ", $sql_query_sel);
133 $wpdb->query($sql_query);
134 }
135
136
137 /*
138 * finally, redirect to the edit post screen for the new draft
139 */
140 $redirect_url = admin_url('edit.php?post_type=' . $post->post_type);
141 wp_safe_redirect($redirect_url);
142
143 exit;
144 } else {
145 wp_die('Post creation failed, could not find original post: ' . $post_id);
146 }
147 }
148 }
149