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