query( $wpdb->prepare( "UPDATE %i SET answered_via = %s WHERE id = %d AND answered_via = ''", Installer::table(), sanitize_key( $via ), (int) $lead_id ) ); } /** * Lines about the last 30 days, most valuable first. * * @return array */ public static function items() { global $wpdb; $since = gmdate( 'Y-m-d H:i:s', time() - self::DAYS * DAY_IN_SECONDS ); $items = array(); $answered = $wpdb->get_row( $wpdb->prepare( "SELECT COUNT(*) AS leads, SUM( CASE WHEN status = 'won' THEN 1 ELSE 0 END ) AS won FROM %i WHERE answered_via <> '' AND created_at >= %s", Installer::table(), $since ) ); if ( $answered && (int) $answered->leads > 0 ) { $items[] = array( 'key' => 'answered', 'text' => sprintf( /* translators: 1: enquiries, 2: customers, 3: amount */ _n( '%1$s enquiry answered through Dealroot – with a quick reply, the AI assistant or from Telegram. %2$s of them became customers, worth %3$s.', '%1$s enquiries answered through Dealroot – with a quick reply, the AI assistant or from Telegram. %2$s of them became customers, worth %3$s.', (int) $answered->leads, 'dealroot' ), number_format_i18n( (int) $answered->leads ), number_format_i18n( (int) $answered->won ), Admin\Format::money_list( self::revenue( 'answered_via', $since ) ) ), ); } $confirmed = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE ai_confirmed_at >= %s', Installer::table(), $since ) ); if ( $confirmed > 0 ) { $items[] = array( 'key' => 'confirmed', /* translators: %s: number of enquiries */ 'text' => sprintf( _n( '%s enquiry got a personal confirmation within a minute.', '%s enquiries got a personal confirmation within a minute.', $confirmed, 'dealroot' ), number_format_i18n( $confirmed ) ), ); } $spam = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE status = 'spam' AND created_at >= %s", Installer::table(), $since ) ); if ( $spam > 0 ) { $items[] = array( 'key' => 'spam', /* translators: %s: number of messages */ 'text' => sprintf( _n( '%s spam message was sorted out, so you did not have to read it.', '%s spam messages were sorted out, so you did not have to read them.', $spam, 'dealroot' ), number_format_i18n( $spam ) ), ); } /** * Filters what Dealroot did in the last 30 days. * * @param array $items Items with key and text. * @param string $since UTC start. */ return array_values( (array) apply_filters( 'dealroot_impact_items', $items, $since ) ); } /** * Revenue of won leads with a column set, per currency. * * @param string $column answered_via. * @param string $since UTC start. * @return array */ private static function revenue( $column, $since ) { global $wpdb; $rows = $wpdb->get_results( $wpdb->prepare( "SELECT currency, SUM( COALESCE( value, 0 ) ) AS revenue FROM %i WHERE %i <> '' AND status = 'won' AND created_at >= %s GROUP BY currency", Installer::table(), $column, $since ) ); $default = (string) Settings::get( 'currency' ); $revenue = array(); foreach ( (array) $rows as $row ) { $currency = '' !== (string) $row->currency ? (string) $row->currency : $default; $revenue[ $currency ] = ( $revenue[ $currency ] ?? 0 ) + (float) $row->revenue; } return array_filter( $revenue ); } /** * On the first days of a month, sends the last 30 days to Telegram, once. */ public static function monthly() { $month = wp_date( 'Y-m' ); if ( (int) wp_date( 'j' ) > 3 || get_option( self::OPTION ) === $month || ! Notifier::telegram_ready() || ! Settings::get( 'telegram_weekly' ) ) { return; } $items = self::items(); update_option( self::OPTION, $month, false ); if ( ! $items ) { return; } $esc = static function ( $text ) { return htmlspecialchars( (string) $text, ENT_NOQUOTES, 'UTF-8' ); }; $lines = array( '' . $esc( __( 'What Dealroot did in the last 30 days', 'dealroot' ) ) . '', '' ); foreach ( $items as $item ) { $lines[] = '• ' . $esc( $item['text'] ); } Notifier::send_telegram( Settings::get( 'telegram_token' ), Settings::get( 'telegram_chat_id' ), implode( "\n", $lines ) ); } }