duplicate-wp-page-post.php
11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/*
Plugin Name: Duplicate Page and Post
Plugin URI: https://wordpress.org/plugins/duplicate-wp-page-post/
Description: Quickly clone a page, post or custom post and supports Gutenberg.
Author: Arjun Thakur
Author URI: https://profiles.wordpress.org/arjunthakur#content-plugins
Version: 2.9.3
License: GPLv2 or later
Text Domain: dpp_wpp_page
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! defined( 'DPP_BASE_NAME' ) ) {
define( 'DPP_BASE_NAME', plugin_basename( __FILE__ ) );
}
if ( ! class_exists( 'dcc_dpp_wpp_page' ) ):
class dpp_wpp_page {
/*AutoLoad Hooks*/
public function __construct() {
$opt = get_option( 'dpp_wpp_page_options' );
register_activation_hook( __FILE__, array( &$this, 'dpp_wpp_page_install' ) );
add_action( 'admin_menu', array( &$this, 'dpp_page_options_page' ) );
add_filter( 'plugin_action_links', array( &$this, 'dpp_settings_link' ), 10, 2 );
add_action( 'admin_action_dt_dpp_post_as_draft', array( &$this, 'dt_dpp_post_as_draft' ) );
add_filter( 'post_row_actions', array( &$this, 'dt_dpp_post_link' ), 10, 2 );
add_filter( 'page_row_actions', array( &$this, 'dt_dpp_post_link' ), 10, 2 );
if ( isset( $opt['dpp_posteditor'] ) && $opt['dpp_posteditor'] == 'gutenberg' ) {
add_action( 'admin_head', array( &$this, 'dpp_wpp_button_guten' ) );
} else {
add_action( 'post_submitbox_misc_actions', array( &$this, 'dpp_wpp_page_custom_button' ) );
}
add_action( 'wp_before_admin_bar_render', array( &$this, 'dpp_wpp_page_admin_bar_link' ) );
}
/*Activation plugin Hook*/
public function dpp_wpp_page_install() {
$defaultsettings = array(
'dpp_post_status' => 'draft',
'dpp_post_redirect' => 'to_list',
'dpp_post_suffix' => '',
'dpp_posteditor' => 'classic',
'dpp_post_link_title' => '',
);
$opt = get_option( 'dpp_wpp_page_options' );
if ( ! $opt['dpp_post_status'] ) {
update_option( 'dpp_wpp_page_options', $defaultsettings );
}
}
/* Page Title and Dashboard Menu (Setting options) */
public function dpp_page_options_page() {
add_options_page( __( 'Duplicate Page and Post', 'dpp_wpp_page' ), __( 'Duplicate post', 'dpp_wpp_page' ), 'manage_options', 'dpp_page_settings', array(
&$this,
'dpp_page_settings'
) );
}
/*Include plugin setting file*/
public function dpp_page_settings() {
if ( current_user_can( 'manage_options' ) ) {
include( 'duplicate-wp-page-post-setting.php' );
}
}
/*Important function*/
public function dt_dpp_post_as_draft() {
$nonce = sanitize_text_field( $_REQUEST['nonce'] );
$getpost = sanitize_text_field( $_GET['post'] );
$post_id = ( isset( $getpost ) ? intval( $getpost ) : intval( $getpost ) );
if ( wp_verify_nonce( $nonce, 'dt-duplicate-page-' . $post_id ) && current_user_can( 'edit_posts' ) ) {
global $wpdb;
/*sanitize_GET POST REQUEST*/
//$post_copy = sanitize_text_field( $_POST["post"] );
//$get_copy = sanitize_text_field( $_GET['post'] );
$request_copy = sanitize_text_field( $_REQUEST['action'] );
$opt = get_option( 'dpp_wpp_page_options' );
$suffix = ! empty( $opt['dpp_post_suffix'] ) ? ' -- ' . esc_attr( $opt['dpp_post_suffix'] ) : '';
$post_status = ! empty( $opt['dpp_post_status'] ) ? esc_attr( $opt['dpp_post_status'] ) : 'draft';
$redirectit = ! empty( $opt['dpp_post_redirect'] ) ? esc_attr( $opt['dpp_post_redirect'] ) : 'to_list';
if ( ! ( isset( $getpost ) || isset( $getpost ) || ( isset( $request_copy ) && 'dt_dpp_post_as_draft' == $request_copy ) ) ) {
wp_die( 'No post!' );
}
$returnpage = '';
$post = get_post( $post_id );
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;
/*Create the post Copy */
if ( isset( $post ) && $post != null ) {
/* Post data array */
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author,
'post_content' => ( isset( $opt['dpp_posteditor'] ) && $opt['dpp_posteditor'] == 'gutenberg' ) ? wp_slash( $post->post_content ) : $post->post_content,
'post_excerpt' => $post->post_excerpt,
//'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => $post_status,
'post_title' => $post->post_title . $suffix,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);
$new_post_id = wp_insert_post( $args );
$taxonomies = array_map( 'sanitize_text_field', get_object_taxonomies( $post->post_type ) );
if ( ! empty( $taxonomies ) && is_array( $taxonomies ) ):
foreach ( $taxonomies as $taxonomy ) {
$post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) );
wp_set_object_terms( $new_post_id, $post_terms, $taxonomy, false );
}
endif;
$post_meta_infos = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=%d", $post_id ) );
if ( count( $post_meta_infos ) != 0 ) {
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
foreach ( $post_meta_infos as $meta_info ) {
$meta_key = sanitize_text_field( $meta_info->meta_key );
$meta_value = addslashes( $meta_info->meta_value );
$sql_query_sel[] = "SELECT $new_post_id, '$meta_key', '$meta_value'";
}
$sql_query .= implode( " UNION ALL ", $sql_query_sel );
$wpdb->query( $sql_query );
}
/*choice redirect */
if ( $post->post_type != 'post' ):$returnpage = '?post_type=' . $post->post_type; endif;
if ( ! empty( $redirectit ) && $redirectit == 'to_list' ):esc_url_raw( wp_redirect( admin_url( 'edit.php' . $returnpage ) ) );
elseif ( ! empty( $redirectit ) && $redirectit == 'to_page' ):esc_url_raw( wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) ) );
else:
wp_redirect( esc_url_raw( admin_url( 'edit.php' . $returnpage ) ) );
endif;
exit;
} else {
wp_die( 'Error! Post creation failed: ' . $post_id );
}
} else {
wp_die( 'Security check issue, Please try again.' );
}
}
/*Add link to action*/
public function dt_dpp_post_link( $actions, $post ) {
$opt = get_option( 'dpp_wpp_page_options' );
$link_title = ! empty( $opt['dpp_post_link_title'] ) ? esc_attr( $opt['dpp_post_link_title'] ) : 'Duplicate';
$opt = get_option( 'dpp_wpp_page_options' );
$post_status = ! empty( $opt['dpp_post_status'] ) ? esc_attr( $opt['dpp_post_status'] ) : 'draft';
if ( current_user_can( 'edit_posts' ) ) {
$actions['dpp'] = '<a href="admin.php?action=dt_dpp_post_as_draft&post=' . $post->ID . '&nonce=' . wp_create_nonce( 'dt-duplicate-page-' . $post->ID ) . '" title="Clone this as ' . $post_status . '" rel="permalink">' . $link_title . '</a>';
}
return $actions;
}
/*Add link to edit Post*/
public function dpp_wpp_page_custom_button() {
$opt = get_option( 'dpp_wpp_page_options' );
$link_title = ! empty( $opt['dpp_post_link_title'] ) ? esc_attr( $opt['dpp_post_link_title'] ) : 'Duplicate';
global $post;
$opt = get_option( 'dpp_wpp_page_options' );
$post_status = ! empty( $opt['duplicate_post_status'] ) ? esc_attr( $opt['duplicate_post_status'] ) : 'draft';
$html = '<div id="major-publishing-actions">';
$html .= '<div id="export-action">';
$html .= '<a href="admin.php?action=dt_dpp_post_as_draft&post=' . $post->ID . '&nonce=' . wp_create_nonce( 'dt-duplicate-page-' . $post->ID ) . '" title="Duplicate this as ' . $post_status . '" rel="permalink">' . $link_title . '</a>';
$html .= '</div>';
$html .= '</div>';
echo $html;
}
/*
* Add the duplicate link to edit screen - gutenberg
*/
public function dpp_wpp_button_guten() {
global $post;
if ( $post ) {
$opt = get_option( 'dpp_wpp_page_options' );
$post_status = ! empty( $opt['dpp_post_status'] ) ? esc_attr( $opt['dpp_post_status'] ) : 'draft';
if ( isset( $opt['dpp_posteditor'] ) && $opt['dpp_posteditor'] == 'gutenberg' ) {
?>
<style> .link_gutenberg {
text-align: center;
margin-top: 15px;
}
.link_gutenberg a {
text-decoration: none;
display: block;
height: 40px;
line-height: 28px;
padding: 3px 12px 2px;
background: #0073AA;
border-radius: 3px;
border-width: 1px;
border-style: solid;
color: #ffffff;
font-size: 16px;
}
.link_gutenberg a:hover {
background: #23282D;
border-color: #23282D;
}</style>
<script>jQuery(window).load(function (e) {
var dpp_postid = "<?php echo esc_attr( $post->ID ); ?>";
var dtnonce = "<?php echo wp_create_nonce( 'dt-duplicate-page-' . $post->ID );?>";
var dpp_posttitle = "Duplicate this as <?php echo esc_attr( $post_status ); ?>";
var dpp_duplicatelink = '<div class="link_gutenberg">';
dpp_duplicatelink += '<a href="admin.php?action=dt_dpp_post_as_draft&post=' + dpp_postid + '&nonce=' + dtnonce + '" title="' + dpp_posttitle + '">Duplicate</a>';
dpp_duplicatelink += '</div>';
jQuery('.edit-post-post-status').append(dpp_duplicatelink);
});</script>
<?php
}
}
}
/*Click here to clone Admin Bar*/
public function dpp_wpp_page_admin_bar_link() {
global $wp_admin_bar;
global $post;
$opt = get_option( 'dpp_wpp_page_options' );
$post_status = ! empty( $opt['dpp_post_status'] ) ? esc_attr( $opt['dpp_post_status'] ) : 'draft';
$current_object = get_queried_object();
if ( empty( $current_object ) ) {
return;
}
if ( ! empty( $current_object->post_type ) && ( $post_type_object = get_post_type_object( $current_object->post_type ) ) && ( $post_type_object->show_ui || $current_object->post_type == 'attachment' ) ) {
$wp_admin_bar->add_menu( array(
'parent' => 'edit',
'id' => 'dpp_this',
'title' => __( "Clone this as " . $post_status . "", 'duplicate-wp-page-post' ),
'href' => admin_url() . 'admin.php?action=dt_dpp_post_as_draft&post=' . $post->ID . '&nonce=' . wp_create_nonce( 'dt-duplicate-page-' . $post->ID )
) );
}
}
/*WP Url Redirect*/
static function dp_redirect( $url ) {
$result = '<script>window.location.href="' . $url . '"</script>';
print( $result );
}
/*plugin settings page link*/
function dpp_settings_link( $links, $file ) {
if ( $file == DPP_BASE_NAME ) {
$links[] = '<a href="' .
admin_url( 'options-general.php?page=dpp_page_settings' ) .
'">' . __( 'Settings' ) . '</a>';
}
return $links;
}
}
new dpp_wpp_page();
endif;
?>