LegalConsents.php
101 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Legal consent model. |
| 4 | * |
| 5 | * @package Tutor\GDPR\Models |
| 6 | * @author Themeum <support@themeum.com> |
| 7 | * @link https://themeum.com |
| 8 | * @since 4.0.0 |
| 9 | */ |
| 10 | |
| 11 | namespace Tutor\GDPR\Models; |
| 12 | |
| 13 | use Tutor\GDPR\DB\LegalConsents as Table; |
| 14 | use Tutor\Models\BaseModel; |
| 15 | |
| 16 | defined( 'ABSPATH' ) || exit; |
| 17 | |
| 18 | /** |
| 19 | * Legal consent model class. |
| 20 | * |
| 21 | * @since 4.0.0 |
| 22 | */ |
| 23 | class LegalConsents extends BaseModel { |
| 24 | |
| 25 | /** |
| 26 | * Table name without prefix. |
| 27 | * |
| 28 | * @since 4.0.0 |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | protected $table_name = 'tutor_legal_consents'; |
| 33 | |
| 34 | /** |
| 35 | * Fillable fields for create/update. |
| 36 | * |
| 37 | * @since 4.0.0 |
| 38 | * |
| 39 | * @var array |
| 40 | */ |
| 41 | protected $fillable = array( |
| 42 | 'consent_title', |
| 43 | 'display_on', |
| 44 | 'consent_message', |
| 45 | 'consent_map', |
| 46 | 'version', |
| 47 | 'consent_method', |
| 48 | 'is_active', |
| 49 | 'settings', |
| 50 | 'created_at_gmt', |
| 51 | 'updated_at_gmt', |
| 52 | ); |
| 53 | |
| 54 | /** |
| 55 | * Constructor. |
| 56 | * |
| 57 | * @since 4.0.0 |
| 58 | */ |
| 59 | public function __construct() { |
| 60 | $this->table_name = Table::get_table_name(); |
| 61 | parent::__construct(); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get fillable fields |
| 66 | * |
| 67 | * @since 4.0.0 |
| 68 | * |
| 69 | * @return array |
| 70 | */ |
| 71 | public function get_fillable_fields() { |
| 72 | return $this->fillable; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Retrieve legal consent entries filtered by display key. |
| 77 | * |
| 78 | * @since 4.0.0 |
| 79 | * |
| 80 | * @param string $display_key The display key to filter consents (e.g. 'login', 'signup', etc.). |
| 81 | * |
| 82 | * @return array An array of legal consent objects or an empty array if none found. |
| 83 | */ |
| 84 | public function get_consents_by_display_key( string $display_key ): array { |
| 85 | global $wpdb; |
| 86 | |
| 87 | $res = $wpdb->get_results( |
| 88 | $wpdb->prepare( |
| 89 | "SELECT |
| 90 | * |
| 91 | FROM {$this->table_name} |
| 92 | WHERE FIND_IN_SET( %s, display_on ) |
| 93 | ", |
| 94 | $display_key |
| 95 | ) |
| 96 | ); |
| 97 | |
| 98 | return is_array( $res ) ? $res : array(); |
| 99 | } |
| 100 | } |
| 101 |