'Unauthorized'],403); $id=absint($_POST['id']??0);if(!$id) wp_send_json_error(['message'=>'Invalid lead'],400); global $wpdb; $cache_group = 'beeschat_lead_details'; $cache_key = 'lead_' . $id; $row = wp_cache_get($cache_key, $cache_group); if (false === $row) { // A single prepared read of the plugin's own table is required for the lead detail popup. // Cache results to avoid duplicate database reads during the cache lifetime. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Custom plugin table cannot use WP_Query. $row = $wpdb->get_row($wpdb->prepare('SELECT * FROM %i WHERE id=%d', $wpdb->prefix . 'bbfw_leads', $id)); if ($row) { wp_cache_set($cache_key, $row, $cache_group, MINUTE_IN_SECONDS); } } if (!$row) wp_send_json_error(['message'=>'Lead not found'],404); $action=sanitize_key($_POST['operation']??''); if($action==='status'){$status=sanitize_key($_POST['status']??'');if(!in_array($status,['new','contacted','qualified','converted','lost'],true)) wp_send_json_error(['message'=>'Invalid status'],400);$all=get_option('beeschat_lead_status_v3',[]);$all[$id]=$status;update_option('beeschat_lead_status_v3',$all,false);wp_send_json_success(['status'=>$status]);} $tracking=json_decode($row->tracking,true)?:[];$fields=$tracking['_beeschat_fields']??[];$labels=$tracking['_beeschat_labels']??[]; $detail=['Date'=>$row->created_at,'Status'=>ucfirst(self::get_lead_status($id)),'Name'=>$row->name,'Email'=>$row->email,'Phone'=>$row->phone]; foreach($fields as $key=>$value) if(is_scalar($value)) $detail[$labels[$key]??$key]=(string)$value; foreach($tracking as $key=>$value) if(strpos((string)$key, '_') !== 0&&is_scalar($value)&&$value!=='') $detail[$key]=(string)$value; $detail['Landing page']=$row->landing_page;$detail['Page']=$row->page_url;$detail['Referrer']=$row->referrer; wp_send_json_success(['details'=>$detail]); } }