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
386 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.4.9 |
| 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.4.9'); |
| 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 | |
| 131 | else { |
| 132 | wp_die(__('Unauthorized Access.','duplicate-page')); |
| 133 | |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | else { |
| 138 | wp_die(__('Security check issue, Please try again.','duplicate-page')); |
| 139 | |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | /** |
| 144 | * Duplicate edit post |
| 145 | */ |
| 146 | public function duplicate_edit_post($post_id,$post_status_update=''){ |
| 147 | global $wpdb; |
| 148 | $opt = get_option('duplicate_page_options'); |
| 149 | $suffix = isset($opt['duplicate_post_suffix']) && !empty($opt['duplicate_post_suffix']) ? ' -- '.esc_attr($opt['duplicate_post_suffix']) : ''; |
| 150 | if($post_status_update == ''){ |
| 151 | $post_status = !empty($opt['duplicate_post_status']) ? esc_attr($opt['duplicate_post_status']) : 'draft'; |
| 152 | }else{ |
| 153 | $post_status = $post_status_update; |
| 154 | } |
| 155 | $redirectit = !empty($opt['duplicate_post_redirect']) ? esc_attr($opt['duplicate_post_redirect']) : 'to_list'; |
| 156 | if (!(isset($_GET['post']) || isset($_POST['post']) || (isset($_REQUEST['action']) && 'dt_duplicate_post_as_draft' == sanitize_text_field($_REQUEST['action'])))) { |
| 157 | wp_die(__('No post to duplicate has been supplied!','duplicate-page')); |
| 158 | } |
| 159 | $returnpage = ''; |
| 160 | /* |
| 161 | * and all the original post data then |
| 162 | */ |
| 163 | $post = get_post($post_id); |
| 164 | /* |
| 165 | * if you don't want current user to be the new post author, |
| 166 | * then change next couple of lines to this: $new_post_author = $post->post_author; |
| 167 | */ |
| 168 | $current_user = wp_get_current_user(); |
| 169 | $new_post_author = $current_user->ID; |
| 170 | /* |
| 171 | * if post data exists, create the post duplicate |
| 172 | */ |
| 173 | if (isset($post) && $post != null) { |
| 174 | /* |
| 175 | * new post data array |
| 176 | */ |
| 177 | $args = array( |
| 178 | 'comment_status' => $post->comment_status, |
| 179 | 'ping_status' => $post->ping_status, |
| 180 | 'post_author' => $new_post_author, |
| 181 | 'post_content' => (isset($opt['duplicate_post_editor']) && $opt['duplicate_post_editor'] == 'gutenberg') ? wp_slash($post->post_content) : $post->post_content, |
| 182 | 'post_excerpt' => $post->post_excerpt, |
| 183 | 'post_parent' => $post->post_parent, |
| 184 | 'post_password' => $post->post_password, |
| 185 | 'post_status' => $post_status, |
| 186 | 'post_title' => $post->post_title.$suffix, |
| 187 | 'post_type' => $post->post_type, |
| 188 | 'to_ping' => $post->to_ping, |
| 189 | 'menu_order' => $post->menu_order, |
| 190 | ); |
| 191 | /* |
| 192 | * insert the post by wp_insert_post() function |
| 193 | */ |
| 194 | $new_post_id = wp_insert_post($args); |
| 195 | /* |
| 196 | * get all current post terms ad set them to the new post draft |
| 197 | */ |
| 198 | $taxonomies = array_map('sanitize_text_field',get_object_taxonomies($post->post_type)); |
| 199 | if (!empty($taxonomies) && is_array($taxonomies)): |
| 200 | foreach ($taxonomies as $taxonomy) { |
| 201 | $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs')); |
| 202 | wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false); |
| 203 | } |
| 204 | endif; |
| 205 | /* |
| 206 | * duplicate all post meta |
| 207 | */ |
| 208 | $post_meta_infos = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=%d",$post_id)); |
| 209 | if (count($post_meta_infos)!=0) { |
| 210 | $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) "; |
| 211 | foreach ($post_meta_infos as $meta_info) { |
| 212 | $meta_key = sanitize_text_field($meta_info->meta_key); |
| 213 | $meta_value = addslashes($meta_info->meta_value); |
| 214 | $sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'"; |
| 215 | } |
| 216 | $sql_query.= implode(" UNION ALL ", $sql_query_sel); |
| 217 | $wpdb->query($sql_query); |
| 218 | } |
| 219 | if(is_plugin_active( 'elementor/elementor.php' )){ |
| 220 | $css = Elementor\Core\Files\CSS\Post::create( $new_post_id ); |
| 221 | $css->update(); |
| 222 | } |
| 223 | /* |
| 224 | * finally, redirecting to your choice |
| 225 | */ |
| 226 | if ($post->post_type != 'post'): |
| 227 | $returnpage = '?post_type='.$post->post_type; |
| 228 | endif; |
| 229 | if (!empty($redirectit) && $redirectit == 'to_list'): |
| 230 | wp_redirect(esc_url_raw(admin_url('edit.php'.$returnpage))); elseif (!empty($redirectit) && $redirectit == 'to_page'): |
| 231 | wp_redirect(esc_url_raw(admin_url('post.php?action=edit&post='.$new_post_id))); else: |
| 232 | wp_redirect(esc_url_raw(admin_url('edit.php'.$returnpage))); |
| 233 | endif; |
| 234 | exit; |
| 235 | } |
| 236 | else { |
| 237 | wp_die(__('Error! Post creation failed, could not find original post: ','duplicate-page').$post_id); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Add the duplicate link to action list for post_row_actions |
| 243 | */ |
| 244 | public function dt_duplicate_post_link($actions, $post) |
| 245 | { |
| 246 | $opt = get_option('duplicate_page_options'); |
| 247 | $post_status = !empty($opt['duplicate_post_status']) ? esc_attr($opt['duplicate_post_status']) : 'draft'; |
| 248 | if (current_user_can('edit_posts')) { |
| 249 | $actions['duplicate'] = '<a href="admin.php?action=dt_duplicate_post_as_draft&post='.$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>'; |
| 250 | } |
| 251 | |
| 252 | return $actions; |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Add the duplicate link to edit screen - classic editor |
| 257 | */ |
| 258 | public function duplicate_page_custom_button_classic() |
| 259 | { |
| 260 | global $post; |
| 261 | $opt = get_option('duplicate_page_options'); |
| 262 | $post_status = !empty($opt['duplicate_post_status']) ? esc_attr($opt['duplicate_post_status']) : 'draft'; |
| 263 | $html = '<div id="major-publishing-actions">'; |
| 264 | $html .= '<div id="export-action">'; |
| 265 | $html .= '<a href="admin.php?action=dt_duplicate_post_as_draft&post='.$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>'; |
| 266 | $html .= '</div>'; |
| 267 | $html .= '</div>'; |
| 268 | $content = apply_filters('wpautop', $html); |
| 269 | $content = str_replace(']]>', ']]>', $content); |
| 270 | echo $content; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Add the duplicate link to edit screen - gutenberg |
| 275 | */ |
| 276 | public function duplicate_page_custom_button_guten() |
| 277 | { |
| 278 | global $post; |
| 279 | if ($post) { |
| 280 | $opt = get_option('duplicate_page_options'); |
| 281 | $post_status = !empty($opt['duplicate_post_status']) ? esc_attr($opt['duplicate_post_status']) : 'draft'; |
| 282 | if (isset($opt['duplicate_post_editor']) && ($opt['duplicate_post_editor'] == 'gutenberg' || $opt['duplicate_post_editor'] == 'all')) { |
| 283 | wp_enqueue_style('dp-main-style', plugin_dir_url(__FILE__) . 'css/dp_gutenberg.css'); |
| 284 | 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); |
| 285 | wp_localize_script( 'dt_duplicate_post_script', 'dt_params', array( |
| 286 | 'dp_post_id' => $post->ID, |
| 287 | 'dtnonce' => wp_create_nonce( 'dt-duplicate-page-'.$post->ID ), |
| 288 | 'dp_post_text' => __("Duplicate This",'duplicate-page'), |
| 289 | 'dp_post_title'=> __('Duplicate this as ','duplicate-page').$post_status, |
| 290 | 'dp_duplicate_link' => "admin.php?action=dt_duplicate_post_as_draft" |
| 291 | ) |
| 292 | ); |
| 293 | wp_enqueue_script( 'dt_duplicate_post_script' ); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Admin Bar Duplicate This Link |
| 300 | */ |
| 301 | public function duplicate_page_admin_bar_link() |
| 302 | { |
| 303 | global $wp_admin_bar, $post; |
| 304 | $opt = get_option('duplicate_page_options'); |
| 305 | $post_status = !empty($opt['duplicate_post_status']) ? esc_attr($opt['duplicate_post_status']) : 'draft'; |
| 306 | $current_object = get_queried_object(); |
| 307 | if (empty($current_object)) { |
| 308 | return; |
| 309 | } |
| 310 | if (!empty($current_object->post_type) |
| 311 | && ($post_type_object = get_post_type_object($current_object->post_type)) |
| 312 | && ($post_type_object->show_ui || $current_object->post_type == 'attachment')) { |
| 313 | $wp_admin_bar->add_menu(array( |
| 314 | 'parent' => 'edit', |
| 315 | 'id' => 'duplicate_this', |
| 316 | 'title' => __('Duplicate This as ', 'duplicate-page').$post_status, |
| 317 | 'href' => esc_url_raw(admin_url().'admin.php?action=dt_duplicate_post_as_draft&post='.$post->ID.'&nonce='.wp_create_nonce( 'dt-duplicate-page-'.$post->ID )) |
| 318 | )); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * Redirect function |
| 324 | */ |
| 325 | public static function dp_redirect($url) |
| 326 | { |
| 327 | $web_url = esc_url_raw($url); |
| 328 | wp_register_script( 'dp-setting-redirect', ''); |
| 329 | wp_enqueue_script( 'dp-setting-redirect' ); |
| 330 | wp_add_inline_script( |
| 331 | 'dp-setting-redirect', |
| 332 | ' window.location.href="'.$web_url.'" ;' |
| 333 | ); |
| 334 | } |
| 335 | /* |
| 336 | Load Help Desk |
| 337 | */ |
| 338 | public function load_help_desk() { |
| 339 | $mkcontent = ''; |
| 340 | $mkcontent .= '<div class="dpmrs" style="display:none;">'; |
| 341 | $mkcontent .= '<div class="l_dpmrs">'; |
| 342 | $mkcontent .= ''; |
| 343 | $mkcontent .= '</div>'; |
| 344 | $mkcontent .= '<div class="r_dpmrs">'; |
| 345 | $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>'; |
| 346 | $mkcontent .= '</div></div>'; |
| 347 | if (false === ($mk_dp_close_dp_help_c = get_option('mk_fm_close_fm_help_c'))) { |
| 348 | echo apply_filters('the_content', $mkcontent); |
| 349 | } |
| 350 | } |
| 351 | /* |
| 352 | Close Help |
| 353 | */ |
| 354 | public function mk_dp_close_dp_help() { |
| 355 | $nonce = sanitize_text_field($_REQUEST['nonce']); |
| 356 | if (wp_verify_nonce($nonce, 'nc_help_desk')) { |
| 357 | if (false === ($mk_fm_close_fm_help_c = get_option('mk_fm_close_fm_help_c'))) { |
| 358 | $set = update_option('mk_fm_close_fm_help_c', 'done'); |
| 359 | if ($set) { |
| 360 | echo 'ok'; |
| 361 | } else { |
| 362 | echo 'oh'; |
| 363 | } |
| 364 | } else { |
| 365 | echo 'ac'; |
| 366 | } |
| 367 | }else { |
| 368 | echo 'ac'; |
| 369 | } |
| 370 | die; |
| 371 | } |
| 372 | /* |
| 373 | Custom Assets |
| 374 | */ |
| 375 | public function custom_assets() { |
| 376 | wp_enqueue_style( 'duplicate-page', plugins_url( '/css/duplicate_page.css', __FILE__ ) ); |
| 377 | wp_enqueue_script( 'duplicate-page', plugins_url( '/js/duplicate_page.js', __FILE__ ) ); |
| 378 | wp_localize_script( 'duplicate-page', 'dt_params', array( |
| 379 | 'ajax_url' => admin_url( 'admin-ajax.php'), |
| 380 | 'nonce' => wp_create_nonce( 'nc_help_desk' )) |
| 381 | ); |
| 382 | } |
| 383 | } |
| 384 | new duplicate_page(); |
| 385 | endif; |
| 386 |