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

242 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace PXLBSAdminify\Inc\Modules\PostDuplicator;
4
5 use PXLBSAdminify\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('pxlbsadminify_wl_plugin_menu_label');
33
34 $this->options = (array) AdminSettings::get_instance()->get();
35
36 // Taxonomy duplicator (reads post_duplicator.taxonomies) is a
37 // Pro-only feature. Pro/Classes/Filters.php registers the
38 // admin_action and *_row_actions filters when the Pro plugin
39 // is active.
40
41 // Check Access for User roles
42 add_action('admin_init', [$this, 'post_duplicator_init']);
43 }
44
45 public function post_duplicator_init()
46 {
47 $post_types = [];
48 if (!empty($this->options['post_duplicator']['post_types'])) {
49 $post_types = $this->options['post_duplicator']['post_types'];
50 }
51 $all_post_types = get_post_types();
52
53 if (empty($post_types) || empty(array_intersect($all_post_types, $post_types))) {
54 return;
55 }
56
57 add_action('admin_action_adminify_duplicate', [$this, 'duplicate_post_as_draft']);
58 add_filter('post_row_actions', [$this, 'duplicator_row_actions'], 10, 2);
59 add_filter('page_row_actions', [$this, 'duplicator_row_actions'], 10, 2);
60
61
62 // foreach ($post_types as $key => $post_type) {
63 // if (in_array($post_type, $all_post_types)) {
64 // add_filter($post_type . '_row_actions', [$this, 'duplicator_row_actions'], 10, 2);
65 // }
66 // }
67 }
68
69
70 /**
71 * Check if current user can clone
72 *
73 * @return bool
74 */
75 public function user_can_clone($post)
76 {
77 if (!current_user_can('edit_posts')) {
78 return false;
79 }
80
81 if (current_user_can('editor') || current_user_can('administrator')) {
82 return true;
83 }
84
85 if (current_user_can('contributor') || current_user_can('author')) {
86 $get_current_user_id = get_current_user_id();
87 $post = get_post($post);
88 $author_id = $post->post_author;
89 if ($get_current_user_id == $author_id) {
90 return true;
91 }
92 }
93 return false;
94 }
95
96
97 /*
98 * Add the duplicate link to action list for post_row_actions
99 */
100 public function duplicator_row_actions($actions, $post)
101 {
102 $post_type = $post->post_type;
103 $allowed_post_types = $post_types = $this->options['post_duplicator']['post_types'];
104 if( !in_array($post_type, $allowed_post_types)){
105 return $actions;
106 }
107 if (!$this->user_can_clone($post)) {
108 return $actions;
109 }
110
111 $adminify_duplicate_link = admin_url('admin.php?action=adminify_duplicate&post=' . $post->ID);
112 $adminify_duplicate_link = wp_nonce_url($adminify_duplicate_link, 'pxlbsadminify_post_duplicator_nonce');
113
114 $clone_title = !empty($this->clone_title) ? $this->clone_title : 'Adminify';
115 $clone_title = preg_replace('/wp/i', '', $clone_title) . ' Clone';
116 $duplicate_title = 'Duplicate this item ' . $post->post_title;
117 /* translators: 1: Duplicate post URL, 2: Link title attribute, 3: Link text */
118 $actions['adminify_duplicate'] = sprintf(__('<a href="%1$s" title="%2$s" rel="permalink">%3$s</a>', 'adminify'), $adminify_duplicate_link, $duplicate_title, $clone_title);
119 return $actions;
120 }
121
122
123 /*
124 * Function creates post duplicate as a draft and redirects then to the edit post screen
125 */
126 public function duplicate_post_as_draft()
127 {
128 global $wpdb;
129
130 if (!(isset($_GET['post']) || isset($_POST['post']) || (isset($_REQUEST['action']) && 'adminify_duplicate' == $_REQUEST['action']))) {
131 wp_die('No post to duplicate has been supplied!');
132 }
133
134 /*
135 * Nonce verification
136 * Return if nonce is not valid
137 */
138 if (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])), 'pxlbsadminify_post_duplicator_nonce')) {
139 return;
140 }
141
142 /*
143 * get the original post id
144 */
145 $post_id = (isset($_GET['post']) ? absint($_GET['post']) : absint($_POST['post']));
146 /*
147 * and all the original post data then
148 */
149 $post = get_post($post_id);
150 if( isset( $_SERVER['HTTP_REFERER']) && str_contains( esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ), 'action=adminify_duplicate' )) {
151 $redirect_url = admin_url('edit.php?post_type=' . $post->post_type);
152 wp_safe_redirect($redirect_url);
153
154 // exit;
155 }
156
157 if (!$this->user_can_clone($post)) {
158 return;
159 }
160
161 /*
162 * if post data exists, create the post duplicate
163 */
164 if (isset($post) && $post != null) {
165
166 /*
167 * new post data array
168 */
169 $args = [
170 'post_title' => $post->post_title,
171 'post_type' => $post->post_type,
172 'post_content' => $post->post_content,
173 'post_excerpt' => $post->post_excerpt,
174 'post_author' => get_current_user_id(),
175 'comment_status' => $post->comment_status,
176 'ping_status' => $post->ping_status,
177 'post_name' => $post->post_name,
178 'post_parent' => $post->post_parent,
179 'post_password' => $post->post_password,
180 'post_status' => 'draft',
181 'to_ping' => $post->to_ping,
182 'menu_order' => $post->menu_order,
183 ];
184
185 /*
186 * insert the post by wp_insert_post() function
187 */
188 $new_post_id = wp_insert_post($args);
189
190 /*
191 * get all current post terms ad set them to the new post draft
192 */
193 $taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
194 foreach ($taxonomies as $taxonomy) {
195 $post_terms = wp_get_object_terms($post_id, $taxonomy, ['fields' => 'slugs']);
196 wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
197 }
198
199 /*
200 * duplicate all post meta just in two SQL queries
201 */
202 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- direct query required for copying all post meta rows; not cached intentionally.
203 $post_meta_infos = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=%d", $post_id));
204 if (count($post_meta_infos) != 0) {
205 $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
206 foreach ($post_meta_infos as $meta_info) {
207 $meta_key = $meta_info->meta_key;
208
209 // Do not copy these values
210 if ($meta_key == '_wp_old_slug') {
211 continue;
212 }
213 if ($meta_key == '_elementor_css') {
214 continue;
215 }
216
217 // Delete already added meta data triggered by wp_insert_post hook
218 if ($meta_key == '_elementor_template_type') {
219 delete_post_meta($new_post_id, '_elementor_template_type');
220 }
221
222 $meta_value = $meta_info->meta_value;
223 $sql_query_sel[] = $wpdb->prepare('SELECT %d, %s, %s', $new_post_id, $meta_key, $meta_value);
224 }
225 $sql_query .= implode(' UNION ALL ', $sql_query_sel);
226 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,PluginCheck.Security.DirectDB.UnescapedDBParameter -- $sql_query is built from $wpdb->prepare() fragments above; direct query required for bulk meta insert, not cached intentionally.
227 $wpdb->query($sql_query);
228 }
229
230 /*
231 * finally, redirect to the edit post screen for the new draft
232 */
233 $redirect_url = admin_url('edit.php?post_type=' . $post->post_type);
234 wp_safe_redirect($redirect_url);
235
236 exit;
237 } else {
238 wp_die('Post creation failed, could not find original post: ' . absint($post_id));
239 }
240 }
241 }
242