PluginProbe
Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets / 4.2.3
Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets v4.2.3
4.5.4 4.2.1 4.2.2 4.2.3 4.5.0 4.5.2 4.5.3 4.2.0 4.1.18 4.1.17 4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.0.9 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 All 146 releases
ultimate-post-kit / includes / class-duplicator.php

class-duplicator.php in Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets 4.2.3, at includes/class-duplicator.php

200 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace UltimatePostKit\Includes;
4
5 use Elementor\Core\Files\CSS\Post as Post_CSS;
6
7 if (!defined('ABSPATH')) {
8 exit;
9 }
10 // Exit if accessed directly
11
12 /**
13 * Duplicator Class
14 */
15
16 if (!class_exists('BdThemes_Duplicator')) :
17 class BdThemes_Duplicator {
18
19 public function __construct() {
20 add_action('admin_action_bdt_duplicate_as_draft', [$this, 'bdt_duplicate_as_draft']);
21 add_filter('post_row_actions', [$this, 'bdt_duplicate_post_link'], 10, 2);
22 add_filter('page_row_actions', [$this, 'bdt_duplicate_post_link'], 10, 2);
23 }
24
25 public function bdt_duplicate_as_draft() {
26
27 if (!current_user_can('edit_posts')) {
28 wp_die('You don\'t have permission to duplicate it; please go back!');
29 }
30
31 if (!(isset($_GET['post']) || isset($_POST['post']) || (isset($_REQUEST['action']) && 'bdt_duplicate_as_draft' == $_REQUEST['action']))) {
32 wp_die('No post to duplicate has been supplied!');
33 }
34
35 /**
36 * Nonce verification
37 */
38 if (!isset($_GET['duplicate_nonce']) || !wp_verify_nonce($_GET['duplicate_nonce'], basename(__FILE__))) {
39 return;
40 }
41
42 /**
43 * get the original post id
44 */
45 $post_id = (isset($_GET['post']) ? absint($_GET['post']) : absint($_POST['post']));
46 /**
47 * and all the original post data then
48 */
49 $post = get_post($post_id);
50
51 /**
52 * if you don't want current user to be the new post author,
53 */
54 $current_user_id = get_current_user_id();
55
56 if (current_user_can('manage_options') || current_user_can('edit_others_posts')) {
57 $this->duplicate_edit_post($post_id);
58 } else if (current_user_can('edit_posts') && $post->post_author == $current_user_id) {
59 $this->duplicate_edit_post($post_id);
60 } else {
61 wp_die('You don\'t have permission to duplicate it; please go back!');
62 }
63 }
64
65 /**
66 * duplicate edit post
67 */
68 public function duplicate_edit_post($post_id) {
69 global $wpdb;
70 /**
71 * and all the original post data then
72 */
73 $bdt_post = get_post($post_id);
74 /**
75 * if you don't want current user to be the new post author,
76 * then change next couple of lines to this: $new_post_author = $post->post_author;
77 */
78 $bdt_current_user = wp_get_current_user();
79 $bdt_new_post_author = $bdt_current_user->ID;
80
81 /**
82 * if post data exists, create the post duplicate
83 */
84 if (isset($bdt_post) && $bdt_post != null) {
85 /**
86 * new post data array
87 */
88 $bdt_args = [
89 'post_status' => 'draft',
90 /* translators: %1$s post title */
91 'post_title' => sprintf(__('%1$s - [Duplicated]', 'ultimate-post-kit'), $bdt_post->post_title),
92 'post_type' => $bdt_post->post_type,
93 'post_name' => $bdt_post->post_name,
94 'post_content' => $bdt_post->post_content,
95 'post_excerpt' => $bdt_post->post_excerpt,
96 'post_author' => $bdt_new_post_author,
97 'post_parent' => $bdt_post->post_parent,
98 'post_password' => $bdt_post->post_password,
99 'comment_status' => $bdt_post->comment_status,
100 'ping_status' => $bdt_post->ping_status,
101 'menu_order' => $bdt_post->menu_order,
102 'to_ping' => $bdt_post->to_ping,
103 ];
104
105 /**
106 * insert the post by wp_insert_post() function
107 */
108 $bdt_new_post_id = wp_insert_post($bdt_args);
109
110 /**
111 * get all current post terms ad set them to the new post draft
112 */
113 $bdt_taxonomies = get_object_taxonomies($bdt_post->post_type);
114
115 /**
116 * returns array of taxonomy names for post type, ex array("category", "post_tag");
117 */
118
119 foreach ($bdt_taxonomies as $bdt_taxonomy) {
120 $bdt_post_terms = wp_get_object_terms($post_id, $bdt_taxonomy, ['fields' => 'slugs']);
121 wp_set_object_terms($bdt_new_post_id, $bdt_post_terms, $bdt_taxonomy, false);
122 }
123
124 /**
125 * duplicate all post meta just in two SQL queries
126 */
127 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- one-off admin duplicate action, caching not applicable.
128 $bdt_post_meta_infos = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d", $post_id));
129
130 if (is_array($bdt_post_meta_infos)) {
131 $bdt_sql_query_sel = [];
132 $bdt_sql_values = [];
133
134 foreach ($bdt_post_meta_infos as $bdt_meta_info) {
135 $bdt_sql_query_sel[] = '( %d, %s, %s )';
136 $bdt_sql_values[] = $bdt_new_post_id;
137 $bdt_sql_values[] = $bdt_meta_info->meta_key;
138 $bdt_sql_values[] = wp_slash($bdt_meta_info->meta_value);
139 }
140
141 if (!empty($bdt_sql_query_sel)) {
142 $bdt_sql_query = "INSERT INTO {$wpdb->postmeta} ( post_id, meta_key, meta_value ) VALUES " . implode(', ', $bdt_sql_query_sel);
143 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Query is built only from static "%d, %s, %s" placeholders and the trusted {$wpdb->postmeta} table name; all values are bound via $wpdb->prepare().
144 $wpdb->query($wpdb->prepare($bdt_sql_query, $bdt_sql_values));
145 }
146
147 /**
148 * fix template type issues
149 */
150 $source_type = get_post_meta($post_id, '_elementor_template_type', true);
151 delete_post_meta($bdt_new_post_id, '_elementor_template_type');
152 update_post_meta($bdt_new_post_id, '_elementor_template_type', $source_type);
153 }
154
155 $css = Post_CSS::create($bdt_new_post_id);
156 $css->update();
157
158 /**
159 * finally, redirect to the edit post screen for the new draft
160 */
161
162 $bdt_all_post_types = get_post_types([], 'names');
163
164 foreach ($bdt_all_post_types as $bdt_key => $bdt_value) {
165 $bdt_names[] = $bdt_key;
166 }
167
168 $current_post_type = get_post_type($post_id);
169
170 if (is_array($bdt_names) && in_array($current_post_type, $bdt_names)) {
171 wp_safe_redirect(admin_url('edit.php?post_type=' . $current_post_type));
172 exit;
173 }
174
175 exit;
176 } else {
177 wp_die(esc_html('Failed. Not Found Post: ' . $post_id));
178 }
179 }
180
181
182 public function bdt_duplicate_post_link($actions, $post) {
183
184 if (current_user_can('manage_options') || current_user_can('edit_others_posts')) {
185 if ($post->post_type == 'post') {
186 $actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=bdt_duplicate_as_draft&post=' . $post->ID, basename(__FILE__), 'duplicate_nonce') . '" title="Duplicate this post" rel="permalink">' . esc_html_x("Duplicate Post", "Admin String", "ultimate-post-kit") . '</a>';
187 } elseif ($post->post_type == 'page') {
188 $actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=bdt_duplicate_as_draft&post=' . $post->ID, basename(__FILE__), 'duplicate_nonce') . '" title="Duplicate this page" rel="permalink">' . esc_html_x("Duplicate Page", "Admin String", "ultimate-post-kit") . '</a>';
189 } elseif ($post->post_type == 'elementor_library') {
190 $actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=bdt_duplicate_as_draft&post=' . $post->ID, basename(__FILE__), 'duplicate_nonce') . '" title="Duplicate this template" rel="permalink">' . esc_html_x("Duplicate Template", "Admin String", "ultimate-post-kit") . '</a>';
191 }
192 }
193 return $actions;
194 }
195 }
196 endif;
197
198
199 new BdThemes_Duplicator();
200