duplicatepage.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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: 2.7
  8. Author URI: https://profiles.wordpress.org/mndpsingh287/
  9. License: GPLv2
  10. Text Domain: duplicate-page
  11. */
  12. if (!defined("DUPLICATE_PAGE_PLUGIN_DIRNAME")) define("DUPLICATE_PAGE_PLUGIN_DIRNAME", plugin_basename(dirname(__FILE__)));
  13. if(!class_exists('duplicate_page')):
  14. class duplicate_page
  15. {
  16. /*
  17. * AutoLoad Hooks
  18. */
  19. public function __construct(){
  20. register_activation_hook(__FILE__, array(&$this, 'duplicate_page_install'));
  21. add_action('admin_menu', array(&$this, 'duplicate_page_options_page'));
  22. add_filter( 'plugin_action_links', array(&$this, 'duplicate_page_plugin_action_links'), 10, 2 );
  23. add_action( 'admin_action_dt_duplicate_post_as_draft', array(&$this,'dt_duplicate_post_as_draft') );
  24. add_filter( 'post_row_actions', array(&$this,'dt_duplicate_post_link'), 10, 2);
  25. add_filter( 'page_row_actions', array(&$this,'dt_duplicate_post_link'), 10, 2);
  26. add_action( 'post_submitbox_misc_actions', array(&$this,'duplicate_page_custom_button'));
  27. add_action( 'wp_before_admin_bar_render', array(&$this, 'duplicate_page_admin_bar_link'));
  28. add_action('init', array(&$this, 'duplicate_page_load_text_domain'));
  29. }
  30. /*
  31. * Localization - 19-dec-2016
  32. */
  33. public function duplicate_page_load_text_domain(){
  34. load_plugin_textdomain('duplicate-page', false, DUPLICATE_PAGE_PLUGIN_DIRNAME . "/languages");
  35. }
  36. /*
  37. * Activation Hook
  38. */
  39. public function duplicate_page_install(){
  40. $defaultsettings = array(
  41. 'duplicate_post_status' => 'draft',
  42. 'duplicate_post_redirect' => 'to_list',
  43. 'duplicate_post_suffix' => ''
  44. );
  45. $opt = get_option('duplicate_page_options');
  46. if(!$opt['duplicate_post_status']) {
  47. update_option('duplicate_page_options', $defaultsettings);
  48. }
  49. }
  50. /*
  51. Action Links
  52. */
  53. public function duplicate_page_plugin_action_links($links, $file){
  54. if ( $file == plugin_basename( __FILE__ ) ) {
  55. $duplicate_page_links = '<a href="'.get_admin_url().'options-general.php?page=duplicate_page_settings">'.__('Settings', 'duplicate-page').'</a>';
  56. $duplicate_page_donate = '<a href="http://www.webdesi9.com/donate/?plugin=duplicate-page" title="Donate Now" target="_blank" style="font-weight:bold">'.__('Donate', 'duplicate-page').'</a>';
  57. array_unshift( $links, $duplicate_page_donate );
  58. array_unshift( $links, $duplicate_page_links );
  59. }
  60. return $links;
  61. }
  62. /*
  63. * Admin Menu
  64. */
  65. public function duplicate_page_options_page(){
  66. add_options_page( __( 'Duplicate Page', 'duplicate-page' ), __( 'Duplicate Page', 'duplicate-page' ), 'manage_options', 'duplicate_page_settings',array(&$this, 'duplicate_page_settings'));
  67. }
  68. /*
  69. * Duplicate Page Admin Settings
  70. */
  71. public function duplicate_page_settings(){
  72. if(current_user_can( 'manage_options' )){
  73. include('admin-settings.php');
  74. }
  75. }
  76. /*
  77. * Main function
  78. */
  79. public function dt_duplicate_post_as_draft(){
  80. global $wpdb;
  81. $opt = get_option('duplicate_page_options');
  82. $suffix = isset($opt['duplicate_post_suffix']) && !empty($opt['duplicate_post_suffix']) ? ' -- '.$opt['duplicate_post_suffix'] : '';
  83. $post_status = !empty($opt['duplicate_post_status']) ? $opt['duplicate_post_status'] : 'draft';
  84. $redirectit = !empty($opt['duplicate_post_redirect']) ? $opt['duplicate_post_redirect'] : 'to_list';
  85. if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'dt_duplicate_post_as_draft' == $_REQUEST['action'] ) ) ) {
  86. wp_die('No post to duplicate has been supplied!');
  87. }
  88. $returnpage = '';
  89. /*
  90. * get the original post id
  91. */
  92. $post_id = (isset($_GET['post']) ? $_GET['post'] : $_POST['post']);
  93. /*
  94. * and all the original post data then
  95. */
  96. $post = get_post( $post_id );
  97. /*
  98. * if you don't want current user to be the new post author,
  99. * then change next couple of lines to this: $new_post_author = $post->post_author;
  100. */
  101. $current_user = wp_get_current_user();
  102. $new_post_author = $current_user->ID;
  103. /*
  104. * if post data exists, create the post duplicate
  105. */
  106. if (isset( $post ) && $post != null) {
  107. /*
  108. * new post data array
  109. */
  110. $args = array(
  111. 'comment_status' => $post->comment_status,
  112. 'ping_status' => $post->ping_status,
  113. 'post_author' => $new_post_author,
  114. 'post_content' => $post->post_content,
  115. 'post_excerpt' => $post->post_excerpt,
  116. 'post_name' => $post->post_name,
  117. 'post_parent' => $post->post_parent,
  118. 'post_password' => $post->post_password,
  119. 'post_status' => $post_status,
  120. 'post_title' => $post->post_title.$suffix,
  121. 'post_type' => $post->post_type,
  122. 'to_ping' => $post->to_ping,
  123. 'menu_order' => $post->menu_order
  124. );
  125. /*
  126. * insert the post by wp_insert_post() function
  127. */
  128. $new_post_id = wp_insert_post( $args );
  129. /*
  130. * get all current post terms ad set them to the new post draft
  131. */
  132. $taxonomies = get_object_taxonomies($post->post_type);
  133. if(!empty($taxonomies) && is_array($taxonomies)):
  134. foreach ($taxonomies as $taxonomy) {
  135. $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
  136. wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
  137. }
  138. endif;
  139. /*
  140. * duplicate all post meta
  141. */
  142. $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
  143. if (count($post_meta_infos)!=0) {
  144. $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
  145. foreach ($post_meta_infos as $meta_info) {
  146. $meta_key = $meta_info->meta_key;
  147. $meta_value = addslashes($meta_info->meta_value);
  148. $sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
  149. }
  150. $sql_query.= implode(" UNION ALL ", $sql_query_sel);
  151. $wpdb->query($sql_query);
  152. }
  153. /*
  154. * finally, redirecting to your choice
  155. */
  156. if($post->post_type != 'post'):
  157. $returnpage = '?post_type='.$post->post_type;
  158. endif;
  159. if(!empty($redirectit) && $redirectit == 'to_list'):
  160. wp_redirect( admin_url( 'edit.php'.$returnpage ) );
  161. elseif(!empty($redirectit) && $redirectit == 'to_page'):
  162. wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
  163. else:
  164. wp_redirect( admin_url( 'edit.php'.$returnpage ) );
  165. endif;
  166. exit;
  167. } else {
  168. wp_die('Error! Post creation failed, could not find original post: ' . $post_id);
  169. }
  170. }
  171. /*
  172. * Add the duplicate link to action list for post_row_actions
  173. */
  174. public function dt_duplicate_post_link( $actions, $post ) {
  175. $opt = get_option('duplicate_page_options');
  176. $post_status = !empty($opt['duplicate_post_status']) ? $opt['duplicate_post_status'] : 'draft';
  177. if (current_user_can('edit_posts')) {
  178. $actions['duplicate'] = '<a href="admin.php?action=dt_duplicate_post_as_draft&amp;post=' . $post->ID . '" title="Duplicate this as '.$post_status.'" rel="permalink">'.__( "Duplicate This", "duplicate-page" ).'</a>';
  179. }
  180. return $actions;
  181. }
  182. /*
  183. * Add the duplicate link to edit screen
  184. */
  185. public function duplicate_page_custom_button(){
  186. global $post;
  187. $opt = get_option('duplicate_page_options');
  188. $post_status = !empty($opt['duplicate_post_status']) ? $opt['duplicate_post_status'] : 'draft';
  189. $html = '<div id="major-publishing-actions">';
  190. $html .= '<div id="export-action">';
  191. $html .= '<a href="admin.php?action=dt_duplicate_post_as_draft&amp;post=' . $post->ID . '" title="Duplicate this as '.$post_status.'" rel="permalink">'.__( "Duplicate This", "duplicate-page" ).'</a>';
  192. $html .= '</div>';
  193. $html .= '</div>';
  194. echo $html;
  195. }
  196. /*
  197. * Admin Bar Duplicate This Link
  198. */
  199. public function duplicate_page_admin_bar_link(){
  200. global $wp_admin_bar, $post;
  201. $opt = get_option('duplicate_page_options');
  202. $post_status = !empty($opt['duplicate_post_status']) ? $opt['duplicate_post_status'] : 'draft';
  203. $current_object = get_queried_object();
  204. if ( empty($current_object) )
  205. return;
  206. if ( ! empty( $current_object->post_type )
  207. && ( $post_type_object = get_post_type_object( $current_object->post_type ) )
  208. && ( $post_type_object->show_ui || $current_object->post_type == 'attachment') )
  209. {
  210. $wp_admin_bar->add_menu( array(
  211. 'parent' => 'edit',
  212. 'id' => 'duplicate_this',
  213. 'title' => __("Duplicate this as ".$post_status."", 'duplicate-page'),
  214. 'href' => admin_url().'admin.php?action=dt_duplicate_post_as_draft&amp;post='. $post->ID
  215. ) );
  216. }
  217. }
  218. /*
  219. * Redirect function
  220. */
  221. static function dp_redirect($url){
  222. echo '<script>window.location.href="'.$url.'"</script>';
  223. }
  224. }
  225. new duplicate_page;
  226. endif;
  227. ?>