PluginProbe
Sky Addons for Elementor / trunk
Sky Addons for Elementor vtrunk
4.0.0 3.8.5 3.8.4 3.8.3 3.8.2 3.8.0 3.8.1 3.3.3 3.3.2 trunk 1.0.0 1.0.10 1.0.2 1.0.6 1.0.7 1.0.8 1.0.9 1.5.0 2.0.0 2.0.8 2.5.10 2.5.11 2.5.12 2.5.13 2.5.15 All 53 releases
sky-elementor-addons / includes / features / class-duplicator.php

class-duplicator.php in Sky Addons for Elementor trunk, at includes/features/class-duplicator.php

137 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Sky_Addons\Features;
4
5 use Elementor\Core\Files\CSS\Post as Post_CSS;
6
7 defined( 'ABSPATH' ) || exit;
8
9 class Duplicator {
10 private static $instance = null;
11
12 private function __construct() {
13 add_action( 'admin_action_sky_duplicate_as_draft', [ $this, 'duplicate_as_draft' ] );
14 /**
15 * Post
16 */
17 add_filter( 'post_row_actions', [ $this, 'duplicate_post_link' ], 10, 2 );
18 /**
19 * Page
20 */
21 add_filter( 'page_row_actions', [ $this, 'duplicate_post_link' ], 10, 2 );
22 }
23
24 public static function get_instance() {
25 if ( null === self::$instance ) {
26 self::$instance = new self();
27 }
28 return self::$instance;
29 }
30
31 public function duplicate_as_draft() {
32 if ( ! current_user_can( 'edit_posts' ) ) {
33 wp_die( 'You don\'t have permission to duplicate it; please go back!' );
34 }
35
36 if ( ! ( isset( $_GET['post'] ) || isset( $_POST['post'] ) || ( isset( $_REQUEST['action'] ) && 'sky_duplicate_as_draft' === $_REQUEST['action'] ) ) ) {
37 wp_die( 'No post to duplicate has been supplied!' );
38 }
39
40 // phpcs:ignore
41 if ( ! isset( $_GET['duplicate_nonce'] ) || ! wp_verify_nonce( wp_unslash( $_GET['duplicate_nonce'] ), basename( __FILE__ ) ) ) {
42 return;
43 }
44
45 $post_id = ( isset( $_GET['post'] ) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
46 $post = get_post( $post_id );
47 $current_user_id = get_current_user_id();
48
49 if ( current_user_can( 'manage_options' ) || current_user_can( 'edit_others_posts' ) ) {
50 $this->duplicate_edit_post( $post_id );
51 } elseif ( current_user_can( 'edit_posts' ) && $post->post_author === $current_user_id ) {
52 $this->duplicate_edit_post( $post_id );
53 } else {
54 wp_die( 'You don\'t have permission to duplicate it; please go back!' );
55 }
56 }
57
58 public function duplicate_edit_post( $post_id ) {
59 global $wpdb;
60 $post = get_post( $post_id );
61 $current_user = wp_get_current_user();
62 $new_post_author = $current_user->ID;
63
64 if ( isset( $post ) && null !== $post ) {
65 $args = [
66 'post_status' => 'draft',
67 // translators: %1$s: Original post title.
68 'post_title' => sprintf( __( '%1$s - [Duplicated]', 'sky-elementor-addons' ), $post->post_title ),
69 'post_type' => $post->post_type,
70 'post_name' => $post->post_name,
71 'post_content' => $post->post_content,
72 'post_excerpt' => $post->post_excerpt,
73 'post_author' => $new_post_author,
74 'post_parent' => $post->post_parent,
75 'post_password' => $post->post_password,
76 'comment_status' => $post->comment_status,
77 'ping_status' => $post->ping_status,
78 'menu_order' => $post->menu_order,
79 'to_ping' => $post->to_ping,
80 ];
81
82 $new_post_id = wp_insert_post( $args );
83 $taxonomies = get_object_taxonomies( $post->post_type );
84
85 foreach ( $taxonomies as $taxonomy ) {
86 $post_terms = wp_get_object_terms( $post_id, $taxonomy, [ 'fields' => 'slugs' ] );
87 wp_set_object_terms( $new_post_id, $post_terms, $taxonomy, false );
88 }
89
90 // phpcs:ignore
91 $post_meta_infos = $wpdb->get_results( $wpdb->prepare(
92 "SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id = %d",
93 $post_id
94 ) );
95
96 if ( is_array( $post_meta_infos ) ) {
97 foreach ( $post_meta_infos as $meta_info ) {
98 // Use the WordPress API so both meta_key and meta_value are
99 // properly escaped internally — no raw SQL interpolation.
100 add_post_meta( $new_post_id, $meta_info->meta_key, $meta_info->meta_value );
101 }
102
103 $source_type = get_post_meta( $post_id, '_elementor_template_type', true );
104 delete_post_meta( $new_post_id, '_elementor_template_type' );
105 update_post_meta( $new_post_id, '_elementor_template_type', $source_type );
106 }
107
108 $css = Post_CSS::create( $new_post_id );
109 $css->update();
110
111 $all_post_types = get_post_types( [], 'names' );
112 $current_post_type = get_post_type( $post_id );
113
114 if ( in_array( $current_post_type, $all_post_types ) ) {
115 wp_safe_redirect( admin_url( 'edit.php?post_type=' . $current_post_type ) );
116 }
117
118 exit;
119 } else {
120 wp_die( 'Failed. Not Found Post: ' . esc_html( $post_id ) );
121 }
122 }
123
124 public function duplicate_post_link( $actions, $post ) {
125 if ( current_user_can( 'manage_options' ) || current_user_can( 'edit_others_posts' ) ) {
126 if ( 'post' === $post->post_type ) {
127 $actions['duplicate'] = '<a href="' . wp_nonce_url( 'admin.php?action=sky_duplicate_as_draft&post=' . $post->ID, basename( __FILE__ ), 'duplicate_nonce' ) . '" title="Duplicate this post" rel="permalink">' . esc_html_x( 'Duplicate Post', 'Admin String', 'sky-elementor-addons' ) . '</a>';
128 } elseif ( 'page' === $post->post_type ) {
129 $actions['duplicate'] = '<a href="' . wp_nonce_url( 'admin.php?action=sky_duplicate_as_draft&post=' . $post->ID, basename( __FILE__ ), 'duplicate_nonce' ) . '" title="Duplicate this page" rel="permalink">' . esc_html_x( 'Duplicate Page', 'Admin String', 'sky-elementor-addons' ) . '</a>';
130 } elseif ( 'elementor_library' === $post->post_type ) {
131 $actions['duplicate'] = '<a href="' . wp_nonce_url( 'admin.php?action=sky_duplicate_as_draft&post=' . $post->ID, basename( __FILE__ ), 'duplicate_nonce' ) . '" title="Duplicate this template" rel="permalink">' . esc_html_x( 'Duplicate Template', 'Admin String', 'sky-elementor-addons' ) . '</a>';
132 }
133 }
134 return $actions;
135 }
136 }
137