PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 4.0.2.5
Adminify – White Label, Admin Menu Editor, Login Customizer v4.0.2.5
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 4.0.2.5, at Inc/Modules/PostDuplicator/PostDuplicator.php

227 lines 6.3 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\Admin\AdminSettings;
6 // no direct access allowed
7 if (!defined('ABSPATH')) {
8 exit;
9 }
10
11 /**
12 * WP Adminify
13 *
14 * @package Duplicate Post/Page/Custom Posts
15 *
16 * @author Jewel Theme <support@jeweltheme.com>
17 */
18
19
20 if (!defined('ABSPATH')) {
21 exit; // If this file is called directly, abort.
22 }
23
24 class PostDuplicator
25 {
26
27 private $clone_title;
28 public $options = [];
29
30 public function __construct()
31 {
32 $this->clone_title = AdminSettings::get_instance()->get('jltwp_adminify_wl_plugin_menu_label');
33
34 $this->options = (array) AdminSettings::get_instance()->get();
35
36 new TaxonomyDuplicator();
37
38 // Check Access for User roles
39 add_action('admin_init', [$this, 'adminify_post_duplicator_init']);
40 }
41
42 public function adminify_post_duplicator_init()
43 {
44 $post_types = [];
45 if (!empty($this->options['post_duplicator']['post_types'])) {
46 $post_types = $this->options['post_duplicator']['post_types'];
47 }
48 $all_post_types = get_post_types();
49
50 if (empty($post_types) || empty(array_intersect($all_post_types, $post_types))) {
51 return;
52 }
53
54 add_action('admin_action_adminify_duplicate', [$this, 'adminify_duplicate_post_as_draft']);
55 foreach ($post_types as $key => $post_type) {
56 if (in_array($post_type, $all_post_types)) {
57 add_filter($post_type . '_row_actions', [$this, 'jltwp_adminify_duplicator_row_actions'], 10, 2);
58 }
59 }
60 }
61
62
63 /**
64 * Check if current user can clone
65 *
66 * @return bool
67 */
68 public function user_can_clone($post)
69 {
70 if (!current_user_can('edit_posts')) {
71 return false;
72 }
73
74 if (current_user_can('editor') || current_user_can('administrator')) {
75 return true;
76 }
77
78 if (current_user_can('contributor') || current_user_can('author')) {
79 $get_current_user_id = get_current_user_id();
80 $post = get_post($post);
81 $author_id = $post->post_author;
82 if ($get_current_user_id == $author_id) {
83 return true;
84 }
85 }
86 return false;
87 }
88
89
90 /*
91 * Add the duplicate link to action list for post_row_actions
92 */
93 public function jltwp_adminify_duplicator_row_actions($actions, $post)
94 {
95 if (!$this->user_can_clone($post)) {
96 return $actions;
97 }
98
99 $adminify_duplicate_link = admin_url('admin.php?action=adminify_duplicate&post=' . $post->ID);
100 $adminify_duplicate_link = wp_nonce_url($adminify_duplicate_link, 'jltwp_adminify_post_duplicator_nonce');
101
102 $clone_title = !empty($this->clone_title) ? $this->clone_title : 'Adminify';
103 $clone_title = preg_replace('/wp/i', '', $clone_title) . ' Clone';
104 $duplicate_title = 'Duplicate this item ' . $post->post_title;
105 $actions['adminify_duplicate'] = sprintf(__('<a href="%1$s" title="%2$s" rel="permalink">%3$s</a>', 'adminify'), $adminify_duplicate_link, $duplicate_title, $clone_title);
106 return $actions;
107 }
108
109
110 /*
111 * Function creates post duplicate as a draft and redirects then to the edit post screen
112 */
113 public function adminify_duplicate_post_as_draft()
114 {
115 global $wpdb;
116
117 if (!(isset($_GET['post']) || isset($_POST['post']) || (isset($_REQUEST['action']) && 'adminify_duplicate' == $_REQUEST['action']))) {
118 wp_die('No post to duplicate has been supplied!');
119 }
120
121 /*
122 * Nonce verification
123 * Return if nonce is not valid
124 */
125 if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])), 'jltwp_adminify_post_duplicator_nonce')) {
126 return;
127 }
128
129 /*
130 * get the original post id
131 */
132 $post_id = (isset($_GET['post']) ? absint($_GET['post']) : absint($_POST['post']));
133 /*
134 * and all the original post data then
135 */
136 $post = get_post($post_id);
137 if( isset( $_SERVER['HTTP_REFERER']) && str_contains( $_SERVER['HTTP_REFERER'], 'action=adminify_duplicate' )) {
138 $redirect_url = admin_url('edit.php?post_type=' . $post->post_type);
139 wp_safe_redirect($redirect_url);
140
141 exit;
142 }
143
144 if (!$this->user_can_clone($post)) {
145 return;
146 }
147
148 /*
149 * if post data exists, create the post duplicate
150 */
151 if (isset($post) && $post != null) {
152
153 /*
154 * new post data array
155 */
156 $args = [
157 'post_title' => $post->post_title,
158 'post_type' => $post->post_type,
159 'post_content' => $post->post_content,
160 'post_excerpt' => $post->post_excerpt,
161 'post_author' => get_current_user_id(),
162 'comment_status' => $post->comment_status,
163 'ping_status' => $post->ping_status,
164 'post_name' => $post->post_name,
165 'post_parent' => $post->post_parent,
166 'post_password' => $post->post_password,
167 'post_status' => 'draft',
168 'to_ping' => $post->to_ping,
169 'menu_order' => $post->menu_order,
170 ];
171
172 /*
173 * insert the post by wp_insert_post() function
174 */
175 $new_post_id = wp_insert_post($args);
176
177 /*
178 * get all current post terms ad set them to the new post draft
179 */
180 $taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
181 foreach ($taxonomies as $taxonomy) {
182 $post_terms = wp_get_object_terms($post_id, $taxonomy, ['fields' => 'slugs']);
183 wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
184 }
185
186 /*
187 * duplicate all post meta just in two SQL queries
188 */
189 $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
190 if (count($post_meta_infos) != 0) {
191 $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
192 foreach ($post_meta_infos as $meta_info) {
193 $meta_key = $meta_info->meta_key;
194
195 // Do not copy these values
196 if ($meta_key == '_wp_old_slug') {
197 continue;
198 }
199 if ($meta_key == '_elementor_css') {
200 continue;
201 }
202
203 // Delete already added meta data triggered by wp_insert_post hook
204 if ($meta_key == '_elementor_template_type') {
205 delete_post_meta($new_post_id, '_elementor_template_type');
206 }
207
208 $meta_value = $meta_info->meta_value;
209 $sql_query_sel[] = $wpdb->prepare('SELECT %d, %s, %s', $new_post_id, $meta_key, $meta_value);
210 }
211 $sql_query .= implode(' UNION ALL ', $sql_query_sel);
212 $wpdb->query($sql_query);
213 }
214
215 /*
216 * finally, redirect to the edit post screen for the new draft
217 */
218 $redirect_url = admin_url('edit.php?post_type=' . $post->post_type);
219 wp_safe_redirect($redirect_url);
220
221 exit;
222 } else {
223 wp_die('Post creation failed, could not find original post: ' . absint($post_id));
224 }
225 }
226 }
227