[label, kind, sql column]. */ public static function rule_fields() { $f = array( 'email' => array( __( 'Email', 'nodebook' ), 'text', 'c.email' ), 'first_name' => array( __( 'First name', 'nodebook' ), 'text', 'c.first_name' ), 'last_name' => array( __( 'Last name', 'nodebook' ), 'text', 'c.last_name' ), 'phone' => array( __( 'Phone', 'nodebook' ), 'text', 'c.phone' ), 'company' => array( __( 'Company', 'nodebook' ), 'text', 'co.name' ), 'job_title' => array( __( 'Job title', 'nodebook' ), 'text', 'c.job_title' ), 'status' => array( __( 'Status', 'nodebook' ), 'status', 'c.status' ), 'source' => array( __( 'Source', 'nodebook' ), 'text', 'c.source' ), 'city' => array( __( 'City', 'nodebook' ), 'text', 'c.city' ), 'state' => array( __( 'State / region', 'nodebook' ), 'text', 'c.state' ), 'country' => array( __( 'Country', 'nodebook' ), 'text', 'c.country' ), 'orders_count' => array( __( 'Orders', 'nodebook' ), 'number', 'c.orders_count' ), 'total_spent' => array( __( 'Total spent', 'nodebook' ), 'number', 'c.total_spent' ), 'visits_count' => array( __( 'Visits', 'nodebook' ), 'number', 'c.visits_count' ), 'created_at' => array( __( 'Created', 'nodebook' ), 'date', 'c.created_at' ), 'last_seen' => array( __( 'Last seen', 'nodebook' ), 'date', 'c.last_seen' ), 'first_source' => array( __( 'First-touch source', 'nodebook' ), 'text', 'c.first_source' ), 'first_medium' => array( __( 'First-touch medium', 'nodebook' ), 'text', 'c.first_medium' ), 'first_campaign' => array( __( 'First-touch campaign', 'nodebook' ), 'text', 'c.first_campaign' ), 'landing_page' => array( __( 'Landing page', 'nodebook' ), 'text', 'c.landing_page' ), 'last_intent_at' => array( __( 'Viewed a high-intent page', 'nodebook' ), 'date', 'c.last_intent_at' ), 'page' => array( __( 'Viewed page (URL)', 'nodebook' ), 'page', '' ), 'watched' => array( __( 'Watched', 'nodebook' ), 'bool', 'c.watched' ), 'email_status' => array( __( 'Email consent', 'nodebook' ), 'emailstatus', 'c.email_status' ), 'event' => array( __( 'Event', 'nodebook' ), 'event', '' ), 'deal' => array( __( 'Deal', 'nodebook' ), 'deal', '' ), /* translators: %s: property, listing, treatment, product… */ 'interest' => array( sprintf( __( 'Viewed %s (title)', 'nodebook' ), Nodebook_Niche::get( 'noun' ) ), 'interest', '' ), 'clicked' => array( __( 'Clicked', 'nodebook' ), 'click', '' ), 'owner' => array( __( 'Owner', 'nodebook' ), 'owner', 'c.owner_id' ), 'remind_at' => array( __( 'Reminder', 'nodebook' ), 'date', 'c.remind_at' ), 'segment' => array( __( 'Static segment', 'nodebook' ), 'segment', '' ), 'activity' => array( __( 'Activity', 'nodebook' ), 'activity', '' ), 'product' => array( __( 'Bought product', 'nodebook' ), 'product', '' ), ); // Your own fields: kept in the values table, so the column is empty and condition() uses a subquery. $kinds = array( 'number' => 'amount', 'money' => 'amount', 'date' => 'date', 'checkbox' => 'bool', ); foreach ( Nodebook_Props::defs( 'contact' ) as $key => $def ) { $f[ $key ] = array( $def['label'], $kinds[ $def['type'] ] ?? 'text', '' ); } return apply_filters( 'nodebook_segment_rule_fields', $f ); } /** A condition on one of your own fields: EXISTS / NOT EXISTS on the indexed values table. */ private static function prop_where( array $c, $kind ) { global $wpdb; $v = (string) $c['value']; $exists = static function ( $cond, array $args = array(), $neg = false ) use ( $c ) { $w = array( "EXISTS (SELECT 1 FROM %i pv WHERE pv.object_type = 'contact' AND pv.object_id = c.id AND pv.field_key = %s AND " . $cond . ')', array_merge( array( Nodebook_Props::table(), (string) $c['field'] ), $args ), ); return $neg ? Nodebook_Sql::not( $w ) : $w; }; switch ( $kind ) { case 'text': switch ( $c['op'] ) { case 'is': return $exists( 'LOWER(pv.value_text) = LOWER(%s)', array( $v ) ); case 'is_not': return $exists( 'LOWER(pv.value_text) = LOWER(%s)', array( $v ), true ); case 'contains': return $exists( 'pv.value_text LIKE %s', array( '%' . $wpdb->esc_like( $v ) . '%' ) ); case 'not_contains': return $exists( 'pv.value_text LIKE %s', array( '%' . $wpdb->esc_like( $v ) . '%' ), true ); case 'starts': return $exists( 'pv.value_text LIKE %s', array( $wpdb->esc_like( $v ) . '%' ) ); case 'ends': return $exists( 'pv.value_text LIKE %s', array( '%' . $wpdb->esc_like( $v ) ) ); case 'empty': return $exists( "pv.value_text <> ''", array(), true ); case 'not_empty': return $exists( "pv.value_text <> ''" ); } break; case 'amount': if ( 'empty' === $c['op'] || 'not_empty' === $c['op'] ) { return $exists( 'pv.value_num IS NOT NULL', array(), 'empty' === $c['op'] ); } $map = array( 'eq' => 'pv.value_num = %f', 'gt' => 'pv.value_num > %f', 'gte' => 'pv.value_num >= %f', 'lt' => 'pv.value_num < %f', 'lte' => 'pv.value_num <= %f', ); $n = Nodebook_Props::to_number( $v ); return null === $n || ! isset( $map[ $c['op'] ] ) ? Nodebook_Sql::none() : $exists( $map[ $c['op'] ], array( $n ) ); case 'date': switch ( $c['op'] ) { case 'within': return $exists( 'pv.value_date >= %s AND pv.value_date <= %s', array( gmdate( 'Y-m-d', time() - absint( $v ) * DAY_IN_SECONDS ), gmdate( 'Y-m-d' ) ) ); case 'older': return $exists( 'pv.value_date < %s', array( gmdate( 'Y-m-d', time() - absint( $v ) * DAY_IN_SECONDS ) ) ); case 'after': case 'before': if ( ! preg_match( '/^\d{4}-\d{2}-\d{2}$/', $v ) ) { return Nodebook_Sql::none(); } return $exists( 'after' === $c['op'] ? 'pv.value_date > %s' : 'pv.value_date < %s', array( $v ) ); case 'empty': return $exists( 'pv.value_date IS NOT NULL', array(), true ); case 'not_empty': return $exists( 'pv.value_date IS NOT NULL' ); } break; case 'bool': return $exists( "pv.value_text = '1'", array(), 'yes' !== $c['op'] ); } return null; } /** * Ready-made smart segments, per business type. key => [ name, description, rules ]. */ public static function templates() { $c = static function ( $field, $op, $value = '' ) { return array( 'field' => $field, 'op' => $op, 'value' => $value, ); }; $t = array( 'hot' => array( __( 'Hot this week', 'nodebook' ), __( 'Viewed a high-intent page or tapped call/WhatsApp in the last 7 days.', 'nodebook' ), array( $c( 'last_intent_at', 'within', '7' ) ) ), 'back' => array( __( 'Came back, still a lead', 'nodebook' ), __( 'Visited 2+ times but never became a customer.', 'nodebook' ), array( $c( 'visits_count', 'gte', '2' ), $c( 'status', 'is', 'lead' ) ) ), 'winback' => array( __( 'Customers gone quiet', 'nodebook' ), __( 'Customers not seen in 6 months: worth a check-in.', 'nodebook' ), array( $c( 'status', 'is', 'customer' ), $c( 'last_seen', 'older', '180' ) ) ), 'subscribers' => array( __( 'Email subscribers', 'nodebook' ), __( 'Everyone who agreed to receive your emails: the list to export to your newsletter tool.', 'nodebook' ), array( $c( 'email_status', 'is', 'subscribed' ) ) ), 'whatsapp' => array( __( 'Tapped WhatsApp or call', 'nodebook' ), __( 'Reached for the phone: make sure someone answered.', 'nodebook' ), array( $c( 'clicked', 'has', 'whatsapp' ), $c( 'clicked', 'has', 'call' ) ), 'any' ), ); switch ( Nodebook_Niche::current() ) { case 'rentals': $t['guests'] = array( __( 'Past guests', 'nodebook' ), __( 'Everyone who booked: your repeat-stay list.', 'nodebook' ), array( $c( 'activity', 'has', 'booking' ) ) ); $t['lookers'] = array( __( 'Looked, never booked', 'nodebook' ), __( 'Viewed properties on 2+ visits, no inquiry or booking yet.', 'nodebook' ), array( $c( 'visits_count', 'gte', '2' ), $c( 'activity', 'has_not', 'booking' ), $c( 'activity', 'has_not', 'form' ) ) ); break; case 'realestate': $t['viewing'] = array( __( 'Asked about a listing', 'nodebook' ), __( 'Sent an inquiry or viewing request.', 'nodebook' ), array( $c( 'activity', 'has', 'form' ) ) ); $t['serious'] = array( __( 'Serious browsers', 'nodebook' ), __( '3+ visits in the last 30 days without a viewing request.', 'nodebook' ), array( $c( 'visits_count', 'gte', '3' ), $c( 'last_seen', 'within', '30' ), $c( 'activity', 'has_not', 'form' ) ) ); break; case 'services': $t['pricing'] = array( __( 'Read prices, no appointment', 'nodebook' ), __( 'Looked at pricing but never booked.', 'nodebook' ), array( $c( 'page', 'contains', 'pric' ), $c( 'activity', 'has_not', 'appointment' ) ) ); $t['clients'] = array( __( 'Clients', 'nodebook' ), __( 'Everyone who booked an appointment.', 'nodebook' ), array( $c( 'activity', 'has', 'appointment' ) ) ); break; case 'shop': $t['repeat'] = array( __( 'Repeat customers', 'nodebook' ), __( 'Ordered two or more times: your best people.', 'nodebook' ), array( $c( 'orders_count', 'gte', '2' ) ) ); $t['cart'] = array( __( 'Went to checkout, no order', 'nodebook' ), __( 'Known contacts who reached the cart or checkout but never bought.', 'nodebook' ), array( $c( 'orders_count', 'eq', '0' ), $c( 'last_intent_at', 'within', '30' ) ) ); break; case 'b2b': $t['specs'] = array( __( 'Downloaded specs', 'nodebook' ), __( 'Opened a datasheet or other download.', 'nodebook' ), array( $c( 'clicked', 'has', 'download' ) ) ); $t['noquote'] = array( __( 'Researching, no quote yet', 'nodebook' ), __( '2+ visits, never asked for a quote.', 'nodebook' ), array( $c( 'visits_count', 'gte', '2' ), $c( 'activity', 'has_not', 'form' ) ) ); break; } return apply_filters( 'nodebook_segment_templates', $t ); } public static function operators() { return array( 'text' => array( 'is' => __( 'is', 'nodebook' ), 'is_not' => __( 'is not', 'nodebook' ), 'contains' => __( 'contains', 'nodebook' ), 'not_contains' => __( 'does not contain', 'nodebook' ), 'starts' => __( 'starts with', 'nodebook' ), 'ends' => __( 'ends with', 'nodebook' ), 'empty' => __( 'is empty', 'nodebook' ), 'not_empty' => __( 'is not empty', 'nodebook' ), ), 'status' => array( 'is' => __( 'is', 'nodebook' ), 'is_not' => __( 'is not', 'nodebook' ), ), 'number' => array( 'eq' => '=', 'gt' => '>', 'gte' => '≥', 'lt' => '<', 'lte' => '≤', ), 'amount' => array( 'eq' => '=', 'gt' => '>', 'gte' => '≥', 'lt' => '<', 'lte' => '≤', 'empty' => __( 'is empty', 'nodebook' ), 'not_empty' => __( 'is not empty', 'nodebook' ), ), 'date' => array( 'within' => __( 'in the last … days', 'nodebook' ), 'older' => __( 'more than … days ago', 'nodebook' ), 'after' => __( 'after (YYYY-MM-DD)', 'nodebook' ), 'before' => __( 'before (YYYY-MM-DD)', 'nodebook' ), 'empty' => __( 'never / empty', 'nodebook' ), 'not_empty' => __( 'has a date', 'nodebook' ), ), 'segment' => array( 'in' => __( 'is in', 'nodebook' ), 'not_in' => __( 'is not in', 'nodebook' ), ), 'activity' => array( 'has' => __( 'has done', 'nodebook' ), 'has_not' => __( 'has not done', 'nodebook' ), ), 'page' => array( 'contains' => __( 'contains', 'nodebook' ), 'not_contains' => __( 'never contains', 'nodebook' ), ), 'interest' => array( 'contains' => __( 'title contains', 'nodebook' ), 'not_contains' => __( 'never viewed a title containing', 'nodebook' ), ), 'click' => array( 'has' => __( 'has', 'nodebook' ), 'has_not' => __( 'has not', 'nodebook' ), ), 'owner' => array( 'is' => __( 'is', 'nodebook' ), 'is_not' => __( 'is not', 'nodebook' ), ), 'bool' => array( 'yes' => __( 'yes', 'nodebook' ), 'no' => __( 'no', 'nodebook' ), ), 'product' => array( 'contains' => __( 'name contains', 'nodebook' ), 'not_contains' => __( 'name does not contain', 'nodebook' ), ), 'emailstatus' => array( 'is' => __( 'is', 'nodebook' ), 'is_not' => __( 'is not', 'nodebook' ), ), 'deal' => array( 'has' => __( 'has', 'nodebook' ), 'has_not' => __( 'has no', 'nodebook' ), ), 'event' => array( 'has' => __( 'has done', 'nodebook' ), 'has_not' => __( 'has not done', 'nodebook' ), 'within' => __( 'did in the last 30 days', 'nodebook' ), ), ); } /* ------------------------------------------------------------------ * * CRUD * ------------------------------------------------------------------ */ public static function get( $id ) { global $wpdb; $row = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', self::table(), $id ), ARRAY_A ); if ( ! $row ) { return null; } $row['id'] = (int) $row['id']; $rules = json_decode( (string) $row['rules'], true ); $row['rules'] = self::sanitize_rules( is_array( $rules ) ? $rules : array() ); return $row; } /** Segments read in this request (one query however many screens list them); reset on save and delete. */ private static $all = null; public static function all( $type = '' ) { global $wpdb; if ( null === self::$all ) { self::$all = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT id, name, type FROM %i ORDER BY name ASC', self::table() ), ARRAY_A ); } if ( ! $type ) { return self::$all; } return array_values( array_filter( self::$all, static function ( $s ) use ( $type ) { return $type === $s['type']; } ) ); } public static function save( array $data, $id = 0 ) { self::$all = null; if ( $id ) { delete_transient( 'nodebook_segcount_' . (int) $id ); } global $wpdb; $row = array( 'name' => mb_substr( sanitize_text_field( $data['name'] ?? '' ), 0, 190 ), 'type' => ( isset( $data['type'] ) && 'smart' === $data['type'] ) ? 'smart' : 'static', 'rules' => wp_json_encode( self::sanitize_rules( (array) ( $data['rules'] ?? array() ) ) ), 'updated_at' => current_time( 'mysql', true ), ); if ( '' === $row['name'] ) { $row['name'] = __( 'Untitled segment', 'nodebook' ); } if ( $id ) { $wpdb->update( self::table(), $row, array( 'id' => (int) $id ) ); return (int) $id; } $row['created_at'] = $row['updated_at']; $wpdb->insert( self::table(), $row ); return (int) $wpdb->insert_id; } public static function delete( $id ) { self::$all = null; if ( $id ) { delete_transient( 'nodebook_segcount_' . (int) $id ); } global $wpdb; $wpdb->delete( self::table(), array( 'id' => (int) $id ) ); $wpdb->delete( self::members_table(), array( 'segment_id' => (int) $id ) ); } public static function add_contacts( $segment_id, array $contact_ids ) { if ( $segment_id ) { delete_transient( 'nodebook_segcount_' . (int) $segment_id ); } global $wpdb; $now = current_time( 'mysql', true ); $n = 0; foreach ( array_filter( array_map( 'absint', $contact_ids ) ) as $cid ) { $n += (int) $wpdb->query( $wpdb->prepare( 'INSERT IGNORE INTO %i (segment_id, contact_id, added_at) VALUES (%d, %d, %s)', self::members_table(), $segment_id, $cid, $now ) ); } return $n; } public static function remove_contacts( $segment_id, array $contact_ids ) { if ( $segment_id ) { delete_transient( 'nodebook_segcount_' . (int) $segment_id ); } global $wpdb; $n = 0; foreach ( array_filter( array_map( 'absint', $contact_ids ) ) as $cid ) { $n += (int) $wpdb->delete( self::members_table(), array( 'segment_id' => (int) $segment_id, 'contact_id' => $cid, ) ); } return $n; } /** Static segments a contact belongs to. */ public static function for_contact( $contact_id ) { global $wpdb; return $wpdb->get_results( $wpdb->prepare( 'SELECT s.id, s.name FROM %i s INNER JOIN %i m ON m.segment_id = s.id WHERE m.contact_id = %d ORDER BY s.name ASC', self::table(), self::members_table(), $contact_id ), ARRAY_A ); } /** Count for overview screens: cached 10 minutes (smart segments over large timelines take a moment). */ public static function cached_count( $segment_id ) { $key = 'nodebook_segcount_' . (int) $segment_id; $count = get_transient( $key ); if ( false === $count ) { $count = self::count( $segment_id ); set_transient( $key, $count, 10 * MINUTE_IN_SECONDS ); } return (int) $count; } public static function count( $segment_id ) { $r = Nodebook_Contacts::query( array( 'segment' => $segment_id, 'per_page' => 1, ) ); return $r['total']; } /* ------------------------------------------------------------------ * * Rules → SQL * ------------------------------------------------------------------ */ /** * Keep only well-formed rules. * * Shape: { match: 'all'|'any', conditions: [ { field, op, value } ] } */ public static function sanitize_rules( array $rules ) { $fields = self::rule_fields(); $ops = self::operators(); $out = array( 'match' => ( isset( $rules['match'] ) && 'any' === $rules['match'] ) ? 'any' : 'all', 'conditions' => array(), ); foreach ( (array) ( $rules['conditions'] ?? array() ) as $c ) { if ( ! is_array( $c ) || empty( $c['field'] ) || ! isset( $fields[ $c['field'] ] ) ) { continue; } $kind = $fields[ $c['field'] ][1]; $op = isset( $c['op'] ) ? (string) $c['op'] : ''; if ( ! isset( $ops[ $kind ][ $op ] ) ) { continue; } $out['conditions'][] = array( 'field' => $c['field'], 'op' => $op, 'value' => mb_substr( sanitize_text_field( (string) ( $c['value'] ?? '' ) ), 0, 190 ), ); if ( count( $out['conditions'] ) >= 25 ) { break; } } return $out; } /** * Condition (see Nodebook_Sql; aliases c = contacts, co = companies) selecting a segment's contacts. * * @return array{0:string,1:array} */ public static function where( $segment_id ) { $seg = self::get( $segment_id ); if ( ! $seg ) { return Nodebook_Sql::none(); } if ( 'static' === $seg['type'] ) { return array( 'EXISTS (SELECT 1 FROM %i m WHERE m.contact_id = c.id AND m.segment_id = %d)', array( self::members_table(), (int) $seg['id'] ) ); } return self::rules_where( $seg['rules'] ); } /** Condition for a set of rules (see Nodebook_Sql). */ public static function rules_where( array $rules ) { $rules = self::sanitize_rules( $rules ); $parts = array(); foreach ( $rules['conditions'] as $c ) { $w = self::condition( $c ); if ( $w ) { $parts[] = $w; } } return $parts ? Nodebook_Sql::join( $parts, 'any' === $rules['match'] ) : Nodebook_Sql::all(); } /** * One rule → condition. Columns come from the rule-field whitelist (as %i identifiers); values are placeholders. * * @return array{0:string,1:array}|null */ private static function condition( array $c ) { global $wpdb; $fields = self::rule_fields(); if ( ! isset( $fields[ $c['field'] ] ) ) { return null; } list( , $kind, $column ) = $fields[ $c['field'] ]; $v = (string) $c['value']; if ( Nodebook_Props::is_key( $c['field'] ) ) { return self::prop_where( $c, $kind ); } $a = Nodebook_Install::table( 'activities' ); // Rule columns are "alias.column" (c.email, co.name): two %i placeholders each time they appear. $col = explode( '.', '' !== $column ? $column : 'c.id', 2 ); $col = array( '', 2 === count( $col ) ? $col : array( 'c', $col[0] ) ); $on = static function ( $tpl, array $values = array(), $times = 1 ) use ( $col ) { $args = array(); for ( $i = 0; $i < $times; $i++ ) { $args = array_merge( $args, $col[1] ); } return array( $tpl, array_merge( $args, $values ) ); }; $has = static function ( $sql, array $args, $yes ) { $w = array( $sql, $args ); return $yes ? $w : Nodebook_Sql::not( $w ); }; switch ( $kind ) { case 'text': switch ( $c['op'] ) { case 'is': return $on( "LOWER(IFNULL(%i.%i,'')) = LOWER(%s)", array( $v ) ); case 'is_not': return $on( "LOWER(IFNULL(%i.%i,'')) <> LOWER(%s)", array( $v ) ); case 'contains': return $on( "%i.%i LIKE %s", array( '%' . $wpdb->esc_like( $v ) . '%' ) ); case 'not_contains': return $on( "IFNULL(%i.%i,'') NOT LIKE %s", array( '%' . $wpdb->esc_like( $v ) . '%' ) ); case 'starts': return $on( "%i.%i LIKE %s", array( $wpdb->esc_like( $v ) . '%' ) ); case 'ends': return $on( "%i.%i LIKE %s", array( '%' . $wpdb->esc_like( $v ) ) ); case 'empty': return $on( "(%i.%i IS NULL OR %i.%i = '')", array(), 2 ); case 'not_empty': return $on( "(%i.%i IS NOT NULL AND %i.%i <> '')", array(), 2 ); } break; case 'status': return $on( 'is' === $c['op'] ? "%i.%i = %s" : "%i.%i <> %s", array( sanitize_key( $v ) ) ); case 'number': $map = array( 'eq' => "%i.%i = %f", 'gt' => "%i.%i > %f", 'gte' => "%i.%i >= %f", 'lt' => "%i.%i < %f", 'lte' => "%i.%i <= %f", ); return isset( $map[ $c['op'] ] ) ? $on( $map[ $c['op'] ], array( (float) $v ) ) : null; case 'date': switch ( $c['op'] ) { case 'within': return $on( "%i.%i >= %s", array( gmdate( 'Y-m-d H:i:s', time() - absint( $v ) * DAY_IN_SECONDS ) ) ); case 'older': return $on( "%i.%i < %s", array( gmdate( 'Y-m-d H:i:s', time() - absint( $v ) * DAY_IN_SECONDS ) ) ); case 'after': case 'before': if ( ! preg_match( '/^\d{4}-\d{2}-\d{2}$/', $v ) ) { return Nodebook_Sql::none(); } return 'after' === $c['op'] ? $on( "%i.%i > %s", array( $v . ' 23:59:59' ) ) : $on( "%i.%i < %s", array( $v . ' 00:00:00' ) ); case 'empty': return $on( "%i.%i IS NULL" ); case 'not_empty': return $on( "%i.%i IS NOT NULL" ); } break; case 'segment': return $has( 'EXISTS (SELECT 1 FROM %i m WHERE m.contact_id = c.id AND m.segment_id = %d)', array( self::members_table(), absint( $v ) ), 'in' === $c['op'] ); case 'activity': return $has( 'EXISTS (SELECT 1 FROM %i ax WHERE ax.contact_id = c.id AND ax.type = %s)', array( $a, sanitize_key( $v ) ), 'has' === $c['op'] ); case 'page': return $has( "EXISTS (SELECT 1 FROM %i ax WHERE ax.contact_id = c.id AND ax.type = 'pageview' AND ax.url LIKE %s)", array( $a, '%' . $wpdb->esc_like( $v ) . '%' ), 'contains' === $c['op'] ); case 'interest': // Resolve matching items first (few rows), then use the (contact_id, type) index. $types = array_values( (array) Nodebook_Interests::item_types() ); $ids = array(); if ( $types ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared -- one %s placeholder per post type. $ids = (array) $wpdb->get_col( $wpdb->prepare( 'SELECT ID FROM %i WHERE post_title LIKE %s AND post_type IN (' . Nodebook_Sql::list_of( count( $types ), '%s' ) . ') LIMIT 500', array_merge( array( $wpdb->posts, '%' . $wpdb->esc_like( $v ) . '%' ), $types ) ) ); } $ids = array_values( array_filter( array_map( 'absint', $ids ) ) ); if ( ! $ids ) { return 'contains' === $c['op'] ? Nodebook_Sql::none() : Nodebook_Sql::all(); } return $has( "EXISTS (SELECT 1 FROM %i ax WHERE ax.contact_id = c.id AND ax.type = 'pageview' AND ax.object_id IN (" . Nodebook_Sql::list_of( count( $ids ) ) . '))', array_merge( array( $a ), $ids ), 'contains' === $c['op'] ); case 'click': $needle = '"kind":' . wp_json_encode( sanitize_key( $v ) ); return $has( "EXISTS (SELECT 1 FROM %i ax WHERE ax.contact_id = c.id AND ax.type = 'click' AND ax.data LIKE %s)", array( $a, '%' . $wpdb->esc_like( $needle ) . '%' ), 'has' === $c['op'] ); case 'owner': // Value: user ID, 0 = unassigned, "me" = whoever views the segment. $uid = 'me' === $v ? get_current_user_id() : absint( $v ); return $has( 'c.owner_id = %d', array( $uid ), 'is' === $c['op'] ); case 'bool': return $on( 'yes' === $c['op'] ? "%i.%i = 1" : "%i.%i = 0" ); case 'emailstatus': $st = 'none' === $v ? '' : sanitize_key( $v ); return $on( 'is' === $c['op'] ? "%i.%i = %s" : "%i.%i <> %s", array( $st ) ); case 'event': if ( 'within' === $c['op'] ) { return array( "EXISTS (SELECT 1 FROM %i ax WHERE ax.contact_id = c.id AND ax.type = 'event' AND ax.object_id = %d AND ax.created_at >= %s)", array( $a, Nodebook_Events::id( $v ), gmdate( 'Y-m-d H:i:s', time() - 30 * DAY_IN_SECONDS ) ) ); } return $has( "EXISTS (SELECT 1 FROM %i ax WHERE ax.contact_id = c.id AND ax.type = 'event' AND ax.object_id = %d)", array( $a, Nodebook_Events::id( $v ) ), 'has' === $c['op'] ); case 'deal': // Value: open, won, lost, any, or stage: (an open deal in that stage). $yes = 'has' === $c['op']; $dt = Nodebook_Deals::table(); if ( 0 === strpos( $v, 'stage:' ) ) { return $has( "EXISTS (SELECT 1 FROM %i dx WHERE dx.contact_id = c.id AND dx.outcome = '' AND dx.stage = %s)", array( $dt, sanitize_key( substr( $v, 6 ) ) ), $yes ); } if ( 'open' === $v ) { return $has( "EXISTS (SELECT 1 FROM %i dx WHERE dx.contact_id = c.id AND dx.outcome = '')", array( $dt ), $yes ); } if ( in_array( $v, array( 'won', 'lost' ), true ) ) { return $has( 'EXISTS (SELECT 1 FROM %i dx WHERE dx.contact_id = c.id AND dx.outcome = %s)', array( $dt, $v ), $yes ); } return $has( 'EXISTS (SELECT 1 FROM %i dx WHERE dx.contact_id = c.id)', array( $dt ), $yes ); case 'product': // Order data is stored as JSON, so search for the JSON-encoded form of the name. $needle = trim( (string) wp_json_encode( $v ), '"' ); return $has( "EXISTS (SELECT 1 FROM %i ax WHERE ax.contact_id = c.id AND ax.type = 'order' AND ax.data LIKE %s)", array( $a, '%' . $wpdb->esc_like( $needle ) . '%' ), 'contains' === $c['op'] ); } return null; } }