'draft', 'duplicate_post_redirect' => 'to_list', 'duplicate_post_suffix' => '', 'duplicate_post_editor' => 'classic', ); $opt = get_option('duplicate_page_options'); if (!isset($opt['duplicate_post_status'])) { update_option('duplicate_page_options', $defaultsettings); } } /* Action Links */ public function duplicate_page_plugin_action_links($links, $file) { if ($file == plugin_basename(__FILE__)) { $duplicate_page_links = ''.__('Settings', 'duplicate-page').''; $duplicate_page_donate = ''.__('Donate', 'duplicate-page').''; array_unshift($links, $duplicate_page_donate); array_unshift($links, $duplicate_page_links); } return $links; } /* * Admin Menu */ public function duplicate_page_options_page() { add_options_page(__('Duplicate Page', 'duplicate-page'), __('Duplicate Page', 'duplicate-page'), 'manage_options', 'duplicate_page_settings', array(&$this, 'duplicate_page_settings')); } /* * Duplicate Page Admin Settings */ public function duplicate_page_settings() { if (current_user_can('manage_options')) { include 'inc/admin-settings.php'; } } /* * Main function */ public function dt_duplicate_post_as_draft() { /* * get Nonce value */ $nonce = sanitize_text_field($_REQUEST['nonce']); /* * get the original post id */ $post_id = (isset($_GET['post']) ? intval($_GET['post']) : intval($_POST['post'])); $post = get_post($post_id); $current_user_id = get_current_user_id(); if(wp_verify_nonce( $nonce, 'dt-duplicate-page-'.$post_id)) { if (current_user_can('manage_options') || current_user_can('edit_others_posts')){ $this->duplicate_edit_post($post_id); } else if (current_user_can('contributor') && $current_user_id == $post->post_author){ $this->duplicate_edit_post($post_id, 'pending'); } else if (current_user_can('edit_posts') && $current_user_id == $post->post_author ){ $this->duplicate_edit_post($post_id); } else { wp_die(__('Unauthorized Access.','duplicate-page')); } } else { wp_die(__('Security check issue, Please try again.','duplicate-page')); } } /** * Duplicate edit post */ public function duplicate_edit_post($post_id,$post_status_update=''){ global $wpdb; $opt = get_option('duplicate_page_options'); $suffix = isset($opt['duplicate_post_suffix']) && !empty($opt['duplicate_post_suffix']) ? ' -- '.esc_attr($opt['duplicate_post_suffix']) : ''; if($post_status_update == ''){ $post_status = !empty($opt['duplicate_post_status']) ? esc_attr($opt['duplicate_post_status']) : 'draft'; }else{ $post_status = $post_status_update; } $redirectit = !empty($opt['duplicate_post_redirect']) ? esc_attr($opt['duplicate_post_redirect']) : 'to_list'; if (!(isset($_GET['post']) || isset($_POST['post']) || (isset($_REQUEST['action']) && 'dt_duplicate_post_as_draft' == sanitize_text_field($_REQUEST['action'])))) { wp_die(__('No post to duplicate has been supplied!','duplicate-page')); } $returnpage = ''; /* * and all the original post data then */ $post = get_post($post_id); /* * if you don't want current user to be the new post author, * then change next couple of lines to this: $new_post_author = $post->post_author; */ $current_user = wp_get_current_user(); $new_post_author = $current_user->ID; /* * if post data exists, create the post duplicate */ if (isset($post) && $post != null) { /* * new post data array */ $args = array( 'comment_status' => $post->comment_status, 'ping_status' => $post->ping_status, 'post_author' => $new_post_author, 'post_content' => (isset($opt['duplicate_post_editor']) && $opt['duplicate_post_editor'] == 'gutenberg') ? wp_slash($post->post_content) : $post->post_content, 'post_excerpt' => $post->post_excerpt, '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, ); /* * insert the post by wp_insert_post() function */ $new_post_id = wp_insert_post($args); /* * get all current post terms ad set them to the new post draft */ $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; /* * duplicate all post meta */ $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); } /* * finally, redirecting to your choice */ if ($post->post_type != 'post'): $returnpage = '?post_type='.$post->post_type; endif; if (!empty($redirectit) && $redirectit == 'to_list'): wp_redirect(esc_url_raw(admin_url('edit.php'.$returnpage))); elseif (!empty($redirectit) && $redirectit == 'to_page'): wp_redirect(esc_url_raw(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, could not find original post: ','duplicate-page').$post_id); } } /* * Add the duplicate link to action list for post_row_actions */ public function dt_duplicate_post_link($actions, $post) { $opt = get_option('duplicate_page_options'); $post_status = !empty($opt['duplicate_post_status']) ? esc_attr($opt['duplicate_post_status']) : 'draft'; if (current_user_can('edit_posts')) { $actions['duplicate'] = ''.__('Duplicate This', 'duplicate-page').''; } return $actions; } /* * Add the duplicate link to edit screen - classic editor */ public function duplicate_page_custom_button_classic() { global $post; $opt = get_option('duplicate_page_options'); $post_status = !empty($opt['duplicate_post_status']) ? esc_attr($opt['duplicate_post_status']) : 'draft'; $html = '