get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', $table, absint( $id ) ) ); } /** * Find campaigns by status. * * @param string $status Campaign status. * @param int $limit Number of rows to return. * @param int $offset Offset for pagination. * @return array Array of row objects. */ public static function find_by_status( $status, $limit = 20, $offset = 0 ) { global $wpdb; $table = Mailpuff_Schema::table( Mailpuff_Schema::CAMPAIGNS ); return $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE status = %s ORDER BY created_at DESC LIMIT %d OFFSET %d', $table, sanitize_text_field( $status ), absint( $limit ), absint( $offset ) ) ); } /** * Count campaigns, optionally filtered by status. * * @param string $status Campaign status. Empty string for all. * @return int */ public static function count_by_status( $status = '' ) { global $wpdb; $table = Mailpuff_Schema::table( Mailpuff_Schema::CAMPAIGNS ); if ( '' !== $status ) { return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE status = %s', $table, sanitize_text_field( $status ) ) ); } return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $table ) ); } /** * Create a new campaign. * * @param array $data Column => value pairs. * @return int Insert ID. */ public static function create( $data ) { global $wpdb; $table = Mailpuff_Schema::table( Mailpuff_Schema::CAMPAIGNS ); $data['created_at'] = current_time( 'mysql' ); $data['updated_at'] = current_time( 'mysql' ); $wpdb->insert( $table, $data ); return (int) $wpdb->insert_id; } /** * Update an existing campaign. * * @param int $id Campaign 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::CAMPAIGNS ); $data['updated_at'] = current_time( 'mysql' ); return $wpdb->update( $table, $data, array( 'id' => absint( $id ) ), null, array( '%d' ) ); } /** * Delete a campaign by ID. * * @param int $id Campaign 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::CAMPAIGNS ); return $wpdb->delete( $table, array( 'id' => absint( $id ) ), array( '%d' ) ); } }