Yoast / duplicate-post

Yoast Duplicate Post plugin for WordPress
https://yoast.com
GNU General Public License v2.0
46 stars 35 forks source link

`"rest_after_insert_{$enabled_post_type}"` actions are set too early, `duplicate_post_enabled_post_types` filter has no effect. #376

Open piotr-bajer opened 3 months ago

piotr-bajer commented 3 months ago

Please give us a description of what happened

The "rest_after_insert_{$enabled_post_type}" actions are added too early, there is no way to add a duplicate_post_enabled_post_types filter before them. The solution would be to add these actions in init action or similar, so these lines would be executed in init: https://github.com/Yoast/duplicate-post/blob/trunk/src/post-republisher.php#L48-L55

This would give the themes/plugins chance to register duplicate_post_enabled_post_types filter before the REST hooks.

To Reproduce

Step-by-step reproduction instructions

  1. Add duplicate_post_enabled_post_types filter in your theme or plugin.
  2. Observe that "rest_after_insert_{$enabled_post_type}" action is registered only for post types from the settings not including ones added through duplicate_post_enabled_post_types filter.

Expected results

  1. The duplicate_post_enabled_post_types should allow to set custom post types that can be then used to add "rest_after_insert_{$enabled_post_type}" actions.

Actual results

  1. The "rest_after_insert_{$enabled_post_type}" actions are set before the theme or plugin has a chance to set duplicate_post_enabled_post_types filter.
  2. This results in An error occurred while deleting the Rewrite & Republish copy. error when you try to duplicate custom post type set in duplicate_post_enabled_post_types filter.

Screenshots, screen recording, code snippet

I get this error when I try to rewrite & republish a custom post type added through filter duplicate_post_enabled_post_types: image

Technical info

Used versions