prefix . 'aieo_contact_messages'; } public static function ready( $fresh = false ) { static $memo = null; if ( $fresh ) { $memo = null; } if ( null !== $memo ) { return $memo; } global $wpdb; $t = self::table(); $memo = ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) === $t ); return $memo; } public static function insert( array $d ) { global $wpdb; if ( ! self::ready() ) { return new WP_Error( 'aieo_cf_no_table', __( 'Requests are not available right now.', 'ai-eshop-optimizer' ) ); } $max = AIEO_Contact_Form_Settings::MAX_LENGTHS; $cut = static function ( $v, $n ) { $v = (string) $v; return function_exists( 'mb_substr' ) ? mb_substr( $v, 0, $n ) : substr( $v, 0, $n ); }; $row = array( 'status' => 'new', 'first_name' => $cut( $d['first_name'] ?? '', $max['first_name'] ), 'last_name' => $cut( $d['last_name'] ?? '', $max['last_name'] ), 'email' => $cut( $d['email'] ?? '', $max['email'] ), 'phone' => $cut( $d['phone'] ?? '', $max['phone'] ), 'city' => $cut( $d['city'] ?? '', $max['city'] ), 'country' => $cut( $d['country'] ?? '', $max['country'] ), 'company' => $cut( $d['company'] ?? '', $max['company'] ), 'message' => $cut( $d['message'] ?? '', $max['message'] ), 'consent' => empty( $d['consent'] ) ? 0 : 1, 'locale' => substr( (string) ( $d['locale'] ?? '' ), 0, 20 ), 'lang' => substr( (string) ( $d['lang'] ?? '' ), 0, 10 ), 'page_url' => substr( esc_url_raw( (string) ( $d['page_url'] ?? '' ) ), 0, 512 ), 'post_id' => absint( $d['post_id'] ?? 0 ), 'user_id' => absint( $d['user_id'] ?? 0 ), 'ip_hash' => (string) ( $d['ip_hash'] ?? '' ), 'user_agent' => $cut( $d['user_agent'] ?? '', 255 ), 'created_at' => gmdate( 'Y-m-d H:i:s' ), ); $ok = (bool) $wpdb->insert( self::table(), $row, array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%s', '%s', '%s' ) ); if ( ! $ok ) { return new WP_Error( 'aieo_cf_insert', __( 'Could not save your request. Please try again.', 'ai-eshop-optimizer' ) ); } $id = (int) $wpdb->insert_id; do_action( 'aieo_contact_message_created', $id, $row ); return $id; } public static function claim( $id, $col ) { global $wpdb; if ( ! in_array( $col, self::CLAIMS, true ) ) { return false; } return 1 === (int) $wpdb->query( $wpdb->prepare( 'UPDATE `' . self::table() . '` SET `' . $col . '` = %s WHERE id = %d AND `' . $col . '` IS NULL', gmdate( 'Y-m-d H:i:s' ), (int) $id ) ); } public static function release( $id, $col ) { global $wpdb; if ( ! in_array( $col, self::CLAIMS, true ) ) { return; } $wpdb->query( $wpdb->prepare( 'UPDATE `' . self::table() . '` SET `' . $col . '` = NULL WHERE id = %d', (int) $id ) ); } public static function set_status( $ids, $status ) { global $wpdb; $status = sanitize_key( (string) $status ); if ( ! in_array( $status, self::STATUSES, true ) ) { return 0; } $ids = self::clean_ids( $ids ); if ( ! $ids ) { return 0; } $now = gmdate( 'Y-m-d H:i:s' ); $in = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); $sql = 'UPDATE `' . self::table() . '` SET status = %s, handled_at = %s, handled_by = %d,' . ' read_at = IF( %s <> \'new\' AND read_at IS NULL, %s, read_at ) WHERE id IN (' . $in . ')'; return (int) $wpdb->query( $wpdb->prepare( $sql, array_merge( array( $status, $now, get_current_user_id(), $status, $now ), $ids ) ) ); } public static function mark_read_if_new( $id ) { global $wpdb; return 1 === (int) $wpdb->query( $wpdb->prepare( 'UPDATE `' . self::table() . '` SET status = \'read\', read_at = %s WHERE id = %d AND status = \'new\'', gmdate( 'Y-m-d H:i:s' ), (int) $id ) ); } public static function delete( $ids ) { global $wpdb; $ids = self::clean_ids( $ids ); if ( ! $ids ) { return 0; } $in = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); $n = (int) $wpdb->query( $wpdb->prepare( 'DELETE FROM `' . self::table() . '` WHERE id IN (' . $in . ')', $ids ) ); if ( class_exists( 'AIEO_Submission_Context' ) && method_exists( 'AIEO_Submission_Context', 'forget_leads' ) ) { AIEO_Submission_Context::forget_leads( array_map( static function ( $id ) { return 'cf:' . $id; }, $ids ) ); } return $n; } public static function clean_ids( $ids ) { $ids = array_values( array_unique( array_filter( array_map( 'absint', (array) $ids ) ) ) ); return array_slice( $ids, 0, 500 ); } public static function get( $id ) { global $wpdb; if ( ! self::ready() ) { return null; } return $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM `' . self::table() . '` WHERE id = %d LIMIT 1', (int) $id ) ); } public static function list_messages( array $args = array() ) { global $wpdb; if ( ! self::ready() ) { return array( 'rows' => array(), 'total' => 0 ); } $where = array( '1=1' ); $params = array(); $status = sanitize_key( (string) ( $args['status'] ?? '' ) ); if ( in_array( $status, self::STATUSES, true ) ) { $where[] = 'status = %s'; $params[] = $status; } $search = trim( (string) ( $args['search'] ?? '' ) ); if ( '' !== $search ) { $like = '%' . $wpdb->esc_like( $search ) . '%'; $where[] = '(email LIKE %s OR first_name LIKE %s OR last_name LIKE %s OR phone LIKE %s OR company LIKE %s OR city LIKE %s OR message LIKE %s)'; $params = array_merge( $params, array_fill( 0, 7, $like ) ); } $sql_where = implode( ' AND ', $where ); $count_sql = 'SELECT COUNT(*) FROM `' . self::table() . '` WHERE ' . $sql_where; $total = (int) $wpdb->get_var( $params ? $wpdb->prepare( $count_sql, $params ) : $count_sql ); $per_page = max( 1, min( 500, (int) ( $args['per_page'] ?? 50 ) ) ); $paged = max( 1, (int) ( $args['paged'] ?? 1 ) ); $rows_sql = 'SELECT * FROM `' . self::table() . '` WHERE ' . $sql_where . ' ORDER BY created_at DESC, id DESC LIMIT %d OFFSET %d'; $rows = (array) $wpdb->get_results( $wpdb->prepare( $rows_sql, array_merge( $params, array( $per_page, ( $paged - 1 ) * $per_page ) ) ) ); return array( 'rows' => $rows, 'total' => $total ); } public static function status_counts() { global $wpdb; if ( ! self::ready() ) { return array(); } $out = array(); foreach ( (array) $wpdb->get_results( 'SELECT status, COUNT(*) AS n FROM `' . self::table() . '` GROUP BY status' ) as $r ) { $out[ (string) $r->status ] = (int) $r->n; } return $out; } public static function count_new() { $c = self::status_counts(); return isset( $c['new'] ) ? (int) $c['new'] : 0; } public static function local_datetime( $gmt ) { $gmt = (string) $gmt; if ( '' === $gmt || 0 === strpos( $gmt, '0000' ) ) { return '—'; } $ts = strtotime( $gmt . ' UTC' ); if ( false === $ts ) { return $gmt; } return wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $ts ); } public static function rate_ok( $bucket, $limit, $window, $key = '' ) { $material = '' !== (string) $key ? (string) $key : self::client_ip(); $id = 'aieo_cf_' . sanitize_key( $bucket ) . '_' . md5( $material ); $n = (int) get_transient( $id ); if ( $n >= (int) $limit ) { return false; } set_transient( $id, $n + 1, (int) $window ); return true; } public static function is_bot() { $ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? (string) wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) : ''; if ( '' === $ua ) { return true; } if ( class_exists( 'AIEO_Session_Tracker' ) && method_exists( 'AIEO_Session_Tracker', 'is_bot_user_agent' ) ) { return (bool) AIEO_Session_Tracker::is_bot_user_agent( $ua ); } return (bool) preg_match( '/bot|crawl|spider|slurp|curl|wget|headless|python-requests/i', $ua ); } public static function client_ip() { if ( class_exists( 'AIEO_Product_Request_Store' ) && method_exists( 'AIEO_Product_Request_Store', 'client_ip' ) ) { return (string) AIEO_Product_Request_Store::client_ip(); } return isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( (string) $_SERVER['REMOTE_ADDR'] ) ) : ''; } public static function ip_hash( $ip = null ) { $ip = null === $ip ? self::client_ip() : (string) $ip; return '' === $ip ? '' : hash( 'sha256', $ip . '|' . wp_salt( 'auth' ) ); } public static function register_exporter( $exporters ) { $exporters['aieo-contact-form'] = array( 'exporter_friendly_name' => __( 'Rooster One — Contact form messages', 'ai-eshop-optimizer' ), 'callback' => array( __CLASS__, 'privacy_export' ), ); return $exporters; } public static function register_eraser( $erasers ) { $erasers['aieo-contact-form'] = array( 'eraser_friendly_name' => __( 'Rooster One — Contact form messages', 'ai-eshop-optimizer' ), 'callback' => array( __CLASS__, 'privacy_erase' ), ); return $erasers; } private static function privacy_where( $email ) { $user = get_user_by( 'email', $email ); if ( $user ) { return array( '( email = %s OR user_id = %d )', array( $email, (int) $user->ID ) ); } return array( 'email = %s', array( $email ) ); } public static function privacy_export( $email, $page = 1 ) { global $wpdb; $email = sanitize_email( (string) $email ); if ( '' === $email || ! self::ready() ) { return array( 'data' => array(), 'done' => true ); } $per = 50; $page = max( 1, (int) $page ); list( $where, $args ) = self::privacy_where( $email ); $rows = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM `' . self::table() . "` WHERE {$where} ORDER BY id ASC LIMIT %d OFFSET %d", array_merge( $args, array( $per, ( $page - 1 ) * $per ) ) ) ); $labels = array( 'created_at' => __( 'Date', 'ai-eshop-optimizer' ), 'first_name' => __( 'First name', 'ai-eshop-optimizer' ), 'last_name' => __( 'Last name', 'ai-eshop-optimizer' ), 'email' => __( 'Email', 'ai-eshop-optimizer' ), 'phone' => __( 'Phone', 'ai-eshop-optimizer' ), 'city' => __( 'City', 'ai-eshop-optimizer' ), 'country' => __( 'Country', 'ai-eshop-optimizer' ), 'company' => __( 'Company', 'ai-eshop-optimizer' ), 'message' => __( 'Message', 'ai-eshop-optimizer' ), 'page_url' => __( 'Page', 'ai-eshop-optimizer' ), ); $data = array(); foreach ( $rows as $r ) { $items = array(); foreach ( $labels as $k => $label ) { $v = isset( $r->$k ) ? trim( (string) $r->$k ) : ''; if ( '' !== $v ) { $items[] = array( 'name' => $label, 'value' => $v ); } } $data[] = array( 'group_id' => 'aieo-contact-form', 'group_label' => __( 'Contact form messages', 'ai-eshop-optimizer' ), 'item_id' => 'aieo-contact-message-' . (int) $r->id, 'data' => $items, ); } return array( 'data' => $data, 'done' => count( $rows ) < $per ); } public static function privacy_erase( $email, $page = 1 ) { global $wpdb; $email = sanitize_email( (string) $email ); $removed = 0; if ( '' !== $email && self::ready() ) { list( $where, $args ) = self::privacy_where( $email ); $removed = (int) $wpdb->query( $wpdb->prepare( 'DELETE FROM `' . self::table() . "` WHERE {$where}", $args ) ); } return array( 'items_removed' => $removed > 0, 'items_retained' => false, 'messages' => array(), 'done' => true, ); } } }