Open xipasduarte opened 2 years ago
Thank you @xipasduarte for working on this PR.
I don't think this is the right solution as all of the filters will be activated. There is no way of only activating certain filters.
https://github.com/Yoast/duplicate-post/blob/1b790c12af24369f7cb73c628fafb53e3fc261ec/src/post-duplicator.php#L101-L111 https://github.com/Yoast/duplicate-post/blob/1b790c12af24369f7cb73c628fafb53e3fc261ec/src/post-duplicator.php#L207-L216 https://github.com/Yoast/duplicate-post/blob/1b790c12af24369f7cb73c628fafb53e3fc261ec/src/post-duplicator.php#L257-L266 https://github.com/Yoast/duplicate-post/blob/1b790c12af24369f7cb73c628fafb53e3fc261ec/src/post-duplicator.php#L283-L292
@grappler Hello, I understand, but what you're saying is a level deeper. We could make this into a map that enables each use of $options['use_filters']
with more granularity. Something like:
[
'use_case_1' => true,
'use_case_2' => false,
]
This would then allow us to change the conditions to:
if ( $options['use_filters']['use_case_1'] ) {
// Run use_case_1
}
Let me know if you have any other suggestions.
Hello. Do you have any suggestions for this? Most 'use_filters'
are disabled for operations that a user would expect to have the duplicate_post_excludelist_filter
work. Copy for republish and rewrite afterwards are the main to concerns here. This PR only adds an option for the copy phase, but we could extend the same solution to the republish.
There is an external (outside duplicate post) solution to do this. By hooking everything in the wp_insert_post
hook, checking that the post is a rewrite, and cleaning meta fields. The same filter could be used to be easier to remove custom code, in case the plugin adds support later.
Something like:
// Add excluded metas.
function my_custom_fields_filter( $meta_excludelist ) {
// Merges the defaults array with our own array of custom fields.
return array_merge( $meta_excludelist, [ 'my_custom_field1', 'my_custom_field2' ] );
}
add_filter( 'duplicate_post_excludelist_filter', 'my_custom_fields_filter' );
// Remove metas on insert.
add_action( 'wp_insert_post', function ( $post_id ) {
// Check if this is a rewrite
$is_rewrite = get_post_meta( '_dp_is_rewrite_republish_copy', $post_id, true );
if ( $is_rewrite ) {
// Get excluded metas.
$excluded_metas = apply_filters( 'duplicate_post_excludelist_filter', [] );
// Delete excluded metas.
foreach ( $excluded_metas as $meta_key ) {
delete_post_meta( $meta_key, $post_id );
}
}
}, 10 );
Context
This was an issue encountered while using the Distributor plugin, but fixing it can allow for greater compatibility with other plugins. When duplicating a post provide a way to enable filtering the original data that gets copied (post data, meta values and terms).
We opted not to include all instances of
use_filters
because one was already addressed in #244, even though the filter's name might need some adjusting.Summary
This PR can be summarized in the following changelog entry: Adds a new filter
duplicate_post_create_duplicate_use_filters
to enable filtering the original data that gets copied (post data, meta values and terms).Relevant technical choices:
The prefix for the filter name was kept consistent with the rest of the plugin, while adding specific details with function and property being filtered.
Test instructions
Test instructions for the acceptance test before the PR gets merged
This PR can be acceptance tested by following these steps:
add_filter( 'duplicate_post_excludelist_filter', 'exclude_meta_keys' );