A unifying approach for higher-order optimality conditions