PluginProbe ʕ •ᴥ•ʔ
Yoast Duplicate Post / 3.1.2
Yoast Duplicate Post v3.1.2
trunk 0.3 0.4 0.5 0.6 0.6.1 1.0 1.1 1.1.1 1.1.2 2.0 2.0.1 2.0.2 2.1 2.1.1 2.2 2.3 2.4 2.4.1 2.5 2.6 3.0 3.0.1 3.0.2 3.0.3 3.1 3.1.1 3.1.2 3.2 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 4.0 4.0.1 4.0.2 4.1 4.1.1 4.1.2 4.2 4.3 4.4 4.5 4.6
duplicate-post / duplicate-post-admin.php
duplicate-post Last commit date
donate.png 9 years ago duplicate-post-admin.php 9 years ago duplicate-post-common.php 9 years ago duplicate-post-options.php 9 years ago duplicate-post.css 9 years ago duplicate-post.php 9 years ago gpl-2.0.txt 9 years ago readme.txt 9 years ago
duplicate-post-admin.php
704 lines
1 <?php
2 // Added by WarmStal
3 if(!is_admin())
4 return;
5
6 require_once (dirname(__FILE__).'/duplicate-post-options.php');
7
8 /**
9 * Wrapper for the option 'duplicate_post_version'
10 */
11 function duplicate_post_get_installed_version() {
12 return get_site_option( 'duplicate_post_version' );
13 }
14
15 /**
16 * Wrapper for the defined constant DUPLICATE_POST_CURRENT_VERSION
17 */
18 function duplicate_post_get_current_version() {
19 return DUPLICATE_POST_CURRENT_VERSION;
20 }
21
22 /**
23 * Plugin upgrade
24 */
25 add_action('admin_init','duplicate_post_plugin_upgrade');
26
27 function duplicate_post_plugin_upgrade() {
28 $installed_version = duplicate_post_get_installed_version();
29
30 if ( $installed_version==duplicate_post_get_current_version() )
31 return;
32
33
34 if (empty($installed_version)) {
35 // Get default roles
36 $default_roles = array(
37 3 => 'editor',
38 8 => 'administrator',
39 );
40
41 // Cycle all roles and assign capability if its level >= duplicate_post_copy_user_level
42 foreach ($default_roles as $level => $name){
43 $role = get_role($name);
44 if(!empty($role)) $role->add_cap( 'copy_posts' );
45 }
46 } else {
47 $min_user_level = get_option('duplicate_post_copy_user_level');
48
49 if (!empty($min_user_level)){
50 // Get default roles
51 $default_roles = array(
52 1 => 'contributor',
53 2 => 'author',
54 3 => 'editor',
55 8 => 'administrator',
56 );
57
58 // Cycle all roles and assign capability if its level >= duplicate_post_copy_user_level
59 foreach ($default_roles as $level => $name){
60 $role = get_role($name);
61 if ($role && $min_user_level <= $level)
62 $role->add_cap( 'copy_posts' );
63 }
64 delete_option('duplicate_post_copy_user_level');
65 }
66 }
67
68
69 add_option('duplicate_post_copytitle','1');
70 add_option('duplicate_post_copydate','0');
71 add_option('duplicate_post_copystatus','0');
72 add_option('duplicate_post_copyslug','1');
73 add_option('duplicate_post_copyexcerpt','1');
74 add_option('duplicate_post_copycontent','1');
75 add_option('duplicate_post_copythumbnail','1');
76 add_option('duplicate_post_copytemplate','1');
77 add_option('duplicate_post_copyformat','1');
78 add_option('duplicate_post_copyauthor','0');
79 add_option('duplicate_post_copypassword','0');
80 add_option('duplicate_post_copyattachments','0');
81 add_option('duplicate_post_copychildren','0');
82 add_option('duplicate_post_copycomments','0');
83 add_option('duplicate_post_copymenuorder','1');
84 add_option('duplicate_post_taxonomies_blacklist',array());
85 add_option('duplicate_post_blacklist','');
86 add_option('duplicate_post_types_enabled',array('post', 'page'));
87 add_option('duplicate_post_show_row','1');
88 add_option('duplicate_post_show_adminbar','1');
89 add_option('duplicate_post_show_submitbox','1');
90 add_option('duplicate_post_show_bulkactions','1');
91
92 $taxonomies_blacklist = get_option('duplicate_post_taxonomies_blacklist');
93 if ($taxonomies_blacklist == "") $taxonomies_blacklist = array();
94 if(in_array('post_format',$taxonomies_blacklist)){
95 update_option('duplicate_post_copyformat', 0);
96 $taxonomies_blacklist = array_diff($taxonomies_blacklist, array('post_format'));
97 update_option('duplicate_post_taxonomies_blacklist', $taxonomies_blacklist);
98 }
99
100 $meta_blacklist = explode(",",get_option('duplicate_post_blacklist'));
101 if ($meta_blacklist == "") $meta_blacklist = array();
102 $meta_blacklist = array_map('trim', $meta_blacklist);
103 if(in_array('_wp_page_template', $meta_blacklist)){
104 update_option('duplicate_post_copytemplate', 0);
105 $meta_blacklist = array_diff($meta_blacklist, array('_wp_page_template'));
106 }
107 if(in_array('_thumbnail_id', $meta_blacklist)){
108 update_option('duplicate_post_copythumbnail', 0);
109 $meta_blacklist = array_diff($meta_blacklist, array('_thumbnail_id'));
110 }
111 update_option('duplicate_post_blacklist', implode(',',$meta_blacklist));
112
113 delete_option('duplicate_post_admin_user_level');
114 delete_option('duplicate_post_create_user_level');
115 delete_option('duplicate_post_view_user_level');
116 delete_option('dp_notice');
117
118 delete_option('duplicate_post_version');
119 update_site_option( 'duplicate_post_version', duplicate_post_get_current_version() );
120
121 delete_option('duplicate_post_show_notice', 0);
122 update_site_option('duplicate_post_show_notice', 1);
123
124 }
125
126 if (get_option('duplicate_post_show_row') == 1){
127 add_filter('post_row_actions', 'duplicate_post_make_duplicate_link_row',10,2);
128 add_filter('page_row_actions', 'duplicate_post_make_duplicate_link_row',10,2);
129 }
130
131
132 if (get_site_option('duplicate_post_show_notice') == 1){
133 /**
134 * Shows the update notice
135 */
136 function duplicate_post_show_update_notice() {
137 if(!current_user_can( 'manage_options')) return;
138 $class = 'notice is-dismissible';
139 $message = '<strong>'.esc_html__('Duplicate Post has new features!', 'duplicate-post').'</strong><br/>';
140 $message .= '<em>'.esc_html__('Clone posts in bulk (WP 4.7+)', 'duplicate-post').''.esc_html__('Wildcards in custom field names', 'duplicate-post').''.esc_html__('Options for thumbnail, post format, post template, author, menu order', 'duplicate-post').'</em><br/>';
141 $message .= sprintf(__('Please <a href="%s">review the settings</a> to make sure it works as you expect.', 'duplicate-post'), admin_url('options-general.php?page=duplicatepost')).'<br/>';
142 $message .= '<strong>'.__('Help me develop the plugin and provide support by <a href="http://lopo.it/duplicate-post-plugin">donating even a small sum</a>.', 'duplicate-post').'</strong>';
143 global $wp_version;
144 if( version_compare($wp_version, '4.2') < 0 ){
145 $message .= ' | <a id="duplicate-post-dismiss-notice" href="javascript:duplicate_post_dismiss_notice();">'.__('Dismiss this notice.').'</a>';
146 }
147 echo '<div id="duplicate-post-notice" class="'.$class.'"><p>'.$message.'</p></div>';
148 echo "<script>
149 function duplicate_post_dismiss_notice(){
150 var data = {
151 'action': 'duplicate_post_dismiss_notice',
152 };
153
154 jQuery.post(ajaxurl, data, function(response) {
155 jQuery('#duplicate-post-notice').hide();
156 });
157 }
158
159 jQuery(document).ready(function(){
160 jQuery('body').on('click', '.notice-dismiss', function(){
161 duplicate_post_dismiss_notice();
162 });
163 });
164 </script>";
165 }
166
167 if(is_multisite()){
168 add_action( 'network_admin_notices', 'duplicate_post_show_update_notice' );
169 } else {
170 add_action( 'admin_notices', 'duplicate_post_show_update_notice' );
171 }
172 add_action( 'wp_ajax_duplicate_post_dismiss_notice', 'duplicate_post_dismiss_notice' );
173
174 function duplicate_post_dismiss_notice() {
175 $result = update_site_option('duplicate_post_show_notice', 0);
176 return $result;
177 wp_die();
178 }
179 }
180
181 /**
182 * Add the link to action list for post_row_actions
183 */
184 function duplicate_post_make_duplicate_link_row($actions, $post) {
185 if (duplicate_post_is_current_user_allowed_to_copy() && duplicate_post_is_post_type_enabled($post->post_type)) {
186 $actions['clone'] = '<a href="'.duplicate_post_get_clone_post_link( $post->ID , 'display', false).'" title="'
187 . esc_attr__("Clone this item", 'duplicate-post')
188 . '">' . esc_html__('Clone', 'duplicate-post') . '</a>';
189 $actions['edit_as_new_draft'] = '<a href="'. duplicate_post_get_clone_post_link( $post->ID ) .'" title="'
190 . esc_attr__('Copy to a new draft', 'duplicate-post')
191 . '">' . esc_html__('New Draft', 'duplicate-post') . '</a>';
192 }
193 return $actions;
194 }
195
196 /**
197 * Add a button in the post/page edit screen to create a clone
198 */
199 if (get_option('duplicate_post_show_submitbox') == 1){
200 add_action( 'post_submitbox_start', 'duplicate_post_add_duplicate_post_button' );
201 }
202
203 function duplicate_post_add_duplicate_post_button() {
204 if ( isset( $_GET['post'] )){
205 $id = $_GET['post'];
206 $post = get_post($id);
207 if(duplicate_post_is_current_user_allowed_to_copy() && duplicate_post_is_post_type_enabled($post->post_type)) {
208 ?>
209 <div id="duplicate-action">
210 <a class="submitduplicate duplication"
211 href="<?php echo duplicate_post_get_clone_post_link( $_GET['post'] ) ?>"><?php esc_html_e('Copy to a new draft', 'duplicate-post'); ?>
212 </a>
213 </div>
214 <?php
215 }
216 }
217 }
218
219 /**
220 * Connect actions to functions
221 */
222 add_action('admin_action_duplicate_post_save_as_new_post', 'duplicate_post_save_as_new_post');
223 add_action('admin_action_duplicate_post_save_as_new_post_draft', 'duplicate_post_save_as_new_post_draft');
224
225 /*
226 * This function calls the creation of a new copy of the selected post (as a draft)
227 * then redirects to the edit post screen
228 */
229 function duplicate_post_save_as_new_post_draft(){
230 duplicate_post_save_as_new_post('draft');
231 }
232
233 add_filter('removable_query_args', 'duplicate_post_add_removable_query_arg', 10, 1);
234
235 function duplicate_post_add_removable_query_arg( $removable_query_args ){
236 $removable_query_args[] = 'cloned';
237 return $removable_query_args;
238 }
239
240 /*
241 * This function calls the creation of a new copy of the selected post (by default preserving the original publish status)
242 * then redirects to the post list
243 */
244 function duplicate_post_save_as_new_post($status = ''){
245 if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'duplicate_post_save_as_new_post' == $_REQUEST['action'] ) ) ) {
246 wp_die(esc_html__('No post to duplicate has been supplied!', 'duplicate-post'));
247 }
248
249 // Get the original post
250 $id = (isset($_GET['post']) ? $_GET['post'] : $_POST['post']);
251 $post = get_post($id);
252
253 // Copy the post and insert it
254 if (isset($post) && $post!=null) {
255 $new_id = duplicate_post_create_duplicate($post, $status);
256
257 if ($status == ''){
258 $sendback = remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'cloned', 'ids'), admin_url( 'edit.php?post_type='.$post->post_type) );
259 // Redirect to the post list screen
260 wp_redirect( add_query_arg( array( 'cloned' => 1, 'ids' => $post->ID), $sendback ) );
261 } else {
262 // Redirect to the edit screen for the new draft post
263 wp_redirect( add_query_arg( array( 'cloned' => 1, 'ids' => $post->ID), admin_url( 'post.php?action=edit&post=' . $new_id ) ) );
264 }
265 exit;
266
267 } else {
268 wp_die(esc_html__('Copy creation failed, could not find original:', 'duplicate-post') . ' ' . htmlspecialchars($id));
269 }
270 }
271
272 /**
273 * Copy the taxonomies of a post to another post
274 */
275 function duplicate_post_copy_post_taxonomies($new_id, $post) {
276 global $wpdb;
277 if (isset($wpdb->terms)) {
278 // Clear default category (added by wp_insert_post)
279 wp_set_object_terms( $new_id, NULL, 'category' );
280
281 $post_taxonomies = get_object_taxonomies($post->post_type);
282 // severl plugins just add support to post-formats but don't register post_format taxonomy
283 if(post_type_supports($post->post_type, 'post-formats') && !in_array('post_format', $post_taxonomies)){
284 $post_taxonomies[] = 'post_format';
285 }
286
287 $taxonomies_blacklist = get_option('duplicate_post_taxonomies_blacklist');
288 if ($taxonomies_blacklist == "") $taxonomies_blacklist = array();
289 if(get_option('duplicate_post_copyformat') == 0){
290 $taxonomies_blacklist[] = 'post_format';
291 }
292 $taxonomies = array_diff($post_taxonomies, $taxonomies_blacklist);
293 foreach ($taxonomies as $taxonomy) {
294 $post_terms = wp_get_object_terms($post->ID, $taxonomy, array( 'orderby' => 'term_order' ));
295 $terms = array();
296 for ($i=0; $i<count($post_terms); $i++) {
297 $terms[] = $post_terms[$i]->slug;
298 }
299 wp_set_object_terms($new_id, $terms, $taxonomy);
300 }
301 }
302 }
303
304 /**
305 * Copy the meta information of a post to another post
306 */
307 function duplicate_post_copy_post_meta_info($new_id, $post) {
308 $post_meta_keys = get_post_custom_keys($post->ID);
309 if (empty($post_meta_keys)) return;
310 $meta_blacklist = get_option('duplicate_post_blacklist');
311 if ($meta_blacklist == ""){
312 $meta_blacklist = array();
313 } else {
314 $meta_blacklist = explode(',', $meta_blacklist);
315 $meta_blacklist = array_filter($meta_blacklist);
316 $meta_blacklist = array_map('trim', $meta_blacklist);
317 }
318 $meta_blacklist[] = '_wpas_done_all'; //Jetpack Publicize
319 $meta_blacklist[] = '_wpas_done_'; //Jetpack Publicize
320 $meta_blacklist[] = '_wpas_mess'; //Jetpack Publicize
321 $meta_blacklist[] = '_edit_lock'; // edit lock
322 $meta_blacklist[] = '_edit_last'; // edit lock
323 if(get_option('duplicate_post_copytemplate') == 0){
324 $meta_blacklist[] = '_wp_page_template';
325 }
326 if(get_option('duplicate_post_copythumbnail') == 0){
327 $meta_blacklist[] = '_thumbnail_id';
328 }
329
330 $meta_blacklist = apply_filters( 'duplicate_post_blacklist_filter' , $meta_blacklist );
331
332 $meta_blacklist_string = '('.implode(')|(',$meta_blacklist).')';
333 if(strpos($meta_blacklist_string, '*') !== false){
334 $meta_blacklist_string = str_replace(array('*'), array('[a-zA-Z0-9_]*'), $meta_blacklist_string);
335
336 $meta_keys = array();
337 foreach($post_meta_keys as $meta_key){
338 if(!preg_match('#^'.$meta_blacklist_string.'$#', $meta_key))
339 $meta_keys[] = $meta_key;
340 }
341 } else {
342 $meta_keys = array_diff($post_meta_keys, $meta_blacklist);
343 }
344
345 $meta_keys = apply_filters( 'duplicate_post_meta_keys_filter', $meta_keys );
346
347 foreach ($meta_keys as $meta_key) {
348 $meta_values = get_post_custom_values($meta_key, $post->ID);
349 foreach ($meta_values as $meta_value) {
350 $meta_value = maybe_unserialize($meta_value);
351 add_post_meta($new_id, $meta_key, duplicate_post_wp_slash($meta_value));
352 }
353 }
354 }
355
356 /*
357 * Workaround for inconsistent wp_slash.
358 * Works only with WP 4.4+ (map_deep)
359 */
360 function duplicate_post_addslashes_deep( $value ) {
361 if (function_exists('map_deep')){
362 return map_deep( $value, 'duplicate_post_addslashes_to_strings_only' );
363 } else {
364 return wp_slash( $value );
365 }
366 }
367
368 function duplicate_post_addslashes_to_strings_only( $value ) {
369 return is_string( $value ) ? addslashes( $value ) : $value;
370 }
371
372 function duplicate_post_wp_slash( $value ) {
373 return duplicate_post_addslashes_deep( $value );
374 }
375
376
377
378 /**
379 * Copy the attachments
380 */
381 function duplicate_post_copy_attachments($new_id, $post){
382 // get thumbnail ID
383 $old_thumbnail_id = get_post_thumbnail_id($post->ID);
384 // get children
385 $children = get_posts(array( 'post_type' => 'any', 'numberposts' => -1, 'post_status' => 'any', 'post_parent' => $post->ID ));
386 // clone old attachments
387 foreach($children as $child){
388 if ($child->post_type != 'attachment') continue;
389 $url = wp_get_attachment_url($child->ID);
390 // Let's copy the actual file
391 $tmp = download_url( $url );
392 if( is_wp_error( $tmp ) ) {
393 @unlink($tmp);
394 continue;
395 }
396
397 $desc = wp_slash($child->post_content);
398
399 $file_array = array();
400 $file_array['name'] = basename($url);
401 $file_array['tmp_name'] = $tmp;
402 // "Upload" to the media collection
403 $new_attachment_id = media_handle_sideload( $file_array, $new_id, $desc );
404
405 if ( is_wp_error($new_attachment_id) ) {
406 @unlink($file_array['tmp_name']);
407 continue;
408 }
409 $new_post_author = wp_get_current_user();
410 $cloned_child = array(
411 'ID' => $new_attachment_id,
412 'post_title' => $child->post_title,
413 'post_exceprt' => $child->post_title,
414 'post_author' => $new_post_author->ID
415 );
416 wp_update_post( wp_slash($cloned_child) );
417
418 $alt_title = get_post_meta($child->ID, '_wp_attachment_image_alt', true);
419 if($alt_title) update_post_meta($new_attachment_id, '_wp_attachment_image_alt', wp_slash($alt_title));
420
421 // if we have cloned the post thumbnail, set the copy as the thumbnail for the new post
422 if(get_option('duplicate_post_copythumbnail') == 1 && $old_thumbnail_id == $child->ID){
423 set_post_thumbnail($new_id, $new_attachment_id);
424 }
425
426 }
427 }
428
429 /**
430 * Copy children posts
431 */
432 function duplicate_post_copy_children($new_id, $post){
433 // get children
434 $children = get_posts(array( 'post_type' => 'any', 'numberposts' => -1, 'post_status' => 'any', 'post_parent' => $post->ID ));
435 // clone old attachments
436 foreach($children as $child){
437 if ($child->post_type == 'attachment') continue;
438 duplicate_post_create_duplicate($child, '', $new_id);
439 }
440 }
441
442 /**
443 * Copy comments
444 */
445 function duplicate_post_copy_comments($new_id, $post){
446 $comments = get_comments(array(
447 'post_id' => $post->ID,
448 'order' => 'ASC',
449 'orderby' => 'comment_date_gmt'
450 ));
451
452 $old_id_to_new = array();
453 foreach ($comments as $comment){
454 //do not copy pingbacks or trackbacks
455 if(!empty($comment->comment_type)) continue;
456 $parent = ($comment->comment_parent && $old_id_to_new[$comment->comment_parent])?$old_id_to_new[$comment->comment_parent]:0;
457 $commentdata = array(
458 'comment_post_ID' => $new_id,
459 'comment_author' => $comment->comment_author,
460 'comment_author_email' => $comment->comment_author_email,
461 'comment_author_url' => $comment->comment_author_url,
462 'comment_content' => $comment->comment_content,
463 'comment_type' => '',
464 'comment_parent' => $parent,
465 'user_id' => $comment->user_id,
466 'comment_author_IP' => $comment->comment_author_IP,
467 'comment_agent' => $comment->comment_agent,
468 'comment_karma' => $comment->comment_karma,
469 'comment_approved' => $comment->comment_approved,
470 );
471 if(get_option('duplicate_post_copydate') == 1){
472 $commentdata['comment_date'] = $comment->comment_date ;
473 $commentdata['comment_date_gmt'] = get_gmt_from_date($comment->comment_date);
474 }
475 $new_comment_id = wp_insert_comment($commentdata);
476 $old_id_to_new[$comment->comment_ID] = $new_comment_id;
477 }
478 }
479
480 // Using our action hooks
481
482 add_action('dp_duplicate_post', 'duplicate_post_copy_post_meta_info', 10, 2);
483 add_action('dp_duplicate_page', 'duplicate_post_copy_post_meta_info', 10, 2);
484
485 if(get_option('duplicate_post_copychildren') == 1){
486 add_action('dp_duplicate_post', 'duplicate_post_copy_children', 20, 2);
487 add_action('dp_duplicate_page', 'duplicate_post_copy_children', 20, 2);
488 }
489
490 if(get_option('duplicate_post_copyattachments') == 1){
491 add_action('dp_duplicate_post', 'duplicate_post_copy_attachments', 30, 2);
492 add_action('dp_duplicate_page', 'duplicate_post_copy_attachments', 30, 2);
493 }
494
495 if(get_option('duplicate_post_copycomments') == 1){
496 add_action('dp_duplicate_post', 'duplicate_post_copy_comments', 40, 2);
497 add_action('dp_duplicate_page', 'duplicate_post_copy_comments', 40, 2);
498 }
499
500 add_action('dp_duplicate_post', 'duplicate_post_copy_post_taxonomies', 50, 2);
501 add_action('dp_duplicate_page', 'duplicate_post_copy_post_taxonomies', 50, 2);
502
503 /**
504 * Create a duplicate from a post
505 */
506 function duplicate_post_create_duplicate($post, $status = '', $parent_id = '') {
507
508 do_action('duplicate_post_pre_copy');
509
510 if (!duplicate_post_is_post_type_enabled($post->post_type) && $post->post_type != 'attachment')
511 wp_die(esc_html__('Copy features for this post type are not enabled in options page', 'duplicate-post'));
512
513 $new_post_status = (empty($status))? $post->post_status: $status;
514
515 if ($post->post_type != 'attachment'){
516 $prefix = sanitize_text_field(get_option('duplicate_post_title_prefix'));
517 $suffix = sanitize_text_field(get_option('duplicate_post_title_suffix'));
518 $title = ' ';
519 if (get_option('duplicate_post_copytitle') == 1) {
520 $title = $post->post_title;
521 if (!empty($prefix)) $prefix.= " ";
522 if (!empty($suffix)) $suffix = " ".$suffix;
523 } else {
524 $title = ' ';
525 }
526 $title = trim($prefix.$title.$suffix);
527
528 if ($title == ''){
529 // empty title
530 $title = __('Untitled');
531 }
532 if (get_option('duplicate_post_copystatus') == 0){
533 $new_post_status = 'draft';
534 } else {
535 if ( 'publish' == $new_post_status || 'future' == $new_post_status ){
536 // check if the user has the right capability
537 if(is_post_type_hierarchical( $post->post_type )){
538 if(!current_user_can('publish_pages')){
539 $new_post_status = 'pending';
540 }
541 } else {
542 if(!current_user_can('publish_posts')){
543 $new_post_status = 'pending';
544 }
545 }
546 }
547 }
548 }
549
550 $new_post_author = wp_get_current_user();
551 $new_post_author_id = $new_post_author->ID;
552 if ( get_option('duplicate_post_copyauthor') == '1' ){
553 // check if the user has the right capability
554 if(is_post_type_hierarchical( $post->post_type )){
555 if(current_user_can('edit_others_pages')){
556 $new_post_author_id = $post->post_author;
557 }
558 } else {
559 if(current_user_can('edit_others_posts')){
560 $new_post_author_id = $post->post_author;
561 }
562 }
563 }
564
565 $menu_order = (get_option('duplicate_post_copymenuorder') == '1') ? $post->menu_order : 0;
566 $increase_menu_order_by = get_option('duplicate_post_increase_menu_order_by');
567 if(!empty($increase_menu_order_by) && is_numeric($increase_menu_order_by)){
568 $menu_order += intval($increase_menu_order_by);
569 }
570
571 $new_post = array(
572 'menu_order' => $menu_order,
573 'comment_status' => $post->comment_status,
574 'ping_status' => $post->ping_status,
575 'post_author' => $new_post_author_id,
576 'post_content' => (get_option('duplicate_post_copycontent') == '1') ? $post->post_content : "" ,
577 'post_content_filtered' => (get_option('duplicate_post_copycontent') == '1') ? $post->post_content_filtered : "" ,
578 'post_excerpt' => (get_option('duplicate_post_copyexcerpt') == '1') ? $post->post_excerpt : "",
579 'post_mime_type' => $post->post_mime_type,
580 'post_parent' => $new_post_parent = empty($parent_id)? $post->post_parent : $parent_id,
581 'post_password' => (get_option('duplicate_post_copypassword') == '1') ? $post->post_password: "",
582 'post_status' => $new_post_status,
583 'post_title' => $title,
584 'post_type' => $post->post_type,
585 );
586
587 if(get_option('duplicate_post_copydate') == 1){
588 $new_post['post_date'] = $new_post_date = $post->post_date ;
589 $new_post['post_date_gmt'] = get_gmt_from_date($new_post_date);
590 }
591
592 $new_post_id = wp_insert_post(wp_slash($new_post));
593
594 // If the copy is published or scheduled, we have to set a proper slug.
595 if ($new_post_status == 'publish' || $new_post_status == 'future'){
596 $post_name = $post->post_name;
597 if(get_option('duplicate_post_copyslug') != 1){
598 $post_name = '';
599 }
600 $post_name = wp_unique_post_slug($post_name, $new_post_id, $new_post_status, $post->post_type, $new_post_parent);
601
602 $new_post = array();
603 $new_post['ID'] = $new_post_id;
604 $new_post['post_name'] = $post_name;
605
606 // Update the post into the database
607 wp_update_post( wp_slash($new_post) );
608 }
609
610 // If you have written a plugin which uses non-WP database tables to save
611 // information about a post you can hook this action to dupe that data.
612 if ($post->post_type == 'page' || is_post_type_hierarchical( $post->post_type ))
613 do_action( 'dp_duplicate_page', $new_post_id, $post );
614 else
615 do_action( 'dp_duplicate_post', $new_post_id, $post );
616
617 delete_post_meta($new_post_id, '_dp_original');
618 add_post_meta($new_post_id, '_dp_original', $post->ID);
619
620 do_action('duplicate_post_post_copy');
621
622 return $new_post_id;
623 }
624
625 //Add some links on the plugin page
626 add_filter('plugin_row_meta', 'duplicate_post_add_plugin_links', 10, 2);
627
628 function duplicate_post_add_plugin_links($links, $file) {
629 if ( $file == plugin_basename(dirname(__FILE__).'/duplicate-post.php') ) {
630 $links[] = '<a href="http://lopo.it/duplicate-post-plugin">' . esc_html__('Donate', 'duplicate-post') . '</a>';
631 $links[] = '<a href="https://translate.wordpress.org/projects/wp-plugins/duplicate-post">' . esc_html__('Translate', 'duplicate-post') . '</a>';
632 }
633 return $links;
634 }
635
636 /*** NOTICES ***/
637
638 add_action( 'admin_notices', 'duplicate_post_action_admin_notice' );
639
640 function duplicate_post_action_admin_notice() {
641 if ( ! empty( $_REQUEST['cloned'] ) ) {
642 $copied_posts = intval( $_REQUEST['cloned'] );
643 printf( '<div id="message" class="updated fade"><p>' .
644 _n( '%s item copied.',
645 '%s items copied.',
646 $copied_posts,
647 'duplicate-post'
648 ) . '</p></div>', $copied_posts );
649 remove_query_arg( 'cloned' );
650 }
651 }
652
653
654 /*** BULK ACTIONS ***/
655
656 add_action('admin_init', 'duplicate_post_add_bulk_filters_for_enabled_post_types');
657
658 function duplicate_post_add_bulk_filters_for_enabled_post_types(){
659 if(get_option('duplicate_post_show_bulkactions') != 1) return;
660 $duplicate_post_types_enabled = get_option('duplicate_post_types_enabled', array ('post', 'page'));
661 if(!is_array($duplicate_post_types_enabled)) $duplicate_post_types_enabled = array($duplicate_post_types_enabled);
662 foreach($duplicate_post_types_enabled as $duplicate_post_type_enabled){
663 add_filter( "bulk_actions-edit-{$duplicate_post_type_enabled}", 'duplicate_post_register_bulk_action' );
664 add_filter( "handle_bulk_actions-edit-{$duplicate_post_type_enabled}", 'duplicate_post_action_handler', 10, 3 );
665 }
666 }
667
668 function duplicate_post_register_bulk_action($bulk_actions) {
669 $bulk_actions['duplicate_post_clone'] = esc_html__( 'Clone', 'duplicate-post');
670 return $bulk_actions;
671 }
672
673 function duplicate_post_action_handler( $redirect_to, $doaction, $post_ids ) {
674 if ( $doaction !== 'duplicate_post_clone' ) {
675 return $redirect_to;
676 }
677 $counter = 0;
678 foreach ( $post_ids as $post_id ) {
679 $post = get_post($post_id);
680 if(!empty($post)){
681 if( get_option('duplicate_post_copychildren') != 1
682 || !is_post_type_hierarchical( $post->post_type )
683 || (is_post_type_hierarchical( $post->post_type ) && !duplicate_post_has_ancestors_marked($post, $post_ids))){
684 if(duplicate_post_create_duplicate($post)){
685 $counter++;
686 }
687 }
688 }
689 }
690 $redirect_to = add_query_arg( 'cloned', $counter, $redirect_to );
691 return $redirect_to;
692 }
693
694 function duplicate_post_has_ancestors_marked($post, $post_ids){
695 $ancestors_in_array = 0;
696 $parent = $post->ID;
697 while ($parent = wp_get_post_parent_id($parent)){
698 if(in_array($parent, $post_ids)){
699 $ancestors_in_array++;
700 }
701 }
702 return ($ancestors_in_array !== 0);
703 }
704