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

Yoast / duplicate-post / 21285632508

23 Jan 2026 12:08PM UTC coverage: 44.915%. First build
21285632508

Pull #446

github

web-flow
Merge 487cc1afb into 207cebb39
Pull Request #446: Use a sidebar panel instead of the metabox in the block editor

2 of 40 new or added lines in 3 files covered. (5.0%)

1166 of 2596 relevant lines covered (44.92%)

1.59 hits per line

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

0.0
/src/handlers/save-post-handler.php
1
<?php
2

3
namespace Yoast\WP\Duplicate_Post\Handlers;
4

5
use Yoast\WP\Duplicate_Post\Permissions_Helper;
6

7
/**
8
 * Duplicate Post handler class for save_post action.
9
 *
10
 * @since 4.0
11
 */
12
class Save_Post_Handler {
13

14
        /**
15
         * Holds the permissions helper.
16
         *
17
         * @var Permissions_Helper
18
         */
19
        protected $permissions_helper;
20

21
        /**
22
         * Initializes the class.
23
         *
24
         * @param Permissions_Helper $permissions_helper The Permissions Helper object.
25
         */
26
        public function __construct( Permissions_Helper $permissions_helper ) {
×
27
                $this->permissions_helper = $permissions_helper;
×
28
        }
29

30
        /**
31
         * Adds hooks to integrate with WordPress.
32
         *
33
         * @return void
34
         */
35
        public function register_hooks() {
×
36
                if ( \intval( \get_option( 'duplicate_post_show_original_meta_box' ) ) === 1
×
37
                        || \intval( \get_option( 'duplicate_post_show_original_column' ) ) === 1 ) {
×
38
                        \add_action( 'save_post', [ $this, 'delete_on_save_post' ] );
×
NEW
39
                        \add_action( 'init', [ $this, 'register_meta' ] );
×
40
                }
41
        }
42

43
        /**
44
         * Registers the meta field for the REST API.
45
         *
46
         * @return void
47
         */
NEW
48
        public function register_meta() {
×
NEW
49
                $post_types = $this->permissions_helper->get_enabled_post_types();
×
50

NEW
51
                foreach ( $post_types as $post_type ) {
×
NEW
52
                        \register_post_meta(
×
NEW
53
                                $post_type,
×
NEW
54
                                '_dp_remove_original',
×
NEW
55
                                [
×
NEW
56
                                        'show_in_rest'      => true,
×
NEW
57
                                        'single'            => true,
×
NEW
58
                                        'type'              => 'boolean',
×
NEW
59
                                        'default'           => false,
×
NEW
60
                                        'auth_callback'     => static function ( $allowed, $meta_key, $post_id ) {
×
NEW
61
                                                return \current_user_can( 'edit_post', $post_id );
×
NEW
62
                                        },
×
NEW
63
                                        'sanitize_callback' => 'rest_sanitize_boolean',
×
NEW
64
                                ]
×
NEW
65
                        );
×
66
                }
67
        }
68

69
        /**
70
         * Deletes the custom field with the ID of the original post.
71
         *
72
         * @param int $post_id The current post ID.
73
         *
74
         * @return void
75
         */
76
        public function delete_on_save_post( $post_id ) {
×
NEW
77
                if ( \defined( 'DOING_AUTOSAVE' ) && \DOING_AUTOSAVE ) {
×
NEW
78
                        return;
×
79
                }
80

NEW
81
                if ( ! \current_user_can( 'edit_post', $post_id ) ) {
×
82
                        return;
×
83
                }
84

85
                $post = \get_post( $post_id );
×
86
                if ( ! $post ) {
×
87
                        return;
×
88
                }
89

NEW
90
                if ( $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) {
×
NEW
91
                        return;
×
92
                }
93

94
                // Check for classic editor (POST request).
NEW
95
                $should_remove_from_post = ! empty( $_POST['duplicate_post_remove_original'] );
×
96

97
                // Check for block editor (meta field).
NEW
98
                $should_remove_from_meta = (bool) \get_post_meta( $post_id, '_dp_remove_original', true );
×
99

NEW
100
                if ( $should_remove_from_post || $should_remove_from_meta ) {
×
101
                        \delete_post_meta( $post_id, '_dp_original' );
×
102
                        // Clean up the flag meta.
NEW
103
                        \delete_post_meta( $post_id, '_dp_remove_original' );
×
104
                }
105
        }
106
}
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