show_ui ) { return false; } $excluded = array( 'attachment', 'revision', 'nav_menu_item', 'custom_css', 'customize_changeset', 'oembed_cache', 'user_request', 'wp_block', 'wp_template', 'wp_template_part', 'wp_global_styles', 'wp_navigation', 'wp_font_family', 'wp_font_face', ); /** * Filter post types excluded from duplication. * * @param string[] $excluded Excluded post type slugs. */ $excluded = (array) apply_filters( 'draftling_excluded_post_types', $excluded ); return ! in_array( $post_type, $excluded, true ); } /** * Duplicate one content item. * * @param WP_Post $post Original post. * @return int|WP_Error New post ID or error. */ public static function duplicate( WP_Post $post ) { $new_post_args = array( 'post_author' => get_current_user_id(), 'post_content' => $post->post_content, 'post_title' => $post->post_title, 'post_excerpt' => $post->post_excerpt, 'post_status' => 'draft', 'post_type' => $post->post_type, 'post_parent' => $post->post_parent, 'menu_order' => $post->menu_order, 'comment_status' => $post->comment_status, 'ping_status' => $post->ping_status, 'post_password' => $post->post_password, 'to_ping' => $post->to_ping, 'post_content_filtered' => $post->post_content_filtered, ); /** * Filter the new post data before insertion. * * @param array $new_post_args New post arguments. * @param WP_Post $post Original post. */ $new_post_args = (array) apply_filters( 'draftling_duplicate_post_args', $new_post_args, $post ); $new_post_id = wp_insert_post( wp_slash( $new_post_args ), true ); if ( is_wp_error( $new_post_id ) ) { return $new_post_id; } self::copy_taxonomies( $post->ID, $new_post_id, $post->post_type ); self::copy_meta( $post->ID, $new_post_id ); clean_post_cache( $new_post_id ); /** * Fires after an item and its terms and metadata are duplicated. * * @param int $new_post_id New post ID. * @param WP_Post $post Original post. */ do_action( 'draftling_content_duplicated', $new_post_id, $post ); return $new_post_id; } /** * Copy all taxonomy relationships using term IDs. * * @param int $source_id Source post ID. * @param int $target_id Target post ID. * @param string $post_type Post type slug. */ private static function copy_taxonomies( $source_id, $target_id, $post_type ) { $taxonomies = get_object_taxonomies( $post_type, 'names' ); foreach ( $taxonomies as $taxonomy ) { $term_ids = wp_get_object_terms( $source_id, $taxonomy, array( 'fields' => 'ids' ) ); if ( is_wp_error( $term_ids ) || empty( $term_ids ) ) { continue; } wp_set_object_terms( $target_id, array_map( 'intval', $term_ids ), $taxonomy, false ); } } /** * Copy all allowed post metadata. * * @param int $source_id Source post ID. * @param int $target_id Target post ID. */ private static function copy_meta( $source_id, $target_id ) { $excluded = array( '_edit_lock', '_edit_last', '_wp_old_slug', '_wp_trash_meta_status', '_wp_trash_meta_time', '_wp_desired_post_slug', ); /** * Filter metadata keys excluded from duplication. * * @param string[] $excluded Excluded metadata keys. */ $excluded = (array) apply_filters( 'draftling_excluded_meta_keys', $excluded ); $all_meta = get_post_meta( $source_id ); foreach ( $all_meta as $meta_key => $meta_values ) { if ( in_array( $meta_key, $excluded, true ) ) { continue; } foreach ( $meta_values as $meta_value ) { add_post_meta( $target_id, $meta_key, wp_slash( maybe_unserialize( $meta_value ) ) ); } } } }