• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / duplicate-post / 27413684951

12 Jun 2026 11:46AM UTC coverage: 60.272% (+0.07%) from 60.207%
27413684951

push

github

web-flow
Merge pull request #508 from Yoast/claude/cve-2026-53739-53740-fix-b9qdjt

Fix CVE-2026-53739 (CSRF) & CVE-2026-53740 (XSS)

8 of 10 new or added lines in 2 files covered. (80.0%)

1 existing line in 1 file now uncovered.

1637 of 2716 relevant lines covered (60.27%)

7.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

46.61
/admin-functions.php
1
<?php
2
/**
3
 * Backend functions.
4
 *
5
 * @package Yoast\WP\Duplicate_Post
6
 * @since   2.0
7
 */
8

9
if ( ! is_admin() ) {
10
        return;
11
}
12

13
use Yoast\WP\Duplicate_Post\UI\Newsletter;
14
use Yoast\WP\Duplicate_Post\Utils;
15

16
require_once DUPLICATE_POST_PATH . 'options.php';
17

18
require_once DUPLICATE_POST_PATH . 'compat/wpml-functions.php';
19
require_once DUPLICATE_POST_PATH . 'compat/jetpack-functions.php';
20

21
/**
22
 * Wrapper for the option 'duplicate_post_version'.
23
 *
24
 * @return mixed
25
 */
26
function duplicate_post_get_installed_version() {
27
        return get_option( 'duplicate_post_version' );
×
28
}
29

30
/**
31
 * Wrapper for the defined constant DUPLICATE_POST_CURRENT_VERSION.
32
 *
33
 * @return string
34
 */
35
function duplicate_post_get_current_version() {
36
        return DUPLICATE_POST_CURRENT_VERSION;
×
37
}
38

39
add_action( 'admin_init', 'duplicate_post_admin_init' );
40

41
/**
42
 * Adds handlers depending on the options.
43
 *
44
 * @return void
45
 */
46
function duplicate_post_admin_init() {
47
        duplicate_post_plugin_upgrade();
×
48

49
        if ( (int) get_site_option( 'duplicate_post_show_notice' ) === 1 ) {
×
50
                if ( is_multisite() ) {
×
51
                        add_action( 'network_admin_notices', 'duplicate_post_show_update_notice' );
×
52
                }
53
                else {
54
                        add_action( 'admin_notices', 'duplicate_post_show_update_notice' );
×
55
                }
56
                add_action( 'wp_ajax_duplicate_post_dismiss_notice', 'duplicate_post_dismiss_notice' );
×
57
        }
58

59
        add_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_post_meta_info', 10, 2 );
×
60

61
        if ( (int) get_option( 'duplicate_post_copychildren' ) === 1 ) {
×
62
                add_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_children', 20, 3 );
×
63
        }
64

65
        if ( (int) get_option( 'duplicate_post_copyattachments' ) === 1 ) {
×
66
                add_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_attachments', 30, 2 );
×
67
        }
68

69
        if ( (int) get_option( 'duplicate_post_copycomments' ) === 1 ) {
×
70
                add_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_comments', 40, 2 );
×
71
        }
72

73
        add_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_post_taxonomies', 50, 2 );
×
74

75
        add_filter( 'plugin_row_meta', 'duplicate_post_add_plugin_links', 10, 2 );
×
76
}
77

78
/**
79
 * Plugin upgrade.
80
 *
81
 * @return void
82
 */
83
function duplicate_post_plugin_upgrade() {
84
        $installed_version = duplicate_post_get_installed_version();
×
85

86
        if ( duplicate_post_get_current_version() === $installed_version ) {
×
87
                return;
×
88
        }
89

90
        if ( empty( $installed_version ) ) {
×
91
                // Get default roles.
92
                $default_roles = [
×
93
                        'editor',
×
94
                        'administrator',
×
95
                        'wpseo_manager',
×
96
                        'wpseo_editor',
×
97
                ];
×
98

99
                foreach ( $default_roles as $name ) {
×
100
                        $role = get_role( $name );
×
101
                        if ( ! empty( $role ) ) {
×
102
                                $role->add_cap( 'copy_posts' );
×
103
                        }
104
                }
105
                add_option( 'duplicate_post_show_notice', 1 );
×
106
        }
107
        else {
108
                update_option( 'duplicate_post_show_notice', 0 );
×
109
        }
110

111
        $show_links_in_defaults = [
×
112
                'row'         => '1',
×
113
                'adminbar'    => '1',
×
114
                'submitbox'   => '1',
×
115
                'bulkactions' => '1',
×
116
        ];
×
117

118
        add_option( 'duplicate_post_copytitle', '1' );
×
119
        add_option( 'duplicate_post_copydate', '0' );
×
120
        add_option( 'duplicate_post_copystatus', '0' );
×
121
        add_option( 'duplicate_post_copyslug', '0' );
×
122
        add_option( 'duplicate_post_copyexcerpt', '1' );
×
123
        add_option( 'duplicate_post_copycontent', '1' );
×
124
        add_option( 'duplicate_post_copythumbnail', '1' );
×
125
        add_option( 'duplicate_post_copytemplate', '1' );
×
126
        add_option( 'duplicate_post_copyformat', '1' );
×
127
        add_option( 'duplicate_post_copyauthor', '0' );
×
128
        add_option( 'duplicate_post_copypassword', '0' );
×
129
        add_option( 'duplicate_post_copyattachments', '0' );
×
130
        add_option( 'duplicate_post_copychildren', '0' );
×
131
        add_option( 'duplicate_post_copycomments', '0' );
×
132
        add_option( 'duplicate_post_copymenuorder', '1' );
×
133
        add_option( 'duplicate_post_taxonomies_blacklist', [] );
×
134
        add_option( 'duplicate_post_blacklist', '' );
×
135
        add_option( 'duplicate_post_types_enabled', [ 'post', 'page' ] );
×
136
        add_option( 'duplicate_post_show_original_column', '0' );
×
137
        add_option( 'duplicate_post_show_original_in_post_states', '0' );
×
138
        add_option( 'duplicate_post_show_original_meta_box', '0' );
×
139
        add_option(
×
140
                'duplicate_post_show_link',
×
141
                [
×
142
                        'new_draft'         => '1',
×
143
                        'clone'             => '1',
×
144
                        'rewrite_republish' => '1',
×
145
                ],
×
146
        );
×
147
        add_option( 'duplicate_post_show_link_in', $show_links_in_defaults );
×
148

149
        $taxonomies_blacklist = get_option( 'duplicate_post_taxonomies_blacklist', [] );
×
150
        if ( empty( $taxonomies_blacklist ) ) {
×
151
                $taxonomies_blacklist = [];
×
152
        }
153
        elseif ( ! is_array( $taxonomies_blacklist ) ) {
×
154
                $taxonomies_blacklist = [ $taxonomies_blacklist ];
×
155
        }
156
        if ( in_array( 'post_format', $taxonomies_blacklist, true ) ) {
×
157
                update_option( 'duplicate_post_copyformat', 0 );
×
158
                $taxonomies_blacklist = array_diff( $taxonomies_blacklist, [ 'post_format' ] );
×
159
                update_option( 'duplicate_post_taxonomies_blacklist', $taxonomies_blacklist );
×
160
        }
161

162
        $meta_blacklist = explode( ',', get_option( 'duplicate_post_blacklist' ) );
×
163
        $meta_blacklist = array_map( 'trim', $meta_blacklist );
×
164
        if ( in_array( '_wp_page_template', $meta_blacklist, true ) ) {
×
165
                update_option( 'duplicate_post_copytemplate', 0 );
×
166
                $meta_blacklist = array_diff( $meta_blacklist, [ '_wp_page_template' ] );
×
167
        }
168
        if ( in_array( '_thumbnail_id', $meta_blacklist, true ) ) {
×
169
                update_option( 'duplicate_post_copythumbnail', 0 );
×
170
                $meta_blacklist = array_diff( $meta_blacklist, [ '_thumbnail_id' ] );
×
171
        }
172
        update_option( 'duplicate_post_blacklist', implode( ',', $meta_blacklist ) );
×
173

174
        if ( version_compare( $installed_version, '4.0.0' ) < 0 ) {
×
175
                // Migrate the 'Show links in' options to the new array-based structure.
176
                duplicate_post_migrate_show_links_in_options( $show_links_in_defaults );
×
177
        }
178

179
        delete_site_option( 'duplicate_post_version' );
×
180
        update_option( 'duplicate_post_version', duplicate_post_get_current_version() );
×
181
}
182

183
/**
184
 * Runs the upgrade routine for version 4.0 to update the options in the database.
185
 *
186
 * @param array $defaults The default options to fall back on.
187
 *
188
 * @return void
189
 */
190
function duplicate_post_migrate_show_links_in_options( $defaults ) {
191
        $options_to_migrate = [
×
192
                'duplicate_post_show_row'         => 'row',
×
193
                'duplicate_post_show_adminbar'    => 'adminbar',
×
194
                'duplicate_post_show_submitbox'   => 'submitbox',
×
195
                'duplicate_post_show_bulkactions' => 'bulkactions',
×
196
        ];
×
197

198
        $new_options = [];
×
199
        foreach ( $options_to_migrate as $old => $new ) {
×
200
                $new_options[ $new ] = get_option( $old, $defaults[ $new ] );
×
201

202
                delete_option( $old );
×
203
        }
204

205
        update_option( 'duplicate_post_show_link_in', $new_options );
×
206
}
207

208
/**
209
 * Shows the welcome notice.
210
 *
211
 * @global string $wp_version The WordPress version string.
212
 *
213
 * @return void
214
 */
215
function duplicate_post_show_update_notice() {
216
        if ( ! current_user_can( 'manage_options' ) ) {
×
217
                return;
×
218
        }
219

220
        $current_screen = get_current_screen();
×
221
        if ( empty( $current_screen )
×
222
                || empty( $current_screen->base )
×
223
                || ( $current_screen->base !== 'dashboard' && $current_screen->base !== 'plugins' )
×
224
        ) {
225
                return;
×
226
        }
227

228
        $title = sprintf(
×
229
                /* translators: %s: Yoast Duplicate Post. */
230
                esc_html__( 'You\'ve successfully installed %s!', 'duplicate-post' ),
×
231
                'Yoast Duplicate Post',
×
232
        );
×
233

234
        $img_path = plugins_url( '/duplicate_post_yoast_icon-125x125.png', __FILE__ );
×
235

236
        echo '<div id="duplicate-post-notice" class="notice is-dismissible" style="display: flex; align-items: flex-start;">
×
237
                        <img src="' . esc_url( $img_path ) . '" alt="" style="margin: 1em 1em 1em 0; width: 130px; align-self: center;"/>
×
238
                        <div style="margin: 0.5em">
239
                                <h1 style="font-size: 14px; color: #a4286a; font-weight: 600; margin-top: 8px;">' . $title . '</h1>' // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: escaped properly above.
×
240
                                . Newsletter::newsletter_signup_form() // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: escaped in newsletter.php.
×
241
                        . '</div>
×
242
                </div>';
×
243

NEW
244
        $dismiss_nonce = wp_create_nonce( 'duplicate_post_dismiss_notice' );
×
245

UNCOV
246
        echo "<script>
×
247
                        function duplicate_post_dismiss_notice(){
248
                                var data = {
249
                                'action': 'duplicate_post_dismiss_notice',
NEW
250
                                'nonce': '" . esc_js( $dismiss_nonce ) . "',
×
251
                                };
252

253
                                jQuery.post(ajaxurl, data, function(response) {
254
                                        jQuery('#duplicate-post-notice').hide();
255
                                });
256
                        }
257

258
                        jQuery(document).ready(function(){
259
                                jQuery('body').on('click', '.notice-dismiss', function(){
260
                                        duplicate_post_dismiss_notice();
261
                                });
262
                        });
263
                        </script>";
×
264
}
265

266
/**
267
 * Dismisses the notice.
268
 *
269
 * @return bool
270
 */
271
function duplicate_post_dismiss_notice() {
272
        if ( ! current_user_can( 'manage_options' ) ) {
12✔
273
                return false;
4✔
274
        }
275

276
        if ( ! check_ajax_referer( 'duplicate_post_dismiss_notice', 'nonce', false ) ) {
8✔
277
                return false;
4✔
278
        }
279

280
        return update_site_option( 'duplicate_post_show_notice', 0 );
4✔
281
}
282

283
/**
284
 * Copies the taxonomies of a post to another post.
285
 *
286
 * @global wpdb $wpdb WordPress database abstraction object.
287
 *
288
 * @param int     $new_id New post ID.
289
 * @param WP_Post $post   The original post object.
290
 *
291
 * @return void
292
 */
293
function duplicate_post_copy_post_taxonomies( $new_id, $post ) {
294
        global $wpdb;
8✔
295
        if ( isset( $wpdb->terms ) ) {
8✔
296
                // Clear default category (added by wp_insert_post).
297
                wp_set_object_terms( $new_id, [], 'category' );
8✔
298

299
                $post_taxonomies = get_object_taxonomies( $post->post_type );
8✔
300
                // Several plugins just add support to post-formats but don't register post_format taxonomy.
301
                if ( post_type_supports( $post->post_type, 'post-formats' ) && ! in_array( 'post_format', $post_taxonomies, true ) ) {
8✔
302
                        $post_taxonomies[] = 'post_format';
×
303
                }
304

305
                $taxonomies_blacklist = get_option( 'duplicate_post_taxonomies_blacklist', [] );
8✔
306
                if ( empty( $taxonomies_blacklist ) ) {
8✔
307
                        $taxonomies_blacklist = [];
4✔
308
                }
309
                elseif ( ! is_array( $taxonomies_blacklist ) ) {
4✔
310
                        $taxonomies_blacklist = [ $taxonomies_blacklist ];
×
311
                }
312
                if ( (int) get_option( 'duplicate_post_copyformat' ) === 0 ) {
8✔
313
                        $taxonomies_blacklist[] = 'post_format';
×
314
                }
315

316
                /**
317
                 * Filters the taxonomy excludelist when copying a post.
318
                 *
319
                 * @param array $taxonomies_blacklist The taxonomy excludelist from the options.
320
                 *
321
                 * @return array
322
                 */
323
                $taxonomies_blacklist = apply_filters( 'duplicate_post_taxonomies_excludelist_filter', $taxonomies_blacklist );
8✔
324

325
                $taxonomies = array_diff( $post_taxonomies, $taxonomies_blacklist );
8✔
326
                foreach ( $taxonomies as $taxonomy ) {
8✔
327
                        $post_terms = wp_get_object_terms( $post->ID, $taxonomy, [ 'orderby' => 'term_order' ] );
8✔
328
                        $terms      = [];
8✔
329
                        $num_terms  = count( $post_terms );
8✔
330
                        for ( $i = 0; $i < $num_terms; $i++ ) {
8✔
331
                                $terms[] = $post_terms[ $i ]->slug;
8✔
332
                        }
333
                        wp_set_object_terms( $new_id, $terms, $taxonomy );
8✔
334
                }
335
        }
336
}
337

338
/**
339
 * Copies the meta information of a post to another post
340
 *
341
 * @param int     $new_id The new post ID.
342
 * @param WP_Post $post   The original post object.
343
 *
344
 * @return void
345
 */
346
function duplicate_post_copy_post_meta_info( $new_id, $post ) {
347
        $post_meta_keys = get_post_custom_keys( $post->ID );
16✔
348
        if ( empty( $post_meta_keys ) ) {
16✔
349
                return;
×
350
        }
351
        $meta_blacklist = get_option( 'duplicate_post_blacklist' );
16✔
352
        if ( $meta_blacklist === '' ) {
16✔
353
                $meta_blacklist = [];
12✔
354
        }
355
        else {
356
                $meta_blacklist = explode( ',', $meta_blacklist );
4✔
357
                $meta_blacklist = array_filter( $meta_blacklist );
4✔
358
                $meta_blacklist = array_map( 'trim', $meta_blacklist );
4✔
359
        }
360
        $meta_blacklist[] = '_edit_lock'; // Edit lock.
16✔
361
        $meta_blacklist[] = '_edit_last'; // Edit lock.
16✔
362
        $meta_blacklist[] = '_dp_is_rewrite_republish_copy';
16✔
363
        $meta_blacklist[] = '_dp_has_rewrite_republish_copy';
16✔
364
        if ( (int) get_option( 'duplicate_post_copytemplate' ) === 0 ) {
16✔
365
                $meta_blacklist[] = '_wp_page_template';
×
366
        }
367
        if ( (int) get_option( 'duplicate_post_copythumbnail' ) === 0 ) {
16✔
368
                $meta_blacklist[] = '_thumbnail_id';
4✔
369
        }
370

371
        /**
372
         * Filters the meta fields excludelist when copying a post.
373
         *
374
         * @param array $meta_blacklist The meta fields excludelist from the options.
375
         *
376
         * @return array
377
         */
378
        $meta_blacklist = apply_filters( 'duplicate_post_excludelist_filter', $meta_blacklist );
16✔
379

380
        $meta_blacklist_string = '(' . implode( ')|(', $meta_blacklist ) . ')';
16✔
381
        if ( strpos( $meta_blacklist_string, '*' ) !== false ) {
16✔
382
                $meta_blacklist_string = str_replace( [ '*' ], [ '[a-zA-Z0-9_]*' ], $meta_blacklist_string );
×
383

384
                $meta_keys = [];
×
385
                foreach ( $post_meta_keys as $meta_key ) {
×
386
                        if ( ! preg_match( '#^(' . $meta_blacklist_string . ')$#', $meta_key ) ) {
×
387
                                $meta_keys[] = $meta_key;
×
388
                        }
389
                }
390
        }
391
        else {
392
                $meta_keys = array_diff( $post_meta_keys, $meta_blacklist );
16✔
393
        }
394

395
        /**
396
         * Filters the list of meta fields names when copying a post.
397
         *
398
         * @param array $meta_keys The list of meta fields name, with the ones in the excludelist already removed.
399
         *
400
         * @return array
401
         */
402
        $meta_keys = apply_filters( 'duplicate_post_meta_keys_filter', $meta_keys );
16✔
403

404
        foreach ( $meta_keys as $meta_key ) {
16✔
405
                $meta_values = get_post_custom_values( $meta_key, $post->ID );
16✔
406
                foreach ( $meta_values as $meta_value ) {
16✔
407
                        $meta_value = maybe_unserialize( $meta_value );
16✔
408
                        add_post_meta( $new_id, $meta_key, duplicate_post_wp_slash( $meta_value ) );
16✔
409
                }
410
        }
411
}
412

413
/**
414
 * Workaround for inconsistent wp_slash.
415
 * Works only with WP 4.4+ (map_deep)
416
 *
417
 * @param mixed $value Array or object to be recursively slashed.
418
 * @return string|mixed
419
 */
420
function duplicate_post_addslashes_deep( $value ) {
421
        if ( function_exists( 'map_deep' ) ) {
×
422
                return map_deep( $value, 'duplicate_post_addslashes_to_strings_only' );
×
423
        }
424
        else {
425
                return wp_slash( $value );
×
426
        }
427
}
428

429
/**
430
 * Adds slashes only to strings.
431
 *
432
 * @param mixed $value Value to slash only if string.
433
 * @return string|mixed
434
 */
435
function duplicate_post_addslashes_to_strings_only( $value ) {
436
        return Utils::addslashes_to_strings_only( $value );
×
437
}
438

439
/**
440
 * Replacement function for faulty core wp_slash().
441
 *
442
 * @param mixed $value What to add slash to.
443
 * @return mixed
444
 */
445
function duplicate_post_wp_slash( $value ) {
446
        return duplicate_post_addslashes_deep( $value );
×
447
}
448

449
/**
450
 * Copies attachments, including physical files.
451
 *
452
 * @param int     $new_id The new post ID.
453
 * @param WP_Post $post   The original post object.
454
 *
455
 * @return void
456
 */
457
function duplicate_post_copy_attachments( $new_id, $post ) {
458
        // Get thumbnail ID.
459
        $old_thumbnail_id = get_post_thumbnail_id( $post->ID );
×
460
        // Get children.
461
        $children = get_posts(
×
462
                [
×
463
                        'post_type'   => 'any',
×
464
                        'numberposts' => -1,
×
465
                        'post_status' => 'any',
×
466
                        'post_parent' => $post->ID,
×
467
                ],
×
468
        );
×
469
        // Clone old attachments.
470
        foreach ( $children as $child ) {
×
471
                if ( $child->post_type !== 'attachment' ) {
×
472
                        continue;
×
473
                }
474
                $url = wp_get_attachment_url( $child->ID );
×
475
                // Let's copy the actual file.
476
                $tmp = download_url( $url );
×
477
                if ( is_wp_error( $tmp ) ) {
×
478
                        continue;
×
479
                }
480

481
                $desc = wp_slash( $child->post_content );
×
482

483
                $file_array             = [];
×
484
                $file_array['name']     = basename( $url );
×
485
                $file_array['tmp_name'] = $tmp;
×
486
                // "Upload" to the media collection
487
                $new_attachment_id = media_handle_sideload( $file_array, $new_id, $desc );
×
488

489
                if ( is_wp_error( $new_attachment_id ) ) {
×
490
                        wp_delete_file( $file_array['tmp_name'] );
×
491
                        continue;
×
492
                }
493
                $new_post_author = wp_get_current_user();
×
494
                $cloned_child    = [
×
495
                        'ID'           => $new_attachment_id,
×
496
                        'post_title'   => $child->post_title,
×
497
                        'post_excerpt' => $child->post_excerpt, // Caption.
×
498
                        'post_content' => $child->post_content, // Description.
×
499
                        'post_author'  => $new_post_author->ID,
×
500
                ];
×
501
                wp_update_post( wp_slash( $cloned_child ) );
×
502

503
                $alt_title = get_post_meta( $child->ID, '_wp_attachment_image_alt', true );
×
504
                if ( $alt_title ) {
×
505
                        update_post_meta( $new_attachment_id, '_wp_attachment_image_alt', wp_slash( $alt_title ) );
×
506
                }
507

508
                // If we have cloned the post thumbnail, set the copy as the thumbnail for the new post.
509
                if ( (int) get_option( 'duplicate_post_copythumbnail' ) === 1 && $old_thumbnail_id === $child->ID ) {
×
510
                        set_post_thumbnail( $new_id, $new_attachment_id );
×
511
                }
512
        }
513
}
514

515
/**
516
 * Copies child posts.
517
 *
518
 * @param int     $new_id The new post ID.
519
 * @param WP_Post $post   The original post object.
520
 * @param string  $status Optional. The destination status.
521
 *
522
 * @return void
523
 */
524
function duplicate_post_copy_children( $new_id, $post, $status = '' ) {
525
        // Get children.
526
        $children = get_posts(
4✔
527
                [
4✔
528
                        'post_type'   => 'any',
4✔
529
                        'numberposts' => -1,
4✔
530
                        'post_status' => 'any',
4✔
531
                        'post_parent' => $post->ID,
4✔
532
                ],
4✔
533
        );
4✔
534

535
        foreach ( $children as $child ) {
4✔
536
                if ( $child->post_type === 'attachment' ) {
4✔
537
                        continue;
×
538
                }
539
                duplicate_post_create_duplicate( $child, $status, $new_id );
4✔
540
        }
541
}
542

543
/**
544
 * Copies comments.
545
 *
546
 * @param int     $new_id The new post ID.
547
 * @param WP_Post $post   The original post object.
548
 *
549
 * @return void
550
 */
551
function duplicate_post_copy_comments( $new_id, $post ) {
552
        $comments = get_comments(
4✔
553
                [
4✔
554
                        'post_id' => $post->ID,
4✔
555
                        'order'   => 'ASC',
4✔
556
                        'orderby' => 'comment_date_gmt',
4✔
557
                ],
4✔
558
        );
4✔
559

560
        $old_id_to_new = [];
4✔
561
        foreach ( $comments as $comment ) {
4✔
562
                // Do not copy pingbacks or trackbacks.
563
                if ( $comment->comment_type === 'pingback' || $comment->comment_type === 'trackback' ) {
4✔
564
                        continue;
×
565
                }
566
                $parent      = ( $comment->comment_parent && $old_id_to_new[ $comment->comment_parent ] ) ? $old_id_to_new[ $comment->comment_parent ] : 0;
4✔
567
                $commentdata = [
4✔
568
                        'comment_post_ID'      => $new_id,
4✔
569
                        'comment_author'       => $comment->comment_author,
4✔
570
                        'comment_author_email' => $comment->comment_author_email,
4✔
571
                        'comment_author_url'   => $comment->comment_author_url,
4✔
572
                        'comment_content'      => $comment->comment_content,
4✔
573
                        'comment_type'         => $comment->comment_type,
4✔
574
                        'comment_parent'       => $parent,
4✔
575
                        'user_id'              => $comment->user_id,
4✔
576
                        'comment_author_IP'    => $comment->comment_author_IP,
4✔
577
                        'comment_agent'        => $comment->comment_agent,
4✔
578
                        'comment_karma'        => $comment->comment_karma,
4✔
579
                        'comment_approved'     => $comment->comment_approved,
4✔
580
                ];
4✔
581
                if ( (int) get_option( 'duplicate_post_copydate' ) === 1 ) {
4✔
582
                        $commentdata['comment_date']     = $comment->comment_date;
×
583
                        $commentdata['comment_date_gmt'] = get_gmt_from_date( $comment->comment_date );
×
584
                }
585
                $new_comment_id = wp_insert_comment( $commentdata );
4✔
586
                $commentmeta    = get_comment_meta( $new_comment_id );
4✔
587
                foreach ( $commentmeta as $meta_key => $meta_value ) {
4✔
588
                        add_comment_meta( $new_comment_id, $meta_key, duplicate_post_wp_slash( $meta_value ) );
×
589
                }
590
                $old_id_to_new[ $comment->comment_ID ] = $new_comment_id;
4✔
591
        }
592
}
593

594
/**
595
 * Creates a duplicate from a post.
596
 *
597
 * This is the main functions that does the cloning.
598
 *
599
 * @param WP_Post $post      The original post object.
600
 * @param string  $status    Optional. The intended destination status.
601
 * @param string  $parent_id Optional. The parent post ID if we are calling this recursively.
602
 * @return int|WP_Error
603
 */
604
function duplicate_post_create_duplicate( $post, $status = '', $parent_id = '' ) {
605
        /**
606
         * Fires before duplicating a post.
607
         *
608
         * @param WP_Post $post      The original post object.
609
         * @param bool    $status    The intended destination status.
610
         * @param int     $parent_id The parent post ID if we are calling this recursively.
611
         */
612
        do_action( 'duplicate_post_pre_copy', $post, $status, $parent_id );
204✔
613

614
        /**
615
         * Filter allowing to copy post.
616
         *
617
         * @param bool    $can_duplicate Default to `true`.
618
         * @param WP_Post $post          The original post object.
619
         * @param bool    $status        The intended destination status.
620
         * @param int     $parent_id     The parent post ID if we are calling this recursively.
621
         *
622
         * @return bool
623
         */
624
        $can_duplicate = apply_filters( 'duplicate_post_allow', true, $post, $status, $parent_id );
204✔
625
        if ( ! $can_duplicate ) {
204✔
626
                wp_die( esc_html( __( 'You aren\'t allowed to duplicate this post', 'duplicate-post' ) ) );
4✔
627
        }
628

629
        if ( ! duplicate_post_is_post_type_enabled( $post->post_type ) && $post->post_type !== 'attachment' ) {
200✔
630
                wp_die(
4✔
631
                        esc_html(
4✔
632
                                __( 'Copy features for this post type are not enabled in options page', 'duplicate-post' ) . ': '
4✔
633
                                . $post->post_type,
4✔
634
                        ),
4✔
635
                );
4✔
636
        }
637

638
        $new_post_status = ( empty( $status ) ) ? $post->post_status : $status;
196✔
639
        $title           = ' ';
196✔
640

641
        if ( $post->post_type !== 'attachment' ) {
196✔
642
                $prefix = sanitize_text_field( get_option( 'duplicate_post_title_prefix' ) );
196✔
643
                $suffix = sanitize_text_field( get_option( 'duplicate_post_title_suffix' ) );
196✔
644
                if ( (int) get_option( 'duplicate_post_copytitle' ) === 1 ) {
196✔
645
                        $title = $post->post_title;
188✔
646
                        if ( ! empty( $prefix ) ) {
188✔
647
                                $prefix .= ' ';
8✔
648
                        }
649
                        if ( ! empty( $suffix ) ) {
188✔
650
                                $suffix = ' ' . $suffix;
98✔
651
                        }
652
                }
653
                else {
654
                        $title = ' ';
8✔
655
                }
656
                $title = trim( $prefix . $title . $suffix );
196✔
657

658
                /*
659
                 * Not sure we should force a title. Instead, we should respect what WP does.
660
                 * if ( '' === $title ) {
661
                 *  // empty title.
662
                 *  $title = __( 'Untitled', 'default' );
663
                 * }
664
                 */
665

666
                if ( (int) get_option( 'duplicate_post_copystatus' ) === 0 ) {
196✔
667
                        $new_post_status = 'draft';
172✔
668
                }
669
                elseif ( $new_post_status === 'publish' || $new_post_status === 'future' ) {
24✔
670
                        // Check if the user has the right capability.
671
                        if ( is_post_type_hierarchical( $post->post_type ) ) {
8✔
672
                                if ( ! current_user_can( 'publish_pages' ) ) {
×
673
                                        $new_post_status = 'pending';
×
674
                                }
675
                        }
676
                        elseif ( ! current_user_can( 'publish_posts' ) ) {
8✔
677
                                $new_post_status = 'pending';
4✔
678
                        }
679
                }
680
        }
681

682
        $new_post_author    = wp_get_current_user();
196✔
683
        $new_post_author_id = $new_post_author->ID;
196✔
684
        if ( (int) get_option( 'duplicate_post_copyauthor' ) === 1 ) {
196✔
685
                // Check if the user has the right capability.
686
                if ( is_post_type_hierarchical( $post->post_type ) ) {
8✔
687
                        if ( current_user_can( 'edit_others_pages' ) ) {
×
688
                                $new_post_author_id = $post->post_author;
×
689
                        }
690
                }
691
                elseif ( current_user_can( 'edit_others_posts' ) ) {
8✔
692
                        $new_post_author_id = $post->post_author;
4✔
693
                }
694
        }
695

696
        $menu_order             = ( (int) get_option( 'duplicate_post_copymenuorder' ) === 1 ) ? $post->menu_order : 0;
196✔
697
        $increase_menu_order_by = get_option( 'duplicate_post_increase_menu_order_by' );
196✔
698
        if ( ! empty( $increase_menu_order_by ) && is_numeric( $increase_menu_order_by ) ) {
196✔
699
                $menu_order += (int) $increase_menu_order_by;
8✔
700
        }
701

702
        $post_name = $post->post_name;
196✔
703
        if ( (int) get_option( 'duplicate_post_copyslug' ) !== 1 ) {
196✔
704
                $post_name = '';
192✔
705
        }
706
        $new_post_parent = empty( $parent_id ) ? $post->post_parent : $parent_id;
196✔
707

708
        $new_post = [
196✔
709
                'menu_order'            => $menu_order,
196✔
710
                'comment_status'        => $post->comment_status,
196✔
711
                'ping_status'           => $post->ping_status,
196✔
712
                'post_author'           => $new_post_author_id,
196✔
713
                'post_content'          => ( (int) get_option( 'duplicate_post_copycontent' ) === 1 ) ? $post->post_content : '',
196✔
714
                'post_content_filtered' => ( (int) get_option( 'duplicate_post_copycontent' ) === 1 ) ? $post->post_content_filtered : '',
196✔
715
                'post_excerpt'          => ( (int) get_option( 'duplicate_post_copyexcerpt' ) === 1 ) ? $post->post_excerpt : '',
196✔
716
                'post_mime_type'        => $post->post_mime_type,
196✔
717
                'post_parent'           => $new_post_parent,
196✔
718
                'post_password'         => ( (int) get_option( 'duplicate_post_copypassword' ) === 1 ) ? $post->post_password : '',
196✔
719
                'post_status'           => $new_post_status,
196✔
720
                'post_title'            => $title,
196✔
721
                'post_type'             => $post->post_type,
196✔
722
                'post_name'             => $post_name,
196✔
723
        ];
196✔
724

725
        if ( (int) get_option( 'duplicate_post_copydate' ) === 1 ) {
196✔
726
                $new_post_date             = $post->post_date;
4✔
727
                $new_post['post_date']     = $new_post_date;
4✔
728
                $new_post['post_date_gmt'] = get_gmt_from_date( $new_post_date );
4✔
729
        }
730

731
        /**
732
         * Filter new post values.
733
         *
734
         * @param array   $new_post New post values.
735
         * @param WP_Post $post     Original post object.
736
         *
737
         * @return array
738
         */
739
        $new_post    = apply_filters( 'duplicate_post_new_post', $new_post, $post );
196✔
740
        $new_post_id = wp_insert_post( wp_slash( $new_post ), true );
196✔
741

742
        // If you have written a plugin which uses non-WP database tables to save
743
        // information about a post you can hook this action to dupe that data.
744
        if ( $new_post_id !== 0 && ! is_wp_error( $new_post_id ) ) {
196✔
745

746
                /**
747
                 * Fires after a post has been duplicated.
748
                 *
749
                 * @param int     $new_post_id The ID of the new post.
750
                 * @param WP_Post $post        The original post object.
751
                 * @param string  $status      The status of the new post.
752
                 * @param string  $post_type   The post type of the duplicated post.
753
                 */
754
                do_action( 'duplicate_post_after_duplicated', $new_post_id, $post, $status, $post->post_type );
196✔
755

756
                // Deprecated hooks for backward compatibility.
757
                if ( $post->post_type === 'page' || is_post_type_hierarchical( $post->post_type ) ) {
196✔
758
                        do_action_deprecated( 'dp_duplicate_page', [ $new_post_id, $post, $status ], 'Yoast Duplicate Post 4.6', 'duplicate_post_after_duplicated' );
16✔
759
                }
760
                else {
761
                        do_action_deprecated( 'dp_duplicate_post', [ $new_post_id, $post, $status ], 'Yoast Duplicate Post 4.6', 'duplicate_post_after_duplicated' );
180✔
762
                }
763

764
                delete_post_meta( $new_post_id, '_dp_original' );
196✔
765
                add_post_meta( $new_post_id, '_dp_original', $post->ID );
196✔
766
        }
767

768
        /**
769
         * Fires after duplicating a post.
770
         *
771
         * @param int|WP_Error $new_post_id The new post id or WP_Error object on error.
772
         * @param WP_Post      $post        The original post object.
773
         * @param bool         $status      The intended destination status.
774
         * @param int          $parent_id   The parent post ID if we are calling this recursively.
775
         */
776
        do_action( 'duplicate_post_post_copy', $new_post_id, $post, $status, $parent_id );
196✔
777

778
        return $new_post_id;
196✔
779
}
780

781
/**
782
 * Adds some links on the plugin page.
783
 *
784
 * @param array<string> $links The links array.
785
 * @param string        $file  The file name.
786
 * @return array<string>
787
 */
788
function duplicate_post_add_plugin_links( $links, $file ) {
789
        if ( plugin_basename( __DIR__ . '/duplicate-post.php' ) === $file ) {
×
790
                $links[] = '<a href="https://yoa.st/4jr">' . esc_html__( 'Documentation', 'duplicate-post' ) . '</a>';
×
791
        }
792
        return $links;
×
793
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc