true ) ), array( 'attachment' ) ) ); return apply_filters( 'nodebook_interest_post_types', $types ); } /** * Is this post something people can be interested in (a property, product, service, listing)? * Blog posts, the home page, shop/cart/checkout/account pages and utility pages * (contact, about, pricing, thank-you, legal…) are not. */ public static function is_item( $post_id ) { $post = $post_id ? get_post( (int) $post_id ) : null; if ( ! $post || 'publish' !== $post->post_status || 'post' === $post->post_type || ! in_array( $post->post_type, self::item_types(), true ) ) { return false; } if ( 'page' !== $post->post_type ) { return (bool) apply_filters( 'nodebook_is_item', true, $post ); } $skip = array_filter( array_map( 'intval', array( get_option( 'page_on_front' ), get_option( 'page_for_posts' ), get_option( 'wp_page_for_privacy_policy' ), get_option( 'woocommerce_shop_page_id' ), get_option( 'woocommerce_cart_page_id' ), get_option( 'woocommerce_checkout_page_id' ), get_option( 'woocommerce_myaccount_page_id' ), get_option( 'woocommerce_terms_page_id' ) ) ) ); $util = '/^(home|inicio|contact|contacto|contacteer|kontakt|about|about-us|over-ons|nosotros|quienes-somos|ueber-uns|pricing|prices|precios|tarieven|preise|rates|tarifas|thank|thanks|gracias|bedankt|danke|merci|privacy|privacidad|terms|terminos|voorwaarden|cookie|legal|aviso-legal|impressum|faq|faqs|preguntas|blog|news|noticias|nieuws|cart|checkout|my-account|account|login|register|carrito|cesta|finalizar-compra|pago|mi-cuenta|winkelwagen|winkelmand|afrekenen|mijn-account|warenkorb|kasse|mein-konto|panier|commande|mon-compte|search|quote|book|booking|reserv|newsletter|sitemap|team|equipo|careers|jobs|empleo)/'; $item = ! in_array( (int) $post->ID, $skip, true ) && ! preg_match( $util, (string) ( $post->post_name ?? '' ) ) && ! Nodebook_Alerts::is_after_conversion( get_permalink( $post ) ); return (bool) apply_filters( 'nodebook_is_item', $item, $post ); } /** Dates searched in a URL, e.g. ?check_in=2027-02-12&check_out=2027-02-19. */ public static function dates_from_url( $url ) { $q = (string) wp_parse_url( (string) $url, PHP_URL_QUERY ); if ( '' === $q ) { return array(); } parse_str( $q, $params ); $params = array_change_key_case( (array) $params, CASE_LOWER ); $in = ''; $out = ''; foreach ( self::IN_KEYS as $k ) { if ( ! $in && ! empty( $params[ $k ] ) && is_string( $params[ $k ] ) && preg_match( '/\d/', $params[ $k ] ) ) { $in = Nodebook_Conversions::ymd( str_replace( '/', '-', $params[ $k ] ) ); } } foreach ( self::OUT_KEYS as $k ) { if ( ! $out && ! empty( $params[ $k ] ) && is_string( $params[ $k ] ) && preg_match( '/\d/', $params[ $k ] ) ) { $out = Nodebook_Conversions::ymd( str_replace( '/', '-', $params[ $k ] ) ); } } // Ignore nonsense (past years, more than two years ahead, reversed ranges). $now = time(); if ( $in && ( strtotime( $in ) < $now - 2 * DAY_IN_SECONDS || strtotime( $in ) > $now + 2 * YEAR_IN_SECONDS ) ) { return array(); } if ( $out && $in && $out <= $in ) { return array(); // Reversed or zero-night range: the URL is not a real search. } return $in ? array_filter( array( 'in' => $in, 'out' => $out, ) ) : array(); } /** * Items a contact viewed, most-viewed first. * * @return array[] id, title, url, type, type_label, views, last, attrs, converted (bool) */ public static function for_contact( $contact_id, $limit = 8 ) { global $wpdb; $a = Nodebook_Activities::table(); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT object_id, COUNT(*) AS views, MAX(created_at) AS last FROM %i WHERE contact_id = %d AND type = 'pageview' AND object_id > 0 GROUP BY object_id ORDER BY views DESC, last DESC LIMIT %d", $a, $contact_id, $limit + 5 ), ARRAY_A ); $conv = array(); $types = Nodebook_Conversions::type_list(); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- one %s placeholder per conversion type. foreach ( (array) $wpdb->get_results( $wpdb->prepare( 'SELECT data FROM %i WHERE contact_id = %d AND type IN (' . Nodebook_Sql::list_of( count( $types ), '%s' ) . ')', array_merge( array( $a, $contact_id ), $types ) ), ARRAY_A ) as $r ) { $d = json_decode( (string) $r['data'], true ); if ( ! empty( $d['item'] ) ) { $conv[ (int) $d['item'] ] = true; } } $front = (int) get_option( 'page_on_front' ); $out = array(); foreach ( (array) $rows as $r ) { $id = (int) $r['object_id']; $post = get_post( $id ); if ( ! $post || $id === $front || Nodebook_Report::is_utility( Nodebook_Report::path( (string) get_permalink( $post ) ) ) || (int) get_option( 'wp_page_for_privacy_policy' ) === $id ) { continue; } $pto = get_post_type_object( $post->post_type ); $out[] = array( 'id' => $id, 'title' => get_the_title( $post ), 'url' => get_permalink( $post ), 'type' => $post->post_type, 'type_label' => $pto ? $pto->labels->singular_name : $post->post_type, 'views' => (int) $r['views'], 'last' => $r['last'], 'attrs' => Nodebook_Niche::attributes( $id ), 'converted' => isset( $conv[ $id ] ), ); if ( count( $out ) >= $limit ) { break; } } return $out; } /** * Date ranges a contact searched for (rentals), most frequent first. * * @return array[] in, out, times */ public static function dates_for_contact( $contact_id ) { global $wpdb; $rows = $wpdb->get_col( $wpdb->prepare( "SELECT data FROM %i WHERE contact_id = %d AND type = 'pageview' AND data LIKE %s ORDER BY id DESC LIMIT 500", Nodebook_Activities::table(), $contact_id, '%' . $wpdb->esc_like( '"dates"' ) . '%' ) ); $count = array(); foreach ( (array) $rows as $json ) { $d = json_decode( (string) $json, true ); if ( empty( $d['dates']['in'] ) ) { continue; } $key = $d['dates']['in'] . '|' . ( $d['dates']['out'] ?? '' ); $count[ $key ] = ( $count[ $key ] ?? 0 ) + 1; } arsort( $count ); $out = array(); foreach ( array_slice( $count, 0, 5, true ) as $k => $n ) { list( $in, $o ) = explode( '|', $k ); $out[] = array( 'in' => $in, 'out' => $o, 'times' => $n, ); } return $out; } /** * Items in demand among known contacts in a window. * * @return array[] id, title, url, viewers, views, hot, converted, attrs */ public static function in_demand( $since, $limit = 15 ) { global $wpdb; $a = Nodebook_Activities::table(); // Based on the most recent 30,000 page views in the period: stays fast on busy sites. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT a.object_id, COUNT(*) AS views, COUNT(DISTINCT a.contact_id) AS viewers, COUNT(DISTINCT CASE WHEN c.last_intent_at >= %s THEN a.contact_id END) AS hot FROM ( SELECT object_id, contact_id FROM %i WHERE type = 'pageview' AND created_at >= %s ORDER BY created_at DESC LIMIT 30000 ) a INNER JOIN %i c ON c.id = a.contact_id WHERE a.object_id > 0 AND a.contact_id > 0 GROUP BY a.object_id ORDER BY viewers DESC, views DESC LIMIT %d", gmdate( 'Y-m-d H:i:s', time() - 7 * DAY_IN_SECONDS ), $a, $since, Nodebook_Contacts::table(), $limit * 2 + 10 ), ARRAY_A ); $conv = array(); $types = Nodebook_Conversions::type_list(); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- one %s placeholder per conversion type. foreach ( (array) $wpdb->get_col( $wpdb->prepare( 'SELECT data FROM %i WHERE type IN (' . Nodebook_Sql::list_of( count( $types ), '%s' ) . ') AND created_at >= %s AND data LIKE %s', array_merge( array( $a ), $types, array( $since, '%' . $wpdb->esc_like( '"item"' ) . '%' ) ) ) ) as $json ) { $d = json_decode( (string) $json, true ); if ( ! empty( $d['item'] ) ) { $conv[ (int) $d['item'] ] = ( $conv[ (int) $d['item'] ] ?? 0 ) + 1; } } $front = (int) get_option( 'page_on_front' ); $out = array(); foreach ( (array) $rows as $r ) { $id = (int) $r['object_id']; $post = get_post( $id ); if ( ! $post || $id === $front || ! self::is_item( $id ) ) { continue; } $out[] = array( 'id' => $id, 'title' => get_the_title( $post ), 'url' => get_permalink( $post ), 'viewers' => (int) $r['viewers'], 'views' => (int) $r['views'], 'hot' => (int) $r['hot'], 'converted' => $conv[ $id ] ?? 0, 'attrs' => Nodebook_Niche::attributes( $id ), 'who' => array_map( 'intval', (array) $wpdb->get_col( $wpdb->prepare( "SELECT contact_id FROM %i WHERE type = 'pageview' AND object_id = %d AND contact_id > 0 AND created_at >= %s GROUP BY contact_id ORDER BY MAX(created_at) DESC LIMIT 8", $a, $id, $since ) ) ), ); if ( count( $out ) >= $limit ) { break; } } return $out; } /** * Companies whose people were on the site (B2B account view). * * @return array[] id, name, people, visits, pageviews, last, hot */ public static function companies_active( $since, $limit = 15 ) { global $wpdb; // Based on the most recent 30,000 page views in the period. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT co.id, co.name, COUNT(DISTINCT c.id) AS people, COUNT(DISTINCT CONCAT(a.contact_id, '-', DATE(a.created_at))) AS visits, COUNT(*) AS pageviews, MAX(a.created_at) AS last, COUNT(DISTINCT CASE WHEN c.last_intent_at >= %s THEN c.id END) AS hot FROM ( SELECT contact_id, created_at FROM %i WHERE type = 'pageview' AND created_at >= %s ORDER BY created_at DESC LIMIT 30000 ) a INNER JOIN %i c ON c.id = a.contact_id INNER JOIN %i co ON co.id = c.company_id GROUP BY co.id, co.name ORDER BY pageviews DESC LIMIT %d", gmdate( 'Y-m-d H:i:s', time() - 7 * DAY_IN_SECONDS ), Nodebook_Activities::table(), $since, Nodebook_Contacts::table(), Nodebook_Companies::table(), $limit ), ARRAY_A ); return array_map( static function ( $r ) { foreach ( array( 'id', 'people', 'visits', 'pageviews', 'hot' ) as $k ) { $r[ $k ] = (int) $r[ $k ]; } return $r; }, (array) $rows ); } /** Recent activity of everyone at a company (company profile). */ public static function company_activity( $company_id, $limit = 40 ) { global $wpdb; list( $restricted, $me, $unassigned ) = Nodebook_Team::scope_args(); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT a.*, c.first_name, c.last_name, c.email, c.phone, c.id AS cid FROM %i a INNER JOIN %i c ON c.id = a.contact_id WHERE c.company_id = %d AND ( %d = 0 OR c.owner_id = %d OR ( %d = 1 AND c.owner_id = 0 ) ) AND a.type NOT IN ('pageview','created','import','segment') ORDER BY a.created_at DESC, a.id DESC LIMIT %d", Nodebook_Activities::table(), Nodebook_Contacts::table(), $company_id, $restricted, $me, $unassigned, $limit ), ARRAY_A ); return (array) $rows; } /** Items everyone at a company viewed. */ public static function company_interests( $company_id, $limit = 8 ) { global $wpdb; $rows = $wpdb->get_results( $wpdb->prepare( "SELECT a.object_id, COUNT(*) AS views, COUNT(DISTINCT a.contact_id) AS people FROM %i a INNER JOIN %i c ON c.id = a.contact_id WHERE c.company_id = %d AND a.type = 'pageview' AND a.object_id > 0 GROUP BY a.object_id ORDER BY views DESC LIMIT %d", Nodebook_Activities::table(), Nodebook_Contacts::table(), $company_id, $limit ), ARRAY_A ); $out = array(); foreach ( (array) $rows as $r ) { $post = get_post( (int) $r['object_id'] ); if ( $post ) { $out[] = array( 'id' => (int) $r['object_id'], 'title' => get_the_title( $post ), 'url' => get_permalink( $post ), 'views' => (int) $r['views'], 'people' => (int) $r['people'], ); } } return $out; } }