PluginProbe
Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets / 4.1.14
Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets v4.1.14
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.1.14, at includes/class-duplicator.php

193 lines 8.0 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 $bdt_post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$bdt_post_id");
128
129 if (is_array($bdt_post_meta_infos)) {
130 $bdt_sql_query = "INSERT INTO {$wpdb->postmeta} ( post_id, meta_key, meta_value ) VALUES ";
131 $bdt_sql_query_sel = [];
132
133 foreach ($bdt_post_meta_infos as $bdt_meta_info) {
134 $bdt_meta_value = wp_slash($bdt_meta_info->meta_value);
135 $bdt_sql_query_sel[] = "( $bdt_new_post_id, '{$bdt_meta_info->meta_key}', '{$bdt_meta_value}' )";
136 }
137
138 $bdt_sql_query .= implode(', ', $bdt_sql_query_sel) . ';';
139 $wpdb->query($bdt_sql_query);
140
141 /**
142 * fix template type issues
143 */
144 $source_type = get_post_meta($post_id, '_elementor_template_type', true);
145 delete_post_meta($bdt_new_post_id, '_elementor_template_type');
146 update_post_meta($bdt_new_post_id, '_elementor_template_type', $source_type);
147 }
148
149 $css = Post_CSS::create($bdt_new_post_id);
150 $css->update();
151
152 /**
153 * finally, redirect to the edit post screen for the new draft
154 */
155
156 $bdt_all_post_types = get_post_types([], 'names');
157
158 foreach ($bdt_all_post_types as $bdt_key => $bdt_value) {
159 $bdt_names[] = $bdt_key;
160 }
161
162 $current_post_type = get_post_type($post_id);
163
164 if (is_array($bdt_names) && in_array($current_post_type, $bdt_names)) {
165 wp_redirect(admin_url('edit.php?post_type=' . $current_post_type));
166 }
167
168 exit;
169 } else {
170 wp_die('Failed. Not Found Post: ' . $post_id);
171 }
172 }
173
174
175 public function bdt_duplicate_post_link($actions, $post) {
176
177 if (current_user_can('manage_options') || current_user_can('edit_others_posts')) {
178 if ($post->post_type == 'post') {
179 $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>';
180 } elseif ($post->post_type == 'page') {
181 $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>';
182 } elseif ($post->post_type == 'elementor_library') {
183 $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>';
184 }
185 }
186 return $actions;
187 }
188 }
189 endif;
190
191
192 new BdThemes_Duplicator();
193