plugin = $plugin; } public function register() { add_action( 'wp_enqueue_scripts', array( $this, 'admin_bar_styles' ), 100 ); add_filter( 'post_row_actions', array( $this, 'duplicate_action' ), 20, 2 ); add_filter( 'page_row_actions', array( $this, 'duplicate_action' ), 20, 2 ); add_action( 'admin_post_divi_powers_duplicate_content', array( $this, 'duplicate_content' ) ); } /** Keep the toolbar available while reclaiming its permanent screen space. */ public function admin_bar_styles() { if ( ! is_user_logged_in() || ! is_admin_bar_showing() || ! $this->plugin->is_power_enabled( 'admin_bar_auto_hide' ) ) { return; } wp_register_style( 'divi-powers-admin-bar-auto-hide', false, array(), DIVI_POWERS_VERSION ); wp_enqueue_style( 'divi-powers-admin-bar-auto-hide' ); wp_add_inline_style( 'divi-powers-admin-bar-auto-hide', 'html{margin-top:0!important}#wpadminbar{position:fixed!important;transform:translateY(calc(-100% + 5px));transition:transform .2s ease;will-change:transform}#wpadminbar:hover,#wpadminbar:focus-within{transform:translateY(0)}body.admin-bar .et-l--header .et_pb_sticky--top{top:0!important}@media (prefers-reduced-motion:reduce){#wpadminbar{transition:none}}' ); } /** Add a protected Duplicate action to every editable public post type. */ public function duplicate_action( $actions, $post ) { if ( ! $this->plugin->is_power_enabled( 'duplicate_content' ) || ! $post instanceof WP_Post || ! current_user_can( 'edit_post', $post->ID ) || ! $this->is_supported_post_type( $post->post_type ) ) { return $actions; } $url = wp_nonce_url( admin_url( 'admin-post.php?action=divi_powers_duplicate_content&post=' . absint( $post->ID ) ), 'divi_powers_duplicate_content_' . absint( $post->ID ) ); $actions['divi_powers_duplicate'] = '' . esc_html__( 'Duplicate', '5d-powers-for-divi' ) . ''; return $actions; } /** Create an editable draft while preserving content, taxonomies, and safe metadata. */ public function duplicate_content() { $post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0; check_admin_referer( 'divi_powers_duplicate_content_' . $post_id ); $source = get_post( $post_id ); if ( ! $this->plugin->is_power_enabled( 'duplicate_content' ) || ! $source || ! current_user_can( 'edit_post', $post_id ) || ! $this->is_supported_post_type( $source->post_type ) ) { wp_die( esc_html__( 'You are not allowed to duplicate this content.', '5d-powers-for-divi' ), '', array( 'response' => 403 ) ); } $new_id = wp_insert_post( array( 'post_type' => $source->post_type, 'post_status' => 'draft', 'post_author' => get_current_user_id(), 'post_title' => sprintf( /* translators: %s: original page title. */ __( '%s — Copy', '5d-powers-for-divi' ), $source->post_title ), 'post_content' => wp_slash( $source->post_content ), 'post_excerpt' => wp_slash( $source->post_excerpt ), 'post_parent' => $source->post_parent, 'menu_order' => $source->menu_order, 'comment_status' => $source->comment_status, 'ping_status' => $source->ping_status, ), true ); if ( is_wp_error( $new_id ) ) { wp_die( esc_html( $new_id->get_error_message() ) ); } $skip_meta = array( '_edit_lock', '_edit_last', '_wp_old_slug' ); foreach ( get_post_meta( $post_id ) as $key => $values ) { if ( in_array( $key, $skip_meta, true ) ) { continue; } foreach ( $values as $value ) { add_post_meta( $new_id, $key, maybe_unserialize( $value ) ); } } foreach ( get_object_taxonomies( $source->post_type ) as $taxonomy ) { $terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) ); if ( ! is_wp_error( $terms ) ) { wp_set_object_terms( $new_id, $terms, $taxonomy, false ); } } wp_safe_redirect( admin_url( 'post.php?post=' . absint( $new_id ) . '&action=edit' ) ); exit; } private function is_supported_post_type( $post_type ) { $object = get_post_type_object( $post_type ); return $object && ! empty( $object->public ) && ! in_array( $post_type, array( 'attachment', 'revision', 'nav_menu_item' ), true ); } }