array( 'post', 'page', 'product', 'elementor_library' ), 'status' => 'draft', 'suffix' => ' (Copy)', 'author' => 'current', 'redirect' => 'edit', ); } /** * Settings fields. * * @return array */ public function get_fields() { return array( 'post_types' => array( 'type' => 'checkboxes', 'label' => __( 'Enable for', 'toolvane' ), 'options' => $this->post_type_options( array( 'acf-field-group', 'acf-field', 'acf-post-type', 'acf-taxonomy', 'acf-ui-options-page' ) ), ), 'status' => array( 'type' => 'select', 'label' => __( 'Status of the copy', 'toolvane' ), 'options' => array( 'draft' => __( 'Draft', 'toolvane' ), 'pending' => __( 'Pending review', 'toolvane' ), 'private' => __( 'Private', 'toolvane' ), 'same' => __( 'Same as the original', 'toolvane' ), ), 'description' => __( 'Draft is recommended so search engines never see duplicate content. Users who cannot publish always get a draft.', 'toolvane' ), ), 'suffix' => array( 'type' => 'text', 'label' => __( 'Title suffix', 'toolvane' ), 'description' => __( 'Appended to the title of the copy. Leave empty for none.', 'toolvane' ), ), 'author' => array( 'type' => 'select', 'label' => __( 'Author of the copy', 'toolvane' ), 'options' => array( 'current' => __( 'The user who duplicates', 'toolvane' ), 'original' => __( 'Keep the original author', 'toolvane' ), ), ), 'redirect' => array( 'type' => 'select', 'label' => __( 'After duplicating', 'toolvane' ), 'options' => array( 'edit' => __( 'Open the copy in the editor', 'toolvane' ), 'list' => __( 'Stay on the list', 'toolvane' ), ), ), ); } /** * Registers hooks. */ public function init() { add_filter( 'post_row_actions', array( $this, 'row_action' ), 10, 2 ); add_filter( 'page_row_actions', array( $this, 'row_action' ), 10, 2 ); add_action( 'admin_bar_menu', array( $this, 'admin_bar' ), 90 ); add_action( 'admin_action_' . self::ACTION, array( $this, 'handle' ) ); add_action( 'admin_notices', array( $this, 'notice' ) ); add_action( 'post_submitbox_misc_actions', array( $this, 'classic_editor_link' ) ); add_action( 'enqueue_block_editor_assets', array( $this, 'block_editor_assets' ) ); add_action( 'admin_init', array( $this, 'register_bulk_actions' ) ); add_filter( 'removable_query_args', array( $this, 'removable_query_args' ) ); } /** * Post types the module is enabled for. * * @return string[] */ private function enabled_types() { return array_map( 'strval', (array) $this->get( 'post_types', array() ) ); } /** * Whether the current user may duplicate a post. * * @param mixed $post Post. * @return bool */ private function can_duplicate( $post ) { if ( ! $post instanceof \WP_Post || 'auto-draft' === $post->post_status || ! in_array( $post->post_type, $this->enabled_types(), true ) ) { return false; } $type = get_post_type_object( $post->post_type ); return $type && current_user_can( 'edit_post', $post->ID ) && current_user_can( $type->cap->create_posts ); } /** * Duplicate link URL. * * @param int $post_id Post ID. * @return string */ private function link( $post_id ) { return wp_nonce_url( add_query_arg( array( 'action' => self::ACTION, 'post' => $post_id, ), admin_url( 'admin.php' ) ), self::ACTION . '_' . $post_id ); } /** * Adds a "Duplicate" row action. * * @param array $actions Row actions. * @param \WP_Post $post Post. * @return array */ public function row_action( $actions, $post ) { // WooCommerce already adds its own "Duplicate" link for products. if ( 'product' === $post->post_type && class_exists( 'WooCommerce' ) ) { return $actions; } if ( 'trash' !== $post->post_status && $this->can_duplicate( $post ) ) { $actions['toolvane_duplicate'] = sprintf( '%3$s', esc_url( $this->link( $post->ID ) ), /* translators: %s: post title */ esc_attr( sprintf( __( 'Duplicate “%s”', 'toolvane' ), _draft_or_post_title( $post ) ) ), esc_html__( 'Duplicate', 'toolvane' ) ); } return $actions; } /** * Adds "Duplicate" to the admin bar on the front end and on Classic edit screens. * * @param \WP_Admin_Bar $bar Admin bar. */ public function admin_bar( $bar ) { $post = null; if ( is_admin() ) { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( $screen && 'post' === $screen->base ) { $post = get_post(); } } elseif ( is_singular() ) { $post = get_queried_object(); } elseif ( function_exists( 'is_shop' ) && is_shop() ) { $post = get_post( wc_get_page_id( 'shop' ) ); } elseif ( is_home() && get_option( 'page_for_posts' ) ) { $post = get_post( (int) get_option( 'page_for_posts' ) ); } if ( $post && $this->can_duplicate( $post ) ) { $bar->add_node( array( 'id' => 'toolvane-duplicate', 'title' => '' . esc_html__( 'Duplicate', 'toolvane' ) . '', 'href' => $this->link( $post->ID ), ) ); } } /** * "Copy to a new draft" link in the Classic editor publish box. * * @param \WP_Post $post Post being edited. */ public function classic_editor_link( $post ) { if ( ! $this->can_duplicate( $post ) || 'trash' === $post->post_status ) { return; } printf( '
%2$s
', esc_url( $this->link( $post->ID ) ), esc_html__( 'Duplicate this item', 'toolvane' ) ); } /** * Adds a "Duplicate" button to the block editor sidebar. */ public function block_editor_assets() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || 'post' !== $screen->base ) { return; } $post = get_post(); if ( ! $post || ! $this->can_duplicate( $post ) ) { return; } wp_enqueue_script( 'toolvane-duplicate-editor', TOOLVANE_URL . 'assets/js/duplicate-editor.js', array( 'wp-plugins', 'wp-element', 'wp-components', 'wp-data', 'wp-i18n', 'wp-dom-ready', 'wp-editor', 'wp-edit-post' ), TOOLVANE_VERSION, true ); wp_set_script_translations( 'toolvane-duplicate-editor', 'toolvane' ); wp_localize_script( 'toolvane-duplicate-editor', 'toolvaneDuplicate', array( 'url' => $this->link( $post->ID ), // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Display only. 'duplicated' => isset( $_GET['toolvane_duplicated'] ), ) ); } /** * Registers the bulk action for every enabled post type. */ public function register_bulk_actions() { foreach ( $this->enabled_types() as $type ) { add_filter( 'bulk_actions-edit-' . $type, array( $this, 'add_bulk_action' ) ); add_filter( 'handle_bulk_actions-edit-' . $type, array( $this, 'handle_bulk_action' ), 10, 3 ); } } /** * Adds "Duplicate" to the bulk actions dropdown. * * @param array $actions Bulk actions. * @return array */ public function add_bulk_action( $actions ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only screen check. $status = isset( $_GET['post_status'] ) ? sanitize_key( wp_unslash( $_GET['post_status'] ) ) : ''; if ( 'trash' !== $status ) { $actions[ self::BULK_ACTION ] = __( 'Duplicate', 'toolvane' ); } return $actions; } /** * Handles the bulk action. WordPress verifies the bulk-posts nonce before this filter runs. * * @param string $redirect Redirect URL. * @param string $action Action name. * @param int[] $post_ids Selected IDs. * @return string */ public function handle_bulk_action( $redirect, $action, $post_ids ) { if ( self::BULK_ACTION !== $action ) { return $redirect; } $count = 0; foreach ( array_slice( array_map( 'absint', (array) $post_ids ), 0, 100 ) as $post_id ) { $post = get_post( $post_id ); if ( ! $this->can_duplicate( $post ) ) { continue; } $new_id = Duplicator::duplicate( $post, $this->copy_args() ); if ( ! is_wp_error( $new_id ) && $new_id ) { ++$count; } } return add_query_arg( 'toolvane_duplicated', $count, remove_query_arg( 'toolvane_duplicated', $redirect ) ); } /** * Options passed to the duplicator. * * @return array */ private function copy_args() { return array( 'status' => (string) $this->get( 'status', 'draft' ), 'suffix' => (string) $this->get( 'suffix', '' ), 'author' => (string) $this->get( 'author', 'current' ), ); } /** * Handles a single duplicate request. */ public function handle() { $post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0; check_admin_referer( self::ACTION . '_' . $post_id ); $post = get_post( $post_id ); if ( ! $this->can_duplicate( $post ) ) { wp_die( esc_html__( 'You are not allowed to duplicate this item.', 'toolvane' ), '', array( 'response' => 403 ) ); } $new_id = Duplicator::duplicate( $post, $this->copy_args() ); if ( is_wp_error( $new_id ) ) { wp_die( esc_html( $new_id->get_error_message() ) ); } if ( 'list' === $this->get( 'redirect' ) ) { $target = add_query_arg( 'post_type', $post->post_type, admin_url( 'edit.php' ) ); } else { $target = get_edit_post_link( $new_id, 'raw' ); } if ( ! $target ) { $target = admin_url( 'edit.php?post_type=' . $post->post_type ); } wp_safe_redirect( add_query_arg( 'toolvane_duplicated', 1, $target ) ); exit; } /** * Success notice on list and Classic edit screens. */ public function notice() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Display only. if ( ! isset( $_GET['toolvane_duplicated'] ) ) { return; } $screen = get_current_screen(); if ( $screen && method_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor() ) { return; // The block editor shows its own notice. } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Display only. $count = absint( $_GET['toolvane_duplicated'] ); if ( $screen && 'post' === $screen->base ) { $message = __( 'Item duplicated. You are now editing the copy.', 'toolvane' ); } else { /* translators: %s: number of duplicated items */ $message = sprintf( _n( '%s item duplicated.', '%s items duplicated.', $count, 'toolvane' ), number_format_i18n( $count ) ); } echo '

' . esc_html( $message ) . '

'; } /** * Removes the notice flag from the URL after the page loaded. * * @param string[] $args Removable query args. * @return string[] */ public function removable_query_args( $args ) { $args[] = 'toolvane_duplicated'; return $args; } }