PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/duplicate-post/duplicate-post-admin.php

https://gitlab.com/gabdark/aceit
PHP | 354 lines | 230 code | 52 blank | 72 comment | 67 complexity | d0290607fb2748ffbc57a9404930a76e MD5 | raw file
  1. <?php
  2. // Added by WarmStal
  3. if(!is_admin())
  4. return;
  5. require_once (dirname(__FILE__).'/duplicate-post-options.php');
  6. /**
  7. * Wrapper for the option 'duplicate_post_version'
  8. */
  9. function duplicate_post_get_installed_version() {
  10. return get_option( 'duplicate_post_version' );
  11. }
  12. /**
  13. * Wrapper for the defined constant DUPLICATE_POST_CURRENT_VERSION
  14. */
  15. function duplicate_post_get_current_version() {
  16. return DUPLICATE_POST_CURRENT_VERSION;
  17. }
  18. /**
  19. * Plugin upgrade
  20. */
  21. add_action('admin_init','duplicate_post_plugin_upgrade');
  22. function duplicate_post_plugin_upgrade() {
  23. $installed_version = duplicate_post_get_installed_version();
  24. if (empty($installed_version)) { // first install
  25. // Add capability to admin and editors
  26. // Get default roles
  27. $default_roles = array(
  28. 3 => 'editor',
  29. 8 => 'administrator',
  30. );
  31. // Cycle all roles and assign capability if its level >= duplicate_post_copy_user_level
  32. foreach ($default_roles as $level => $name){
  33. $role = get_role($name);
  34. if(!empty($role)) $role->add_cap( 'copy_posts' );
  35. }
  36. add_option('duplicate_post_copyexcerpt','1');
  37. add_option('duplicate_post_copyattachments','0');
  38. add_option('duplicate_post_copychildren','0');
  39. add_option('duplicate_post_copystatus','0');
  40. add_option('duplicate_post_taxonomies_blacklist',array());
  41. add_option('duplicate_post_show_row','1');
  42. add_option('duplicate_post_show_adminbar','1');
  43. add_option('duplicate_post_show_submitbox','1');
  44. } else if ( $installed_version==duplicate_post_get_current_version() ) { //re-install
  45. // do nothing
  46. } else { //upgrade form previous version
  47. // delete old, obsolete options
  48. delete_option('duplicate_post_admin_user_level');
  49. delete_option('duplicate_post_create_user_level');
  50. delete_option('duplicate_post_view_user_level');
  51. delete_option('dp_notice');
  52. /*
  53. * Convert old userlevel option to new capability scheme
  54. */
  55. // Get old duplicate_post_copy_user_level option
  56. $min_user_level = get_option('duplicate_post_copy_user_level');
  57. if (!empty($min_user_level)){
  58. // Get default roles
  59. $default_roles = array(
  60. 1 => 'contributor',
  61. 2 => 'author',
  62. 3 => 'editor',
  63. 8 => 'administrator',
  64. );
  65. // Cycle all roles and assign capability if its level >= duplicate_post_copy_user_level
  66. foreach ($default_roles as $level => $name){
  67. $role = get_role($name);
  68. if ($role && $min_user_level <= $level)
  69. $role->add_cap( 'copy_posts' );
  70. }
  71. // delete old option
  72. delete_option('duplicate_post_copy_user_level');
  73. }
  74. add_option('duplicate_post_copyexcerpt','1');
  75. add_option('duplicate_post_copyattachments','0');
  76. add_option('duplicate_post_copychildren','0');
  77. add_option('duplicate_post_copystatus','0');
  78. add_option('duplicate_post_taxonomies_blacklist',array());
  79. add_option('duplicate_post_show_row','1');
  80. add_option('duplicate_post_show_adminbar','1');
  81. add_option('duplicate_post_show_submitbox','1');
  82. }
  83. // Update version number
  84. update_option( 'duplicate_post_version', duplicate_post_get_current_version() );
  85. }
  86. if (get_option('duplicate_post_show_row') == 1){
  87. add_filter('post_row_actions', 'duplicate_post_make_duplicate_link_row',10,2);
  88. add_filter('page_row_actions', 'duplicate_post_make_duplicate_link_row',10,2);
  89. }
  90. /**
  91. * Add the link to action list for post_row_actions
  92. */
  93. function duplicate_post_make_duplicate_link_row($actions, $post) {
  94. if (duplicate_post_is_current_user_allowed_to_copy()) {
  95. $actions['clone'] = '<a href="'.duplicate_post_get_clone_post_link( $post->ID , 'display', false).'" title="'
  96. . esc_attr(__("Clone this item", DUPLICATE_POST_I18N_DOMAIN))
  97. . '">' . __('Clone', DUPLICATE_POST_I18N_DOMAIN) . '</a>';
  98. $actions['edit_as_new_draft'] = '<a href="'. duplicate_post_get_clone_post_link( $post->ID ) .'" title="'
  99. . esc_attr(__('Copy to a new draft', DUPLICATE_POST_I18N_DOMAIN))
  100. . '">' . __('New Draft', DUPLICATE_POST_I18N_DOMAIN) . '</a>';
  101. }
  102. return $actions;
  103. }
  104. /**
  105. * Add a button in the post/page edit screen to create a clone
  106. */
  107. if (get_option('duplicate_post_show_submitbox') == 1){
  108. add_action( 'post_submitbox_start', 'duplicate_post_add_duplicate_post_button' );
  109. }
  110. function duplicate_post_add_duplicate_post_button() {
  111. if ( isset( $_GET['post'] ) && duplicate_post_is_current_user_allowed_to_copy()) {
  112. ?>
  113. <div id="duplicate-action">
  114. <a class="submitduplicate duplication"
  115. href="<?php echo duplicate_post_get_clone_post_link( $_GET['post'] ) ?>"><?php _e('Copy to a new draft', DUPLICATE_POST_I18N_DOMAIN); ?>
  116. </a>
  117. </div>
  118. <?php
  119. }
  120. }
  121. /**
  122. * Connect actions to functions
  123. */
  124. add_action('admin_action_duplicate_post_save_as_new_post', 'duplicate_post_save_as_new_post');
  125. add_action('admin_action_duplicate_post_save_as_new_post_draft', 'duplicate_post_save_as_new_post_draft');
  126. /*
  127. * This function calls the creation of a new copy of the selected post (as a draft)
  128. * then redirects to the edit post screen
  129. */
  130. function duplicate_post_save_as_new_post_draft(){
  131. duplicate_post_save_as_new_post('draft');
  132. }
  133. /*
  134. * This function calls the creation of a new copy of the selected post (by default preserving the original publish status)
  135. * then redirects to the post list
  136. */
  137. function duplicate_post_save_as_new_post($status = ''){
  138. if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'duplicate_post_save_as_new_post' == $_REQUEST['action'] ) ) ) {
  139. wp_die(__('No post to duplicate has been supplied!', DUPLICATE_POST_I18N_DOMAIN));
  140. }
  141. // Get the original post
  142. $id = (isset($_GET['post']) ? $_GET['post'] : $_POST['post']);
  143. $post = get_post($id);
  144. // Copy the post and insert it
  145. if (isset($post) && $post!=null) {
  146. $new_id = duplicate_post_create_duplicate($post, $status);
  147. if ($status == ''){
  148. // Redirect to the post list screen
  149. wp_redirect( admin_url( 'edit.php?post_type='.$post->post_type) );
  150. } else {
  151. // Redirect to the edit screen for the new draft post
  152. wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_id ) );
  153. }
  154. exit;
  155. } else {
  156. $post_type_obj = get_post_type_object( $post->post_type );
  157. wp_die(esc_attr(__('Copy creation failed, could not find original:', DUPLICATE_POST_I18N_DOMAIN)) . ' ' . htmlspecialchars($id));
  158. }
  159. }
  160. /**
  161. * Get the currently registered user
  162. */
  163. function duplicate_post_get_current_user() {
  164. if (function_exists('wp_get_current_user')) {
  165. return wp_get_current_user();
  166. } else if (function_exists('get_currentuserinfo')) {
  167. global $userdata;
  168. get_currentuserinfo();
  169. return $userdata;
  170. } else {
  171. $user_login = $_COOKIE[USER_COOKIE];
  172. $sql = $wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_login=%s", $user_login);
  173. $current_user = $wpdb->get_results($sql);
  174. return $current_user;
  175. }
  176. }
  177. /**
  178. * Copy the taxonomies of a post to another post
  179. */
  180. function duplicate_post_copy_post_taxonomies($new_id, $post) {
  181. global $wpdb;
  182. if (isset($wpdb->terms)) {
  183. // Clear default category (added by wp_insert_post)
  184. wp_set_object_terms( $new_id, NULL, 'category' );
  185. $post_taxonomies = get_object_taxonomies($post->post_type);
  186. $taxonomies_blacklist = get_option('duplicate_post_taxonomies_blacklist');
  187. if ($taxonomies_blacklist == "") $taxonomies_blacklist = array();
  188. $taxonomies = array_diff($post_taxonomies, $taxonomies_blacklist);
  189. foreach ($taxonomies as $taxonomy) {
  190. $post_terms = wp_get_object_terms($post->ID, $taxonomy, array( 'orderby' => 'term_order' ));
  191. $terms = array();
  192. for ($i=0; $i<count($post_terms); $i++) {
  193. $terms[] = $post_terms[$i]->slug;
  194. }
  195. wp_set_object_terms($new_id, $terms, $taxonomy);
  196. }
  197. }
  198. }
  199. // Using our action hooks to copy taxonomies
  200. add_action('dp_duplicate_post', 'duplicate_post_copy_post_taxonomies', 10, 2);
  201. add_action('dp_duplicate_page', 'duplicate_post_copy_post_taxonomies', 10, 2);
  202. /**
  203. * Copy the meta information of a post to another post
  204. */
  205. function duplicate_post_copy_post_meta_info($new_id, $post) {
  206. $post_meta_keys = get_post_custom_keys($post->ID);
  207. if (empty($post_meta_keys)) return;
  208. $meta_blacklist = explode(",",get_option('duplicate_post_blacklist'));
  209. if ($meta_blacklist == "") $meta_blacklist = array();
  210. $meta_keys = array_diff($post_meta_keys, $meta_blacklist);
  211. foreach ($meta_keys as $meta_key) {
  212. $meta_values = get_post_custom_values($meta_key, $post->ID);
  213. foreach ($meta_values as $meta_value) {
  214. $meta_value = maybe_unserialize($meta_value);
  215. add_post_meta($new_id, $meta_key, $meta_value);
  216. }
  217. }
  218. }
  219. // Using our action hooks to copy meta fields
  220. add_action('dp_duplicate_post', 'duplicate_post_copy_post_meta_info', 10, 2);
  221. add_action('dp_duplicate_page', 'duplicate_post_copy_post_meta_info', 10, 2);
  222. /**
  223. * Copy the attachments
  224. * It simply copies the table entries, actual file won't be duplicated
  225. */
  226. function duplicate_post_copy_children($new_id, $post){
  227. $copy_attachments = get_option('duplicate_post_copyattachments');
  228. $copy_children = get_option('duplicate_post_copychildren');
  229. // get children
  230. $children = get_posts(array( 'post_type' => 'any', 'numberposts' => -1, 'post_status' => 'any', 'post_parent' => $post->ID ));
  231. // clone old attachments
  232. foreach($children as $child){
  233. if ($copy_attachments == 0 && $child->post_type == 'attachment') continue;
  234. if ($copy_children == 0 && $child->post_type != 'attachment') continue;
  235. duplicate_post_create_duplicate($child, '', $new_id);
  236. }
  237. }
  238. // Using our action hooks to copy attachments
  239. add_action('dp_duplicate_post', 'duplicate_post_copy_children', 10, 2);
  240. add_action('dp_duplicate_page', 'duplicate_post_copy_children', 10, 2);
  241. /**
  242. * Create a duplicate from a post
  243. */
  244. function duplicate_post_create_duplicate($post, $status = '', $parent_id = '') {
  245. // We don't want to clone revisions
  246. if ($post->post_type == 'revision') return;
  247. if ($post->post_type != 'attachment'){
  248. $prefix = get_option('duplicate_post_title_prefix');
  249. $suffix = get_option('duplicate_post_title_suffix');
  250. if (!empty($prefix)) $prefix.= " ";
  251. if (!empty($suffix)) $suffix = " ".$suffix;
  252. if (get_option('duplicate_post_copystatus') == 0) $status = 'draft';
  253. }
  254. $new_post_author = duplicate_post_get_current_user();
  255. $new_post = array(
  256. 'menu_order' => $post->menu_order,
  257. 'comment_status' => $post->comment_status,
  258. 'ping_status' => $post->ping_status,
  259. 'post_author' => $new_post_author->ID,
  260. 'post_content' => $post->post_content,
  261. 'post_excerpt' => (get_option('duplicate_post_copyexcerpt') == '1') ? $post->post_excerpt : "",
  262. 'post_mime_type' => $post->post_mime_type,
  263. 'post_parent' => $new_post_parent = empty($parent_id)? $post->post_parent : $parent_id,
  264. 'post_password' => $post->post_password,
  265. 'post_status' => $new_post_status = (empty($status))? $post->post_status: $status,
  266. 'post_title' => $prefix.$post->post_title.$suffix,
  267. 'post_type' => $post->post_type,
  268. );
  269. if(get_option('duplicate_post_copydate') == 1){
  270. $new_post['post_date'] = $new_post_date = $post->post_date ;
  271. $new_post['post_date_gmt'] = get_gmt_from_date($new_post_date);
  272. }
  273. $new_post_id = wp_insert_post($new_post);
  274. // If the copy is published or scheduled, we have to set a proper slug.
  275. if ($new_post_status == 'publish' || $new_post_status == 'future'){
  276. $post_name = wp_unique_post_slug($post->post_name, $new_post_id, $new_post_status, $post->post_type, $new_post_parent);
  277. $new_post = array();
  278. $new_post['ID'] = $new_post_id;
  279. $new_post['post_name'] = $post_name;
  280. // Update the post into the database
  281. wp_update_post( $new_post );
  282. }
  283. // If you have written a plugin which uses non-WP database tables to save
  284. // information about a post you can hook this action to dupe that data.
  285. if ($post->post_type == 'page' || (function_exists('is_post_type_hierarchical') && is_post_type_hierarchical( $post->post_type )))
  286. do_action( 'dp_duplicate_page', $new_post_id, $post );
  287. else
  288. do_action( 'dp_duplicate_post', $new_post_id, $post );
  289. delete_post_meta($new_post_id, '_dp_original');
  290. add_post_meta($new_post_id, '_dp_original', $post->ID);
  291. return $new_post_id;
  292. }
  293. //Add some links on the plugin page
  294. add_filter('plugin_row_meta', 'duplicate_post_add_plugin_links', 10, 2);
  295. function duplicate_post_add_plugin_links($links, $file) {
  296. if ( $file == plugin_basename(dirname(__FILE__).'/duplicate-post.php') ) {
  297. $links[] = '<a href="http://lopo.it/duplicate-post-plugin">' . __('Donate', DUPLICATE_POST_I18N_DOMAIN) . '</a>';
  298. $links[] = '<a href="http://lopo.it/duplicate-post-plugin">' . __('Translate', DUPLICATE_POST_I18N_DOMAIN) . '</a>';
  299. }
  300. return $links;
  301. }
  302. ?>