PluginProbe
Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits / 3.1.2
Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits v3.1.2
3.2.2 3.2.3 3.2.1 3.2.0 3.1.9 3.1.8 3.1.7 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.9 trunk 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.3 1.1.4 1.1.5 All 174 releases
master-addons / inc / modules / utilities / duplicator / duplicator.php

duplicator.php in Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits 3.1.2, at inc/modules/utilities/duplicator/duplicator.php

192 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MasterAddons\Modules\Utilities;
4
5 /**
6 * Author Name: Liton Arefin
7 * Author URL: https://jeweltheme.com
8 * Date: 7/4/2020
9 */
10 if (!defined('ABSPATH')) exit; // If this file is called directly, abort.
11
12 if (!class_exists('MasterAddons\Modules\Utilities\Duplicator')) {
13 class Duplicator
14 {
15
16 private static $instance = null;
17
18 public static function get_instance()
19 {
20 if (!self::$instance) {
21 self::$instance = new self;
22 }
23 return self::$instance;
24 }
25
26 public function __construct()
27 {
28 add_action('admin_action_jltma_duplicate', [$this, 'jltma_duplicate_post_as_draft']);
29 add_filter('post_row_actions', [$this, 'jltma_duplicator_row_actions'], 10, 2);
30 add_filter('page_row_actions', [$this, 'jltma_duplicator_row_actions'], 10, 2);
31 }
32
33 /**
34 * Check if current user can clone
35 *
36 * @return bool
37 */
38 public function user_can_clone($post)
39 {
40 if(!current_user_can('edit_posts')){
41 return false;
42 }
43
44 if (current_user_can('editor') || current_user_can('administrator')) {
45 return true;
46 }
47
48 if (current_user_can('contributor') || current_user_can('author')) {
49 $get_current_user_id = get_current_user_id();
50 $post = get_post($post);
51 $author_id = $post->post_author;
52 if($get_current_user_id == $author_id){
53 return true;
54 }
55 }
56 return false;
57 }
58
59
60 /*
61 * Add the duplicate link to action list for post_row_actions
62 */
63 public function jltma_duplicator_row_actions($actions, $post)
64 {
65 if (!$this->user_can_clone($post)) {
66 return $actions;
67 }
68
69 $jltma_duplicate_link = admin_url('admin.php?action=jltma_duplicate&post=' . absint($post->ID));
70 $jltma_duplicate_link = wp_nonce_url($jltma_duplicate_link, 'jltma_post_duplicator_nonce');
71
72 /* translators: %s: the post title. */
73 $jltma_duplicate_title = sprintf( __( 'Duplicate this item %s', 'master-addons' ), $post->post_title );
74 $actions['jltma_duplicate'] = sprintf('<a href="%s" title="%s" rel="permalink">%s</a>', $jltma_duplicate_link, esc_attr( $jltma_duplicate_title ), esc_html__('MA Duplicator', 'master-addons' ));
75 return $actions;
76 }
77
78
79 /*
80 * Function creates post duplicate as a draft and redirects then to the edit post screen
81 */
82 public function jltma_duplicate_post_as_draft()
83 {
84
85 global $wpdb;
86
87 if (!(isset($_GET['post']) || isset($_POST['post']) || (isset($_REQUEST['action']) && 'jltma_duplicate' == $_REQUEST['action']))) {
88 wp_die('No post to duplicate has been supplied!');
89 }
90
91 /*
92 * Nonce verification
93 * Return if nonce is not valid
94 */
95 if (empty($_REQUEST['_wpnonce']) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'jltma_post_duplicator_nonce')) {
96 return;
97 }
98
99 /*
100 * get the original post id
101 */
102 $post_id = (isset($_GET['post']) ? absint($_GET['post']) : absint($_POST['post']));
103 /*
104 * and all the original post data then
105 */
106 $post = get_post($post_id);
107
108 if (!$this->user_can_clone($post)) {
109 return;
110 }
111
112 /*
113 * if post data exists, create the post duplicate
114 */
115 if (isset($post) && $post != null) {
116
117 /*
118 * new post data array
119 */
120 $args = array(
121 /* translators: %1$s: original post title, %2$d: original post ID */
122 'post_title' => sprintf( __( '%1$s - [Duplicated #%2$d]', 'master-addons' ), $post->post_title, $post->ID ),
123 'post_type' => $post->post_type,
124 'post_content' => $post->post_content,
125 'post_excerpt' => $post->post_excerpt,
126 'post_author' => get_current_user_id(),
127 'comment_status' => $post->comment_status,
128 'ping_status' => $post->ping_status,
129 'post_name' => $post->post_name,
130 'post_parent' => $post->post_parent,
131 'post_password' => $post->post_password,
132 'post_status' => 'draft',
133 'to_ping' => $post->to_ping,
134 'menu_order' => $post->menu_order
135 );
136
137 /*
138 * insert the post by wp_insert_post() function
139 */
140 $new_post_id = wp_insert_post($args);
141
142 /*
143 * get all current post terms ad set them to the new post draft
144 */
145 $taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
146 foreach ($taxonomies as $taxonomy) {
147 $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
148 wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
149 }
150
151 /*
152 * duplicate all post meta just in two SQL queries
153 */
154 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- direct query required to fetch all post meta for duplication
155 $post_meta_infos = $wpdb->get_results(
156 $wpdb->prepare(
157 "SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id = %d",
158 (int) $post_id
159 )
160 );
161 if (count($post_meta_infos) != 0) {
162 $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
163 foreach ($post_meta_infos as $meta_info) {
164 $meta_key = $meta_info->meta_key;
165 if ($meta_key == '_wp_old_slug') continue;
166 if ($meta_key == \Elementor\Core\Files\CSS\Post::META_KEY) continue;
167 $meta_value = $meta_info->meta_value;
168 $sql_query_sel[] = $wpdb->prepare("SELECT %d, %s, %s", $new_post_id, $meta_key, $meta_value);
169 }
170 $sql_query .= implode(" UNION ALL ", $sql_query_sel);
171 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter -- query is built from wpdb->prepare() fragments joined with UNION ALL; table name cannot be parameterized
172 $wpdb->query( $sql_query );
173 }
174
175 /*
176 * finally, redirect to the edit post screen for the new draft
177 */
178 $redirect_url = admin_url('edit.php?post_type=' . $post->post_type);
179 wp_safe_redirect($redirect_url);
180
181 exit;
182 } else {
183 wp_die( 'Post creation failed, could not find original post: ' . esc_html( $post_id ) );
184 }
185 }
186 }
187 }
188
189 if (class_exists('MasterAddons\Modules\Utilities\Duplicator')) {
190 Duplicator::get_instance();
191 }
192