get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE created_at >= %s AND created_at < %s', $ct, $from, $to ) ); }; $count_type = static function ( $types, $from, $to ) use ( $wpdb, $a ) { $in = implode( ',', array_fill( 0, count( $types ), '%s' ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- $in is a list of %s placeholders. return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i USE INDEX (type_created) WHERE type IN ($in) AND created_at >= %s AND created_at < %s AND contact_id > 0", array_merge( array( $a ), $types, array( $from, $to ) ) ) ); }; $revenue = static function ( $from, $to ) use ( $wpdb, $a ) { $rows = $wpdb->get_col( $wpdb->prepare( "SELECT data FROM %i USE INDEX (type_created) WHERE type IN ('order','booking') AND created_at >= %s AND created_at < %s LIMIT 5000", $a, $from, $to ) ); $sum = 0.0; $main = Nodebook_Conversions::currency(); foreach ( (array) $rows as $json ) { $d = json_decode( (string) $json, true ); $cur = is_array( $d ) ? strtoupper( (string) ( $d['currency'] ?? '' ) ) : ''; // Only amounts in the main currency are added up (no conversion is guessed). if ( '' !== $main && '' !== $cur && $cur !== $main ) { continue; } if ( is_array( $d ) && isset( $d['value'] ) && ! in_array( $d['status'] ?? '', array( 'cancelled', 'refunded', 'failed' ), true ) ) { $sum += (float) $d['value']; } } return $sum; }; $sources = $wpdb->get_results( $wpdb->prepare( 'SELECT first_source, first_medium, COUNT(*) AS n FROM %i WHERE created_at >= %s AND created_at < %s GROUP BY first_source, first_medium ORDER BY n DESC LIMIT 6', $ct, $since, $end ), ARRAY_A ); $pages = $wpdb->get_results( $wpdb->prepare( "SELECT title, COUNT(*) AS n, COUNT(DISTINCT contact_id) AS people FROM %i USE INDEX (type_created) WHERE type = 'pageview' AND created_at >= %s AND created_at < %s AND contact_id > 0 GROUP BY title ORDER BY people DESC, n DESC LIMIT 5", $a, $since, $end ), ARRAY_A ); $won = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE status_at >= %s AND status_at < %s AND status = 'customer'", $ct, $since, $end ) ); $pipe = array(); foreach ( (array) $wpdb->get_results( $wpdb->prepare( 'SELECT status, COUNT(*) AS n FROM %i GROUP BY status', $ct ), ARRAY_A ) as $r ) { $pipe[ $r['status'] ] = (int) $r['n']; } $data = array( 'since' => $since, 'new' => array( $count_new( $since, $end ), $count_new( $prev, $since ) ), 'forms' => array( $count_type( array( 'form' ), $since, $end ), $count_type( array( 'form' ), $prev, $since ) ), 'orders' => array( $count_type( array( 'order', 'booking' ), $since, $end ), $count_type( array( 'order', 'booking' ), $prev, $since ) ), 'revenue' => array( $revenue( $since, $end ), $revenue( $prev, $since ) ), 'moves' => $count_type( array( 'status' ), $since, $end ), 'touches' => $count_type( array( 'touch', 'followup' ), $since, $end ), 'won' => $won, 'sources' => (array) $sources, 'pages' => (array) $pages, 'pipeline' => $pipe, 'due' => Nodebook_Followup::count_due(), 'deals' => Nodebook_Deals::won_since( $since, $end ), ); // Deals added this week and the value still open. $data['deals_new'] = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE created_at >= %s AND created_at < %s', Nodebook_Deals::table(), $since, $end ) ); $open = Nodebook_Deals::open_value(); $data['deals_open'] = $open; if ( ! Nodebook_Deals::on() ) { $data['deals'] = array( 0, 0, 0 ); $data['deals_new'] = 0; $data['deals_open'] = 0; } $data['any'] = $data['new'][0] || $data['forms'][0] || $data['orders'][0] || $data['moves'] || $data['touches'] || $data['pages'] || $data['deals'][0] || $data['deals_new']; /** * The weekly summary's numbers. Modules can add their own keys and show them with nodebook_weekly_sections. * * @param array $data Numbers for the week. * @param int $now Timestamp the week ends at. */ return (array) apply_filters( 'nodebook_weekly_data', $data, $now ); } /** "+3 vs last week" / "same as last week". */ private static function delta( $now, $before, $money = false ) { $diff = $now - $before; if ( abs( $diff ) < 0.005 ) { return __( 'same as last week', 'nodebook' ); } $num = $money ? Nodebook_Report::money( abs( $diff ) ) : number_format_i18n( abs( $diff ) ); /* translators: 1: + or −, 2: difference */ return sprintf( __( '%1$s%2$s vs last week', 'nodebook' ), $diff > 0 ? '+' : '−', $num ); } /** * @return array{subject:string,html:string} */ public static function render( array $d ) { $site = wp_specialchars_decode( (string) get_option( 'blogname' ), ENT_QUOTES ); $range = wp_date( 'j M', strtotime( $d['since'] . ' UTC' ) ) . ' – ' . wp_date( 'j M' ); $kpis = array( array( __( 'New contacts', 'nodebook' ), number_format_i18n( $d['new'][0] ), self::delta( $d['new'][0], $d['new'][1] ) ), array( __( 'Form submissions', 'nodebook' ), number_format_i18n( $d['forms'][0] ), self::delta( $d['forms'][0], $d['forms'][1] ) ), ); if ( $d['orders'][0] || $d['orders'][1] || Nodebook_Woocommerce::active() ) { $kpis[] = array( __( 'Orders and bookings', 'nodebook' ), number_format_i18n( $d['orders'][0] ), self::delta( $d['orders'][0], $d['orders'][1] ) ); $kpis[] = array( __( 'Revenue', 'nodebook' ), Nodebook_Report::money( $d['revenue'][0] ), self::delta( $d['revenue'][0], $d['revenue'][1], true ) ); } $html = '
'; $html .= '

' . esc_html__( 'Your week in Nodebook', 'nodebook' ) . '

' . esc_html( $site . ' · ' . $range ) . '

'; $html .= ''; foreach ( $kpis as $k ) { $html .= ''; } $html .= '
' . esc_html( $k[0] ) . '' . esc_html( $k[1] ) . '' . esc_html( $k[2] ) . '
'; if ( $d['sources'] && $d['new'][0] ) { $html .= '

' . esc_html__( 'Where new contacts came from', 'nodebook' ) . '

'; } $moves = array(); if ( $d['won'] ) { /* translators: %d: number of contacts */ $moves[] = sprintf( _n( '%d became a customer', '%d became customers', $d['won'], 'nodebook' ), $d['won'] ); } if ( ! empty( $d['deals'][0] ) ) { $moves[] = sprintf( /* translators: 1: number of deals, 2: money */ _n( '%1$d deal won (%2$s)', '%1$d deals won (%2$s)', $d['deals'][0], 'nodebook' ), $d['deals'][0], Nodebook_Report::money( $d['deals'][1] ) ) . ( ! empty( $d['deals'][2] ) ? ' · ' . sprintf( /* translators: %s: money */ __( '%s of it paid through orders or bookings above', 'nodebook' ), Nodebook_Report::money( $d['deals'][2] ) ) : '' ); } if ( ! empty( $d['deals_new'] ) ) { /* translators: %d: number of deals */ $moves[] = sprintf( _n( '%d new deal', '%d new deals', $d['deals_new'], 'nodebook' ), $d['deals_new'] ) . ( ! empty( $d['deals_open'] ) ? ' · ' . sprintf( /* translators: %s: money */ __( '%s open in the pipeline', 'nodebook' ), Nodebook_Report::money( $d['deals_open'] ) ) : '' ); } if ( $d['moves'] ) { /* translators: %d: number of status changes */ $moves[] = sprintf( _n( '%d status change on the Board', '%d status changes on the Board', $d['moves'], 'nodebook' ), $d['moves'] ); } if ( $d['touches'] ) { /* translators: %d: number of follow-ups */ $moves[] = sprintf( _n( '%d follow-up logged', '%d follow-ups logged', $d['touches'], 'nodebook' ), $d['touches'] ); } if ( $moves ) { $html .= '

' . esc_html__( 'Pipeline', 'nodebook' ) . '

' . esc_html( implode( ' · ', $moves ) ) . '

'; } $parts = array(); foreach ( Nodebook_Fields::statuses() as $slug => $label ) { if ( ! empty( $d['pipeline'][ $slug ] ) ) { $parts[] = $label . ' ' . number_format_i18n( $d['pipeline'][ $slug ] ); } } if ( $parts ) { $html .= '

' . esc_html__( 'Now:', 'nodebook' ) . ' ' . esc_html( implode( ' · ', $parts ) ) . '

'; } if ( $d['pages'] ) { $html .= '

' . esc_html__( 'What known contacts looked at most', 'nodebook' ) . '

'; } /** * Extra HTML sections in the weekly summary email, above the buttons. Use inline styles (it is an email). * * @param string $html Extra HTML (empty by default). * @param array $d The week's numbers (see nodebook_weekly_data). */ $html .= wp_kses_post( (string) apply_filters( 'nodebook_weekly_sections', '', $d ) ); $html .= '

' . esc_html( $d['due'] /* translators: %d: number of contacts to follow up */ ? sprintf( _n( 'Follow up with %d person', 'Follow up with %d people', $d['due'], 'nodebook' ), $d['due'] ) : __( 'Open Nodebook', 'nodebook' ) ) . '   ' . esc_html__( 'Full report', 'nodebook' ) . '

'; $html .= '

' . esc_html__( 'Sent by Nodebook every week. Change the day or recipients under Nodebook → Settings → Signals.', 'nodebook' ) . '

'; $subject = sprintf( /* translators: 1: site name, 2: new contacts, 3: what changed vs last week */ __( '[%1$s] Your week: %2$s new contacts (%3$s)', 'nodebook' ), $site, number_format_i18n( $d['new'][0] ), self::delta( $d['new'][0], $d['new'][1] ) ); return array( 'subject' => $subject, 'html' => $html, ); } }