Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow use_filters change when creating post #267

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from

Conversation

xipasduarte
Copy link

@xipasduarte xipasduarte commented Oct 18, 2022

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:

  • Enable data filtering on create:
// Uses WordPress' global functions.
add_filter( 'duplicate_post_create_duplicate_use_filters', '__return_true' );
  • Add some meta to be excluded
public function exclude_meta_keys( array $meta_keys ) : array {
	// Replace this meta key according to your setup.
	$meta_keys[] = 'excluded_meta_key';
	return $meta_keys;
}

add_filter( 'duplicate_post_excludelist_filter', 'exclude_meta_keys' );
  • Duplicate and check that the 'excluded_meta_key' is not present in the new post.

Relevant test scenarios

  • Changes should be tested with the browser console open
  • Changes should be tested on different posts/pages/taxonomies/custom post types/custom taxonomies
  • Changes should be tested on different editors (Block/Classic/Elementor/other)
  • Changes should be tested on different browsers
  • Changes should be tested on multisite

Test instructions for QA when the code is in the RC

  • QA should use the same steps as above.

Impact check

This PR affects the following parts of the plugin, which may require extra testing:

  • None that we could find.

UI changes

  • This PR changes the UI in the plugin. I have added the 'UI change' label to this PR.

Documentation

  • I have written documentation for this change.

Quality assurance

  • I have tested this code to the best of my abilities
  • I have added unittests to verify the code works as intended

Fixes #204

@grappler
Copy link

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.

if ( $options['use_filters'] ) {
/**
* Filter new post values.
*
* @param array $new_post New post values.
* @param WP_Post $post Original post object.
*
* @return array
*/
$new_post = \apply_filters( 'duplicate_post_new_post', $new_post, $post );
}

if ( $options['use_filters'] ) {
/**
* Filters the taxonomy excludelist when copying a post.
*
* @param array $taxonomies_excludelist The taxonomy excludelist from the options.
*
* @return array
*/
$taxonomies_excludelist = \apply_filters( 'duplicate_post_taxonomies_excludelist_filter', $taxonomies_excludelist );
}

if ( $options['use_filters'] ) {
/**
* Filters the meta fields excludelist when copying a post.
*
* @param array $meta_excludelist The meta fields excludelist from the options.
*
* @return array
*/
$meta_excludelist = \apply_filters( 'duplicate_post_excludelist_filter', $meta_excludelist );
}

if ( $options['use_filters'] ) {
/**
* Filters the list of meta fields names when copying a post.
*
* @param array $meta_keys The list of meta fields name, with the ones in the excludelist already removed.
*
* @return array
*/
$meta_keys = \apply_filters( 'duplicate_post_meta_keys_filter', $meta_keys );
}

@xipasduarte
Copy link
Author

xipasduarte commented Nov 3, 2022

@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.

@xipasduarte
Copy link
Author

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 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Filter to change Rewrite & Republish Options
3 participants