compliance-settings.php
103 lines
| 1 | <?php |
| 2 | /** |
| 3 | * SureForms single form settings - Compliance settings. |
| 4 | * |
| 5 | * @package sureforms. |
| 6 | * @since 0.0.2 |
| 7 | */ |
| 8 | |
| 9 | namespace SRFM\Inc\Single_Form_Settings; |
| 10 | |
| 11 | use SRFM\Inc\Database\Tables\Entries; |
| 12 | use SRFM\Inc\Helper; |
| 13 | use SRFM\Inc\Traits\Get_Instance; |
| 14 | |
| 15 | /** |
| 16 | * SureForms single form settings - Compliance settings. |
| 17 | * |
| 18 | * @since 0.0.2 |
| 19 | */ |
| 20 | class Compliance_Settings { |
| 21 | use Get_Instance; |
| 22 | |
| 23 | /** |
| 24 | * Constructor |
| 25 | * |
| 26 | * @since 0.0.2 |
| 27 | */ |
| 28 | public function __construct() { |
| 29 | add_action( 'srfm_daily_scheduled_action', [ $this, 'pre_auto_delete_entries' ] ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Runs every 24 hours for SureForms. |
| 34 | * And check if auto delete entries are enabled for any forms. |
| 35 | * If enabled then delete the entries that are older than the days_old. |
| 36 | * |
| 37 | * @hooked - srfm_daily_scheduled_action |
| 38 | * @since 0.0.2 |
| 39 | * @return void |
| 40 | */ |
| 41 | public function pre_auto_delete_entries() { |
| 42 | |
| 43 | // Get all the sureforms form post ids. |
| 44 | $form_ids = []; |
| 45 | |
| 46 | $args = [ |
| 47 | 'post_type' => 'sureforms_form', |
| 48 | 'posts_per_page' => -1, |
| 49 | 'fields' => 'ids', |
| 50 | ]; |
| 51 | |
| 52 | $form_ids = get_posts( $args ); |
| 53 | |
| 54 | if ( empty( $form_ids ) ) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | // For each form post id check if auto_delete_entries is enabled. If enabled then delete the entries that are older than the days_old. |
| 59 | foreach ( $form_ids as $form_id ) { |
| 60 | $compliance_settings = get_post_meta( $form_id, '_srfm_compliance', true ); |
| 61 | |
| 62 | $gdpr = false; |
| 63 | $do_not_store_entries = false; |
| 64 | $is_auto_delete_entries = false; |
| 65 | $days_old = 0; |
| 66 | |
| 67 | if ( is_array( $compliance_settings ) && ! empty( $compliance_settings[0] ) && is_array( $compliance_settings[0] ) && isset( $compliance_settings[0]['gdpr'], $compliance_settings[0]['do_not_store_entries'], $compliance_settings[0]['auto_delete_entries'] ) ) { |
| 68 | $gdpr = $compliance_settings[0]['gdpr']; |
| 69 | $do_not_store_entries = $compliance_settings[0]['do_not_store_entries']; |
| 70 | $is_auto_delete_entries = $compliance_settings[0]['auto_delete_entries']; |
| 71 | $days_old = $compliance_settings[0]['auto_delete_days']; |
| 72 | } |
| 73 | |
| 74 | // Only delete entries if gdpr, is_auto_delete_entries are enabled and do_not_store_entries is not enabled. |
| 75 | if ( $gdpr && ! $do_not_store_entries && $is_auto_delete_entries ) { |
| 76 | self::delete_old_entries( $days_old, $form_id ); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Delete all the entries that are older than the days_old. |
| 83 | * |
| 84 | * @param int $days_old Number of days old. |
| 85 | * @param int $form_id Form ID. |
| 86 | * @since 0.0.2 |
| 87 | * @return void |
| 88 | */ |
| 89 | public static function delete_old_entries( $days_old, $form_id ) { |
| 90 | $entries = Helper::get_entries_from_form_ids( $days_old, [ $form_id ] ); |
| 91 | |
| 92 | if ( ! is_array( $entries ) || empty( $entries ) ) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | foreach ( $entries as $entry ) { |
| 97 | $entry_id = isset( $entry['ID'] ) ? Helper::get_integer_value( $entry['ID'] ) : 0; |
| 98 | Entries::delete( $entry_id ); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | } |
| 103 |