Нет описания

duplicate-post-common.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Common functions.
  4. *
  5. * @package Duplicate Post
  6. * @since 2.0
  7. */
  8. use Yoast\WP\Duplicate_Post\Permissions_Helper;
  9. use Yoast\WP\Duplicate_Post\UI\Link_Builder;
  10. use Yoast\WP\Duplicate_Post\Utils;
  11. /**
  12. * Tests if post type is enabled to be copied.
  13. *
  14. * @param string $post_type The post type to check.
  15. * @return bool
  16. */
  17. function duplicate_post_is_post_type_enabled( $post_type ) {
  18. $duplicate_post_types_enabled = get_option( 'duplicate_post_types_enabled', array( 'post', 'page' ) );
  19. if ( ! is_array( $duplicate_post_types_enabled ) ) {
  20. $duplicate_post_types_enabled = array( $duplicate_post_types_enabled );
  21. }
  22. /** This filter is documented in src/class-permissions-helper.php */
  23. $duplicate_post_types_enabled = apply_filters( 'duplicate_post_enabled_post_types', $duplicate_post_types_enabled );
  24. return in_array( $post_type, $duplicate_post_types_enabled, true );
  25. }
  26. /**
  27. * Template tag to retrieve/display duplicate post link for post.
  28. *
  29. * @param int $id Optional. Post ID.
  30. * @param string $context Optional, default to display. How to write the '&', defaults to '&amp;'.
  31. * @param bool $draft Optional, default to true.
  32. * @return string
  33. */
  34. function duplicate_post_get_clone_post_link( $id = 0, $context = 'display', $draft = true ) {
  35. $post = get_post( $id );
  36. if ( ! $post ) {
  37. return '';
  38. }
  39. $link_builder = new Link_Builder();
  40. $permissions_helper = new Permissions_Helper();
  41. if ( ! $permissions_helper->should_links_be_displayed( $post ) ) {
  42. return '';
  43. }
  44. if ( $draft ) {
  45. return $link_builder->build_new_draft_link( $post, $context );
  46. } else {
  47. return $link_builder->build_clone_link( $post, $context );
  48. }
  49. }
  50. /**
  51. * Displays duplicate post link for post.
  52. *
  53. * @param string|null $link Optional. Anchor text.
  54. * @param string $before Optional. Display before edit link.
  55. * @param string $after Optional. Display after edit link.
  56. * @param int $id Optional. Post ID.
  57. */
  58. function duplicate_post_clone_post_link( $link = null, $before = '', $after = '', $id = 0 ) {
  59. $post = get_post( $id );
  60. if ( ! $post ) {
  61. return;
  62. }
  63. $url = duplicate_post_get_clone_post_link( $post->ID );
  64. if ( ! $url ) {
  65. return;
  66. }
  67. if ( null === $link ) {
  68. $link = esc_html__( 'Copy to a new draft', 'duplicate-post' );
  69. }
  70. $link = '<a class="post-clone-link" href="' . esc_url( $url ) . '">' . $link . '</a>';
  71. /**
  72. * Filter on the clone link HTML.
  73. *
  74. * @param string $link The full HTML tag of the link.
  75. * @param int $ID The ID of the post.
  76. *
  77. * @return string
  78. */
  79. echo $before . apply_filters( 'duplicate_post_clone_post_link', $link, $post->ID ) . $after; // phpcs:ignore WordPress.Security.EscapeOutput
  80. }
  81. /**
  82. * Gets the original post.
  83. *
  84. * @param int|null $post Optional. Post ID or Post object.
  85. * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
  86. * @return mixed Post data.
  87. */
  88. function duplicate_post_get_original( $post = null, $output = OBJECT ) {
  89. return Utils::get_original( $post, $output );
  90. }