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
|
|
|
if ( ! empty( $_GET[ 'copy' ] ) ) { |
18
|
|
|
add_filter( 'wp_insert_post_data', array( $this, 'filter_post_data' ) ); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
add_filter( 'post_row_actions', array( $this, 'add_row_action' ), 10, 2 ); |
22
|
|
|
add_filter( 'page_row_actions', array( $this, 'add_row_action' ), 10, 2 ); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function user_can_edit_post( $post ) { |
26
|
|
|
return get_current_user_id() === (int) $post[ 'post_author' ] || current_user_can( 'edit_others_posts' ); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
function filter_post_data( $data ) { |
30
|
|
|
$source_post = get_post( $_GET['copy'], ARRAY_A ); |
31
|
|
|
if ( ! $source_post || ! $this->user_can_edit_post( $source_post ) ) { |
32
|
|
|
return $data; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// Required to satify get_default_post_to_edit(), which has these filters after post creation. |
36
|
|
|
add_filter( 'default_title', array( $this, 'filter_title' ), 10, 2 ); |
37
|
|
|
add_filter( 'default_content', array( $this, 'filter_content' ), 10, 2 ); |
38
|
|
|
add_filter( 'default_excerpt', array( $this, 'filter_excerpt' ), 10, 2 ); |
39
|
|
|
|
40
|
|
|
$data = array_merge( |
41
|
|
|
$data, |
42
|
|
|
array( |
43
|
|
|
'post_title' => $source_post[ 'post_title' ], |
44
|
|
|
'post_content' => $source_post[ 'post_content' ], |
45
|
|
|
'post_excerpt' => $source_post[ 'post_excerpt' ], |
46
|
|
|
) |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$data = apply_filters( 'jetpack_copy_post_data', $data ); |
50
|
|
|
|
51
|
|
|
do_action( 'jetpack_copy_post' ); |
52
|
|
|
return $data; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
function filter_title( $post_title, $post ) { |
56
|
|
|
return $post->post_title; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
function filter_content( $post_content, $post ) { |
60
|
|
|
return $post->post_content; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
function filter_excerpt( $post_excerpt, $post ) { |
64
|
|
|
return $post->post_excerpt; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
function add_row_action( $actions, $post ) { |
68
|
|
|
$edit_url = add_query_arg( array( |
69
|
|
|
'copy' => $post->ID, |
70
|
|
|
'_wpnonce' => wp_create_nonce( 'jetpack-copy-post' ), |
71
|
|
|
), admin_url( 'post-new.php' ) ); |
72
|
|
|
$edit_action = array( |
73
|
|
|
'copy' => sprintf( |
74
|
|
|
'<a href="%s" aria-label="%s">%s</a>', |
75
|
|
|
esc_url( $edit_url ), |
76
|
|
|
esc_attr( __( 'Copy this post.' ) ), |
77
|
|
|
__( 'Copy' ) |
78
|
|
|
), |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
// Insert the Copy action before the Trash action. |
82
|
|
|
$edit_offset = array_search( 'trash', array_keys( $actions ), true ); |
83
|
|
|
$actions = array_merge( |
84
|
|
|
array_slice( $actions, 0, $edit_offset ), |
85
|
|
|
$edit_action, |
86
|
|
|
array_slice( $actions, $edit_offset ) |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
return $actions; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
function jetpack_copy_post_by_param_init() { |
94
|
|
|
new Jetpack_Copy_Post_By_Param(); |
95
|
|
|
} |
96
|
|
|
add_action( 'admin_init', 'jetpack_copy_post_by_param_init' ); |
97
|
|
|
|