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

Yoast / duplicate-post / 7229177020

16 Dec 2023 02:19AM UTC coverage: 50.203%. Remained the same
7229177020

push

github

web-flow
Merge pull request #351 from Yoast/JRF/qa-use-import-use

CS/QA: use import `use` statements

0 of 1 new or added line in 1 file covered. (0.0%)

31 existing lines in 1 file now uncovered.

1236 of 2462 relevant lines covered (50.2%)

1.62 hits per line

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

0.0
/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
function duplicate_post_get_installed_version() {
25
        return get_option( 'duplicate_post_version' );
×
26
}
27

28
/**
29
 * Wrapper for the defined constant DUPLICATE_POST_CURRENT_VERSION.
30
 */
31
function duplicate_post_get_current_version() {
32
        return DUPLICATE_POST_CURRENT_VERSION;
×
33
}
34

35
add_action( 'admin_init', 'duplicate_post_admin_init' );
×
36

37
/**
38
 * Adds handlers depending on the options.
39
 */
40
function duplicate_post_admin_init() {
41
        duplicate_post_plugin_upgrade();
×
42

43
        if ( intval( get_site_option( 'duplicate_post_show_notice' ) ) === 1 ) {
×
44
                if ( is_multisite() ) {
×
45
                        add_action( 'network_admin_notices', 'duplicate_post_show_update_notice' );
×
46
                }
47
                else {
48
                        add_action( 'admin_notices', 'duplicate_post_show_update_notice' );
×
49
                }
50
                add_action( 'wp_ajax_duplicate_post_dismiss_notice', 'duplicate_post_dismiss_notice' );
×
51
        }
52

53
        add_action( 'dp_duplicate_post', 'duplicate_post_copy_post_meta_info', 10, 2 );
×
54
        add_action( 'dp_duplicate_page', 'duplicate_post_copy_post_meta_info', 10, 2 );
×
55

56
        if ( intval( get_option( 'duplicate_post_copychildren' ) ) === 1 ) {
×
57
                add_action( 'dp_duplicate_post', 'duplicate_post_copy_children', 20, 3 );
×
58
                add_action( 'dp_duplicate_page', 'duplicate_post_copy_children', 20, 3 );
×
59
        }
60

61
        if ( intval( get_option( 'duplicate_post_copyattachments' ) ) === 1 ) {
×
62
                add_action( 'dp_duplicate_post', 'duplicate_post_copy_attachments', 30, 2 );
×
63
                add_action( 'dp_duplicate_page', 'duplicate_post_copy_attachments', 30, 2 );
×
64
        }
65

66
        if ( intval( get_option( 'duplicate_post_copycomments' ) ) === 1 ) {
×
67
                add_action( 'dp_duplicate_post', 'duplicate_post_copy_comments', 40, 2 );
×
68
                add_action( 'dp_duplicate_page', 'duplicate_post_copy_comments', 40, 2 );
×
69
        }
70

71
        add_action( 'dp_duplicate_post', 'duplicate_post_copy_post_taxonomies', 50, 2 );
×
72
        add_action( 'dp_duplicate_page', 'duplicate_post_copy_post_taxonomies', 50, 2 );
×
73

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

77
/**
78
 * Plugin upgrade.
79
 */
80
function duplicate_post_plugin_upgrade() {
81
        $installed_version = duplicate_post_get_installed_version();
×
82

83
        if ( duplicate_post_get_current_version() === $installed_version ) {
×
84
                return;
×
85
        }
86

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

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

108
        $show_links_in_defaults = [
×
109
                'row'         => '1',
×
110
                'adminbar'    => '1',
×
111
                'submitbox'   => '1',
×
112
                'bulkactions' => '1',
×
113
        ];
×
114

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

146
        $taxonomies_blacklist = get_option( 'duplicate_post_taxonomies_blacklist' );
×
147
        if ( $taxonomies_blacklist === '' ) {
×
148
                $taxonomies_blacklist = [];
×
149
        }
150
        if ( in_array( 'post_format', $taxonomies_blacklist, true ) ) {
×
151
                update_option( 'duplicate_post_copyformat', 0 );
×
152
                $taxonomies_blacklist = array_diff( $taxonomies_blacklist, [ 'post_format' ] );
×
153
                update_option( 'duplicate_post_taxonomies_blacklist', $taxonomies_blacklist );
×
154
        }
155

156
        $meta_blacklist = explode( ',', get_option( 'duplicate_post_blacklist' ) );
×
157
        $meta_blacklist = array_map( 'trim', $meta_blacklist );
×
158
        if ( in_array( '_wp_page_template', $meta_blacklist, true ) ) {
×
159
                update_option( 'duplicate_post_copytemplate', 0 );
×
160
                $meta_blacklist = array_diff( $meta_blacklist, [ '_wp_page_template' ] );
×
161
        }
162
        if ( in_array( '_thumbnail_id', $meta_blacklist, true ) ) {
×
163
                update_option( 'duplicate_post_copythumbnail', 0 );
×
164
                $meta_blacklist = array_diff( $meta_blacklist, [ '_thumbnail_id' ] );
×
165
        }
166
        update_option( 'duplicate_post_blacklist', implode( ',', $meta_blacklist ) );
×
167

168
        if ( version_compare( $installed_version, '4.0.0' ) < 0 ) {
×
169
                // Migrate the 'Show links in' options to the new array-based structure.
170
                duplicate_post_migrate_show_links_in_options( $show_links_in_defaults );
×
171
        }
172

173
        delete_site_option( 'duplicate_post_version' );
×
174
        update_option( 'duplicate_post_version', duplicate_post_get_current_version() );
×
175
}
176

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

192
        $new_options = [];
×
193
        foreach ( $options_to_migrate as $old => $new ) {
×
194
                $new_options[ $new ] = get_option( $old, $defaults[ $new ] );
×
195

196
                delete_option( $old );
×
197
        }
198

199
        update_option( 'duplicate_post_show_link_in', $new_options );
×
200
}
201

202
/**
203
 * Shows the welcome notice.
204
 *
205
 * @global string $wp_version The WordPress version string.
206
 */
207
function duplicate_post_show_update_notice() {
208
        if ( ! current_user_can( 'manage_options' ) ) {
×
209
                return;
×
210
        }
211

212
        $current_screen = get_current_screen();
×
213
        if ( empty( $current_screen )
×
214
                || empty( $current_screen->base )
×
215
                || ( $current_screen->base !== 'dashboard' && $current_screen->base !== 'plugins' )
×
216
        ) {
217
                return;
×
218
        }
219

220
        $title = sprintf(
×
221
                /* translators: %s: Yoast Duplicate Post. */
222
                esc_html__( 'You\'ve successfully installed %s!', 'duplicate-post' ),
×
223
                'Yoast Duplicate Post'
×
224
        );
×
225

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

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

236
        echo "<script>
×
237
                        function duplicate_post_dismiss_notice(){
238
                                var data = {
239
                                'action': 'duplicate_post_dismiss_notice',
240
                                };
241

242
                                jQuery.post(ajaxurl, data, function(response) {
243
                                        jQuery('#duplicate-post-notice').hide();
244
                                });
245
                        }
246

247
                        jQuery(document).ready(function(){
248
                                jQuery('body').on('click', '.notice-dismiss', function(){
249
                                        duplicate_post_dismiss_notice();
250
                                });
251
                        });
252
                        </script>";
×
253
}
254

255
/**
256
 * Dismisses the notice.
257
 *
258
 * @return bool
259
 */
260
function duplicate_post_dismiss_notice() {
261
        return update_site_option( 'duplicate_post_show_notice', 0 );
×
262
}
263

264
/**
265
 * Copies the taxonomies of a post to another post.
266
 *
267
 * @global wpdb $wpdb WordPress database abstraction object.
268
 *
269
 * @param int     $new_id New post ID.
270
 * @param WP_Post $post   The original post object.
271
 */
272
function duplicate_post_copy_post_taxonomies( $new_id, $post ) {
273
        global $wpdb;
×
274
        if ( isset( $wpdb->terms ) ) {
×
275
                // Clear default category (added by wp_insert_post).
276
                wp_set_object_terms( $new_id, [], 'category' );
×
277

278
                $post_taxonomies = get_object_taxonomies( $post->post_type );
×
279
                // Several plugins just add support to post-formats but don't register post_format taxonomy.
280
                if ( post_type_supports( $post->post_type, 'post-formats' ) && ! in_array( 'post_format', $post_taxonomies, true ) ) {
×
281
                        $post_taxonomies[] = 'post_format';
×
282
                }
283

284
                $taxonomies_blacklist = get_option( 'duplicate_post_taxonomies_blacklist' );
×
285
                if ( $taxonomies_blacklist === '' ) {
×
286
                        $taxonomies_blacklist = [];
×
287
                }
288
                if ( intval( get_option( 'duplicate_post_copyformat' ) ) === 0 ) {
×
289
                        $taxonomies_blacklist[] = 'post_format';
×
290
                }
291

292
                /**
293
                 * Filters the taxonomy excludelist when copying a post.
294
                 *
295
                 * @param array $taxonomies_blacklist The taxonomy excludelist from the options.
296
                 *
297
                 * @return array
298
                 */
299
                $taxonomies_blacklist = apply_filters( 'duplicate_post_taxonomies_excludelist_filter', $taxonomies_blacklist );
×
300

301
                $taxonomies = array_diff( $post_taxonomies, $taxonomies_blacklist );
×
302
                foreach ( $taxonomies as $taxonomy ) {
×
303
                        $post_terms = wp_get_object_terms( $post->ID, $taxonomy, [ 'orderby' => 'term_order' ] );
×
304
                        $terms      = [];
×
305
                        $num_terms  = count( $post_terms );
×
306
                        for ( $i = 0; $i < $num_terms; $i++ ) {
×
307
                                $terms[] = $post_terms[ $i ]->slug;
×
308
                        }
309
                        wp_set_object_terms( $new_id, $terms, $taxonomy );
×
310
                }
311
        }
312
}
313

314
/**
315
 * Copies the meta information of a post to another post
316
 *
317
 * @param int     $new_id The new post ID.
318
 * @param WP_Post $post   The original post object.
319
 */
320
function duplicate_post_copy_post_meta_info( $new_id, $post ) {
321
        $post_meta_keys = get_post_custom_keys( $post->ID );
×
322
        if ( empty( $post_meta_keys ) ) {
×
323
                return;
×
324
        }
325
        $meta_blacklist = get_option( 'duplicate_post_blacklist' );
×
326
        if ( $meta_blacklist === '' ) {
×
327
                $meta_blacklist = [];
×
328
        }
329
        else {
330
                $meta_blacklist = explode( ',', $meta_blacklist );
×
331
                $meta_blacklist = array_filter( $meta_blacklist );
×
332
                $meta_blacklist = array_map( 'trim', $meta_blacklist );
×
333
        }
334
        $meta_blacklist[] = '_edit_lock'; // Edit lock.
×
335
        $meta_blacklist[] = '_edit_last'; // Edit lock.
×
336
        $meta_blacklist[] = '_dp_is_rewrite_republish_copy';
×
337
        $meta_blacklist[] = '_dp_has_rewrite_republish_copy';
×
338
        if ( intval( get_option( 'duplicate_post_copytemplate' ) ) === 0 ) {
×
339
                $meta_blacklist[] = '_wp_page_template';
×
340
        }
341
        if ( intval( get_option( 'duplicate_post_copythumbnail' ) ) === 0 ) {
×
342
                $meta_blacklist[] = '_thumbnail_id';
×
343
        }
344

345
        /**
346
         * Filters the meta fields excludelist when copying a post.
347
         *
348
         * @param array $meta_blacklist The meta fields excludelist from the options.
349
         *
350
         * @return array
351
         */
352
        $meta_blacklist = apply_filters( 'duplicate_post_excludelist_filter', $meta_blacklist );
×
353

354
        $meta_blacklist_string = '(' . implode( ')|(', $meta_blacklist ) . ')';
×
355
        if ( strpos( $meta_blacklist_string, '*' ) !== false ) {
×
356
                $meta_blacklist_string = str_replace( [ '*' ], [ '[a-zA-Z0-9_]*' ], $meta_blacklist_string );
×
357

358
                $meta_keys = [];
×
359
                foreach ( $post_meta_keys as $meta_key ) {
×
360
                        if ( ! preg_match( '#^(' . $meta_blacklist_string . ')$#', $meta_key ) ) {
×
361
                                $meta_keys[] = $meta_key;
×
362
                        }
363
                }
364
        }
365
        else {
366
                $meta_keys = array_diff( $post_meta_keys, $meta_blacklist );
×
367
        }
368

369
        /**
370
         * Filters the list of meta fields names when copying a post.
371
         *
372
         * @param array $meta_keys The list of meta fields name, with the ones in the excludelist already removed.
373
         *
374
         * @return array
375
         */
376
        $meta_keys = apply_filters( 'duplicate_post_meta_keys_filter', $meta_keys );
×
377

378
        foreach ( $meta_keys as $meta_key ) {
×
379
                $meta_values = get_post_custom_values( $meta_key, $post->ID );
×
380
                foreach ( $meta_values as $meta_value ) {
×
381
                        $meta_value = maybe_unserialize( $meta_value );
×
382
                        add_post_meta( $new_id, $meta_key, duplicate_post_wp_slash( $meta_value ) );
×
383
                }
384
        }
385
}
386

387
/**
388
 * Workaround for inconsistent wp_slash.
389
 * Works only with WP 4.4+ (map_deep)
390
 *
391
 * @param mixed $value Array or object to be recursively slashed.
392
 * @return string|mixed
393
 */
394
function duplicate_post_addslashes_deep( $value ) {
395
        if ( function_exists( 'map_deep' ) ) {
×
396
                return map_deep( $value, 'duplicate_post_addslashes_to_strings_only' );
×
397
        }
398
        else {
399
                return wp_slash( $value );
×
400
        }
401
}
402

403
/**
404
 * Adds slashes only to strings.
405
 *
406
 * @param mixed $value Value to slash only if string.
407
 * @return string|mixed
408
 */
409
function duplicate_post_addslashes_to_strings_only( $value ) {
NEW
410
        return Utils::addslashes_to_strings_only( $value );
×
411
}
412

413
/**
414
 * Replacement function for faulty core wp_slash().
415
 *
416
 * @param mixed $value What to add slash to.
417
 * @return mixed
418
 */
419
function duplicate_post_wp_slash( $value ) {
420
        return duplicate_post_addslashes_deep( $value );
×
421
}
422

423
/**
424
 * Copies attachments, including physical files.
425
 *
426
 * @param int     $new_id The new post ID.
427
 * @param WP_Post $post   The original post object.
428
 */
429
function duplicate_post_copy_attachments( $new_id, $post ) {
430
        // Get thumbnail ID.
431
        $old_thumbnail_id = get_post_thumbnail_id( $post->ID );
×
432
        // Get children.
433
        $children = get_posts(
×
434
                [
×
435
                        'post_type'   => 'any',
×
436
                        'numberposts' => -1,
×
437
                        'post_status' => 'any',
×
438
                        'post_parent' => $post->ID,
×
439
                ]
×
440
        );
×
441
        // Clone old attachments.
442
        foreach ( $children as $child ) {
×
443
                if ( $child->post_type !== 'attachment' ) {
×
444
                        continue;
×
445
                }
446
                $url = wp_get_attachment_url( $child->ID );
×
447
                // Let's copy the actual file.
448
                $tmp = download_url( $url );
×
449
                if ( is_wp_error( $tmp ) ) {
×
450
                        continue;
×
451
                }
452

453
                $desc = wp_slash( $child->post_content );
×
454

455
                $file_array             = [];
×
456
                $file_array['name']     = basename( $url );
×
457
                $file_array['tmp_name'] = $tmp;
×
458
                // "Upload" to the media collection
459
                $new_attachment_id = media_handle_sideload( $file_array, $new_id, $desc );
×
460

461
                if ( is_wp_error( $new_attachment_id ) ) {
×
462
                        unlink( $file_array['tmp_name'] );
×
463
                        continue;
×
464
                }
465
                $new_post_author = wp_get_current_user();
×
466
                $cloned_child    = [
×
467
                        'ID'           => $new_attachment_id,
×
468
                        'post_title'   => $child->post_title,
×
469
                        'post_exceprt' => $child->post_title,
×
470
                        'post_author'  => $new_post_author->ID,
×
471
                ];
×
472
                wp_update_post( wp_slash( $cloned_child ) );
×
473

474
                $alt_title = get_post_meta( $child->ID, '_wp_attachment_image_alt', true );
×
475
                if ( $alt_title ) {
×
476
                        update_post_meta( $new_attachment_id, '_wp_attachment_image_alt', wp_slash( $alt_title ) );
×
477
                }
478

479
                // If we have cloned the post thumbnail, set the copy as the thumbnail for the new post.
480
                if ( intval( get_option( 'duplicate_post_copythumbnail' ) ) === 1 && $old_thumbnail_id === $child->ID ) {
×
481
                        set_post_thumbnail( $new_id, $new_attachment_id );
×
482
                }
483
        }
484
}
485

486
/**
487
 * Copies child posts.
488
 *
489
 * @param int     $new_id The new post ID.
490
 * @param WP_Post $post   The original post object.
491
 * @param string  $status Optional. The destination status.
492
 */
493
function duplicate_post_copy_children( $new_id, $post, $status = '' ) {
494
        // Get children.
495
        $children = get_posts(
×
496
                [
×
497
                        'post_type'   => 'any',
×
498
                        'numberposts' => -1,
×
499
                        'post_status' => 'any',
×
500
                        'post_parent' => $post->ID,
×
501
                ]
×
502
        );
×
503

504
        foreach ( $children as $child ) {
×
505
                if ( $child->post_type === 'attachment' ) {
×
506
                        continue;
×
507
                }
508
                duplicate_post_create_duplicate( $child, $status, $new_id );
×
509
        }
510
}
511

512
/**
513
 * Copies comments.
514
 *
515
 * @param int     $new_id The new post ID.
516
 * @param WP_Post $post   The original post object.
517
 */
518
function duplicate_post_copy_comments( $new_id, $post ) {
519
        $comments = get_comments(
×
520
                [
×
521
                        'post_id' => $post->ID,
×
522
                        'order'   => 'ASC',
×
523
                        'orderby' => 'comment_date_gmt',
×
524
                ]
×
525
        );
×
526

527
        $old_id_to_new = [];
×
528
        foreach ( $comments as $comment ) {
×
529
                // Do not copy pingbacks or trackbacks.
530
                if ( $comment->comment_type === 'pingback' || $comment->comment_type === 'trackback' ) {
×
531
                        continue;
×
532
                }
533
                $parent      = ( $comment->comment_parent && $old_id_to_new[ $comment->comment_parent ] ) ? $old_id_to_new[ $comment->comment_parent ] : 0;
×
534
                $commentdata = [
×
535
                        'comment_post_ID'      => $new_id,
×
536
                        'comment_author'       => $comment->comment_author,
×
537
                        'comment_author_email' => $comment->comment_author_email,
×
538
                        'comment_author_url'   => $comment->comment_author_url,
×
539
                        'comment_content'      => $comment->comment_content,
×
540
                        'comment_type'         => $comment->comment_type,
×
541
                        'comment_parent'       => $parent,
×
542
                        'user_id'              => $comment->user_id,
×
543
                        'comment_author_IP'    => $comment->comment_author_IP,
×
544
                        'comment_agent'        => $comment->comment_agent,
×
545
                        'comment_karma'        => $comment->comment_karma,
×
546
                        'comment_approved'     => $comment->comment_approved,
×
547
                ];
×
548
                if ( intval( get_option( 'duplicate_post_copydate' ) ) === 1 ) {
×
549
                        $commentdata['comment_date']     = $comment->comment_date;
×
550
                        $commentdata['comment_date_gmt'] = get_gmt_from_date( $comment->comment_date );
×
551
                }
552
                $new_comment_id = wp_insert_comment( $commentdata );
×
553
                $commentmeta    = get_comment_meta( $new_comment_id );
×
554
                foreach ( $commentmeta as $meta_key => $meta_value ) {
×
555
                        add_comment_meta( $new_comment_id, $meta_key, duplicate_post_wp_slash( $meta_value ) );
×
556
                }
557
                $old_id_to_new[ $comment->comment_ID ] = $new_comment_id;
×
558
        }
559
}
560

561
/**
562
 * Creates a duplicate from a post.
563
 *
564
 * This is the main functions that does the cloning.
565
 *
566
 * @param WP_Post $post      The original post object.
567
 * @param string  $status    Optional. The intended destination status.
568
 * @param string  $parent_id Optional. The parent post ID if we are calling this recursively.
569
 * @return int|WP_Error
570
 */
571
function duplicate_post_create_duplicate( $post, $status = '', $parent_id = '' ) {
572
        /**
573
         * Fires before duplicating a post.
574
         *
575
         * @param WP_Post $post      The original post object.
576
         * @param bool    $status    The intended destination status.
577
         * @param int     $parent_id The parent post ID if we are calling this recursively.
578
         */
579
        do_action( 'duplicate_post_pre_copy', $post, $status, $parent_id );
×
580

581
        /**
582
         * Filter allowing to copy post.
583
         *
584
         * @param bool    $can_duplicate Default to `true`.
585
         * @param WP_Post $post          The original post object.
586
         * @param bool    $status        The intended destination status.
587
         * @param int     $parent_id     The parent post ID if we are calling this recursively.
588
         *
589
         * @return bool
590
         */
591
        $can_duplicate = apply_filters( 'duplicate_post_allow', true, $post, $status, $parent_id );
×
592
        if ( ! $can_duplicate ) {
×
593
                wp_die( esc_html( __( 'You aren\'t allowed to duplicate this post', 'duplicate-post' ) ) );
×
594
        }
595

596
        if ( ! duplicate_post_is_post_type_enabled( $post->post_type ) && $post->post_type !== 'attachment' ) {
×
597
                wp_die(
×
598
                        esc_html(
×
599
                                __( 'Copy features for this post type are not enabled in options page', 'duplicate-post' ) . ': '
×
600
                                . $post->post_type
×
601
                        )
×
602
                );
×
603
        }
604

605
        $new_post_status = ( empty( $status ) ) ? $post->post_status : $status;
×
606
        $title           = ' ';
×
607

608
        if ( $post->post_type !== 'attachment' ) {
×
609
                $prefix = sanitize_text_field( get_option( 'duplicate_post_title_prefix' ) );
×
610
                $suffix = sanitize_text_field( get_option( 'duplicate_post_title_suffix' ) );
×
611
                if ( intval( get_option( 'duplicate_post_copytitle' ) ) === 1 ) {
×
612
                        $title = $post->post_title;
×
613
                        if ( ! empty( $prefix ) ) {
×
614
                                $prefix .= ' ';
×
615
                        }
616
                        if ( ! empty( $suffix ) ) {
×
617
                                $suffix = ' ' . $suffix;
×
618
                        }
619
                }
620
                else {
621
                        $title = ' ';
×
622
                }
623
                $title = trim( $prefix . $title . $suffix );
×
624

625
                /*
626
                 * Not sure we should force a title. Instead, we should respect what WP does.
627
                 * if ( '' === $title ) {
628
                 *  // empty title.
629
                 *  $title = __( 'Untitled', 'default' );
630
                 * }
631
                 */
632

633
                if ( intval( get_option( 'duplicate_post_copystatus' ) ) === 0 ) {
×
634
                        $new_post_status = 'draft';
×
635
                }
636
                elseif ( $new_post_status === 'publish' || $new_post_status === 'future' ) {
×
637
                        // Check if the user has the right capability.
638
                        if ( is_post_type_hierarchical( $post->post_type ) ) {
×
639
                                if ( ! current_user_can( 'publish_pages' ) ) {
×
640
                                        $new_post_status = 'pending';
×
641
                                }
642
                        }
643
                        elseif ( ! current_user_can( 'publish_posts' ) ) {
×
644
                                $new_post_status = 'pending';
×
645
                        }
646
                }
647
        }
648

649
        $new_post_author    = wp_get_current_user();
×
650
        $new_post_author_id = $new_post_author->ID;
×
651
        if ( intval( get_option( 'duplicate_post_copyauthor' ) ) === 1 ) {
×
652
                // Check if the user has the right capability.
653
                if ( is_post_type_hierarchical( $post->post_type ) ) {
×
654
                        if ( current_user_can( 'edit_others_pages' ) ) {
×
655
                                $new_post_author_id = $post->post_author;
×
656
                        }
657
                }
658
                elseif ( current_user_can( 'edit_others_posts' ) ) {
×
659
                        $new_post_author_id = $post->post_author;
×
660
                }
661
        }
662

663
        $menu_order             = ( intval( get_option( 'duplicate_post_copymenuorder' ) ) === 1 ) ? $post->menu_order : 0;
×
664
        $increase_menu_order_by = get_option( 'duplicate_post_increase_menu_order_by' );
×
665
        if ( ! empty( $increase_menu_order_by ) && is_numeric( $increase_menu_order_by ) ) {
×
666
                $menu_order += intval( $increase_menu_order_by );
×
667
        }
668

669
        $post_name = $post->post_name;
×
670
        if ( intval( get_option( 'duplicate_post_copyslug' ) ) !== 1 ) {
×
671
                $post_name = '';
×
672
        }
673
        $new_post_parent = empty( $parent_id ) ? $post->post_parent : $parent_id;
×
674

675
        $new_post = [
×
676
                'menu_order'            => $menu_order,
×
677
                'comment_status'        => $post->comment_status,
×
678
                'ping_status'           => $post->ping_status,
×
679
                'post_author'           => $new_post_author_id,
×
680
                'post_content'          => ( intval( get_option( 'duplicate_post_copycontent' ) ) === 1 ) ? $post->post_content : '',
×
681
                'post_content_filtered' => ( intval( get_option( 'duplicate_post_copycontent' ) ) === 1 ) ? $post->post_content_filtered : '',
×
682
                'post_excerpt'          => ( intval( get_option( 'duplicate_post_copyexcerpt' ) ) === 1 ) ? $post->post_excerpt : '',
×
683
                'post_mime_type'        => $post->post_mime_type,
×
684
                'post_parent'           => $new_post_parent,
×
685
                'post_password'         => ( intval( get_option( 'duplicate_post_copypassword' ) ) === 1 ) ? $post->post_password : '',
×
686
                'post_status'           => $new_post_status,
×
687
                'post_title'            => $title,
×
688
                'post_type'             => $post->post_type,
×
689
                'post_name'             => $post_name,
×
690
        ];
×
691

692
        if ( intval( get_option( 'duplicate_post_copydate' ) ) === 1 ) {
×
693
                $new_post_date             = $post->post_date;
×
694
                $new_post['post_date']     = $new_post_date;
×
695
                $new_post['post_date_gmt'] = get_gmt_from_date( $new_post_date );
×
696
        }
697

698
        /**
699
         * Filter new post values.
700
         *
701
         * @param array   $new_post New post values.
702
         * @param WP_Post $post     Original post object.
703
         *
704
         * @return array
705
         */
706
        $new_post    = apply_filters( 'duplicate_post_new_post', $new_post, $post );
×
707
        $new_post_id = wp_insert_post( wp_slash( $new_post ), true );
×
708

709
        // If you have written a plugin which uses non-WP database tables to save
710
        // information about a post you can hook this action to dupe that data.
711
        if ( $new_post_id !== 0 && ! is_wp_error( $new_post_id ) ) {
×
712

713
                if ( $post->post_type === 'page' || is_post_type_hierarchical( $post->post_type ) ) {
×
714
                        do_action( 'dp_duplicate_page', $new_post_id, $post, $status );
×
715
                }
716
                else {
717
                        do_action( 'dp_duplicate_post', $new_post_id, $post, $status );
×
718
                }
719

720
                delete_post_meta( $new_post_id, '_dp_original' );
×
721
                add_post_meta( $new_post_id, '_dp_original', $post->ID );
×
722
        }
723

724
        /**
725
         * Fires after duplicating a post.
726
         *
727
         * @param int|WP_Error $new_post_id The new post id or WP_Error object on error.
728
         * @param WP_Post      $post        The original post object.
729
         * @param bool         $status      The intended destination status.
730
         * @param int          $parent_id   The parent post ID if we are calling this recursively.
731
         */
732
        do_action( 'duplicate_post_post_copy', $new_post_id, $post, $status, $parent_id );
×
733

734
        return $new_post_id;
×
735
}
736

737
/**
738
 * Adds some links on the plugin page.
739
 *
740
 * @param array  $links The links array.
741
 * @param string $file  The file name.
742
 * @return array
743
 */
744
function duplicate_post_add_plugin_links( $links, $file ) {
745
        if ( plugin_basename( __DIR__ . '/duplicate-post.php' ) === $file ) {
×
746
                $links[] = '<a href="https://yoast.com/wordpress/plugins/duplicate-post">' . esc_html__( 'Documentation', 'duplicate-post' ) . '</a>';
×
747
        }
748
        return $links;
×
749
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc