Plugin to blog post: https://kau-boys.com/2596/wordpress/prevent-deletion-of-post-type-items-that-are-still-used
<?php | |
/** | |
* Deletion Prevention | |
* | |
* @package pec | |
* @author Bernhard Kau | |
* @license GPLv3 | |
* | |
* @wordpress-plugin | |
* Plugin Name: Deletion Prevention | |
* Plugin URI: https://gist.github.com/2ndkauboy/157ad2b7d47b540f3c0125f43dd7f18c | |
* Description: Prevent deletion of Contact Form 7 items when they are embedded in posts or pages. | |
* Version: 1.0.0 | |
* Author: Bernhard Kau | |
* Author URI: https://kau-boys.de | |
* Text Domain: deletion-prevention | |
* License: GPLv3 | |
* License URI: https://www.gnu.org/licenses/gpl-3.0.txt | |
*/ | |
/** | |
* Register the helper taxonomy for the deletion prevention. | |
*/ | |
function deletion_prevention_register_taxonomy() { | |
register_taxonomy( | |
'deletion_prevention_tax', | |
array( 'post' ), | |
array( | |
'hierarchical' => false, | |
'public' => false, | |
) | |
); | |
} | |
add_action( 'init', 'deletion_prevention_register_taxonomy' ); | |
/** | |
* Fires once a post has been saved. | |
* | |
* @param int $post_ID Post ID. | |
* @param WP_Post $post Post object. | |
* @param bool $update Whether this is an existing post being updated. | |
*/ | |
function deletion_prevention_save_post( $post_ID, $post, $update ) { | |
if ( ! in_array( $post->post_type, array( 'post', 'page' ), true ) ) { | |
return; | |
} | |
$blocks = parse_blocks( $post->post_content ); | |
$forms = array(); | |
foreach ( $blocks as $block ) { | |
if ( 'contact-form-7/contact-form-selector' === $block['blockName'] ) { | |
$forms[] = (string) $block['attrs']['id']; | |
} | |
} | |
wp_set_object_terms( $post_ID, $forms, 'deletion_prevention_tax' ); | |
} | |
add_action( 'save_post', 'deletion_prevention_save_post', 10, 3 ); | |
/** | |
* Prevent trashing if question is currently used in a quiz. | |
* | |
* @param bool|null $trash Whether to go forward with trashing. | |
* @param WP_Post $post Post object. | |
*/ | |
function deletion_prevention_trash_check( $trash, $post ) { | |
deletion_prevention_check( $post ); | |
} | |
add_action( 'pre_trash_post', 'deletion_prevention_trash_check', 10, 2 ); | |
/** | |
* Prevent deletion if question is currently used in a quiz. | |
* | |
* @param bool|null $delete Whether to go forward with deletion. | |
* @param WP_Post $post Post object. | |
* @param bool $force_delete Whether to bypass the Trash. | |
*/ | |
function deletion_prevention_delete_check( $delete, $post, $force_delete ) { | |
deletion_prevention_check( $post ); | |
} | |
add_action( 'pre_delete_post', 'deletion_prevention_delete_check', 10, 3 ); | |
/** | |
* Check if the question is used in a quiz. | |
* | |
* @param WP_Post $post Post object. | |
*/ | |
function deletion_prevention_check( $post ) { | |
$args = array( | |
'post_type' => array( 'post', 'page' ), | |
'post_status' => array( | |
'publish', | |
'pending', | |
'draft', | |
'future', | |
'private', | |
), | |
'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query | |
array( | |
'taxonomy' => 'deletion_prevention_tax', | |
'field' => 'slug', | |
'terms' => (string) $post->ID, | |
), | |
), | |
'posts_per_page' => 1, | |
'fields' => 'ids', | |
); | |
$posts = get_posts( $args ); | |
if ( ! empty( $posts ) ) { | |
wp_die( | |
wp_kses_post( | |
sprintf( | |
/* translators: %1$s: link to a filtered posts list, %2$s: link to a filtered pages list */ | |
__( | |
'This item as it is still used in <a href="%1$s">posts</a> or <a href="%2$s">pages</a>.', | |
'deletion-prevention' | |
), | |
admin_url( 'edit.php?post_type=post&taxonomy=deletion_prevention_tax&term=' . $post->ID ), | |
admin_url( 'edit.php?taxonomy=deletion_prevention_tax&term=' . $post->ID ) | |
) | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment