get_var( $wpdb->prepare( 'SELECT id FROM %i WHERE email = %s', $table, $email ) ); if ( $existing ) { // Update profile only — deliberately DO NOT touch `status`. `customer_type` // and `gross_revenue` are written only when the caller supplies them, so an // event that carries just names (e.g. a re-install) can't wipe a value set // by an earlier event (e.g. a prior "paid"). $update = array( 'first_name' => $first, 'last_name' => $last, 'source' => static::SOURCE, 'updated_at' => $now, ); $formats = array( '%s', '%s', '%s', '%s' ); if ( $has_type ) { $update['customer_type'] = $type; $formats[] = '%s'; } if ( $has_rev ) { $update['gross_revenue'] = $gross; $formats[] = '%f'; } $wpdb->update( $table, $update, array( 'id' => (int) $existing ), $formats, array( '%d' ) ); return (int) $existing; } $status = Mailpuff_Schema::normalize_status( $data['status'] ?? '' ); $insert = array( 'email' => $email, 'first_name' => $first, 'last_name' => $last, 'status' => $status, 'source' => static::SOURCE, 'unsubscribe_token' => wp_generate_password( 32, false ), 'email_hash' => Mailpuff_Schema::email_hash( $email ), 'created_at' => $now, 'updated_at' => $now, ); $formats = array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ); if ( $has_type ) { $insert['customer_type'] = $type; $formats[] = '%s'; } if ( $has_rev ) { $insert['gross_revenue'] = $gross; $formats[] = '%f'; } $wpdb->insert( $table, $insert, $formats ); $new_id = (int) $wpdb->insert_id; if ( $new_id ) { do_action( 'mailpuff_contact_created', $new_id, static::SOURCE ); } return $new_id; } /** * Find a group by name (case-insensitive), creating it if absent. * * @param string $name Group name. * @return int Group ID (0 on failure). */ protected static function ensure_list( $name ) { foreach ( Mailpuff_Group::all() as $g ) { $gn = is_object( $g ) ? ( $g->name ?? '' ) : ( $g['name'] ?? '' ); if ( 0 === strcasecmp( $gn, $name ) ) { return is_object( $g ) ? (int) $g->id : (int) $g['id']; } } $created = Mailpuff_Group::create( array( 'name' => $name ) ); if ( is_object( $created ) ) { return (int) ( $created->id ?? 0 ); } if ( is_array( $created ) ) { return (int) ( $created['id'] ?? 0 ); } return (int) $created; } /** * Add a contact to a group (idempotent). * * @param int $contact_id Contact ID. * @param int $group_id Group ID. */ protected static function assign( $contact_id, $group_id ) { if ( ! $contact_id || ! $group_id ) { return; } global $wpdb; $table = Mailpuff_Schema::table( Mailpuff_Schema::SUBSCRIBER_LISTS ); $exists = $wpdb->get_var( $wpdb->prepare( 'SELECT 1 FROM %i WHERE contact_id = %d AND group_id = %d', $table, $contact_id, $group_id ) ); if ( ! $exists ) { $wpdb->insert( $table, array( 'contact_id' => $contact_id, 'group_id' => $group_id, 'added_at' => current_time( 'mysql' ), ), array( '%d', '%d', '%s' ) ); // Fires only on a NEW membership — drives the group-join automation // trigger. Re-running a sync (idempotent assign) does not re-fire. do_action( 'mailpuff_contact_added_to_group', (int) $contact_id, (int) $group_id ); } } /** * Schedule or clear a recurring cron based on an enabled flag. * * Reschedules when the recurrence changes so an install that was set up on * the old cadence (e.g. hourly) migrates to the new one (e.g. daily) without * a manual reset. * * @param string $hook Cron hook name. * @param bool $enabled Whether the source is enabled. * @param string $recurrence WP schedule slug (e.g. 'hourly', 'daily'). */ protected static function sync_cron( $hook, $enabled, $recurrence = 'hourly' ) { $scheduled = wp_next_scheduled( $hook ); if ( $enabled ) { if ( $scheduled && wp_get_schedule( $hook ) !== $recurrence ) { wp_clear_scheduled_hook( $hook ); $scheduled = false; } if ( ! $scheduled ) { wp_schedule_event( time() + 60, $recurrence, $hook ); } } elseif ( $scheduled ) { wp_clear_scheduled_hook( $hook ); } } }