get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', $table, absint( $id ) ) ); } /** * The ids, of those given, that name a group that exists. * * @param int[] $ids Group ids. * @return int[] */ public static function existing_ids( array $ids ) { $ids = array_values( array_unique( array_filter( array_map( 'absint', $ids ) ) ) ); if ( ! $ids ) { return array(); } global $wpdb; $table = Mailpuff_Schema::table( Mailpuff_Schema::LISTS ); $in = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- ids bound through the %d list. return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT id FROM %i WHERE id IN ($in)", array_merge( array( $table ), $ids ) ) ) ); } /** * A visibility value: public, or internal for anything else. * * @param mixed $value Raw value. * @return string */ public static function visibility( $value ) { return Mailpuff_Schema::GROUP_PUBLIC === $value ? Mailpuff_Schema::GROUP_PUBLIC : Mailpuff_Schema::GROUP_INTERNAL; } /** * Get all groups with contact counts. * * @return array Array of row objects with contact_count field. */ public static function all() { global $wpdb; $lists_table = Mailpuff_Schema::table( Mailpuff_Schema::LISTS ); $subscriber_lists_table = Mailpuff_Schema::table( Mailpuff_Schema::SUBSCRIBER_LISTS ); return $wpdb->get_results( $wpdb->prepare( 'SELECT g.*, COUNT( cg.contact_id ) AS contact_count FROM %i g LEFT JOIN %i cg ON cg.group_id = g.id GROUP BY g.id ORDER BY g.name ASC', $lists_table, $subscriber_lists_table ) ); } /** * Create a new group. * * @param array $data Column => value pairs. * @return int Insert ID. */ public static function create( $data ) { global $wpdb; $table = Mailpuff_Schema::table( Mailpuff_Schema::LISTS ); $data['created_at'] = current_time( 'mysql' ); $data['visibility'] = self::visibility( $data['visibility'] ?? '' ); $wpdb->insert( $table, $data ); return (int) $wpdb->insert_id; } /** * Update an existing group. * * @param int $id Group ID. * @param array $data Column => value pairs. * @return int|false Number of rows updated or false on error. */ public static function update( $id, $data ) { global $wpdb; $table = Mailpuff_Schema::table( Mailpuff_Schema::LISTS ); if ( isset( $data['visibility'] ) ) { $data['visibility'] = self::visibility( $data['visibility'] ); } return $wpdb->update( $table, $data, array( 'id' => absint( $id ) ), null, array( '%d' ) ); } /** * Delete a group by ID. * * Also removes all subscriber_lists associations for this group. * * @param int $id Group ID. * @return int|false Number of rows deleted or false on error. */ public static function delete( $id ) { global $wpdb; $table = Mailpuff_Schema::table( Mailpuff_Schema::LISTS ); $subscriber_lists_table = Mailpuff_Schema::table( Mailpuff_Schema::SUBSCRIBER_LISTS ); $wpdb->delete( $subscriber_lists_table, array( 'group_id' => absint( $id ) ), array( '%d' ) ); Mailpuff_Tag_Emails::forget( absint( $id ) ); return $wpdb->delete( $table, array( 'id' => absint( $id ) ), array( '%d' ) ); } }