post_status || ! Draftling_Duplicator::is_supported_post_type( $post->post_type ) || ! current_user_can( 'edit_post', $post->ID ) ) { return $actions; } $post_type_object = get_post_type_object( $post->post_type ); if ( ! $post_type_object || ! isset( $post_type_object->cap->create_posts ) || ! current_user_can( $post_type_object->cap->create_posts ) ) { return $actions; } $url = wp_nonce_url( add_query_arg( array( 'action' => 'draftling_duplicate_content', 'post' => $post->ID, ), admin_url( 'admin.php' ) ), 'draftling_duplicate_' . $post->ID ); $actions['draftling_duplicate'] = sprintf( '%3$s', esc_url( $url ), esc_attr( sprintf( /* translators: %s: Content title. */ __( 'Duplicate ā€œ%sā€ (opens in a new tab)', 'draftling' ), $post->post_title ) ), esc_html__( 'Duplicate', 'draftling' ) ); return $actions; } /** * Validate and process a duplicate request. */ public function handle_duplicate_request() { $post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- The nonce is verified below; it is tied to this ID. if ( ! $post_id ) { $this->stop( __( 'No content item was provided.', 'draftling' ), 400 ); } check_admin_referer( 'draftling_duplicate_' . $post_id ); $post = get_post( $post_id ); if ( ! $post ) { $this->stop( __( 'The original content item could not be found.', 'draftling' ), 404 ); } if ( ! Draftling_Duplicator::is_supported_post_type( $post->post_type ) ) { $this->stop( __( 'This content type cannot be duplicated.', 'draftling' ), 400 ); } if ( ! current_user_can( 'edit_post', $post_id ) ) { $this->stop( __( 'You are not allowed to duplicate this item.', 'draftling' ), 403 ); } $post_type_object = get_post_type_object( $post->post_type ); if ( ! $post_type_object || ! isset( $post_type_object->cap->create_posts ) || ! current_user_can( $post_type_object->cap->create_posts ) ) { $this->stop( __( 'You are not allowed to create this type of content.', 'draftling' ), 403 ); } $result = Draftling_Duplicator::duplicate( $post ); if ( is_wp_error( $result ) ) { $this->stop( $result->get_error_message(), 500 ); } $redirect_url = add_query_arg( array( 'action' => 'edit', 'post' => $result, 'draftling_duplicated' => 1, ), admin_url( 'post.php' ) ); wp_safe_redirect( $redirect_url ); exit; } /** * Display a confirmation after redirecting to the new draft. * * These query arguments only decide whether to show a static success * message; nothing is changed, so no nonce is needed. */ public function show_admin_notice() { // phpcs:disable WordPress.Security.NonceVerification.Recommended if ( empty( $_GET['draftling_duplicated'] ) || ! isset( $_GET['post'] ) ) { return; } $new_post_id = absint( $_GET['post'] ); // phpcs:enable WordPress.Security.NonceVerification.Recommended if ( ! $new_post_id || ! current_user_can( 'edit_post', $new_post_id ) ) { return; } echo '

'; echo esc_html__( 'The item was duplicated successfully. You are editing the new draft.', 'draftling' ); echo '

'; } /** * Stop the request with a standard WordPress error screen. * * @param string $message Error message. * @param int $status HTTP response status. */ private function stop( $message, $status ) { wp_die( esc_html( $message ), esc_html__( 'Duplication failed', 'draftling' ), array( 'response' => absint( $status ) ) ); } }