Completed
Push — add/copy-a-post ( 1263e1 )
by Kirk
06:40
created

copy-post.php ➔ jetpack_copy_post_by_param_init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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: Yes
10
 * Module Tags: Writing
11
 * Feature: Writing
12
 * Additional Search Queries: copy, duplicate
13
 */
14
15
if ( empty( $_GET['copy'] ) ) {
16
    return;
17
}
18
19
class Jetpack_Copy_Post_By_Param {
20
    private $post;
21
22
    function __construct() {
23
        $post = get_post( $_GET['copy'] );
24
        error_log($post);
25
        if ( ! $post || ! $this->user_can_edit_post( $post ) ) {
26
            return;
27
        }
28
29
        $this->post = $post;
30
31
        add_filter( 'default_title', array( $this, 'default_title' ) );
32
        add_filter( 'default_content', array( $this, 'default_content' ) );
33
        add_filter( 'default_excerpt', array( $this, 'default_excerpt' ) );
34
35
        do_action( 'jetpack_post_copy_post' );
36
    }
37
38
    function default_title() {
39
        return $this->post->post_title;
40
    }
41
42
    function default_content() {
43
        return $this->post->post_content;
44
    }
45
46
    function default_excerpt() {
47
        return $this->post->post_excerpt;
48
    }
49
50
    protected function user_can_edit_post( $post ) {
51
        return get_current_user_id() === (int) $post->post_author || current_user_can( 'edit_others_posts' );
52
    }
53
}
54
55
function jetpack_copy_post_by_param_init() {
56
    new Jetpack_Copy_Post_By_Param();
57
}
58
add_action( 'init', 'jetpack_copy_post_by_param_init' );
59