settings = Plugin::get_settings( 'posts' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( empty( $this->settings['duplicate_post_enabled'] ) ) { return; } add_action( 'admin_post_adminease_duplicate_post', [ $this, 'handle_duplicate' ] ); if( ! is_admin() ) { return; } add_filter( 'post_row_actions', [ $this, 'row_actions' ], 10, 2 ); add_filter( 'page_row_actions', [ $this, 'row_actions' ], 10, 2 ); add_action( 'admin_init', [ $this, 'register_bulk_actions' ] ); add_action( 'admin_notices', [ $this, 'admin_notices' ] ); add_filter( 'removable_query_args', static function ( $args ) { return array_merge( (array) $args, [ 'adminease_duplicated', 'adminease_duplicate_failed', 'adminease_new_post' ] ); } ); } /** * Adds the settings fields. * * @param array $fields The existing settings fields. * * @return array */ public function adminease_settings_fields( array $fields ): array { $fields['posts']['fields'][] = [ 'type' => 'switch', 'id' => 'duplicate-post-enabled', 'name' => 'adminease[posts][duplicate_post_enabled]', 'value' => $this->settings['duplicate_post_enabled'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input toggle-field', 'label' => __( 'Duplicate Posts and Pages', 'adminease' ), 'risk' => 'safe', 'description' => __( 'Adds a Duplicate link under each item in the list and a Duplicate bulk action. The copy is a draft with the same content, custom fields (including ACF and page builder data such as Elementor), featured image, page template and terms. Only people who can edit the original can copy it.', 'adminease' ), 'child_fields' => [ [ 'type' => 'select', 'id' => 'duplicate-post-post-types', 'name' => 'adminease[posts][duplicate_post_post_types][]', 'value' => $this->get_saved_post_types(), 'options' => $this->get_post_type_options(), 'label_class' => 'pwp-field-label', 'input_class' => 'pwp-field-input adminease-choices', 'wrapper_class' => 'form-group-child', 'label' => __( 'Content types that can be duplicated', 'adminease' ), 'field_description' => __( 'WooCommerce products and orders are left out: use the duplicate button WooCommerce gives products.', 'adminease' ), 'attributes' => [ 'multiple' => 'multiple', 'data-allow_clear' => true, 'data-parent' => 'duplicate-post-enabled', ], ], ], ]; return $fields; } /** * Post types the saved setting allows, defaulting to posts and pages. * * @return string[] */ private function get_saved_post_types(): array { $saved = $this->settings['duplicate_post_post_types'] ?? []; if( ! is_array( $saved ) || empty( $saved ) ) { return [ 'post', 'page' ]; } return array_values( array_map( 'strval', $saved ) ); } /** * Types that may be offered: public types with their own screen, minus media and shop types. * * @return array Post type name => label. */ private function get_post_type_options(): array { $skip = [ 'attachment', 'product', 'product_variation', 'shop_order', 'shop_order_refund', 'shop_coupon', 'shop_subscription' ]; $options = []; foreach( get_post_types( [ 'show_ui' => true ], 'objects' ) as $name => $object ) { if( ! in_array( $name, $skip, true ) && ( $object->public || in_array( $name, $this->get_saved_post_types(), true ) ) ) { $options[ $name ] = $object->labels->name ?? $name; } } return $options; } /** * Post types the feature works on right now. * * @return string[] */ private function get_active_post_types(): array { return array_values( array_map( 'strval', (array) apply_filters( 'adminease_duplicate_post_post_types', $this->get_saved_post_types() ) ) ); } /** * Tells whether the current user may duplicate a post. * * They must be able to edit the original and to create posts of that type. * * @param WP_Post $post The original. * * @return bool */ private function user_can_duplicate( WP_Post $post ): bool { $type = get_post_type_object( $post->post_type ); if( ! $type || ! in_array( $post->post_type, $this->get_active_post_types(), true ) ) { return false; } return current_user_can( 'edit_post', $post->ID ) && current_user_can( $type->cap->create_posts ); } /** * Builds the nonce-protected link that duplicates one post. * * @param int $post_id The original. * * @return string */ private function get_duplicate_url( int $post_id ): string { return wp_nonce_url( add_query_arg( [ 'action' => 'adminease_duplicate_post', 'post' => $post_id ], admin_url( 'admin-post.php' ) ), 'adminease_duplicate_post_' . $post_id ); } /** * Adds the Duplicate link to a list row. * * @param array $actions The row actions. * @param WP_Post $post The row's post. * * @return array */ public function row_actions( $actions, $post ) { if( ! is_array( $actions ) || ! $post instanceof WP_Post || 'trash' === $post->post_status || ! $this->user_can_duplicate( $post ) ) { return $actions; } $actions['adminease_duplicate'] = sprintf( '%3$s', esc_url( $this->get_duplicate_url( $post->ID ) ), /* translators: %s: the post title. */ esc_attr( sprintf( __( 'Duplicate “%s”', 'adminease' ), get_the_title( $post ) ) ), esc_html__( 'Duplicate', 'adminease' ) ); return $actions; } /** * Registers the bulk action on each list screen that can duplicate. * * @return void */ public function register_bulk_actions(): void { foreach( $this->get_active_post_types() as $post_type ) { add_filter( "bulk_actions-edit-{$post_type}", [ $this, 'add_bulk_action' ] ); add_filter( "handle_bulk_actions-edit-{$post_type}", [ $this, 'handle_bulk_action' ], 10, 3 ); } } /** * Adds Duplicate to the bulk actions menu. * * @param array $actions The bulk actions. * * @return array */ public function add_bulk_action( $actions ) { if( is_array( $actions ) ) { $actions['adminease_duplicate'] = __( 'Duplicate', 'adminease' ); } return $actions; } /** * Duplicates the ticked posts. WordPress has already checked the bulk nonce. * * @param string $redirect_to The list URL to return to. * @param string $action The chosen bulk action. * @param array $post_ids The ticked posts. * * @return string */ public function handle_bulk_action( $redirect_to, $action, $post_ids ) { if( 'adminease_duplicate' !== $action || ! is_array( $post_ids ) ) { return $redirect_to; } $redirect_to = remove_query_arg( [ 'adminease_duplicated', 'adminease_duplicate_failed' ], $redirect_to ); $done = 0; $failed = max( 0, count( $post_ids ) - self::BULK_LIMIT ); foreach( array_slice( array_map( 'absint', $post_ids ), 0, self::BULK_LIMIT ) as $post_id ) { if( $this->duplicate( $post_id ) ) { ++$done; } else { ++$failed; } } return add_query_arg( [ 'adminease_duplicated' => $done, 'adminease_duplicate_failed' => $failed ], $redirect_to ); } /** * Handles one Duplicate link. * * @return void */ public function handle_duplicate(): void { $post_id = isset( $_GET['post'] ) ? absint( wp_unslash( $_GET['post'] ) ) : 0; check_admin_referer( 'adminease_duplicate_post_' . $post_id ); $post = $post_id ? get_post( $post_id ) : null; $new_id = $this->duplicate( $post_id ); $type = $post ? $post->post_type : 'post'; $list_url = admin_url( 'post' === $type ? 'edit.php' : 'edit.php?post_type=' . rawurlencode( $type ) ); wp_safe_redirect( add_query_arg( $new_id ? [ 'adminease_duplicated' => 1, 'adminease_new_post' => $new_id ] : [ 'adminease_duplicate_failed' => 1 ], $list_url ) ); exit; } /** * Shows the result after duplicating. * * @return void */ public function admin_notices(): void { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only display of a result counter. $done = isset( $_GET['adminease_duplicated'] ) ? absint( $_GET['adminease_duplicated'] ) : 0; $failed = isset( $_GET['adminease_duplicate_failed'] ) ? absint( $_GET['adminease_duplicate_failed'] ) : 0; $new_id = isset( $_GET['adminease_new_post'] ) ? absint( $_GET['adminease_new_post'] ) : 0; // phpcs:enable if( $done > 0 ) { $link = ''; if( $new_id && current_user_can( 'edit_post', $new_id ) ) { $link = ' ' . esc_html__( 'Edit the copy', 'adminease' ) . ''; } echo '
' . esc_html( /* translators: %d: number of copies made. */ sprintf( _n( '%d copy created as a draft.', '%d copies created as drafts.', $done, 'adminease' ), $done ) ) . wp_kses( $link, [ 'a' => [ 'href' => [] ] ] ) . '
' . esc_html( /* translators: %d: number of items that could not be copied. */ sprintf( _n( '%d item could not be duplicated.', '%d items could not be duplicated.', $failed, 'adminease' ), $failed ) ) . '