| Server IP : 217.160.0.223 / Your IP : 216.73.216.141 Web Server : Apache System : Linux www 6.18.38-i1-ampere+ #1089 SMP Tue Jul 7 14:54:42 CEST 2026 aarch64 User : sws1073902667 ( 1073902667) PHP Version : 8.2.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /home/www/public/wp-content/plugins/duplicate-it/ |
Upload File : |
<?php
/*
Plugin Name: Duplicate It
Plugin URI: https://wordpress.org/plugins/duplicate-it
description: This plugin quickly creates a clone of page or post or any custom Post
Version: 2.5
Author: Smartzminds
Author URI: https://profiles.wordpress.org/shubhamgrover7256
License: GPL2
*/
// Enqueue CSS file
function duplicate_it_enqueue_scripts() {
wp_enqueue_style( 'post-duplicate', plugins_url( 'css/duplicate-it.css', __FILE__ ) );
}
add_action( 'admin_enqueue_scripts', 'duplicate_it_enqueue_scripts' );
/*
* Function creates a post duplicate as a draft and redirects to the edit post screen
*/
function duplicate_it_post_as_draft() {
global $wpdb;
// Check if post ID is provided
if ( ! ( isset( $_GET['post'] ) || isset( $_POST['post'] ) || ( isset( $_REQUEST['action'] ) && 'rd_duplicate_post_as_draft' == $_REQUEST['action'] ) ) ) {
wp_die( 'No post to duplicate has been supplied!' );
}
// Nonce verification
if ( ! isset( $_GET['duplicate_nonce'] ) || ! wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) ) {
return;
}
$post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : absint( $_POST['post'] );
// Get the original post data
$post = get_post( $post_id );
// Get the current user
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;
// Get the prefix and suffix for the duplicated post title
$prefix = get_option( 'post_duplicate_it_title_prefix' );
$suffix = get_option( 'post_duplicate_it_title_suffix' );
$title = $prefix . $post->post_title . $suffix;
// Get the post status for the duplicated post
$status = get_option( 'post_duplicate_it_status' );
// Create the new post duplicate
if ( isset( $post ) && null !== $post ) {
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => $status,
'post_title' => $title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order,
);
// Insert the new post
$new_post_id = wp_insert_post( $args );
$auto_detect_enabled = get_option('duplicate_it_auto_detect_editor');
if ($auto_detect_enabled) {
$is_elementor = get_post_meta($post_id, '_elementor_data', true);
$has_blocks = function_exists('has_blocks') ? has_blocks($post_id) : false;
if (!empty($is_elementor)) {
update_post_meta($new_post_id, '_elementor_edit_mode', 'builder');
update_post_meta($new_post_id, '_elementor_template_type', 'wp-page');
update_post_meta($new_post_id, '_wp_page_template', 'elementor_canvas');
delete_post_meta($new_post_id, '_editor_preference');
} elseif ($has_blocks) {
delete_post_meta($new_post_id, '_editor_preference');
delete_post_meta($new_post_id, '_elementor_data');
delete_post_meta($new_post_id, '_elementor_edit_mode');
delete_post_meta($new_post_id, '_elementor_template_type');
} else {
update_post_meta($new_post_id, '_editor_preference', 'classic');
delete_post_meta($new_post_id, '_elementor_data');
delete_post_meta($new_post_id, '_elementor_edit_mode');
delete_post_meta($new_post_id, '_elementor_template_type');
}
}
/* =========================
AUTO DETECT ACTIVE BUILDER
========================= */
// Elementor detect
$is_elementor = get_post_meta($post_id, '_elementor_data', true);
// Gutenberg detect
$has_blocks = function_exists('has_blocks') ? has_blocks($post_id) : false;
if ( ! empty($is_elementor) ) {
// ✅ ELEMENTOR
update_post_meta($new_post_id, '_elementor_edit_mode', 'builder');
update_post_meta($new_post_id, '_elementor_template_type', 'wp-page');
update_post_meta($new_post_id, '_wp_page_template', 'elementor_canvas');
delete_post_meta($new_post_id, '_editor_preference');
} elseif ( $has_blocks ) {
// ✅ GUTENBERG
delete_post_meta($new_post_id, '_editor_preference');
delete_post_meta($new_post_id, '_elementor_data');
delete_post_meta($new_post_id, '_elementor_edit_mode');
delete_post_meta($new_post_id, '_elementor_template_type');
} else {
// ✅ CLASSIC EDITOR
update_post_meta($new_post_id, '_editor_preference', 'classic');
delete_post_meta($new_post_id, '_elementor_data');
delete_post_meta($new_post_id, '_elementor_edit_mode');
delete_post_meta($new_post_id, '_elementor_template_type');
}
if ( ! empty($is_elementor) && class_exists('Elementor\Core\Files\CSS\Post') ) {
$css = Elementor\Core\Files\CSS\Post::create($new_post_id);
$css->update();
}
/* =========================
AUTO DETECT & APPLY BUILDER
========================= */
// Elementor detect
$is_elementor = get_post_meta($post_id, '_elementor_data', true);
// Gutenberg detect
$has_blocks = function_exists('has_blocks') ? has_blocks($post_id) : false;
if ( ! empty($is_elementor) ) {
// ✅ ELEMENTOR
update_post_meta($new_post_id, '_elementor_edit_mode', 'builder');
update_post_meta($new_post_id, '_elementor_template_type', 'wp-page');
update_post_meta($new_post_id, '_wp_page_template', 'elementor_canvas');
delete_post_meta($new_post_id, '_editor_preference');
} elseif ( $has_blocks ) {
// ✅ GUTENBERG
delete_post_meta($new_post_id, '_editor_preference');
delete_post_meta($new_post_id, '_elementor_data');
delete_post_meta($new_post_id, '_elementor_edit_mode');
delete_post_meta($new_post_id, '_elementor_template_type');
} else {
// ✅ CLASSIC EDITOR
update_post_meta($new_post_id, '_editor_preference', 'classic');
delete_post_meta($new_post_id, '_elementor_data');
delete_post_meta($new_post_id, '_elementor_edit_mode');
delete_post_meta($new_post_id, '_elementor_template_type');
}
/* =========================
AUTO DETECT & APPLY BUILDER
========================= */
// Check if original post is Elementor
$is_elementor = get_post_meta($post_id, '_elementor_data', true);
// Check if Gutenberg (block editor)
$has_blocks = function_exists('has_blocks') ? has_blocks($post_id) : false;
if ( ! empty($is_elementor) ) {
// ✅ ELEMENTOR
update_post_meta($new_post_id, '_elementor_edit_mode', 'builder');
update_post_meta($new_post_id, '_elementor_template_type', 'wp-page');
update_post_meta($new_post_id, '_wp_page_template', 'elementor_canvas');
// Remove Classic preference
delete_post_meta($new_post_id, '_editor_preference');
} elseif ( $has_blocks ) {
// ✅ GUTENBERG
delete_post_meta($new_post_id, '_editor_preference');
// Remove Elementor data
delete_post_meta($new_post_id, '_elementor_data');
delete_post_meta($new_post_id, '_elementor_edit_mode');
delete_post_meta($new_post_id, '_elementor_template_type');
} else {
// ✅ CLASSIC EDITOR
update_post_meta($new_post_id, '_editor_preference', 'classic');
// Remove Elementor data
delete_post_meta($new_post_id, '_elementor_data');
delete_post_meta($new_post_id, '_elementor_edit_mode');
delete_post_meta($new_post_id, '_elementor_template_type');
}
/* =========================
APPLY EDITOR SETTING
========================= */
/* ========================= */
// "All Editors" ke liye kuch nahi karna
// Get and set the post terms
$excluded_taxonomies = get_option('post_duplicate_it_excluded_taxonomies', []);
if (!is_array($excluded_taxonomies)) {
$excluded_taxonomies = [];
}
$taxonomies = get_object_taxonomies($post->post_type);
foreach ($taxonomies as $taxonomy) {
// Skip selected taxonomies
if (in_array($taxonomy, $excluded_taxonomies)) {
continue;
}
$post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
if (!is_wp_error($post_terms)) {
wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
}
}
// Duplicate post meta
$post_meta_infos = $wpdb->get_results( "SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id = $post_id" );
if ( count( $post_meta_infos ) !== 0 ) {
foreach ( $post_meta_infos as $meta_info ) {
$meta_key = $meta_info->meta_key;
if ( $meta_key === '_wp_old_slug' ) {
continue;
}
$meta_value = addslashes( $meta_info->meta_value );
add_post_meta( $new_post_id, $meta_key, $meta_value );
}
}
// Finally, redirect to the edit post screen for the new draft
wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
exit;
} else {
wp_die( 'Post creation failed, could not find original post: ' . $post_id );
}
}
add_action( 'admin_action_rd_duplicate_post_as_draft', 'duplicate_it_post_as_draft' );
/***** Plugin setting option *****/
require_once( dirname( __FILE__ ) . '/duplicate-post-setting.php' );
/********* Plugin setting link *********/
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'add_action_links' );
function add_action_links( $links ) {
$mylinks = array(
'<a href="' . admin_url( 'options-general.php?page=duplicate_it_settings' ) . '">Settings</a>',
);
return array_merge( $links, $mylinks );
}
/****** Bulk duplicate ********/
// Add "Duplicate" to bulk actions dropdown
function duplicate_bulk_actions( $bulk_actions ) {
$bulk_actions['duplicate'] = __( 'Duplicate' );
return $bulk_actions;
}
add_filter( 'bulk_actions-edit-post', 'duplicate_bulk_actions' );
add_filter( 'bulk_actions-edit-page', 'duplicate_bulk_actions' );
// Handle the "Duplicate" action
function handle_duplicate_bulk_action() {
// Check if the action is set to "duplicate"
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] === 'duplicate' ) {
$post_ids = isset( $_REQUEST['post'] ) ? $_REQUEST['post'] : array();
// Loop through each selected post and duplicate it
foreach ( $post_ids as $post_id ) {
$new_post_id = duplicate_post( $post_id ); // Function to duplicate the post
}
// Redirect to the post list page after duplication
$redirect_to = add_query_arg( array(
'post_type' => get_post_type( $post_id ),
'duplicated' => count( $post_ids ),
), 'edit.php' );
wp_redirect( $redirect_to );
exit;
}
}
add_action( 'admin_action_duplicate', 'handle_duplicate_bulk_action' );
// Duplicate a post or page
function duplicate_post( $post_id ) {
$post = get_post( $post_id );
if ( ! $post ) {
return new WP_Error( 'duplicate_post_error', 'Invalid post ID.' );
}
$args = array(
'post_title' => get_option( 'post_duplicate_it_title_prefix', '' ) . $post->post_title . get_option( 'post_duplicate_it_title_suffix', '' ),
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_status' => 'draft',
'post_type' => $post->post_type,
'post_author' => get_current_user_id(),
'post_parent' => $post->post_parent,
'menu_order' => $post->menu_order,
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_password' => $post->post_password,
'post_name' => $post->post_name . '-duplicate',
);
$new_post_id = wp_insert_post( $args );
if ( is_wp_error( $new_post_id ) ) {
return $new_post_id;
}
/* =========================
COPY POST META
========================== */
$meta_data = get_post_meta( $post_id );
foreach ( $meta_data as $meta_key => $meta_values ) {
foreach ( $meta_values as $meta_value ) {
add_post_meta( $new_post_id, $meta_key, maybe_unserialize( $meta_value ) );
}
}
/* =========================
COPY TAXONOMIES (EXCEPT SELECTED)
========================== */
$excluded_taxonomies = get_option( 'post_duplicate_it_excluded_taxonomies', [] );
if ( ! is_array( $excluded_taxonomies ) ) {
$excluded_taxonomies = [];
}
$taxonomies = get_object_taxonomies( $post->post_type );
foreach ( $taxonomies as $taxonomy ) {
// Skip excluded taxonomies
if ( in_array( $taxonomy, $excluded_taxonomies ) ) {
continue;
}
$terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'ids' ) );
if ( ! is_wp_error( $terms ) ) {
wp_set_object_terms( $new_post_id, $terms, $taxonomy );
}
}
/* =========================
ELEMENTOR META
========================== */
$elementor_meta_keys = array(
'_elementor_data',
'_elementor_edit_mode',
'_elementor_template_type',
'_elementor_version'
);
foreach ( $elementor_meta_keys as $meta_key ) {
$meta_value = get_post_meta( $post_id, $meta_key, true );
if ( $meta_value ) {
update_post_meta( $new_post_id, $meta_key, $meta_value );
}
}
/* =========================
REGENERATE ELEMENTOR CSS
========================== */
if ( is_plugin_active( 'elementor/elementor.php' ) ) {
if ( class_exists( 'Elementor\Core\Files\CSS\Post' ) ) {
$css = Elementor\Core\Files\CSS\Post::create( $new_post_id );
$css->update();
}
if ( function_exists( 'Elementor\Plugin' ) ) {
\Elementor\Plugin::$instance->db->reset_post( $new_post_id );
}
}
return $new_post_id;
}
// add counter and thier functionality showing alert messages
function post_duplicate_link_pages( $actions, $post ) {
if (current_user_can('edit_posts')) {
$increment_value = get_post_meta( $post->ID, 'duplicate_counter', true ); // Get the current value of the counter
$increment_value = $increment_value ? $increment_value : 0; // If the counter value is not set, initialize it to 0
$actions['duplicate'] = '<div class="duplicate-counter">
<span class="counter-value">' . $increment_value . '</span>
<button class="counter-button increment">+</button>
<button class="counter-button decrement">-</button>
<a href="#" class="duplicate-link" data-post-id="' . $post->ID . '">Duplicate</a>
</div>';
}
return $actions;
}
add_filter( 'page_row_actions', 'post_duplicate_link_pages', 10, 2 );
function post_duplicate_link_new( $actions, $post ) {
if (current_user_can('edit_posts')) {
$increment_value = get_post_meta( $post->ID, 'duplicate_counter', true ); // Get the current value of the counter
$increment_value = $increment_value ? $increment_value : 0; // If the counter value is not set, initialize it to 0
$actions['duplicate'] = '<div class="duplicate-counter">
<span class="counter-value">' . $increment_value . '</span>
<button class="counter-button increment">+</button>
<button class="counter-button decrement">-</button>
<a href="#" class="duplicate-link" data-post-id="' . $post->ID . '">Duplicate</a>
</div>';
}
return $actions;
}
add_filter( 'post_row_actions', 'post_duplicate_link_new', 10, 2 );
function duplicate_post_as_draft() {
if ( isset( $_GET['post'] ) ) {
$post_id = absint( $_GET['post'] );
if ( current_user_can( 'edit_posts' ) ) {
$post = get_post( $post_id );
$new_post_author = $post->post_author;
$current_post_type = $post->post_type;
$new_post_status = 'draft';
$increment_value = isset( $_GET['counter'] ) ? absint( $_GET['counter'] ) : 0; // Get the counter value from the URL parameter
$prefix = get_option( 'post_duplicate_it_title_prefix' );
$suffix = get_option( 'post_duplicate_it_title_suffix' );
for ( $i = 0; $i < $increment_value; $i++ ) {
$duplicated_title = $prefix . $post->post_title . ($i + 1) . $suffix; // Modify the title of the duplicated post as per your requirements
$args = array(
'post_content' => $post->post_content,
'post_name' => $post->post_name,
'post_title' => $duplicated_title,
'post_status' => $new_post_status,
'post_type' => $current_post_type,
'post_author' => $new_post_author,
);
$new_post_id = wp_insert_post( $args ); // Create the duplicated post
// Save the increment value as post meta for the new duplicated post
update_post_meta( $new_post_id, 'duplicate_counter', 1 );
// Check if the original page is created with Elementor
if ( get_post_meta( $post_id, '_elementor_data', true ) ) {
// Set Elementor full-width template (Elementor Canvas)
update_post_meta( $new_post_id, '_wp_page_template', 'elementor_canvas' );
// Mark the post for Elementor edit mode
update_post_meta( $new_post_id, '_elementor_edit_mode', 'builder' );
// Mark the post as an Elementor page
update_post_meta( $new_post_id, '_elementor_template_type', 'elementor' );
} else {
// If not created with Elementor, don't set Elementor-specific data
update_post_meta( $new_post_id, '_wp_page_template', 'default' );
}
}
}
// Redirect to the post list screen
wp_safe_redirect( admin_url( 'edit.php?post_type=' . get_post_type( $post_id ) ) );
exit;
}
}
add_action( 'admin_action_rd_duplicate_post_as_draft', 'duplicate_post_as_draft' );
// enqueue the js file
function Zumper_widget_enqueue_script()
{
wp_enqueue_script( 'post-duplicate', plugin_dir_url( __FILE__ ) . 'js/duplicate-it.js' );
}
add_action('admin_enqueue_scripts', 'Zumper_widget_enqueue_script');
add_action('admin_head', 'duplicate_counter_admin_head');
function duplicate_counter_admin_head() {
echo '<script>var adminURL = "' . admin_url() . '";</script>';
}
// hooks for activate and deactivate the plugin
register_activation_hook(__FILE__, 'duplicate_it_activate');
add_action('admin_init', 'duplucate_it_redirect');
function duplicate_it_activate() {
add_option('duplicate_do_activation_redirect', true);
}
function duplucate_it_redirect() {
if (get_option('duplicate_do_activation_redirect', false)) {
delete_option('duplicate_do_activation_redirect');
exit( wp_redirect( admin_url( 'options-general.php?page=duplicate_it_settings' ) ) );
}
}
// Add "Rate this plugin" link with 5-star rating
function duplicate_it_plugin_action_links($links) {
$plugin_links = array(
'<a class="rating-review" href="https://wordpress.org/support/plugin/duplicate-it/reviews/" target="_blank">Rate and Review <span class="rating-stars">★★★★★</span></a>',
);
return array_merge($plugin_links, $links);
}
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'duplicate_it_plugin_action_links');
// add notification on welcome screen
function duplicate_it_show_rating_notification() {
if (get_option('duplicate_it_plugin_rated')) {
return;
}
ob_start();
?>
<div class="notice notice-info is-dismissible">
<p><?php _e('Enjoying Duplicate It? Please consider leaving a 5-star rating!', 'duplicate-it'); ?></p>
<p>
<a href="https://wordpress.org/support/plugin/duplicate-it/reviews/?filter=5/#new-post" target="_blank" class="button button-primary">
<?php _e('Rate this plugin ★★★★★', 'duplicate-it'); ?>
</a>
<a href="?duplicate_it_dismiss_rating=1" class="button button-secondary">
<?php _e('Dismiss', 'duplicate-it'); ?>
</a>
</p>
</div>
<?php
$notification_html = ob_get_clean();
echo $notification_html;
}
// Dismiss the rating notification
if (isset($_GET['duplicate_it_dismiss_rating'])) {
update_option('duplicate_it_plugin_rated', 1);
}
// Hook the function to welcome_panel
// add_action('welcome_panel', 'duplicate_it_show_rating_notification');
function duplicate_it_plugin_settings_page_notification() {
duplicate_it_show_rating_notification();
}
// Hook the function to plugin settings page
add_action('admin_notices', 'duplicate_it_plugin_settings_page_notification');
// Hide the "Edit with Elementor" button for non-Elementor pages
function hide_edit_with_elementor_button( $actions, $post ) {
// Check if the page was created with Elementor
if ( ! get_post_meta( $post->ID, '_elementor_data', true ) ) {
// Remove the "Edit with Elementor" link if not built with Elementor
if ( isset( $actions['edit_with_elementor'] ) ) {
unset( $actions['edit_with_elementor'] );
}
}
return $actions;
}
add_filter( 'page_row_actions', 'hide_edit_with_elementor_button', 10, 2 );
?>