Completed
Push — add/copy-a-post ( b6c4e0...dd202e )
by Kirk
06:48
created

Jetpack_Copy_Post_By_Param::update_post_data()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 3
dl 0
loc 39
rs 8.6737
c 0
b 0
f 0
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( $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
        $data = apply_filters( 'jetpack_copy_post_data', array(
45
            'ID' => $post_ID,
46
            'post_title' => $source_post->post_title,
47
            'post_content' => $source_post->post_content,
48
            'post_excerpt' => $source_post->post_excerpt,
49
            'post_category' => $source_post->post_category,
50
            'tags_input' => $source_post->tags_input,
51
        ) );
52
        wp_update_post( $data );
53
54
        // Featured Image
55
        $featured_image_id = get_post_thumbnail_id( $source_post );
56
        if ( $featured_image_id ) {
57
            update_post_meta( $post_ID, '_thumbnail_id', $featured_image_id );
58
        }
59
60
        // Post Formats
61
        $post_format = get_post_format( $source_post );
62
        if ( $post_format ) {
63
            set_post_format( $post_ID, $post_format );
64
        }
65
66
        do_action( 'jetpack_copy_post' );
67
68
        // Required to satify get_default_post_to_edit(), which has these filters after post creation.
69
        add_filter( 'default_title', array( $this, 'filter_title' ), 10, 2 );
70
        add_filter( 'default_content', array( $this, 'filter_content' ), 10, 2 );
71
        add_filter( 'default_excerpt', array( $this, 'filter_excerpt' ), 10, 2 );
72
    }
73
74
    function filter_title( $post_title, $post ) {
75
        return $post->post_title;
76
    }
77
78
    function filter_content( $post_content, $post ) {
79
        return $post->post_content;
80
    }
81
82
    function filter_excerpt( $post_excerpt, $post ) {
83
        return $post->post_excerpt;
84
    }
85
86
    function add_row_action( $actions, $post ) {
87
        $edit_url = add_query_arg( array(
88
            'post_type' => $post->post_type,
89
            'copy' => $post->ID,
90
            '_wpnonce' => wp_create_nonce( 'jetpack-copy-post' ),
91
        ), admin_url( 'post-new.php' ) );
92
        $edit_action = array(
93
            'copy' => sprintf(
94
                '<a href="%s" aria-label="%s">%s</a>',
95
                esc_url( $edit_url ),
96
                esc_attr( __( 'Copy this post.' ) ),
97
                __( 'Copy' )
98
            ),
99
        );
100
101
        // Insert the Copy action before the Trash action.
102
        $edit_offset = array_search( 'trash', array_keys( $actions ), true );
103
        $actions = array_merge(
104
            array_slice( $actions, 0, $edit_offset ),
105
            $edit_action,
106
            array_slice( $actions, $edit_offset )
107
        );
108
109
        return $actions;
110
    }
111
}
112
113
function jetpack_copy_post_by_param_init() {
114
    new Jetpack_Copy_Post_By_Param();
115
}
116
add_action( 'admin_init', 'jetpack_copy_post_by_param_init' );
117