PluginProbe
Easy Elements for Elementor – Addons & Website Templates / 1.4.7
Easy Elements for Elementor – Addons & Website Templates v1.4.7
1.5.3 1.5.2 1.5.1 1.5.0 1.4.9 1.4.6 1.4.7 1.4.8 1.4.5 1.4.4 1.4.3 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 All 47 releases
easy-elements / includes / Extensions / Duplicator / EasyelDuplicator.php

EasyelDuplicator.php in Easy Elements for Elementor – Addons & Website Templates 1.4.7, at includes/Extensions/Duplicator/EasyelDuplicator.php

170 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Easyel\EasyElements\Extensions\Duplicator;
3 /**
4 * Easy Duplicator - Duplicate Any Post Type as Draft
5 *
6 * @package EasyElements
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 final class EasyelDuplicator {
14
15 /**
16 * Holds the singleton instance
17 *
18 * @var Easy_Duplicator|null
19 */
20 private static $instance = null;
21
22 /**
23 * Constructor
24 */
25 private function __construct() {
26
27 $tab_slug = 'extensions';
28 $extensions_settings = get_option('easy_element_' . $tab_slug, [] );
29
30 $enable_post_duplicator = isset( $extensions_settings['enable_post_duplicator'] ) ? $extensions_settings['enable_post_duplicator'] : 0;
31
32 if( (int) $enable_post_duplicator !== 1 ) {
33 return;
34 }
35
36 add_filter( 'post_row_actions', [ $this, 'easyel_add_duplicate_link' ], 10, 2 );
37 add_filter( 'page_row_actions', [ $this, 'easyel_add_duplicate_link' ], 10, 2 );
38 add_action( 'admin_action_easyel_duplicate_post', [ $this, 'easyel_duplicate_post_as_draft' ] );
39 }
40
41 /**
42 * Get instance
43 */
44 public static function get_instance() : self {
45 if ( null === self::$instance ) {
46 self::$instance = new self();
47 }
48 return self::$instance;
49 }
50
51 /**
52 * Add "Duplicate" action link in post rows
53 */
54 public function easyel_add_duplicate_link( $actions, $post ) {
55
56 if ( in_array( $post->post_status, [ 'auto-draft', 'inherit' ], true ) ) {
57 return $actions;
58 }
59
60 if ( current_user_can( 'edit_post', $post->ID ) ) {
61 $url = wp_nonce_url(
62 add_query_arg(
63 [
64 'action' => 'easyel_duplicate_post',
65 'post' => $post->ID,
66 ],
67 admin_url( 'admin.php' )
68 ),
69 'easyel_duplicate_nonce_' . $post->ID
70 );
71
72 $actions['easyel_duplicate'] = sprintf(
73 '<a href="%s" title="%s">%s</a>',
74 esc_url( $url ),
75 esc_attr__( 'Duplicate this item', 'easy-elements' ),
76 esc_html__( 'Easy Clone', 'easy-elements' )
77 );
78 }
79
80 return $actions;
81 }
82
83 /**
84 * Duplicate post as draft
85 */
86 public function easyel_duplicate_post_as_draft() {
87
88 $post_id = isset($_GET['post']) ? absint($_GET['post']) : 0;
89 $nonce = isset($_GET['_wpnonce']) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : '';
90
91 if ( ! $post_id || ! wp_verify_nonce($nonce, 'easyel_duplicate_nonce_' . $post_id) ) {
92 wp_die('Invalid request.');
93 }
94
95 $original = get_post($post_id);
96
97 if ( ! $original || 'trash' === $original->post_status ) {
98 wp_die('Post not found.');
99 }
100
101 $new_post_data = [
102 'post_title' => $original->post_title . ' (Copy)',
103 'post_content' => $original->post_content,
104 'post_status' => 'draft',
105 'post_type' => $original->post_type,
106 'post_author' => get_current_user_id(),
107 'post_excerpt' => $original->post_excerpt,
108 'post_parent' => $original->post_parent,
109 'menu_order' => $original->menu_order,
110 ];
111
112 $new_post_id = wp_insert_post( $new_post_data );
113
114 if ( is_wp_error( $new_post_id ) ) {
115 wp_die('Error duplicating post.');
116 }
117
118 $taxonomies = get_object_taxonomies($original->post_type);
119 foreach ($taxonomies as $taxonomy) {
120 $terms = wp_get_object_terms($post_id, $taxonomy, ['fields' => 'ids']);
121 wp_set_object_terms($new_post_id, $terms, $taxonomy);
122 }
123
124 $meta = get_post_meta($post_id);
125 foreach ($meta as $key => $values) {
126
127 if ($key === '_wp_old_slug') {
128 continue;
129 }
130
131 if (in_array($key, ['_elementor_data', '_elementor_controls_usage'], true)) {
132 $raw_value = get_post_meta($post_id, $key, true);
133 update_post_meta($new_post_id, $key, wp_slash($raw_value));
134 continue;
135 }
136
137 foreach ($values as $value) {
138 add_post_meta($new_post_id, $key, wp_slash(maybe_unserialize($value)));
139 }
140 }
141
142 if ( class_exists('\Elementor\Plugin') ) {
143
144 delete_post_meta($new_post_id, '_elementor_css');
145
146 try {
147 $css_file = new \Elementor\Core\Files\CSS\Post( $new_post_id );
148 $css_file->update();
149 } catch ( \Exception $e ) {
150
151 }
152 }
153
154 clean_post_cache( $new_post_id );
155
156 // Redirect
157 wp_safe_redirect(
158 add_query_arg(
159 [
160 'post_type' => $original->post_type,
161 'duplicated' => 'true',
162 ],
163 admin_url('edit.php')
164 )
165 );
166
167 exit;
168 }
169 }
170