| 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 |
return new \WP_Error( 401, sprintf( 'User dont have capability to create page of type - %s.', $post->post_type ), [ 'status' => 401 ] ); |
| 54 |
} |
| 55 |
|
| 56 |
if ( ! $post ) { |
| 57 |
return new \WP_Error( 500, 'Post not found' ); |
| 58 |
} |
| 59 |
|
| 60 |
$new_post_id = $this->duplicate_post( $post, $post_title ); |
| 61 |
|
| 62 |
if ( is_wp_error( $new_post_id ) ) { |
| 63 |
return new \WP_Error( 500, 'Error while duplicating post.' ); |
| 64 |
} |
| 65 |
|
| 66 |
//Duplicate all post meta |
| 67 |
$this->duplicate_post_meta( $post_id, $new_post_id ); |
| 68 |
|
| 69 |
//Duplicate all taxonomies |
| 70 |
$this->duplicate_post_taxonomies( $post_id, $new_post_id ); |
| 71 |
|
| 72 |
return [ |
| 73 |
'post_id' => $new_post_id, |
| 74 |
]; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Duplicate post |
| 79 |
* |
| 80 |
* @param $post |
| 81 |
* |
| 82 |
* @return int|\WP_Error |
| 83 |
*/ |
| 84 |
private function duplicate_post( $post, $post_title ) { |
| 85 |
$post_status = 'draft'; |
| 86 |
$current_user = wp_get_current_user(); |
| 87 |
$new_post_author = $current_user->ID; |
| 88 |
|
| 89 |
$args = [ |
| 90 |
'comment_status' => $post->comment_status, |
| 91 |
'ping_status' => $post->ping_status, |
| 92 |
'post_author' => $new_post_author, |
| 93 |
'post_content' => $post->post_content, |
| 94 |
'post_excerpt' => $post->post_excerpt, |
| 95 |
'post_parent' => $post->post_parent, |
| 96 |
'post_password' => $post->post_password, |
| 97 |
'post_status' => $post_status, |
| 98 |
'post_title' => $post_title, |
| 99 |
'post_type' => $post->post_type, |
| 100 |
'to_ping' => $post->to_ping, |
| 101 |
'menu_order' => $post->menu_order, |
| 102 |
]; |
| 103 |
|
| 104 |
return wp_insert_post( $args ); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
/** |
| 109 |
* Duplicate the associated post meta to the new post ID. |
| 110 |
* |
| 111 |
* @param int $post_id |
| 112 |
* @param int $new_post_id |
| 113 |
*/ |
| 114 |
private function duplicate_post_meta( int $post_id, int $new_post_id ) { |
| 115 |
$post_meta = get_post_meta( $post_id ); |
| 116 |
|
| 117 |
if ( empty( $post_meta ) || ! is_array( $post_meta ) ) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
foreach ( $post_meta as $key => $values ) { |
| 122 |
if ( '_wp_old_slug' === $key ) { // Ignore this meta key |
| 123 |
continue; |
| 124 |
} |
| 125 |
|
| 126 |
foreach ( $values as $value ) { |
| 127 |
$value = maybe_unserialize( $value ); |
| 128 |
add_post_meta( $new_post_id, $key, wp_slash( $value ) ); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* duplicate_post_taxonomies |
| 135 |
* |
| 136 |
* @param int $post_id |
| 137 |
* @param int $new_post_id |
| 138 |
*/ |
| 139 |
private function duplicate_post_taxonomies( $post_id, $new_post_id ) { |
| 140 |
$taxonomies = array_map( 'sanitize_text_field', get_object_taxonomies( get_post_type( $post_id ) ) ); |
| 141 |
|
| 142 |
if ( empty( $taxonomies ) || ! is_array( $taxonomies ) ) { |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
foreach ( $taxonomies as $taxonomy ) { |
| 147 |
$post_terms = wp_get_object_terms( $post_id, $taxonomy, [ 'fields' => 'slugs' ] ); |
| 148 |
|
| 149 |
if ( ! is_wp_error( $post_terms ) ) { |
| 150 |
wp_set_object_terms( $new_post_id, $post_terms, $taxonomy, false ); |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|