|
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'], ARRAY_A ); |
|
40
|
|
|
if ( ! $source_post || ! $this->user_can_edit_post( $source_post ) ) { |
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// Required to satify get_default_post_to_edit(), which has these filters after post creation. |
|
45
|
|
|
add_filter( 'default_title', array( $this, 'filter_title' ), 10, 2 ); |
|
46
|
|
|
add_filter( 'default_content', array( $this, 'filter_content' ), 10, 2 ); |
|
47
|
|
|
add_filter( 'default_excerpt', array( $this, 'filter_excerpt' ), 10, 2 ); |
|
48
|
|
|
|
|
49
|
|
|
$data = apply_filters( 'jetpack_copy_post_data', array( |
|
50
|
|
|
'ID' => $post_ID, |
|
51
|
|
|
'post_title' => $source_post[ 'post_title' ], |
|
52
|
|
|
'post_content' => $source_post[ 'post_content' ], |
|
53
|
|
|
'post_excerpt' => $source_post[ 'post_excerpt' ], |
|
54
|
|
|
'post_category' => $source_post[ 'post_category' ], |
|
55
|
|
|
'tags_input' => $source_post[ 'tags_input' ], |
|
56
|
|
|
) ); |
|
57
|
|
|
|
|
58
|
|
|
do_action( 'jetpack_copy_post' ); |
|
59
|
|
|
wp_update_post( $data ); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
function filter_title( $post_title, $post ) { |
|
63
|
|
|
return $post->post_title; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
function filter_content( $post_content, $post ) { |
|
67
|
|
|
return $post->post_content; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
function filter_excerpt( $post_excerpt, $post ) { |
|
71
|
|
|
return $post->post_excerpt; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
function add_row_action( $actions, $post ) { |
|
75
|
|
|
$edit_url = add_query_arg( array( |
|
76
|
|
|
'copy' => $post->ID, |
|
77
|
|
|
'_wpnonce' => wp_create_nonce( 'jetpack-copy-post' ), |
|
78
|
|
|
), admin_url( 'post-new.php' ) ); |
|
79
|
|
|
$edit_action = array( |
|
80
|
|
|
'copy' => sprintf( |
|
81
|
|
|
'<a href="%s" aria-label="%s">%s</a>', |
|
82
|
|
|
esc_url( $edit_url ), |
|
83
|
|
|
esc_attr( __( 'Copy this post.' ) ), |
|
84
|
|
|
__( 'Copy' ) |
|
85
|
|
|
), |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
// Insert the Copy action before the Trash action. |
|
89
|
|
|
$edit_offset = array_search( 'trash', array_keys( $actions ), true ); |
|
90
|
|
|
$actions = array_merge( |
|
91
|
|
|
array_slice( $actions, 0, $edit_offset ), |
|
92
|
|
|
$edit_action, |
|
93
|
|
|
array_slice( $actions, $edit_offset ) |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
return $actions; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
function jetpack_copy_post_by_param_init() { |
|
101
|
|
|
new Jetpack_Copy_Post_By_Param(); |
|
102
|
|
|
} |
|
103
|
|
|
add_action( 'admin_init', 'jetpack_copy_post_by_param_init' ); |
|
104
|
|
|
|