Ei kuvausta

duplicate-post-admin.php 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. <?php
  2. /**
  3. * Backend functions.
  4. *
  5. * @package Duplicate Post
  6. * @since 2.0
  7. */
  8. if ( ! is_admin() ) {
  9. return;
  10. }
  11. require_once dirname( __FILE__ ) . '/duplicate-post-options.php';
  12. require_once dirname( __FILE__ ) . '/compat/duplicate-post-wpml.php';
  13. require_once dirname( __FILE__ ) . '/compat/duplicate-post-jetpack.php';
  14. /**
  15. * Wrapper for the option 'duplicate_post_version'.
  16. */
  17. function duplicate_post_get_installed_version() {
  18. return get_option( 'duplicate_post_version' );
  19. }
  20. /**
  21. * Wrapper for the defined constant DUPLICATE_POST_CURRENT_VERSION.
  22. */
  23. function duplicate_post_get_current_version() {
  24. return DUPLICATE_POST_CURRENT_VERSION;
  25. }
  26. add_action( 'admin_init', 'duplicate_post_admin_init' );
  27. /**
  28. * Adds handlers depending on the options.
  29. */
  30. function duplicate_post_admin_init() {
  31. duplicate_post_plugin_upgrade();
  32. if ( intval( get_site_option( 'duplicate_post_show_notice' ) ) === 1 ) {
  33. if ( is_multisite() ) {
  34. add_action( 'network_admin_notices', 'duplicate_post_show_update_notice' );
  35. } else {
  36. add_action( 'admin_notices', 'duplicate_post_show_update_notice' );
  37. }
  38. add_action( 'wp_ajax_duplicate_post_dismiss_notice', 'duplicate_post_dismiss_notice' );
  39. }
  40. add_action( 'dp_duplicate_post', 'duplicate_post_copy_post_meta_info', 10, 2 );
  41. add_action( 'dp_duplicate_page', 'duplicate_post_copy_post_meta_info', 10, 2 );
  42. if ( intval( get_option( 'duplicate_post_copychildren' ) ) === 1 ) {
  43. add_action( 'dp_duplicate_post', 'duplicate_post_copy_children', 20, 3 );
  44. add_action( 'dp_duplicate_page', 'duplicate_post_copy_children', 20, 3 );
  45. }
  46. if ( intval( get_option( 'duplicate_post_copyattachments' ) ) === 1 ) {
  47. add_action( 'dp_duplicate_post', 'duplicate_post_copy_attachments', 30, 2 );
  48. add_action( 'dp_duplicate_page', 'duplicate_post_copy_attachments', 30, 2 );
  49. }
  50. if ( intval( get_option( 'duplicate_post_copycomments' ) ) === 1 ) {
  51. add_action( 'dp_duplicate_post', 'duplicate_post_copy_comments', 40, 2 );
  52. add_action( 'dp_duplicate_page', 'duplicate_post_copy_comments', 40, 2 );
  53. }
  54. add_action( 'dp_duplicate_post', 'duplicate_post_copy_post_taxonomies', 50, 2 );
  55. add_action( 'dp_duplicate_page', 'duplicate_post_copy_post_taxonomies', 50, 2 );
  56. add_filter( 'plugin_row_meta', 'duplicate_post_add_plugin_links', 10, 2 );
  57. }
  58. /**
  59. * Plugin upgrade.
  60. */
  61. function duplicate_post_plugin_upgrade() {
  62. $installed_version = duplicate_post_get_installed_version();
  63. if ( duplicate_post_get_current_version() === $installed_version ) {
  64. return;
  65. }
  66. if ( empty( $installed_version ) ) {
  67. // Get default roles.
  68. $default_roles = array(
  69. 'editor',
  70. 'administrator',
  71. 'wpseo_manager',
  72. 'wpseo_editor',
  73. );
  74. foreach ( $default_roles as $name ) {
  75. $role = get_role( $name );
  76. if ( ! empty( $role ) ) {
  77. $role->add_cap( 'copy_posts' );
  78. }
  79. }
  80. }
  81. $show_links_in_defaults = [
  82. 'row' => '1',
  83. 'adminbar' => '1',
  84. 'submitbox' => '1',
  85. 'bulkactions' => '1',
  86. ];
  87. add_option( 'duplicate_post_copytitle', '1' );
  88. add_option( 'duplicate_post_copydate', '0' );
  89. add_option( 'duplicate_post_copystatus', '0' );
  90. add_option( 'duplicate_post_copyslug', '0' );
  91. add_option( 'duplicate_post_copyexcerpt', '1' );
  92. add_option( 'duplicate_post_copycontent', '1' );
  93. add_option( 'duplicate_post_copythumbnail', '1' );
  94. add_option( 'duplicate_post_copytemplate', '1' );
  95. add_option( 'duplicate_post_copyformat', '1' );
  96. add_option( 'duplicate_post_copyauthor', '0' );
  97. add_option( 'duplicate_post_copypassword', '0' );
  98. add_option( 'duplicate_post_copyattachments', '0' );
  99. add_option( 'duplicate_post_copychildren', '0' );
  100. add_option( 'duplicate_post_copycomments', '0' );
  101. add_option( 'duplicate_post_copymenuorder', '1' );
  102. add_option( 'duplicate_post_taxonomies_blacklist', array() );
  103. add_option( 'duplicate_post_blacklist', '' );
  104. add_option( 'duplicate_post_types_enabled', array( 'post', 'page' ) );
  105. add_option( 'duplicate_post_show_original_column', '0' );
  106. add_option( 'duplicate_post_show_original_in_post_states', '0' );
  107. add_option( 'duplicate_post_show_original_meta_box', '0' );
  108. add_option(
  109. 'duplicate_post_show_link',
  110. [
  111. 'new_draft' => '1',
  112. 'clone' => '1',
  113. 'rewrite_republish' => '1',
  114. ]
  115. );
  116. add_option( 'duplicate_post_show_link_in', $show_links_in_defaults );
  117. $taxonomies_blacklist = get_option( 'duplicate_post_taxonomies_blacklist' );
  118. if ( '' === $taxonomies_blacklist ) {
  119. $taxonomies_blacklist = array();
  120. }
  121. if ( in_array( 'post_format', $taxonomies_blacklist, true ) ) {
  122. update_option( 'duplicate_post_copyformat', 0 );
  123. $taxonomies_blacklist = array_diff( $taxonomies_blacklist, array( 'post_format' ) );
  124. update_option( 'duplicate_post_taxonomies_blacklist', $taxonomies_blacklist );
  125. }
  126. $meta_blacklist = explode( ',', get_option( 'duplicate_post_blacklist' ) );
  127. if ( '' === $meta_blacklist ) {
  128. $meta_blacklist = array();
  129. }
  130. $meta_blacklist = array_map( 'trim', $meta_blacklist );
  131. if ( in_array( '_wp_page_template', $meta_blacklist, true ) ) {
  132. update_option( 'duplicate_post_copytemplate', 0 );
  133. $meta_blacklist = array_diff( $meta_blacklist, array( '_wp_page_template' ) );
  134. }
  135. if ( in_array( '_thumbnail_id', $meta_blacklist, true ) ) {
  136. update_option( 'duplicate_post_copythumbnail', 0 );
  137. $meta_blacklist = array_diff( $meta_blacklist, array( '_thumbnail_id' ) );
  138. }
  139. update_option( 'duplicate_post_blacklist', implode( ',', $meta_blacklist ) );
  140. delete_option( 'duplicate_post_show_notice' );
  141. if ( version_compare( $installed_version, '4.1.0' ) < 0 ) {
  142. update_site_option( 'duplicate_post_show_notice', 1 );
  143. }
  144. if ( version_compare( $installed_version, '4.0.0' ) < 0 ) {
  145. // Migrate the 'Show links in' options to the new array-based structure.
  146. duplicate_post_migrate_show_links_in_options( $show_links_in_defaults );
  147. }
  148. delete_site_option( 'duplicate_post_version' );
  149. update_option( 'duplicate_post_version', duplicate_post_get_current_version() );
  150. }
  151. /**
  152. * Runs the upgrade routine for version 4.0 to update the options in the database.
  153. *
  154. * @param array $defaults The default options to fall back on.
  155. *
  156. * @return void
  157. */
  158. function duplicate_post_migrate_show_links_in_options( $defaults ) {
  159. $options_to_migrate = [
  160. 'duplicate_post_show_row' => 'row',
  161. 'duplicate_post_show_adminbar' => 'adminbar',
  162. 'duplicate_post_show_submitbox' => 'submitbox',
  163. 'duplicate_post_show_bulkactions' => 'bulkactions',
  164. ];
  165. $new_options = [];
  166. foreach ( $options_to_migrate as $old => $new ) {
  167. $new_options[ $new ] = \get_option( $old, $defaults[ $new ] );
  168. \delete_option( $old );
  169. }
  170. \update_option( 'duplicate_post_show_link_in', $new_options );
  171. }
  172. /**
  173. * Shows the update notice.
  174. *
  175. * @global string $wp_version The WordPress version string.
  176. */
  177. function duplicate_post_show_update_notice() {
  178. if ( ! current_user_can( 'manage_options' ) ) {
  179. return;
  180. }
  181. $current_screen = get_current_screen();
  182. if (
  183. empty( $current_screen ) ||
  184. empty( $current_screen->base ) ||
  185. ( $current_screen->base !== 'dashboard' && $current_screen->base !== 'plugins' )
  186. ) {
  187. return;
  188. }
  189. $class = 'notice is-dismissible';
  190. $message = '<p><strong>' . sprintf(
  191. /* translators: %s: Yoast Duplicate Post version. */
  192. __( "What's new in Yoast Duplicate Post version %s:", 'duplicate-post' ),
  193. DUPLICATE_POST_CURRENT_VERSION
  194. ) . '</strong> ';
  195. $message .= __( 'Now also available in Elementor: the powerful Rewrite & Republish feature. Updating your content has never been easier!', 'duplicate-post' )
  196. . ' ';
  197. $message .= '<a href="https://yoa.st/duplicate-post-4-1">'
  198. . sprintf(
  199. /* translators: %s: Yoast Duplicate Post version. */
  200. __( 'Read more about what’s new in Yoast Duplicate Post %s!', 'duplicate-post' ),
  201. DUPLICATE_POST_CURRENT_VERSION
  202. )
  203. . '</a></p>';
  204. $message .= '<p>%%SIGNUP_FORM%%</p>';
  205. $allowed_tags = array(
  206. 'a' => array(
  207. 'href' => array(),
  208. ),
  209. 'br' => array(),
  210. 'p' => array(),
  211. 'strong' => array(),
  212. );
  213. $sanitized_message = wp_kses( $message, $allowed_tags );
  214. $sanitized_message = str_replace( '%%SIGNUP_FORM%%', duplicate_post_newsletter_signup_form(), $sanitized_message );
  215. $img_path = plugins_url( '/duplicate_post_yoast_icon-125x125.png', __FILE__ );
  216. echo '<div id="duplicate-post-notice" class="' . esc_attr( $class ) . '" style="display: flex; align-items: center;">
  217. <img src="' . esc_url( $img_path ) . '" alt=""/>
  218. <div style="margin: 0.5em">' . $sanitized_message . // phpcs:ignore WordPress.Security.EscapeOutput
  219. '</div></div>';
  220. echo "<script>
  221. function duplicate_post_dismiss_notice(){
  222. var data = {
  223. 'action': 'duplicate_post_dismiss_notice',
  224. };
  225. jQuery.post(ajaxurl, data, function(response) {
  226. jQuery('#duplicate-post-notice').hide();
  227. });
  228. }
  229. jQuery(document).ready(function(){
  230. jQuery('body').on('click', '.notice-dismiss', function(){
  231. duplicate_post_dismiss_notice();
  232. });
  233. });
  234. </script>";
  235. }
  236. /**
  237. * Dismisses the notice.
  238. *
  239. * @return bool
  240. */
  241. function duplicate_post_dismiss_notice() {
  242. $result = update_site_option( 'duplicate_post_show_notice', 0 );
  243. return $result;
  244. }
  245. /**
  246. * Copies the taxonomies of a post to another post.
  247. *
  248. * @global wpdb $wpdb WordPress database abstraction object.
  249. *
  250. * @param int $new_id New post ID.
  251. * @param WP_Post $post The original post object.
  252. */
  253. function duplicate_post_copy_post_taxonomies( $new_id, $post ) {
  254. global $wpdb;
  255. if ( isset( $wpdb->terms ) ) {
  256. // Clear default category (added by wp_insert_post).
  257. wp_set_object_terms( $new_id, null, 'category' );
  258. $post_taxonomies = get_object_taxonomies( $post->post_type );
  259. // Several plugins just add support to post-formats but don't register post_format taxonomy.
  260. if ( post_type_supports( $post->post_type, 'post-formats' ) && ! in_array( 'post_format', $post_taxonomies, true ) ) {
  261. $post_taxonomies[] = 'post_format';
  262. }
  263. $taxonomies_blacklist = get_option( 'duplicate_post_taxonomies_blacklist' );
  264. if ( '' === $taxonomies_blacklist ) {
  265. $taxonomies_blacklist = array();
  266. }
  267. if ( intval( get_option( 'duplicate_post_copyformat' ) ) === 0 ) {
  268. $taxonomies_blacklist[] = 'post_format';
  269. }
  270. /**
  271. * Filters the taxonomy excludelist when copying a post.
  272. *
  273. * @param array $taxonomies_blacklist The taxonomy excludelist from the options.
  274. *
  275. * @return array
  276. */
  277. $taxonomies_blacklist = apply_filters( 'duplicate_post_taxonomies_excludelist_filter', $taxonomies_blacklist );
  278. $taxonomies = array_diff( $post_taxonomies, $taxonomies_blacklist );
  279. foreach ( $taxonomies as $taxonomy ) {
  280. $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'orderby' => 'term_order' ) );
  281. $terms = array();
  282. $num_terms = count( $post_terms );
  283. for ( $i = 0; $i < $num_terms; $i++ ) {
  284. $terms[] = $post_terms[ $i ]->slug;
  285. }
  286. wp_set_object_terms( $new_id, $terms, $taxonomy );
  287. }
  288. }
  289. }
  290. /**
  291. * Copies the meta information of a post to another post
  292. *
  293. * @param int $new_id The new post ID.
  294. * @param WP_Post $post The original post object.
  295. */
  296. function duplicate_post_copy_post_meta_info( $new_id, $post ) {
  297. $post_meta_keys = get_post_custom_keys( $post->ID );
  298. if ( empty( $post_meta_keys ) ) {
  299. return;
  300. }
  301. $meta_blacklist = get_option( 'duplicate_post_blacklist' );
  302. if ( '' === $meta_blacklist ) {
  303. $meta_blacklist = array();
  304. } else {
  305. $meta_blacklist = explode( ',', $meta_blacklist );
  306. $meta_blacklist = array_filter( $meta_blacklist );
  307. $meta_blacklist = array_map( 'trim', $meta_blacklist );
  308. }
  309. $meta_blacklist[] = '_edit_lock'; // Edit lock.
  310. $meta_blacklist[] = '_edit_last'; // Edit lock.
  311. $meta_blacklist[] = '_dp_is_rewrite_republish_copy';
  312. $meta_blacklist[] = '_dp_has_rewrite_republish_copy';
  313. if ( intval( get_option( 'duplicate_post_copytemplate' ) ) === 0 ) {
  314. $meta_blacklist[] = '_wp_page_template';
  315. }
  316. if ( intval( get_option( 'duplicate_post_copythumbnail' ) ) === 0 ) {
  317. $meta_blacklist[] = '_thumbnail_id';
  318. }
  319. $meta_blacklist = apply_filters_deprecated( 'duplicate_post_blacklist_filter', array( $meta_blacklist ), '3.2.5', 'duplicate_post_excludelist_filter' );
  320. /**
  321. * Filters the meta fields excludelist when copying a post.
  322. *
  323. * @param array $meta_blacklist The meta fields excludelist from the options.
  324. *
  325. * @return array
  326. */
  327. $meta_blacklist = apply_filters( 'duplicate_post_excludelist_filter', $meta_blacklist );
  328. $meta_blacklist_string = '(' . implode( ')|(', $meta_blacklist ) . ')';
  329. if ( strpos( $meta_blacklist_string, '*' ) !== false ) {
  330. $meta_blacklist_string = str_replace( array( '*' ), array( '[a-zA-Z0-9_]*' ), $meta_blacklist_string );
  331. $meta_keys = array();
  332. foreach ( $post_meta_keys as $meta_key ) {
  333. if ( ! preg_match( '#^' . $meta_blacklist_string . '$#', $meta_key ) ) {
  334. $meta_keys[] = $meta_key;
  335. }
  336. }
  337. } else {
  338. $meta_keys = array_diff( $post_meta_keys, $meta_blacklist );
  339. }
  340. /**
  341. * Filters the list of meta fields names when copying a post.
  342. *
  343. * @param array $meta_keys The list of meta fields name, with the ones in the excludelist already removed.
  344. *
  345. * @return array
  346. */
  347. $meta_keys = apply_filters( 'duplicate_post_meta_keys_filter', $meta_keys );
  348. foreach ( $meta_keys as $meta_key ) {
  349. $meta_values = get_post_custom_values( $meta_key, $post->ID );
  350. foreach ( $meta_values as $meta_value ) {
  351. $meta_value = maybe_unserialize( $meta_value );
  352. add_post_meta( $new_id, $meta_key, duplicate_post_wp_slash( $meta_value ) );
  353. }
  354. }
  355. }
  356. /**
  357. * Workaround for inconsistent wp_slash.
  358. * Works only with WP 4.4+ (map_deep)
  359. *
  360. * @ignore
  361. *
  362. * @param mixed $value Array or object to be recursively slashed.
  363. * @return string|mixed
  364. */
  365. function duplicate_post_addslashes_deep( $value ) {
  366. if ( function_exists( 'map_deep' ) ) {
  367. return map_deep( $value, 'duplicate_post_addslashes_to_strings_only' );
  368. } else {
  369. return wp_slash( $value );
  370. }
  371. }
  372. /**
  373. * Adds slashes only to strings.
  374. *
  375. * @ignore
  376. *
  377. * @param mixed $value Value to slash only if string.
  378. * @return string|mixed
  379. */
  380. function duplicate_post_addslashes_to_strings_only( $value ) {
  381. return is_string( $value ) ? addslashes( $value ) : $value;
  382. }
  383. /**
  384. * Replacement function for faulty core wp_slash().
  385. *
  386. * @ignore
  387. *
  388. * @param mixed $value What to add slash to.
  389. * @return mixed
  390. */
  391. function duplicate_post_wp_slash( $value ) {
  392. return duplicate_post_addslashes_deep( $value );
  393. }
  394. /**
  395. * Copies attachments, including physical files.
  396. *
  397. * @param int $new_id The new post ID.
  398. * @param WP_Post $post The original post object.
  399. */
  400. function duplicate_post_copy_attachments( $new_id, $post ) {
  401. // Get thumbnail ID.
  402. $old_thumbnail_id = get_post_thumbnail_id( $post->ID );
  403. // Get children.
  404. $children = get_posts(
  405. array(
  406. 'post_type' => 'any',
  407. 'numberposts' => -1,
  408. 'post_status' => 'any',
  409. 'post_parent' => $post->ID,
  410. )
  411. );
  412. // Clone old attachments.
  413. foreach ( $children as $child ) {
  414. if ( 'attachment' !== $child->post_type ) {
  415. continue;
  416. }
  417. $url = wp_get_attachment_url( $child->ID );
  418. // Let's copy the actual file.
  419. $tmp = download_url( $url );
  420. if ( is_wp_error( $tmp ) ) {
  421. continue;
  422. }
  423. $desc = wp_slash( $child->post_content );
  424. $file_array = array();
  425. $file_array['name'] = basename( $url );
  426. $file_array['tmp_name'] = $tmp;
  427. // "Upload" to the media collection
  428. $new_attachment_id = media_handle_sideload( $file_array, $new_id, $desc );
  429. if ( is_wp_error( $new_attachment_id ) ) {
  430. unlink( $file_array['tmp_name'] );
  431. continue;
  432. }
  433. $new_post_author = wp_get_current_user();
  434. $cloned_child = array(
  435. 'ID' => $new_attachment_id,
  436. 'post_title' => $child->post_title,
  437. 'post_exceprt' => $child->post_title,
  438. 'post_author' => $new_post_author->ID,
  439. );
  440. wp_update_post( wp_slash( $cloned_child ) );
  441. $alt_title = get_post_meta( $child->ID, '_wp_attachment_image_alt', true );
  442. if ( $alt_title ) {
  443. update_post_meta( $new_attachment_id, '_wp_attachment_image_alt', wp_slash( $alt_title ) );
  444. }
  445. // If we have cloned the post thumbnail, set the copy as the thumbnail for the new post.
  446. if ( intval( get_option( 'duplicate_post_copythumbnail' ) ) === 1 && $old_thumbnail_id === $child->ID ) {
  447. set_post_thumbnail( $new_id, $new_attachment_id );
  448. }
  449. }
  450. }
  451. /**
  452. * Copies child posts.
  453. *
  454. * @param int $new_id The new post ID.
  455. * @param WP_Post $post The original post object.
  456. * @param string $status Optional. The destination status.
  457. */
  458. function duplicate_post_copy_children( $new_id, $post, $status = '' ) {
  459. // Get children.
  460. $children = get_posts(
  461. array(
  462. 'post_type' => 'any',
  463. 'numberposts' => -1,
  464. 'post_status' => 'any',
  465. 'post_parent' => $post->ID,
  466. )
  467. );
  468. foreach ( $children as $child ) {
  469. if ( 'attachment' === $child->post_type ) {
  470. continue;
  471. }
  472. duplicate_post_create_duplicate( $child, $status, $new_id );
  473. }
  474. }
  475. /**
  476. * Copies comments.
  477. *
  478. * @param int $new_id The new post ID.
  479. * @param WP_Post $post The original post object.
  480. */
  481. function duplicate_post_copy_comments( $new_id, $post ) {
  482. $comments = get_comments(
  483. array(
  484. 'post_id' => $post->ID,
  485. 'order' => 'ASC',
  486. 'orderby' => 'comment_date_gmt',
  487. )
  488. );
  489. $old_id_to_new = array();
  490. foreach ( $comments as $comment ) {
  491. // Do not copy pingbacks or trackbacks.
  492. if ( $comment->comment_type === 'pingback' || $comment->comment_type === 'trackback' ) {
  493. continue;
  494. }
  495. $parent = ( $comment->comment_parent && $old_id_to_new[ $comment->comment_parent ] ) ? $old_id_to_new[ $comment->comment_parent ] : 0;
  496. $commentdata = array(
  497. 'comment_post_ID' => $new_id,
  498. 'comment_author' => $comment->comment_author,
  499. 'comment_author_email' => $comment->comment_author_email,
  500. 'comment_author_url' => $comment->comment_author_url,
  501. 'comment_content' => $comment->comment_content,
  502. 'comment_type' => $comment->comment_type,
  503. 'comment_parent' => $parent,
  504. 'user_id' => $comment->user_id,
  505. 'comment_author_IP' => $comment->comment_author_IP,
  506. 'comment_agent' => $comment->comment_agent,
  507. 'comment_karma' => $comment->comment_karma,
  508. 'comment_approved' => $comment->comment_approved,
  509. );
  510. if ( intval( get_option( 'duplicate_post_copydate' ) ) === 1 ) {
  511. $commentdata['comment_date'] = $comment->comment_date;
  512. $commentdata['comment_date_gmt'] = get_gmt_from_date( $comment->comment_date );
  513. }
  514. $new_comment_id = wp_insert_comment( $commentdata );
  515. $commentmeta = get_comment_meta( $new_comment_id );
  516. foreach ( $commentmeta as $meta_key => $meta_value ) {
  517. add_comment_meta( $new_comment_id, $meta_key, duplicate_post_wp_slash( $meta_value ) );
  518. }
  519. $old_id_to_new[ $comment->comment_ID ] = $new_comment_id;
  520. }
  521. }
  522. /**
  523. * Creates a duplicate from a post.
  524. *
  525. * This is the main functions that does the cloning.
  526. *
  527. * @param WP_Post $post The original post object.
  528. * @param string $status Optional. The intended destination status.
  529. * @param string $parent_id Optional. The parent post ID if we are calling this recursively.
  530. * @return int|WP_Error
  531. */
  532. function duplicate_post_create_duplicate( $post, $status = '', $parent_id = '' ) {
  533. /**
  534. * Fires before duplicating a post.
  535. *
  536. * @param WP_Post $post The original post object.
  537. * @param bool $status The intended destination status.
  538. * @param int $parent_id The parent post ID if we are calling this recursively.
  539. */
  540. do_action( 'duplicate_post_pre_copy', $post, $status, $parent_id );
  541. /**
  542. * Filter allowing to copy post.
  543. *
  544. * @param bool $can_duplicate Default to `true`.
  545. * @param WP_Post $post The original post object.
  546. * @param bool $status The intended destination status.
  547. * @param int $parent_id The parent post ID if we are calling this recursively.
  548. *
  549. * @return bool
  550. */
  551. $can_duplicate = apply_filters( 'duplicate_post_allow', true, $post, $status, $parent_id );
  552. if ( ! $can_duplicate ) {
  553. wp_die( esc_html( __( 'You aren\'t allowed to duplicate this post', 'duplicate-post' ) ) );
  554. }
  555. if ( ! duplicate_post_is_post_type_enabled( $post->post_type ) && 'attachment' !== $post->post_type ) {
  556. wp_die(
  557. esc_html(
  558. __( 'Copy features for this post type are not enabled in options page', 'duplicate-post' ) . ': ' .
  559. $post->post_type
  560. )
  561. );
  562. }
  563. $new_post_status = ( empty( $status ) ) ? $post->post_status : $status;
  564. $title = ' ';
  565. if ( 'attachment' !== $post->post_type ) {
  566. $prefix = sanitize_text_field( get_option( 'duplicate_post_title_prefix' ) );
  567. $suffix = sanitize_text_field( get_option( 'duplicate_post_title_suffix' ) );
  568. if ( intval( get_option( 'duplicate_post_copytitle' ) ) === 1 ) {
  569. $title = $post->post_title;
  570. if ( ! empty( $prefix ) ) {
  571. $prefix .= ' ';
  572. }
  573. if ( ! empty( $suffix ) ) {
  574. $suffix = ' ' . $suffix;
  575. }
  576. } else {
  577. $title = ' ';
  578. }
  579. $title = trim( $prefix . $title . $suffix );
  580. /*
  581. * Not sure we should force a title. Instead, we should respect what WP does.
  582. * if ( '' === $title ) {
  583. * // empty title.
  584. * $title = __( 'Untitled', 'default' );
  585. * }
  586. */
  587. if ( intval( get_option( 'duplicate_post_copystatus' ) ) === 0 ) {
  588. $new_post_status = 'draft';
  589. } else {
  590. if ( 'publish' === $new_post_status || 'future' === $new_post_status ) {
  591. // Check if the user has the right capability.
  592. if ( is_post_type_hierarchical( $post->post_type ) ) {
  593. if ( ! current_user_can( 'publish_pages' ) ) {
  594. $new_post_status = 'pending';
  595. }
  596. } else {
  597. if ( ! current_user_can( 'publish_posts' ) ) {
  598. $new_post_status = 'pending';
  599. }
  600. }
  601. }
  602. }
  603. }
  604. $new_post_author = wp_get_current_user();
  605. $new_post_author_id = $new_post_author->ID;
  606. if ( intval( get_option( 'duplicate_post_copyauthor' ) ) === 1 ) {
  607. // Check if the user has the right capability.
  608. if ( is_post_type_hierarchical( $post->post_type ) ) {
  609. if ( current_user_can( 'edit_others_pages' ) ) {
  610. $new_post_author_id = $post->post_author;
  611. }
  612. } else {
  613. if ( current_user_can( 'edit_others_posts' ) ) {
  614. $new_post_author_id = $post->post_author;
  615. }
  616. }
  617. }
  618. $menu_order = ( intval( get_option( 'duplicate_post_copymenuorder' ) ) === 1 ) ? $post->menu_order : 0;
  619. $increase_menu_order_by = get_option( 'duplicate_post_increase_menu_order_by' );
  620. if ( ! empty( $increase_menu_order_by ) && is_numeric( $increase_menu_order_by ) ) {
  621. $menu_order += intval( $increase_menu_order_by );
  622. }
  623. $post_name = $post->post_name;
  624. if ( intval( get_option( 'duplicate_post_copyslug' ) ) !== 1 ) {
  625. $post_name = '';
  626. }
  627. $new_post_parent = empty( $parent_id ) ? $post->post_parent : $parent_id;
  628. $new_post = array(
  629. 'menu_order' => $menu_order,
  630. 'comment_status' => $post->comment_status,
  631. 'ping_status' => $post->ping_status,
  632. 'post_author' => $new_post_author_id,
  633. 'post_content' => ( intval( get_option( 'duplicate_post_copycontent' ) ) === 1 ) ? $post->post_content : '',
  634. 'post_content_filtered' => ( intval( get_option( 'duplicate_post_copycontent' ) ) === 1 ) ? $post->post_content_filtered : '',
  635. 'post_excerpt' => ( intval( get_option( 'duplicate_post_copyexcerpt' ) ) === 1 ) ? $post->post_excerpt : '',
  636. 'post_mime_type' => $post->post_mime_type,
  637. 'post_parent' => $new_post_parent,
  638. 'post_password' => ( intval( get_option( 'duplicate_post_copypassword' ) ) === 1 ) ? $post->post_password : '',
  639. 'post_status' => $new_post_status,
  640. 'post_title' => $title,
  641. 'post_type' => $post->post_type,
  642. 'post_name' => $post_name,
  643. );
  644. if ( intval( get_option( 'duplicate_post_copydate' ) ) === 1 ) {
  645. $new_post_date = $post->post_date;
  646. $new_post['post_date'] = $new_post_date;
  647. $new_post['post_date_gmt'] = get_gmt_from_date( $new_post_date );
  648. }
  649. /**
  650. * Filter new post values.
  651. *
  652. * @param array $new_post New post values.
  653. * @param WP_Post $post Original post object.
  654. *
  655. * @return array
  656. */
  657. $new_post = apply_filters( 'duplicate_post_new_post', $new_post, $post );
  658. $new_post_id = wp_insert_post( wp_slash( $new_post ), true );
  659. // If you have written a plugin which uses non-WP database tables to save
  660. // information about a post you can hook this action to dupe that data.
  661. if ( 0 !== $new_post_id && ! is_wp_error( $new_post_id ) ) {
  662. if ( 'page' === $post->post_type || is_post_type_hierarchical( $post->post_type ) ) {
  663. do_action( 'dp_duplicate_page', $new_post_id, $post, $status );
  664. } else {
  665. do_action( 'dp_duplicate_post', $new_post_id, $post, $status );
  666. }
  667. delete_post_meta( $new_post_id, '_dp_original' );
  668. add_post_meta( $new_post_id, '_dp_original', $post->ID );
  669. }
  670. /**
  671. * Fires after duplicating a post.
  672. *
  673. * @param int|WP_Error $new_post_id The new post id or WP_Error object on error.
  674. * @param WP_Post $post The original post object.
  675. * @param bool $status The intended destination status.
  676. * @param int $parent_id The parent post ID if we are calling this recursively.
  677. */
  678. do_action( 'duplicate_post_post_copy', $new_post_id, $post, $status, $parent_id );
  679. return $new_post_id;
  680. }
  681. /**
  682. * Adds some links on the plugin page.
  683. *
  684. * @param array $links The links array.
  685. * @param string $file The file name.
  686. * @return array
  687. */
  688. function duplicate_post_add_plugin_links( $links, $file ) {
  689. if ( plugin_basename( dirname( __FILE__ ) . '/duplicate-post.php' ) === $file ) {
  690. $links[] = '<a href="https://yoast.com/wordpress/plugins/duplicate-post">' . esc_html__( 'Documentation', 'duplicate-post' ) . '</a>';
  691. }
  692. return $links;
  693. }
  694. /**
  695. * Renders the newsletter signup form.
  696. *
  697. * @return string The HTML of the newsletter signup form (escaped).
  698. */
  699. function duplicate_post_newsletter_signup_form() {
  700. $copy = sprintf(
  701. /* translators: 1: Yoast */
  702. __(
  703. 'If you want to stay up to date about all the exciting developments around Duplicate Post, subscribe to the %1$s newsletter!',
  704. 'duplicate-post'
  705. ),
  706. 'Yoast'
  707. );
  708. $email_label = __( 'Email Address', 'duplicate-post' );
  709. $html = '
  710. <!-- Begin Mailchimp Signup Form -->
  711. <div id="mc_embed_signup">
  712. <form action="https://yoast.us1.list-manage.com/subscribe/post?u=ffa93edfe21752c921f860358&amp;id=972f1c9122" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
  713. <div id="mc_embed_signup_scroll">
  714. ' . $copy . '
  715. <div class="mc-field-group" style="margin-top: 8px;">
  716. <label for="mce-EMAIL">' . $email_label . '</label>
  717. <input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">
  718. <input type="submit" value="' . esc_attr__( 'Subscribe', 'duplicate-post' ) . '" name="subscribe" id="mc-embedded-subscribe" class="button">
  719. </div>
  720. <div id="mce-responses" class="clear">
  721. <div class="response" id="mce-error-response" style="display:none"></div>
  722. <div class="response" id="mce-success-response" style="display:none"></div>
  723. </div> <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
  724. <div class="screen-reader-text" aria-hidden="true"><input type="text" name="b_ffa93edfe21752c921f860358_972f1c9122" tabindex="-1" value=""></div>
  725. </div>
  726. </form>
  727. </div>
  728. <!--End mc_embed_signup-->
  729. ';
  730. return $html;
  731. }