prefix . 'agenli_leads'; $sql = "CREATE TABLE IF NOT EXISTS {$table} ( id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(200) DEFAULT '', email VARCHAR(200) NOT NULL, phone VARCHAR(50) DEFAULT '', source VARCHAR(200) DEFAULT '', page_url VARCHAR(500) DEFAULT '', score INT DEFAULT 0, tags VARCHAR(500) DEFAULT '', created_at DATETIME DEFAULT CURRENT_TIMESTAMP ) " . $wpdb->get_charset_collate() . ";"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); } public static function capture() { // phpcs:disable WordPress.Security.NonceVerification.Missing $name = sanitize_text_field( wp_unslash( $_POST['ab_name'] ?? '' ) ); $email = sanitize_email( wp_unslash( $_POST['ab_email'] ?? '' ) ); $phone = sanitize_text_field( wp_unslash( $_POST['ab_phone'] ?? '' ) ); $source = sanitize_text_field( wp_unslash( $_POST['ab_source'] ?? 'popup' ) ); $page = esc_url_raw( wp_unslash( $_POST['ab_page'] ?? '' ) ); // phpcs:enable WordPress.Security.NonceVerification.Missing if ( ! is_email( $email ) ) { wp_send_json_error( array( 'message' => 'Please enter a valid email address.' ) ); } global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $inserted = $wpdb->insert( $wpdb->prefix . 'agenli_leads', array( 'name' => $name, 'email' => $email, 'phone' => $phone, 'source' => $source, 'page_url' => $page, 'score' => self::score_lead( $email, $phone ), ) ); if ( $inserted ) { wp_send_json_success( array( 'message' => agenli_get_option( 'leads_success_msg', 'Thank you! We will be in touch soon.' ) ) ); } else { wp_send_json_error( array( 'message' => 'Something went wrong. Please try again.' ) ); } } private static function score_lead( $email, $phone ) { $score = 10; $free_domains = array( 'gmail.com', 'yahoo.com', 'hotmail.com', 'outlook.com' ); $domain = strtolower( substr( strrchr( $email, '@' ), 1 ) ); if ( ! in_array( $domain, $free_domains, true ) ) $score += 30; if ( $phone ) $score += 20; return min( $score, 100 ); } public static function get_leads( $limit = 50, $offset = 0 ) { global $wpdb; $cache_key = 'agenli_leads_' . absint( $limit ) . '_' . absint( $offset ); $results = wp_cache_get( $cache_key, 'agenli_leads' ); if ( false === $results ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $results = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'agenli_leads ORDER BY created_at DESC LIMIT %d OFFSET %d', absint( $limit ), absint( $offset ) ) ); wp_cache_set( $cache_key, $results, 'agenli_leads', 300 ); } return $results; } public static function get_count() { global $wpdb; $cache_key = 'agenli_leads_count'; $count = wp_cache_get( $cache_key, 'agenli_leads' ); if ( false === $count ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $count = (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'agenli_leads' ); wp_cache_set( $cache_key, $count, 'agenli_leads', 300 ); } return (int) $count; } public static function get_this_month_count() { global $wpdb; $cache_key = 'agenli_leads_month_' . gmdate( 'Y_m' ); $count = wp_cache_get( $cache_key, 'agenli_leads' ); if ( false === $count ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $count = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'agenli_leads WHERE MONTH(created_at) = %d AND YEAR(created_at) = %d', (int) gmdate( 'n' ), (int) gmdate( 'Y' ) ) ); wp_cache_set( $cache_key, $count, 'agenli_leads', 300 ); } return (int) $count; } public static function render_popup() { if ( agenli_get_option( 'popup_enabled', '0' ) !== '1' ) return; $title = agenli_get_option( 'popup_title', 'Get a Free Marketing Audit' ); $sub = agenli_get_option( 'popup_subtitle', 'Enter your details and we\'ll send you a personalised report.' ); $btn = agenli_get_option( 'popup_btn_text', 'Send My Free Report' ); $color = agenli_brand_color(); ?>
id, $l->name, $l->email, $l->phone, $l->source, $l->page_url, $l->score, $l->tags, gmdate( 'Y-m-d H:i:s', strtotime( $l->created_at ) ), ); } $csv = ''; foreach ( $rows as $row ) { $escaped = array_map( function( $cell ) { return '"' . str_replace( '"', '""', $cell ) . '"'; }, $row ); $csv .= implode( ',', $escaped ) . "\r\n"; } $filename = 'agencyboost-leads-' . gmdate( 'Y-m-d' ) . '.csv'; header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); header( 'Content-Length: ' . strlen( $csv ) ); echo $csv; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped wp_die(); } }