10up / ads-txt

Ads.txt Manager for WordPress: Manage your ads.txt and app-ads.txt files in the WordPress dashboard
https://wordpress.org/plugins/ads-txt/
GNU General Public License v2.0
57 stars 23 forks source link

Clean orphaned adstxt post types. #93

Closed peterwilsoncc closed 1 year ago

peterwilsoncc commented 2 years ago

Is your enhancement related to a problem? Please describe.

The options app_adstxt_post and adstxt_post are used to refer to the post ID for the relevant file.

In the event these options are deleted, a new post of the type app-adstxt or adstxt will be created the next time an admin visits the management screen. The unreferenced post will remain as published in the database.

Once a post and the option have become detached, the post should be deleted. There are a number of options:

Designs

No response

Describe alternatives you've considered

preventing the option from being deleted

This is possible by hooking in to an *_option hook.

However, the option is only likely to be altered via /wp-admin/options.php, WP CLI or by modifying the database directly. Blocking deletion of the option to sufficiently advanced users isn't practical and likely to lead to more people hitting the database directly.

Code of Conduct

sksaju commented 1 year ago

@peterwilsoncc After reviewing the plugin, I think removing old posts will disable the user to browse Revisions! Any thoughts? Thanks

peterwilsoncc commented 1 year ago

Once the options are deleted, the revision history is lost as an ads.txt post is created from scratch, without any revisions.

However, maybe the plugin could look for an orphaned post and use that if the option has been deleted. That would prevent multiple ads.txt post types been created and restore the revisions from the previous version.

sksaju commented 1 year ago

Thanks, I'll create a PR, ASAP

sksaju commented 1 year ago

Once the options are deleted, the revision history is lost as an ads.txt post is created from scratch, without any revisions.

However, maybe the plugin could look for an orphaned post and use that if the option has been deleted. That would prevent multiple ads.txt post types been created and restore the revisions from the previous version.

@peterwilsoncc I've added an initial commit on this PR https://github.com/10up/ads-txt/pull/138 to clean orphaned posts and it's working but needs your opinion on triggering the function! currently, I use admin_init hook to trigger the callback function which is not the best option I guess, should we trigger the function on saving adstxt posts? Thanks

Update: I've moved the function into admin.php, so that function can be triggered while visiting the settings page :)