Completed
Push — add/copy-a-post ( dd202e...719ab8 )
by Kirk
40:11 queued 33:29
created

Jetpack_Copy_Post_By_Param   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 4
A user_can_edit_post() 0 3 2
A update_post_data() 0 21 4
A update_content_and_taxonomies() 0 11 1
A update_featured_image() 0 4 1
A update_post_format() 0 4 1
A filter_title() 0 3 1
A filter_content() 0 3 1
A filter_excerpt() 0 3 1
A add_row_action() 0 25 1
1
<?php
2
/**
3
 * Module Name: Copy Post
4
 * Module Description: Copy an existing post's content into a new post.
5
 * Jumpstart Description: Copy an existing post's content into a new post.
6
 * Sort Order: 15
7
 * First Introduced: 6.9
8
 * Requires Connection: No
9
 * Auto Activate: No
10
 * Module Tags: Writing
11
 * Feature: Writing
12
 * Additional Search Queries: copy, duplicate
13
 */
14
15
class Jetpack_Copy_Post_By_Param {
16
    function __construct() {
17
        // Add row actions to post/page/CPT listing screens.
18
        if ( 'edit.php' === $GLOBALS[ 'pagenow' ] ) {
19
            add_filter( 'post_row_actions', array( $this, 'add_row_action' ), 10, 2 );
20
            add_filter( 'page_row_actions', array( $this, 'add_row_action' ), 10, 2 );
21
            return;
22
        }
23
24
        // Process any `?copy` param if on a create new post/page/CPT screen.
25
        if ( ! empty( $_GET[ 'copy' ] ) && 'post-new.php' === $GLOBALS[ 'pagenow' ] ) {
26
            add_action( 'wp_insert_post', array( $this, 'update_post_data' ), 10, 3 );
27
        }
28
    }
29
30
    protected function user_can_edit_post( $post ) {
31
        return get_current_user_id() === (int) $post->post_author || current_user_can( 'edit_others_posts' );
32
    }
33
34
    function update_post_data( $target_post_id, $post, $update ) {
35
        if ( $update ) {
36
            return;
37
        }
38
39
        $source_post = get_post( $_GET['copy'] );
40
        if ( ! $source_post || ! $this->user_can_edit_post( $source_post ) ) {
41
            return;
42
        }
43
44
        $update_content = $this->update_content_and_taxonomies( $source_post, $target_post_id );
45
        $update_featured_image = $this->update_featured_image( $source_post, $target_post_id );
46
        $update_post_format = $this->update_post_format( $source_post, $target_post_id );
47
48
        // Required to satify get_default_post_to_edit(), which has these filters after post creation.
49
        add_filter( 'default_title', array( $this, 'filter_title' ), 10, 2 );
50
        add_filter( 'default_content', array( $this, 'filter_content' ), 10, 2 );
51
        add_filter( 'default_excerpt', array( $this, 'filter_excerpt' ), 10, 2 );
52
53
        do_action( 'jetpack_copy_post', $source_post, $target_post_id, $update_content, $update_featured_image, $update_post_format );
54
    }
55
56
    protected function update_content_and_taxonomies( $source_post, $target_post_id ) {
57
        $data = apply_filters( 'jetpack_copy_post_data', array(
58
            'ID' => $target_post_id,
59
            'post_title' => $source_post->post_title,
60
            'post_content' => $source_post->post_content,
61
            'post_excerpt' => $source_post->post_excerpt,
62
            'post_category' => $source_post->post_category,
63
            'tags_input' => $source_post->tags_input,
64
        ) );
65
        return wp_update_post( $data );
66
    }
67
68
    protected function update_featured_image( $source_post, $target_post_id ) {
69
        $featured_image_id = get_post_thumbnail_id( $source_post );
70
        return update_post_meta( $target_post_id, '_thumbnail_id', $featured_image_id );
71
    }
72
73
    protected function update_post_format( $source_post, $target_post_id ) {
74
        $post_format = get_post_format( $source_post );
75
        return set_post_format( $target_post_id, $post_format );
76
    }
77
78
    function filter_title( $post_title, $post ) {
79
        return $post->post_title;
80
    }
81
82
    function filter_content( $post_content, $post ) {
83
        return $post->post_content;
84
    }
85
86
    function filter_excerpt( $post_excerpt, $post ) {
87
        return $post->post_excerpt;
88
    }
89
90
    function add_row_action( $actions, $post ) {
91
        $edit_url = add_query_arg( array(
92
            'post_type' => $post->post_type,
93
            'copy' => $post->ID,
94
            '_wpnonce' => wp_create_nonce( 'jetpack-copy-post' ),
95
        ), admin_url( 'post-new.php' ) );
96
        $edit_action = array(
97
            'copy' => sprintf(
98
                '<a href="%s" aria-label="%s">%s</a>',
99
                esc_url( $edit_url ),
100
                esc_attr( __( 'Copy this post.' ) ),
101
                __( 'Copy' )
102
            ),
103
        );
104
105
        // Insert the Copy action before the Trash action.
106
        $edit_offset = array_search( 'trash', array_keys( $actions ), true );
107
        $actions = array_merge(
108
            array_slice( $actions, 0, $edit_offset ),
109
            $edit_action,
110
            array_slice( $actions, $edit_offset )
111
        );
112
113
        return $actions;
114
    }
115
}
116
117
function jetpack_copy_post_by_param_init() {
118
    new Jetpack_Copy_Post_By_Param();
119
}
120
add_action( 'admin_init', 'jetpack_copy_post_by_param_init' );
121