/** * Thehe jQuery ajax call to create a new post. * Duplicates all the data including custom meta. * * @since 2.9 */ function m4c_duplicate_post() { // Get access to the database global $wpdb; // Check the nonce check_ajax_referer('m4c_ajax_file_nonce', 'security'); // Get variables $original_id = $_POST['original_id']; // Get the post as an array $duplicate = get_post($original_id, 'ARRAY_A'); $settings = get_mtphr_post_duplicator_settings(); // Modify some of the elements $duplicate['post_title'] = $duplicate['post_title'] . ' Copy'; // Set the status if ($settings['status'] != 'same') { $duplicate['post_status'] = $settings['status']; } // Set the post date $timestamp = $settings['timestamp'] == 'duplicate' ? strtotime($duplicate['post_date']) : current_time('timestamp', 0); $timestamp_gmt = $settings['timestamp'] == 'duplicate' ? strtotime($duplicate['post_date_gmt']) : current_time('timestamp', 1); if ($settings['time_offset']) { $offset = intval($settings['time_offset_seconds'] + $settings['time_offset_minutes'] * 60 + $settings['time_offset_hours'] * 3600 + $settings['time_offset_days'] * 86400); if ($settings['time_offset_direction'] == 'newer') { $timestamp = intval($timestamp + $offset); $timestamp_gmt = intval($timestamp_gmt + $offset); } else { $timestamp = intval($timestamp - $offset); $timestamp_gmt = intval($timestamp_gmt - $offset); } } $duplicate['post_date'] = date('Y-m-d H:i:s', $timestamp); $duplicate['post_date_gmt'] = date('Y-m-d H:i:s', $timestamp_gmt); $duplicate['post_modified'] = date('Y-m-d H:i:s', current_time('timestamp', 0)); $duplicate['post_modified_gmt'] = date('Y-m-d H:i:s', current_time('timestamp', 1)); // Remove some of the keys unset($duplicate['ID']); unset($duplicate['guid']); unset($duplicate['comment_count']); // Insert the post into the database $duplicate_id = wp_insert_post($duplicate); // Duplicate all the taxonomies/terms $taxonomies = get_object_taxonomies($duplicate['post_type']); foreach ($taxonomies as $taxonomy) { $terms = wp_get_post_terms($original_id, $taxonomy, array('fields' => 'names')); wp_set_object_terms($duplicate_id, $terms, $taxonomy); } // Duplicate all the custom fields $custom_fields = get_post_custom($original_id); foreach ($custom_fields as $key => $value) { if (is_array($value) && count($value) > 0) { foreach ($value as $i => $v) { add_post_meta($duplicate_id, $key, maybe_unserialize($v)); } } } echo 'Duplicate Post Created!'; die; // this is required to return a proper result }
/** * Add a duplicate post link. * * @since 2.10 */ function mtphr_post_duplicator_action_row($actions, $post) { $settings = get_mtphr_post_duplicator_settings(); // Get the post type object $post_type = get_post_type_object($post->post_type); // Set the button label $label = sprintf(__('Duplicate %s', 'post-duplicator'), $post_type->labels->singular_name); // Modify the label if duplicating to new post type if ($settings['type'] != 'same') { $new_post_type = get_post_type_object($settings['type']); if ($post_type->name != $new_post_type->name) { $label = sprintf(__('Duplicate %1$s to %2$s', 'post-duplicator'), $post_type->labels->singular_name, $new_post_type->labels->singular_name); } } // Create a nonce & add an action $nonce = wp_create_nonce('m4c_ajax_file_nonce'); $actions['duplicate_post'] = '<a class="m4c-duplicate-post" rel="' . $nonce . '" href="' . $post->ID . '">' . $label . '</a>'; return $actions; }
function mtphr_post_duplicator_notice() { $duplicated_id = isset($_GET['post-duplicated']) ? htmlspecialchars($_GET['post-duplicated'], ENT_QUOTES, 'UTF-8') : ''; if ($duplicated_id != '') { $settings = get_mtphr_post_duplicator_settings(); // Get the post type object $duplicated_post = get_post($duplicated_id); $post_type = get_post_type_object($duplicated_post->post_type); // Set the button label $pt = $post_type->labels->singular_name; $link = '<a href="' . get_edit_post_link($duplicated_id) . '">' . __('here', 'post-duplicator') . '</a>'; $label = sprintf(__('Successfully Duplicated! You can edit your new %1$s %2$s.', 'post-duplicator'), $pt, $link); ?> <div class="updated"> <p><?php echo $label; ?> </p> </div> <?php } }