Control whether or not to trash a custom post type

I need to have control over the trash / delete action on a custom post type.

I need to check for some conditions and decide if i should trash the CPT or not.

The wp_trash_action hook that is fired before the trash function does not give me the option to cancel the trash procedure.

To be more specific , i need to check for a condition when the user presses TRASH. If that condition is false then i need to stop the trashing action. If it is true it should continue.

Solutions Collecting From Web of "Control whether or not to trash a custom post type"

Hook onto trashed_post and use wp_untrash_post() to reverse upon meeting a condition, pseudocode example…

add_action('trashed_post', 'wpse_218031_trashed_post');

function wpse_218031_trashed_post($post_id){
   //use post_id to check conditions... if not met call:
   if ( $some_condition ) {
       wp_untrash_post($post_id);
   }
}

trashed_post runs after wp_trash_post and after the post is moved to trash.

By the way, if the constant EMPTY_TRASH_DAYS is defined and is false posts will be permanently deleted and the subsequent hooks mentioned will not be fired, in which case wp_delete_post() will be called instead.