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