如何限制 Admin and Site Enhancements (ASE) 在特定的 Post Type 不顯示?

Admin and Site Enhancements (ASE) 是一個很好用的後台管理介面全方位外掛,當中有一個功能 Content Duplicate 可以讓你複製 Post,這個功能是許多人都會需要的。

只是這個功能在大多數的情況雖然可以運作,但是對於複雜類型的 Post Type 便可能無法正常運作。因為一個 Post 被製作出來的過程可能被該外掛作者客製化過不少過程,這是單純複製無法複製成功的。例如 ASE 的程式碼中就自然的跳過了 Woocommerce 的 Product。

// class-content-management.php: line 100
    public function add_duplication_action_link( $actions, $post )
    {
        $post_type = $post->post_type;
        if ( current_user_can( 'edit_posts' ) ) {
            // Not WooCommerce product
            if ( 'product' != $post_type ) {
                $actions['asenha-duplicate'] = '<a href="admin.php?action=asenha_enable_duplication&post=' . $post->ID . '&nonce=' . wp_create_nonce( 'asenha-duplicate-' . $post->ID ) . '" title="Duplicate this as draft">Duplicate</a>';
            }
        }
        return $actions;
    }

可惜的是 ASE 並沒有提供我們自訂選項可以不在哪些 Post Type 後台表格下顯示複製,所以我們只能自己另外攔截表格輸出 (使用 page_row_actions filter)。例如我們想要避免 ACF 系統的 Post Type 被複製可以使用下面的程式碼

add_filter('page_row_actions', 'prevent_acf_to_be_duplicate', 99, 2);

function prevent_acf_to_be_duplicate($actions, $post) {
    $post_type = $post->post_type;
    if (str_starts_with($post_type, 'acf-')) {
        unset($actions['asenha-duplicate']);
    }
    return $actions;
}

ACF 的欄位如果被複製的話,會連「key」都被複製,導致不同的 Field Group 有相同的 key,最終造成後台的各種混亂狀況。