PluginProbe
Elementor Website Builder – more than just a page builder / 3.35.0-dev2
Elementor Website Builder – more than just a page builder v3.35.0-dev2
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / site-navigation / data / endpoints / duplicate-post.php

duplicate-post.php in Elementor Website Builder – more than just a page builder 3.35.0-dev2, at modules/site-navigation/data/endpoints/duplicate-post.php

156 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\SiteNavigation\Data\Endpoints;
4
5 use Elementor\Data\V2\Base\Endpoint;
6 use Elementor\Plugin;
7 use Elementor\User;
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly.
11 }
12
13 class Duplicate_Post extends Endpoint {
14
15 protected function register() {
16 $args = [
17 'post_id' => [
18 'description' => 'Post id to duplicate',
19 'type' => 'integer',
20 'required' => true,
21 'sanitize_callback' => 'absint',
22 'validate_callback' => 'rest_validate_request_arg',
23 ],
24 'title' => [
25 'description' => 'Post title',
26 'type' => 'string',
27 'required' => false,
28 'sanitize_callback' => function ( $value ) {
29 return sanitize_text_field( $value );
30 },
31 'validate_callback' => 'rest_validate_request_arg',
32 ],
33 ];
34
35 $this->register_items_route( \WP_REST_Server::CREATABLE, $args );
36 }
37
38 public function get_name() {
39 return 'duplicate-post';
40 }
41
42 public function get_format() {
43 return 'site-navigation/duplicate-post';
44 }
45
46 public function create_items( $request ) {
47 $post_id = $request->get_param( 'post_id' );
48 $post_title = $request->get_param( 'title' );
49
50 $post = get_post( $post_id );
51
52 if ( ! User::is_current_user_can_edit_post_type( $post->post_type ) ) {
53 $sanitized_post_type = esc_html( str_replace( '%', '%%', $post->post_type ) );
54 return new \WP_Error( 401, sprintf( 'User dont have capability to create page of type - %s.', $sanitized_post_type ), [ 'status' => 401 ] );
55 }
56
57 if ( ! $post ) {
58 return new \WP_Error( 500, 'Post not found' );
59 }
60
61 $new_post_id = $this->duplicate_post( $post, $post_title );
62
63 if ( is_wp_error( $new_post_id ) ) {
64 return new \WP_Error( 500, 'Error while duplicating post.' );
65 }
66
67 // Duplicate all post meta
68 $this->duplicate_post_meta( $post_id, $new_post_id );
69
70 // Duplicate all taxonomies
71 $this->duplicate_post_taxonomies( $post_id, $new_post_id );
72
73 return [
74 'post_id' => $new_post_id,
75 ];
76 }
77
78 /**
79 * Duplicate post
80 *
81 * @param $post
82 *
83 * @return int|\WP_Error
84 */
85 private function duplicate_post( $post, $post_title ) {
86 $post_status = 'draft';
87 $current_user = wp_get_current_user();
88 $new_post_author = $current_user->ID;
89
90 $args = [
91 'comment_status' => $post->comment_status,
92 'ping_status' => $post->ping_status,
93 'post_author' => $new_post_author,
94 'post_content' => $post->post_content,
95 'post_excerpt' => $post->post_excerpt,
96 'post_parent' => $post->post_parent,
97 'post_password' => $post->post_password,
98 'post_status' => $post_status,
99 'post_title' => $post_title,
100 'post_type' => $post->post_type,
101 'to_ping' => $post->to_ping,
102 'menu_order' => $post->menu_order,
103 ];
104
105 return wp_insert_post( $args );
106 }
107
108
109 /**
110 * Duplicate the associated post meta to the new post ID.
111 *
112 * @param int $post_id
113 * @param int $new_post_id
114 */
115 private function duplicate_post_meta( int $post_id, int $new_post_id ) {
116 $post_meta = get_post_meta( $post_id );
117
118 if ( empty( $post_meta ) || ! is_array( $post_meta ) ) {
119 return;
120 }
121
122 foreach ( $post_meta as $key => $values ) {
123 if ( '_wp_old_slug' === $key ) { // Ignore this meta key
124 continue;
125 }
126
127 foreach ( $values as $value ) {
128 $value = maybe_unserialize( $value );
129 add_post_meta( $new_post_id, $key, wp_slash( $value ) );
130 }
131 }
132 }
133
134 /**
135 * Duplicate_post_taxonomies
136 *
137 * @param int $post_id
138 * @param int $new_post_id
139 */
140 private function duplicate_post_taxonomies( $post_id, $new_post_id ) {
141 $taxonomies = array_map( 'sanitize_text_field', get_object_taxonomies( get_post_type( $post_id ) ) );
142
143 if ( empty( $taxonomies ) || ! is_array( $taxonomies ) ) {
144 return;
145 }
146
147 foreach ( $taxonomies as $taxonomy ) {
148 $post_terms = wp_get_object_terms( $post_id, $taxonomy, [ 'fields' => 'slugs' ] );
149
150 if ( ! is_wp_error( $post_terms ) ) {
151 wp_set_object_terms( $new_post_id, $post_terms, $taxonomy, false );
152 }
153 }
154 }
155 }
156