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