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

Yoast / duplicate-post / 14725616456

29 Apr 2025 07:21AM UTC coverage: 45.469% (-4.7%) from 50.122%
14725616456

push

github

web-flow
Merge pull request #402 from Yoast/feature/drop-php-7.2-7.3

Drop support for Php 7.2 and 7.3

1164 of 2560 relevant lines covered (45.47%)

1.61 hits per line

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

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

3
namespace Yoast\WP\Duplicate_Post\Handlers;
4

5
use Yoast\WP\Duplicate_Post\Permissions_Helper;
6
use Yoast\WP\Duplicate_Post\Post_Duplicator;
7

8
/**
9
 * Duplicate Post handler class for duplication actions from links.
10
 *
11
 * @since 4.0
12
 */
13
class Link_Handler {
14

15
        /**
16
         * Post_Duplicator object.
17
         *
18
         * @var Post_Duplicator
19
         */
20
        protected $post_duplicator;
21

22
        /**
23
         * Holds the permissions helper.
24
         *
25
         * @var Permissions_Helper
26
         */
27
        protected $permissions_helper;
28

29
        /**
30
         * Initializes the class.
31
         *
32
         * @param Post_Duplicator    $post_duplicator    The Post_Duplicator object.
33
         * @param Permissions_Helper $permissions_helper The Permissions Helper object.
34
         */
35
        public function __construct( Post_Duplicator $post_duplicator, Permissions_Helper $permissions_helper ) {
×
36
                $this->post_duplicator    = $post_duplicator;
×
37
                $this->permissions_helper = $permissions_helper;
×
38
        }
39

40
        /**
41
         * Adds hooks to integrate with WordPress.
42
         *
43
         * @return void
44
         */
45
        public function register_hooks() {
×
46
                \add_action( 'admin_action_duplicate_post_rewrite', [ $this, 'rewrite_link_action_handler' ] );
×
47
                \add_action( 'admin_action_duplicate_post_clone', [ $this, 'clone_link_action_handler' ] );
×
48
                \add_action( 'admin_action_duplicate_post_new_draft', [ $this, 'new_draft_link_action_handler' ] );
×
49
        }
50

51
        /**
52
         * Handles the action for copying a post to a new draft.
53
         *
54
         * @return void
55
         */
56
        public function new_draft_link_action_handler() {
×
57
                if ( ! $this->permissions_helper->is_current_user_allowed_to_copy() ) {
×
58
                        \wp_die( \esc_html__( 'Current user is not allowed to copy posts.', 'duplicate-post' ) );
×
59
                }
60

61
                if ( ! ( isset( $_GET['post'] ) || isset( $_POST['post'] )
×
62
                        || ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] === 'duplicate_post_new_draft' ) ) ) {
×
63
                        \wp_die( \esc_html__( 'No post to duplicate has been supplied!', 'duplicate-post' ) );
×
64
                }
65

66
                $id = ( isset( $_GET['post'] ) ? \intval( \wp_unslash( $_GET['post'] ) ) : \intval( \wp_unslash( $_POST['post'] ) ) );
×
67

68
                \check_admin_referer( 'duplicate_post_new_draft_' . $id );
×
69

70
                $post = \get_post( $id );
×
71

72
                if ( ! $post ) {
×
73
                        \wp_die(
×
74
                                \esc_html(
×
75
                                        \__( 'Copy creation failed, could not find original:', 'duplicate-post' ) . ' '
×
76
                                        . $id
×
77
                                )
×
78
                        );
×
79
                }
80

81
                if ( $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) {
×
82
                        \wp_die(
×
83
                                \esc_html__( 'You cannot create a copy of a post which is intended for Rewrite & Republish.', 'duplicate-post' )
×
84
                        );
×
85
                }
86

87
                $new_id = \duplicate_post_create_duplicate( $post, 'draft' );
×
88

89
                if ( \is_wp_error( $new_id ) ) {
×
90
                        \wp_die(
×
91
                                \esc_html__( 'Copy creation failed, could not create a copy.', 'duplicate-post' )
×
92
                        );
×
93
                }
94

95
                \wp_safe_redirect(
×
96
                        \add_query_arg(
×
97
                                [
×
98
                                        'cloned' => 1,
×
99
                                        'ids'    => $post->ID,
×
100
                                ],
×
101
                                \admin_url( 'post.php?action=edit&post=' . $new_id . ( isset( $_GET['classic-editor'] ) ? '&classic-editor' : '' ) )
×
102
                        )
×
103
                );
×
104
                exit();
×
105
        }
106

107
        /**
108
         * Handles the action for copying a post and redirecting to the post list.
109
         *
110
         * @return void
111
         */
112
        public function clone_link_action_handler() {
×
113
                if ( ! $this->permissions_helper->is_current_user_allowed_to_copy() ) {
×
114
                        \wp_die( \esc_html__( 'Current user is not allowed to copy posts.', 'duplicate-post' ) );
×
115
                }
116

117
                if ( ! ( isset( $_GET['post'] ) || isset( $_POST['post'] )
×
118
                        || ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] === 'duplicate_post_clone' ) ) ) {
×
119
                        \wp_die( \esc_html__( 'No post to duplicate has been supplied!', 'duplicate-post' ) );
×
120
                }
121

122
                $id = ( isset( $_GET['post'] ) ? \intval( \wp_unslash( $_GET['post'] ) ) : \intval( \wp_unslash( $_POST['post'] ) ) );
×
123

124
                \check_admin_referer( 'duplicate_post_clone_' . $id );
×
125

126
                $post = \get_post( $id );
×
127

128
                if ( ! $post ) {
×
129
                        \wp_die(
×
130
                                \esc_html(
×
131
                                        \__( 'Copy creation failed, could not find original:', 'duplicate-post' ) . ' '
×
132
                                        . $id
×
133
                                )
×
134
                        );
×
135
                }
136

137
                if ( $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) {
×
138
                        \wp_die(
×
139
                                \esc_html__( 'You cannot create a copy of a post which is intended for Rewrite & Republish.', 'duplicate-post' )
×
140
                        );
×
141
                }
142

143
                $new_id = \duplicate_post_create_duplicate( $post );
×
144

145
                if ( \is_wp_error( $new_id ) ) {
×
146
                        \wp_die(
×
147
                                \esc_html__( 'Copy creation failed, could not create a copy.', 'duplicate-post' )
×
148
                        );
×
149
                }
150

151
                $post_type = $post->post_type;
×
152
                $sendback  = \wp_get_referer();
×
153
                if ( ! $sendback || \strpos( $sendback, 'post.php' ) !== false || \strpos( $sendback, 'post-new.php' ) !== false ) {
×
154
                        if ( $post_type === 'attachment' ) {
×
155
                                $sendback = \admin_url( 'upload.php' );
×
156
                        }
157
                        else {
158
                                $sendback = \admin_url( 'edit.php' );
×
159
                                if ( ! empty( $post_type ) ) {
×
160
                                        $sendback = \add_query_arg( 'post_type', $post_type, $sendback );
×
161
                                }
162
                        }
163
                }
164
                else {
165
                        $sendback = \remove_query_arg( [ 'trashed', 'untrashed', 'deleted', 'cloned', 'ids' ], $sendback );
×
166
                }
167

168
                // Redirect to the post list screen.
169
                \wp_safe_redirect(
×
170
                        \add_query_arg(
×
171
                                [
×
172
                                        'cloned' => 1,
×
173
                                        'ids'    => $post->ID,
×
174
                                ],
×
175
                                $sendback
×
176
                        )
×
177
                );
×
178
                exit();
×
179
        }
180

181
        /**
182
         * Handles the action for copying a post for the Rewrite & Republish feature.
183
         *
184
         * @return void
185
         */
186
        public function rewrite_link_action_handler() {
×
187
                if ( ! $this->permissions_helper->is_current_user_allowed_to_copy() ) {
×
188
                        \wp_die( \esc_html__( 'Current user is not allowed to copy posts.', 'duplicate-post' ) );
×
189
                }
190

191
                if ( ! ( isset( $_GET['post'] ) || isset( $_POST['post'] )
×
192
                        || ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] === 'duplicate_post_rewrite' ) ) ) {
×
193
                        \wp_die( \esc_html__( 'No post to duplicate has been supplied!', 'duplicate-post' ) );
×
194
                }
195

196
                $id = ( isset( $_GET['post'] ) ? \intval( \wp_unslash( $_GET['post'] ) ) : \intval( \wp_unslash( $_POST['post'] ) ) );
×
197

198
                \check_admin_referer( 'duplicate_post_rewrite_' . $id );
×
199

200
                $post = \get_post( $id );
×
201

202
                if ( ! $post ) {
×
203
                        \wp_die(
×
204
                                \esc_html(
×
205
                                        \__( 'Copy creation failed, could not find original:', 'duplicate-post' ) . ' '
×
206
                                        . $id
×
207
                                )
×
208
                        );
×
209
                }
210

211
                if ( ! $this->permissions_helper->should_rewrite_and_republish_be_allowed( $post ) ) {
×
212
                        \wp_die(
×
213
                                \esc_html__( 'You cannot create a copy for Rewrite & Republish if the original is not published or if it already has a copy.', 'duplicate-post' )
×
214
                        );
×
215
                }
216

217
                $new_id = $this->post_duplicator->create_duplicate_for_rewrite_and_republish( $post );
×
218

219
                if ( \is_wp_error( $new_id ) ) {
×
220
                        \wp_die(
×
221
                                \esc_html__( 'Copy creation failed, could not create a copy.', 'duplicate-post' )
×
222
                        );
×
223
                }
224

225
                \wp_safe_redirect(
×
226
                        \add_query_arg(
×
227
                                [
×
228
                                        'rewriting' => 1,
×
229
                                        'ids'       => $post->ID,
×
230
                                ],
×
231
                                \admin_url( 'post.php?action=edit&post=' . $new_id . ( isset( $_GET['classic-editor'] ) ? '&classic-editor' : '' ) )
×
232
                        )
×
233
                );
×
234
                exit();
×
235
        }
236
}
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