query( $wpdb->prepare( "INSERT INTO `{$t}` (email_hash, email, source, consented_at) VALUES (%s, %s, %s, %s) ON DUPLICATE KEY UPDATE email = VALUES(email), source = VALUES(source), consented_at = VALUES(consented_at)", sha1( $email ), $email, substr( sanitize_key( (string) $source ), 0, 20 ), gmdate( 'Y-m-d H:i:s' ) ) ); return false !== $n; } public static function withdraw_consent( $email ) { global $wpdb; $email = self::clean_email( $email ); if ( '' === $email || ! self::table_ready() ) { return false; } $t = self::table(); return (int) $wpdb->query( $wpdb->prepare( "DELETE FROM `{$t}` WHERE email_hash = %s", sha1( $email ) ) ) > 0; } public static function has_record( $email ) { global $wpdb; $email = self::clean_email( $email ); if ( '' === $email || ! self::table_ready() ) { return false; } $t = self::table(); return 1 === (int) $wpdb->get_var( $wpdb->prepare( "SELECT 1 FROM `{$t}` WHERE email_hash = %s LIMIT 1", sha1( $email ) ) ); } public static function has_consent( $email ) { if ( ! self::has_record( $email ) ) { return false; } return ! ( class_exists( 'AIEO_AW_Decommission' ) && AIEO_AW_Decommission::is_suppressed( $email ) ); } public static function is_opted_in( $email ) { $email = self::clean_email( $email ); if ( '' === $email ) { return false; } if ( class_exists( 'AIEO_AW_Decommission' ) && method_exists( 'AIEO_AW_Decommission', 'is_suppressed' ) ) { return ! AIEO_AW_Decommission::is_suppressed( $email ); } return true; } public static function consent_sql( $email_expr ) { if ( ! self::table_ready() ) { return '0'; } return 'EXISTS ( SELECT 1 FROM `' . self::table() . '` ec WHERE ec.email_hash = SHA1( LOWER( TRIM( ' . $email_expr . ' ) ) ) )'; } public static function table() { global $wpdb; return $wpdb->prefix . 'aieo_email_consent'; } public static function ensure_schema() { if ( '1' === get_option( self::SCHEMA_OPTION, '' ) ) { return true; } if ( ! class_exists( 'AIEO_Schema_Lock' ) || ! AIEO_Schema_Lock::acquire( self::SCHEMA_OPTION ) ) { return self::table_ready(); } global $wpdb; $t = self::table(); $charset = function_exists( 'aieo_get_charset_collate' ) ? aieo_get_charset_collate() : $wpdb->get_charset_collate(); $wpdb->query( "CREATE TABLE IF NOT EXISTS `{$t}` ( email_hash char(40) NOT NULL, email varchar(190) NOT NULL DEFAULT '', source varchar(20) NOT NULL DEFAULT '', consented_at datetime NOT NULL, PRIMARY KEY (email_hash) ) {$charset};" ); $made = self::table_exists( $t ); if ( $made ) { update_option( self::SCHEMA_OPTION, '1', false ); } return $made; } private static function table_ready() { static $ready = false; if ( $ready || '1' === get_option( self::SCHEMA_OPTION, '' ) ) { return $ready = true; } return $ready = self::table_exists( self::table() ); } private static function table_exists( $t ) { global $wpdb; return $t === $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $t ) ) ); } private static function clean_email( $email ) { $email = strtolower( sanitize_email( (string) $email ) ); return is_email( $email ) ? $email : ''; } }