duplicate-page
Last commit date
css
3 years ago
inc
3 years ago
js
3 years ago
languages
3 years ago
duplicatepage.php
3 years ago
readme.txt
3 years ago
duplicatepage.php
396 lines
| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: Duplicate Page |
| 4 | Plugin URI: https://wordpress.org/plugins/duplicate-page/ |
| 5 | Description: Duplicate Posts, Pages and Custom Posts using single click. |
| 6 | Author: mndpsingh287 |
| 7 | Version: 4.5 |
| 8 | Author URI: https://profiles.wordpress.org/mndpsingh287/ |
| 9 | License: GPLv2 |
| 10 | Text Domain: duplicate-page |
| 11 | */ |
| 12 | if (!defined('DUPLICATE_PAGE_PLUGIN_DIRNAME')) { |
| 13 | define('DUPLICATE_PAGE_PLUGIN_DIRNAME', plugin_basename(dirname(__FILE__))); |
| 14 | } |
| 15 | if (!defined('DUPLICATE_PAGE_PLUGIN_VERSION')) { |
| 16 | define('DUPLICATE_PAGE_PLUGIN_VERSION', '4.5'); |
| 17 | } |
| 18 | if (!class_exists('duplicate_page')): |
| 19 | class duplicate_page |
| 20 | { |
| 21 | /* |
| 22 | * AutoLoad Hooks |
| 23 | */ |
| 24 | public function __construct() |
| 25 | { |
| 26 | $opt = get_option('duplicate_page_options'); |
| 27 | register_activation_hook(__FILE__, array(&$this, 'duplicate_page_install')); |
| 28 | add_action('admin_menu', array(&$this, 'duplicate_page_options_page')); |
| 29 | add_filter('plugin_action_links', array(&$this, 'duplicate_page_plugin_action_links'), 10, 2); |
| 30 | add_action('admin_action_dt_duplicate_post_as_draft', array(&$this, 'dt_duplicate_post_as_draft')); |
| 31 | |
| 32 | add_filter('post_row_actions', array(&$this, 'dt_duplicate_post_link'), 10, 2); |
| 33 | add_filter('page_row_actions', array(&$this, 'dt_duplicate_post_link'), 10, 2); |
| 34 | if (isset($opt['duplicate_post_editor']) && $opt['duplicate_post_editor'] == 'gutenberg') { |
| 35 | add_action('admin_head', array(&$this, 'duplicate_page_custom_button_guten')); |
| 36 | } elseif(isset($opt['duplicate_post_editor']) && $opt['duplicate_post_editor'] == 'all'){ |
| 37 | add_action('admin_head', array(&$this, 'duplicate_page_custom_button_guten')); |
| 38 | add_action('post_submitbox_misc_actions', array(&$this, 'duplicate_page_custom_button_classic')); |
| 39 | } else { |
| 40 | add_action('post_submitbox_misc_actions', array(&$this, 'duplicate_page_custom_button_classic')); |
| 41 | } |
| 42 | add_action('wp_before_admin_bar_render', array(&$this, 'duplicate_page_admin_bar_link')); |
| 43 | add_action('init', array(&$this, 'duplicate_page_load_text_domain')); |
| 44 | add_action('wp_ajax_mk_dp_close_dp_help', array($this, 'mk_dp_close_dp_help')); |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * Localization - 19-dec-2016 |
| 49 | */ |
| 50 | public function duplicate_page_load_text_domain() |
| 51 | { |
| 52 | load_plugin_textdomain('duplicate-page', false, DUPLICATE_PAGE_PLUGIN_DIRNAME.'/languages'); |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * Activation Hook |
| 57 | */ |
| 58 | public function duplicate_page_install() |
| 59 | { |
| 60 | $defaultsettings = array( |
| 61 | 'duplicate_post_status' => 'draft', |
| 62 | 'duplicate_post_redirect' => 'to_list', |
| 63 | 'duplicate_post_suffix' => '', |
| 64 | 'duplicate_post_editor' => 'classic', |
| 65 | ); |
| 66 | $opt = get_option('duplicate_page_options'); |
| 67 | if (!isset($opt['duplicate_post_status'])) { |
| 68 | update_option('duplicate_page_options', $defaultsettings); |
| 69 | } |
| 70 | } |
| 71 | /* |
| 72 | Action Links |
| 73 | */ |
| 74 | public function duplicate_page_plugin_action_links($links, $file) |
| 75 | { |
| 76 | if ($file == plugin_basename(__FILE__)) { |
| 77 | $duplicate_page_links = '<a href="'.esc_url(get_admin_url()).'options-general.php?page=duplicate_page_settings">'.__('Settings', 'duplicate-page').'</a>'; |
| 78 | $duplicate_page_donate = '<a href="https://www.webdesi9.com/donate/?plugin=duplicate-page" title="'.__('Donate Now','duplicate-page').'" target="_blank" style="font-weight:bold">'.__('Donate', 'duplicate-page').'</a>'; |
| 79 | array_unshift($links, $duplicate_page_donate); |
| 80 | array_unshift($links, $duplicate_page_links); |
| 81 | } |
| 82 | |
| 83 | return $links; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Admin Menu |
| 88 | */ |
| 89 | public function duplicate_page_options_page() |
| 90 | { |
| 91 | add_options_page(__('Duplicate Page', 'duplicate-page'), __('Duplicate Page', 'duplicate-page'), 'manage_options', 'duplicate_page_settings', array(&$this, 'duplicate_page_settings')); |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Duplicate Page Admin Settings |
| 96 | */ |
| 97 | public function duplicate_page_settings() |
| 98 | { |
| 99 | if (current_user_can('manage_options')) { |
| 100 | include 'inc/admin-settings.php'; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Main function |
| 106 | */ |
| 107 | public function dt_duplicate_post_as_draft() |
| 108 | { |
| 109 | /* |
| 110 | * get Nonce value |
| 111 | */ |
| 112 | $nonce = sanitize_text_field($_REQUEST['nonce']); |
| 113 | /* |
| 114 | * get the original post id |
| 115 | */ |
| 116 | |
| 117 | $post_id = (isset($_GET['post']) ? intval($_GET['post']) : intval($_POST['post'])); |
| 118 | $post = get_post($post_id); |
| 119 | $current_user_id = get_current_user_id(); |
| 120 | if(wp_verify_nonce( $nonce, 'dt-duplicate-page-'.$post_id)) { |
| 121 | if (current_user_can('manage_options') || current_user_can('edit_others_posts')) { |
| 122 | $this->duplicate_edit_post($post_id); |
| 123 | } |
| 124 | else if (current_user_can('contributor') && $current_user_id == $post->post_author){ |
| 125 | $this->duplicate_edit_post($post_id, 'pending'); |
| 126 | } |
| 127 | else if (current_user_can('edit_posts') && $current_user_id == $post->post_author ){ |
| 128 | $this->duplicate_edit_post($post_id); |
| 129 | } |
| 130 | else { |
| 131 | wp_die(__('Unauthorized Access.','duplicate-page')); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | else { |
| 136 | wp_die(__('Security check issue, Please try again.','duplicate-page')); |
| 137 | } |
| 138 | |
| 139 | } |
| 140 | /** |
| 141 | * Duplicate edit post |
| 142 | */ |
| 143 | public function duplicate_edit_post($post_id,$post_status_update='') |
| 144 | { |
| 145 | global $wpdb; |
| 146 | $opt = get_option('duplicate_page_options'); |
| 147 | $suffix = isset($opt['duplicate_post_suffix']) && !empty($opt['duplicate_post_suffix']) ? ' -- '.esc_attr($opt['duplicate_post_suffix']) : ''; |
| 148 | if($post_status_update == ''){ |
| 149 | $post_status = !empty($opt['duplicate_post_status']) ? esc_attr($opt['duplicate_post_status']) : 'draft'; |
| 150 | }else{ |
| 151 | $post_status = $post_status_update; |
| 152 | } |
| 153 | $redirectit = !empty($opt['duplicate_post_redirect']) ? esc_attr($opt['duplicate_post_redirect']) : 'to_list'; |
| 154 | if (!(isset($_GET['post']) || isset($_POST['post']) || (isset($_REQUEST['action']) && 'dt_duplicate_post_as_draft' == sanitize_text_field($_REQUEST['action'])))) { |
| 155 | wp_die(__('No post to duplicate has been supplied!','duplicate-page')); |
| 156 | } |
| 157 | $returnpage = ''; |
| 158 | /* |
| 159 | * and all the original post data then |
| 160 | */ |
| 161 | $post = get_post($post_id); |
| 162 | /* |
| 163 | * if you don't want current user to be the new post author, |
| 164 | * then change next couple of lines to this: $new_post_author = $post->post_author; |
| 165 | */ |
| 166 | $current_user = wp_get_current_user(); |
| 167 | $new_post_author = $current_user->ID; |
| 168 | /* |
| 169 | * if post data exists, create the post duplicate |
| 170 | */ |
| 171 | if (isset($post) && $post != null) { |
| 172 | /* |
| 173 | * new post data array |
| 174 | */ |
| 175 | $args = array( |
| 176 | 'comment_status' => $post->comment_status, |
| 177 | 'ping_status' => $post->ping_status, |
| 178 | 'post_author' => $new_post_author, |
| 179 | 'post_content' => (isset($opt['duplicate_post_editor']) && $opt['duplicate_post_editor'] == 'gutenberg') ? wp_slash($post->post_content) : $post->post_content, |
| 180 | 'post_excerpt' => $post->post_excerpt, |
| 181 | 'post_parent' => $post->post_parent, |
| 182 | 'post_password' => $post->post_password, |
| 183 | 'post_status' => $post_status, |
| 184 | 'post_title' => $post->post_title.$suffix, |
| 185 | 'post_type' => $post->post_type, |
| 186 | 'to_ping' => $post->to_ping, |
| 187 | 'menu_order' => $post->menu_order, |
| 188 | ); |
| 189 | /* |
| 190 | * insert the post by wp_insert_post() function |
| 191 | */ |
| 192 | $new_post_id = wp_insert_post($args); |
| 193 | if(is_wp_error($new_post_id)){ |
| 194 | wp_die(__($new_post_id->get_error_message(),'duplicate-page')); |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * get all current post terms ad set them to the new post draft |
| 199 | */ |
| 200 | $taxonomies = array_map('sanitize_text_field',get_object_taxonomies($post->post_type)); |
| 201 | if (!empty($taxonomies) && is_array($taxonomies)): |
| 202 | foreach ($taxonomies as $taxonomy) { |
| 203 | $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs')); |
| 204 | wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false); |
| 205 | } |
| 206 | endif; |
| 207 | /* |
| 208 | * duplicate all post meta |
| 209 | */ |
| 210 | $post_meta = get_post_meta( $post_id ); |
| 211 | if ( ! empty( $post_meta ) ) { |
| 212 | foreach ( $post_meta as $key => $item ) { |
| 213 | $meta_value = current( $item ); |
| 214 | $update = $wpdb->update( |
| 215 | "{$wpdb->prefix}postmeta", |
| 216 | [ 'meta_value' => $meta_value ], |
| 217 | [ 'post_id' => $new_post_id, 'meta_key' => $key, ] |
| 218 | ); |
| 219 | if(!$update || $update < 1){ |
| 220 | $wpdb->insert( "{$wpdb->prefix}postmeta", |
| 221 | [ 'meta_key' => $key, 'meta_value' => $meta_value, 'post_id' => $new_post_id ] |
| 222 | ); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if(is_plugin_active( 'elementor/elementor.php' )){ |
| 228 | $css = Elementor\Core\Files\CSS\Post::create( $new_post_id ); |
| 229 | $css->update(); |
| 230 | } |
| 231 | /* |
| 232 | * finally, redirecting to your choice |
| 233 | */ |
| 234 | if ($post->post_type != 'post'){ |
| 235 | $returnpage = '?post_type='.$post->post_type; |
| 236 | } |
| 237 | if (!empty($redirectit) && $redirectit == 'to_list'){ |
| 238 | wp_redirect(esc_url_raw(admin_url('edit.php'.$returnpage))); |
| 239 | } elseif (!empty($redirectit) && $redirectit == 'to_page'){ |
| 240 | wp_redirect(esc_url_raw(admin_url('post.php?action=edit&post='.$new_post_id))); |
| 241 | } else { |
| 242 | wp_redirect(esc_url_raw(admin_url('edit.php'.$returnpage))); |
| 243 | } |
| 244 | exit; |
| 245 | } |
| 246 | else { |
| 247 | wp_die(__('Error! Post creation failed, could not find original post: ','duplicate-page').$post_id); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * Add the duplicate link to action list for post_row_actions |
| 253 | */ |
| 254 | public function dt_duplicate_post_link($actions, $post) |
| 255 | { |
| 256 | $opt = get_option('duplicate_page_options'); |
| 257 | $post_status = !empty($opt['duplicate_post_status']) ? esc_attr($opt['duplicate_post_status']) : 'draft'; |
| 258 | if (current_user_can('edit_posts')) { |
| 259 | $actions['duplicate'] = isset($post) ? '<a href="admin.php?action=dt_duplicate_post_as_draft&post='.intval($post->ID).'&nonce='.wp_create_nonce( 'dt-duplicate-page-'.intval($post->ID) ).'" title="'.__('Duplicate this as ', 'duplicate-page').$post_status.'" rel="permalink">'.__('Duplicate This', 'duplicate-page').'</a>' : ''; |
| 260 | } |
| 261 | |
| 262 | return $actions; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Add the duplicate link to edit screen - classic editor |
| 267 | */ |
| 268 | public function duplicate_page_custom_button_classic() |
| 269 | { |
| 270 | global $post; |
| 271 | $opt = get_option('duplicate_page_options'); |
| 272 | $post_status = !empty($opt['duplicate_post_status']) ? esc_attr($opt['duplicate_post_status']) : 'draft'; |
| 273 | $html = '<div id="major-publishing-actions">'; |
| 274 | $html .= '<div id="export-action">'; |
| 275 | $html .= isset($post) ? '<a href="admin.php?action=dt_duplicate_post_as_draft&post='.intval($post->ID).'&nonce='.wp_create_nonce( 'dt-duplicate-page-'.$post->ID ).'" title="'.__('Duplicate this as ','duplicate-page').$post_status.'" rel="permalink">'.__('Duplicate This', 'duplicate-page').'</a>' :''; |
| 276 | $html .= '</div>'; |
| 277 | $html .= '</div>'; |
| 278 | $content = apply_filters('wpautop', $html); |
| 279 | $content = str_replace(']]>', ']]>', $content); |
| 280 | echo $content; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Add the duplicate link to edit screen - gutenberg |
| 285 | */ |
| 286 | public function duplicate_page_custom_button_guten() |
| 287 | { |
| 288 | global $post; |
| 289 | if ($post) { |
| 290 | $opt = get_option('duplicate_page_options'); |
| 291 | $post_status = !empty($opt['duplicate_post_status']) ? esc_attr($opt['duplicate_post_status']) : 'draft'; |
| 292 | if (isset($opt['duplicate_post_editor']) && ($opt['duplicate_post_editor'] == 'gutenberg' || $opt['duplicate_post_editor'] == 'all')) { |
| 293 | wp_enqueue_style('dp-main-style', plugin_dir_url(__FILE__) . 'css/dp_gutenberg.css'); |
| 294 | wp_register_script( "dt_duplicate_post_script", plugins_url( '/js/editor-script.js', __FILE__ ), array( 'wp-edit-post', 'wp-plugins', 'wp-i18n', 'wp-element' ), DUPLICATE_PAGE_PLUGIN_VERSION); |
| 295 | wp_localize_script( 'dt_duplicate_post_script', 'dt_params', array( |
| 296 | 'dp_post_id' => intval($post->ID), |
| 297 | 'dtnonce' => wp_create_nonce( 'dt-duplicate-page-'.intval($post->ID)), |
| 298 | 'dp_post_text' => __("Duplicate This",'duplicate-page'), |
| 299 | 'dp_post_title'=> __('Duplicate this as ','duplicate-page').$post_status, |
| 300 | 'dp_duplicate_link' => "admin.php?action=dt_duplicate_post_as_draft" |
| 301 | ) |
| 302 | ); |
| 303 | wp_enqueue_script( 'dt_duplicate_post_script' ); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * Admin Bar Duplicate This Link |
| 310 | */ |
| 311 | public function duplicate_page_admin_bar_link() |
| 312 | { |
| 313 | global $wp_admin_bar, $post; |
| 314 | $opt = get_option('duplicate_page_options'); |
| 315 | $post_status = !empty($opt['duplicate_post_status']) ? esc_attr($opt['duplicate_post_status']) : 'draft'; |
| 316 | $current_object = get_queried_object(); |
| 317 | if (empty($current_object)) { |
| 318 | return; |
| 319 | } |
| 320 | if (!empty($current_object->post_type) |
| 321 | && ($post_type_object = get_post_type_object($current_object->post_type)) |
| 322 | && ($post_type_object->show_ui || $current_object->post_type == 'attachment')) { |
| 323 | $wp_admin_bar->add_menu(array( |
| 324 | 'parent' => 'edit', |
| 325 | 'id' => 'duplicate_this', |
| 326 | 'title' => __('Duplicate This as ', 'duplicate-page').$post_status, |
| 327 | 'href' => isset($post) ? esc_url_raw(admin_url().'admin.php?action=dt_duplicate_post_as_draft&post='.intval($post->ID).'&nonce='.wp_create_nonce( 'dt-duplicate-page-'.intval($post->ID))) :'', |
| 328 | )); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Redirect function |
| 334 | */ |
| 335 | public static function dp_redirect($url) |
| 336 | { |
| 337 | $web_url = esc_url_raw($url); |
| 338 | wp_register_script( 'dp-setting-redirect', ''); |
| 339 | wp_enqueue_script( 'dp-setting-redirect' ); |
| 340 | wp_add_inline_script( |
| 341 | 'dp-setting-redirect', |
| 342 | ' window.location.href="'.$web_url.'" ;' |
| 343 | ); |
| 344 | } |
| 345 | /* |
| 346 | Load Help Desk |
| 347 | */ |
| 348 | public function load_help_desk() { |
| 349 | $mkcontent = ''; |
| 350 | $mkcontent .= '<div class="dpmrs" style="display:none;">'; |
| 351 | $mkcontent .= '<div class="l_dpmrs">'; |
| 352 | $mkcontent .= ''; |
| 353 | $mkcontent .= '</div>'; |
| 354 | $mkcontent .= '<div class="r_dpmrs">'; |
| 355 | $mkcontent .= '<a class="close_dp_help fm_close_btn" href="javascript:void(0)" data-ct="rate_later" title="'.__('close','duplicate-page').'">X</a><strong>'.__('Duplicate Page','duplicate-page').'</strong><p>'.__('We love and care about you. Our team is putting maximum efforts to provide you the best functionalities. It would be highly appreciable if you could spend a couple of seconds to give a Nice Review to the plugin to appreciate our efforts. So we can work hard to provide new features regularly :)','duplicate-page').'</p><a class="close_dp_help fm_close_btn_1" href="javascript:void(0)" data-ct="rate_later" title="'.__('Remind me later','duplicate-page').'">'.__('Later','duplicate-page').'</a> <a class="close_dp_help fm_close_btn_2" href="https://wordpress.org/support/plugin/duplicate-page/reviews/?filter=5" data-ct="rate_now" title="'.__('Rate us now','duplicate-page').'" target="_blank">'.__('Rate Us','duplicate-page').'</a> <a class="close_dp_help fm_close_btn_3" href="javascript:void(0)" data-ct="rate_never" title="'.__('Not interested','duplicate-page').'">'.__('Never','duplicate-page').'</a>'; |
| 356 | $mkcontent .= '</div></div>'; |
| 357 | if (false === ($mk_dp_close_dp_help_c = get_option('mk_fm_close_fm_help_c'))) { |
| 358 | echo apply_filters('the_content', $mkcontent); |
| 359 | } |
| 360 | } |
| 361 | /* |
| 362 | Close Help |
| 363 | */ |
| 364 | public function mk_dp_close_dp_help() { |
| 365 | $nonce = sanitize_text_field($_REQUEST['nonce']); |
| 366 | if (wp_verify_nonce($nonce, 'nc_help_desk')) { |
| 367 | if (false === ($mk_fm_close_fm_help_c = get_option('mk_fm_close_fm_help_c'))) { |
| 368 | $set = update_option('mk_fm_close_fm_help_c', 'done'); |
| 369 | if ($set) { |
| 370 | echo 'ok'; |
| 371 | } else { |
| 372 | echo 'oh'; |
| 373 | } |
| 374 | } else { |
| 375 | echo 'ac'; |
| 376 | } |
| 377 | }else { |
| 378 | echo 'ac'; |
| 379 | } |
| 380 | die; |
| 381 | } |
| 382 | /* |
| 383 | Custom Assets |
| 384 | */ |
| 385 | public function custom_assets() { |
| 386 | wp_enqueue_style( 'duplicate-page', plugins_url( '/css/duplicate_page.css', __FILE__ ) ); |
| 387 | wp_enqueue_script( 'duplicate-page', plugins_url( '/js/duplicate_page.js', __FILE__ ) ); |
| 388 | wp_localize_script( 'duplicate-page', 'dt_params', array( |
| 389 | 'ajax_url' => admin_url( 'admin-ajax.php'), |
| 390 | 'nonce' => wp_create_nonce( 'nc_help_desk' )) |
| 391 | ); |
| 392 | } |
| 393 | } |
| 394 | new duplicate_page(); |
| 395 | endif; |
| 396 |