PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/project-extra/includes/helpers/duplicate-post.php

https://gitlab.com/webkod3r/tripolis
PHP | 102 lines | 58 code | 10 blank | 34 comment | 11 complexity | 31f565881da6d874b1d945a5659992c3 MD5 | raw file
  1. <?php
  2. /*
  3. * Crear duplicado de las entradas como borrador y redirige luego a la pantalla de ediciĆ³n
  4. */
  5. function rd_duplicate_post_as_draft(){
  6. global $wpdb;
  7. if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'rd_duplicate_post_as_draft' == $_REQUEST['action'] ) ) ) {
  8. wp_die('No post to duplicate has been supplied!');
  9. }
  10. /*
  11. * get the original post id
  12. */
  13. $post_id = (isset($_GET['post']) ? $_GET['post'] : $_POST['post']);
  14. /*
  15. * and all the original post data then
  16. */
  17. $post = get_post( $post_id );
  18. /*
  19. * if you don't want current user to be the new post author,
  20. * then change next couple of lines to this: $new_post_author = $post->post_author;
  21. */
  22. $current_user = wp_get_current_user();
  23. $new_post_author = $current_user->ID;
  24. /*
  25. * if post data exists, create the post duplicate
  26. */
  27. if (isset( $post ) && $post != null) {
  28. /*
  29. * new post data array
  30. */
  31. $args = array(
  32. 'comment_status' => $post->comment_status,
  33. 'ping_status' => $post->ping_status,
  34. 'post_author' => $new_post_author,
  35. 'post_content' => $post->post_content,
  36. 'post_excerpt' => $post->post_excerpt,
  37. 'post_name' => $post->post_name,
  38. 'post_parent' => $post->post_parent,
  39. 'post_password' => $post->post_password,
  40. 'post_status' => 'draft',
  41. 'post_title' => $post->post_title,
  42. 'post_type' => $post->post_type,
  43. 'to_ping' => $post->to_ping,
  44. 'menu_order' => $post->menu_order
  45. );
  46. /*
  47. * insert the post by wp_insert_post() function
  48. */
  49. $new_post_id = wp_insert_post( $args );
  50. /*
  51. * get all current post terms ad set them to the new post draft
  52. */
  53. $taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
  54. foreach ($taxonomies as $taxonomy) {
  55. $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
  56. wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
  57. }
  58. /*
  59. * duplicate all post meta
  60. */
  61. $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
  62. if (count($post_meta_infos)!=0) {
  63. $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
  64. foreach ($post_meta_infos as $meta_info) {
  65. $meta_key = $meta_info->meta_key;
  66. $meta_value = addslashes($meta_info->meta_value);
  67. $sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
  68. }
  69. $sql_query.= implode(" UNION ALL ", $sql_query_sel);
  70. $wpdb->query($sql_query);
  71. }
  72. /*
  73. * finally, redirect to the edit post screen for the new draft
  74. */
  75. wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
  76. exit;
  77. } else {
  78. wp_die('Post creation failed, could not find original post: ' . $post_id);
  79. }
  80. }
  81. add_action( 'admin_action_rd_duplicate_post_as_draft', 'rd_duplicate_post_as_draft' );
  82. /*
  83. * Add the duplicate link to action list for post_row_actions
  84. */
  85. function rd_duplicate_post_link( $actions, $post ) {
  86. if (current_user_can('edit_posts')) {
  87. $actions['duplicate'] = '<a href="admin.php?action=rd_duplicate_post_as_draft&amp;post=' . $post->ID . '" title="Duplicar este elemento" rel="permalink">Duplicar</a>';
  88. }
  89. return $actions;
  90. }
  91. add_filter( 'post_row_actions', 'rd_duplicate_post_link', 10, 2); /* for posts */
  92. add_filter( 'page_row_actions', 'rd_duplicate_post_link', 10, 2); /* for pages */