post_type, self::types(), true ) || 'auto-draft' === $post->post_status || 'trash' === $post->post_status ) { 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 ); } public static function link( $post, $to = 'list' ) { return wp_nonce_url( admin_url( 'admin-post.php?action=' . self::ACTION . '&post=' . (int) $post->ID . '&to=' . $to ), self::ACTION ); } public static function row_action( $actions, $post ) { if ( self::can( $post ) ) { $actions['svabg_duplicate'] = sprintf( '%3$s', esc_url( self::link( $post ) ), esc_attr( sprintf( /* translators: %s: post title. */ __( 'Duplicate "%s"', 'ai-guardwall' ), get_the_title( $post ) ) ), esc_html__( 'Duplicate', 'ai-guardwall' ) ); } return $actions; } public static function admin_bar( $bar ) { $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 $post = get_post( (int) $_GET['post'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended } } elseif ( is_singular() ) { $post = get_queried_object(); } if ( $post instanceof WP_Post && self::can( $post ) ) { $bar->add_node( array( 'id' => 'svabg-duplicate', 'title' => '' . esc_html__( 'Duplicate this', 'ai-guardwall' ) . '', 'href' => self::link( $post, 'edit' ), ) ); } } public static function register_bulk() { foreach ( self::types() as $type ) { add_filter( 'bulk_actions-edit-' . $type, function ( $actions ) { $actions[ Softvolt_Ai_Bodyguard_Duplicate::ACTION ] = __( 'Duplicate', 'ai-guardwall' ); return $actions; } ); add_filter( 'handle_bulk_actions-edit-' . $type, array( __CLASS__, 'handle_bulk' ), 10, 3 ); } } public static function handle_bulk( $redirect, $action, $ids ) { if ( self::ACTION !== $action ) { return $redirect; } $done = 0; foreach ( (array) $ids as $id ) { if ( self::can( $id ) && ! is_wp_error( self::duplicate( $id ) ) ) { $done++; } } self::remember( $done, 0 ); return $redirect; } /** * What the next screen tells the user who duplicated something. It is kept for this user only, so the * notice reads nothing from the address (which anybody could make up). * * @since 1.5.1 * @param int $done Copies made. * @param int $new The copy to link to (0 for none). */ private static function remember( $done, $new ) { if ( $done > 0 ) { set_transient( 'svabg_duplicated_' . get_current_user_id(), array( 'done' => (int) $done, 'new' => (int) $new ), 5 * MINUTE_IN_SECONDS ); } } public static function handle() { // The nonce first; the post is read only after it passed, then the capability for that post. check_admin_referer( self::ACTION ); $id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0; if ( ! $id || ! self::can( $id ) ) { wp_die( esc_html__( 'You are not allowed to duplicate this item.', 'ai-guardwall' ), 403 ); } $new = self::duplicate( $id ); if ( is_wp_error( $new ) ) { wp_die( esc_html( $new->get_error_message() ) ); } $to = isset( $_GET['to'] ) && 'edit' === sanitize_key( wp_unslash( $_GET['to'] ) ) ? 'edit' : 'list'; if ( 'edit' === $to ) { wp_safe_redirect( get_edit_post_link( $new, 'raw' ) ); exit; } $post = get_post( $id ); $back = wp_get_referer() ? wp_get_referer() : admin_url( 'edit.php?post_type=' . $post->post_type ); self::remember( 1, $new ); wp_safe_redirect( $back ); exit; } /** * Copy a post. * * @param int $id Original. * @return int|WP_Error New post id. */ public static function duplicate( $id ) { global $wpdb; $post = get_post( $id ); if ( ! $post ) { return new WP_Error( 'svabg_duplicate', __( 'The item no longer exists.', 'ai-guardwall' ) ); } $status = (string) Softvolt_Ai_Bodyguard_Modules::get( 'duplicate_status' ); if ( 'same' === $status ) { $status = in_array( $post->post_status, array( 'publish', 'future' ), true ) && ! current_user_can( get_post_type_object( $post->post_type )->cap->publish_posts ) ? 'pending' : $post->post_status; } $suffix = (string) Softvolt_Ai_Bodyguard_Modules::get( 'duplicate_suffix' ); if ( '' === $suffix ) { $suffix = __( '(Copy)', 'ai-guardwall' ); } $data = array( 'post_type' => $post->post_type, 'post_title' => trim( $post->post_title . ' ' . $suffix ), 'post_content' => $post->post_content, 'post_excerpt' => $post->post_excerpt, 'post_status' => $status, 'post_author' => get_current_user_id(), '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, 'post_mime_type' => $post->post_mime_type, ); // Keep the content exactly as it is (scripts, shortcodes, block markup). $kses = has_filter( 'content_save_pre', 'wp_filter_post_kses' ); if ( $kses && current_user_can( 'unfiltered_html' ) ) { kses_remove_filters(); } $new = wp_insert_post( wp_slash( $data ), true ); if ( $kses && current_user_can( 'unfiltered_html' ) ) { kses_init_filters(); } if ( is_wp_error( $new ) ) { return $new; } foreach ( get_object_taxonomies( $post->post_type ) as $tax ) { if ( 'post_format' === $tax || 'post_translations' === $tax ) { if ( 'post_format' === $tax ) { set_post_format( $new, get_post_format( $post ) ); } continue; } $terms = wp_get_object_terms( $post->ID, $tax, array( 'fields' => 'ids' ) ); if ( ! is_wp_error( $terms ) && $terms ) { wp_set_object_terms( $new, $terms, $tax ); } } $meta = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d ORDER BY meta_id", $post->ID ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery foreach ( (array) $meta as $m ) { if ( in_array( $m->meta_key, self::SKIP_META, true ) ) { continue; } add_post_meta( $new, $m->meta_key, wp_slash( maybe_unserialize( $m->meta_value ) ) ); } add_post_meta( $new, '_svabg_duplicate_of', $post->ID ); /** * A post was duplicated. * * @since 1.2.0 * @param int $new The copy. * @param WP_Post $post The original. */ do_action( 'svabg_post_duplicated', $new, $post ); Softvolt_Ai_Bodyguard_Logger::log( 'info', 'post_duplicated', sprintf( /* translators: 1: title, 2: new id. */ __( 'Duplicated "%1$s" as #%2$d', 'ai-guardwall' ), $post->post_title, $new ), array( 'original' => $post->ID, 'copy' => $new ) ); return (int) $new; } public static function notice() { $key = 'svabg_duplicated_' . get_current_user_id(); $note = get_transient( $key ); if ( ! is_array( $note ) || empty( $note['done'] ) ) { return; } delete_transient( $key ); $n = (int) $note['done']; $new = isset( $note['new'] ) ? (int) $note['new'] : 0; $text = sprintf( /* translators: %d: number of copies. */ _n( '%d item duplicated.', '%d items duplicated.', $n, 'ai-guardwall' ), $n ); echo '
' . esc_html( $text ); if ( $new && get_post( $new ) && current_user_can( 'edit_post', $new ) ) { echo ' ' . esc_html( sprintf( /* translators: %s: title. */ __( 'Edit "%s"', 'ai-guardwall' ), get_the_title( $new ) ) ) . ''; } echo '