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

Yoast / duplicate-post / 8391281769

22 Mar 2024 01:44PM UTC coverage: 50.142% (-0.08%) from 50.223%
8391281769

push

github

web-flow
Merge pull request #360 from michael-sumner/trunk

Fix Possible NULL Value of `duplicate_post_taxonomies_blacklist`

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

2 existing lines in 1 file now uncovered.

1237 of 2467 relevant lines covered (50.14%)

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
 * @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 ( intval( 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( 'dp_duplicate_post', 'duplicate_post_copy_post_meta_info', 10, 2 );
×
60
        add_action( 'dp_duplicate_page', 'duplicate_post_copy_post_meta_info', 10, 2 );
×
61

62
        if ( intval( get_option( 'duplicate_post_copychildren' ) ) === 1 ) {
×
63
                add_action( 'dp_duplicate_post', 'duplicate_post_copy_children', 20, 3 );
×
64
                add_action( 'dp_duplicate_page', 'duplicate_post_copy_children', 20, 3 );
×
65
        }
66

67
        if ( intval( get_option( 'duplicate_post_copyattachments' ) ) === 1 ) {
×
68
                add_action( 'dp_duplicate_post', 'duplicate_post_copy_attachments', 30, 2 );
×
69
                add_action( 'dp_duplicate_page', 'duplicate_post_copy_attachments', 30, 2 );
×
70
        }
71

72
        if ( intval( get_option( 'duplicate_post_copycomments' ) ) === 1 ) {
×
73
                add_action( 'dp_duplicate_post', 'duplicate_post_copy_comments', 40, 2 );
×
74
                add_action( 'dp_duplicate_page', 'duplicate_post_copy_comments', 40, 2 );
×
75
        }
76

77
        add_action( 'dp_duplicate_post', 'duplicate_post_copy_post_taxonomies', 50, 2 );
×
78
        add_action( 'dp_duplicate_page', 'duplicate_post_copy_post_taxonomies', 50, 2 );
×
79

80
        add_filter( 'plugin_row_meta', 'duplicate_post_add_plugin_links', 10, 2 );
×
81
}
82

83
/**
84
 * Plugin upgrade.
85
 *
86
 * @return void
87
 */
88
function duplicate_post_plugin_upgrade() {
89
        $installed_version = duplicate_post_get_installed_version();
×
90

91
        if ( duplicate_post_get_current_version() === $installed_version ) {
×
92
                return;
×
93
        }
94

95
        if ( empty( $installed_version ) ) {
×
96
                // Get default roles.
97
                $default_roles = [
×
98
                        'editor',
×
99
                        'administrator',
×
100
                        'wpseo_manager',
×
101
                        'wpseo_editor',
×
102
                ];
×
103

104
                foreach ( $default_roles as $name ) {
×
105
                        $role = get_role( $name );
×
106
                        if ( ! empty( $role ) ) {
×
107
                                $role->add_cap( 'copy_posts' );
×
108
                        }
109
                }
110
                add_option( 'duplicate_post_show_notice', 1 );
×
111
        }
112
        else {
113
                update_option( 'duplicate_post_show_notice', 0 );
×
114
        }
115

116
        $show_links_in_defaults = [
×
117
                'row'         => '1',
×
118
                'adminbar'    => '1',
×
119
                'submitbox'   => '1',
×
120
                'bulkactions' => '1',
×
121
        ];
×
122

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

NEW
154
        $taxonomies_blacklist = get_option( 'duplicate_post_taxonomies_blacklist', [] );
×
NEW
155
        if ( empty( $taxonomies_blacklist ) ) {
×
UNCOV
156
                $taxonomies_blacklist = [];
×
157
        }
NEW
158
        elseif ( ! is_array( $taxonomies_blacklist ) ) {
×
NEW
159
                $taxonomies_blacklist = [ $taxonomies_blacklist ];
×
160
        }
161
        if ( in_array( 'post_format', $taxonomies_blacklist, true ) ) {
×
162
                update_option( 'duplicate_post_copyformat', 0 );
×
163
                $taxonomies_blacklist = array_diff( $taxonomies_blacklist, [ 'post_format' ] );
×
164
                update_option( 'duplicate_post_taxonomies_blacklist', $taxonomies_blacklist );
×
165
        }
166

167
        $meta_blacklist = explode( ',', get_option( 'duplicate_post_blacklist' ) );
×
168
        $meta_blacklist = array_map( 'trim', $meta_blacklist );
×
169
        if ( in_array( '_wp_page_template', $meta_blacklist, true ) ) {
×
170
                update_option( 'duplicate_post_copytemplate', 0 );
×
171
                $meta_blacklist = array_diff( $meta_blacklist, [ '_wp_page_template' ] );
×
172
        }
173
        if ( in_array( '_thumbnail_id', $meta_blacklist, true ) ) {
×
174
                update_option( 'duplicate_post_copythumbnail', 0 );
×
175
                $meta_blacklist = array_diff( $meta_blacklist, [ '_thumbnail_id' ] );
×
176
        }
177
        update_option( 'duplicate_post_blacklist', implode( ',', $meta_blacklist ) );
×
178

179
        if ( version_compare( $installed_version, '4.0.0' ) < 0 ) {
×
180
                // Migrate the 'Show links in' options to the new array-based structure.
181
                duplicate_post_migrate_show_links_in_options( $show_links_in_defaults );
×
182
        }
183

184
        delete_site_option( 'duplicate_post_version' );
×
185
        update_option( 'duplicate_post_version', duplicate_post_get_current_version() );
×
186
}
187

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

203
        $new_options = [];
×
204
        foreach ( $options_to_migrate as $old => $new ) {
×
205
                $new_options[ $new ] = get_option( $old, $defaults[ $new ] );
×
206

207
                delete_option( $old );
×
208
        }
209

210
        update_option( 'duplicate_post_show_link_in', $new_options );
×
211
}
212

213
/**
214
 * Shows the welcome notice.
215
 *
216
 * @global string $wp_version The WordPress version string.
217
 *
218
 * @return void
219
 */
220
function duplicate_post_show_update_notice() {
221
        if ( ! current_user_can( 'manage_options' ) ) {
×
222
                return;
×
223
        }
224

225
        $current_screen = get_current_screen();
×
226
        if ( empty( $current_screen )
×
227
                || empty( $current_screen->base )
×
228
                || ( $current_screen->base !== 'dashboard' && $current_screen->base !== 'plugins' )
×
229
        ) {
230
                return;
×
231
        }
232

233
        $title = sprintf(
×
234
                /* translators: %s: Yoast Duplicate Post. */
235
                esc_html__( 'You\'ve successfully installed %s!', 'duplicate-post' ),
×
236
                'Yoast Duplicate Post'
×
237
        );
×
238

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

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

249
        echo "<script>
×
250
                        function duplicate_post_dismiss_notice(){
251
                                var data = {
252
                                'action': 'duplicate_post_dismiss_notice',
253
                                };
254

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

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

268
/**
269
 * Dismisses the notice.
270
 *
271
 * @return bool
272
 */
273
function duplicate_post_dismiss_notice() {
274
        return update_site_option( 'duplicate_post_show_notice', 0 );
×
275
}
276

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

293
                $post_taxonomies = get_object_taxonomies( $post->post_type );
×
294
                // Several plugins just add support to post-formats but don't register post_format taxonomy.
295
                if ( post_type_supports( $post->post_type, 'post-formats' ) && ! in_array( 'post_format', $post_taxonomies, true ) ) {
×
296
                        $post_taxonomies[] = 'post_format';
×
297
                }
298

NEW
299
                $taxonomies_blacklist = get_option( 'duplicate_post_taxonomies_blacklist', [] );
×
NEW
300
                if ( empty( $taxonomies_blacklist ) ) {
×
UNCOV
301
                        $taxonomies_blacklist = [];
×
302
                }
NEW
303
                elseif ( ! is_array( $taxonomies_blacklist ) ) {
×
NEW
304
                        $taxonomies_blacklist = [ $taxonomies_blacklist ];
×
305
                }
306
                if ( intval( get_option( 'duplicate_post_copyformat' ) ) === 0 ) {
×
307
                        $taxonomies_blacklist[] = 'post_format';
×
308
                }
309

310
                /**
311
                 * Filters the taxonomy excludelist when copying a post.
312
                 *
313
                 * @param array $taxonomies_blacklist The taxonomy excludelist from the options.
314
                 *
315
                 * @return array
316
                 */
317
                $taxonomies_blacklist = apply_filters( 'duplicate_post_taxonomies_excludelist_filter', $taxonomies_blacklist );
×
318

319
                $taxonomies = array_diff( $post_taxonomies, $taxonomies_blacklist );
×
320
                foreach ( $taxonomies as $taxonomy ) {
×
321
                        $post_terms = wp_get_object_terms( $post->ID, $taxonomy, [ 'orderby' => 'term_order' ] );
×
322
                        $terms      = [];
×
323
                        $num_terms  = count( $post_terms );
×
324
                        for ( $i = 0; $i < $num_terms; $i++ ) {
×
325
                                $terms[] = $post_terms[ $i ]->slug;
×
326
                        }
327
                        wp_set_object_terms( $new_id, $terms, $taxonomy );
×
328
                }
329
        }
330
}
331

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

365
        /**
366
         * Filters the meta fields excludelist when copying a post.
367
         *
368
         * @param array $meta_blacklist The meta fields excludelist from the options.
369
         *
370
         * @return array
371
         */
372
        $meta_blacklist = apply_filters( 'duplicate_post_excludelist_filter', $meta_blacklist );
×
373

374
        $meta_blacklist_string = '(' . implode( ')|(', $meta_blacklist ) . ')';
×
375
        if ( strpos( $meta_blacklist_string, '*' ) !== false ) {
×
376
                $meta_blacklist_string = str_replace( [ '*' ], [ '[a-zA-Z0-9_]*' ], $meta_blacklist_string );
×
377

378
                $meta_keys = [];
×
379
                foreach ( $post_meta_keys as $meta_key ) {
×
380
                        if ( ! preg_match( '#^(' . $meta_blacklist_string . ')$#', $meta_key ) ) {
×
381
                                $meta_keys[] = $meta_key;
×
382
                        }
383
                }
384
        }
385
        else {
386
                $meta_keys = array_diff( $post_meta_keys, $meta_blacklist );
×
387
        }
388

389
        /**
390
         * Filters the list of meta fields names when copying a post.
391
         *
392
         * @param array $meta_keys The list of meta fields name, with the ones in the excludelist already removed.
393
         *
394
         * @return array
395
         */
396
        $meta_keys = apply_filters( 'duplicate_post_meta_keys_filter', $meta_keys );
×
397

398
        foreach ( $meta_keys as $meta_key ) {
×
399
                $meta_values = get_post_custom_values( $meta_key, $post->ID );
×
400
                foreach ( $meta_values as $meta_value ) {
×
401
                        $meta_value = maybe_unserialize( $meta_value );
×
402
                        add_post_meta( $new_id, $meta_key, duplicate_post_wp_slash( $meta_value ) );
×
403
                }
404
        }
405
}
406

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

423
/**
424
 * Adds slashes only to strings.
425
 *
426
 * @param mixed $value Value to slash only if string.
427
 * @return string|mixed
428
 */
429
function duplicate_post_addslashes_to_strings_only( $value ) {
430
        return Utils::addslashes_to_strings_only( $value );
×
431
}
432

433
/**
434
 * Replacement function for faulty core wp_slash().
435
 *
436
 * @param mixed $value What to add slash to.
437
 * @return mixed
438
 */
439
function duplicate_post_wp_slash( $value ) {
440
        return duplicate_post_addslashes_deep( $value );
×
441
}
442

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

475
                $desc = wp_slash( $child->post_content );
×
476

477
                $file_array             = [];
×
478
                $file_array['name']     = basename( $url );
×
479
                $file_array['tmp_name'] = $tmp;
×
480
                // "Upload" to the media collection
481
                $new_attachment_id = media_handle_sideload( $file_array, $new_id, $desc );
×
482

483
                if ( is_wp_error( $new_attachment_id ) ) {
×
484
                        wp_delete_file( $file_array['tmp_name'] );
×
485
                        continue;
×
486
                }
487
                $new_post_author = wp_get_current_user();
×
488
                $cloned_child    = [
×
489
                        'ID'           => $new_attachment_id,
×
490
                        'post_title'   => $child->post_title,
×
491
                        'post_exceprt' => $child->post_title,
×
492
                        'post_author'  => $new_post_author->ID,
×
493
                ];
×
494
                wp_update_post( wp_slash( $cloned_child ) );
×
495

496
                $alt_title = get_post_meta( $child->ID, '_wp_attachment_image_alt', true );
×
497
                if ( $alt_title ) {
×
498
                        update_post_meta( $new_attachment_id, '_wp_attachment_image_alt', wp_slash( $alt_title ) );
×
499
                }
500

501
                // If we have cloned the post thumbnail, set the copy as the thumbnail for the new post.
502
                if ( intval( get_option( 'duplicate_post_copythumbnail' ) ) === 1 && $old_thumbnail_id === $child->ID ) {
×
503
                        set_post_thumbnail( $new_id, $new_attachment_id );
×
504
                }
505
        }
506
}
507

508
/**
509
 * Copies child posts.
510
 *
511
 * @param int     $new_id The new post ID.
512
 * @param WP_Post $post   The original post object.
513
 * @param string  $status Optional. The destination status.
514
 *
515
 * @return void
516
 */
517
function duplicate_post_copy_children( $new_id, $post, $status = '' ) {
518
        // Get children.
519
        $children = get_posts(
×
520
                [
×
521
                        'post_type'   => 'any',
×
522
                        'numberposts' => -1,
×
523
                        'post_status' => 'any',
×
524
                        'post_parent' => $post->ID,
×
525
                ]
×
526
        );
×
527

528
        foreach ( $children as $child ) {
×
529
                if ( $child->post_type === 'attachment' ) {
×
530
                        continue;
×
531
                }
532
                duplicate_post_create_duplicate( $child, $status, $new_id );
×
533
        }
534
}
535

536
/**
537
 * Copies comments.
538
 *
539
 * @param int     $new_id The new post ID.
540
 * @param WP_Post $post   The original post object.
541
 *
542
 * @return void
543
 */
544
function duplicate_post_copy_comments( $new_id, $post ) {
545
        $comments = get_comments(
×
546
                [
×
547
                        'post_id' => $post->ID,
×
548
                        'order'   => 'ASC',
×
549
                        'orderby' => 'comment_date_gmt',
×
550
                ]
×
551
        );
×
552

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

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

607
        /**
608
         * Filter allowing to copy post.
609
         *
610
         * @param bool    $can_duplicate Default to `true`.
611
         * @param WP_Post $post          The original post object.
612
         * @param bool    $status        The intended destination status.
613
         * @param int     $parent_id     The parent post ID if we are calling this recursively.
614
         *
615
         * @return bool
616
         */
617
        $can_duplicate = apply_filters( 'duplicate_post_allow', true, $post, $status, $parent_id );
×
618
        if ( ! $can_duplicate ) {
×
619
                wp_die( esc_html( __( 'You aren\'t allowed to duplicate this post', 'duplicate-post' ) ) );
×
620
        }
621

622
        if ( ! duplicate_post_is_post_type_enabled( $post->post_type ) && $post->post_type !== 'attachment' ) {
×
623
                wp_die(
×
624
                        esc_html(
×
625
                                __( 'Copy features for this post type are not enabled in options page', 'duplicate-post' ) . ': '
×
626
                                . $post->post_type
×
627
                        )
×
628
                );
×
629
        }
630

631
        $new_post_status = ( empty( $status ) ) ? $post->post_status : $status;
×
632
        $title           = ' ';
×
633

634
        if ( $post->post_type !== 'attachment' ) {
×
635
                $prefix = sanitize_text_field( get_option( 'duplicate_post_title_prefix' ) );
×
636
                $suffix = sanitize_text_field( get_option( 'duplicate_post_title_suffix' ) );
×
637
                if ( intval( get_option( 'duplicate_post_copytitle' ) ) === 1 ) {
×
638
                        $title = $post->post_title;
×
639
                        if ( ! empty( $prefix ) ) {
×
640
                                $prefix .= ' ';
×
641
                        }
642
                        if ( ! empty( $suffix ) ) {
×
643
                                $suffix = ' ' . $suffix;
×
644
                        }
645
                }
646
                else {
647
                        $title = ' ';
×
648
                }
649
                $title = trim( $prefix . $title . $suffix );
×
650

651
                /*
652
                 * Not sure we should force a title. Instead, we should respect what WP does.
653
                 * if ( '' === $title ) {
654
                 *  // empty title.
655
                 *  $title = __( 'Untitled', 'default' );
656
                 * }
657
                 */
658

659
                if ( intval( get_option( 'duplicate_post_copystatus' ) ) === 0 ) {
×
660
                        $new_post_status = 'draft';
×
661
                }
662
                elseif ( $new_post_status === 'publish' || $new_post_status === 'future' ) {
×
663
                        // Check if the user has the right capability.
664
                        if ( is_post_type_hierarchical( $post->post_type ) ) {
×
665
                                if ( ! current_user_can( 'publish_pages' ) ) {
×
666
                                        $new_post_status = 'pending';
×
667
                                }
668
                        }
669
                        elseif ( ! current_user_can( 'publish_posts' ) ) {
×
670
                                $new_post_status = 'pending';
×
671
                        }
672
                }
673
        }
674

675
        $new_post_author    = wp_get_current_user();
×
676
        $new_post_author_id = $new_post_author->ID;
×
677
        if ( intval( get_option( 'duplicate_post_copyauthor' ) ) === 1 ) {
×
678
                // Check if the user has the right capability.
679
                if ( is_post_type_hierarchical( $post->post_type ) ) {
×
680
                        if ( current_user_can( 'edit_others_pages' ) ) {
×
681
                                $new_post_author_id = $post->post_author;
×
682
                        }
683
                }
684
                elseif ( current_user_can( 'edit_others_posts' ) ) {
×
685
                        $new_post_author_id = $post->post_author;
×
686
                }
687
        }
688

689
        $menu_order             = ( intval( get_option( 'duplicate_post_copymenuorder' ) ) === 1 ) ? $post->menu_order : 0;
×
690
        $increase_menu_order_by = get_option( 'duplicate_post_increase_menu_order_by' );
×
691
        if ( ! empty( $increase_menu_order_by ) && is_numeric( $increase_menu_order_by ) ) {
×
692
                $menu_order += intval( $increase_menu_order_by );
×
693
        }
694

695
        $post_name = $post->post_name;
×
696
        if ( intval( get_option( 'duplicate_post_copyslug' ) ) !== 1 ) {
×
697
                $post_name = '';
×
698
        }
699
        $new_post_parent = empty( $parent_id ) ? $post->post_parent : $parent_id;
×
700

701
        $new_post = [
×
702
                'menu_order'            => $menu_order,
×
703
                'comment_status'        => $post->comment_status,
×
704
                'ping_status'           => $post->ping_status,
×
705
                'post_author'           => $new_post_author_id,
×
706
                'post_content'          => ( intval( get_option( 'duplicate_post_copycontent' ) ) === 1 ) ? $post->post_content : '',
×
707
                'post_content_filtered' => ( intval( get_option( 'duplicate_post_copycontent' ) ) === 1 ) ? $post->post_content_filtered : '',
×
708
                'post_excerpt'          => ( intval( get_option( 'duplicate_post_copyexcerpt' ) ) === 1 ) ? $post->post_excerpt : '',
×
709
                'post_mime_type'        => $post->post_mime_type,
×
710
                'post_parent'           => $new_post_parent,
×
711
                'post_password'         => ( intval( get_option( 'duplicate_post_copypassword' ) ) === 1 ) ? $post->post_password : '',
×
712
                'post_status'           => $new_post_status,
×
713
                'post_title'            => $title,
×
714
                'post_type'             => $post->post_type,
×
715
                'post_name'             => $post_name,
×
716
        ];
×
717

718
        if ( intval( get_option( 'duplicate_post_copydate' ) ) === 1 ) {
×
719
                $new_post_date             = $post->post_date;
×
720
                $new_post['post_date']     = $new_post_date;
×
721
                $new_post['post_date_gmt'] = get_gmt_from_date( $new_post_date );
×
722
        }
723

724
        /**
725
         * Filter new post values.
726
         *
727
         * @param array   $new_post New post values.
728
         * @param WP_Post $post     Original post object.
729
         *
730
         * @return array
731
         */
732
        $new_post    = apply_filters( 'duplicate_post_new_post', $new_post, $post );
×
733
        $new_post_id = wp_insert_post( wp_slash( $new_post ), true );
×
734

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

739
                if ( $post->post_type === 'page' || is_post_type_hierarchical( $post->post_type ) ) {
×
740
                        do_action( 'dp_duplicate_page', $new_post_id, $post, $status );
×
741
                }
742
                else {
743
                        do_action( 'dp_duplicate_post', $new_post_id, $post, $status );
×
744
                }
745

746
                delete_post_meta( $new_post_id, '_dp_original' );
×
747
                add_post_meta( $new_post_id, '_dp_original', $post->ID );
×
748
        }
749

750
        /**
751
         * Fires after duplicating a post.
752
         *
753
         * @param int|WP_Error $new_post_id The new post id or WP_Error object on error.
754
         * @param WP_Post      $post        The original post object.
755
         * @param bool         $status      The intended destination status.
756
         * @param int          $parent_id   The parent post ID if we are calling this recursively.
757
         */
758
        do_action( 'duplicate_post_post_copy', $new_post_id, $post, $status, $parent_id );
×
759

760
        return $new_post_id;
×
761
}
762

763
/**
764
 * Adds some links on the plugin page.
765
 *
766
 * @param array<string> $links The links array.
767
 * @param string        $file  The file name.
768
 * @return array<string>
769
 */
770
function duplicate_post_add_plugin_links( $links, $file ) {
771
        if ( plugin_basename( __DIR__ . '/duplicate-post.php' ) === $file ) {
×
772
                $links[] = '<a href="https://yoast.com/wordpress/plugins/duplicate-post">' . esc_html__( 'Documentation', 'duplicate-post' ) . '</a>';
×
773
        }
774
        return $links;
×
775
}
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

© 2025 Coveralls, Inc