slug => label. */ public static function post_type_choices(): array { $skip = array( 'attachment', 'product', 'wp_block', 'wp_template', 'wp_template_part', 'wp_global_styles', 'wp_navigation', 'wp_font_family', 'wp_font_face', 'shop_order', 'shop_coupon' ); $choices = array(); foreach ( get_post_types( array( 'show_ui' => true ), 'objects' ) as $type ) { if ( ! in_array( $type->name, $skip, true ) ) { $choices[ $type->name ] = $type->labels->name; } } return $choices; } /** * Whether the current user may copy a post. * * @param WP_Post $post Post. * @return bool */ public static function can_copy( WP_Post $post ): bool { $type = get_post_type_object( $post->post_type ); return $type && in_array( $post->post_type, self::post_types(), true ) && ! in_array( $post->post_status, array( 'auto-draft', 'trash', 'inherit' ), true ) && current_user_can( 'edit_post', $post->ID ) && current_user_can( $type->cap->create_posts ); } /** * The link that copies one post. * * @param int $post_id Post ID. * @return string */ public static function url( int $post_id ): string { return wp_nonce_url( admin_url( 'admin-post.php?action=' . self::ACTION . '&post=' . $post_id ), self::ACTION . '_' . $post_id ); } /** * "Duplicate" in the list table's row actions. * * @param array $actions Row actions. * @param WP_Post $post Post. * @return array */ public function row_action( $actions, $post ) { if ( $post instanceof WP_Post && self::can_copy( $post ) ) { $actions['aiou_duplicate'] = sprintf( '%3$s', esc_url( self::url( $post->ID ) ), /* translators: %s: post title */ esc_attr( sprintf( __( 'Duplicate "%s"', 'all-in-one-utilities' ), get_the_title( $post ) ) ), esc_html__( 'Duplicate', 'all-in-one-utilities' ) ); } return $actions; } /** * "Duplicate" in the Bulk actions menu of each enabled post type. * * @return void */ public function register_bulk_actions(): void { foreach ( self::post_types() as $post_type ) { add_filter( 'bulk_actions-edit-' . $post_type, array( $this, 'bulk_action' ) ); add_filter( 'handle_bulk_actions-edit-' . $post_type, array( $this, 'handle_bulk' ), 10, 3 ); } } /** * Add the bulk action. * * @param array $actions Bulk actions. * @return array */ public function bulk_action( $actions ) { $actions[ self::BULK ] = __( 'Duplicate', 'all-in-one-utilities' ); return $actions; } /** * Copy the selected posts. * * @param string $redirect Where to go next. * @param string $action Chosen bulk action. * @param int[] $post_ids Selected posts. * @return string */ public function handle_bulk( $redirect, $action, $post_ids ) { if ( self::BULK !== $action ) { return $redirect; } $copied = 0; foreach ( array_map( 'intval', (array) $post_ids ) as $post_id ) { $post = get_post( $post_id ); if ( $post && self::can_copy( $post ) && ! is_wp_error( self::copy( $post ) ) ) { ++$copied; } } return add_query_arg( 'aiou_duplicated', $copied, remove_query_arg( 'aiou_duplicated', $redirect ) ); } /** * Copy one post (row action / toolbar link). * * @return void */ public function handle_single(): void { $post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0; check_admin_referer( self::ACTION . '_' . $post_id ); $post = get_post( $post_id ); if ( ! $post || ! self::can_copy( $post ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to duplicate this item.', 'all-in-one-utilities' ), '', array( 'response' => 403 ) ); } $copy = self::copy( $post ); if ( is_wp_error( $copy ) ) { wp_die( esc_html( $copy->get_error_message() ) ); } if ( aiou_get_setting( 'content', self::SECTION, 'open_editor', 0 ) ) { wp_safe_redirect( (string) get_edit_post_link( $copy, 'url' ) ); exit; } $back = wp_get_referer(); $back = $back && ! str_contains( $back, 'admin-post.php' ) ? $back : admin_url( 'edit.php?post_type=' . $post->post_type ); wp_safe_redirect( add_query_arg( 'aiou_duplicated', 1, remove_query_arg( 'aiou_duplicated', $back ) ) ); exit; } /** * Make the copy. * * @param WP_Post $post Original. * @return int|WP_Error New post ID. */ public static function copy( WP_Post $post ) { $suffix = trim( (string) aiou_get_setting( 'content', self::SECTION, 'title_suffix', __( '(Copy)', 'all-in-one-utilities' ) ) ); $status = (string) aiou_get_setting( 'content', self::SECTION, 'status', 'draft' ); $type = get_post_type_object( $post->post_type ); if ( 'same' === $status ) { $status = $post->post_status; } // Never publish (or schedule) a copy the user couldn't publish // themselves; scheduled originals become drafts. if ( in_array( $status, array( 'publish', 'future', 'private' ), true ) && ( ! $type || ! current_user_can( $type->cap->publish_posts ) ) ) { $status = 'pending'; } if ( 'future' === $status ) { $status = 'draft'; } $copy_id = wp_insert_post( wp_slash( array( 'post_type' => $post->post_type, 'post_title' => trim( $post->post_title . ( '' !== $suffix ? ' ' . $suffix : '' ) ), 'post_content' => $post->post_content, 'post_excerpt' => $post->post_excerpt, 'post_status' => in_array( $status, array( 'draft', 'pending', 'private', 'publish' ), true ) ? $status : 'draft', 'post_author' => get_current_user_id(), 'post_parent' => $post->post_parent, 'menu_order' => $post->menu_order, 'post_password' => $post->post_password, 'comment_status' => $post->comment_status, 'ping_status' => $post->ping_status, 'post_mime_type' => $post->post_mime_type, ) ), true ); if ( is_wp_error( $copy_id ) ) { return $copy_id; } foreach ( get_object_taxonomies( $post->post_type ) as $taxonomy ) { $terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); if ( ! is_wp_error( $terms ) && $terms ) { wp_set_object_terms( $copy_id, array_map( 'intval', $terms ), $taxonomy ); } } foreach ( (array) get_post_meta( $post->ID ) as $key => $values ) { if ( in_array( $key, self::SKIP_META, true ) || str_starts_with( (string) $key, '_wp_trash' ) ) { continue; } foreach ( (array) $values as $value ) { add_post_meta( $copy_id, (string) $key, wp_slash( maybe_unserialize( $value ) ) ); } } update_post_meta( $copy_id, '_aiou_duplicated_from', $post->ID ); /** * Fires after a post has been copied. * * @since 2.0.0 * * @param int $copy_id New post ID. * @param WP_Post $post Original. */ do_action( 'aiou_post_duplicated', $copy_id, $post ); return $copy_id; } /** * "Duplicate this" in the toolbar, on the edit screen and when viewing * a single post. * * @param WP_Admin_Bar $wp_admin_bar Toolbar. * @return void */ public function toolbar( $wp_admin_bar ): void { $post = null; if ( is_admin() ) { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( $screen && 'post' === $screen->base && isset( $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only: which post is being edited. $post = get_post( absint( $_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended } } elseif ( is_singular() ) { $post = get_queried_object(); } if ( ! $post instanceof WP_Post || ! self::can_copy( $post ) || ! $wp_admin_bar instanceof WP_Admin_Bar ) { return; } $wp_admin_bar->add_node( array( 'id' => 'aiou-duplicate', 'title' => '' . esc_html__( 'Duplicate', 'all-in-one-utilities' ) . '', 'href' => self::url( $post->ID ), ) ); } /** * A "Copy to a new draft" link in the classic editor's Publish box. * * @param WP_Post $post Post being edited. * @return void */ public function classic_editor_link( $post ): void { if ( ! $post instanceof WP_Post || ! self::can_copy( $post ) ) { return; } printf( '', esc_url( self::url( $post->ID ) ), esc_html__( 'Duplicate', 'all-in-one-utilities' ) ); } /** * "N items duplicated." after copying. * * @return void */ public function notice(): void { if ( ! isset( $_GET['aiou_duplicated'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display only. return; } $count = absint( $_GET['aiou_duplicated'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display only. printf( '

%s

', esc_html( sprintf( /* translators: %d: number of copies made */ _n( '%d item duplicated.', '%d items duplicated.', $count, 'all-in-one-utilities' ), $count ) ) ); } }